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
|
#include <QtCore/QDebug>
#include <sowatch.h>
#include "daemon.h"
using namespace sowatch;
Daemon::Daemon(QObject *parent) :
QObject(parent),
_registry(Registry::registry()),
_config(new GConfKey("/apps/sowatch", this)),
_watches_list(_config->getSubkey("watches", this)),
_status_mapper(new QSignalMapper(this))
{
connect(_config, SIGNAL(subkeyChanged(QString)),
SLOT(handleSettingsChanged(QString)));
connect(_status_mapper, SIGNAL(mapped(QString)),
SLOT(handleWatchStatusChange(QString)));
startEnabledWatches();
}
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(_config->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;
handleWatchStatusChange(name);
// Connect watch status signals
_status_mapper->setMapping(watch, name);
connect(watch, SIGNAL(connected()),
_status_mapper, SLOT(map()));
connect(watch, SIGNAL(disconnected()),
_status_mapper, SLOT(map()));
// 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;
WatchServer* server = _servers[name];
Watch* watch = server->watch();
server->deleteLater();
watch->deleteLater();
_servers.remove(name);
handleWatchStatusChange(name);
}
void Daemon::startEnabledWatches()
{
QStringList watches = _watches_list->value().toStringList();
QSet<QString> startedWatches = _servers.keys().toSet();
QSet<QString> removed = startedWatches - watches.toSet();
// Those watches have been entirely removed from the list, not disabled first
foreach (const QString& s, removed) {
stopWatch(s);
}
foreach (const QString& s, watches) {
bool enabled_in_config = _config->value(s + "/enable").toBool();
bool currently_started = _servers.contains(s);
if (enabled_in_config && !currently_started) {
startWatch(s);
} else if (currently_started && !enabled_in_config) {
stopWatch(s);
}
}
}
void Daemon::handleSettingsChanged(const QString &subkey)
{
qDebug() << "Daemon settings changed" << subkey;
static QRegExp enabled_key_pattern("^([^/])/enable$");
if (enabled_key_pattern.exactMatch(subkey)) {
QStringList watches = _watches_list->value().toStringList();
QString watchName = enabled_key_pattern.cap(1);
bool enabled_in_config = _config->value(subkey).toBool();
bool currently_started = _servers.contains(watchName);
if (enabled_in_config && !currently_started) {
startWatch(watchName);
} else if (currently_started && !enabled_in_config) {
stopWatch(watchName);
}
} else if (subkey == "watches") {
startEnabledWatches();
}
}
void Daemon::handleWatchStatusChange(const QString &name)
{
if (_servers.contains(name)) {
WatchServer* server = _servers[name];
Watch* watch = server->watch();
if (watch->isConnected()) {
emit WatchStatusChanged(name, QLatin1String("connected"));
} else {
emit WatchStatusChanged(name, QLatin1String("enabled"));
}
} else if (_watches_list->value().toStringList().contains(name)) {
emit WatchStatusChanged(name, QLatin1String("disabled"));
} else {
emit WatchStatusChanged(name, QLatin1String("unconfigured"));
}
}
|