File indexing completed on 2024-11-10 04:40:07

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "config.h" //krazy:exclude=includes
0008 #include "akonaditest_debug.h"
0009 
0010 #include <QDir>
0011 #include <QFile>
0012 #include <QFileInfo>
0013 #include <QPair>
0014 #include <QXmlStreamReader>
0015 
0016 Q_GLOBAL_STATIC(Config, globalConfig) // NOLINT(readability-redundant-member-init)
0017 
0018 Config::Config()
0019 {
0020 }
0021 
0022 Config::~Config()
0023 {
0024 }
0025 
0026 Config *Config::instance(const QString &pathToConfig)
0027 {
0028     globalConfig()->readConfiguration(pathToConfig);
0029     return globalConfig();
0030 }
0031 
0032 Config *Config::instance()
0033 {
0034     return globalConfig();
0035 }
0036 
0037 void Config::readConfiguration(const QString &configfile)
0038 {
0039     QFile file(configfile);
0040 
0041     if (!file.open(QIODevice::ReadOnly)) {
0042         qFatal("Error reading file %s: %s", qPrintable(configfile), qUtf8Printable(file.errorString()));
0043     }
0044 
0045     mBasePath = QFileInfo(configfile).absolutePath() + QLatin1Char('/');
0046     qCDebug(AKONADITEST_LOG) << "Base path" << mBasePath;
0047     QXmlStreamReader reader(&file);
0048 
0049     while (!reader.atEnd()) {
0050         reader.readNext();
0051         if (reader.name() == QLatin1StringView("config")) {
0052             while (!reader.atEnd() && !(reader.name() == QLatin1StringView("config") && reader.isEndElement())) {
0053                 reader.readNext();
0054                 if (reader.name() == QLatin1StringView("backends")) {
0055                     QStringList backends;
0056                     while (!reader.atEnd() && !(reader.name() == QLatin1StringView("backends") && reader.isEndElement())) {
0057                         reader.readNext();
0058                         if (reader.name() == QLatin1StringView("backend")) {
0059                             backends << reader.readElementText();
0060                         }
0061                     }
0062                     setBackends(backends);
0063                 } else if (reader.name() == QLatin1StringView("datahome")) {
0064                     setXdgDataHome(mBasePath + reader.readElementText());
0065                 } else if (reader.name() == QLatin1StringView("agent")) {
0066                     const auto attrs = reader.attributes();
0067                     insertAgent(reader.readElementText(), attrs.value(QLatin1StringView("synchronize")) == QLatin1StringView("true"));
0068                 } else if (reader.name() == QLatin1StringView("envvar")) {
0069                     const auto attrs = reader.attributes();
0070                     const auto name = attrs.value(QLatin1StringView("name"));
0071                     if (name.isEmpty()) {
0072                         qCWarning(AKONADITEST_LOG) << "Given envvar with no name.";
0073                     } else {
0074                         mEnvVars[name.toString()] = reader.readElementText();
0075                     }
0076                 } else if (reader.name() == QLatin1StringView("dbbackend")) {
0077                     setDbBackend(reader.readElementText());
0078                 }
0079             }
0080         }
0081     }
0082 }
0083 
0084 QString Config::xdgDataHome() const
0085 {
0086     return mXdgDataHome;
0087 }
0088 
0089 QString Config::xdgConfigHome() const
0090 {
0091     return mXdgConfigHome;
0092 }
0093 
0094 QString Config::basePath() const
0095 {
0096     return mBasePath;
0097 }
0098 
0099 QStringList Config::backends() const
0100 {
0101     return mBackends;
0102 }
0103 
0104 QString Config::dbBackend() const
0105 {
0106     return mDbBackend;
0107 }
0108 
0109 void Config::setXdgDataHome(const QString &dataHome)
0110 {
0111     const QDir dataHomeDir(dataHome);
0112     mXdgDataHome = dataHomeDir.absolutePath();
0113 }
0114 
0115 void Config::setXdgConfigHome(const QString &configHome)
0116 {
0117     const QDir configHomeDir(configHome);
0118     mXdgConfigHome = configHomeDir.absolutePath();
0119 }
0120 
0121 void Config::setBackends(const QStringList &backends)
0122 {
0123     mBackends = backends;
0124 }
0125 
0126 bool Config::setDbBackend(const QString &backend)
0127 {
0128     if (mBackends.isEmpty() || mBackends.contains(backend)) {
0129         mDbBackend = backend;
0130         return true;
0131     } else {
0132         return false;
0133     }
0134 }
0135 
0136 void Config::insertAgent(const QString &agent, bool sync)
0137 {
0138     mAgents.append(qMakePair(agent, sync));
0139 }
0140 
0141 QList<QPair<QString, bool>> Config::agents() const
0142 {
0143     return mAgents;
0144 }
0145 
0146 QHash<QString, QString> Config::envVars() const
0147 {
0148     return mEnvVars;
0149 }