File indexing completed on 2024-04-28 09:36:09

0001 /*
0002     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "hgconfig.h"
0008 #include "hgwrapper.h"
0009 
0010 #include <QDir>
0011 #include <QDebug>
0012 #include <KConfig>
0013 #include <KConfigGroup>
0014 
0015 HgConfig::HgConfig(ConfigType configType) :
0016     m_configType(configType),
0017     m_config(nullptr)
0018 {
0019     getConfigFilePath();
0020     loadConfig();
0021 }
0022 
0023 HgConfig::~HgConfig()
0024 {
0025     delete m_config;
0026 }
0027 
0028 QString HgConfig::configFilePath() const
0029 {
0030     return m_configFilePath;
0031 }
0032 
0033 bool HgConfig::getConfigFilePath()
0034 {
0035     switch (m_configType) {
0036     case RepoConfig:
0037         {
0038             m_configFilePath = HgWrapper::instance()->getBaseDir() + QLatin1String("/.hg/hgrc");
0039             break;
0040         }
0041     case GlobalConfig:
0042         {
0043             m_configFilePath = QDir::homePath() + QLatin1String("/.hgrc");
0044             break;
0045         }
0046     case TempConfig:
0047         break;
0048     }
0049     return true;
0050 }
0051 
0052 bool HgConfig::loadConfig()
0053 {
0054     m_config = new KConfig(m_configFilePath, KConfig::SimpleConfig);
0055     return true;
0056 }
0057 
0058 QString HgConfig::property(const QString &section,
0059                            const QString &propertyName) const
0060 {
0061     KConfigGroup group(m_config, section);
0062     return group.readEntry(propertyName, QString()).trimmed();
0063 }
0064 
0065 void HgConfig::setProperty(const QString &section, 
0066                               const QString &propertyName,
0067                               const QString &propertyValue)
0068 {
0069     KConfigGroup uiGroup(m_config, section);
0070     if (propertyValue.isEmpty()) {
0071         uiGroup.deleteEntry(propertyName, KConfigGroup::Normal);
0072         return;
0073     }
0074     uiGroup.writeEntry(propertyName, propertyValue.trimmed());
0075 }
0076 
0077 /* User Interface [ui] Section */
0078 
0079 QString HgConfig::username() const
0080 {
0081     return property(QStringLiteral("ui"), QStringLiteral("username"));
0082 }
0083 
0084 void HgConfig::setUsername(const QString &userName)
0085 {
0086     setProperty(QStringLiteral("ui"), QStringLiteral("username"), userName);
0087 }
0088 
0089 QString HgConfig::editor() const 
0090 {
0091     return property(QStringLiteral("ui"), QStringLiteral("editor"));
0092 }
0093 
0094 void HgConfig::setEditor(const QString &pathToEditor)
0095 {
0096     setProperty(QStringLiteral("ui"), QStringLiteral("editor"), pathToEditor);
0097 }
0098 
0099 QString HgConfig::merge() const 
0100 {
0101     return property(QStringLiteral("ui"), QStringLiteral("merge"));
0102 }
0103 
0104 void HgConfig::setMerge(const QString &pathToMergeTool)
0105 {
0106     setProperty(QStringLiteral("ui"), QStringLiteral("merge"), pathToMergeTool);
0107 }
0108 
0109 /* Repo specific */
0110 
0111 void HgConfig::setRepoRemotePath(const QString &alias, const QString &url)
0112 {
0113     Q_ASSERT(m_configType == RepoConfig);
0114     setProperty(QStringLiteral("paths"), alias, url);
0115 }
0116 
0117 void HgConfig::deleteRepoRemotePath(const QString &alias)
0118 {
0119     Q_ASSERT(m_configType == RepoConfig);
0120 
0121     KConfigGroup group(m_config, QStringLiteral("paths"));
0122     group.deleteEntry(alias);
0123 }
0124 
0125 QString HgConfig::repoRemotePath(const QString &alias) const
0126 {
0127     Q_ASSERT(m_configType == RepoConfig);
0128     return property(QStringLiteral("paths"), alias);
0129 }
0130 
0131 QMap<QString, QString> HgConfig::repoRemotePathList() const
0132 {
0133     Q_ASSERT(m_configType == RepoConfig);
0134 
0135     KConfigGroup group(m_config, QStringLiteral("paths"));
0136     return group.entryMap();
0137 }
0138 
0139 //
0140