summaryrefslogtreecommitdiff
path: root/sowatchd/daemon.cpp
blob: 0cc6c05a02196d822ef51d32815730feb011ec18 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <QtCore/QDebug>
#include <QtCore/QPluginLoader>
#include <QtCore/QSettings>
#include <QtCore/QDir>
#include <sowatch.h>
#include "daemon.h"

using namespace sowatch;

Daemon::Daemon(QObject *parent) :
    QObject(parent)
{
	loadDrivers();
	loadProviders();
	loadWatchlets();
	initWatches();
}

void Daemon::loadDrivers()
{
	QDir dir(SOWATCH_DRIVERS_DIR);
	foreach (QString file, dir.entryList(QDir::Files)) {
#if defined(Q_OS_UNIX)
		// Temporary workaround for QtC deploy plugin issues
		if (!file.endsWith(".so")) continue;
#endif
		QPluginLoader loader(dir.absoluteFilePath(file));
		QObject *pluginObj = loader.instance();
		if (pluginObj) {
			WatchPluginInterface *plugin = qobject_cast<WatchPluginInterface*>(pluginObj);
			if (plugin) {
				QStringList drivers = plugin->drivers();
				foreach (const QString& driver, drivers) {
					_drivers[driver] = plugin;
				}
			} else {
				qWarning() << "Invalid plugin" << file;
				loader.unload();
			}
		} else {
			qWarning() << "Invalid plugin" << file << loader.errorString();
			loader.unload();
		}
	}

	qDebug() << "loaded drivers" << _drivers.keys();
}

void Daemon::loadProviders()
{
	QDir dir(SOWATCH_NOTIFICATIONS_DIR);
	foreach (QString file, dir.entryList(QDir::Files)) {
#if defined(Q_OS_UNIX)
		// Temporary workaround for QtC deploy plugin issues
		if (!file.endsWith(".so")) continue;
#endif
		QPluginLoader loader(dir.absoluteFilePath(file));
		QObject *pluginObj = loader.instance();
		if (pluginObj) {
			NotificationPluginInterface *plugin = qobject_cast<NotificationPluginInterface*>(pluginObj);
			if (plugin) {
				QStringList providers = plugin->providers();
				foreach (const QString& provider, providers) {
					_providers[provider] = plugin;
				}
			} else {
				qWarning() << "Invalid plugin" << file;
				loader.unload();
			}
		} else {
			qWarning() << "Invalid plugin" << file << loader.errorString();
			loader.unload();
		}
	}

	qDebug() << "loaded providers" << _providers.keys();
}

void Daemon::initWatches()
{
	QSettings settings;
	int size = settings.beginReadArray("watches");

	for (int i = 0; i < size; i++) {
		settings.setArrayIndex(i);
		QString driver = settings.value("driver").toString().toLower();
		WatchPluginInterface *plugin = _drivers[driver];
		if (plugin) {
			Watch *watch = plugin->getWatch(driver, settings, this);
			if (watch) {
				initWatch(watch, settings);
			} else {
				qWarning() << "Driver" << driver << "refused to getWatch";
			}
		} else {
			qWarning() << "Invalid driver" << driver;
		}
	}

	settings.endArray();
	qDebug() << "handling" << _servers.size() << "watches";
}

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

	// Create the server
	WatchServer* server = new WatchServer(watch, this);
	_servers.append(server);

	// Configure the server
	server->setNextWatchletButton(settings.value("nextWatchletButton").toString());

	// Initialize providers
	size = settings.beginReadArray("notifications");
	for (int i = 0; i < size; i++) {
		settings.setArrayIndex(i);
		QString id = settings.value("provider").toString().toLower();
		NotificationPluginInterface *plugin = _providers[id];
		if (plugin) {
			NotificationProvider *provider = plugin->getProvider(id, settings, server);
			server->addProvider(provider);
		} else {
			qWarning() << "Unknown notification provider" << id;
		}
	}
	settings.endArray();

	// Initialize watchlets
	size = settings.beginReadArray("watchlets");
	for (int i = 0; i < size; i++) {
		settings.setArrayIndex(i);
		QString id = settings.value("id").toString().toLower();
		WatchletPluginInterface *plugin = _watchlets[id];
		if (plugin) {
			plugin->getWatchlet(id, settings, server);
			// Watchlets are associated to server via parent-child relationship.
		} else {
			qWarning() << "Unknown watchlet" << id;
		}
	}
	settings.endArray();
}

void Daemon::loadWatchlets()
{
	QDir dir(SOWATCH_WATCHLETS_DIR);
	foreach (QString file, dir.entryList(QDir::Files)) {
#if defined(Q_OS_UNIX)
		// Temporary workaround for QtC deploy plugin issues
		if (!file.endsWith(".so")) continue;
#endif
		QPluginLoader loader(dir.absoluteFilePath(file));
		QObject *pluginObj = loader.instance();
		if (pluginObj) {
			WatchletPluginInterface *plugin = qobject_cast<WatchletPluginInterface*>(pluginObj);
			if (plugin) {
				QStringList watchlets = plugin->watchlets();
				foreach (const QString& watchlet, watchlets) {
					_watchlets[watchlet] = plugin;
				}
			} else {
				qWarning() << "Invalid plugin" << file;
				loader.unload();
			}
		} else {
			qWarning() << "Invalid plugin" << file << loader.errorString();
			loader.unload();
		}
	}

	qDebug() << "loaded watchlets" << _watchlets.keys();
}