5b9c7247e888d85bcb323bf456c0a8cbf6b66d76
[situare] / src / network / networkhandler.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <icd/dbus_api.h>
23
24 #include "networkhandler.h"
25
26 static QDBusConnection dBusConnection = QDBusConnection::systemBus();
27 const int CONNECTION_STATE_INDEX = 7;
28
29 NetworkHandler::NetworkHandler(QObject *parent)
30     : QObject(parent),
31       m_connected(false),
32       m_connecting(false)
33 {
34     dBusInterface = new QDBusInterface(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
35                                        ICD_DBUS_API_INTERFACE, dBusConnection);
36
37     dBusConnection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
38                            ICD_DBUS_API_STATE_SIG,
39                            this, SLOT(stateChanged(const QDBusMessage &)));
40
41     state();
42 }
43
44 void NetworkHandler::stateChanged(const QDBusMessage &message)
45 {
46     qDebug() << __PRETTY_FUNCTION__ << message.arguments();
47
48     if (message.arguments().count() >= 8) {
49
50         bool conversionOk;
51         int connectionState = message.arguments().at(CONNECTION_STATE_INDEX).toInt(&conversionOk);
52         qWarning() << __PRETTY_FUNCTION__ << "Connection state: " << connectionState;
53
54         if (conversionOk) {
55             if ((connectionState == ICD_STATE_DISCONNECTED) && (isConnected())) {
56                 m_connected = false;
57                 m_connecting = false;
58                 emit disconnected();
59             }
60             else if ((connectionState == ICD_STATE_CONNECTED) &&(!isConnected())) {
61                 m_connected = true;
62                 m_connecting = false;
63                 emit connected();
64             }
65         }
66     }
67 }
68
69 void NetworkHandler::connect()
70 {
71     qDebug() << __PRETTY_FUNCTION__;
72
73     if (!m_connecting && !m_connected) {
74         m_connecting = true;
75         dBusInterface->call(ICD_DBUS_API_CONNECT_REQ,
76                             QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
77     }
78 }
79
80 void NetworkHandler::disconnect()
81 {
82     qDebug() << __PRETTY_FUNCTION__;
83
84     dBusInterface->call(ICD_DBUS_API_DISCONNECT_REQ,
85                         QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
86 }
87
88 bool NetworkHandler::isConnected()
89 {
90     qDebug() << __PRETTY_FUNCTION__;
91
92     return m_connected;
93 }
94
95 void NetworkHandler::state()
96 {
97     qDebug() << __PRETTY_FUNCTION__;
98
99     dBusInterface->call(ICD_DBUS_API_STATE_REQ);
100 }