Merge branch 'master' into network_handler
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 24 May 2010 11:50:27 +0000 (14:50 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Mon, 24 May 2010 11:50:27 +0000 (14:50 +0300)
src/engine/engine.cpp
src/engine/engine.h
src/engine/networkaccessmanager.cpp [new file with mode: 0644]
src/engine/networkaccessmanager.h [new file with mode: 0644]
src/engine/networkhandler.cpp [new file with mode: 0644]
src/engine/networkhandler.h [new file with mode: 0644]
src/map/mapengine.cpp
src/map/mapfetcher.cpp
src/map/mapfetcher.h
src/src.pro

index 2df601f..5172f07 100644 (file)
@@ -29,6 +29,7 @@
 #include "map/mapengine.h"
 #include "situareservice/situareservice.h"
 #include "ui/mainwindow.h"
+#include "networkhandler.h"
 
 #include "engine.h"
 
@@ -42,6 +43,9 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     qDebug() << __PRETTY_FUNCTION__;
     m_ui = new MainWindow;
 
+    // build NetworkHandler
+    m_networkHandler = new NetworkHandler(this);
+
     // build MapEngine
     m_mapEngine = new MapEngine(this);
     m_ui->setMapViewScene(m_mapEngine->scene());
@@ -57,6 +61,7 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     m_facebookAuthenticator = new FacebookAuthentication(this);
 
     // connect signals
+    signalsFromNetworkHandler();
     signalsFromMapEngine();
     signalsFromGPS();
     signalsFromSituareService();
@@ -94,6 +99,8 @@ SituareEngine::SituareEngine(QMainWindow *parent)
     if (gpsEnabled.toBool() && autoCenteringEnabled.toBool())
         m_ui->showMaemoInformationBox(tr("Auto centering enabled"));
 
+    //Request network connection.
+//    m_networkHandler->connect();
     m_facebookAuthenticator->start();
 }
 
@@ -117,6 +124,14 @@ void SituareEngine::changeAutoCenteringSetting(bool enabled)
     enableAutoCentering(enabled);
 }
 
+void SituareEngine::connectedToNetwork()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+//    m_facebookAuthenticator->start();
+    //TODO: Start map image fetching
+}
+
 void SituareEngine::disableAutoCentering()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -125,6 +140,12 @@ void SituareEngine::disableAutoCentering()
     m_ui->showMaemoInformationBox(tr("Auto centering disabled"));
 }
 
+void SituareEngine::disconnectedFromNetwork()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+    //TODO: Stop map image fetching
+}
+
 void SituareEngine::enableAutoCentering(bool enabled)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -324,6 +345,17 @@ void SituareEngine::signalsFromMapEngine()
             m_ui, SIGNAL(locationItemClicked(QList<QString>)));
 }
 
+void SituareEngine::signalsFromNetworkHandler()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    connect(m_networkHandler, SIGNAL(connected()),
+            this, SLOT(connectedToNetwork()));
+
+    connect(m_networkHandler, SIGNAL(disconnected()),
+            this, SLOT(disconnectedFromNetwork()));
+}
+
 void SituareEngine::signalsFromSituareService()
 {
     qDebug() << __PRETTY_FUNCTION__;
index d0cfa69..1b4d42e 100644 (file)
@@ -36,6 +36,7 @@ class MainWindow;
 class MapEngine;
 class SituareService;
 class User;
+class NetworkHandler;
 
 /**
 * @brief Engine class for Situare Application
@@ -150,6 +151,11 @@ private:
     void signalsFromMapView();
 
     /**
+      * @brief Connect signals coming from NetworkHandler.
+      */
+    void signalsFromNetworkHandler();
+
+    /**
       * @brief Connect signals coming from Situare
       */
     void signalsFromSituareService();
@@ -161,11 +167,25 @@ private slots:
     void changeAutoCenteringSetting(bool enabled);
 
     /**
+    * @brief Slot for connected to network.
+    *
+    * Starts network requests.
+    */
+    void connectedToNetwork();
+
+    /**
       * @brief Slot for disabling automatic centering when map is scrolled manually
       */
     void disableAutoCentering();
 
     /**
+    * @brief Slot for disconnecting from network.
+    *
+    * Stops all network requests.
+    */
+    void disconnectedFromNetwork();
+
+    /**
     * @brief Slot for auto centering enabling.
     *
     * Calls gps to send last known position
@@ -210,6 +230,7 @@ private:
     MainWindow *m_ui;                                ///< Instance of the MainWindow UI
     MapEngine *m_mapEngine;                          ///< MapEngine
     SituareService *m_situareService;  ///< Instance of the situare server communication service
+    NetworkHandler *m_networkHandler;  ///< Instance of NetworkHandler
 };
 
 #endif // ENGINE_H
diff --git a/src/engine/networkaccessmanager.cpp b/src/engine/networkaccessmanager.cpp
new file mode 100644 (file)
index 0000000..2f67958
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+   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 "networkhandler.h"
+#include "networkaccessmanager.h"
+
+NetworkAccessManager::NetworkAccessManager(QObject *parent)
+    : QNetworkAccessManager(parent),
+      m_networkHandler(0)
+{
+    m_networkHandler = new NetworkHandler(this);
+
+    connect(m_networkHandler, SIGNAL(connected()),
+            this, SLOT(connected()));
+}
+
+QNetworkReply *NetworkAccessManager::getti(const QNetworkRequest &request)
+{
+    if (!m_networkHandler->isConnected()) {
+        m_requestQueue.append(request);
+        m_networkHandler->connect();
+        return createRequest(QNetworkAccessManager::GetOperation, request);
+    }
+    else {
+        return get(request);
+    }
+}
+
+void NetworkAccessManager::connected()
+{
+    foreach (QNetworkRequest request, m_requestQueue)
+        get(request);
+
+    m_requestQueue.clear();
+}
diff --git a/src/engine/networkaccessmanager.h b/src/engine/networkaccessmanager.h
new file mode 100644 (file)
index 0000000..cf67745
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+   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 <QNetworkAccessManager>
+#include <QList>
+#include <QNetworkReply>
+#include <QNetworkRequest>
+
+class NetworkHandler;
+
+class NetworkAccessManager : public QNetworkAccessManager
+{
+    Q_OBJECT
+
+public:
+    NetworkAccessManager(QObject *parent = 0);
+
+    QNetworkReply *getti(const QNetworkRequest &request);
+
+private slots:
+    void connected();
+
+private:
+    NetworkHandler *m_networkHandler;
+    QList<QNetworkRequest> m_requestQueue;
+};
+
+#endif // NETWORKACCESSMANAGER_H
diff --git a/src/engine/networkhandler.cpp b/src/engine/networkhandler.cpp
new file mode 100644 (file)
index 0000000..f28cc08
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+   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)
+{
+    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 &)));
+}
+
+void NetworkHandler::stateChanged(const QDBusMessage &message)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (message.arguments().count() >= 8) {
+
+        bool conversionOk;
+        int connectionState = message.arguments().at(CONNECTION_STATE_INDEX).toInt(&conversionOk);
+
+        if (conversionOk) {
+            if (connectionState == DISCONNECTED) {
+                m_connected = false;
+                emit disconnected();
+            }
+            else if (connectionState == CONNECTED) {
+                m_connected = true;
+                emit connected();
+            }
+        }
+    }
+}
+
+void NetworkHandler::connect()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    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()
+{
+    return m_connected;
+}
diff --git a/src/engine/networkhandler.h b/src/engine/networkhandler.h
new file mode 100644 (file)
index 0000000..5ef7aa8
--- /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.
+*/
+
+#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();
+
+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
+};
+
+#endif // NETWORKHANDLER_H
index ca2e42d..e22408d 100644 (file)
@@ -56,7 +56,7 @@ MapEngine::MapEngine(QObject *parent)
 
     m_mapScene = new MapScene(this);
 
