1 #include "locationfinder.h"
6 #include <QXmlStreamReader>
8 #include <QNetworkRequest>
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)
18 QUrl fullUrl(Ytv::Url);
20 fullUrl.addEncodedQueryItem( "key", this->address.toAscii().toPercentEncoding() );
21 fullUrl.addQueryItem( "user", Ytv::Username );
22 fullUrl.addQueryItem( "pass", Ytv::Password );
24 qDebug() << "The query url: " << fullUrl.toString();
26 this->reply = Ytv::manager.get(QNetworkRequest(fullUrl));
27 connect(this->reply, SIGNAL(finished()), this, SLOT(processReply()));
30 LocationFinder::~LocationFinder()
33 this->reply->deleteLater();
35 while (!this->places.isEmpty())
37 Location *loc = this->places.takeLast();
40 while (!this->roadNames.isEmpty())
42 Location *loc = this->roadNames.takeLast();
45 while (!this->stops.isEmpty())
47 Location *loc = this->stops.takeLast();
53 void LocationFinder::processReply()
55 qDebug() << "Processing reply from Reittiopas in LocationFinder";
56 QXmlStreamReader xml(this->reply->readAll());
58 this->reply->disconnect(this, SLOT(searchFinished()));
59 this->reply->deleteLater();
64 qDebug() << "Reading next element";
67 if (xml.isStartElement())
69 QString xmlName(xml.name().toString());
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())
87 name.append(attributes.value("city").toString());
89 if (category == "poi")
91 this->places.append(new Location(newX, newY, name));
93 else if (category == "street")
95 this->roadNames.append(new Location(newX, newY, name));
97 else if (category == "stop")
99 this->stops.append(new Location(newX, newY, name));
103 QString errorMessage("Unknown category: ");
104 errorMessage.append(category);
105 qDebug() << errorMessage;
109 if (xmlName == "ERROR") {
110 this->invalidResponse = true;
116 qDebug() << xml.errorString();
117 if ( xml.hasError() ) {
118 qDebug() << "Invalid response received from Ytv";
119 this->invalidResponse = true;
121 int locationsFound = this->places.size() + this->roadNames.size() + this->stops.size();
122 qDebug() << "Number of locations received: " + locationsFound;
124 qDebug() << "Exiting xml parsing.";
129 bool LocationFinder::responseWasValid() const
131 return !this->invalidResponse;
134 bool LocationFinder::locationsFound() const
136 return (this->numberOfLocationsFound() > 0);
139 int LocationFinder::numberOfLocationsFound() const
141 return this->numberOfPlaces() + this->numberOfRoadNames() + this->numberOfStops();
144 int LocationFinder::numberOfPlaces() const
146 return this->places.size();
149 int LocationFinder::numberOfRoadNames() const
151 return this->roadNames.size();
154 int LocationFinder::numberOfStops() const
156 return this->stops.size();
159 Location* LocationFinder::getPlace(int index) const
161 if (index < 0 || index >= this->places.size())
162 throw std::invalid_argument("Given index out of bounds.");
164 return new Location(*(this->places.at(index)));
167 Location* LocationFinder::getRoadName(int index) const
169 if (index < 0 || index >= this->roadNames.size())
170 throw std::invalid_argument("Given index out of bounds.");
172 return new Location(*(this->roadNames.at(index)));
175 Location* LocationFinder::getStop(int index) const
177 if (index < 0 || index >= this->stops.size())
178 throw std::invalid_argument("Given index out of bounds.");
180 return new Location(*(this->stops.at(index)));
183 const QList<Location*>& LocationFinder::getPlaces() const
188 const QList<Location*>& LocationFinder::getRoadNames() const
190 return this->roadNames;
193 const QList<Location*>& LocationFinder::getStops() const