diff options
Diffstat (limited to 'sowatchd')
-rw-r--r-- | sowatchd/daemon.cpp | 84 | ||||
-rw-r--r-- | sowatchd/daemon.h | 4 |
2 files changed, 64 insertions, 24 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<ConfigKey> watchSettings(_settings->getSubkey(name)); + QScopedPointer<ConfigKey> 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<QString> confActiveWatches = _settings->value("active-watches").toStringList().toSet(); - QSet<QString> curActiveWatches = _servers.keys().toSet(); - QSet<QString> removed = curActiveWatches - confActiveWatches; - QSet<QString> added = confActiveWatches - curActiveWatches; - foreach (const QString& s, removed) { + QStringList watches = _watches_list->value().toStringList(); + QSet<QString> startedWatches = _servers.keys().toSet(); + + QSet<QString> 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<QString, WatchServer*> _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); }; |