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
|
/*
SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "kconfigwatcher.h"
#include "config-kconfig.h"
#include "kconfig_core_log_settings.h"
#if KCONFIG_USE_DBUS
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusMetaType>
#endif
#include <QDebug>
#include <QThreadStorage>
#include <QHash>
class KConfigWatcherPrivate {
public:
KSharedConfig::Ptr m_config;
};
KConfigWatcher::Ptr KConfigWatcher::create(const KSharedConfig::Ptr &config)
{
static QThreadStorage<QHash<KSharedConfig*, QWeakPointer<KConfigWatcher>>> watcherList;
auto c = config.data();
KConfigWatcher::Ptr watcher;
if (!watcherList.localData().contains(c)) {
watcher = KConfigWatcher::Ptr(new KConfigWatcher(config));
watcherList.localData().insert(c, watcher.toWeakRef());
QObject::connect(watcher.data(), &QObject::destroyed, [c]() {
watcherList.localData().remove(c);
});
}
return watcherList.localData().value(c).toStrongRef();
}
KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config):
QObject (nullptr),
d(new KConfigWatcherPrivate)
{
Q_ASSERT(config);
#if KCONFIG_USE_DBUS
qDBusRegisterMetaType<QByteArrayList>();
qDBusRegisterMetaType<QHash<QString, QByteArrayList>>();
d->m_config = config;
QStringList watchedPaths;
watchedPaths << QLatin1Char('/') + d->m_config->name();
for (const QString &file: d->m_config->additionalConfigSources()) {
watchedPaths << QLatin1Char('/') + file;
}
if (d->m_config->openFlags() & KConfig::IncludeGlobals) {
watchedPaths << QStringLiteral("/kdeglobals");
}
for(const QString &path: qAsConst(watchedPaths)) {
QDBusConnection::sessionBus().connect(QString(),
path,
QStringLiteral("org.kde.kconfig.notify"),
QStringLiteral("ConfigChanged"),
this,
SLOT(onConfigChangeNotification(QHash<QString,QByteArrayList>)));
}
#else
qCWarning(KCONFIG_CORE_LOG) << "Use of KConfigWatcher without DBus support. You will not receive updates";
#endif
}
KConfigWatcher::~KConfigWatcher() = default;
KSharedConfig::Ptr KConfigWatcher::config() const
{
return d->m_config;
}
void KConfigWatcher::onConfigChangeNotification(const QHash<QString, QByteArrayList> &changes)
{
//should we ever need it we can determine the file changed with QDbusContext::message().path(), but it doesn't seem too useful
d->m_config->reparseConfiguration();
for(auto it = changes.constBegin(); it != changes.constEnd(); it++) {
KConfigGroup group = d->m_config->group(QString());//top level group
const auto parts = it.key().split(QLatin1Char('\x1d')); //magic char, see KConfig
for(const QString &groupName: parts) {
group = group.group(groupName);
}
Q_EMIT configChanged(group, it.value());
}
}
|