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

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