You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.8 KiB
118 lines
2.8 KiB
// SPDX-License-Identifier: BSD-3-Clause
|
|
// Copyright (c) 2020-2021, The Monero Project.
|
|
// Copyright (c) 2020 KeePassXC Team <team@keepassxc.org>
|
|
// Copyright (c) 2011 Felix Geyer <debfx@fobos.de>
|
|
|
|
#include "Config.h"
|
|
|
|
#include <QDir>
|
|
#include <QCoreApplication>
|
|
|
|
#define QS QStringLiteral
|
|
|
|
struct ConfigDirective
|
|
{
|
|
QString name;
|
|
QVariant defaultValue;
|
|
};
|
|
|
|
static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
|
|
{Config::network,{QS("network"), "mainnet"}},
|
|
{Config::pathToMonerod,{QS("pathToMonerod"), ""}},
|
|
{Config::pruneBlockchain,{QS("pruneBlockchain"), true}},
|
|
{Config::dataDir,{QS("dataDir"), ""}},
|
|
{Config::logLevel,{QS("logLevel"), 0}},
|
|
{Config::limitRateDown,{QS("limitRateDown"), 0}},
|
|
{Config::customStartupFlags,{QS("customStartupFlags"), ""}},
|
|
{Config::noZeroMQ,{QS("noZeroMQ"), true}},
|
|
{Config::RPCBindIP,{QS("RPCBindIP"), ""}},
|
|
{Config::RPCBindPort,{QS("RPCBindPort"), 0}},
|
|
{Config::P2PBindIP,{QS("P2PBindIP"), ""}},
|
|
{Config::P2PBindPort,{QS("P2PBindPort"), 0}},
|
|
};
|
|
|
|
|
|
QPointer<Config> Config::m_instance(nullptr);
|
|
|
|
QVariant Config::get(ConfigKey key)
|
|
{
|
|
auto cfg = configStrings[key];
|
|
auto defaultValue = configStrings[key].defaultValue;
|
|
|
|
return m_settings->value(cfg.name, defaultValue);
|
|
}
|
|
|
|
QString Config::getFileName()
|
|
{
|
|
return m_settings->fileName();
|
|
}
|
|
|
|
void Config::set(ConfigKey key, const QVariant& value)
|
|
{
|
|
if (get(key) == value) {
|
|
return;
|
|
}
|
|
|
|
auto cfg = configStrings[key];
|
|
m_settings->setValue(cfg.name, value);
|
|
|
|
this->sync();
|
|
emit changed(key);
|
|
}
|
|
|
|
/**
|
|
* Sync configuration with persistent storage.
|
|
*
|
|
* Usually, you don't need to call this method manually, but if you are writing
|
|
* configurations after an emitted \link QCoreApplication::aboutToQuit() signal,
|
|
* use it to guarantee your config values are persisted.
|
|
*/
|
|
void Config::sync()
|
|
{
|
|
m_settings->sync();
|
|
}
|
|
|
|
void Config::resetToDefaults()
|
|
{
|
|
m_settings->clear();
|
|
}
|
|
|
|
Config::Config(const QString& fileName, QObject* parent)
|
|
: QObject(parent)
|
|
{
|
|
init(fileName);
|
|
}
|
|
|
|
Config::Config(QObject* parent)
|
|
: QObject(parent)
|
|
{
|
|
QString configPath;
|
|
|
|
configPath = QDir::homePath();
|
|
configPath += "/.config/moneroservice/settings.json";
|
|
|
|
init(QDir::toNativeSeparators(configPath));
|
|
}
|
|
|
|
|
|
Config::~Config()
|
|
{
|
|
}
|
|
|
|
void Config::init(const QString& configFileName)
|
|
{
|
|
// const QSettings::Format jsonFormat = QSettings::registerFormat("json", Utils::readJsonFile, Utils::writeJsonFile);
|
|
// QSettings::setDefaultFormat(jsonFormat);
|
|
m_settings.reset(new QSettings());
|
|
|
|
connect(qApp, &QCoreApplication::aboutToQuit, this, &Config::sync);
|
|
}
|
|
|
|
Config* Config::instance()
|
|
{
|
|
if (!m_instance) {
|
|
m_instance = new Config(qApp);
|
|
}
|
|
|
|
return m_instance;
|
|
} |