Created new ui for the program. Almost everything that worked previously
[ptas] / zouba / src / logic / locationfinder.cpp
1 #include "locationfinder.h"
2 #include "ytv.h"
3
4 #include <QString>
5 #include <QUrl>
6 #include <QXmlStreamReader>
7 #include <QDebug>
8 #include <QNetworkRequest>
9 #include <stdexcept>
10
11 #include <QTimer>
12
13 LocationFinder::LocationFinder(QString address, QObject *parent) :
14     QObject(parent), address(address), reply(NULL),
15     places(QList<Location*>()), roadNames(QList<Location*>()),
16     stops(QList<Location*>()), invalidResponse(false)
17 {
18     QUrl fullUrl(Ytv::Url);
19
20     fullUrl.addEncodedQueryItem( "key", this->address.toAscii().toPercentEncoding() );
21     fullUrl.addQueryItem( "user", Ytv::Username );
22     fullUrl.addQueryItem( "pass", Ytv::Password );
23
24     qDebug() << "The query url: " << fullUrl.toString();
25
26     this->reply = Ytv::manager.get(QNetworkRequest(fullUrl));
27     connect(this->reply, SIGNAL(finished()), this, SLOT(processReply()));
28 }
29
30 LocationFinder::~LocationFinder()
31 {
32     if (this->reply)
33         this->reply->deleteLater();
34
35     while (!this->places.isEmpty())
36     {
37         Location *loc = this->places.takeLast();
38         delete loc;
39     }
40     while (!this->roadNames.isEmpty())
41     {
42         Location *loc = this->roadNames.takeLast();
43         delete loc;
44     }
45     while (!this->stops.isEmpty())
46     {
47         Location *loc = this->stops.takeLast();
48         delete loc;
49     }
50 }
51
52
53 void LocationFinder::processReply()
54 {
55     qDebug() << "Processing reply from Reittiopas in LocationFinder";
56     QXmlStreamReader xml(this->reply->readAll());
57
58     this->reply->disconnect(this, SLOT(searchFinished()));
59     this->reply->deleteLater();
60     this->reply = NULL;
61
62     while (!xml.atEnd())
63     {
64         qDebug() << "Reading next element";
65         xml.readNext();
66
67         if (xml.isStartElement())
68         {
69             QString xmlName(xml.name().toString());
70
71             if (xmlName == "LOC")
72             {
73                 QXmlStreamAttributes attributes(xml.attributes());
74                 QStringRef xAttribute( attributes.value("x") );
75                 QStringRef yAttribute( attributes.value("y") );
76                 QString newX( xAttribute.toString() );
77                 QString newY( yAttribute.toString() );
78                 QString category(attributes.value("category").toString());
79                 QString name(attributes.value("name1").toString());
80                 QString number(attributes.value("number").toString());
81                 if (!number.isEmpty())
82                 {
83                     name.append(" ");
84                     name.append(number);
85                 }
86                 name.append(", ");
87                 name.append(attributes.value("city").toString());
88
89                 if (category == "poi")
90                 {
91                     this->places.append(new Location(newX, newY, name));
92                 }
93                 else if (category == "street")
94                 {
95                     this->roadNames.append(new Location(newX, newY, name));
96                 }
97                 else if (category == "stop")
98                 {
99                     this->stops.append(new Location(newX, newY, name));
100                 }
101                 else
102                 {
103                     QString errorMessage("Unknown category: ");
104                     errorMessage.append(category);
105                     qDebug() << errorMessage;
106                 }
107             }
108
109             if (xmlName == "ERROR") {
110                 this->invalidResponse = true;
111             }
112
113         }
114     }
115
116     qDebug() << xml.errorString();
117     if ( xml.hasError() ) {
118         qDebug() << "Invalid response received from Ytv";
119         this->invalidResponse = true;
120     } else {
121         int locationsFound = this->places.size() + this->roadNames.size() + this->stops.size();
122         qDebug() << "Number of locations received: " + locationsFound;
123     }
124     qDebug() << "Exiting xml parsing.";
125
126     emit(finished());
127 }
128
129 bool LocationFinder::responseWasValid() const
130 {
131     return !this->invalidResponse;
132 }
133
134 bool LocationFinder::locationsFound() const
135 {
136     return (this->numberOfLocationsFound() > 0);
137 }
138
139 int LocationFinder::numberOfLocationsFound() const
140 {
141     return this->numberOfPlaces() + this->numberOfRoadNames() + this->numberOfStops();
142 }
143
144 int LocationFinder::numberOfPlaces() const
145 {
146     return this->places.size();
147 }
148
149 int LocationFinder::numberOfRoadNames() const
150 {
151     return this->roadNames.size();
152 }
153
154 int LocationFinder::numberOfStops() const
155 {
156     return this->stops.size();
157 }
158
159 Location* LocationFinder::getPlace(int index) const
160 {
161     if (index < 0 || index >= this->places.size())
162         throw std::invalid_argument("Given index out of bounds.");
163
164     return new Location(*(this->places.at(index)));
165 }
166
167 Location* LocationFinder::getRoadName(int index) const
168 {
169     if (index < 0 || index >= this->roadNames.size())
170         throw std::invalid_argument("Given index out of bounds.");
171
172     return new Location(*(this->roadNames.at(index)));
173 }
174
175 Location* LocationFinder::getStop(int index) const
176 {
177     if (index < 0 || index >= this->stops.size())
178         throw std::invalid_argument("Given index out of bounds.");
179
180     return new Location(*(this->stops.at(index)));
181 }
182
183 const QList<Location*>& LocationFinder::getPlaces() const
184 {
185     return this->places;
186 }
187
188 const QList<Location*>& LocationFinder::getRoadNames() const
189 {
190     return this->roadNames;
191 }
192
193 const QList<Location*>& LocationFinder::getStops() const
194 {
195     return this->stops;
196 }