Added network error flag to NetworkReply.
[situare] / src / network / networkhandlerprivate.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 <QDebug>
23
24 #include <icd/dbus_api.h>
25
26 #include "networkhandler.h"
27 #include "networkhandlerprivate.h"
28
29 const int CONNECTION_STATE_INDEX = 7;
30
31 static QDBusConnection dBusConnection = QDBusConnection::systemBus();
32
33 NetworkHandlerPrivate::NetworkHandlerPrivate(QObject *parent)
34     : QObject(parent),
35       m_connected(false),
36       m_connecting(false)
37 {
38     qDebug() << __PRETTY_FUNCTION__;
39
40     m_parent = static_cast<NetworkHandler*>(parent);
41
42     dBusInterface = new QDBusInterface(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
43                                        ICD_DBUS_API_INTERFACE, dBusConnection);
44
45     dBusConnection.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
46                            ICD_DBUS_API_STATE_SIG,
47                            this, SLOT(stateChanged(const QDBusMessage &)));
48
49     state();
50 }
51
52 void NetworkHandlerPrivate::stateChanged(const QDBusMessage &message)
53 {
54     qDebug() << __PRETTY_FUNCTION__ << message;
55
56     if (message.arguments().count() > CONNECTION_STATE_INDEX) {
57
58         bool conversionOk;
59         int connectionState = message.arguments().at(CONNECTION_STATE_INDEX).toInt(&conversionOk);
60
61         if (conversionOk) {
62             if ((connectionState == ICD_STATE_DISCONNECTED) && (isConnected())) {
63                 m_connected = false;
64                 m_connecting = false;
65                 emit m_parent->disconnected();
66             }
67             if (connectionState == ICD_STATE_DISCONNECTING) {
68                 m_connecting = false;
69             }
70             else if ((connectionState == ICD_STATE_CONNECTED) &&(!isConnected())) {
71                 m_connected = true;
72                 m_connecting = false;
73                 emit m_parent->connected();
74             }
75         }
76     }
77 }
78
79 void NetworkHandlerPrivate::connect()
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     if (!m_connecting && !m_connected) {
84         m_connecting = true;
85         dBusInterface->call(ICD_DBUS_API_CONNECT_REQ,
86                             QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
87     }
88 }
89
90 void NetworkHandlerPrivate::disconnect()
91 {
92     qDebug() << __PRETTY_FUNCTION__;
93
94     dBusInterface->call(ICD_DBUS_API_DISCONNECT_REQ,
95                         QVariant((unsigned int)ICD_CONNECTION_FLAG_USER_EVENT));
96 }
97
98 bool NetworkHandlerPrivate::isConnected()
99 {
100     qDebug() << __PRETTY_FUNCTION__ << m_connected;
101
102     return m_connected;
103 }
104
105 void NetworkHandlerPrivate::state()
106 {
107     qDebug() << __PRETTY_FUNCTION__;
108
109     dBusInterface->call(ICD_DBUS_API_STATE_REQ);
110 }