First draft of routing service
[situare] / src / routing / routingservice.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5       Henri Lampela - henri.lampela@ixonos.com
6
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.
10
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.
15
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,
19    USA.
20 */
21
22 #include <QFile>
23 #include <QDebug>
24 #include <QtGlobal>
25 #include <QStringList>
26 #include <QNetworkReply>
27 #include <QPointF>
28
29 #include "common.h"
30 #include "parser.h"
31 #include "network/networkaccessmanager.h"
32 #include "routingservice.h"
33
34 const int NO_ERROR = 0;
35
36 RoutingService::RoutingService(QObject *parent)
37         : QObject(parent)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     m_networkManager = NetworkAccessManager::instance();
42     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
43             this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
44
45     QByteArray tmp; // remove
46     parseRouteData(tmp); // remove
47 }
48
49 RoutingService::~RoutingService()
50 {
51     qDebug() << __PRETTY_FUNCTION__;
52 }
53
54 void RoutingService::parseRouteData(const QByteArray &jsonReply)
55 {
56     qDebug() << __PRETTY_FUNCTION__;
57
58     QJson::Parser parser;
59     bool ok;
60     // remove when you need real data ->
61     QVariantMap result;
62     QByteArray jsonDummy;
63     QFile temp("route_from_Lehtokertunkuja_to_Jukolankuja.js");
64     if(temp.open(QIODevice::ReadOnly)) {
65         qDebug() << "File found";
66         jsonDummy = temp.readAll();
67         temp.close();
68         result = parser.parse (jsonDummy, &ok).toMap();
69     } else
70         result = parser.parse (jsonReply, &ok).toMap();
71     // <- remove when you need real data
72
73     //QVariantMap result = parser.parse (jsonReply, &ok).toMap();
74     if (!ok) {
75         emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
76         return;
77     } else {
78         if(result.value("status").toInt() == NO_ERROR) {
79             QVariant routeSummary = result.value("route_summary");
80             QMap<QString, QVariant> routeSummaryMap = routeSummary.toMap();
81
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();
86
87             qDebug() << totalDistance;
88             qDebug() << totalTime;
89             qDebug() << startPoint;
90             qDebug() << endPoint;
91
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()));
96             }
97             qDebug() << geometryList;
98
99             foreach(QVariant routeInstructions, result["route_instructions"].toList()) {
100                 QStringList list = routeInstructions.toStringList();
101                 qDebug() << list.join(",");
102             }
103         }
104     }
105 }
106
107 void RoutingService::requestFinished(QNetworkReply *reply)
108 {
109     qDebug() << __PRETTY_FUNCTION__;
110
111     if (m_currentRequests.contains(reply)) {
112
113         if (reply->error())
114             emit error(ErrorContext::NETWORK, reply->error());
115         else
116             parseRouteData(reply->readAll());
117
118         m_currentRequests.removeAll(reply);
119         reply->deleteLater();
120     }
121 }
122
123 void RoutingService::sendRequest(const QUrl &url)
124 {
125     qDebug() << __PRETTY_FUNCTION__;
126
127     QNetworkRequest request;
128
129     request.setUrl(url);
130     request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
131
132     QNetworkReply *reply = m_networkManager->get(request, true);
133
134     m_currentRequests.append(reply);
135 }