File indexing completed on 2025-02-02 05:08:36

0001 /*
0002     SPDX-FileCopyrightText: 2010-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "configfile.h"
0008 
0009 #include <KCMultiDialog>
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 #include <KPluginMetaData>
0014 #include <KStringHandler>
0015 
0016 #include <QPointer>
0017 
0018 ConfigFile::ConfigFile(const QString &configName, QObject *parent)
0019     : SetupObject(parent)
0020     , m_config(new KConfig(configName))
0021 {
0022     m_name = configName;
0023 }
0024 
0025 ConfigFile::~ConfigFile()
0026 {
0027     delete m_config;
0028 }
0029 
0030 void ConfigFile::write()
0031 {
0032     create();
0033 }
0034 
0035 void ConfigFile::create()
0036 {
0037     Q_EMIT info(i18n("Writing config file for %1...", m_name));
0038 
0039     for (const Config &c : std::as_const(m_configData)) {
0040         KConfigGroup grp = m_config->group(c.group);
0041         if (c.obscure) {
0042             grp.writeEntry(c.key, KStringHandler::obscure(c.value));
0043         } else {
0044             grp.writeEntry(c.key, c.value);
0045         }
0046     }
0047 
0048     m_config->sync();
0049 
0050     if (m_editMode) {
0051         edit();
0052     }
0053 
0054     Q_EMIT finished(i18n("Config file for %1 is writing.", m_name));
0055 }
0056 
0057 void ConfigFile::destroy()
0058 {
0059     Q_EMIT info(i18n("Config file for %1 was not changed.", m_name));
0060 }
0061 
0062 void ConfigFile::setName(const QString &name)
0063 {
0064     m_name = name;
0065 }
0066 
0067 void ConfigFile::setConfig(const QString &group, const QString &key, const QString &value)
0068 {
0069     Config conf;
0070     conf.group = group;
0071     conf.key = key;
0072     conf.value = value;
0073     conf.obscure = false;
0074     m_configData.append(conf);
0075 }
0076 
0077 void ConfigFile::setPassword(const QString &group, const QString &key, const QString &value)
0078 {
0079     Config conf;
0080     conf.group = group;
0081     conf.key = key;
0082     conf.value = value;
0083     conf.obscure = true;
0084     m_configData.append(conf);
0085 }
0086 
0087 void ConfigFile::edit()
0088 {
0089     if (m_editName.isEmpty()) {
0090         Q_EMIT error(i18n("No given name for the configuration."));
0091         return;
0092     }
0093 
0094     if (m_editName == QLatin1StringView("freebusy")) {
0095         QPointer<KCMultiDialog> dialog = new KCMultiDialog();
0096         dialog->addModule(KPluginMetaData(QStringLiteral("korganizer_configfreebusy.desktop")));
0097         dialog->exec();
0098         delete dialog;
0099         return;
0100     }
0101 
0102     Q_EMIT error(i18n("Unknown configuration name '%1'", m_editName));
0103 }
0104 
0105 void ConfigFile::setEditName(const QString &name)
0106 {
0107     m_editName = name;
0108 }
0109 
0110 void ConfigFile::setEditMode(const bool editMode)
0111 {
0112     m_editMode = editMode;
0113 }
0114 
0115 #include "moc_configfile.cpp"