2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Henri Lampela - henri.lampela@ixonos.com
7 Situare is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
11 Situare is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Situare; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 #include <QStringList>
26 #include <QNetworkReply>
31 #include "network/networkaccessmanager.h"
32 #include "routingservice.h"
34 const int NO_ERROR = 0;
36 RoutingService::RoutingService(QObject *parent)
39 qDebug() << __PRETTY_FUNCTION__;
41 m_networkManager = NetworkAccessManager::instance();
42 connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
43 this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
45 QByteArray tmp; // remove
46 parseRouteData(tmp); // remove
49 RoutingService::~RoutingService()
51 qDebug() << __PRETTY_FUNCTION__;
54 void RoutingService::parseRouteData(const QByteArray &jsonReply)
56 qDebug() << __PRETTY_FUNCTION__;
60 // remove when you need real data ->
63 QFile temp("route_from_Lehtokertunkuja_to_Jukolankuja.js");
64 if(temp.open(QIODevice::ReadOnly)) {
65 qDebug() << "File found";
66 jsonDummy = temp.readAll();
68 result = parser.parse (jsonDummy, &ok).toMap();
70 result = parser.parse (jsonReply, &ok).toMap();
71 // <- remove when you need real data
73 //QVariantMap result = parser.parse (jsonReply, &ok).toMap();
75 emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
78 if(result.value("status").toInt() == NO_ERROR) {
79 QVariant routeSummary = result.value("route_summary");
80 QMap<QString, QVariant> routeSummaryMap = routeSummary.toMap();
82 QString totalDistance = routeSummaryMap["total_distance"].toString();
83 QString totalTime = routeSummaryMap["total_time"].toString();
84 QString startPoint = routeSummaryMap["start_point"].toString();
85 QString endPoint = routeSummaryMap["end_point"].toString();
87 qDebug() << totalDistance;
88 qDebug() << totalTime;
89 qDebug() << startPoint;
92 QList<QPointF> geometryList;
93 foreach(QVariant routeGeometry, result["route_geometry"].toList()) {
94 QStringList list = routeGeometry.toStringList();
95 geometryList.append(QPointF(list.at(0).toDouble(), list.at(1).toDouble()));
97 qDebug() << geometryList;
99 foreach(QVariant routeInstructions, result["route_instructions"].toList()) {
100 QStringList list = routeInstructions.toStringList();
101 qDebug() << list.join(",");
107 void RoutingService::requestFinished(QNetworkReply *reply)
109 qDebug() << __PRETTY_FUNCTION__;
111 if (m_currentRequests.contains(reply)) {
114 emit error(ErrorContext::NETWORK, reply->error());
116 parseRouteData(reply->readAll());
118 m_currentRequests.removeAll(reply);
119 reply->deleteLater();
123 void RoutingService::sendRequest(const QUrl &url)
125 qDebug() << __PRETTY_FUNCTION__;
127 QNetworkRequest request;
130 request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
132 QNetworkReply *reply = m_networkManager->get(request, true);
134 m_currentRequests.append(reply);