File indexing completed on 2024-05-12 05:38:24

0001 /*
0002     SPDX-FileCopyrightText: 2011-2012 Sebastian Kügler <sebas@kde.org>
0003     SPDX-FileCopyrightText: 2013 Aaron Seigo <aseigo@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "configgroup.h"
0009 #include "debug.h"
0010 
0011 #include <QDebug>
0012 #include <QTimer>
0013 #include <chrono>
0014 #include <kconfig.h>
0015 #include <kconfiggroup.h>
0016 #include <ksharedconfig.h>
0017 
0018 using namespace std::chrono_literals;
0019 
0020 class ConfigGroupPrivate
0021 {
0022 public:
0023     ConfigGroupPrivate(ConfigGroup *q)
0024         : q(q)
0025         , config(nullptr)
0026         , configGroup(nullptr)
0027     {
0028     }
0029 
0030     ~ConfigGroupPrivate()
0031     {
0032         delete configGroup;
0033     }
0034 
0035     ConfigGroup *q;
0036     KSharedConfigPtr config;
0037     KConfigGroup *configGroup;
0038     QString file;
0039     QTimer *synchTimer;
0040     QString group;
0041 };
0042 
0043 ConfigGroup::ConfigGroup(QObject *parent)
0044     : QObject(parent)
0045     , d(new ConfigGroupPrivate(this))
0046 {
0047     // Delay and compress everything within 5 seconds into one sync
0048     d->synchTimer = new QTimer(this);
0049     d->synchTimer->setSingleShot(true);
0050     d->synchTimer->setInterval(1500ms);
0051     connect(d->synchTimer, &QTimer::timeout, this, &ConfigGroup::sync);
0052 }
0053 
0054 ConfigGroup::~ConfigGroup()
0055 {
0056     if (d->synchTimer->isActive()) {
0057         // qDebug() << "SYNC......";
0058         d->synchTimer->stop();
0059         sync();
0060     }
0061 
0062     delete d;
0063 }
0064 
0065 KConfigGroup *ConfigGroup::configGroup()
0066 {
0067     return d->configGroup;
0068 }
0069 
0070 QString ConfigGroup::file() const
0071 {
0072     return d->file;
0073 }
0074 
0075 void ConfigGroup::setFile(const QString &filename)
0076 {
0077     if (d->file == filename) {
0078         return;
0079     }
0080     d->file = filename;
0081     d->config = nullptr;
0082     readConfigFile();
0083     Q_EMIT fileChanged();
0084 }
0085 
0086 KSharedConfigPtr ConfigGroup::config() const
0087 {
0088     return d->config;
0089 }
0090 
0091 void ConfigGroup::setConfig(KSharedConfigPtr config)
0092 {
0093     if (d->config == config) {
0094         return;
0095     }
0096 
0097     d->config = config;
0098 
0099     if (d->config) {
0100         d->file = config->name();
0101     } else {
0102         d->file.clear();
0103     }
0104 
0105     readConfigFile();
0106 }
0107 
0108 QString ConfigGroup::group() const
0109 {
0110     return d->group;
0111 }
0112 
0113 void ConfigGroup::setGroup(const QString &groupname)
0114 {
0115     if (d->group == groupname) {
0116         return;
0117     }
0118     d->group = groupname;
0119     readConfigFile();
0120     Q_EMIT groupChanged();
0121     Q_EMIT keyListChanged();
0122 }
0123 
0124 QStringList ConfigGroup::keyList() const
0125 {
0126     if (!d->configGroup) {
0127         return QStringList();
0128     }
0129     return d->configGroup->keyList();
0130 }
0131 
0132 QStringList ConfigGroup::groupList() const
0133 {
0134     if (!d->configGroup) {
0135         return QStringList();
0136     }
0137     return d->configGroup->groupList();
0138 }
0139 
0140 bool ConfigGroup::readConfigFile()
0141 {
0142     // Find parent ConfigGroup
0143     ConfigGroup *parentGroup = nullptr;
0144     QObject *current = parent();
0145     while (current) {
0146         parentGroup = qobject_cast<ConfigGroup *>(current);
0147         if (parentGroup) {
0148             break;
0149         }
0150         current = current->parent();
0151     }
0152 
0153     delete d->configGroup;
0154     d->configGroup = nullptr;
0155 
0156     if (parentGroup) {
0157         d->configGroup = new KConfigGroup(parentGroup->configGroup(), d->group);
0158         return true;
0159     } else {
0160         if (!d->config) {
0161             if (d->file.isEmpty()) {
0162                 qCWarning(PLASMASHELL) << "Could not find KConfig Parent: specify a file or parent to another ConfigGroup";
0163                 return false;
0164             }
0165 
0166             d->config = KSharedConfig::openConfig(d->file);
0167         }
0168 
0169         d->configGroup = new KConfigGroup(d->config, d->group);
0170         return true;
0171     }
0172 }
0173 
0174 // Bound methods and slots
0175 
0176 bool ConfigGroup::writeEntry(const QString &key, const QJSValue &value)
0177 {
0178     if (!d->configGroup) {
0179         return false;
0180     }
0181 
0182     d->configGroup->writeEntry(key, value.toVariant());
0183     d->synchTimer->start();
0184     return true;
0185 }
0186 
0187 QVariant ConfigGroup::readEntry(const QString &key)
0188 {
0189     if (!d->configGroup) {
0190         return QVariant();
0191     }
0192     const QVariant value = d->configGroup->readEntry(key, QVariant(""));
0193     // qDebug() << " reading setting: " << key << value;
0194     return value;
0195 }
0196 
0197 void ConfigGroup::deleteEntry(const QString &key)
0198 {
0199     if (d->configGroup) {
0200         d->configGroup->deleteEntry(key);
0201     }
0202 }
0203 
0204 void ConfigGroup::sync()
0205 {
0206     if (d->configGroup) {
0207         // qDebug() << "synching config...";
0208         d->configGroup->sync();
0209     }
0210 }