From: Max Lapan Date: Wed, 10 Mar 2010 18:31:02 +0000 (+0300) Subject: Connection tracker class finished. X-Git-Tag: v0.1~49 X-Git-Url: https://vcs.maemo.org/git/?p=yandex-traffic;a=commitdiff_plain;h=2af109727c2bbd289adc872dfc52b92a111a1e37 Connection tracker class finished. --- diff --git a/connection.cpp b/connection.cpp index 03207d5..e60590a 100644 --- a/connection.cpp +++ b/connection.cpp @@ -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(); - 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); } } diff --git a/connection.hpp b/connection.hpp index 5c0d85c..501adda 100644 --- a/connection.hpp +++ b/connection.hpp @@ -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); }; diff --git a/tests/conn/mainwindow.hpp b/tests/conn/mainwindow.hpp index 0892bef..c854ff0 100644 --- a/tests/conn/mainwindow.hpp +++ b/tests/conn/mainwindow.hpp @@ -4,16 +4,19 @@ #include #include -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 (); } };