File indexing completed on 2022-09-27 14:08:49

0001 /* This file is part of KDevelop
0002    Copyright 2017 Anton Anikin <anton@anikin.xyz>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; see the file COPYING. If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "config.h"
0021 
0022 #include "debug.h"
0023 
0024 #include <qtcompat_p.h>
0025 
0026 #include <KShell>
0027 
0028 namespace Valgrind
0029 {
0030 
0031 class Config::CmdItem
0032 {
0033 public:
0034     CmdItem(ItemInt* item, const QString& cmdName)
0035         : CmdItem(static_cast<KConfigSkeletonItem*>(item), cmdName)
0036     {
0037         m_type = Int;
0038     }
0039 
0040     CmdItem(ItemBool* item, const QString& cmdName)
0041         : CmdItem(static_cast<KConfigSkeletonItem*>(item), cmdName)
0042     {
0043         m_type = Bool;
0044     }
0045 
0046     CmdItem(ItemDouble* item, const QString& cmdName)
0047         : CmdItem(static_cast<KConfigSkeletonItem*>(item), cmdName)
0048     {
0049         m_type = Double;
0050     }
0051 
0052     CmdItem(ItemString* item, const QString& cmdName)
0053         : CmdItem(static_cast<KConfigSkeletonItem*>(item), cmdName)
0054     {
0055         m_type = String;
0056     }
0057 
0058     ~CmdItem() = default;
0059 
0060     QString cmdArg()
0061     {
0062         QString value;
0063 
0064         switch (m_type) {
0065             case Int:
0066                 value = QString::number(m_item->property().toInt());
0067                 break;
0068 
0069             case Bool:
0070                 value = m_item->property().toBool() ? QStringLiteral("yes") : QStringLiteral("no");
0071                 break;
0072 
0073             case Double:
0074                 value = QString::number(m_item->property().toDouble(), 'f', 1);
0075                 break;
0076 
0077             case String:
0078                 value = m_item->property().toString();
0079                 break;
0080         }
0081 
0082         return QStringLiteral("--%1=%2").arg(m_cmdName, value);
0083     }
0084 
0085 private:
0086     CmdItem(KConfigSkeletonItem* item, const QString& cmdName)
0087         : m_item(item)
0088         , m_cmdName(cmdName)
0089     {
0090         Q_ASSERT(item);
0091         Q_ASSERT(!cmdName.isEmpty());
0092     }
0093 
0094     enum Type
0095     {
0096         Int,
0097         Bool,
0098         Double,
0099         String
0100     };
0101 
0102     KConfigSkeletonItem* m_item;
0103     QString m_cmdName;
0104     Type m_type;
0105 };
0106 
0107 Config::Config(const QString& group)
0108     : m_group(group)
0109 {
0110     setCurrentGroup(group);
0111 
0112     addItemString(QStringLiteral("extraArgs"), m_extraArgs);
0113 }
0114 
0115 Config::~Config()
0116 {
0117     qDeleteAll(m_cmdItems);
0118 }
0119 
0120 inline void addParent(QString& fullName, const KConfigGroup& parent)
0121 {
0122     fullName = parent.name() + QLatin1Char('\x1d') + fullName;
0123 }
0124 
0125 // TODO create patch for KCoreConfigSkeleton::setCurrentGroup(const KConfigGroup& group) overload ?
0126 void Config::setConfigGroup(const KConfigGroup& group)
0127 {
0128     setSharedConfig(KSharedConfig::openConfig(group.config()->name(), group.config()->openFlags()));
0129 
0130     QString fullName(m_group);
0131     addParent(fullName, group);
0132 
0133     KConfigGroup parentGroup = group.parent();
0134     while (parentGroup.isValid() && parentGroup.name() != QStringLiteral("<default>")) {
0135         addParent(fullName, parentGroup);
0136         parentGroup = parentGroup.parent();
0137     }
0138 
0139     setCurrentGroup(fullName);
0140 
0141     const auto allItems = items();
0142     for (auto item : allItems) {
0143         item->setGroup(fullName);
0144     }
0145 }
0146 
0147 QStringList Config::cmdArgs()
0148 {
0149     QStringList args;
0150     for (auto cmdItem : qAsConst(m_cmdItems)) {
0151         args += cmdItem->cmdArg();
0152     }
0153     args += KShell::splitArgs(m_extraArgs);
0154     args.removeAll(QStringLiteral(""));
0155 
0156     return args;
0157 }
0158 
0159 Config::ItemInt* Config::addCmdItemInt(const QString& name, int& reference, int defaultValue, const QString& cmdName)
0160 {
0161     auto item = addItemInt(name, reference, defaultValue);
0162     m_cmdItems += new CmdItem(item, cmdName);
0163     return item;
0164 }
0165 
0166 Config::ItemBool* Config::addCmdItemBool(const QString& name, bool& reference, bool defaultValue, const QString& cmdName)
0167 {
0168     auto item = addItemBool(name, reference, defaultValue);
0169     m_cmdItems += new CmdItem(item, cmdName);
0170     return item;
0171 }
0172 
0173 Config::ItemDouble* Config::addCmdItemDouble(const QString& name, double& reference, double defaultValue, const QString& cmdName)
0174 {
0175     auto item = addItemDouble(name, reference, defaultValue);
0176     m_cmdItems += new CmdItem(item, cmdName);
0177     return item;
0178 }
0179 
0180 Config::ItemString* Config::addCmdItemString(const QString& name, QString& reference, const QString& defaultValue, const QString& cmdName)
0181 {
0182     auto item = addItemString(name, reference, defaultValue);
0183     m_cmdItems += new CmdItem(item, cmdName);
0184     return item;
0185 }
0186 
0187 }