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
|
#include <QtCore/QDebug>
#include <sowatch.h>
#include "daemon.h"
using namespace sowatch;
Daemon::Daemon(QObject *parent) :
QObject(parent),
_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 (_watches.contains(name)) {
return _watches[name]->status();
} else {
return QLatin1String("disabled");
}
}
void Daemon::terminate()
{
QApplication::quit();
}
void Daemon::startWatch(const QString &name)
{
qDebug() << "Starting watch" << name;
QScopedPointer<ConfigKey> watchConfig(_config->getSubkey(name));
WatchHandler *handler = new WatchHandler(watchConfig.data(), this);
_status_mapper->setMapping(handler, name);
_watches[name] = handler;
handleWatchStatusChange(name);
connect(handler, SIGNAL(statusChanged()),
_status_mapper, SLOT(map()));
}
void Daemon::stopWatch(const QString &name)
{
qDebug() << "Stopping watch" << name;
WatchHandler *handler = _watches[name];
_watches.remove(name);
_status_mapper->removeMappings(handler);
delete handler;
handleWatchStatusChange(name);
}
void Daemon::startEnabledWatches()
{
QStringList watches = _watches_list->value().toStringList();
QSet<QString> startedWatches = _watches.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 = _watches.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)) {
QString watchName = enabled_key_pattern.cap(1);
bool enabled_in_config = _config->value(subkey).toBool();
bool currently_started = _watches.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 (_watches.contains(name)) {
emit WatchStatusChanged(name, _watches[name]->status());
} else if (_watches_list->value().toStringList().contains(name)) {
emit WatchStatusChanged(name, QLatin1String("disabled"));
} else {
emit WatchStatusChanged(name, QLatin1String("unconfigured"));
}
}
|