Class which monitors n900 connection state.
authorMax Lapan <max.lapan@gmail.com>
Wed, 10 Mar 2010 14:25:03 +0000 (17:25 +0300)
committerMax Lapan <max.lapan@gmail.com>
Wed, 10 Mar 2010 14:25:03 +0000 (17:25 +0300)
Finished. There is still an issue with request of connection state on class
creation, but broadcast messages are handled correctly.

connection.cpp
connection.hpp
icd2_light.h [new file with mode: 0644]
yandex-traffic-core.pri

index 18a2ee0..03207d5 100644 (file)
@@ -1,4 +1,8 @@
+#include <QtDBus>
+
 #include "connection.hpp"
+#include "icd2_light.h"
+
 
 static ConnectionChecker *_instance = NULL;
 
@@ -15,13 +19,40 @@ ConnectionChecker *ConnectionChecker::instance ()
 
 
 ConnectionChecker::ConnectionChecker ()
+    : _bus (QDBusConnection::systemBus ())
+{
+    _connected = true;
+
+    _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&)));
+
+    requestState ();
+}
+
+
+void ConnectionChecker::requestState ()
 {
-    // start timer which will check connection
-    startTimer (15*1000);
+    QDBusMessage msg = QDBusMessage::createSignal (ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_REQ);
+
+    _bus.send (msg);
 }
 
 
-void ConnectionChecker::timerEvent (QTimerEvent *)
+void ConnectionChecker::stateSignal (const QDBusMessage& msg)
 {
-    // check for connection
+    unsigned int status = msg.arguments ().value (7).value<unsigned int>();
+
+    if (status == ICD_STATE_CONNECTED) {
+        if (!_connected) {
+            _connected = true;
+            emit connected (true);
+        }
+    }
+    else {
+        if (_connected) {
+            _connected = false;
+            emit connected (false);
+        }
+    }
 }
index 1840ae8..5c0d85c 100644 (file)
@@ -2,22 +2,33 @@
 #define __CONNECTION_H__
 
 #include <QtCore>
+#include <QtDBus>
 
 
-// Singleton, which periodically checks for connection state and notifies when it changed.
+// Singleton, which listens for ICD DBUS events about connection change
 class ConnectionChecker : public QObject
 {
     Q_OBJECT
 
 private:
+    bool _connected;
+    QDBusConnection _bus;
+    QDBusInterface *_itf;
+
     ConnectionChecker ();
 
 protected:
-    void timerEvent (QTimerEvent *event);
+    void requestState ();
+
+protected slots:
+    void stateSignal (const QDBusMessage& msg);
 
 public:
     static ConnectionChecker *instance ();
 
+    bool isConnected () const
+    { return _connected; };
+
 signals:
     void connected (bool active);
 };
diff --git a/icd2_light.h b/icd2_light.h
new file mode 100644 (file)
index 0000000..d0a8da5
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef __ICD2_LIGHT_H__
+#define __ICD2_LIGHT_H__
+
+/*
+ * This is truncated version of file /usr/include/icd/dbus_api.h
+ * I created it because of glib.h inclusion in the original file
+ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ICD_DBUS_API_INTERFACE "com.nokia.icd2"
+#define ICD_DBUS_API_PATH      "/com/nokia/icd2"
+#define ICD_DBUS_API_STATE_SIG "state_sig"
+#define ICD_DBUS_API_STATE_REQ "state_req"
+
+
+/** Connection state */
+enum icd_connection_state {
+  /** Network is or became disconnected */
+  ICD_STATE_DISCONNECTED = 0,
+  /** Establishing network connection */
+  ICD_STATE_CONNECTING = 1,
+  /** Network is connected */
+  ICD_STATE_CONNECTED = 2,
+  /** Network is being disconnected */
+  ICD_STATE_DISCONNECTING = 3,
+  /** service provider module informs about enabled limited connectivity */
+  ICD_STATE_LIMITED_CONN_ENABLED = 4,
+  /** service provider module informs about disabled limited connectivity */
+  ICD_STATE_LIMITED_CONN_DISABLED = 5,
+
+  /** Network searching started */
+  ICD_STATE_SEARCH_START = 8,
+  /** Network searching stopped */
+  ICD_STATE_SEARCH_STOP = 9,
+
+  /** Internal network state, IP address(es) has/have been acquired */
+  ICD_STATE_INTERNAL_ADDRESS_ACQUIRED = 15
+};
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ICD2_LIGHT_H__ */
index 425c2b1..485b2c6 100644 (file)
@@ -1,6 +1,8 @@
 HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp $$PWD/connection.hpp
 SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp $$PWD/connection.cpp
 
+HEADERS += $$PWD/icd2_light.h
+
 QT += network xml dbus
 CONFIG += qdbus