File indexing completed on 2024-12-22 05:29:15

0001 /*
0002  * Copyright 2019 Kevin Ottens <kevin.ottens@enioka.com>
0003  * Copyright 2019 Tomaz Canabrava <tcanabrava@kde.org>
0004  *
0005  * This program is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU General Public License as
0007  * published by the Free Software Foundation; either version 2 of
0008  * the License or (at your option) version 3 or any later version
0009  * accepted by the membership of KDE e.V. (or its successor approved
0010  * by the membership of KDE e.V.), which shall act as a proxy
0011  * defined in Section 14 of version 3 of the license.
0012  *
0013  * This program is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  * GNU General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU General Public License
0019  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0020  */
0021 
0022 #include "autosettingshandler.h"
0023 
0024 #include <KCoreConfigSkeleton>
0025 #include <QDebug>
0026 #include <QMetaProperty>
0027 
0028 void AutoSettingsHandler::addSettings(KCoreConfigSkeleton *setting)
0029 {
0030     m_settings.append(setting);
0031     auto settingsChangedSlotIndex = metaObject()->indexOfMethod("triggerSave()");
0032     auto settingsChangedSlot = metaObject()->method(settingsChangedSlotIndex);
0033 
0034     for (const auto &item : setting->items()) {
0035         const auto signallingItem = dynamic_cast<KConfigCompilerSignallingItem *>(item);
0036 
0037         if (!signallingItem) {
0038             continue;
0039         }
0040 
0041         QString name = signallingItem->name();
0042         name[0] = name[0].toLower();
0043 
0044         const auto metaObject = setting->metaObject();
0045         const auto propertyIndex = metaObject->indexOfProperty(name.toUtf8().constData());
0046 
0047         const auto property = metaObject->property(propertyIndex);
0048         if (!property.hasNotifySignal()) {
0049             continue;
0050         }
0051 
0052         const auto changedSignal = property.notifySignal();
0053         QObject::connect(setting, changedSignal, this, settingsChangedSlot);
0054     }
0055 }
0056 
0057 void AutoSettingsHandler::triggerSave()
0058 {
0059     for (const auto &setting : qAsConst(m_settings)) {
0060         setting->save();
0061     }
0062 }