Moved network related files from src/engine to src/network.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 27 May 2010 09:18:57 +0000 (12:18 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 27 May 2010 09:18:57 +0000 (12:18 +0300)
17 files changed:
src/engine/networkaccessmanager.cpp [deleted file]
src/engine/networkaccessmanager.h [deleted file]
src/engine/networkhandler.cpp [deleted file]
src/engine/networkhandler.h [deleted file]
src/engine/networkreply.cpp [deleted file]
src/engine/networkreply.h [deleted file]
src/gps/gpspositionprivate.cpp
src/map/mapengine.cpp
src/map/mapfetcher.cpp
src/network/networkaccessmanager.cpp [new file with mode: 0644]
src/network/networkaccessmanager.h [new file with mode: 0644]
src/network/networkhandler.cpp [new file with mode: 0644]
src/network/networkhandler.h [new file with mode: 0644]
src/network/networkreply.cpp [new file with mode: 0644]
src/network/networkreply.h [new file with mode: 0644]
src/situareservice/imagefetcher.cpp
src/situareservice/situareservice.cpp

diff --git a/src/engine/networkaccessmanager.cpp b/src/engine/networkaccessmanager.cpp
deleted file mode 100644 (file)
index 63de236..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 <QNetworkRequest>
-#include <QNetworkAccessManager>
-
-#include "networkhandler.h"
-#include "networkaccessmanager.h"
-#include "networkreply.h"
-
-NetworkAccessManager *NetworkAccessManager::m_instance = 0;
-
-NetworkAccessManager::NetworkAccessManager(QObject *parent)
-    : QObject(parent),
-      m_networkHandler(0),
-      m_networkAccessManagerPrivate(0)
-{
-    m_networkHandler = new NetworkHandler(this);
-    m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
-
-    connect(m_networkHandler, SIGNAL(connected()),
-            this, SLOT(connected()));
-
-    connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
-            this, SLOT(downloadFinished(QNetworkReply*)));
-}
-
-NetworkAccessManager *NetworkAccessManager::instance(QObject *parent)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (!m_instance)
-        m_instance = new NetworkAccessManager(parent);
-
-    return m_instance;
-}
-
-void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    m_networkAccessManagerPrivate->setCache(cache);
-}
-
-QAbstractNetworkCache *NetworkAccessManager::cache() const
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    return m_networkAccessManagerPrivate->cache();
-}
-
-QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
-{
-    qWarning() << __PRETTY_FUNCTION__ << request.url().toString();
-
-    //Disconnected from network, queue request and return empty reply.
-    if (!m_networkHandler->isConnected()) {
-        qWarning() << __PRETTY_FUNCTION__ << "not connected";
-        m_requestQueue.append(request);
-        m_networkHandler->connect();
-        QNetworkReply *reply = new NetworkReply(request, this);
-        m_offlineReplyQueue.insert(request.url().toString(), reply);
-        return reply;
-    }
-    //Connected, use normal get method.
-    else {
-        qWarning() << __PRETTY_FUNCTION__ << "connected";
-        return m_networkAccessManagerPrivate->get(request);
-    }
-}
-
-void NetworkAccessManager::connected()
-{
-    qWarning() << __PRETTY_FUNCTION__;
-
-    //Loop through all requests and calls get method.
-    foreach (const QNetworkRequest &request, m_requestQueue) {
-        qWarning() << __PRETTY_FUNCTION__ << "get():" << request.url().toString();
-        QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
-        m_temporaryReplyQueue.insert(request.url().toString(), reply);
-    }
-
-    m_requestQueue.clear();
-}
-
-void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
-{
-    qWarning() << __PRETTY_FUNCTION__ << reply->url().toString();
-
-    QString key = m_temporaryReplyQueue.key(reply);
-
-    //Replace offline reply object's content with server reply's content
-    if (!key.isNull()) {
-        qWarning() << "Sizeof reply: " << sizeof(QNetworkReply);
-
-        QNetworkReply *offlineReply = m_offlineReplyQueue.value(key);
-
-        if (offlineReply) {
-            memmove(offlineReply, reply, sizeof(QNetworkReply));
-            m_offlineReplyQueue.remove(key);
-            m_temporaryReplyQueue.remove(key);
-            emit finished(offlineReply);
-        }
-    }
-    //Forward online request's reply
-    else {
-        emit finished(reply);
-    }
-}
diff --git a/src/engine/networkaccessmanager.h b/src/engine/networkaccessmanager.h
deleted file mode 100644 (file)
index 4eed37c..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 NETWORKACCESSMANAGER_H
-#define NETWORKACCESSMANAGER_H
-
-#include <QList>
-#include <QHash>
-#include <QNetworkReply>
-#include <QNetworkRequest>
-#include <QAbstractNetworkCache>
-
-class NetworkHandler;
-class QNetworkAccessManager;
-
-class NetworkAccessManager : public QObject
-{
-    Q_OBJECT
-
-public:
-    static NetworkAccessManager *instance(QObject *parent = 0);
-
-    QNetworkReply *get(const QNetworkRequest &request);
-
-    void setCache(QAbstractNetworkCache *cache);
-
-    QAbstractNetworkCache *cache() const;
-
-protected:
-    NetworkAccessManager(QObject *parent = 0);
-
-private slots:
-    void connected();
-    void downloadFinished(QNetworkReply *reply);
-
-signals:
-    void finished(QNetworkReply *reply);
-
-private:
-    static NetworkAccessManager *m_instance;
-    NetworkHandler *m_networkHandler;
-    QNetworkAccessManager *m_networkAccessManagerPrivate;
-    QList<QNetworkRequest> m_requestQueue;
-    QHash<QString, QNetworkReply*> m_offlineReplyQueue;
-    QHash<QString, QNetworkReply*> m_temporaryReplyQueue;
-};
-
-#endif // NETWORKACCESSMANAGER_H
diff --git a/src/engine/networkhandler.cpp b/src/engine/networkhandler.cpp
deleted file mode 100644 (file)
index 0540950..0000000
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 <icd/dbus_api.h>
-
-#include "networkhandler.h"
-
-static QDBusConnection dBusConnection = QDBusConnection::systemBus();
-const int CONNECTION_STATE_INDEX = 7;
-enum ConnectionState {DISCONNECTED, CONNECTING, CONNECTED, DISCONNECTING};
-
-NetworkHandler::NetworkHandler(QObject *parent)
-    : QObject(parent),
-      m_connected(false),
-      m_connecting(false)
-{
-    dBusInterface = new QDBusInterface(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
-                                       ICD_DBUS_API_INTERFACE, dBusConnection);
-
-    dBusConnection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
-                           ICD_DBUS_API_STATE_SIG,
-                           this, SLOT(stateChanged(const QDBusMessage &)));
-
-    state();
-}
-
-void NetworkHandler::stateChanged(const QDBusMessage &message)
-{
-    qDebug() << __PRETTY_FUNCTION__ << message.arguments();
-
-    if (message.arguments().count() >= 8) {
-
-        bool conversionOk;
-        int connectionState = message.arguments().at(CONNECTION_STATE_INDEX).toInt(&conversionOk);
-        qWarning() << __PRETTY_FUNCTION__ << "Connection state: " << connectionState;
-
-        if (conversionOk) {
-            if ((connectionState == DISCONNECTED) && (isConnected())) {
-                m_connected = false;
-                m_connecting = false;
-                emit disconnected();
-            }
-            else if ((connectionState == CONNECTED) &&(!isConnected())) {
-                m_connected = true;
-                m_connecting = false;
-                emit connected();
-            }
-        }
-    }
-}
-
-void NetworkHandler::connect()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    if (!m_connecting) {
-        m_connecting = true;
-        dBusInterface->call(ICD_DBUS_API_CONNECT_REQ,
-                            QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
-    }
-}
-
-void NetworkHandler::disconnect()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    dBusInterface->call(ICD_DBUS_API_DISCONNECT_REQ,
-                        QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
-}
-
-bool NetworkHandler::isConnected()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    return m_connected;
-}
-
-void NetworkHandler::state()
-{
-    qDebug() << __PRETTY_FUNCTION__;
-
-    dBusInterface->call(ICD_DBUS_API_STATE_REQ,
-                        QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
-}
diff --git a/src/engine/networkhandler.h b/src/engine/networkhandler.h
deleted file mode 100644 (file)
index 6a09647..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 NETWORKHANDLER_H
-#define NETWORKHANDLER_H
-
-#include <QtDBus>
-
-/**
-* @brief NetworkHandler class.
-*
-* This class handles network connection. Class notifies about
-* network connection states.
-*/
-class NetworkHandler : public QObject
-{
-    Q_OBJECT
-
-public:
-    /**
-    * @brief Constructor.
-    *
-    * Creates ICD D-Bus interface.
-    */
-    NetworkHandler(QObject *parent = 0);
-
-/*******************************************************************************
- * MEMBER FUNCTIONS AND SLOTS
- ******************************************************************************/
-public:
-    /**
-    * @brief Requests network connection.
-    *
-    * Request is done via ICD D-Bus.
-    */
-    void connect();
-
-    /**
-    * @brief Requests to disconnect a connection.
-    *
-    * Request is done via ICD D-Bus.
-    */
-    void disconnect();
-
-    /**
-    * @brief Checks if connected to network.
-    *
-    * @return true if connected, false otherwise
-    */
-    bool isConnected();
-
-    void state();
-
-private slots:
-    /**
-    * @brief Slot for ICD D-Bus state change.
-    *
-    * @param message received D-Bus message
-    */
-    void stateChanged(const QDBusMessage &message);
-
-/*******************************************************************************
- * SIGNALS
- ******************************************************************************/
-signals:
-    /**
-    * @brief Signals when connected to network.
-    */
-    void connected();
-
-    /**
-    * @brief Signals when disconnected from network.
-    */
-    void disconnected();
-
-/*******************************************************************************
- * DATA MEMBERS
- ******************************************************************************/
-private:
-    QDBusInterface *dBusInterface;  ///< D-Bus interface
-    bool m_connected;               ///< Connection state variable
-    bool m_connecting;              ///< Connecting state variable
-};
-
-#endif // NETWORKHANDLER_H
diff --git a/src/engine/networkreply.cpp b/src/engine/networkreply.cpp
deleted file mode 100644 (file)
index 9148432..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 "networkreply.h"
-
-NetworkReply::NetworkReply(const QNetworkRequest &request, QObject *parent)
-    : QNetworkReply(parent)
-{
-    setRequest(request);
-}
-
-qint64 NetworkReply::readData(char *data, qint64 maxlen)
-{
-    if (m_offset < m_content.size()) {
-        qint64 number = qMin(maxlen, m_content.size() - m_offset);
-        memcpy(data, m_content.constData() + m_offset, number);
-        m_offset += number;
-        return number;
-    } else
-        return -1;
-}
-
-void NetworkReply::abort()
-{
-
-}
diff --git a/src/engine/networkreply.h b/src/engine/networkreply.h
deleted file mode 100644 (file)
index 6d0ee19..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
-   Situare - A location system for Facebook
-   Copyright (C) 2010  Ixonos Plc. Authors:
-
-       Jussi Laitinen - jussi.laitinen@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 NETWORKREPLY_H
-#define NETWORKREPLY_H
-
-#include <QNetworkReply>
-#include <QNetworkRequest>
-
-class NetworkReply : public QNetworkReply
-{
-public:
-    NetworkReply(const QNetworkRequest &request, QObject *parent = 0);
-
-protected:
-    qint64 readData(char *data, qint64 maxlen);
-    void abort();
-
-private:
-    QByteArray m_content;
-    qint64 m_offset;
-
-};
-
-#endif // NETWORKREPLY_H
index db7efb4..4c4d60c 100644 (file)
@@ -47,11 +47,15 @@ void GPSPositionPrivate::setMode(GPSPosition::Mode mode, const QString &filePath
         delete m_gpsSource;
     }
 
-    if (mode == GPSPosition::Default)
+    if (mode == GPSPosition::Default) {
         m_gpsSource = QGeoPositionInfoSource::createDefaultSource(this);
 
-    if ((!m_gpsSource) || (mode == GPSPosition::Simulation)) {
-        qDebug() << "Using NMEA info source.";
+        if (!m_gpsSource) {
+            emit m_parent->error(tr("Unable to use GPS"));
+            return;
+        }
+    }
+    else if (mode == GPSPosition::Simulation) {
         QNmeaPositionInfoSource *nmeaSource = new QNmeaPositionInfoSource(
                 QNmeaPositionInfoSource::SimulationMode, this);
         QFile *logFile = new QFile(filePath, this);
index aa72153..13a736e 100644 (file)
@@ -56,7 +56,7 @@ MapEngine::MapEngine(QObject *parent)
 
     m_mapScene = new MapScene(this);
 
-    m_mapFetcher = new MapFetcher(NetworkAccessManager::instance(this), this);
+    m_mapFetcher = new MapFetcher(NetworkAccessManager::instance(), this);
     connect(this, SIGNAL(fetchImage(int, int, int)),
             m_mapFetcher, SLOT(enqueueFetchMapImage(int, int, int)));
     connect(m_mapFetcher, SIGNAL(mapImageReceived(int, int, int, QPixmap)),
index 838c50c..84eef04 100644 (file)
@@ -107,37 +107,38 @@ void MapFetcher::checkNextRequestFromCache()
 
 void MapFetcher::downloadFinished(QNetworkReply *reply)
 {
-    qWarning() << __PRETTY_FUNCTION__ << reply->url().toString();
+    qDebug() << __PRETTY_FUNCTION__;
 
-    if (reply->error() == QNetworkReply::NoError) {
-        QImage image;
-        QUrl url = reply->url();
+    if (m_currentDownloads.contains(reply)) {
 
-        if (!image.load(reply, 0))
-            image = QImage();
+        if (reply->error() == QNetworkReply::NoError) {
+            QImage image;
+            QUrl url = reply->url();
 
-        int zoomLevel;
-        int x;
-        int y;
-        parseURL(url, &zoomLevel, &x, &y);
+            if (!image.load(reply, 0))
+                image = QImage();
 
-        emit mapImageReceived(zoomLevel, x, y, QPixmap::fromImage(image));
-    }
-    else {
-        emit error(reply->errorString());
-    }
+            int zoomLevel;
+            int x;
+            int y;
+            parseURL(url, &zoomLevel, &x, &y);
+
+            emit mapImageReceived(zoomLevel, x, y, QPixmap::fromImage(image));
+        }
+        else {
+            emit error(reply->errorString());
+        }
 
-    qWarning() << __PRETTY_FUNCTION__ << "Removed: " << m_currentDownloads.removeAll(reply);
-    reply->deleteLater();
-    startNextDownload();
+        m_currentDownloads.removeAll(reply);
+        reply->deleteLater();
+        startNextDownload();
+    }
 }
 
 void MapFetcher::enqueueFetchMapImage(int zoomLevel, int x, int y)
 {
     QUrl url = buildURL(zoomLevel, QPoint(x, y));
 
-    qWarning() << __PRETTY_FUNCTION__ << "url:" << url.toString();
-
     // check if new request is already in the list and move it to the begin of the list...
     bool found = false;
     for (int i = 0; i < m_pendingRequests.size(); i++) {
diff --git a/src/network/networkaccessmanager.cpp b/src/network/networkaccessmanager.cpp
new file mode 100644 (file)
index 0000000..f276103
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 <QNetworkRequest>
+#include <QNetworkAccessManager>
+
+#include "networkhandler.h"
+#include "networkaccessmanager.h"
+#include "networkreply.h"
+
+NetworkAccessManager *NetworkAccessManager::m_instance = 0;
+
+NetworkAccessManager::NetworkAccessManager()
+    : m_networkHandler(0),
+      m_networkAccessManagerPrivate(0)
+{
+    m_networkHandler = new NetworkHandler(this);
+    m_networkAccessManagerPrivate = new QNetworkAccessManager(this);
+
+    connect(m_networkHandler, SIGNAL(connected()),
+            this, SLOT(connected()));
+
+    connect(m_networkAccessManagerPrivate, SIGNAL(finished(QNetworkReply*)),
+            this, SLOT(downloadFinished(QNetworkReply*)));
+}
+
+QAbstractNetworkCache *NetworkAccessManager::cache() const
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_networkAccessManagerPrivate->cache();
+}
+
+void NetworkAccessManager::connected()
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    //Loop through all requests and calls get method.
+    foreach (const QNetworkRequest &request, m_requestQueue) {
+        qWarning() << __PRETTY_FUNCTION__ << "get():" << request.url().toString();
+        QNetworkReply *reply = m_networkAccessManagerPrivate->get(request);
+        m_temporaryReplyQueue.insert(request.url().toString(), reply);
+    }
+
+    m_requestQueue.clear();
+}
+
+void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
+{
+    qWarning() << __PRETTY_FUNCTION__ << reply->url().toString();
+
+    QString key = m_temporaryReplyQueue.key(reply, "");
+
+    //Replace offline reply object's content with server reply's content
+    if (!key.isEmpty()) {
+        QNetworkReply *offlineReply = m_offlineReplyQueue.value(key, 0);
+
+        if (offlineReply) {
+            memmove(offlineReply, reply, sizeof(QNetworkReply));
+            m_offlineReplyQueue.remove(key);
+            m_temporaryReplyQueue.remove(key);
+            emit finished(offlineReply);
+        }
+    }
+    //Forward online request's reply
+    else {
+        emit finished(reply);
+    }
+}
+
+QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request)
+{
+    qWarning() << __PRETTY_FUNCTION__ << request.url().toString();
+
+    //Disconnected from network, queue request and return empty reply.
+    if (!m_networkHandler->isConnected()) {
+        qWarning() << __PRETTY_FUNCTION__ << "not connected";
+        m_requestQueue.append(request);
+        m_networkHandler->connect();
+        QNetworkReply *reply = new NetworkReply(request, this);
+        m_offlineReplyQueue.insert(request.url().toString(), reply);
+        return reply;
+    }
+    //Connected, use normal get method.
+    else {
+        qWarning() << __PRETTY_FUNCTION__ << "connected";
+        return m_networkAccessManagerPrivate->get(request);
+    }
+}
+
+
+NetworkAccessManager *NetworkAccessManager::instance()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_instance)
+        m_instance = new NetworkAccessManager();
+
+    return m_instance;
+}
+
+void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_networkAccessManagerPrivate->setCache(cache);
+}
diff --git a/src/network/networkaccessmanager.h b/src/network/networkaccessmanager.h
new file mode 100644 (file)
index 0000000..10a2ae3
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 NETWORKACCESSMANAGER_H
+#define NETWORKACCESSMANAGER_H
+
+#include <QList>
+#include <QHash>
+#include <QNetworkReply>
+#include <QNetworkRequest>
+#include <QAbstractNetworkCache>
+
+class NetworkHandler;
+class QNetworkAccessManager;
+
+/**
+* @brief NetworkAccessManager class.
+*
+* This class handles network requests and receives network replies.
+* NetworkAccessManager queues requests when disconnected from network
+* and makes requests when connected to network.
+*/
+class NetworkAccessManager : public QObject
+{
+    Q_OBJECT
+
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public:
+    /**
+    * @brief Returns instance of NetworkAccessManager.
+    *
+    * Creates instance if not created.
+    */
+    static NetworkAccessManager *instance();
+
+    /**
+    * @brief Makes request and return reply.
+    *
+    * @param request QNetworkRequest
+    * @return QNetworkReply
+    */
+    QNetworkReply *get(const QNetworkRequest &request);
+
+    /**
+    * @brief Sets cache.
+    *
+    * @param cache QAbstractNetworkCache instance
+    */
+    void setCache(QAbstractNetworkCache *cache);
+
+    /**
+    * @brief Returns cache.
+    *
+    * @return QAbstractNetworkCache
+    */
+    QAbstractNetworkCache *cache() const;
+
+protected:
+    /**
+    * @brief Constructor.
+    *
+    * Instance of this class can only be created by using instance method.
+    */
+    NetworkAccessManager();
+
+private slots:
+    /**
+    * @brief Slot for network connected state.
+    */
+    void connected();
+
+    /**
+    * @brief Slot for finished download.
+    *
+    * @param reply reply from network
+    */
+    void downloadFinished(QNetworkReply *reply);
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+signals:
+    /**
+    * Signal for finished download.
+    *
+    * @param reply reply from network
+    */
+    void finished(QNetworkReply *reply);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    static NetworkAccessManager *m_instance;                ///< Instance of NetworkAccessManager
+    NetworkHandler *m_networkHandler;                       ///< Instance of NetworkHandler
+    QNetworkAccessManager *m_networkAccessManagerPrivate;   ///< Instance of QNetworkAccessManager
+    QList<QNetworkRequest> m_requestQueue;                  ///< Queue for requests
+    QHash<QString, QNetworkReply*> m_offlineReplyQueue;     ///< Queue for offline replies
+    QHash<QString, QNetworkReply*> m_temporaryReplyQueue;   ///< Queue for temporary replies
+};
+
+#endif // NETWORKACCESSMANAGER_H
diff --git a/src/network/networkhandler.cpp b/src/network/networkhandler.cpp
new file mode 100644 (file)
index 0000000..5b9c724
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 <icd/dbus_api.h>
+
+#include "networkhandler.h"
+
+static QDBusConnection dBusConnection = QDBusConnection::systemBus();
+const int CONNECTION_STATE_INDEX = 7;
+
+NetworkHandler::NetworkHandler(QObject *parent)
+    : QObject(parent),
+      m_connected(false),
+      m_connecting(false)
+{
+    dBusInterface = new QDBusInterface(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
+                                       ICD_DBUS_API_INTERFACE, dBusConnection);
+
+    dBusConnection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
+                           ICD_DBUS_API_STATE_SIG,
+                           this, SLOT(stateChanged(const QDBusMessage &)));
+
+    state();
+}
+
+void NetworkHandler::stateChanged(const QDBusMessage &message)
+{
+    qDebug() << __PRETTY_FUNCTION__ << message.arguments();
+
+    if (message.arguments().count() >= 8) {
+
+        bool conversionOk;
+        int connectionState = message.arguments().at(CONNECTION_STATE_INDEX).toInt(&conversionOk);
+        qWarning() << __PRETTY_FUNCTION__ << "Connection state: " << connectionState;
+
+        if (conversionOk) {
+            if ((connectionState == ICD_STATE_DISCONNECTED) && (isConnected())) {
+                m_connected = false;
+                m_connecting = false;
+                emit disconnected();
+            }
+            else if ((connectionState == ICD_STATE_CONNECTED) &&(!isConnected())) {
+                m_connected = true;
+                m_connecting = false;
+                emit connected();
+            }
+        }
+    }
+}
+
+void NetworkHandler::connect()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (!m_connecting && !m_connected) {
+        m_connecting = true;
+        dBusInterface->call(ICD_DBUS_API_CONNECT_REQ,
+                            QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
+    }
+}
+
+void NetworkHandler::disconnect()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    dBusInterface->call(ICD_DBUS_API_DISCONNECT_REQ,
+                        QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
+}
+
+bool NetworkHandler::isConnected()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    return m_connected;
+}
+
+void NetworkHandler::state()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    dBusInterface->call(ICD_DBUS_API_STATE_REQ);
+}
diff --git a/src/network/networkhandler.h b/src/network/networkhandler.h
new file mode 100644 (file)
index 0000000..f85d156
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 NETWORKHANDLER_H
+#define NETWORKHANDLER_H
+
+#include <QtDBus>
+
+/**
+* @brief NetworkHandler class.
+*
+* This class handles network connection. Class notifies about
+* network connection states.
+*/
+class NetworkHandler : public QObject
+{
+    Q_OBJECT
+
+public:
+    /**
+    * @brief Constructor.
+    *
+    * Creates ICD D-Bus interface.
+    */
+    NetworkHandler(QObject *parent = 0);
+
+/*******************************************************************************
+ * MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public:
+    /**
+    * @brief Requests network connection.
+    *
+    * Request is done via ICD D-Bus.
+    */
+    void connect();
+
+    /**
+    * @brief Requests to disconnect a connection.
+    *
+    * Request is done via ICD D-Bus.
+    */
+    void disconnect();
+
+    /**
+    * @brief Checks if connected to network.
+    *
+    * @return true if connected, false otherwise
+    */
+    bool isConnected();
+
+    /**
+    * @brief Requests network state.
+    */
+    void state();
+
+private slots:
+    /**
+    * @brief Slot for ICD D-Bus state change.
+    *
+    * @param message received D-Bus message
+    */
+    void stateChanged(const QDBusMessage &message);
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+signals:
+    /**
+    * @brief Signals when connected to network.
+    */
+    void connected();
+
+    /**
+    * @brief Signals when disconnected from network.
+    */
+    void disconnected();
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    QDBusInterface *dBusInterface;  ///< D-Bus interface
+    bool m_connected;               ///< Connection state variable
+    bool m_connecting;              ///< Connecting state variable
+};
+
+#endif // NETWORKHANDLER_H
diff --git a/src/network/networkreply.cpp b/src/network/networkreply.cpp
new file mode 100644 (file)
index 0000000..ca6bf0d
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 <QDebug>
+
+#include "networkreply.h"
+
+NetworkReply::NetworkReply(const QNetworkRequest &request, QObject *parent)
+    : QNetworkReply(parent)
+{
+    setRequest(request);
+}
+
+qint64 NetworkReply::readData(char *data, qint64 maxlen)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_offset < m_content.size()) {
+        qint64 number = qMin(maxlen, m_content.size() - m_offset);
+        memcpy(data, m_content.constData() + m_offset, number);
+        m_offset += number;
+        return number;
+    } else
+        return -1;
+}
+
+void NetworkReply::abort()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+}
diff --git a/src/network/networkreply.h b/src/network/networkreply.h
new file mode 100644 (file)
index 0000000..dc5755a
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+       Jussi Laitinen - jussi.laitinen@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 NETWORKREPLY_H
+#define NETWORKREPLY_H
+
+#include <QNetworkReply>
+#include <QNetworkRequest>
+
+/**
+* @brief NetworkReply class.
+*
+* Class is used by NetworkAccessManager when network state is disconnected.
+*/
+class NetworkReply : public QNetworkReply
+{
+public:
+    /**
+    * @brief Constructor.
+    *
+    * @param request QNetworkRequest
+    * @param parent QObject
+    */
+    NetworkReply(const QNetworkRequest &request, QObject *parent = 0);
+
+/*******************************************************************************
+ * BASE CLASS INHERITED AND REIMPLEMENTED MEMBER FUNCTIONS
+ ******************************************************************************/
+protected:
+    /**
+    * @brief Reads data from the device.
+    *
+    * @param data data is read into this
+    * @param maxlen maximum size to read
+    * @return number of bytes read or -1 if error occured
+    */
+    qint64 readData(char *data, qint64 maxlen);
+
+    /**
+    * @brief Aborts the operation.
+    */
+    void abort();
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    QByteArray m_content;   ///< Content of data
+    qint64 m_offset;        ///< Offset for data read
+
+};
+
+#endif // NETWORKREPLY_H
index 99bc5e8..a4e0763 100644 (file)
@@ -62,22 +62,26 @@ void ImageFetcher::downloadFinished(QNetworkReply *reply)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (reply->error() == QNetworkReply::NoError) {
-        QPixmap image;
-        QUrl url = reply->url();
+    //Reply from Facebook
+    if (m_currentDownloads.contains(reply)) {
 
-        if (!image.loadFromData(reply->readAll(), 0))
-            image = QPixmap();
+        if (reply->error() == QNetworkReply::NoError) {
+            QPixmap image;
+            QUrl url = reply->url();
 
-        emit imageReceived(url, image);
-    }
-    else {
-        emit error(reply->errorString());
-    }
+            if (!image.loadFromData(reply->readAll(), 0))
+                image = QPixmap();
 
-    m_currentDownloads.removeAll(reply);
-    reply->deleteLater();
+            emit imageReceived(url, image);
+        }
+        else {
+            emit error(reply->errorString());
+        }
 
-    if (!m_downloadQueue.isEmpty())
-        startNextDownload();
+        m_currentDownloads.removeAll(reply);
+        reply->deleteLater();
+
+        if (!m_downloadQueue.isEmpty())
+            startNextDownload();
+    }
 }
