File indexing completed on 2024-12-22 05:07:41

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