feather/src/utils/config.cpp

274 lines
10 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2022-02-10 10:26:41 +00:00
// SPDX-FileCopyrightText: 2011 Felix Geyer <debfx@fobos.de>
// SPDX-FileCopyrightText: 2020 KeePassXC Team <team@keepassxc.org>
2024-01-01 17:07:58 +00:00
// SPDX-FileCopyrightText: 2020-2024 The Monero Project
#include "config.h"
2024-10-01 20:17:54 +00:00
#include <QCoreApplication>
#include <QStandardPaths>
2021-07-06 19:36:27 +00:00
#include "utils/Utils.h"
2021-05-25 13:06:38 +00:00
#include "utils/os/tails.h"
#define QS QStringLiteral
struct ConfigDirective
{
QString name;
QVariant defaultValue;
};
static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
// General
2021-05-18 15:59:18 +00:00
{Config::firstRun, {QS("firstRun"), true}},
{Config::warnOnStagenet,{QS("warnOnStagenet"), true}},
{Config::warnOnTestnet,{QS("warnOnTestnet"), true}},
2023-12-02 18:28:31 +00:00
{Config::warnOnKiImport,{QS("warnOnKiImport"), true}},
{Config::logLevel,{QS("logLevel"), 0}},
2021-05-18 15:59:18 +00:00
{Config::homeWidget,{QS("homeWidget"), "ccs"}},
{Config::donateBeg,{QS("donateBeg"), 1}},
2021-05-18 15:59:18 +00:00
{Config::showHistorySyncNotice, {QS("showHistorySyncNotice"), true}},
{Config::geometry, {QS("geometry"), {}}},
{Config::windowState, {QS("windowState"), {}}},
{Config::GUI_HistoryViewState, {QS("GUI_HistoryViewState"), {}}},
2023-12-02 18:28:31 +00:00
{Config::geometryOTSWizard, {QS("geometryOTSWizard"), {}}},
2021-05-18 15:59:18 +00:00
// Wallets
{Config::walletDirectory,{QS("walletDirectory"), ""}},
{Config::autoOpenWalletPath,{QS("autoOpenWalletPath"), ""}},
2021-05-18 15:59:18 +00:00
{Config::recentlyOpenedWallets, {QS("recentlyOpenedWallets"), {}}},
// Nodes
{Config::nodes,{QS("nodes"), "{}"}},
{Config::nodeSource,{QS("nodeSource"), 0}},
{Config::useOnionNodes,{QS("useOnionNodes"), false}},
2021-05-18 15:59:18 +00:00
// Tabs
2023-11-30 14:01:39 +00:00
{Config::enabledTabs, {QS("enabledTabs"), QStringList{"Home", "History", "Send", "Receive", "Calc"}}},
2021-05-23 14:58:18 +00:00
{Config::showSearchbar,{QS("showSearchbar"), true}},
2021-05-18 15:59:18 +00:00
2023-12-30 15:46:14 +00:00
// History
{Config::historyShowFullTxid, {QS("historyShowFullTxid"), false}},
2023-11-16 15:26:58 +00:00
// Receive
{Config::showUsedAddresses,{QS("showUsedAddresses"), false}},
{Config::showHiddenAddresses,{QS("showHiddenAddresses"), false}},
{Config::showFullAddresses, {QS("showFullAddresses"), false}},
{Config::showChangeAddresses,{QS("showChangeAddresses"), false}},
{Config::showAddressIndex,{QS("showAddressIndex"), true}},
{Config::showAddressLabels,{QS("showAddressLabels"), true}},
2021-05-18 15:59:18 +00:00
// Mining
2021-10-12 12:45:06 +00:00
{Config::miningMode,{QS("miningMode"), Config::MiningMode::Pool}},
2021-05-18 15:59:18 +00:00
{Config::xmrigPath,{QS("xmrigPath"), ""}},
2021-10-12 12:45:06 +00:00
{Config::xmrigElevated,{QS("xmrigElevated"), false}},
{Config::xmrigThreads,{QS("xmrigThreads"), 1}},
2021-05-18 15:59:18 +00:00
{Config::xmrigPool,{QS("xmrigPool"), "pool.xmr.pt:9000"}},
{Config::xmrigDaemon,{QS("xmrigDaemon"), "127.0.0.1:18081"}},
2021-10-12 12:45:06 +00:00
{Config::xmrigNetworkTLS,{QS("xmrigNetworkTLS"), true}},
{Config::xmrigNetworkTor,{QS("xmrigNetworkTor"), false}},
2021-07-07 17:11:12 +00:00
{Config::pools,{QS("pools"), {}}},
2021-05-18 15:59:18 +00:00
// Settings
2022-03-11 13:56:07 +00:00
{Config::lastSettingsPage, {QS("lastSettingsPage"), 0}},
2021-05-18 15:59:18 +00:00
{Config::preferredFiatCurrency,{QS("preferredFiatCurrency"), "USD"}},
{Config::skin,{QS("skin"), "light"}},
{Config::amountPrecision, {QS("amountPrecision"), 12}},
2021-03-14 21:12:02 +00:00
{Config::dateFormat, {QS("dateFormat"), "yyyy-MM-dd"}},
2021-03-16 03:17:01 +00:00
{Config::timeFormat, {QS("timeFormat"), "HH:mm"}},
2021-05-22 14:12:39 +00:00
{Config::balanceDisplay, {QS("balanceDisplay"), Config::BalanceDisplay::spendablePlusUnconfirmed}},
2022-03-04 16:20:17 +00:00
{Config::inactivityLockEnabled, {QS("inactivityLockEnabled"), false}},
{Config::inactivityLockTimeout, {QS("inactivityLockTimeout"), 10}},
2023-02-11 17:11:21 +00:00
{Config::lockOnMinimize, {QS("lockOnMinimize"), false}},
2022-03-04 21:55:29 +00:00
{Config::disableWebsocket, {QS("disableWebsocket"), false}},
2022-03-14 21:36:58 +00:00
{Config::offlineMode, {QS("offlineMode"), false}},
2021-05-18 15:59:18 +00:00
// Transactions
2021-05-02 18:22:38 +00:00
{Config::multiBroadcast, {QS("multiBroadcast"), true}},
2023-12-02 18:28:31 +00:00
{Config::offlineTxSigningMethod, {QS("offlineTxSigningMethod"), Config::OTSMethod::UnifiedResources}},
{Config::offlineTxSigningForceKISync, {QS("offlineTxSigningForceKISync"), false}},
{Config::manualFeeTierSelection, {QS("manualFeeTierSelection"), false}},
{Config::subtractFeeFromAmount, {QS("subtractFeeFromAmount"), false}},
2021-05-18 15:59:18 +00:00
{Config::warnOnExternalLink,{QS("warnOnExternalLink"), true}},
{Config::hideBalance, {QS("hideBalance"), false}},
2023-02-11 17:11:21 +00:00
{Config::hideNotifications, {QS("hideNotifications"), false}},
{Config::hideUpdateNotifications, {QS("hideUpdateNotifications"), false}},
{Config::disableLogging, {QS("disableLogging"), true}},
{Config::writeStackTraceToDisk, {QS("writeStackTraceToDisk"), true}},
{Config::writeRecentlyOpenedWallets, {QS("writeRecentlyOpenedWallets"), true}},
2021-05-18 15:59:18 +00:00
{Config::blockExplorers, {QS("blockExplorers"), QStringList{"https://xmrchain.net/tx/%txid%",
"https://melo.tools/explorer/mainnet/tx/%txid%",
"https://moneroblocks.info/tx/%txid%",
"https://blockchair.com/monero/transaction/%txid%",
"http://blkchairbknpn73cfjhevhla7rkp4ed5gg2knctvv7it4lioy22defid.onion/monero/transaction/%txid%",
"http://127.0.0.1:31312/tx?id=%txid%"}}},
{Config::blockExplorer,{QS("blockExplorer"), "https://xmrchain.net/tx/%txid%"}},
2022-06-23 17:48:30 +00:00
{Config::bountiesFrontend, {QS("bountiesFrontend"), "https://bounties.monero.social"}},
2023-12-02 18:28:31 +00:00
{Config::lastPath, {QS("lastPath"), QDir::homePath()}},
{Config::URmsPerFragment, {QS("URmsPerFragment"), 80}},
{Config::URfragmentLength, {QS("URfragmentLength"), 150}},
{Config::URfountainCode, {QS("URfountainCode"), false}},
{Config::cameraManualExposure, {QS("cameraManualExposure"), false}},
{Config::cameraExposureTime, {QS("cameraExposureTime"), 10}},
2021-05-18 15:59:18 +00:00
2021-05-26 13:14:53 +00:00
{Config::fiatSymbols, {QS("fiatSymbols"), QStringList{"USD", "EUR", "GBP", "CAD", "AUD", "RUB"}}},
{Config::cryptoSymbols, {QS("cryptoSymbols"), QStringList{"BTC", "ETH", "LTC", "XMR", "ZEC"}}},
2021-05-18 15:59:18 +00:00
// Tor
2023-02-11 17:11:21 +00:00
{Config::proxy, {QS("proxy"), Config::Proxy::Tor}},
2021-05-02 18:22:38 +00:00
{Config::torPrivacyLevel, {QS("torPrivacyLevel"), 1}},
2023-02-11 17:11:21 +00:00
{Config::torOnlyAllowOnion, {QS("torOnlyAllowOnion"), false}},
2021-05-02 18:22:38 +00:00
{Config::socks5Host, {QS("socks5Host"), "127.0.0.1"}},
{Config::socks5Port, {QS("socks5Port"), "9050"}},
2021-05-18 15:59:18 +00:00
{Config::socks5User, {QS("socks5User"), ""}}, // Unused
{Config::socks5Pass, {QS("socks5Pass"), ""}}, // Unused
2023-02-11 17:11:21 +00:00
{Config::torManagedPort, {QS("torManagedPort"), "19450"}},
2021-05-24 15:20:19 +00:00
{Config::useLocalTor, {QS("useLocalTor"), false}},
2023-11-30 14:01:39 +00:00
{Config::initSyncThreshold, {QS("initSyncThreshold"), 360}},
2024-05-07 19:45:57 +00:00
{Config::enabledPlugins, {QS("enabledPlugins"), QStringList{"tickers", "crowdfunding", "bounties", "revuo", "calc", "xmrig"}}},
2023-11-30 14:01:39 +00:00
{Config::restartRequired, {QS("restartRequired"), false}},
{Config::tickers, {QS("tickers"), QStringList{"XMR", "BTC", "XMR/BTC"}}},
{Config::tickersShowFiatBalance, {QS("tickersShowFiatBalance"), true}},
};
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);
}
void Config::remove(ConfigKey key)
{
auto cfg = configStrings[key];
m_settings->remove(cfg.name);
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)
2021-03-12 18:26:48 +00:00
: QObject(parent)
{
2021-03-12 18:26:48 +00:00
QDir configDir = Config::defaultConfigDir();
2021-03-24 01:37:54 +00:00
if (!QDir().mkpath(configDir.path())) {
qWarning() << "Unable to create config path: " << configDir.path();
}
2021-03-12 18:26:48 +00:00
QString configPath = configDir.filePath("settings.json");
init(QDir::toNativeSeparators(configPath));
}
2021-03-12 18:26:48 +00:00
QDir Config::defaultConfigDir() {
2022-06-22 19:06:32 +00:00
if (Utils::isPortableMode()) {
return Utils::portablePath();
2021-03-24 01:37:54 +00:00
}
if (TailsOS::detect()) {
QString path = []{
QString appImagePath = qgetenv("APPIMAGE");
if (appImagePath.isEmpty()) {
qDebug() << "Not an appimage, using currentPath()";
if (QDir(QDir::currentPath() + "/.feather").exists()) {
return QDir::currentPath() + "/.feather/.config/feather";
}
return QDir::currentPath() + "/feather_data";
2021-03-24 01:37:54 +00:00
}
QFileInfo appImageDir(appImagePath);
QString absolutePath = appImageDir.absoluteDir().path();
if (QDir(absolutePath + "/.feather").exists()) {
return absolutePath + "/.feather/.config/feather";
}
return absolutePath + "/feather_data";
2021-03-24 01:37:54 +00:00
}();
return QDir(path);
}
2021-03-12 18:26:48 +00:00
#if defined(Q_OS_WIN)
return QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
#elif defined(Q_OS_MACOS)
return QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
#else
return QDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/feather");
#endif
}
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(configFileName, jsonFormat));
connect(qApp, &QCoreApplication::aboutToQuit, this, &Config::sync);
}
Config* Config::instance()
{
if (!m_instance) {
m_instance = new Config(qApp);
}
return m_instance;
}