From fa1b478ddb7eccea2ae33246212a36ec503cdbca Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 26 Aug 2020 12:55:29 +0200 Subject: [PATCH] qt: Pass argsmanager reference to intro instead of relying on global --- src/qt/bitcoin.cpp | 4 +++- src/qt/intro.cpp | 41 ++++++++++++++++++++++------------------- src/qt/intro.h | 6 +++--- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index e63ffdfb36f..c5a2a7f8cbc 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -469,6 +470,7 @@ int GuiMain(int argc, char* argv[]) /// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these // Command-line options take precedence: SetupServerArgs(node_context); + ArgsManager& args = *Assert(node_context.args); SetupUIArgs(gArgs); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { @@ -508,7 +510,7 @@ int GuiMain(int argc, char* argv[]) bool did_show_intro = false; bool prune = false; // Intro dialog prune check box // Gracefully exit if the user cancels - if (!Intro::showIfNeeded(did_show_intro, prune)) return EXIT_SUCCESS; + if (!Intro::showIfNeeded(args, did_show_intro, prune)) return EXIT_SUCCESS; /// 6. Determine availability of data directory and parse bitcoin.conf /// - Do not call GetDataDir(true) before this step finishes diff --git a/src/qt/intro.cpp b/src/qt/intro.cpp index 235722d091a..876f4fcb182 100644 --- a/src/qt/intro.cpp +++ b/src/qt/intro.cpp @@ -110,22 +110,22 @@ void FreespaceChecker::check() namespace { //! Return pruning size that will be used if automatic pruning is enabled. -int GetPruneTargetGB() +int GetPruneTargetGB(const ArgsManager& args) { - int64_t prune_target_mib = gArgs.GetArg("-prune", 0); + int64_t prune_target_mib = args.GetArg("-prune", 0); // >1 means automatic pruning is enabled by config, 1 means manual pruning, 0 means no pruning. return prune_target_mib > 1 ? PruneMiBtoGB(prune_target_mib) : DEFAULT_PRUNE_TARGET_GB; } } // namespace -Intro::Intro(QWidget *parent, int64_t blockchain_size_gb, int64_t chain_state_size_gb) : - QDialog(parent), - ui(new Ui::Intro), - thread(nullptr), - signalled(false), - m_blockchain_size_gb(blockchain_size_gb), - m_chain_state_size_gb(chain_state_size_gb), - m_prune_target_gb{GetPruneTargetGB()} +Intro::Intro(const ArgsManager& args, QWidget* parent, int64_t blockchain_size_gb, int64_t chain_state_size_gb) + : QDialog(parent), + ui(new Ui::Intro), + thread(nullptr), + signalled(false), + m_blockchain_size_gb(blockchain_size_gb), + m_chain_state_size_gb(chain_state_size_gb), + m_prune_target_gb{GetPruneTargetGB(args)} { ui->setupUi(this); ui->welcomeLabel->setText(ui->welcomeLabel->text().arg(PACKAGE_NAME)); @@ -139,7 +139,7 @@ Intro::Intro(QWidget *parent, int64_t blockchain_size_gb, int64_t chain_state_si ); ui->lblExplanation2->setText(ui->lblExplanation2->text().arg(PACKAGE_NAME)); - if (gArgs.GetArg("-prune", 0) > 1) { // -prune=1 means enabled, above that it's a size in MiB + if (args.GetArg("-prune", 0) > 1) { // -prune=1 means enabled, above that it's a size in MiB ui->prune->setChecked(true); ui->prune->setEnabled(false); } @@ -182,31 +182,34 @@ void Intro::setDataDirectory(const QString &dataDir) } } -bool Intro::showIfNeeded(bool& did_show_intro, bool& prune) +bool Intro::showIfNeeded(ArgsManager& args, bool& did_show_intro, bool& prune) { did_show_intro = false; QSettings settings; /* If data directory provided on command line, no need to look at settings or show a picking dialog */ - if(!gArgs.GetArg("-datadir", "").empty()) + if (!args.GetArg("-datadir", "").empty()) { return true; + } /* 1) Default data directory for operating system */ QString dataDir = GUIUtil::getDefaultDataDirectory(); /* 2) Allow QSettings to override default dir */ dataDir = settings.value("strDataDir", dataDir).toString(); - if(!fs::exists(GUIUtil::qstringToBoostPath(dataDir)) || gArgs.GetBoolArg("-choosedatadir", DEFAULT_CHOOSE_DATADIR) || settings.value("fReset", false).toBool() || gArgs.GetBoolArg("-resetguisettings", false)) - { + if (!fs::exists(GUIUtil::qstringToBoostPath(dataDir)) || + args.GetBoolArg("-choosedatadir", DEFAULT_CHOOSE_DATADIR) || + settings.value("fReset", false).toBool() || + args.GetBoolArg("-resetguisettings", false)) { /* Use selectParams here to guarantee Params() can be used by node interface */ try { - SelectParams(gArgs.GetChainName()); + SelectParams(args.GetChainName()); } catch (const std::exception&) { return false; } /* If current default data directory does not exist, let the user choose one */ - Intro intro(0, Params().AssumedBlockchainSize(), Params().AssumedChainStateSize()); + Intro intro(args, nullptr, Params().AssumedBlockchainSize(), Params().AssumedChainStateSize()); intro.setDataDirectory(dataDir); intro.setWindowIcon(QIcon(":icons/bitcoin")); did_show_intro = true; @@ -242,8 +245,8 @@ bool Intro::showIfNeeded(bool& did_show_intro, bool& prune) * override -datadir in the bitcoin.conf file in the default data directory * (to be consistent with bitcoind behavior) */ - if(dataDir != GUIUtil::getDefaultDataDirectory()) { - gArgs.SoftSetArg("-datadir", GUIUtil::qstringToBoostPath(dataDir).string()); // use OS locale for path setting + if (dataDir != GUIUtil::getDefaultDataDirectory()) { + args.SoftSetArg("-datadir", GUIUtil::qstringToBoostPath(dataDir).string()); // use OS locale for path setting } return true; } diff --git a/src/qt/intro.h b/src/qt/intro.h index 51f42de7ac1..11e9f666835 100644 --- a/src/qt/intro.h +++ b/src/qt/intro.h @@ -11,6 +11,7 @@ static const bool DEFAULT_CHOOSE_DATADIR = false; +class ArgsManager; class FreespaceChecker; namespace interfaces { @@ -30,8 +31,7 @@ class Intro : public QDialog Q_OBJECT public: - explicit Intro(QWidget *parent = nullptr, - int64_t blockchain_size_gb = 0, int64_t chain_state_size_gb = 0); + explicit Intro(const ArgsManager& args, QWidget* parent = nullptr, int64_t blockchain_size_gb = 0, int64_t chain_state_size_gb = 0); ~Intro(); QString getDataDirectory(); @@ -47,7 +47,7 @@ class Intro : public QDialog * @note do NOT call global GetDataDir() before calling this function, this * will cause the wrong path to be cached. */ - static bool showIfNeeded(bool& did_show_intro, bool& prune); + static bool showIfNeeded(ArgsManager& args, bool& did_show_intro, bool& prune); Q_SIGNALS: void requestCheck();