KCMUtils

dispatcher.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 2003 Matthias Kretz <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #include "dispatcher.h"
9 
10 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 85)
11 #include "dispatcher_p.h"
12 
13 #include <kcmutils_debug.h>
14 
15 namespace KSettings
16 {
17 namespace Dispatcher
18 {
19 Q_GLOBAL_STATIC(DispatcherPrivate, d)
20 
21 void registerComponent(const QString &componentName, QObject *recv, const char *slot)
22 {
23  Q_ASSERT(!componentName.isEmpty());
24  // qDebug() << componentName;
25  d()->m_componentName[recv] = componentName;
26  d()->m_componentInfo[componentName].slotList.append(ComponentInfo::Slot(recv, slot));
27 
28  ++(d()->m_componentInfo[componentName].count);
29  QObject::connect(recv, &QObject::destroyed, d(), &DispatcherPrivate::unregisterComponent);
30 }
31 
32 KSharedConfig::Ptr configForComponentName(const QString &componentName)
33 {
34  // qDebug() ;
35  return KSharedConfig::openConfig(componentName + QStringLiteral("rc"));
36 }
37 
39 {
40  // qDebug() ;
41  QList<QString> names;
42  for (QMap<QString, ComponentInfo>::ConstIterator it = d()->m_componentInfo.constBegin(), total = d()->m_componentInfo.constEnd(); it != total; ++it) {
43  if ((*it).count > 0) {
44  names.append(it.key());
45  }
46  }
47  return names;
48 }
49 
50 void reparseConfiguration(const QString &componentName)
51 {
52  // qDebug() << componentName;
53  // check if the componentName is valid:
54  if (!d()->m_componentInfo.contains(componentName)) {
55  return;
56  }
57  // first we reparse the config so that the KConfig object will be up to date
58  KSharedConfig::Ptr config = configForComponentName(componentName);
59  config->reparseConfiguration();
60 
61  const auto lstSlot = d()->m_componentInfo[componentName].slotList;
62  for (const ComponentInfo::Slot &slot : lstSlot) {
63  QMetaObject::invokeMethod(slot.first, slot.second);
64  }
65 }
66 
68 {
69  for (QMap<QString, ComponentInfo>::ConstIterator it = d()->m_componentInfo.constBegin(), total = d()->m_componentInfo.constEnd(); it != total; ++it) {
70  KSharedConfig::Ptr config = configForComponentName(it.key());
71  config->sync();
72  }
73 }
74 
75 void DispatcherPrivate::unregisterComponent(QObject *obj)
76 {
77  if (!m_componentName.contains(obj)) {
78  qCWarning(KCMUTILS_LOG) << Q_FUNC_INFO << "Tried to unregister an object which is not already registered.";
79  return;
80  }
81 
82  QString name = m_componentName[obj];
83  m_componentName.remove(obj); // obj will be destroyed when we return, so we better remove this entry
84  --(m_componentInfo[name].count);
85  // qDebug() << "componentName=" << name << "refcount=" << m_componentInfo[name].count;
86  Q_ASSERT(m_componentInfo[name].count >= 0);
87  if (m_componentInfo[name].count == 0) {
88  m_componentInfo.remove(name);
89  }
90 }
91 
92 } // namespace Dispatcher
93 } // namespace KSettings
94 
95 #include "moc_dispatcher_p.cpp"
96 #endif
void append(const T &value)
QList< QString > componentNames()
Definition: dispatcher.cpp:38
QMap::const_iterator constBegin() const const
void syncConfiguration()
When this function is called the KConfig objects of all the registered instances are sync()ed.
Definition: dispatcher.cpp:67
Definition: dialog.h:19
void reparseConfiguration(const QString &componentName)
Call this function when the configuration belonging to the associated componentData name has changed.
Definition: dispatcher.cpp:50
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void destroyed(QObject *obj)
void registerComponent(const QString &componentName, QObject *recv, const char *slot)
Register a slot to be called when the configuration for the componentData has changed.
Definition: dispatcher.cpp:21
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
A collection of classes to create configuration dialogs that work over component boundaries.
Definition: dialog.cpp:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 03:51:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.