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
|
#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)));
// Connect to the registry in case plugins are unloaded
connect(registry, SIGNAL(driverUnloaded(QString)),
SLOT(handleDriverUnloaded(QString)));
connect(registry, SIGNAL(watchletLoaded(QString)),
SLOT(updateWatchlets()));
connect(registry, SIGNAL(watchletUnloaded(QString)),
SLOT(handleWatchletUnloaded(QString)));
connect(registry, SIGNAL(notificationProviderLoaded(QString)),
SLOT(updateProviders()));
connect(registry, SIGNAL(notificationProviderUnloaded(QString)),
SLOT(handleProviderUnloaded(QString)));
// Get the watch driver
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 && _watch->isConnected()) {
return "connected";
} else if (_config->value("enable").toBool()) {
return "enabled";
} else {
return "disabled";
}
}
Watchlet* WatchHandler::createWatchlet(const QString &id)
{
Registry *registry = Registry::registry();
WatchletPluginInterface *plugin = registry->getWatchletPlugin(id);
if (!plugin) {
qWarning() << "Unknown watchlet" << id;
return 0;
}
ConfigKey *subconfig = _config->getSubkey(id);
Watchlet* watchlet = plugin->getWatchlet(id, subconfig, _server);
delete subconfig;
return watchlet;
}
void WatchHandler::deleteWatchletAt(int index)
{
const QString id = _watchlet_order[index];
Watchlet *watchlet = _watchlets[id];
_server->removeWatchlet(watchlet);
_watchlet_order.removeAt(index);
_watchlets.remove(id);
delete watchlet;
}
void WatchHandler::updateWatchlets()
{
if (!_server) return;
QStringList newWatchlets = _config->value("watchlets").toStringList();
// Try to do one operation at the time
// (e.g. move a watchlet or add/remove it).
// Find the first difference
int i;
for (i = 0; i < newWatchlets.size(); i++) {
// Precondition: newWatchlets and curWatchlets are equal in range 0..i-1
if (i >= _watchlet_order.size()) {
// We need to add this watchlet
const QString id = newWatchlets[i];
Watchlet *watchlet = createWatchlet(id);
_watchlet_order << id;
_watchlets[id] = watchlet;
_server->addWatchlet(watchlet);
} else if (newWatchlets[i] != _watchlet_order[i]) {
// Let's find out if this watchlet has been moved, or removed.
const QString id = _watchlet_order[i];
int j;
for (j = i; j < newWatchlets.size(); j++) {
if (id == newWatchlets[j]) {
break; // Found
}
}
if (j == _watchlet_order.size()) {
// It was not found, so it has been removed
deleteWatchletAt(i);
} else {
// It has been found at index j, it needs to moved.
_watchlet_order.move(i, j);
_server->moveWatchlet(_watchlets[id], j);
}
}
}
while (i < _watchlet_order.size()) {
// These watchlets are to be unloaded
deleteWatchletAt(i);
}
Q_ASSERT(newWatchlets == _watchlet_order);
qDebug() << "New watchlet order: " << _watchlet_order;
}
void WatchHandler::updateProviders()
{
Registry *registry = Registry::registry();
if (!_server) return;
QSet<QString> curProviders = _providers.keys().toSet();
QSet<QString> newProviders = _config->value("providers").toStringList().toSet();
QSet<QString> removed = curProviders - newProviders;
QSet<QString> added = newProviders - curProviders;
qDebug() << "Providers to remove: " << removed;
qDebug() << "Providers to add: " << added;
foreach (const QString& s, removed) {
NotificationProvider *provider = _providers[s];
_server->removeProvider(provider);
_providers.remove(s);
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;
}
qDebug() << "Providers reloaded: " << _providers.keys();
}
void WatchHandler::handleConfigSubkeyChanged(const QString &subkey)
{
if (subkey == "watchlets") {
qDebug() << "Watchlets list changed";
updateWatchlets();
} else if (subkey == "providers") {
qDebug() << "Providers list changed";
updateProviders();
} else if (subkey == "next-watchlet-button" && _server) {
_server->setNextWatchletButton(_config->value("next-watchlet-button").toString());
}
}
void WatchHandler::handleDriverUnloaded(const QString &id)
{
if (id == _config->value("driver").toString()) {
// Emergency disconnection!
qWarning("Unloading driver of active watch!");
if (_server) {
delete _server;
_server = 0;
}
if (_watch) {
delete _watch;
_watch = 0;
}
emit statusChanged();
}
}
void WatchHandler::handleWatchletUnloaded(const QString &id)
{
if (_watchlets.contains(id)) {
qDebug() << "Unloading watchlet" << id << "from watch";
Watchlet* watchlet = _watchlets[id];
_server->removeWatchlet(watchlet);
_watchlets.remove(id);
delete watchlet;
}
}
void WatchHandler::handleProviderUnloaded(const QString &id)
{
if (_providers.contains(id)) {
qDebug() << "Unloading provider" << id << "from watch";
NotificationProvider *provider = _providers[id];
_server->removeProvider(provider);
_providers.remove(id);
delete provider;
}
}
|