From f7c420cdf5db9f3c25724aae11410a04ae531556 Mon Sep 17 00:00:00 2001 From: Jussi Laitinen Date: Fri, 21 May 2010 12:30:55 +0300 Subject: [PATCH] Added NetworkHandler class. --- src/engine/networkhandler.cpp | 73 ++++++++++++++++++++++++++++++++ src/engine/networkhandler.h | 92 +++++++++++++++++++++++++++++++++++++++++ src/src.pro | 6 ++- 3 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 src/engine/networkhandler.cpp create mode 100644 src/engine/networkhandler.h diff --git a/src/engine/networkhandler.cpp b/src/engine/networkhandler.cpp new file mode 100644 index 0000000..bab737d --- /dev/null +++ b/src/engine/networkhandler.cpp @@ -0,0 +1,73 @@ +/* + 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 + +#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) +{ + 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) + emit disconnected(); + else if (connectionState == CONNECTED) + 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)); +} diff --git a/src/engine/networkhandler.h b/src/engine/networkhandler.h new file mode 100644 index 0000000..d70410d --- /dev/null +++ b/src/engine/networkhandler.h @@ -0,0 +1,92 @@ +/* + 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 + +/** +* @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(); + +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 +}; + +#endif // NETWORKHANDLER_H diff --git a/src/src.pro b/src/src.pro index 81d3bae..089132a 100644 --- a/src/src.pro +++ b/src/src.pro @@ -41,7 +41,8 @@ SOURCES += main.cpp \ gps/gpsposition.cpp \ map/gpslocationitem.cpp \ ui/zoombuttonpanel.cpp \ - ui/userinfo.cpp + ui/userinfo.cpp \ + engine/networkhandler.cpp HEADERS += ui/mainwindow.h \ ui/mapviewscreen.h \ map/mapengine.h \ @@ -82,7 +83,8 @@ HEADERS += ui/mainwindow.h \ gps/gpscommon.h \ ui/zoombuttonpanel.h \ common.h \ - ui/userinfo.h + ui/userinfo.h \ + engine/networkhandler.h QT += network \ webkit -- 1.7.9.5