3 #include "connection.hpp"
4 #include "icd2_light.h"
7 static ConnectionChecker *_instance = NULL;
10 // --------------------------------------------------
11 // ConnectionChecker singleton
12 // --------------------------------------------------
13 ConnectionChecker *ConnectionChecker::instance ()
16 _instance = new ConnectionChecker;
21 ConnectionChecker::ConnectionChecker ()
22 : _bus (QDBusConnection::systemBus ())
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&)));
35 void ConnectionChecker::requestState ()
37 QDBusMessage reply = _itf->call (ICD_DBUS_API_STATE_REQ);
39 // If there is no connection, we get no reply at all
40 if (!reply.arguments ().value (0).toUInt ())
45 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
47 if (msg.arguments ().count () != 8)
50 unsigned int state = msg.arguments ().value (7).value<unsigned int>();
51 QString net = msg.arguments ().value (3).toString ();
53 if (state == ICD_STATE_CONNECTED)
55 if (state == ICD_STATE_DISCONNECTED)
58 if (_conn_counter > 1)
60 if (_conn_counter < 0)
63 Log::instance ()->add (QString ("stateSignal: state = %1, net = %2, counter = %3").arg (state).arg (net).arg (_conn_counter));
65 if (state == ICD_STATE_CONNECTED || !_conn_counter)
66 updateState (state == ICD_STATE_CONNECTED, net);
70 void ConnectionChecker::updateState (bool new_state, const QString& net_type)
72 network_type_t new_net = Net_None;
74 Log::instance ()->add (QString ("ConnectionChecker::updateState (%1, %2)").arg (new_state ? "connected" : "not connected").arg (net_type));
76 if (new_state != _connected) {
77 _connected = new_state;
78 emit connected (_connected);
82 if (net_type.startsWith ("WLAN"))
84 else if (net_type.startsWith ("GPRS") || net_type.startsWith ("DUN_GSM"))
88 if (new_net != _net_type) {
90 type_changed (_net_type);
95 bool ConnectionChecker::checkConnection (bool allow_gsm, bool allow_wifi)
102 Log::instance ()->add ("checkConnection: Net_None, allow");
105 Log::instance ()->add (QString ("checkConnection: Net_WLAN, allow = %1").arg (allow_wifi ? "true" : "false"));
108 Log::instance ()->add (QString ("checkConnection: Net_GSM, allow = %1").arg (allow_gsm ? "true" : "false"));
111 Log::instance ()->add ("checkConnection: unknown, allow");