File indexing completed on 2024-05-12 03:54:10

0001 /*
0002     SPDX-FileCopyrightText: 2020 Kevin Ottens <kevin.ottens@enioka.com>
0003     SPDX-FileCopyrightText: 2020 Cyril Rossi <cyril.rossi@enioka.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "settingstateproxy.h"
0009 #include "kcmutils_debug.h"
0010 
0011 #include <QDebug>
0012 #include <QMetaMethod>
0013 
0014 KCoreConfigSkeleton *SettingStateProxy::configObject() const
0015 {
0016     return m_configObject;
0017 }
0018 
0019 void SettingStateProxy::setConfigObject(KCoreConfigSkeleton *configObject)
0020 {
0021     if (m_configObject == configObject) {
0022         return;
0023     }
0024 
0025     if (m_configObject) {
0026         m_configObject->disconnect(this);
0027     }
0028 
0029     m_configObject = configObject;
0030     Q_EMIT configObjectChanged();
0031     updateState();
0032     connectSetting();
0033 }
0034 
0035 QString SettingStateProxy::settingName() const
0036 {
0037     return m_settingName;
0038 }
0039 
0040 void SettingStateProxy::setSettingName(const QString &settingName)
0041 {
0042     if (m_settingName == settingName) {
0043         return;
0044     }
0045 
0046     if (m_configObject) {
0047         m_configObject->disconnect(this);
0048     }
0049 
0050     m_settingName = settingName;
0051     Q_EMIT settingNameChanged();
0052     updateState();
0053     connectSetting();
0054 }
0055 
0056 bool SettingStateProxy::isImmutable() const
0057 {
0058     return m_immutable;
0059 }
0060 
0061 bool SettingStateProxy::isDefaulted() const
0062 {
0063     return m_defaulted;
0064 }
0065 
0066 void SettingStateProxy::updateState()
0067 {
0068     const auto item = m_configObject ? m_configObject->findItem(m_settingName) : nullptr;
0069     const auto immutable = item ? item->isImmutable() : false;
0070     const auto defaulted = item ? item->isDefault() : true;
0071 
0072     if (m_immutable != immutable) {
0073         m_immutable = immutable;
0074         Q_EMIT immutableChanged();
0075     }
0076 
0077     if (m_defaulted != defaulted) {
0078         m_defaulted = defaulted;
0079         Q_EMIT defaultedChanged();
0080     }
0081 }
0082 
0083 void SettingStateProxy::connectSetting()
0084 {
0085     const auto item = m_configObject ? m_configObject->findItem(m_settingName) : nullptr;
0086     if (!item) {
0087         return;
0088     }
0089 
0090     const auto updateStateSlotIndex = metaObject()->indexOfMethod("updateState()");
0091     Q_ASSERT(updateStateSlotIndex >= 0);
0092     const auto updateStateSlot = metaObject()->method(updateStateSlotIndex);
0093     Q_ASSERT(updateStateSlot.isValid());
0094 
0095     const auto itemHasSignals = dynamic_cast<KConfigCompilerSignallingItem *>(item) || dynamic_cast<KPropertySkeletonItem *>(item);
0096     if (!itemHasSignals) {
0097         qCWarning(KCMUTILS_LOG) << "Attempting to use SettingStateProxy with a non signalling item:" << m_settingName;
0098         return;
0099     }
0100 
0101     const auto propertyName = [this] {
0102         auto name = m_settingName;
0103         if (name.at(0).isUpper()) {
0104             name[0] = name[0].toLower();
0105         }
0106         return name.toUtf8();
0107     }();
0108 
0109     const auto metaObject = m_configObject->metaObject();
0110     const auto propertyIndex = metaObject->indexOfProperty(propertyName.constData());
0111     Q_ASSERT(propertyIndex >= 0);
0112     const auto property = metaObject->property(propertyIndex);
0113     Q_ASSERT(property.isValid());
0114     if (!property.hasNotifySignal()) {
0115         qCWarning(KCMUTILS_LOG) << "Attempting to use SettingStateProxy with a non notifying property:" << propertyName;
0116         return;
0117     }
0118 
0119     const auto changedSignal = property.notifySignal();
0120     Q_ASSERT(changedSignal.isValid());
0121     connect(m_configObject, changedSignal, this, updateStateSlot);
0122     connect(m_configObject, &KCoreConfigSkeleton::configChanged, this, &SettingStateProxy::updateState);
0123 }
0124 
0125 #include "moc_settingstateproxy.cpp"