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"
36 #include "network/networkaccessmanager.h"
39 #include "routesegment.h"
41 #include "routingservice.h"
43 // defines for IMEI retrieval via dbus
44 #define SIM_DBUS_SERVICE "com.nokia.phone.SIM"
45 #define SIM_DBUS_PATH "/com/nokia/phone/SIM/security"
46 #define SIM_DBUS_IFACE "Phone.Sim.Security"
47 #define SIM_IMEI_REQ "get_imei"
49 const int NO_ERROR = 0;
52 const QString CLOUDMADE_AUTH_PART = "http://auth.cloudmade.com/token/";
53 const QString CLOUDMADE_API_KEY = "0cea0072adbf42ce823f4c78b974f12d";
56 RoutingService::RoutingService(QObject *parent)
59 qDebug() << __PRETTY_FUNCTION__;
61 m_pendingRequest.clear();
64 m_networkManager = new NetworkAccessManager(this);
65 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
66 this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
70 void RoutingService::parseRouteData(const QByteArray &jsonReply)
72 qDebug() << __PRETTY_FUNCTION__;
76 QVariantMap result = parser.parse (jsonReply, &ok).toMap();
78 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
81 if(result.value("status").toInt() == NO_ERROR) {
82 QVariant routeSummary = result.value("route_summary");
83 QMap<QString, QVariant> routeSummaryMap = routeSummary.toMap();
86 route.setEndPointName(routeSummaryMap["end_point"].toString());
87 route.setStartPointName(routeSummaryMap["start_point"].toString());
88 route.setTotalDistance(routeSummaryMap["total_distance"].toInt());
89 route.setTotalTime(routeSummaryMap["total_time"].toInt());
91 foreach(QVariant routeGeometry, result["route_geometry"].toList()) {
92 QStringList list = routeGeometry.toStringList();
93 route.appendGeometryPoint(GeoCoordinate(list.at(0).toDouble(),
94 list.at(1).toDouble()));
97 foreach(QVariant routeInstructions, result["route_instructions"].toList()) {
98 QStringList list = routeInstructions.toStringList();
100 segment.setInstruction(list.at(0));
101 segment.setLength(list.at(1).toDouble());
102 segment.setPositionIndex(list.at(2).toInt());
103 segment.setTime(list.at(3).toInt());
104 segment.setLengthCaption(list.at(4));
105 segment.setEarthDirection(list.at(5));
106 segment.setAzimuth(list.at(6).toDouble());
107 if (list.count() == 8) {
108 segment.setTurnType(list.at(7));
109 segment.setTurnAngle(list.at(8).toDouble());
111 route.appendSegment(segment);
114 emit routeParsed(route);
119 void RoutingService::requestAuthorizationToken()
121 qDebug() << __PRETTY_FUNCTION__;
123 // create token request of format: http://auth.cloudmade.com/token/APIKEY?userid=UserID
124 // where APIKEY is our application specific key and userID is md5 hashed value of IMEI code
125 QString url = CLOUDMADE_AUTH_PART;
126 url.append(CLOUDMADE_API_KEY);
127 QString uidPart = "?userid=";
130 #if defined(Q_WS_MAEMO_5) & defined(ARMEL)
131 // get the device IMEI code
132 QDBusInterface interface(SIM_DBUS_SERVICE, SIM_DBUS_PATH, SIM_DBUS_IFACE,
133 QDBusConnection::systemBus());
135 QDBusMessage reply = interface.call(SIM_IMEI_REQ);
136 if (reply.type() == QDBusMessage::ErrorMessage)
137 qDebug() << reply.errorMessage();
139 QList<QVariant> args = reply.arguments();
140 qDebug() << QString("Phone's IMEI: %1").arg(args.at(0).toString());
141 rawData = args.at(0).toByteArray();
144 // for desktop we need to use login name
145 rawData.append(system("whoami"));
148 QCryptographicHash crypto(QCryptographicHash::Md5);
149 crypto.addData(rawData);
150 uidPart.append(crypto.result().toHex());
156 void RoutingService::requestFinished(QNetworkReply *reply)
158 qDebug() << __PRETTY_FUNCTION__;
160 if (m_currentRequests.contains(reply)) {
162 QByteArray replyArray = reply->readAll();
163 qDebug() << "Reply from: " << reply->url() << "reply " << replyArray;
165 if (reply->error()) {
166 emit error(ErrorContext::NETWORK, reply->error());
167 } else if(reply->url().toString().contains(CLOUDMADE_AUTH_PART)) {
168 m_token = replyArray;
169 m_pendingRequest.append(m_token);
170 sendRequest(m_pendingRequest);
171 m_pendingRequest.clear();
173 parseRouteData(replyArray);
176 m_currentRequests.removeAll(reply);
177 reply->deleteLater();
181 void RoutingService::requestRoute(GeoCoordinate from, GeoCoordinate to)
183 qDebug() << __PRETTY_FUNCTION__;
185 QString url = "http://routes.cloudmade.com/";
186 url.append(CLOUDMADE_API_KEY);
187 url.append("/api/0.3/");
188 url.append(QString::number(from.latitude()) + "," + QString::number(from.longitude()) + ",");
189 url.append(QString::number(to.latitude()) + "," + QString::number(to.longitude()));
190 url.append("/car/fastest.js?lang=en&units=km&token=");
192 if(m_token.isEmpty()) {
193 m_pendingRequest.append(url);
194 requestAuthorizationToken();
197 sendRequest(QUrl(url));
201 void RoutingService::sendRequest(const QUrl &url)
203 qDebug() << __PRETTY_FUNCTION__;
205 QNetworkRequest request;
208 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
211 QNetworkReply *reply = m_networkManager->post(request, ba, false);
213 m_currentRequests.append(reply);