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
|
#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";
}
}
void WatchHandler::updateWatchlets()
{
Registry *registry = Registry::registry();
if (!_server) return;
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: " << _watchlets.keys();
}
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;
}
}
|