KConfig

kconfigwatcher.cpp
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 <QThreadStorage>
24
25class KConfigWatcherPrivate
26{
27public:
28 KSharedConfig::Ptr m_config;
29};
30
32{
34
35 auto c = config.data();
36 KConfigWatcher::Ptr watcher;
37
38 if (!watcherList.localData().contains(c)) {
40
41 watcherList.localData().insert(c, watcher.toWeakRef());
42
43 QObject::connect(watcher.data(), &QObject::destroyed, [c]() {
44 watcherList.localData().remove(c);
45 });
46 }
47 return watcherList.localData().value(c).toStrongRef();
48}
49
50KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config)
51 : QObject(nullptr)
52 , d(new KConfigWatcherPrivate)
53{
54 Q_ASSERT(config);
55 d->m_config = config;
56 if (config->name().isEmpty()) {
57 return;
58 }
59
60 // Watching absolute paths is not supported and also makes no sense.
61 const bool isAbsolutePath = config->name().at(0) == QLatin1Char('/');
62 if (isAbsolutePath) {
63 qCWarning(KCONFIG_CORE_LOG) << "Watching absolute paths is not supported" << config->name();
64 return;
65 }
66
67#if KCONFIG_USE_DBUS
68 qDBusRegisterMetaType<QByteArrayList>();
69 qDBusRegisterMetaType<QHash<QString, QByteArrayList>>();
70
71 QStringList watchedPaths = d->m_config->additionalConfigSources();
72 for (QString &file : watchedPaths) {
73 file.prepend(QLatin1Char('/'));
74 }
75 watchedPaths.prepend(kconfigDBusSanitizePath(QLatin1Char('/') + d->m_config->name()));
76
77 if (d->m_config->openFlags() & KConfig::IncludeGlobals) {
78 watchedPaths << QStringLiteral("/kdeglobals");
79 }
80
81 for (const QString &path : std::as_const(watchedPaths)) {
83 path,
84 QStringLiteral("org.kde.kconfig.notify"),
85 QStringLiteral("ConfigChanged"),
86 this,
87 // clang-format off
88 SLOT(onConfigChangeNotification(QHash<QString,QByteArrayList>))
89 // clang-format on
90 );
91 }
92#endif
93}
94
95KConfigWatcher::~KConfigWatcher() = default;
96
98{
99 return d->m_config;
100}
101
102void KConfigWatcher::onConfigChangeNotification(const QHash<QString, QByteArrayList> &changes)
103{
104 // should we ever need it we can determine the file changed with QDbusContext::message().path(), but it doesn't seem too useful
105
106 d->m_config->reparseConfiguration();
107
108 for (auto it = changes.constBegin(); it != changes.constEnd(); it++) {
109 KConfigGroup group = d->m_config->group(QString()); // top level group
110 const auto parts = it.key().split(QLatin1Char('\x1d')); // magic char, see KConfig
111 for (const QString &groupName : parts) {
112 group = group.group(groupName);
113 }
114 Q_EMIT configChanged(group, it.value());
115 }
116}
117
118#include "moc_kconfigwatcher.cpp"
KConfigGroup group(const QString &group)
Returns an object for the named subgroup.
A class for one specific group in a KConfig object.
Notifies when another client has updated this config file with the Notify flag set.
static Ptr create(const KSharedConfig::Ptr &config)
Instantiate a ConfigWatcher for a given config.
void configChanged(const KConfigGroup &group, const QByteArrayList &names)
Emitted when a config group has changed The config will be reloaded before this signal is emitted.
KSharedConfig::Ptr config() const
Returns the config being watched.
@ IncludeGlobals
Blend kdeglobals into the config object.
Definition kconfig.h:82
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QDBusConnection sessionBus()
const_iterator constBegin() const const
const_iterator constEnd() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
T * data() const const
QWeakPointer< T > toWeakRef() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:55:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.