summaryrefslogtreecommitdiff
path: root/libsowatch/registry.cpp
blob: 4836acafbd052670d15bb3ed0542adb469fc67a2 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <QtCore/QDebug>
#include <QtCore/QPluginLoader>
#include <QtCore/QSettings>
#include <QtCore/QDir>

#include "watchplugininterface.h"
#include "notificationplugininterface.h"
#include "watchletplugininterface.h"
#include "registry.h"

using namespace sowatch;

Registry* Registry::singleRegistry = 0;

Registry* Registry::registry()
{
	if (!singleRegistry) {
		singleRegistry = new Registry();
	}

	return singleRegistry;
}

Registry::~Registry()
{
}

Registry::Registry()
	: _watcher(new QFileSystemWatcher(this))
{
	_watcher->addPath(SOWATCH_DRIVERS_DIR);
	_watcher->addPath(SOWATCH_NOTIFICATIONS_DIR);
	_watcher->addPath(SOWATCH_WATCHLETS_DIR);

	loadDrivers();
	loadNotificationProviders();
	loadWatchlets();

	connect(_watcher, SIGNAL(directoryChanged(QString)),
			this, SLOT(handlePluginDirectoryChanged(QString)));
	connect(_watcher, SIGNAL(fileChanged(QString)),
			this, SLOT(handlePluginFileChanged(QString)));
}

