Incorporated changes from bus project.
[ptas] / src / location.cpp
1 #include "location.h"
2
3 #include "location_p.h"
4
5 #include "ytv.h"
6
7 #include <QString>
8 #include <QObject>
9 #include <QNetworkAccessManager>
10 #include <QUrl>
11 #include <QNetworkRequest>
12 #include <QNetworkReply>
13 #include <QXmlStreamReader>
14 #include <QDebug>
15 #include <QXmlStreamAttributes>
16 #include <QStringRef>
17 #include <QGeoPositionInfo>
18
19 QTM_USE_NAMESPACE
20
21 Location::Location(const QString &x, const QString &y, const QString &label) :
22     q(new LocationPrivate(x, y, label)),
23     manager(new QNetworkAccessManager(this))
24 {
25     connect(
26                 manager, SIGNAL(finished(QNetworkReply*)),
27                 this, SLOT(replyFinished(QNetworkReply*))
28                 );
29 }
30
31 Location::Location(const QGeoPositionInfo &positionInfo, const QString &label) :
32     q(new LocationPrivate(label)),
33     manager(0)
34 {
35     setLocation(positionInfo);
36 }
37
38 void Location::setLocation(const QGeoPositionInfo &positionInfo)
39 {
40     double latitude = positionInfo.coordinate().latitude();
41     double longitude = positionInfo.coordinate().longitude();
42
43     qDebug() << "GPS Location/" << longitude << latitude;
44
45     q->setLongitude(longitude);
46     q->setlatitude(latitude);
47     q->setValid(true);
48 }
49
50 Location::Location(const Location &from) :
51     QObject(0),
52     q(new LocationPrivate(from.label())),
53     manager(0)
54 {
55     q->setAddress(from.address());
56     q->setLongitude(from.longitude());
57     q->setlatitude(from.latitude());
58     q->setValid(from.isValid());
59     if (from.manager != 0) {
60         manager = new QNetworkAccessManager(this);
61         connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
62     }
63 }
64
65 Location::Location(const QString &label) :
66     q(new LocationPrivate(label)),
67     manager(new QNetworkAccessManager(this))
68 {
69     connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
70 }
71
72 Location::~Location()
73 {
74     delete q;
75     q=0;
76     delete manager;
77     manager=0;
78 }
79
80 Location &Location::operator=(const Location &from)
81 {
82     q = new LocationPrivate(from.label());
83     q->setAddress(from.address());
84     q->setLongitude(from.longitude());
85     q->setlatitude(from.latitude());
86     q->setValid(from.isValid());
87
88     if (from.manager != 0) {
89         manager = new QNetworkAccessManager(this);
90         connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
91     } else {
92         manager = 0;
93     }
94
95     return *this;
96 }
97
98 void Location::resolveAddress(const QString &address)
99 {
100     qDebug() << "resolving address (" << address << ")";
101
102     q->setAddress(address);
103     q->setValid(false);
104
105     QUrl fullUrl(Ytv::Url);
106     QByteArray encodedAddress = QUrl::toPercentEncoding(address);
107     qDebug() << "encodedAddress/" << encodedAddress;
108
109     fullUrl.addQueryItem("user", Ytv::Username);
110     fullUrl.addQueryItem("pass", Ytv::Password);
111     fullUrl.addEncodedQueryItem("key", encodedAddress);
112     fullUrl.addQueryItem("request", Ytv::Geocode);
113     fullUrl.addQueryItem("format", Ytv::Xml);
114     fullUrl.addQueryItem("language", Ytv::English);
115     fullUrl.addQueryItem("epsg_out", Ytv::WGS84);
116
117     qDebug() << "resolveAddress/" << address << "/" << fullUrl;
118     manager->get(QNetworkRequest(fullUrl));
119     qDebug() << "waiting for reply from Ytv";
120     emit busy(true);
121 }
122
123 void Location::replyFinished(QNetworkReply * reply)
124 {
125     qDebug() << "address resolved";
126     q->parseReply(reply->readAll());
127
128     if (isValid()) {
129         qDebug() << label() << "becomeValid";
130         emit becomeValid();
131     } else {
132         qDebug() << label() << "not valid";
133         emit becomeInValid();
134     }
135
136     emit(busy(false));
137 }
138
139 QString Location::longitude() const
140 {
141     return q->longitude();
142 }
143
144 QString Location::latitude() const
145 {
146     return q->latitude();
147 }
148
149 void Location::setLabel(const QString &label) const
150 {
151     q->setLabel(label);
152 }
153
154 void Location::setValid(bool valid) const
155 {
156     q->setValid(valid);
157 }
158
159 QString Location::label() const
160 {
161     return q->label();
162 }
163
164 void Location::setAddress(const QString &address) const
165 {
166     qDebug() << "setting address to" << address;
167     q->setAddress(address);
168 }
169
170 QString Location::address() const
171 {
172     return q->address();
173 }
174
175 bool Location::isValid() const
176 {
177     return q->isValid();
178 }