From e65ec50de15866f192be96ec5d7bba7d4f5ba5a7 Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Thu, 11 Mar 2010 16:33:23 +0300 Subject: [PATCH] Class that watches for device state (locked or not). --- devstate.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++ devstate.hpp | 39 ++++++++++++++++++++++++ tests/state/main.cpp | 13 ++++++++ tests/state/mainwindow.hpp | 34 +++++++++++++++++++++ tests/state/state.pro | 6 ++++ tests/tests.pro | 2 +- yandex-traffic-core.pri | 4 +-- 7 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 devstate.cpp create mode 100644 devstate.hpp create mode 100644 tests/state/main.cpp create mode 100644 tests/state/mainwindow.hpp create mode 100644 tests/state/state.pro diff --git a/devstate.cpp b/devstate.cpp new file mode 100644 index 0000000..496aa51 --- /dev/null +++ b/devstate.cpp @@ -0,0 +1,70 @@ +#include + +#include "devstate.hpp" + +#ifdef Q_WS_MAEMO_5 +#include +#include + +#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 index 0000000..7d05813 --- /dev/null +++ b/devstate.hpp @@ -0,0 +1,39 @@ +#ifndef __DEVSTATE_H__ +#define __DEVSTATE_H__ + +#include +#include + + +// 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 index 0000000..2673345 --- /dev/null +++ b/tests/state/main.cpp @@ -0,0 +1,13 @@ +#include +#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 index 0000000..c329557 --- /dev/null +++ b/tests/state/mainwindow.hpp @@ -0,0 +1,34 @@ +#ifndef __MAINWINDOW_H__ +#define __MAINWINDOW_H__ + +#include + +#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 index 0000000..07b4a99 --- /dev/null +++ b/tests/state/state.pro @@ -0,0 +1,6 @@ +TEMPLATE = app + +SOURCES += main.cpp +HEADERS += mainwindow.hpp + +include (../../yandex-traffic-core.pri) diff --git a/tests/tests.pro b/tests/tests.pro index 0edec27..d4bc1c0 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS = regions traffic widget conn +SUBDIRS = regions traffic widget conn state diff --git a/yandex-traffic-core.pri b/yandex-traffic-core.pri index 58e7c6f..279f265 100644 --- a/yandex-traffic-core.pri +++ b/yandex-traffic-core.pri @@ -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 -- 1.7.9.5