From b1ccb04d7dd2fb20def829be084474ed329d4988 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Thu, 9 Aug 2012 21:02:14 +0200 Subject: change config of enabled/disabled --- sowatchd/daemon.cpp | 84 ++++++++++++++++++++++++++++++----------- sowatchd/daemon.h | 4 +- sowatchui/qml/NewWatchSheet.qml | 13 +++++++ sowatchui/sowatchui.pro | 9 +++++ sowatchui/watchesmodel.cpp | 71 ++++++++++------------------------ sowatchui/watchesmodel.h | 8 ++-- sowatchui/watchscannermodel.cpp | 3 ++ 7 files changed, 112 insertions(+), 80 deletions(-) diff --git a/sowatchd/daemon.cpp b/sowatchd/daemon.cpp index 2a6c84f..c668f59 100644 --- a/sowatchd/daemon.cpp +++ b/sowatchd/daemon.cpp @@ -7,18 +7,16 @@ using namespace sowatch; Daemon::Daemon(QObject *parent) : QObject(parent), _registry(Registry::registry()), - _settings(new GConfKey("/apps/sowatch", this)), - _status_mapper(new QSignalMapper(this)) + _config(new GConfKey("/apps/sowatch", this)), + _watches_list(_config->getSubkey("watches", this)), + _status_mapper(new QSignalMapper(this)) { - connect(_settings, SIGNAL(subkeyChanged(QString)), + connect(_config, SIGNAL(subkeyChanged(QString)), SLOT(handleSettingsChanged(QString))); connect(_status_mapper, SIGNAL(mapped(QString)), SLOT(handleWatchStatusChange(QString))); - QStringList activeWatches = _settings->value("active-watches").toStringList(); - foreach (const QString& s, activeWatches) { - startWatch(s); - } + startEnabledWatches(); } QString Daemon::getWatchStatus(const QString &name) @@ -44,7 +42,7 @@ void Daemon::terminate() void Daemon::startWatch(const QString &name) { qDebug() << "Starting watch" << name; - QScopedPointer watchSettings(_settings->getSubkey(name)); + QScopedPointer watchSettings(_config->getSubkey(name)); const QString driver = watchSettings->value("driver").toString().toLower(); if (driver.isEmpty()) { @@ -68,6 +66,8 @@ void Daemon::startWatch(const QString &name) WatchServer* server = new WatchServer(watch, this); _servers[name] = server; + handleWatchStatusChange(name); + // Connect watch status signals _status_mapper->setMapping(watch, name); connect(watch, SIGNAL(connected()), @@ -111,32 +111,70 @@ void Daemon::startWatch(const QString &name) void Daemon::stopWatch(const QString &name) { qDebug() << "Stopping watch" << name; + WatchServer* server = _servers[name]; + Watch* watch = server->watch(); + server->deleteLater(); + watch->deleteLater(); + _servers.remove(name); + handleWatchStatusChange(name); } -void Daemon::handleSettingsChanged(const QString &subkey) +void Daemon::startEnabledWatches() { - qDebug() << "Daemon settings changed" << subkey; - if (subkey == "active-watches") { - QSet confActiveWatches = _settings->value("active-watches").toStringList().toSet(); - QSet curActiveWatches = _servers.keys().toSet(); - QSet removed = curActiveWatches - confActiveWatches; - QSet added = confActiveWatches - curActiveWatches; - foreach (const QString& s, removed) { + QStringList watches = _watches_list->value().toStringList(); + QSet startedWatches = _servers.keys().toSet(); + + QSet removed = startedWatches - watches.toSet(); + // Those watches have been entirely removed from the list, not disabled first + foreach (const QString& s, removed) { + stopWatch(s); + } + + foreach (const QString& s, watches) { + bool enabled_in_config = _config->value(s + "/enable").toBool(); + bool currently_started = _servers.contains(s); + + if (enabled_in_config && !currently_started) { + startWatch(s); + } else if (currently_started && !enabled_in_config) { stopWatch(s); } - foreach (const QString& s, added) { - startWatch(s); + } +} + +void Daemon::handleSettingsChanged(const QString &subkey) +{ + qDebug() << "Daemon settings changed" << subkey; + static QRegExp enabled_key_pattern("^([^/])/enable$"); + if (enabled_key_pattern.exactMatch(subkey)) { + QStringList watches = _watches_list->value().toStringList(); + QString watchName = enabled_key_pattern.cap(1); + bool enabled_in_config = _config->value(subkey).toBool(); + bool currently_started = _servers.contains(watchName); + + if (enabled_in_config && !currently_started) { + startWatch(watchName); + } else if (currently_started && !enabled_in_config) { + stopWatch(watchName); } + } else if (subkey == "watches") { + startEnabledWatches(); } } void Daemon::handleWatchStatusChange(const QString &name) { - WatchServer* server = _servers[name]; - Watch* watch = server->watch(); - if (watch->isConnected()) { - emit WatchStatusChanged(name, QLatin1String("connected")); + if (_servers.contains(name)) { + WatchServer* server = _servers[name]; + Watch* watch = server->watch(); + if (watch->isConnected()) { + emit WatchStatusChanged(name, QLatin1String("connected")); + } else { + emit WatchStatusChanged(name, QLatin1String("enabled")); + } + } else if (_watches_list->value().toStringList().contains(name)) { + emit WatchStatusChanged(name, QLatin1String("disabled")); } else { - emit WatchStatusChanged(name, QLatin1String("enabled")); + emit WatchStatusChanged(name, QLatin1String("unconfigured")); } } diff --git a/sowatchd/daemon.h b/sowatchd/daemon.h index 338eb2a..d35b56b 100644 --- a/sowatchd/daemon.h +++ b/sowatchd/daemon.h @@ -26,7 +26,8 @@ signals: private: Registry* _registry; - ConfigKey* _settings; + ConfigKey* _config; + ConfigKey* _watches_list; QMap _servers; QSignalMapper *_status_mapper; @@ -34,6 +35,7 @@ private: void stopWatch(const QString& name); private slots: + void startEnabledWatches(); void handleSettingsChanged(const QString& subkey); void handleWatchStatusChange(const QString& watch); }; diff --git a/sowatchui/qml/NewWatchSheet.qml b/sowatchui/qml/NewWatchSheet.qml index 1028bd5..12eecf2 100644 --- a/sowatchui/qml/NewWatchSheet.qml +++ b/sowatchui/qml/NewWatchSheet.qml @@ -28,6 +28,19 @@ Sheet { model: watchScanner + header: Column { + width: parent.width + + Label { + text: qsTr("Pick a watch") + font: UiConstants.HeaderFont + } + GroupHeader { + width: parent.width + text: qsTr("Nearby watches") + } + } + delegate: ListDelegate { onClicked: { watches.addFoundWatch(object); diff --git a/sowatchui/sowatchui.pro b/sowatchui/sowatchui.pro index 93a2438..9caa163 100644 --- a/sowatchui/sowatchui.pro +++ b/sowatchui/sowatchui.pro @@ -7,6 +7,15 @@ qml_folder.source = qml qml_folder.target = . DEPLOYMENTFOLDERS = qml_folder +# Install icon files also to resources directory +res_files.files += sowatch64.png sowatch80.png +!isEmpty(MEEGO_VERSION_MAJOR)|maemo5 { + res_files.path = /opt/sowatch/share/metawatch +} else { + res_files.path = /usr/share/sowatch/metawatch +} +INSTALLS += res_files + # Additional import path used to resolve QML modules in Creator's code model QML_IMPORT_PATH = simulator { diff --git a/sowatchui/watchesmodel.cpp b/sowatchui/watchesmodel.cpp index c2bcd1a..0930715 100644 --- a/sowatchui/watchesmodel.cpp +++ b/sowatchui/watchesmodel.cpp @@ -7,21 +7,19 @@ using namespace sowatch; WatchesModel::WatchesModel(QObject *parent) : QAbstractListModel(parent), _config(new GConfKey("/apps/sowatch", this)), - _active_watches(_config->getSubkey("active-watches", this)), + _watches_list(_config->getSubkey("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"); + roles[EnabledRole] = QByteArray("enabled"); roles[ConfigKeyRole] = QByteArray("configKey"); roles[ConfigQmlUrlRole] = QByteArray("configQmlUrl"); setRoleNames(roles); - connect(_config, SIGNAL(changed()), - this, SLOT(handleConfigChanged())); - connect(_config, SIGNAL(subkeyChanged(QString)), - this, SLOT(handleSubkeyChanged(QString))); + connect(_watches_list, SIGNAL(changed()), + SLOT(handleWatchesListChanged())); connect(_daemon, SIGNAL(WatchStatusChanged(QString,QString)), this, SLOT(handleWatchStatusChanged(QString,QString))); @@ -47,18 +45,20 @@ QVariant WatchesModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: return config->value("name"); case Qt::StatusTipRole: - if (isWatchIdActive(id)) { + if (config->value("enable").toBool()) { QString status = _status[id]; if (status == "connected") { return QVariant(tr("Connected")); } else if (status == "enabled") { - return QVariant(tr("Searching...")); + return QVariant(tr("Disconnected, Searching...")); } else { - return QVariant(tr("Enabled")); + return QVariant(tr("Disconnected")); } } else { return QVariant(tr("Disabled")); } + case EnabledRole: + return config->value("enable"); case ConfigKeyRole: return QVariant::fromValue(key); case ConfigQmlUrlRole: @@ -106,23 +106,26 @@ void WatchesModel::addFoundWatch(const QVariantMap &info) qDebug() << "Would add" << providerId; } - // Now add to active watches - QStringList active = _active_watches->value().toStringList(); + // Now add to the watches list + QStringList active = _watches_list->value().toStringList(); active << name; - _active_watches->set(active); + _watches_list->set(active); + + // Now enable + newkey->set("enable", true); } void WatchesModel::reload() { - QStringList dirs = _config->dirs(); + QStringList names = _watches_list->value().toStringList(); beginResetModel(); foreach (ConfigKey* conf, _list) { - conf->deleteLater(); + delete conf; } _status.clear(); _list.clear(); - foreach (const QString& s, dirs) { + foreach (const QString& s, names) { _list.append(_config->getSubkey(s, this)); QDBusReply reply = _daemon->GetWatchStatus(s); if (reply.isValid()) { @@ -134,37 +137,9 @@ void WatchesModel::reload() qDebug() << "Found" << _list.count() << "configured watches"; } -void WatchesModel::handleConfigChanged() -{ - qDebug() << "Key changed"; -} - -void WatchesModel::handleSubkeyChanged(const QString &subkey) +void WatchesModel::handleWatchesListChanged() { - 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(); - } - } + reload(); } void WatchesModel::handleWatchStatusChanged(const QString &watch, const QString &status) @@ -186,9 +161,3 @@ int WatchesModel::findRowByWatchId(const QString &id) } 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 396fc8d..6e9b65e 100644 --- a/sowatchui/watchesmodel.h +++ b/sowatchui/watchesmodel.h @@ -15,7 +15,7 @@ public: ~WatchesModel(); enum DataRoles { - ObjectRole = Qt::UserRole, + EnabledRole = Qt::UserRole, ConfigKeyRole, ConfigQmlUrlRole }; @@ -29,17 +29,15 @@ public slots: private slots: void reload(); - void handleConfigChanged(); - void handleSubkeyChanged(const QString& subkey); + void handleWatchesListChanged(); 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; + sowatch::ConfigKey *_watches_list; DaemonProxy *_daemon; QList _list; QMap _status; diff --git a/sowatchui/watchscannermodel.cpp b/sowatchui/watchscannermodel.cpp index 8dd8d1e..f2c76f7 100644 --- a/sowatchui/watchscannermodel.cpp +++ b/sowatchui/watchscannermodel.cpp @@ -8,6 +8,7 @@ WatchScannerModel::WatchScannerModel(QObject *parent) : { QHash roles = roleNames(); roles[Qt::DisplayRole] = QByteArray("title"); + roles[Qt::DecorationRole] = QByteArray("iconSource"); roles[Qt::StatusTipRole] = QByteArray("subtitle"); roles[ObjectRole] = QByteArray("object"); setRoleNames(roles); @@ -58,6 +59,8 @@ QVariant WatchScannerModel::data(const QModelIndex &index, int role) const switch (role) { case Qt::DisplayRole: return info["name"]; + case Qt::DecorationRole: + return QVariant(SOWATCH_RESOURCES_DIR "/sowatch64.png"); case Qt::StatusTipRole: return info["address"]; case ObjectRole: -- cgit v1.2.3