diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2012-08-10 00:45:24 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2012-08-10 00:45:24 +0200 |
commit | d194a6112299cfe045c34e5cdb6adbbb81418d09 (patch) | |
tree | 4973f946b6701c8aea8bed73e5c4c6c978ce8e66 /sowatchd | |
parent | b1ccb04d7dd2fb20def829be084474ed329d4988 (diff) | |
download | sowatch-d194a6112299cfe045c34e5cdb6adbbb81418d09.tar.gz sowatch-d194a6112299cfe045c34e5cdb6adbbb81418d09.zip |
new watchhandler for watchlet/prov lifecycle mgmt
Diffstat (limited to 'sowatchd')
-rw-r--r-- | sowatchd/daemon.cpp | 105 | ||||
-rw-r--r-- | sowatchd/daemon.h | 4 | ||||
-rw-r--r-- | sowatchd/sowatchd.pro | 6 | ||||
-rw-r--r-- | sowatchd/watchhandler.cpp | 132 | ||||
-rw-r--r-- | sowatchd/watchhandler.h | 41 |
5 files changed, 197 insertions, 91 deletions
diff --git a/sowatchd/daemon.cpp b/sowatchd/daemon.cpp index c668f59..bcf3686 100644 --- a/sowatchd/daemon.cpp +++ b/sowatchd/daemon.cpp @@ -21,14 +21,8 @@ Daemon::Daemon(QObject *parent) : QString Daemon::getWatchStatus(const QString &name) { - if (_servers.contains(name)) { - WatchServer* server = _servers[name]; - Watch* watch = server->watch(); - if (watch->isConnected()) { - return QLatin1String("connected"); - } else { - return QLatin1String("enabled"); - } + if (_watches.contains(name)) { + return _watches[name]->status(); } else { return QLatin1String("disabled"); } @@ -42,87 +36,29 @@ void Daemon::terminate() void Daemon::startWatch(const QString &name) { qDebug() << "Starting watch" << name; - QScopedPointer<ConfigKey> watchSettings(_config->getSubkey(name)); - - const QString driver = watchSettings->value("driver").toString().toLower(); - if (driver.isEmpty()) { - qWarning() << "Watch" << name << "has no driver setting"; - return; - } - - WatchPluginInterface *watchPlugin = _registry->getWatchPlugin(driver); - if (!watchPlugin) { - qWarning() << "Invalid driver" << driver; - return; - } - - // Create the watch object from the plugin - Watch *watch = watchPlugin->getWatch(driver, watchSettings.data(), this); - if (!watch) { - qWarning() << "Driver" << driver << "failed to initiate watch"; - } - - // Create the server - WatchServer* server = new WatchServer(watch, this); - _servers[name] = server; - + QScopedPointer<ConfigKey> watchConfig(_config->getSubkey(name)); + WatchHandler *handler = new WatchHandler(watchConfig.data(), this); + _status_mapper->setMapping(handler, name); + _watches[name] = handler; handleWatchStatusChange(name); - - // Connect watch status signals - _status_mapper->setMapping(watch, name); - connect(watch, SIGNAL(connected()), + connect(handler, SIGNAL(statusChanged()), _status_mapper, SLOT(map())); - connect(watch, SIGNAL(disconnected()), - _status_mapper, SLOT(map())); - - // Configure the server - server->setNextWatchletButton(watchSettings->value("next-watchlet-button").toString()); - - // Initialize providers - QStringList list; - list = watchSettings->value("active-notifications").toStringList(); - foreach (const QString& s, list) { - QScopedPointer<ConfigKey> settings(watchSettings->getSubkey(s)); - QString id = settings->value("id").toString().toLower(); - NotificationPluginInterface *plugin = _registry->getNotificationPlugin(id); - if (plugin) { - NotificationProvider *provider = plugin->getProvider(id, settings.data(), server); - server->addProvider(provider); - } else { - qWarning() << "Unknown notification provider" << id; - } - } - - // Initialize watchlets - list = watchSettings->value("active-watchlets").toStringList(); - foreach (const QString& s, list) { - QScopedPointer<ConfigKey> settings(watchSettings->getSubkey(s)); - QString id = settings->value("id").toString().toLower(); - WatchletPluginInterface *plugin = _registry->getWatchletPlugin(id); - if (plugin) { - Watchlet *watchlet = plugin->getWatchlet(id, settings.data(), server); - server->addWatchlet(watchlet); - } else { - qWarning() << "Unknown watchlet" << id; - } - } } 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); + WatchHandler *handler = _watches[name]; + _watches.remove(name); + _status_mapper->removeMappings(handler); + delete handler; handleWatchStatusChange(name); } void Daemon::startEnabledWatches() { QStringList watches = _watches_list->value().toStringList(); - QSet<QString> startedWatches = _servers.keys().toSet(); + QSet<QString> startedWatches = _watches.keys().toSet(); QSet<QString> removed = startedWatches - watches.toSet(); // Those watches have been entirely removed from the list, not disabled first @@ -132,7 +68,7 @@ void Daemon::startEnabledWatches() foreach (const QString& s, watches) { bool enabled_in_config = _config->value(s + "/enable").toBool(); - bool currently_started = _servers.contains(s); + bool currently_started = _watches.contains(s); if (enabled_in_config && !currently_started) { startWatch(s); @@ -145,12 +81,11 @@ void Daemon::startEnabledWatches() void Daemon::handleSettingsChanged(const QString &subkey) { qDebug() << "Daemon settings changed" << subkey; - static QRegExp enabled_key_pattern("^([^/])/enable$"); + 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); + bool currently_started = _watches.contains(watchName); if (enabled_in_config && !currently_started) { startWatch(watchName); @@ -164,14 +99,8 @@ void Daemon::handleSettingsChanged(const QString &subkey) void Daemon::handleWatchStatusChange(const QString &name) { - 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")); - } + if (_watches.contains(name)) { + emit WatchStatusChanged(name, _watches[name]->status()); } else if (_watches_list->value().toStringList().contains(name)) { emit WatchStatusChanged(name, QLatin1String("disabled")); } else { diff --git a/sowatchd/daemon.h b/sowatchd/daemon.h index d35b56b..e3f748b 100644 --- a/sowatchd/daemon.h +++ b/sowatchd/daemon.h @@ -7,6 +7,8 @@ #include <sowatch.h> +#include "watchhandler.h" + namespace sowatch { @@ -28,7 +30,7 @@ private: Registry* _registry; ConfigKey* _config; ConfigKey* _watches_list; - QMap<QString, WatchServer*> _servers; + QMap<QString, WatchHandler*> _watches; QSignalMapper *_status_mapper; void startWatch(const QString& name); diff --git a/sowatchd/sowatchd.pro b/sowatchd/sowatchd.pro index 4b1b668..9299f05 100644 --- a/sowatchd/sowatchd.pro +++ b/sowatchd/sowatchd.pro @@ -5,8 +5,10 @@ TEMPLATE = app QT += core gui dbus CONFIG -= app_bundle -SOURCES += main.cpp daemon.cpp daemonadaptor.cpp -HEADERS += global.h daemon.h daemonadaptor.h +SOURCES += main.cpp daemon.cpp daemonadaptor.cpp \ + watchhandler.cpp +HEADERS += global.h daemon.h daemonadaptor.h \ + watchhandler.h LIBS += -L$$OUT_PWD/../libsowatch/ -lsowatch INCLUDEPATH += $$PWD/../libsowatch diff --git a/sowatchd/watchhandler.cpp b/sowatchd/watchhandler.cpp new file mode 100644 index 0000000..e396c17 --- /dev/null +++ b/sowatchd/watchhandler.cpp @@ -0,0 +1,132 @@ +#include "watchhandler.h" + +using namespace sowatch; + +WatchHandler::WatchHandler(ConfigKey *config, QObject *parent) + : QObject(parent), + _config(config->getSubkey("", this)) +{ + Registry *registry = Registry::registry(); + + qDebug() << "Starting watch handler on" << _config->key(); + + connect(_config, SIGNAL(subkeyChanged(QString)), + SLOT(handleConfigSubkeyChanged(QString))); + + const QString driver = _config->value("driver").toString(); + if (driver.isEmpty()) { + qWarning() << "Watch" << _config->value("name") << "has no driver setting"; + return; + } + + WatchPluginInterface *watchPlugin = registry->getWatchPlugin(driver); + if (!watchPlugin) { + qWarning() << "Invalid driver" << driver; + return; + } + + // Create the watch object from the plugin + _watch = watchPlugin->getWatch(driver, _config, this); + if (!_watch) { + qWarning() << "Driver" << driver << "failed to initiate watch"; + return; + } + + // Setup watch status connections + connect(_watch, SIGNAL(connected()), + SIGNAL(statusChanged())); + connect(_watch, SIGNAL(connected()), + SIGNAL(statusChanged())); + + // Now create the UI server + _server = new WatchServer(_watch, this); + + // Configure the server + _server->setNextWatchletButton(_config->value("next-watchlet-button").toString()); + + updateProviders(); + updateWatchlets(); +} + +QString WatchHandler::status() const +{ + if (_watch->isConnected()) { + return "connected"; + } else if (_config->value("enable").toBool()) { + return "enabled"; + } else { + return "disabled"; + } +} + +void WatchHandler::updateWatchlets() +{ + Registry *registry = Registry::registry(); + QStringList newWatchlets = _config->value("watchlets").toStringList(); + QStringList curWatchlets = _watchlet_order; + + // TODO: Something better than removing all and readding + foreach (const QString& s, curWatchlets) { + Watchlet* watchlet = _watchlets[s]; + _server->removeWatchlet(watchlet); + delete watchlet; + } + + _watchlet_order.clear(); + _watchlets.clear(); + + foreach (const QString& s, newWatchlets) { + WatchletPluginInterface *plugin = registry->getWatchletPlugin(s); + if (!plugin) { + qWarning() << "Unknown watchlet" << s; + continue; + } + ConfigKey *subconfig = _config->getSubkey(s); + Watchlet* watchlet = plugin->getWatchlet(s, subconfig, _server); + _watchlet_order << s; + _watchlets[s] = watchlet; + _server->addWatchlet(watchlet); + delete subconfig; + } + + qDebug() << "Watchlets reloaded"; +} + +void WatchHandler::updateProviders() +{ + Registry *registry = Registry::registry(); + QSet<QString> curProviders = _providers.keys().toSet(); + QSet<QString> newProviders = _config->value("providers").toStringList().toSet(); + QSet<QString> removed = curProviders - newProviders; + QSet<QString> added = newProviders - curProviders; + + foreach (const QString& s, removed) { + NotificationProvider *provider = _providers[s]; + _server->removeProvider(provider); + delete provider; + } + + foreach (const QString& s, added) { + NotificationPluginInterface *plugin = registry->getNotificationPlugin(s); + if (!plugin) { + qWarning() << "Unknown notification provider" << s; + continue; + } + ConfigKey *subconfig = _config->getSubkey(s); + NotificationProvider *provider = plugin->getProvider(s, subconfig, _server); + _server->addProvider(provider); + _providers[s] = provider; + delete subconfig; + } +} + +void WatchHandler::handleConfigSubkeyChanged(const QString &subkey) +{ + if (subkey == "watchlets") { + updateWatchlets(); + } else if (subkey == "providers") { + updateProviders(); + } else if (subkey == "next-watchlet-button") { + _server->setNextWatchletButton(_config->value("next-watchlet-button").toString()); + } +} diff --git a/sowatchd/watchhandler.h b/sowatchd/watchhandler.h new file mode 100644 index 0000000..3fa7c6b --- /dev/null +++ b/sowatchd/watchhandler.h @@ -0,0 +1,41 @@ +#ifndef WATCHHANDLER_H +#define WATCHHANDLER_H + +#include <QtCore/QObject> +#include <QtCore/QList> + +#include <sowatch.h> + +namespace sowatch +{ + +class WatchHandler : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString status READ status NOTIFY statusChanged) + +public: + explicit WatchHandler(ConfigKey *config, QObject *parent = 0); + + QString status() const; + +signals: + void statusChanged(); + +private slots: + void updateWatchlets(); + void updateProviders(); + void handleConfigSubkeyChanged(const QString& key); + +private: + ConfigKey *_config; + Watch *_watch; + WatchServer *_server; + QList<QString> _watchlet_order; + QMap<QString, Watchlet*> _watchlets; + QMap<QString, NotificationProvider*> _providers; +}; + +} + +#endif // WATCHHANDLER_H |