Connection tracker class finished.
[yandex-traffic] / connection.cpp
1 #include <QtDBus>
2
3 #include "connection.hpp"
4 #include "icd2_light.h"
5
6
7 static ConnectionChecker *_instance = NULL;
8
9
10 // --------------------------------------------------
11 // ConnectionChecker singleton
12 // --------------------------------------------------
13 ConnectionChecker *ConnectionChecker::instance ()
14 {
15     if (!_instance)
16         _instance = new ConnectionChecker;
17     return _instance;
18 }
19
20
21 ConnectionChecker::ConnectionChecker ()
22     : _bus (QDBusConnection::systemBus ())
23 {
24     _connected = true;
25
26     _itf = new QDBusInterface (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, _bus);
27     _bus.connect (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
28                   this, SLOT (stateSignal (const QDBusMessage&)));
29
30     requestState ();
31 }
32
33
34 void ConnectionChecker::requestState ()
35 {
36     QDBusMessage reply = _itf->call (ICD_DBUS_API_STATE_REQ);
37
38     // If there is no connection, we get no reply at all
39     if (!reply.arguments ().value (0).toUInt ())
40         updateState (false);
41 }
42
43
44 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
45 {
46     unsigned int status = msg.arguments ().value (7).value<unsigned int>();
47
48     updateState (status == ICD_STATE_CONNECTED);
49 }
50
51
52 void ConnectionChecker::updateState (bool new_state)
53 {
54     if (new_state != _connected) {
55         _connected = new_state;
56         emit connected (_connected);
57     }
58 }