summaryrefslogtreecommitdiff
path: root/sowatchd/watchhandler.cpp
blob: 314156a06f61ddf7436c28ee3f0507e2a80b0e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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(disconnected()),
	        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;

	if (newWatchlets == curWatchlets) return; // Nothing to do

	// 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());
	}
}