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 "flightstatuses.h" |
23 | #include "node.h" |
24 | #include <QStack> |
25 | |
26 | FlightStatuses::FlightStatuses(QObject *parent) |
27 | : XmlDataSource(parent) |
28 | { |
29 | // use http://flydata.avinor.no/flightStatuses.asp?code=A for specific statuses |
30 | setUrl(QUrl("http://flydata.avinor.no/flightStatuses.asp" )); |
31 | setRefreshRate(86400000); //msecs == 24h |
32 | QObject::connect(this, SIGNAL(dataUpdated(Node*)), this, SLOT(createMapping(Node*))); |
33 | } |
34 | |
35 | FlightStatuses::~FlightStatuses() |
36 | { |
37 | } |
38 | |
39 | FlightStatuses::StatusCode FlightStatuses::statusCode() const |
40 | { |
41 | return m_statusCode; |
42 | } |
43 | |
44 | void FlightStatuses::setStatusCode(StatusCode code) |
45 | { |
46 | m_statusCode = code; |
47 | insertQuery("code" , toString(code)); |
48 | emit statusCodeChanged(); |
49 | } |
50 | |
51 | QString FlightStatuses::statusCodeString() const |
52 | { |
53 | return toString(statusCode()); |
54 | } |
55 | |
56 | void FlightStatuses::setStatusCodeString(const QString &code) |
57 | { |
58 | setStatusCode(fromString(code)); |
59 | } |
60 | |
61 | QString FlightStatuses::statusTextEnglish(const QString &code) const |
62 | { |
63 | return m_mappingEnglish.value(code, code); |
64 | } |
65 | |
66 | QString FlightStatuses::statusTextNorwegian(const QString &code) const |
67 | { |
68 | return m_mappingNorwegian.value(code, code); |
69 | } |
70 | |
71 | void FlightStatuses::createMapping(Node *root) |
72 | { |
73 | m_mappingEnglish.clear(); |
74 | m_mappingNorwegian.clear(); |
75 | QStack<Node*> pending; |
76 | pending.push(root); |
77 | while (!pending.isEmpty()) { |
78 | Node *node = pending.pop(); |
79 | if (node->type() == "flightStatus" ) { |
80 | const QString code = node->attribute("code" ); |
81 | m_mappingEnglish.insert(code, node->attribute("statusTextEn" )); |
82 | m_mappingNorwegian.insert(code, node->attribute("statusTextNo" )); |
83 | } |
84 | for (int i = 0; i < node->childCount(); ++i) |
85 | pending.push(node->child(i)); |
86 | } |
87 | } |
88 | |
89 | QString FlightStatuses::toString(StatusCode code) |
90 | { |
91 | switch (code) { |
92 | case NewInfo: return "N" ; |
93 | case NewTime: return "E" ; |
94 | case Departed: return "D" ; |
95 | case Arrived: return "A" ; |
96 | case Canceled: return "C" ; |
97 | case None: |
98 | default: |
99 | break; |
100 | } |
101 | return QString(); |
102 | } |
103 | |
104 | FlightStatuses::StatusCode FlightStatuses::fromString(const QString &code) |
105 | { |
106 | if (code == "N" ) |
107 | return NewInfo; |
108 | if (code == "E" ) |
109 | return NewTime; |
110 | if (code == "D" ) |
111 | return Departed; |
112 | if (code == "A" ) |
113 | return Arrived; |
114 | if (code == "C" ) |
115 | return Canceled; |
116 | return None; |
117 | } |
118 | |