Class that watches for device state (locked or not).
authorMax Lapan <max.lapan@gmail.com>
Thu, 11 Mar 2010 13:33:23 +0000 (16:33 +0300)
committerMax Lapan <max.lapan@gmail.com>
Thu, 11 Mar 2010 13:33:23 +0000 (16:33 +0300)
devstate.cpp [new file with mode: 0644]
devstate.hpp [new file with mode: 0644]
tests/state/main.cpp [new file with mode: 0644]
tests/state/mainwindow.hpp [new file with mode: 0644]
tests/state/state.pro [new file with mode: 0644]
tests/tests.pro
yandex-traffic-core.pri

diff --git a/devstate.cpp b/devstate.cpp
new file mode 100644 (file)
index 0000000..496aa51
--- /dev/null
@@ -0,0 +1,70 @@
+#include <QtDBus>
+
+#include "devstate.hpp"
+
+#ifdef Q_WS_MAEMO_5
+#include <mce/dbus-names.h>
+#include <mce/mode-names.h>
+
+#define MCE_AVAILABLE 1
+#else
+#define MCE_AVAILABLE 0
+#endif
+
+static DeviceState *_instance = NULL;
+
+
+// --------------------------------------------------
+// DeviceState
+// --------------------------------------------------
+DeviceState* DeviceState::instance ()
+{
+    if (!_instance)
+        _instance = new DeviceState;
+    return _instance;
+}
+
+
+DeviceState::DeviceState ()
+    : _bus (QDBusConnection::systemBus ())
+{
+    _locked = false;
+
+#if MCE_AVAILABLE
+    _itf = new QDBusInterface (MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, _bus);
+    _bus.connect (MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_TKLOCK_MODE_SIG,
+                  this, SLOT (tkLockMessage (const QDBusMessage&)));
+    requestState ();
+#endif
+}
+
+
+void DeviceState::requestState ()
+{
+#if MCE_AVAILABLE
+    tkLockMessage (_itf->call (MCE_TKLOCK_MODE_GET));
+#endif
+}
+
+
+void DeviceState::tkLockMessage (const QDBusMessage &msg)
+{
+#if MCE_AVAILABLE
+    QString s;
+
+    if (msg.arguments ().count () > 0) {
+        s = msg.arguments ().value (0).toString ();
+        printf ("Reply = %s\n", s.toUtf8 ().data ());
+        setLocked (s == QString (MCE_TK_LOCKED));
+    }
+#endif
+}
+
+
+void DeviceState::setLocked (bool new_val)
+{
+    if (_locked != new_val) {
+        _locked = new_val;
+        lockChanged (_locked);
+    }
+}
diff --git a/devstate.hpp b/devstate.hpp
new file mode 100644 (file)
index 0000000..7d05813
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef __DEVSTATE_H__
+#define __DEVSTATE_H__
+
+#include <QtCore>
+#include <QtDBus>
+
+
+// Singleton which handles device lock state (and, possibly, other MCE stuff)
+class DeviceState : public QObject
+{
+    Q_OBJECT
+
+private:
+    bool _locked;
+
+    QDBusConnection _bus;
+    QDBusInterface *_itf;
+
+protected:
+    DeviceState ();
+
+    void setLocked (bool new_val);
+
+protected slots:
+    void tkLockMessage (const QDBusMessage &msg);
+
+public:
+    static DeviceState *instance ();
+
+    bool locked () const
+    { return _locked; };
+
+    void requestState ();
+
+signals:
+    void lockChanged (bool locked);
+};
+
+#endif // __DEVSTATE_H__
diff --git a/tests/state/main.cpp b/tests/state/main.cpp
new file mode 100644 (file)
index 0000000..2673345
--- /dev/null
@@ -0,0 +1,13 @@
+#include <QtGui>
+#include "mainwindow.hpp"
+
+
+int main(int argc, char *argv[])
+{
+    QApplication app (argc, argv);
+    MainWindow w;
+
+    w.show ();
+
+    return app.exec ();
+}
diff --git a/tests/state/mainwindow.hpp b/tests/state/mainwindow.hpp
new file mode 100644 (file)
index 0000000..c329557
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef __MAINWINDOW_H__
+#define __MAINWINDOW_H__
+
+#include <QtGui>
+
+#include "devstate.hpp"
+
+class MainWindow : public QPushButton
+{
+    Q_OBJECT
+
+protected slots:
+    void clicked ()
+    {
+        printf ("Clicked, request state\n");
+        DeviceState::instance ()->requestState ();
+    }
+
+    void lockChanged (bool state)
+    {
+        printf (state ? "device locked\n" : "device unlocked\n");
+    }
+
+public:
+    MainWindow ()
+        : QPushButton ()
+    {
+        setText ("Push me to get state");
+        connect (this, SIGNAL (clicked ()), this, SLOT (clicked ()));
+        connect (DeviceState::instance (), SIGNAL (lockChanged (bool)), this, SLOT (lockChanged (bool)));
+    }
+};
+
+#endif // __MAINWINDOW_H__
diff --git a/tests/state/state.pro b/tests/state/state.pro
new file mode 100644 (file)
index 0000000..07b4a99
--- /dev/null
@@ -0,0 +1,6 @@
+TEMPLATE = app
+
+SOURCES += main.cpp
+HEADERS += mainwindow.hpp
+
+include (../../yandex-traffic-core.pri)
index 0edec27..d4bc1c0 100644 (file)
@@ -1,2 +1,2 @@
 TEMPLATE = subdirs
-SUBDIRS = regions traffic widget conn
+SUBDIRS = regions traffic widget conn state
index 58e7c6f..279f265 100644 (file)
@@ -1,5 +1,5 @@
-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/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp $$PWD/connection.hpp $$PWD/devstate.hpp
+SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp $$PWD/connection.cpp $$PWD/devstate.cpp
 
 HEADERS += $$PWD/icd2_light.h $$PWD/globals.hpp