File indexing completed on 2024-05-05 16:05:44

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2003 Matthias Kretz <kretz@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "dispatcher.h"
0009 
0010 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 85)
0011 #include "dispatcher_p.h"
0012 
0013 #include <kcmutils_debug.h>
0014 
0015 namespace KSettings
0016 {
0017 namespace Dispatcher
0018 {
0019 Q_GLOBAL_STATIC(DispatcherPrivate, d)
0020 
0021 void registerComponent(const QString &componentName, QObject *recv, const char *slot)
0022 {
0023     Q_ASSERT(!componentName.isEmpty());
0024     // qDebug() << componentName;
0025     d()->m_componentName[recv] = componentName;
0026     d()->m_componentInfo[componentName].slotList.append(ComponentInfo::Slot(recv, slot));
0027 
0028     ++(d()->m_componentInfo[componentName].count);
0029     QObject::connect(recv, &QObject::destroyed, d(), &DispatcherPrivate::unregisterComponent);
0030 }
0031 
0032 KSharedConfig::Ptr configForComponentName(const QString &componentName)
0033 {
0034     // qDebug() ;
0035     return KSharedConfig::openConfig(componentName + QStringLiteral("rc"));
0036 }
0037 
0038 QList<QString> componentNames()
0039 {
0040     // qDebug() ;
0041     QList<QString> names;
0042     for (QMap<QString, ComponentInfo>::ConstIterator it = d()->m_componentInfo.constBegin(), total = d()->m_componentInfo.constEnd(); it != total; ++it) {
0043         if ((*it).count > 0) {
0044             names.append(it.key());
0045         }
0046     }
0047     return names;
0048 }
0049 
0050 void reparseConfiguration(const QString &componentName)
0051 {
0052     // qDebug() << componentName;
0053     // check if the componentName is valid:
0054     if (!d()->m_componentInfo.contains(componentName)) {
0055         return;
0056     }
0057     // first we reparse the config so that the KConfig object will be up to date
0058     KSharedConfig::Ptr config = configForComponentName(componentName);
0059     config->reparseConfiguration();
0060 
0061     const auto lstSlot = d()->m_componentInfo[componentName].slotList;
0062     for (const ComponentInfo::Slot &slot : lstSlot) {
0063         QMetaObject::invokeMethod(slot.first, slot.second);
0064     }
0065 }
0066 
0067 void syncConfiguration()
0068 {
0069     for (QMap<QString, ComponentInfo>::ConstIterator it = d()->m_componentInfo.constBegin(), total = d()->m_componentInfo.constEnd(); it != total; ++it) {
0070         KSharedConfig::Ptr config = configForComponentName(it.key());
0071         config->sync();
0072     }
0073 }
0074 
0075 void DispatcherPrivate::unregisterComponent(QObject *obj)
0076 {
0077     if (!m_componentName.contains(obj)) {
0078         qCWarning(KCMUTILS_LOG) << Q_FUNC_INFO << "Tried to unregister an object which is not already registered.";
0079         return;
0080     }
0081 
0082     QString name = m_componentName[obj];
0083     m_componentName.remove(obj); // obj will be destroyed when we return, so we better remove this entry
0084     --(m_componentInfo[name].count);
0085     // qDebug() << "componentName=" << name << "refcount=" << m_componentInfo[name].count;
0086     Q_ASSERT(m_componentInfo[name].count >= 0);
0087     if (m_componentInfo[name].count == 0) {
0088         m_componentInfo.remove(name);
0089     }
0090 }
0091 
0092 } // namespace Dispatcher
0093 } // namespace KSettings
0094 
0095 #include "moc_dispatcher_p.cpp"
0096 #endif