diff options
Diffstat (limited to 'sowatchd/daemon.cpp')
-rw-r--r-- | sowatchd/daemon.cpp | 84 |
1 files changed, 61 insertions, 23 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")); } } |