1/*
2 SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
3 SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kconfigwatcher.h"
9
10#include "config-kconfig.h"
11#include "kconfig_core_log_settings.h"
12
13#if KCONFIG_USE_DBUS
14#include <QDBusConnection>
15#include <QDBusMessage>
16#include <QDBusMetaType>
17
18#include "dbussanitizer_p.h"
19#endif
20
21#include <QDebug>
22#include <QHash>
23#include <QPointer>
24#include <QThreadStorage>
25
26class KConfigWatcherPrivate
27{
28public:
29 KSharedConfig::Ptr m_config;
30};
31
32KConfigWatcher::Ptr KConfigWatcher::create(const KSharedConfig::Ptr &config)
33{
34 static QThreadStorage<QHash<KSharedConfig *, QWeakPointer<KConfigWatcher>>> watcherList;
35
36 auto c = config.data();
37 KConfigWatcher::Ptr watcher;
38
39 if (!watcherList.localData().contains(key: c)) {
40 watcher = KConfigWatcher::Ptr(new KConfigWatcher(config));
41
42 watcherList.localData().insert(key: c, value: watcher.toWeakRef());
43
44 QObject::connect(sender: watcher.data(), signal: &QObject::destroyed, slot: [c]() {
45 watcherList.localData().remove(key: c);
46 });
47 }
48 return watcherList.localData().value(key: c).toStrongRef();
49}
50
51KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config)
52 : QObject(nullptr)
53 , d(new KConfigWatcherPrivate)
54{
55 Q_ASSERT(config);
56 d->m_config = config;
57 if (config->name().isEmpty()) {
58 return;
59 }
60
61 // Watching absolute paths is not supported and also makes no sense.
62 const bool isAbsolutePath = config->name().at(i: 0) == QLatin1Char('/');
63 if (isAbsolutePath) {
64 qCWarning(KCONFIG_CORE_LOG) << "Watching absolute paths is not supported" << config->name();
65 return;
66 }
67
68#if KCONFIG_USE_DBUS
69 qDBusRegisterMetaType<QByteArrayList>();
70 qDBusRegisterMetaType<QHash<QString, QByteArrayList>>();
71
72 QStringList watchedPaths = d->m_config->additionalConfigSources();
73 for (QString &file : watchedPaths) {
74 file.prepend(c: QLatin1Char('/'));
75 }
76 watchedPaths.prepend(t: kconfigDBusSanitizePath(path: QLatin1Char('/') + d->m_config->name()));
77
78 if (d->m_config->openFlags() & KConfig::IncludeGlobals) {
79 watchedPaths << QStringLiteral("/kdeglobals");
80 }
81
82 for (const QString &path : std::as_const(t&: watchedPaths)) {
83 QDBusConnection::sessionBus().connect(service: QString(),
84 path,
85 QStringLiteral("org.kde.kconfig.notify"),
86 QStringLiteral("ConfigChanged"),
87 receiver: this,
88 // clang-format off
89 SLOT(onConfigChangeNotification(QHash<QString,QByteArrayList>))
90 // clang-format on
91 );
92 }
93#endif
94}
95
96KConfigWatcher::~KConfigWatcher() = default;
97
98KSharedConfig::Ptr KConfigWatcher::config() const
99{
100 return d->m_config;
101}
102
103void KConfigWatcher::onConfigChangeNotification(const QHash<QString, QByteArrayList> &changes)
104{
105 // should we ever need it we can determine the file changed with QDbusContext::message().path(), but it doesn't seem too useful
106
107 d->m_config->reparseConfiguration();
108
109 QPointer guard(this);
110 for (auto it = changes.constBegin(); it != changes.constEnd(); it++) {
111 KConfigGroup group = d->m_config->group(group: QString()); // top level group
112 const auto parts = it.key().split(sep: QLatin1Char('\x1d')); // magic char, see KConfig
113 for (const QString &groupName : parts) {
114 group = group.group(group: groupName);
115 }
116 Q_EMIT configChanged(group, names: it.value());
117 if (!guard) {
118 return;
119 }
120 }
121}
122
123#include "moc_kconfigwatcher.cpp"
124

source code of kconfig/src/core/kconfigwatcher.cpp