KConfig

kconfigwatcher.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 David Edmundson <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "kconfigwatcher.h"
8 
9 #include "config-kconfig.h"
10 #include "kconfig_core_log_settings.h"
11 
12 #if KCONFIG_USE_DBUS
13 #include <QDBusConnection>
14 #include <QDBusMessage>
15 #include <QDBusMetaType>
16 #endif
17 
18 #include <QDebug>
19 #include <QHash>
20 #include <QThreadStorage>
21 
22 class KConfigWatcherPrivate
23 {
24 public:
25  KSharedConfig::Ptr m_config;
26 };
27 
29 {
31 
32  auto c = config.data();
33  KConfigWatcher::Ptr watcher;
34 
35  if (!watcherList.localData().contains(c)) {
37 
38  watcherList.localData().insert(c, watcher.toWeakRef());
39 
40  QObject::connect(watcher.data(), &QObject::destroyed, [c]() {
41  watcherList.localData().remove(c);
42  });
43  }
44  return watcherList.localData().value(c).toStrongRef();
45 }
46 
47 KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config)
48  : QObject(nullptr)
49  , d(new KConfigWatcherPrivate)
50 {
51  Q_ASSERT(config);
52  d->m_config = config;
53 
54 #if KCONFIG_USE_DBUS
55 
56  qDBusRegisterMetaType<QByteArrayList>();
57  qDBusRegisterMetaType<QHash<QString, QByteArrayList>>();
58 
59 
60  QStringList watchedPaths = d->m_config->additionalConfigSources();
61  for (QString &file : watchedPaths) {
62  file.prepend(QLatin1Char('/'));
63  }
64  watchedPaths.prepend(QLatin1Char('/') + d->m_config->name().replace(QLatin1Char('-'), QLatin1Char('_')));
65 
66  if (d->m_config->openFlags() & KConfig::IncludeGlobals) {
67  watchedPaths << QStringLiteral("/kdeglobals");
68  }
69 
70  for (const QString &path : std::as_const(watchedPaths)) {
72  path,
73  QStringLiteral("org.kde.kconfig.notify"),
74  QStringLiteral("ConfigChanged"),
75  this,
76  SLOT(onConfigChangeNotification(QHash<QString, QByteArrayList>)));
77  }
78 #else
79  qCWarning(KCONFIG_CORE_LOG) << "Use of KConfigWatcher without DBus support. You will not receive updates";
80 #endif
81 }
82 
83 KConfigWatcher::~KConfigWatcher() = default;
84 
86 {
87  return d->m_config;
88 }
89 
90 void KConfigWatcher::onConfigChangeNotification(const QHash<QString, QByteArrayList> &changes)
91 {
92  // should we ever need it we can determine the file changed with QDbusContext::message().path(), but it doesn't seem too useful
93 
94  d->m_config->reparseConfiguration();
95 
96  for (auto it = changes.constBegin(); it != changes.constEnd(); it++) {
97  KConfigGroup group = d->m_config->group(QString()); // top level group
98  const auto parts = it.key().split(QLatin1Char('\x1d')); // magic char, see KConfig
99  for (const QString &groupName : parts) {
100  group = group.group(groupName);
101  }
102  Q_EMIT configChanged(group, it.value());
103  }
104 }
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
T * data() const const
Q_EMITQ_EMIT
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.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void destroyed(QObject *obj)
QDBusConnection sessionBus()
QHash::const_iterator constBegin() const const
QHash::const_iterator constEnd() const const
static Ptr create(const KSharedConfig::Ptr &config)
Instantiate a ConfigWatcher for a given config.
@ IncludeGlobals
Blend kdeglobals into the config object.
Definition: kconfig.h:83
KConfigGroup group(const QString &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:36
KSharedConfig::Ptr config() const
Returns the config being watched.
QWeakPointer< T > toWeakRef() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:07:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.