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 <QThreadStorage>
20 #include <QHash>
21 
22 class KConfigWatcherPrivate {
23 public:
24  KSharedConfig::Ptr m_config;
25 };
26 
28 {
30 
31  auto c = config.data();
32  KConfigWatcher::Ptr watcher;
33 
34  if (!watcherList.localData().contains(c)) {
35  watcher = KConfigWatcher::Ptr(new KConfigWatcher(config));
36 
37  watcherList.localData().insert(c, watcher.toWeakRef());
38 
39  QObject::connect(watcher.data(), &QObject::destroyed, [c]() {
40  watcherList.localData().remove(c);
41  });
42  }
43  return watcherList.localData().value(c).toStrongRef();
44 }
45 
46 KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config):
47  QObject (nullptr),
48  d(new KConfigWatcherPrivate)
49 {
50  Q_ASSERT(config);
51 #if KCONFIG_USE_DBUS
52 
53  qDBusRegisterMetaType<QByteArrayList>();
54  qDBusRegisterMetaType<QHash<QString, QByteArrayList>>();
55 
56  d->m_config = config;
57 
58  QStringList watchedPaths;
59  watchedPaths << QLatin1Char('/') + d->m_config->name();
60  for (const QString &file: d->m_config->additionalConfigSources()) {
61  watchedPaths << QLatin1Char('/') + file;
62  }
63  if (d->m_config->openFlags() & KConfig::IncludeGlobals) {
64  watchedPaths << QStringLiteral("/kdeglobals");
65  }
66 
67  for(const QString &path: qAsConst(watchedPaths)) {
69  path,
70  QStringLiteral("org.kde.kconfig.notify"),
71  QStringLiteral("ConfigChanged"),
72  this,
73  SLOT(onConfigChangeNotification(QHash<QString,QByteArrayList>)));
74  }
75 #else
76  qCWarning(KCONFIG_CORE_LOG) << "Use of KConfigWatcher without DBus support. You will not receive updates";
77 #endif
78 }
79 
80 KConfigWatcher::~KConfigWatcher() = default;
81 
83 {
84  return d->m_config;
85 }
86 
87 void KConfigWatcher::onConfigChangeNotification(const QHash<QString, QByteArrayList> &changes)
88 {
89  //should we ever need it we can determine the file changed with QDbusContext::message().path(), but it doesn't seem too useful
90 
91  d->m_config->reparseConfiguration();
92 
93  for(auto it = changes.constBegin(); it != changes.constEnd(); it++) {
94  KConfigGroup group = d->m_config->group(QString());//top level group
95  const auto parts = it.key().split(QLatin1Char('\x1d')); //magic char, see KConfig
96  for(const QString &groupName: parts) {
97  group = group.group(groupName);
98  }
99  emit configChanged(group, it.value());
100  }
101 }
102 
Blend kdeglobals into the config object.
Definition: kconfig.h:83
KConfigGroup group(const QByteArray &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:31
QDBusConnection sessionBus()
Notifies when another client has updated this config file with the Notify flag set.
T * data() const const
QHash::const_iterator constEnd() const const
static Ptr create(const KSharedConfig::Ptr &config)
Instantiate a ConfigWatcher for a given config.
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:38
QHash::const_iterator constBegin() const const
QWeakPointer< T > toWeakRef() const const
KSharedConfig::Ptr config() const
Returns the config being watched.
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 22:46:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.