Fix typo
[yandex-traffic] / connection.cpp
index a487364..dc959fa 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "connection.hpp"
 #include "icd2_light.h"
-
+#include "log.hpp"
 
 static ConnectionChecker *_instance = NULL;
 
@@ -26,6 +26,7 @@ ConnectionChecker::ConnectionChecker ()
     _itf = new QDBusInterface (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, _bus);
     _bus.connect (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
                   this, SLOT (stateSignal (const QDBusMessage&)));
+    _conn_counter = 0;
 
     requestState ();
 }
@@ -43,11 +44,26 @@ void ConnectionChecker::requestState ()
 
 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
 {
-    if (msg.arguments ().count () == 8) {
-        unsigned int status = msg.arguments ().value (7).value<unsigned int>();
+    if (msg.arguments ().count () != 8)
+        return;
 
-        updateState (status == ICD_STATE_CONNECTED, msg.arguments ().value (3).toString ());
-    }
+    unsigned int state = msg.arguments ().value (7).value<unsigned int>();
+    QString net = msg.arguments ().value (3).toString ();
+
+    if (state == ICD_STATE_CONNECTED)
+        _conn_counter++;
+    if (state == ICD_STATE_DISCONNECTED)
+        _conn_counter--;
+
+    if (_conn_counter > 1)
+        _conn_counter = 1;
+    if (_conn_counter < 0)
+        _conn_counter = 0;
+
+    Log::instance ()->add (QString ("stateSignal: state = %1, net = %2, counter = %3").arg (state).arg (net).arg (_conn_counter));
+
+    if (state == ICD_STATE_CONNECTED || !_conn_counter)
+        updateState (state == ICD_STATE_CONNECTED, net);
 }
 
 
@@ -55,6 +71,8 @@ void ConnectionChecker::updateState (bool new_state, const QString& net_type)
 {
     network_type_t new_net = Net_None;
 
+    Log::instance ()->add (QString ("ConnectionChecker::updateState (%1, %2)").arg (new_state ? "connected" : "not connected").arg (net_type));
+
     if (new_state != _connected) {
         _connected = new_state;
         emit connected (_connected);
@@ -65,9 +83,32 @@ void ConnectionChecker::updateState (bool new_state, const QString& net_type)
             new_net = Net_WLAN;
         else if (net_type.startsWith ("GPRS") || net_type.startsWith ("DUN_GSM"))
             new_net = Net_GSM;
-        if (new_net != _net_type) {
-            _net_type = new_net;
-            type_changed (_net_type);
-        }
+    }
+
+    if (new_net != _net_type) {
+        _net_type = new_net;
+        type_changed (_net_type);
+    }
+}
+
+
+bool ConnectionChecker::checkConnection (bool allow_gsm, bool allow_wifi)
+{
+    if (!_connected)
+        return false;
+
+    switch (_net_type) {
+        case Net_None:
+            Log::instance ()->add ("checkConnection: Net_None, allow");
+            return true;
+        case Net_WLAN:
+            Log::instance ()->add (QString ("checkConnection: Net_WLAN, allow = %1").arg (allow_wifi ? "true" : "false"));
+            return allow_wifi;
+        case Net_GSM:
+            Log::instance ()->add (QString ("checkConnection: Net_GSM, allow = %1").arg (allow_gsm ? "true" : "false"));
+            return allow_gsm;
+        default:
+            Log::instance ()->add ("checkConnection: unknown, allow");
+            return true;
     }
 }