--- /dev/null
+/*
+ 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);
+}
--- /dev/null
+/*
+ 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