Fix typo
[yandex-traffic] / connection.cpp
1 #include <QtDBus>
2
3 #include "connection.hpp"
4 #include "icd2_light.h"
5 #include "log.hpp"
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     _conn_counter = 0;
30
31     requestState ();
32 }
33
34
35 void ConnectionChecker::requestState ()
36 {
37     QDBusMessage reply = _itf->call (ICD_DBUS_API_STATE_REQ);
38
39     // If there is no connection, we get no reply at all
40     if (!reply.arguments ().value (0).toUInt ())
41         updateState (false);
42 }
43
44
45 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
46 {
47     if (msg.arguments ().count () != 8)
48         return;
49
50     unsigned int state = msg.arguments ().value (7).value<unsigned int>();
51     QString net = msg.arguments ().value (3).toString ();
52
53     if (state == ICD_STATE_CONNECTED)
54         _conn_counter++;
55     if (state == ICD_STATE_DISCONNECTED)
56         _conn_counter--;
57
58     if (_conn_counter > 1)
59         _conn_counter = 1;
60     if (_conn_counter < 0)
61         _conn_counter = 0;
62
63     Log::instance ()->add (QString ("stateSignal: state = %1, net = %2, counter = %3").arg (state).arg (net).arg (_conn_counter));
64
65     if (state == ICD_STATE_CONNECTED || !_conn_counter)
66         updateState (state == ICD_STATE_CONNECTED, net);
67 }
68
69
70 void ConnectionChecker::updateState (bool new_state, const QString& net_type)
71 {
72     network_type_t new_net = Net_None;
73
74     Log::instance ()->add (QString ("ConnectionChecker::updateState (%1, %2)").arg (new_state ? "connected" : "not connected").arg (net_type));
75
76     if (new_state != _connected) {
77         _connected = new_state;
78         emit connected (_connected);
79     }
80
81     if (_connected) {
82         if (net_type.startsWith ("WLAN"))
83             new_net = Net_WLAN;
84         else if (net_type.startsWith ("GPRS") || net_type.startsWith ("DUN_GSM"))
85             new_net = Net_GSM;
86     }
87
88     if (new_net != _net_type) {
89         _net_type = new_net;
90         type_changed (_net_type);
91     }
92 }
93
94
95 bool ConnectionChecker::checkConnection (bool allow_gsm, bool allow_wifi)
96 {
97     if (!_connected)
98         return false;
99
100     switch (_net_type) {
101         case Net_None:
102             Log::instance ()->add ("checkConnection: Net_None, allow");
103             return true;
104         case Net_WLAN:
105             Log::instance ()->add (QString ("checkConnection: Net_WLAN, allow = %1").arg (allow_wifi ? "true" : "false"));
106             return allow_wifi;
107         case Net_GSM:
108             Log::instance ()->add (QString ("checkConnection: Net_GSM, allow = %1").arg (allow_gsm ? "true" : "false"));
109             return allow_gsm;
110         default:
111             Log::instance ()->add ("checkConnection: unknown, allow");
112             return true;
113     }
114 }