Determine kind of internet connection we are using GSM or WLAN.
authorMax Lapan <max.lapan@gmail.com>
Tue, 16 Mar 2010 10:37:34 +0000 (13:37 +0300)
committerMax Lapan <max.lapan@gmail.com>
Tue, 16 Mar 2010 10:37:34 +0000 (13:37 +0300)
connection.cpp
connection.hpp
tests/conn/mainwindow.hpp

index e60590a..a487364 100644 (file)
@@ -43,16 +43,31 @@ void ConnectionChecker::requestState ()
 
 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
 {
-    unsigned int status = msg.arguments ().value (7).value<unsigned int>();
+    if (msg.arguments ().count () == 8) {
+        unsigned int status = msg.arguments ().value (7).value<unsigned int>();
 
-    updateState (status == ICD_STATE_CONNECTED);
+        updateState (status == ICD_STATE_CONNECTED, msg.arguments ().value (3).toString ());
+    }
 }
 
 
-void ConnectionChecker::updateState (bool new_state)
+void ConnectionChecker::updateState (bool new_state, const QString& net_type)
 {
+    network_type_t new_net = Net_None;
+
     if (new_state != _connected) {
         _connected = new_state;
         emit connected (_connected);
     }
+
+    if (_connected) {
+        if (net_type.startsWith ("WLAN"))
+            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);
+        }
+    }
 }
index 501adda..3d6bd8c 100644 (file)
@@ -10,15 +10,24 @@ class ConnectionChecker : public QObject
 {
     Q_OBJECT
 
+public:
+    enum network_type_t {
+        Net_None,
+        Net_WLAN,
+        Net_GSM,
+    };
+
 private:
     bool _connected;
+    network_type_t _net_type;
+
     QDBusConnection _bus;
     QDBusInterface *_itf;
 
 protected:
     ConnectionChecker ();
 
-    void updateState (bool new_state);
+    void updateState (bool new_state, const QString& net_type = QString ());
 
 protected slots:
     void stateSignal (const QDBusMessage& msg);
@@ -29,10 +38,14 @@ public:
     bool isConnected () const
     { return _connected; };
 
+    network_type_t network_type () const
+    { return _net_type; };
+
     void requestState ();
 
 signals:
     void connected (bool active);
+    void type_changed (ConnectionChecker::network_type_t type);
 };
 
 #endif // __CONNECTION_H__
index c854ff0..f0c8f9b 100644 (file)
@@ -14,6 +14,7 @@ public:
         ConnectionChecker *cc = ConnectionChecker::instance ();
 
         connect (cc, SIGNAL (connected (bool)), SLOT (connected (bool)));
+        connect (cc, SIGNAL (type_changed (ConnectionChecker::network_type_t)), SLOT (type_changed (ConnectionChecker::network_type_t)));
 
         setText (cc->isConnected () ? "Connected" : "Not connected");
         connect (this, SIGNAL (clicked ()), SLOT (checkConnection ()));
@@ -29,6 +30,21 @@ protected slots:
         setText (ConnectionChecker::instance ()->isConnected () ? "Connected" : "Not connected");
     }
 
+    void type_changed (ConnectionChecker::network_type_t type)
+    {
+        switch (type) {
+            case ConnectionChecker::Net_None:
+                printf ("Type: none\n");
+                break;
+            case ConnectionChecker::Net_WLAN:
+                printf ("Type: WLAN\n");
+                break;
+            case ConnectionChecker::Net_GSM:
+                printf ("Type: GSM\n");
+                break;
+        }
+    }
+
     void checkConnection ()
     {
         ConnectionChecker::instance ()->requestState ();