Connection tracker class finished.
authorMax Lapan <max.lapan@gmail.com>
Wed, 10 Mar 2010 18:31:02 +0000 (21:31 +0300)
committerMax Lapan <max.lapan@gmail.com>
Wed, 10 Mar 2010 18:31:02 +0000 (21:31 +0300)
connection.cpp
connection.hpp
tests/conn/mainwindow.hpp

index 03207d5..e60590a 100644 (file)
@@ -33,9 +33,11 @@ ConnectionChecker::ConnectionChecker ()
 
 void ConnectionChecker::requestState ()
 {
-    QDBusMessage msg = QDBusMessage::createSignal (ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_REQ);
+    QDBusMessage reply = _itf->call (ICD_DBUS_API_STATE_REQ);
 
-    _bus.send (msg);
+    // If there is no connection, we get no reply at all
+    if (!reply.arguments ().value (0).toUInt ())
+        updateState (false);
 }
 
 
@@ -43,16 +45,14 @@ void ConnectionChecker::stateSignal (const QDBusMessage& msg)
 {
     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);
-        }
+    updateState (status == ICD_STATE_CONNECTED);
+}
+
+
+void ConnectionChecker::updateState (bool new_state)
+{
+    if (new_state != _connected) {
+        _connected = new_state;
+        emit connected (_connected);
     }
 }
index 5c0d85c..501adda 100644 (file)
@@ -15,10 +15,10 @@ private:
     QDBusConnection _bus;
     QDBusInterface *_itf;
 
+protected:
     ConnectionChecker ();
 
-protected:
-    void requestState ();
+    void updateState (bool new_state);
 
 protected slots:
     void stateSignal (const QDBusMessage& msg);
@@ -29,6 +29,8 @@ public:
     bool isConnected () const
     { return _connected; };
 
+    void requestState ();
+
 signals:
     void connected (bool active);
 };
index 0892bef..c854ff0 100644 (file)
@@ -4,16 +4,19 @@
 #include <QtGui>
 #include <connection.hpp>
 
-class MainWindow : public QWidget
+class MainWindow : public QPushButton
 {
     Q_OBJECT
 public:
     MainWindow ()
-        : QWidget ()
+        : QPushButton ()
     {
         ConnectionChecker *cc = ConnectionChecker::instance ();
 
         connect (cc, SIGNAL (connected (bool)), SLOT (connected (bool)));
+
+        setText (cc->isConnected () ? "Connected" : "Not connected");
+        connect (this, SIGNAL (clicked ()), SLOT (checkConnection ()));
     }
 
 protected slots:
@@ -23,6 +26,12 @@ protected slots:
             printf ("Device connected\n");
         else
             printf ("Device not connected\n");
+        setText (ConnectionChecker::instance ()->isConnected () ? "Connected" : "Not connected");
+    }
+
+    void checkConnection ()
+    {
+        ConnectionChecker::instance ()->requestState ();
     }
 };