summaryrefslogtreecommitdiff
path: root/sowatchd/daemon.cpp
blob: 61144de0a7aaff61214bd3393df750d42dea6893 (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
#include <QtCore/QDebug>
#include <sowatch.h>
#include "daemon.h"

using namespace sowatch;

Daemon::Daemon(QObject *parent) :
	QObject(parent),
	_registry(Registry::registry()),
	_settings(new GConfKey("/apps/sowatch", this))
{
	connect(_settings, SIGNAL(subkeyChanged(QString)), SLOT(settingsChanged(QString)));
	QStringList activeWatches = _settings->value("active-watches").toStringList();
	foreach (const QString& s, activeWatches) {
		startWatch(s);
	}
}

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");
		}
	} else {
		return QLatin1String("disabled");
	}
}

void Daemon::terminate()
{
	QApplication::quit();
}

void Daemon::startWatch(const QString &name)
{
	qDebug() << "Starting watch" << name;
	QScopedPointer<ConfigKey> watchSettings(_settings->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;

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

#if TODO
void Daemon::initWatch(Watch* watch, QSettings& settings)
{
	int size;


}
#endif

void Daemon::settingsChanged(const QString &subkey)
{
	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) {
			stopWatch(s);
		}
		foreach (const QString& s, added) {
			startWatch(s);
		}
	}
}