1 | /* |
2 | * Copyright (C) 2012 Cutehacks AS. All rights reserved. |
3 | * info@cutehacks.com |
4 | * http://cutehacks.com |
5 | * |
6 | * This file is part of Fly. |
7 | * |
8 | * Fly is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * Fly is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with Fly. If not, see <http://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | #include "airportnames.h" |
23 | #include "node.h" |
24 | #include <QStack> |
25 | |
26 | static const QString ch_avinorAirports[] = { |
27 | "Alta (ALF)" , |
28 | "Andøya (ANX)" , |
29 | "Bardufoss (BDU)" , |
30 | "Bergen (BGO)" , |
31 | "Berlevåg (BVG)" , |
32 | "Bodø (BOO)" , |
33 | "Brønnøysunn (BNN)" , |
34 | "Båtsfjord (BJF)" , |
35 | "Fagernes (VDB)" , |
36 | "Florø (FRO)" , |
37 | "Førde (FDE)" , |
38 | "Hammerfest (HFT)" , |
39 | "Harstad/Narvik (EVE)" , |
40 | "Hasvik (HAA)" , |
41 | "Haugesund (HAU)" , |
42 | "Honningsvåg (HVG)" , |
43 | "Kirkenes (KKN)" , |
44 | "Kristiansand (KRS)" , |
45 | "Kristiansund (KSU)" , |
46 | "Lakselv (LKL)" , |
47 | "Leknes (LKN)" , |
48 | "Mehamn (MEH)" , |
49 | "Mo i Rana (MQN)" , |
50 | "Molde (MOL)" , |
51 | "Mosjøen (MJF)" , |
52 | "Namsos (OSY)" , |
53 | "Narvik (NVK)" , |
54 | "Oslo (OSL)" , |
55 | "Røros (RRS)" , |
56 | "Rørvik (RVK)" , |
57 | "Røst (RET)" , |
58 | "Sandane (SDN)" , |
59 | "Sandnessjøen (SSJ)" , |
60 | "Sogndal (SOG)" , |
61 | "Stavanger (SVG)" , |
62 | "Stokmarknes (SKN)" , |
63 | "Svalbard (LYR)" , |
64 | "Svolvær (SVJ)" , |
65 | "Sørkjosen (SOJ)" , |
66 | "Tromsø (TOS)" , |
67 | "Trondheim (TRD)" , |
68 | "Vadsø (VDS)" , |
69 | "Vardø (VAW)" , |
70 | "Værøy (VRY)" , |
71 | "Ørsta-Volda (HOV)" , |
72 | "Ålesund (AES)" |
73 | }; |
74 | |
75 | AirportNames::AirportNames(QObject *parent) |
76 | : XmlDataSource(parent) |
77 | { |
78 | for (int i = 0; i < 46; ++i) |
79 | m_airports.append(ch_avinorAirports[i]); |
80 | setUrl(QUrl("http://flydata.avinor.no/airportNames.asp" )); |
81 | setRefreshRate(86400000); //msecs == 24h |
82 | QObject::connect(this, SIGNAL(dataUpdated(Node*)), this, SLOT(createMapping(Node*))); |
83 | } |
84 | |
85 | AirportNames::~AirportNames() |
86 | { |
87 | } |
88 | |
89 | QStringList AirportNames::avinorAirports() const |
90 | { |
91 | return m_airports; |
92 | } |
93 | |
94 | QString AirportNames::avinorAirportCodeAt(int index) const |
95 | { |
96 | QString airport = m_airports.value(index); |
97 | return airport.mid(airport.length() - 4, 3); |
98 | } |
99 | |
100 | QString AirportNames::airportName(const QString &code) const |
101 | { |
102 | return m_mapping.value(code, code); |
103 | } |
104 | |
105 | void AirportNames::createMapping(Node *root) |
106 | { |
107 | m_mapping.clear(); |
108 | QStack<Node*> pending; |
109 | pending.push(root); |
110 | while (!pending.isEmpty()) { |
111 | Node *node = pending.pop(); |
112 | if (node->type() == "airportName" ) |
113 | m_mapping.insert(node->attribute("code" ), node->attribute("name" )); |
114 | for (int i = 0; i < node->childCount(); ++i) |
115 | pending.push(node->child(i)); |
116 | } |
117 | } |
118 | |