First draft of routing service
authorlampehe-local <henri.lampela@ixonos.com>
Fri, 9 Jul 2010 11:47:08 +0000 (14:47 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Fri, 9 Jul 2010 11:47:08 +0000 (14:47 +0300)
src/engine/engine.cpp
src/engine/engine.h
src/routing/routingservice.cpp [new file with mode: 0644]
src/routing/routingservice.h [new file with mode: 0644]
src/src.pro

index 1fe7989..93ed69d 100644 (file)
@@ -32,6 +32,7 @@
 #include "facebookservice/facebookauthentication.h"
 #include "gps/gpsposition.h"
 #include "map/mapengine.h"
+#include "routing/routingservice.h"
 #include "situareservice/situareservice.h"
 #include "ui/mainwindow.h"
 #include "network/networkaccessmanager.h"
@@ -83,6 +84,9 @@ SituareEngine::SituareEngine()
     // build FacebookAuthenticator
     m_facebookAuthenticator = new FacebookAuthentication(this);
 
+    // build routing service
+    m_routingService = new RoutingService(this); // create this when needed, not in constructor!
+
     // connect signals
     signalsFromMapEngine();
     signalsFromGPS();
index 9abeab1..cf927ef 100644 (file)
@@ -39,6 +39,7 @@ class GPSPosition;
 class MainWindow;
 class MapEngine;
 class NetworkAccessManager;
+class RoutingService;
 class SituareService;
 class User;
 class MCE;
@@ -311,6 +312,7 @@ private:
     MainWindow *m_ui;                                ///< Instance of the MainWindow UI
     MapEngine *m_mapEngine;                          ///< MapEngine
     NetworkAccessManager *m_networkAccessManager;    ///< NetworkAccessManager
+    RoutingService *m_routingService;  ///< Instance of the routing service
     SituareService *m_situareService;  ///< Instance of the situare server communication service
     MCE *m_mce;                        ///< Instance of the MCE
 
diff --git a/src/routing/routingservice.cpp b/src/routing/routingservice.cpp
new file mode 100644 (file)
index 0000000..24cbee7
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#include <QFile>
+#include <QDebug>
+#include <QtGlobal>
+#include <QStringList>
+#include <QNetworkReply>
+#include <QPointF>
+
+#include "common.h"
+#include "parser.h"
+#include "network/networkaccessmanager.h"
+#include "routingservice.h"
+
+const int NO_ERROR = 0;
+
+RoutingService::RoutingService(QObject *parent)
+        : QObject(parent)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_networkManager = NetworkAccessManager::instance();
+    connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
+            this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
+
+    QByteArray tmp; // remove
+    parseRouteData(tmp); // remove
+}
+
+RoutingService::~RoutingService()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
+
+void RoutingService::parseRouteData(const QByteArray &jsonReply)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QJson::Parser parser;
+    bool ok;
+    // remove when you need real data ->
+    QVariantMap result;
+    QByteArray jsonDummy;
+    QFile temp("route_from_Lehtokertunkuja_to_Jukolankuja.js");
+    if(temp.open(QIODevice::ReadOnly)) {
+        qDebug() << "File found";
+        jsonDummy = temp.readAll();
+        temp.close();
+        result = parser.parse (jsonDummy, &ok).toMap();
+    } else
+        result = parser.parse (jsonReply, &ok).toMap();
+    // <- remove when you need real data
+
+    //QVariantMap result = parser.parse (jsonReply, &ok).toMap();
+    if (!ok) {
+        emit error(ErrorContext::SITUARE, SituareError::INVALID_JSON);
+        return;
+    } else {
+        if(result.value("status").toInt() == NO_ERROR) {
+            QVariant routeSummary = result.value("route_summary");
+            QMap<QString, QVariant> routeSummaryMap = routeSummary.toMap();
+
+            QString totalDistance = routeSummaryMap["total_distance"].toString();
+            QString totalTime = routeSummaryMap["total_time"].toString();
+            QString startPoint = routeSummaryMap["start_point"].toString();
+            QString endPoint = routeSummaryMap["end_point"].toString();
+
+            qDebug() << totalDistance;
+            qDebug() << totalTime;
+            qDebug() << startPoint;
+            qDebug() << endPoint;
+
+            QList<QPointF> geometryList;
+            foreach(QVariant routeGeometry, result["route_geometry"].toList()) {
+                QStringList list = routeGeometry.toStringList();
+                geometryList.append(QPointF(list.at(0).toDouble(), list.at(1).toDouble()));
+            }
+            qDebug() << geometryList;
+
+            foreach(QVariant routeInstructions, result["route_instructions"].toList()) {
+                QStringList list = routeInstructions.toStringList();
+                qDebug() << list.join(",");
+            }
+        }
+    }
+}
+
+void RoutingService::requestFinished(QNetworkReply *reply)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_currentRequests.contains(reply)) {
+
+        if (reply->error())
+            emit error(ErrorContext::NETWORK, reply->error());
+        else
+            parseRouteData(reply->readAll());
+
+        m_currentRequests.removeAll(reply);
+        reply->deleteLater();
+    }
+}
+
+void RoutingService::sendRequest(const QUrl &url)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    QNetworkRequest request;
+
+    request.setUrl(url);
+    request.setAttribute(QNetworkRequest::CacheSaveControlAttribute, false);
+
+    QNetworkReply *reply = m_networkManager->get(request, true);
+
+    m_currentRequests.append(reply);
+}
diff --git a/src/routing/routingservice.h b/src/routing/routingservice.h
new file mode 100644 (file)
index 0000000..2be1ecf
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#ifndef ROUTINGSERVICE_H
+#define ROUTINGSERVICE_H
+
+#include <QObject>
+
+class NetworkAccessManager;
+class QNetworkReply;
+class QNetworkRequest;
+class QUrl;
+
+/**
+* @brief RoutingService class for communicating with CloudMade server
+*        and parsing routing data
+*
+* @author Henri Lampela
+* @class RoutingService routingservice.h "routingservice/routingservice.h"
+*/
+class RoutingService : public QObject
+{
+    Q_OBJECT
+
+public:
+
+    /**
+    * @brief Default constructor
+    *
+    * @param parent instance of parent
+    */
+    RoutingService(QObject *parent = 0);
+
+    /**
+    * @brief Destructor
+    *
+    */
+    ~RoutingService();
+
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public slots:
+
+    /**
+    * @brief Public slot, which indicates when http request has been completed
+    *
+    * @param reply storage for http reply
+    */
+    void requestFinished(QNetworkReply *reply);
+
+private:
+
+    /**
+    * @brief Parses routing data from JSON string
+    *
+    * @param jsonReply JSON string
+    */
+    void parseRouteData(const QByteArray &jsonReply);
+
+    /**
+    * @brief Sends http request
+    *
+    * @param url destination
+    */
+    void sendRequest(const QUrl &url);
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+
+signals:
+
+    /**
+    * @brief Signals error
+    *
+    * @param context error context
+    * @param error error code
+    */
+    void error(const int context, const int error);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+
+private:
+
+    QList<QNetworkReply *> m_currentRequests;   ///< List of current http requests
+
+    NetworkAccessManager *m_networkManager;    ///< Pointer to QNetworkAccessManager
+};
+
+#endif // ROUTINGSERVICE_H
index 1469bca..1ce4116 100644 (file)
@@ -54,7 +54,8 @@ SOURCES += main.cpp \
     ui/zoombuttonpanel.cpp \
     user/user.cpp \
     ui/fullscreenbutton.cpp \
-    engine/mce.cpp
+    engine/mce.cpp \
+    routing/routingservice.cpp
 HEADERS += application.h \
     common.h \
     engine/engine.h \
@@ -106,11 +107,12 @@ HEADERS += application.h \
     ui/zoombuttonpanel.h \
     user/user.h \
     ui/fullscreenbutton.h \
-    engine/mce.h
+    engine/mce.h \
+    routing/routingservice.h
 QT += network \
     webkit
 
-DEFINES += QT_NO_DEBUG_OUTPUT
+#DEFINES += QT_NO_DEBUG_OUTPUT
 
 simulator {
     SOURCES += network/networkhandlerprivatestub.cpp \