2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
6 Sami Rämö - sami.ramo@ixonos.com
8 Situare is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 Situare is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Situare; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <QStringList>
26 #include <QNetworkReply>
27 #include <QCryptographicHash>
29 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
30 #include <QDBusInterface>
31 #include <QDBusMessage>
35 #include "coordinates/geocoordinate.h"
38 #include "network/networkaccessmanager.h"
40 #include "routesegment.h"
42 #include "routingservice.h"
44 // defines for IMEI retrieval via dbus
45 #define SIM_DBUS_SERVICE "com.nokia.phone.SIM"
46 #define SIM_DBUS_PATH "/com/nokia/phone/SIM/security"
47 #define SIM_DBUS_IFACE "Phone.Sim.Security"
48 #define SIM_IMEI_REQ "get_imei"
50 const int NO_ERROR = 0;
53 const QString CLOUDMADE_AUTH_PART = "http://auth.cloudmade.com/token/";
54 const QString CLOUDMADE_API_KEY = "0cea0072adbf42ce823f4c78b974f12d";
57 const QString GEOCODE = "http://maps.google.com/maps/api/geocode/json?address=";
60 RoutingService::RoutingService(QObject *parent)
63 qDebug() << __PRETTY_FUNCTION__;
65 m_pendingRequest.clear();
68 m_networkManager = new NetworkAccessManager(this);
69 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
70 this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
74 void RoutingService::parseRouteData(const QByteArray &jsonReply)
76 qDebug() << __PRETTY_FUNCTION__;
80 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
82 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
85 if(result.value("status").toInt() == NO_ERROR) {
86 QVariant routeSummary = result.value("route_summary");
87 QMap<QString, QVariant> routeSummaryMap = routeSummary.toMap();
90 route.setEndPointName(routeSummaryMap["end_point"].toString());
91 route.setStartPointName(routeSummaryMap["start_point"].toString());
92 route.setTotalDistance(routeSummaryMap["total_distance"].toInt());
93 route.setTotalTime(routeSummaryMap["total_time"].toInt());
95 foreach(QVariant routeGeometry, result["route_geometry"].toList()) {
96 QStringList list = routeGeometry.toStringList();
97 route.appendGeometryPoint(GeoCoordinate(list.at(0).toDouble(),
98 list.at(1).toDouble()));
101 foreach(QVariant routeInstructions, result["route_instructions"].toList()) {
102 QStringList list = routeInstructions.toStringList();
103 RouteSegment segment;
104 segment.setInstruction(list.at(0));
105 segment.setLength(list.at(1).toDouble());
106 segment.setPositionIndex(list.at(2).toInt());
107 segment.setTime(list.at(3).toInt());
108 segment.setLengthCaption(list.at(4));
109 segment.setEarthDirection(list.at(5));
110 segment.setAzimuth(list.at(6).toDouble());
111 if (list.count() == 8) {
112 segment.setTurnType(list.at(7));
113 segment.setTurnAngle(list.at(8).toDouble());
115 route.appendSegment(segment);
118 emit routeParsed(route);
123 void RoutingService::parseSearchResults(const QByteArray &jsonReply)
125 qDebug() << __PRETTY_FUNCTION__;
127 const QString NO_DATA = "ZERO_RESULTS";
129 m_searchResults.clear();
131 QJson::Parser parser;
133 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
135 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
138 if(result.value("status") != NO_DATA) {
139 foreach(QVariant hitVariant, result["results"].toList()) {
141 QMap<QString, QVariant> hitMap = hitVariant.toMap();
143 location.setFormattedAddress(hitMap["formatted_address"].toString());
145 QVariant geometryVariant = hitMap["geometry"];
146 QMap<QString, QVariant> geometryMap = geometryVariant.toMap();
147 QVariant locationVariant = geometryMap["location"];
148 QMap<QString, QVariant> locationMap = locationVariant.toMap();
149 location.setCoordinates(locationMap["lat"].toDouble(),
150 locationMap["lng"].toDouble());
152 QVariant boundsVariant = geometryMap["bounds"];
153 QMap<QString, QVariant> boundsMap = boundsVariant.toMap();
154 QVariant swVariant = boundsMap["southwest"];
155 QMap<QString, QVariant> swMap = swVariant.toMap();
156 location.setSWBound(swMap["lat"].toDouble(),
157 swMap["lng"].toDouble());
158 QVariant neVariant = boundsMap["northeast"];
159 QMap<QString, QVariant> neMap = neVariant.toMap();
160 location.setNEBound(neMap["lat"].toDouble(),
161 neMap["lng"].toDouble());
163 m_searchResults.append(location);
165 emit locationDataParsed(m_searchResults);
170 void RoutingService::requestAuthorizationToken()
172 qDebug() << __PRETTY_FUNCTION__;
174 // create token request of format: http://auth.cloudmade.com/token/APIKEY?userid=UserID
175 // where APIKEY is our application specific key and userID is md5 hashed value of IMEI code
176 QString url = CLOUDMADE_AUTH_PART;
177 url.append(CLOUDMADE_API_KEY);
178 QString uidPart = "?userid=";
181 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
182 // get the device IMEI code
183 QDBusInterface interface(SIM_DBUS_SERVICE, SIM_DBUS_PATH, SIM_DBUS_IFACE,
184 QDBusConnection::systemBus());
186 QDBusMessage reply = interface.call(SIM_IMEI_REQ);
187 if (reply.type() == QDBusMessage::ErrorMessage)
188 qDebug() << reply.errorMessage();
190 QList<QVariant> args = reply.arguments();
191 qDebug() << QString("Phone's IMEI: %1").arg(args.at(0).toString());
192 rawData = args.at(0).toByteArray();
195 // for desktop we need to use login name
196 rawData.append(system("whoami"));
199 QCryptographicHash crypto(QCryptographicHash::Md5);
200 crypto.addData(rawData);
201 uidPart.append(crypto.result().toHex());
207 void RoutingService::requestFinished(QNetworkReply *reply)
209 qDebug() << __PRETTY_FUNCTION__;
211 if (m_currentRequests.contains(reply)) {
213 QByteArray replyArray = reply->readAll();
214 qDebug() << "Reply from: " << reply->url() << "reply " << replyArray;
216 if (reply->error()) {
217 emit error(ErrorContext::NETWORK, reply->error());
218 } else if(reply->url().toString().contains(CLOUDMADE_AUTH_PART)) {
219 m_token = replyArray;
220 m_pendingRequest.append(m_token);
221 sendRequest(m_pendingRequest);
222 m_pendingRequest.clear();
223 } else if(reply->url().toString().contains(GEOCODE)) {
224 parseSearchResults(replyArray);
226 parseRouteData(replyArray);
229 m_currentRequests.removeAll(reply);
230 reply->deleteLater();
234 void RoutingService::requestLocation(QString location)
236 qDebug() << __PRETTY_FUNCTION__;
238 const QString urlEnd = "&sensor=false";
240 QString url = GEOCODE;
241 url.append(location);
247 void RoutingService::requestRoute(GeoCoordinate from, GeoCoordinate to)
249 qDebug() << __PRETTY_FUNCTION__;
251 QString url = "http://routes.cloudmade.com/";
252 url.append(CLOUDMADE_API_KEY);
253 url.append("/api/0.3/");
254 url.append(QString::number(from.latitude()) + "," + QString::number(from.longitude()) + ",");
255 url.append(QString::number(to.latitude()) + "," + QString::number(to.longitude()));
256 url.append("/car/fastest.js?lang=en&units=km&token=");
258 if(m_token.isEmpty()) {
259 m_pendingRequest.append(url);
260 requestAuthorizationToken();
263 sendRequest(QUrl(url));
267 void RoutingService::sendRequest(const QUrl &url)
269 qDebug() << __PRETTY_FUNCTION__;
271 QNetworkRequest request;
274 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
277 QNetworkReply *reply = m_networkManager->post(request, ba, false);
279 m_currentRequests.append(reply);