index 285fd4d..1b5276d 100644 (file)
@@ -37,10 +37,10 @@ SituareService::SituareService(QObject *parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_networkManager = NetworkAccessManager::instance(this);
+    m_networkManager = NetworkAccessManager::instance();
     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
 
-    m_imageFetcher = new ImageFetcher(NetworkAccessManager::instance(this), this);
+    m_imageFetcher = new ImageFetcher(NetworkAccessManager::instance(), this);
     connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
     connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QPixmap)), this,
             SLOT(imageReceived(QUrl, QPixmap)));
@@ -226,7 +226,7 @@ QString SituareService::formUrlParameters(const QPointF &coordinates, QString st
 
 void SituareService::sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie)
 {
-    qDebug() << __PRETTY_FUNCTION__ << "url: " << url;
+    qDebug() << __PRETTY_FUNCTION__;
 
     QNetworkRequest request;
 
@@ -242,52 +242,56 @@ void SituareService::requestFinished(QNetworkReply *reply)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    QUrl url = reply->url();
-    qDebug() << "BytesAvailable: " << reply->bytesAvailable();
-    if (reply->error()) {
-        qDebug() << reply->errorString();
-        emit error(reply->errorString());
-    }
-    else {
-        qint64 max = reply->size();
-        QByteArray replyArray = reply->read(max);
-        qDebug() << "Reply from: " << url << "reply " << replyArray;
-        // ToDo: results handling includes Situare's errors
-        // works like situare's error handling i.e. both lat & lon are missing/wrong
-        // -> we get only error for wrong lon
-        if(replyArray == ERROR_LAT.toAscii()) {
-            qDebug() << "Error: " << ERROR_LAT;
-            emit error(replyArray);
-        }
-        else if(replyArray == ERROR_LON.toAscii()) {
-            qDebug() << "Error: " << ERROR_LON;
-            emit error(replyArray);
-        }
-        else if(replyArray.contains(ERROR_SESSION.toAscii())) {
-            qDebug() << "Error: " << ERROR_SESSION;
-            emit invalidSessionCredentials();
-        }
-        else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
-            qDebug() << "JSON string";
-            parseUserData(replyArray);
+    //Reply from situare
+    if (m_currentRequests.contains(reply)) {
+
+        QUrl url = reply->url();
+        qDebug() << "BytesAvailable: " << reply->bytesAvailable();
+        if (reply->error()) {
+            qDebug() << reply->errorString();
+            emit error(reply->errorString());
         }
-        else if(replyArray == "") {
-                       if(url.toString().contains(UPDATE_LOCATION.toAscii())) {
-                emit updateWasSuccessful();
+        else {
+            qint64 max = reply->size();
+            QByteArray replyArray = reply->read(max);
+            qDebug() << "Reply from: " << url << "reply " << replyArray;
+            // ToDo: results handling includes Situare's errors
+            // works like situare's error handling i.e. both lat & lon are missing/wrong
+            // -> we get only error for wrong lon
+            if(replyArray == ERROR_LAT.toAscii()) {
+                qDebug() << "Error: " << ERROR_LAT;
+                emit error(replyArray);
             }
-            else {
-                // session credentials are invalid
+            else if(replyArray == ERROR_LON.toAscii()) {
+                qDebug() << "Error: " << ERROR_LON;
+                emit error(replyArray);
+            }
+            else if(replyArray.contains(ERROR_SESSION.toAscii())) {
+                qDebug() << "Error: " << ERROR_SESSION;
                 emit invalidSessionCredentials();
             }
+            else if(replyArray.startsWith(OPENING_BRACE_MARK.toAscii())) {
+                qDebug() << "JSON string";
+                parseUserData(replyArray);
+            }
+            else if(replyArray == "") {
+                            if(url.toString().contains(UPDATE_LOCATION.toAscii())) {
+                    emit updateWasSuccessful();
+                }
+                else {
+                    // session credentials are invalid
+                    emit invalidSessionCredentials();
+                }
+            }
+            else {
+                // Street address ready
+                QString address = QString::fromUtf8(replyArray);
+                emit reverseGeoReady(address);
+            }
         }
-        else {
-            // Street address ready
-            QString address = QString::fromUtf8(replyArray);
-            emit reverseGeoReady(address);
-        }
+        m_currentRequests.removeAll(reply);
+        reply->deleteLater();
     }
-    m_currentRequests.removeAll(reply);
-    reply->deleteLater();
 }
 
 void SituareService::credentialsReady(const FacebookCredentials &credentials)