From 406332eb6b3199d19388f359d04c9f184e6082b5 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Thu, 9 Aug 2012 04:03:20 +0200 Subject: watch status UI --- sowatchui/daemonproxy.cpp | 26 ++++ sowatchui/daemonproxy.h | 63 ++++++++ sowatchui/main.cpp | 2 + .../qmlapplicationviewer/qmlapplicationviewer.cpp | 5 + .../qmlapplicationviewer/qmlapplicationviewer.h | 2 + sowatchui/scannerproxy.cc | 26 ---- sowatchui/scannerproxy.cpp | 26 ++++ sowatchui/scanwatchesmodel.cc | 102 ------------- sowatchui/scanwatchesmodel.cpp | 102 +++++++++++++ sowatchui/sowatchui.pro | 9 +- sowatchui/watchesmodel.cc | 136 ----------------- sowatchui/watchesmodel.cpp | 166 +++++++++++++++++++++ sowatchui/watchesmodel.h | 6 + 13 files changed, 404 insertions(+), 267 deletions(-) create mode 100644 sowatchui/daemonproxy.cpp create mode 100644 sowatchui/daemonproxy.h delete mode 100644 sowatchui/scannerproxy.cc create mode 100644 sowatchui/scannerproxy.cpp delete mode 100644 sowatchui/scanwatchesmodel.cc create mode 100644 sowatchui/scanwatchesmodel.cpp delete mode 100644 sowatchui/watchesmodel.cc create mode 100644 sowatchui/watchesmodel.cpp (limited to 'sowatchui') diff --git a/sowatchui/daemonproxy.cpp b/sowatchui/daemonproxy.cpp new file mode 100644 index 0000000..6a4aee2 --- /dev/null +++ b/sowatchui/daemonproxy.cpp @@ -0,0 +1,26 @@ +/* + * This file was generated by qdbusxml2cpp version 0.7 + * Command line was: qdbusxml2cpp -c DaemonProxy -p daemonproxy ../sowatchd/daemon.xml + * + * qdbusxml2cpp is Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * + * This is an auto-generated file. + * This file may have been hand-edited. Look for HAND-EDIT comments + * before re-generating it. + */ + +#include "daemonproxy.h" + +/* + * Implementation of interface class DaemonProxy + */ + +DaemonProxy::DaemonProxy(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) + : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) +{ +} + +DaemonProxy::~DaemonProxy() +{ +} + diff --git a/sowatchui/daemonproxy.h b/sowatchui/daemonproxy.h new file mode 100644 index 0000000..5083cf3 --- /dev/null +++ b/sowatchui/daemonproxy.h @@ -0,0 +1,63 @@ +/* + * This file was generated by qdbusxml2cpp version 0.7 + * Command line was: qdbusxml2cpp -c DaemonProxy -p daemonproxy ../sowatchd/daemon.xml + * + * qdbusxml2cpp is Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * + * This is an auto-generated file. + * Do not edit! All changes made to it will be lost. + */ + +#ifndef DAEMONPROXY_H_1344472584 +#define DAEMONPROXY_H_1344472584 + +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Proxy class for interface com.javispedro.sowatch.Daemon + */ +class DaemonProxy: public QDBusAbstractInterface +{ + Q_OBJECT +public: + static inline const char *staticInterfaceName() + { return "com.javispedro.sowatch.Daemon"; } + +public: + DaemonProxy(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent = 0); + + ~DaemonProxy(); + +public Q_SLOTS: // METHODS + inline QDBusPendingReply GetWatchStatus(const QString &watch) + { + QList argumentList; + argumentList << qVariantFromValue(watch); + return asyncCallWithArgumentList(QLatin1String("GetWatchStatus"), argumentList); + } + + inline QDBusPendingReply<> Terminate() + { + QList argumentList; + return asyncCallWithArgumentList(QLatin1String("Terminate"), argumentList); + } + +Q_SIGNALS: // SIGNALS + void WatchStatusChanged(const QString &watch, const QString &status); +}; + +namespace com { + namespace javispedro { + namespace sowatch { + typedef ::DaemonProxy Daemon; + } + } +} +#endif diff --git a/sowatchui/main.cpp b/sowatchui/main.cpp index f306fa9..ea4a172 100644 --- a/sowatchui/main.cpp +++ b/sowatchui/main.cpp @@ -19,6 +19,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) watches = new WatchesModel(app.data()); watchScanner = new ScanWatchesModel(app.data()); + qDebug() << "Starting" << watches << endl; + viewer->rootContext()->setContextProperty("watches", watches); viewer->rootContext()->setContextProperty("watchScanner", watchScanner); diff --git a/sowatchui/qmlapplicationviewer/qmlapplicationviewer.cpp b/sowatchui/qmlapplicationviewer/qmlapplicationviewer.cpp index 8ba6e88..672abd8 100644 --- a/sowatchui/qmlapplicationviewer/qmlapplicationviewer.cpp +++ b/sowatchui/qmlapplicationviewer/qmlapplicationviewer.cpp @@ -125,6 +125,11 @@ QmlApplicationViewer *QmlApplicationViewer::create() #endif } +QDeclarativeContext * QmlApplicationViewer::rootContext() +{ + return d->view->rootContext(); +} + void QmlApplicationViewer::setMainQmlFile(const QString &file) { d->mainQmlFile = d->adjustPath(file); diff --git a/sowatchui/qmlapplicationviewer/qmlapplicationviewer.h b/sowatchui/qmlapplicationviewer/qmlapplicationviewer.h index f8008f5..84fa3db 100644 --- a/sowatchui/qmlapplicationviewer/qmlapplicationviewer.h +++ b/sowatchui/qmlapplicationviewer/qmlapplicationviewer.h @@ -29,6 +29,8 @@ public: static QmlApplicationViewer *create(); + QDeclarativeContext *rootContext(); + void setMainQmlFile(const QString &file); void addImportPath(const QString &path); diff --git a/sowatchui/scannerproxy.cc b/sowatchui/scannerproxy.cc deleted file mode 100644 index cfa4365..0000000 --- a/sowatchui/scannerproxy.cc +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file was generated by qdbusxml2cpp version 0.7 - * Command line was: qdbusxml2cpp -c ScannerProxy -p scannerproxy ../sowatchd/scanner.xml - * - * qdbusxml2cpp is Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). - * - * This is an auto-generated file. - * This file may have been hand-edited. Look for HAND-EDIT comments - * before re-generating it. - */ - -#include "scannerproxy.h" - -/* - * Implementation of interface class ScannerProxy - */ - -ScannerProxy::ScannerProxy(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) - : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) -{ -} - -ScannerProxy::~ScannerProxy() -{ -} - diff --git a/sowatchui/scannerproxy.cpp b/sowatchui/scannerproxy.cpp new file mode 100644 index 0000000..cfa4365 --- /dev/null +++ b/sowatchui/scannerproxy.cpp @@ -0,0 +1,26 @@ +/* + * This file was generated by qdbusxml2cpp version 0.7 + * Command line was: qdbusxml2cpp -c ScannerProxy -p scannerproxy ../sowatchd/scanner.xml + * + * qdbusxml2cpp is Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * + * This is an auto-generated file. + * This file may have been hand-edited. Look for HAND-EDIT comments + * before re-generating it. + */ + +#include "scannerproxy.h" + +/* + * Implementation of interface class ScannerProxy + */ + +ScannerProxy::ScannerProxy(const QString &service, const QString &path, const QDBusConnection &connection, QObject *parent) + : QDBusAbstractInterface(service, path, staticInterfaceName(), connection, parent) +{ +} + +ScannerProxy::~ScannerProxy() +{ +} + diff --git a/sowatchui/scanwatchesmodel.cc b/sowatchui/scanwatchesmodel.cc deleted file mode 100644 index 5b7c331..0000000 --- a/sowatchui/scanwatchesmodel.cc +++ /dev/null @@ -1,102 +0,0 @@ -#include - -#include "scanwatchesmodel.h" - -ScanWatchesModel::ScanWatchesModel(QObject *parent) : - QAbstractListModel(parent), - _scanner(new ScannerProxy("com.javispedro.sowatchd", "/com/javispedro/sowatch/allscanner", QDBusConnection::sessionBus())), - _timer(new QTimer(this)), - _enabled(false), _active(false) -{ - QHash roles = roleNames(); - roles[Qt::DisplayRole] = QByteArray("title"); - roles[Qt::StatusTipRole] = QByteArray("subtitle"); - roles[ObjectRole] = QByteArray("object"); - setRoleNames(roles); - - _timer->setSingleShot(true); - _timer->setInterval(3000); - - connect(_scanner, SIGNAL(WatchFound(QVariantMap)), SLOT(handleWatchFound(QVariantMap))); - connect(_scanner, SIGNAL(Started()), SLOT(handleStarted())); - connect(_scanner, SIGNAL(Finished()), SLOT(handleFinished())); - connect(_timer, SIGNAL(timeout()), SLOT(handleTimeout())); -} - -ScanWatchesModel::~ScanWatchesModel() -{ -} - -bool ScanWatchesModel::enabled() const -{ - return _enabled; -} - -void ScanWatchesModel::setEnabled(bool enabled) -{ - _timer->stop(); - - _enabled = enabled; - - if (_enabled && !_active) { - _scanner->Start(); - } -} - -bool ScanWatchesModel::active() const -{ - return _active; -} - -int ScanWatchesModel::rowCount(const QModelIndex &parent) const -{ - return _list.count(); -} - -QVariant ScanWatchesModel::data(const QModelIndex &index, int role) const -{ - qDebug() << "Asked for data" << index.row() << index.column() << role; - const QVariantMap &info = _list.at(index.row()); - switch (role) { - case Qt::DisplayRole: - return info["name"]; - case Qt::StatusTipRole: - return info["address"]; - case ObjectRole: - return QVariant::fromValue(info); - } - return QVariant(); -} - -void ScanWatchesModel::handleWatchFound(const QVariantMap &info) -{ - qDebug() << "Watch found" << info << endl; - if (!_list.contains(info)) { - int count = _list.count(); - beginInsertRows(QModelIndex(), count, count); - _list.append(info); - endInsertRows(); - } -} - -void ScanWatchesModel::handleStarted() -{ - _active = true; - emit activeChanged(); -} - -void ScanWatchesModel::handleFinished() -{ - qDebug() << "Scan finished"; - _active = false; - if (_enabled) { - _timer->start(); - } - emit activeChanged(); -} - -void ScanWatchesModel::handleTimeout() -{ - qDebug() << "Restarting scan"; - _scanner->Start(); -} diff --git a/sowatchui/scanwatchesmodel.cpp b/sowatchui/scanwatchesmodel.cpp new file mode 100644 index 0000000..5b7c331 --- /dev/null +++ b/sowatchui/scanwatchesmodel.cpp @@ -0,0 +1,102 @@ +#include + +#include "scanwatchesmodel.h" + +ScanWatchesModel::ScanWatchesModel(QObject *parent) : + QAbstractListModel(parent), + _scanner(new ScannerProxy("com.javispedro.sowatchd", "/com/javispedro/sowatch/allscanner", QDBusConnection::sessionBus())), + _timer(new QTimer(this)), + _enabled(false), _active(false) +{ + QHash roles = roleNames(); + roles[Qt::DisplayRole] = QByteArray("title"); + roles[Qt::StatusTipRole] = QByteArray("subtitle"); + roles[ObjectRole] = QByteArray("object"); + setRoleNames(roles); + + _timer->setSingleShot(true); + _timer->setInterval(3000); + + connect(_scanner, SIGNAL(WatchFound(QVariantMap)), SLOT(handleWatchFound(QVariantMap))); + connect(_scanner, SIGNAL(Started()), SLOT(handleStarted())); + connect(_scanner, SIGNAL(Finished()), SLOT(handleFinished())); + connect(_timer, SIGNAL(timeout()), SLOT(handleTimeout())); +} + +ScanWatchesModel::~ScanWatchesModel() +{ +} + +bool ScanWatchesModel::enabled() const +{ + return _enabled; +} + +void ScanWatchesModel::setEnabled(bool enabled) +{ + _timer->stop(); + + _enabled = enabled; + + if (_enabled && !_active) { + _scanner->Start(); + } +} + +bool ScanWatchesModel::active() const +{ + return _active; +} + +int ScanWatchesModel::rowCount(const QModelIndex &parent) const +{ + return _list.count(); +} + +QVariant ScanWatchesModel::data(const QModelIndex &index, int role) const +{ + qDebug() << "Asked for data" << index.row() << index.column() << role; + const QVariantMap &info = _list.at(index.row()); + switch (role) { + case Qt::DisplayRole: + return info["name"]; + case Qt::StatusTipRole: + return info["address"]; + case ObjectRole: + return QVariant::fromValue(info); + } + return QVariant(); +} + +void ScanWatchesModel::handleWatchFound(const QVariantMap &info) +{ + qDebug() << "Watch found" << info << endl; + if (!_list.contains(info)) { + int count = _list.count(); + beginInsertRows(QModelIndex(), count, count); + _list.append(info); + endInsertRows(); + } +} + +void ScanWatchesModel::handleStarted() +{ + _active = true; + emit activeChanged(); +} + +void ScanWatchesModel::handleFinished() +{ + qDebug() << "Scan finished"; + _active = false; + if (_enabled) { + _timer->start(); + } + emit activeChanged(); +} + +void ScanWatchesModel::handleTimeout() +{ + qDebug() << "Restarting scan"; + _scanner->Start(); +} diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro index e02afcc..53abe73 100644 --- a/sowatchui/sowatchui.pro +++ b/sowatchui/sowatchui.pro @@ -21,14 +21,17 @@ CONFIG += qdeclarative-boostable LIBS += -L$$OUT_PWD/../libsowatch/ -lsowatch INCLUDEPATH += $$PWD/../libsowatch DEPENDPATH += $$PWD/../libsowatch +!isEmpty(MEEGO_VERSION_MAJOR)|maemo5 { + QMAKE_RPATHDIR += /opt/sowatch/lib +} # Source files SOURCES += main.cpp \ - watchesmodel.cc \ - scanwatchesmodel.cc scannerproxy.cc + watchesmodel.cpp daemonproxy.cpp \ + scanwatchesmodel.cpp scannerproxy.cpp HEADERS += \ - watchesmodel.h \ + watchesmodel.h daemonproxy.h \ scanwatchesmodel.h scannerproxy.h OTHER_FILES += qml/main.qml \ diff --git a/sowatchui/watchesmodel.cc b/sowatchui/watchesmodel.cc deleted file mode 100644 index d12e7db..0000000 --- a/sowatchui/watchesmodel.cc +++ /dev/null @@ -1,136 +0,0 @@ -#include - -#include "watchesmodel.h" - -using namespace sowatch; - -WatchesModel::WatchesModel(QObject *parent) : - QAbstractListModel(parent), - _config(new GConfKey("/apps/sowatch", this)), - _active_watches(_config->getSubkey("active-watches", this)) -{ - QHash roles = roleNames(); - roles[Qt::DisplayRole] = QByteArray("title"); - roles[Qt::StatusTipRole] = QByteArray("subtitle"); - roles[ObjectRole] = QByteArray("object"); - setRoleNames(roles); - - connect(_config, SIGNAL(changed()), - this, SLOT(handleConfigChanged())); - connect(_config, SIGNAL(subkeyChanged(QString)), - this, SLOT(handleSubkeyChanged(QString))); - qDebug() << "connects"; - - reload(); -} - -WatchesModel::~WatchesModel() -{ -} - -int WatchesModel::rowCount(const QModelIndex &parent) const -{ - return _list.count(); -} - -QVariant WatchesModel::data(const QModelIndex &index, int role) const -{ - qDebug() << "Asked for data" << index.row() << index.column() << role; - ConfigKey *config = _list[index.row()]; - switch (role) { - case Qt::DisplayRole: - return config->value("name"); - case Qt::StatusTipRole: - return QVariant(tr("Configured")); - } - return QVariant(); -} - -bool WatchesModel::removeRows(int row, int count, const QModelIndex &parent) -{ - -} - -void WatchesModel::addFoundWatch(const QVariantMap &info) -{ - QStringList existing = _config->dirs(); - QString base = "watch%1"; - QString name = base.arg(""); - int num = 1; - - while (existing.contains(name)) { - num++; - name = base.arg(num); - } - - ConfigKey* newkey = _config->getSubkey(name); - foreach (const QString& key, info.keys()) { - newkey->set(key, info[key]); - } - - // Now add to active watches - QStringList active = _active_watches->value().toStringList(); - active << name; - _active_watches->set(active); -} - -void WatchesModel::reload() -{ - QStringList dirs = _config->dirs(); - - beginResetModel(); - foreach (ConfigKey* conf, _list) { - conf->deleteLater(); - } - _list.clear(); - foreach (const QString& s, dirs) { - _list.append(_config->getSubkey(s, this)); - } - endResetModel(); - - qDebug() << "Found" << _list.count() << "configured watches"; -} - -void WatchesModel::handleConfigChanged() -{ - qDebug() << "Key changed"; -} - -void WatchesModel::handleSubkeyChanged(const QString &subkey) -{ - QRegExp nameexp("^([^/]+)/name"); - if (nameexp.exactMatch(subkey)) { - qDebug() << "Name key changed:" << subkey; - QString id = nameexp.cap(1); - int i = findRowByWatchId(id); - if (i != -1) { - if (_config->value(subkey).isNull()) { - beginRemoveRows(QModelIndex(), i, i); - _list[i]->deleteLater(); - _list.removeAt(i); - qDebug() << "Removing" << i; - endRemoveRows(); - } else { - emit dataChanged(createIndex(i, 0), createIndex(i, 0)); - qDebug() << "Changing" << i; - } - } else { - i = _list.size(); - qDebug() << "Inserting" << i; - beginInsertRows(QModelIndex(), i, i); - _list.append(_config->getSubkey(id, this)); - endInsertRows(); - } - } -} - -int WatchesModel::findRowByWatchId(const QString &id) -{ - QString pattern(_config->key() + "/" + id); - for (int i = 0; i < _list.size(); i++) { - if (_list[i]->key().endsWith("/" + id)) { - return i; - } - } - return -1; -} diff --git a/sowatchui/watchesmodel.cpp b/sowatchui/watchesmodel.cpp new file mode 100644 index 0000000..4531544 --- /dev/null +++ b/sowatchui/watchesmodel.cpp @@ -0,0 +1,166 @@ +#include + +#include "watchesmodel.h" + +using namespace sowatch; + +WatchesModel::WatchesModel(QObject *parent) : + QAbstractListModel(parent), + _config(new GConfKey("/apps/sowatch", this)), + _active_watches(_config->getSubkey("active-watches", this)), + _daemon(new DaemonProxy("com.javispedro.sowatchd", "/com/javispedro/sowatch/daemon", QDBusConnection::sessionBus())) +{ + QHash roles = roleNames(); + roles[Qt::DisplayRole] = QByteArray("title"); + roles[Qt::StatusTipRole] = QByteArray("subtitle"); + roles[ObjectRole] = QByteArray("object"); + setRoleNames(roles); + + connect(_config, SIGNAL(changed()), + this, SLOT(handleConfigChanged())); + connect(_config, SIGNAL(subkeyChanged(QString)), + this, SLOT(handleSubkeyChanged(QString))); + connect(_daemon, SIGNAL(WatchStatusChanged(QString,QString)), + this, SLOT(handleWatchStatusChanged(QString,QString))); + + reload(); +} + +WatchesModel::~WatchesModel() +{ +} + +int WatchesModel::rowCount(const QModelIndex &parent) const +{ + return _list.count(); +} + +QVariant WatchesModel::data(const QModelIndex &index, int role) const +{ + qDebug() << "Asked for data" << index.row() << index.column() << role; + ConfigKey *config = _list[index.row()]; + QString key = config->key(); + QString id = key.mid(key.lastIndexOf('/') + 1); + switch (role) { + case Qt::DisplayRole: + return config->value("name"); + case Qt::StatusTipRole: + if (isWatchIdActive(id)) { + QString status = _status[id]; + if (status == "connected") { + return QVariant(tr("Connected")); + } else if (status == "enabled") { + return QVariant(tr("Searching...")); + } else { + return QVariant(tr("Enabled")); + } + } else { + return QVariant(tr("Disabled")); + } + } + return QVariant(); +} + +bool WatchesModel::removeRows(int row, int count, const QModelIndex &parent) +{ + return false; // TODO +} + +void WatchesModel::addFoundWatch(const QVariantMap &info) +{ + QStringList existing = _config->dirs(); + QString base = "watch%1"; + QString name = base.arg(""); + int num = 1; + + while (existing.contains(name)) { + num++; + name = base.arg(num); + } + + ConfigKey* newkey = _config->getSubkey(name); + foreach (const QString& key, info.keys()) { + newkey->set(key, info[key]); + } + + // Now add to active watches + QStringList active = _active_watches->value().toStringList(); + active << name; + _active_watches->set(active); +} + +void WatchesModel::reload() +{ + QStringList dirs = _config->dirs(); + + beginResetModel(); + foreach (ConfigKey* conf, _list) { + conf->deleteLater(); + } + _list.clear(); + foreach (const QString& s, dirs) { + _list.append(_config->getSubkey(s, this)); + } + endResetModel(); + + qDebug() << "Found" << _list.count() << "configured watches"; +} + +void WatchesModel::handleConfigChanged() +{ + qDebug() << "Key changed"; +} + +void WatchesModel::handleSubkeyChanged(const QString &subkey) +{ + QRegExp nameexp("^([^/]+)/name"); + if (nameexp.exactMatch(subkey)) { + qDebug() << "Name key changed:" << subkey; + QString id = nameexp.cap(1); + int i = findRowByWatchId(id); + if (i != -1) { + if (_config->value(subkey).isNull()) { + beginRemoveRows(QModelIndex(), i, i); + _list[i]->deleteLater(); + _list.removeAt(i); + qDebug() << "Removing" << i; + endRemoveRows(); + } else { + emit dataChanged(createIndex(i, 0), createIndex(i, 0)); + qDebug() << "Changing" << i; + } + } else { + i = _list.size(); + qDebug() << "Inserting" << i; + beginInsertRows(QModelIndex(), i, i); + _list.append(_config->getSubkey(id, this)); + endInsertRows(); + } + } +} + +void WatchesModel::handleWatchStatusChanged(const QString &watch, const QString &status) +{ + _status[watch] = status; + int i = findRowByWatchId(watch); + if (i != -1) { + emit dataChanged(createIndex(i, 0), createIndex(i, 0)); + } +} + +int WatchesModel::findRowByWatchId(const QString &id) +{ + QString pattern(_config->key() + "/" + id); + for (int i = 0; i < _list.size(); i++) { + if (_list[i]->key().endsWith("/" + id)) { + return i; + } + } + return -1; +} + +bool WatchesModel::isWatchIdActive(const QString &id) const +{ + QStringList active = _active_watches->value().toStringList(); + return active.contains(id); +} diff --git a/sowatchui/watchesmodel.h b/sowatchui/watchesmodel.h index cc6c9af..4962725 100644 --- a/sowatchui/watchesmodel.h +++ b/sowatchui/watchesmodel.h @@ -5,6 +5,8 @@ #include +#include "daemonproxy.h" + class WatchesModel : public QAbstractListModel { Q_OBJECT @@ -27,14 +29,18 @@ private slots: void reload(); void handleConfigChanged(); void handleSubkeyChanged(const QString& subkey); + void handleWatchStatusChanged(const QString& watch, const QString& status); private: int findRowByWatchId(const QString& id); + bool isWatchIdActive(const QString& id) const; private: sowatch::ConfigKey *_config; sowatch::ConfigKey *_active_watches; + DaemonProxy *_daemon; QList _list; + QMap _status; }; #endif // WATCHESMODEL_H -- cgit v1.2.3