void Registry::loadDrivers()
{
	QDir dir(SOWATCH_DRIVERS_DIR);
	foreach (QString entry, dir.entryList(QDir::Files)) {
		QString file = dir.absoluteFilePath(entry);
		loadDriver(file);
	}

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

void Registry::loadNotificationProviders()
{
	QDir dir(SOWATCH_NOTIFICATIONS_DIR);
	foreach (QString entry, dir.entryList(QDir::Files)) {
		QString file = dir.absoluteFilePath(entry);
		loadNotificationProvider(file);
	}

	qDebug() << "loaded notification providers" << _providerIds.keys();
}

void Registry::loadWatchlets()
{
	QDir dir(SOWATCH_WATCHLETS_DIR);
	foreach (QString entry, dir.entryList(QDir::Files)) {
		QString file = dir.absoluteFilePath(entry);
		loadWatchlet(file);
	}

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

void Registry::loadDriver(const QString &file)
{
	QPluginLoader* loader = new QPluginLoader(file, this);
	QObject *pluginObj = loader->instance();
	if (pluginObj) {
		WatchPluginInterface *plugin = qobject_cast<WatchPluginInterface*>(pluginObj);
		if (plugin) {
			_driverFiles[file] = loader;
			_drivers += plugin;
			QStringList drivers = plugin->drivers();
			foreach (const QString& driver, drivers) {
				_driverIds[driver] = plugin;
				emit driverLoaded(driver);
			}
			_watcher->addPath(file);
		} else {
			qWarning() << "Invalid plugin" << file;
			loader->unload();
			delete loader;
		}
	} else {
		qWarning() << "Invalid plugin" << file << loader->errorString();
		loader->unload();
		delete loader;
	}
}

void Registry::loadNotificationProvider(const QString &file)
{
	QPluginLoader* loader = new QPluginLoader(file, this);
	QObject *pluginObj = loader->instance();
	if (pluginObj) {
		NotificationPluginInterface *plugin = qobject_cast<NotificationPluginInterface*>(pluginObj);
		if (plugin) {
			_providerFiles[file] = loader;
			_providers += plugin;
			QStringList providers = plugin->providers();
			foreach (const QString& provider, providers) {
				_providerIds[provider] = plugin;
				emit notificationProviderLoaded(provider);
			}
			_watcher->addPath(file);
		} else {
			qWarning() << "Invalid plugin" << file;
			loader->unload();
			delete loader;
		}
	} else {
		qWarning() << "Invalid plugin" << file << loader->errorString();
		loader->unload();
		delete loader;
	}
}

void Registry::loadWatchlet(const QString &file)
{
	QPluginLoader* loader = new QPluginLoader(file, this);
	QObject *pluginObj = loader->instance();
	if (pluginObj) {
		WatchletPluginInterface *plugin = qobject_cast<WatchletPluginInterface*>(pluginObj);
		if (plugin) {
			_watchletFiles[file] = loader;
			_watchlets += plugin;
			QStringList watchlets = plugin->watchlets();
			foreach (const QString& watchlet, watchlets) {
				_watchletIds[watchlet] = plugin;
				emit watchletLoaded(watchlet);
			}
			_watcher->addPath(file);
		} else {
			qWarning() << "Invalid plugin" << file;
			loader->unload();
			delete loader;
		}
	} else {
		qWarning() << "Invalid plugin" << file << loader->errorString();
		loader->unload();
		delete loader;
	}
}

void Registry::unloadDriver(QPluginLoader *loader)
{
	QString file = loader->fileName();

	WatchPluginInterface *plugin = qobject_cast<WatchPluginInterface*>(loader->instance());
	QStringList drivers = plugin->drivers();

	foreach (const QString& driver, drivers) {
		emit driverUnloaded(driver);
		_driverIds.remove(driver);
	}

	_drivers.removeAll(plugin);
	_driverFiles.remove(file);
	_watcher->removePath(file);

	qDebug() << "Now unloading" << file;
	if (!loader->unload()) {
		qWarning() << "Could not unload plugin" << file;
	}

	delete loader;
}

void Registry::unloadNotificationProvider(QPluginLoader *loader)
{
	QString file = loader->fileName();
	NotificationPluginInterface *plugin = qobject_cast<NotificationPluginInterface*>(loader->instance());
	QStringList providers = plugin->providers();

	foreach (const QString& provider, providers) {
		emit notificationProviderUnloaded(provider);
		_providerIds.remove(provider);
	}

	_providers.removeAll(plugin);
	_providerFiles.remove(file);
	_watcher->removePath(file);

	qDebug() << "Now unloading" << file;
	if (!loader->unload()) {
		qWarning() << "Could not unload plugin" << file;
	}

	delete loader;
}

void Registry::unloadWatchlet(QPluginLoader *loader)
{
	QString file = loader->fileName();
	WatchletPluginInterface *plugin = qobject_cast<WatchletPluginInterface*>(loader->instance());
	QStringList watchlets = plugin->watchlets();

	foreach (const QString& watchlet, watchlets) {
		emit watchletUnloaded(watchlet);
		_watchletIds.remove(watchlet);
	}

	_watchlets.removeAll(plugin);
	_watchletFiles.remove(file);
	_watcher->removePath(file);

	qDebug() << "Now unloading" << file;
	if (!loader->unload()) {
		qWarning() << "Could not unload plugin" << file;
	}

	delete loader;
}

void Registry::handlePluginDirectoryChanged(const QString &path)
{
	// If the directory changed, rescan it to discover new plugins.
	if (path == SOWATCH_DRIVERS_DIR) {
		QDir dir(path);
		foreach (QString entry, dir.entryList(QDir::Files)) {
			QString file = dir.absoluteFilePath(entry);
			if (!_driverFiles.contains(file)) {
				loadDriver(file);
			}
		}
	} else if (path == SOWATCH_NOTIFICATIONS_DIR) {
		QDir dir(path);
		foreach (QString entry, dir.entryList(QDir::Files)) {
			QString file = dir.absoluteFilePath(entry);
			if (!_providerFiles.contains(file)) {
				loadNotificationProvider(file);
			}
		}
	} else if (path == SOWATCH_WATCHLETS_DIR) {
		QDir dir(path);
		foreach (QString entry, dir.entryList(QDir::Files)) {
			QString file = dir.absoluteFilePath(entry);
			if (!_watchletFiles.contains(file)) {
				loadWatchlet(file);
			}
		}
	}
}

void Registry::handlePluginFileChanged(const QString &file)
{
	if (_driverFiles.contains(file)) {
		unloadDriver(_driverFiles[file]);
	}
	if (_providerFiles.contains(file)) {
		unloadNotificationProvider(_providerFiles[file]);
	}
	if (_watchletFiles.contains(file)) {
		unloadWatchlet(_watchletFiles[file]);
	}
}