aboutsummaryrefslogtreecommitdiff
path: root/src/settings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/settings.cpp')
-rw-r--r--src/settings.cpp182
1 files changed, 0 insertions, 182 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
deleted file mode 100644
index 7f867f7..0000000
--- a/src/settings.cpp
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#include "settings.h"
-#include <QStandardPaths>
-#include <fstream>
-#include <cstdio>
-#include <QDir>
-#include <QFile>
-#include <QFileInfo>
-#include <QFileSystemWatcher>
-
-Settings::Settings(const QString &configFile, const QString &defaultsFile)
-{
- m_configurationPath = configFile;
- m_defaultsPath = defaultsFile;
-
- // homeLocation is the user's home folder
- homeLocation = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
-
- // settingsLocation is the location of the configFile
- if(QFile::exists(configFile)) {
- settingsLocation = QFileInfo(configFile).dir().absolutePath();
- } else {
- // if file doesn't exist, use the generic location
- settingsLocation = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
- }
-
- // cacheLocation
- cacheLocation = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
-
- userValues = parse(m_configurationPath);
- defaultValues = parse(m_defaultsPath);
-
- m_watcher = new QFileSystemWatcher();
- m_watcher->addPath(configFile);
- QObject::connect(m_watcher, &QFileSystemWatcher::fileChanged, [this](const QString &path) {
- if(path == m_configurationPath) {
-#ifdef QT_DEBUG
- qDebug("Reloading user configuration");
-#endif
- userValues = parse(m_configurationPath);
- }
- });
-
-#ifdef QT_DEBUG
- qDebug(">> Configuration");
- qDebug("- userconf: [%s]", qUtf8Printable(m_configurationPath));
- qDebug("- defaults: [%s]", qUtf8Printable(m_defaultsPath));
- qDebug("- $home [%s]", qUtf8Printable(homeLocation));
- qDebug("- $settings [%s]", qUtf8Printable(settingsLocation));
- qDebug("- $cache [%s]", qUtf8Printable(cacheLocation));
-#endif
-}
-
-Settings::~Settings()
-{
- m_watcher->deleteLater();
-}
-
-QString Settings::configurationPath() const
-{
- return m_configurationPath;
-}
-
-QString Settings::defaultsPath() const
-{
- return m_defaultsPath;
-}
-
-bool Settings::isEmpty() const
-{
- return userValues.empty();
-}
-
-QVariant Settings::value(const QString &key) const
-{
- const toml::Value *cValue = userValues.find(key.toStdString());
- const toml::Value *dValue = defaultValues.find(key.toStdString());
-
- QVariant r;
-
- if(userValues.has(key.toStdString())) {
- r = valueToVariant(cValue);
- } else {
- if(defaultValues.has(key.toStdString())) {
- r = valueToVariant(dValue);
- }
- }
-
- // check if key is a path, in which case replace '~' with the home location
- if(key.endsWith(QLatin1String("path"), Qt::CaseInsensitive)) {
- QString value = r.toString();
- while(value.contains('$')) {
- value.replace("$settings", settingsLocation);
- value.replace("$cache", cacheLocation);
- value.replace("$home", homeLocation);
- }
- r = QVariant(value);
- }
-
- return r;
-}
-
-toml::Value Settings::parse(const QString &filename)
-{
- toml::Value r;
-
- if(filename.isEmpty()) {
- qWarning("Empty configuration path.");
-
- } else {
- QFile file(filename);
- if(file.open(QIODevice::ReadOnly)) {
- std::stringstream d(file.readAll().toStdString());
- file.close();
-
- toml::ParseResult result = toml::parse(d);
- if(!result.valid()) {
- qWarning("Invalid configuration: %s", result.errorReason.c_str());
- }
- r = result.value;
-
- } else {
- qWarning("Cannot open configuration: %s", qUtf8Printable(filename));
- }
- }
-
- return r;
-}
-
-QVariant Settings::fromList(const toml::Value *list) const
-{
- QStringList l;
-
- for(const toml::Value &v : list->as<toml::Array>()) {
- // TODO check value type
- l.append(QString::fromStdString(v.as<std::string>()));
- }
-
- return QVariant(l);
-}
-
-QVariant Settings::valueToVariant(const toml::Value *value) const
-{
- QVariant r;
- switch (value->type()) {
- case toml::Value::NULL_TYPE:
- break;
-
- case toml::Value::BOOL_TYPE:
- r = QVariant(value->as<bool>());
- break;
-
- case toml::Value::INT_TYPE:
- r = QVariant(value->as<int>());
- break;
-
- case toml::Value::DOUBLE_TYPE:
- r = QVariant(value->as<double>());
- break;
-
- case toml::Value::STRING_TYPE:
- r = QVariant(QString::fromStdString(value->as<std::string>()));
- break;
-
- case toml::Value::ARRAY_TYPE:
- r = fromList(value);
- break;
-
- default:
- qWarning("Unhandled type in configuration");
- break;
- }
-
- return r;
-}