-    m_mapFetcher = new MapFetcher(new QNetworkAccessManager(this), this);
+    m_mapFetcher = new MapFetcher(new NetworkAccessManager(this), 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 9a01db8..7b0acfe 100644 (file)
 
 #include "mapfetcher.h"
 #include "mapcommon.h"
+#include "engine/networkaccessmanager.h"
 
 const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
 const int NOT_FOUND = -1; ///< Return value if matching request is not found from the list
 
-MapFetcher::MapFetcher(QNetworkAccessManager *manager, QObject *parent)
+MapFetcher::MapFetcher(NetworkAccessManager *manager, QObject *parent)
     : QObject(parent)
     , m_pendingRequestsSize(0)
     , m_fetchMapImagesTimerRunning(false)
@@ -292,7 +293,7 @@ void MapFetcher::startNextDownload()
 
         QNetworkRequest request(url);
         request.setRawHeader("User-Agent", "Situare");
-        QNetworkReply *reply = m_manager->get(request);
+        QNetworkReply *reply = m_manager->getti(request);
 
         m_currentDownloads.append(reply);
     }
index 9edbfb5..12a5809 100644 (file)
@@ -27,6 +27,7 @@
 #include <QNetworkAccessManager>
 
 #include "maptilerequest.h"
+#include "engine/networkaccessmanager.h"
 
 class QNetworkReply;
 class QUrl;
@@ -48,7 +49,7 @@ public:
     * @param manager Network access manager
     * @param parent parent object
     */
-    MapFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
+    MapFetcher(NetworkAccessManager *manager, QObject *parent = 0);
 
 /*******************************************************************************
  * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
@@ -191,7 +192,7 @@ private:
     QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
     int m_pendingRequestsSize; ///< Max number of pending requests
     bool m_fetchMapImagesTimerRunning; ///< is the singleshot timer already running
-    QNetworkAccessManager *m_manager; ///< Network access manager
+    NetworkAccessManager *m_manager; ///< Network access manager
     QList<MapTileRequest> m_pendingRequests; ///< List of map image fetching requests
 };
 
index 7049872..f9953fd 100644 (file)
@@ -3,8 +3,9 @@
 # -------------------------------------------------
 TARGET = ../situare
 TEMPLATE = app
-INCLUDEPATH += "/usr/include/qjson"
-LIBS += "-lqjson"
+INCLUDEPATH += /usr/include/qjson /usr/include/glib-2.0 /usr/lib/glib-2.0/include
+LIBS += -lqjson
+CONFIG += icd2 qdbus
 RESOURCES += ../images.qrc
 SOURCES += main.cpp \
     ui/mainwindow.cpp \
@@ -41,7 +42,9 @@ SOURCES += main.cpp \
     map/gpslocationitem.cpp \
     ui/zoombuttonpanel.cpp \
     ui/userinfo.cpp \
-    ui/sidepanel.cpp
+    engine/networkhandler.cpp \
+    ui/sidepanel.cpp \
+    engine/networkaccessmanager.cpp
 HEADERS += ui/mainwindow.h \
     map/mapengine.h \
     map/mapview.h \
@@ -82,7 +85,9 @@ HEADERS += ui/mainwindow.h \
     ui/zoombuttonpanel.h \
     common.h \
     ui/userinfo.h \
-    ui/sidepanel.h
+    engine/networkhandler.h \
+    ui/sidepanel.h \
+    engine/networkaccessmanager.h
 QT += network \
     webkit