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())
36 delete this->places.takeLast();
38 while (!this->roadNames.isEmpty())
39 delete this->roadNames.takeLast();
41 while (!this->stops.isEmpty())
42 delete this->stops.takeLast();
46 void LocationFinder::processReply()
48 qDebug() << "Processing reply from Reittiopas in LocationFinder";
49 QXmlStreamReader xml(this->reply->readAll());
51 this->reply->disconnect(this);
52 this->reply->deleteLater();
58 qDebug() << "Reading next element";
61 if (xml.isStartElement())
63 QString xmlName(xml.name().toString());
67 QXmlStreamAttributes attributes(xml.attributes());
68 QStringRef xAttribute( attributes.value("x") );
69 QStringRef yAttribute( attributes.value("y") );
70 QString newX( xAttribute.toString() );
71 QString newY( yAttribute.toString() );
72 QString category(attributes.value("category").toString());
73 QString address(attributes.value("name1").toString());
74 QString number(attributes.value("number").toString());
75 if (!number.isEmpty())
78 address.append(number);
81 address.append(attributes.value("city").toString());
83 QString name("Result" + resultNumber++);
84 if (category == "poi")
86 this->places.append(new Location(newX, newY, name, address));
88 else if (category == "street")
90 this->roadNames.append(new Location(newX, newY, name, address));
92 else if (category == "stop")
94 this->stops.append(new Location(newX, newY, name, address));
99 QString errorMessage("Unknown category: ");
100 errorMessage.append(category);
101 qDebug() << errorMessage;
105 if (xmlName == "ERROR") {
106 this->invalidResponse = true;
112 qDebug() << xml.errorString();
113 if ( xml.hasError() ) {
114 qDebug() << "Invalid response received from Ytv";
115 this->invalidResponse = true;
117 int locationsFound = this->places.size() + this->roadNames.size() + this->stops.size();
118 qDebug() << "Number of locations received: " + locationsFound;
120 qDebug() << "Exiting xml parsing.";
125 bool LocationFinder::responseWasValid() const
127 return !this->invalidResponse;
130 bool LocationFinder::locationsFound() const
132 return (this->numberOfLocationsFound() > 0);
135 int LocationFinder::numberOfLocationsFound() const
137 return this->numberOfPlaces() + this->numberOfRoadNames() + this->numberOfStops();
140 int LocationFinder::numberOfPlaces() const
142 return this->places.size();
145 int LocationFinder::numberOfRoadNames() const
147 return this->roadNames.size();
150 int LocationFinder::numberOfStops() const
152 return this->stops.size();
155 Location* LocationFinder::getPlace(int index) const
157 if (index < 0 || index >= this->places.size())
158 throw std::invalid_argument("Given index out of bounds.");
160 return new Location(*(this->places.at(index)));
163 Location* LocationFinder::getRoadName(int index) const
165 if (index < 0 || index >= this->roadNames.size())
166 throw std::invalid_argument("Given index out of bounds.");
168 return new Location(*(this->roadNames.at(index)));
171 Location* LocationFinder::getStop(int index) const
173 if (index < 0 || index >= this->stops.size())
174 throw std::invalid_argument("Given index out of bounds.");
176 return new Location(*(this->stops.at(index)));
179 const QList<Location*>& LocationFinder::getPlaces() const
184 const QList<Location*>& LocationFinder::getRoadNames() const
186 return this->roadNames;
189 const QList<Location*>& LocationFinder::getStops() const