Determine kind of internet connection we are using GSM or WLAN.
[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     if (msg.arguments ().count () == 8) {
47         unsigned int status = msg.arguments ().value (7).value<unsigned int>();
48
49         updateState (status == ICD_STATE_CONNECTED, msg.arguments ().value (3).toString ());
50     }
51 }
52
53
54 void ConnectionChecker::updateState (bool new_state, const QString& net_type)
55 {
56     network_type_t new_net = Net_None;
57
58     if (new_state != _connected) {
59         _connected = new_state;
60         emit connected (_connected);
61     }
62
63     if (_connected) {
64         if (net_type.startsWith ("WLAN"))
65             new_net = Net_WLAN;
66         else if (net_type.startsWith ("GPRS") || net_type.startsWith ("DUN_GSM"))
67             new_net = Net_GSM;
68         if (new_net != _net_type) {
69             _net_type = new_net;
70             type_changed (_net_type);
71         }
72     }
73 }