diff --git a/src/interface/node.h b/src/interface/node.h --- a/src/interface/node.h +++ b/src/interface/node.h @@ -5,12 +5,15 @@ #ifndef BITCOIN_INTERFACE_NODE_H #define BITCOIN_INTERFACE_NODE_H +#include // For Network + #include #include #include class Config; class HTTPRPCRequestProcessor; +class proxyType; class RPCServer; namespace interface { @@ -25,6 +28,13 @@ //! Set command line arguments. virtual void parseParameters(int argc, const char *const argv[]) = 0; + //! Set a command line argument if it doesn't already have a value + virtual bool softSetArg(const std::string &arg, + const std::string &value) = 0; + + //! Set a command line boolean argument if it doesn't already have a value + virtual bool softSetBoolArg(const std::string &arg, bool value) = 0; + //! Load settings from configuration file. virtual void readConfigFile(const std::string &conf_path) = 0; @@ -54,6 +64,12 @@ //! Start shutdown. virtual void startShutdown() = 0; + //! Map port. + virtual void mapPort(bool use_upnp) = 0; + + //! Get proxy. + virtual bool getProxy(Network net, proxyType &proxy_info) = 0; + //! Register handler for init messages. using InitMessageFn = std::function; virtual std::unique_ptr handleInitMessage(InitMessageFn fn) = 0; diff --git a/src/interface/node.cpp b/src/interface/node.cpp --- a/src/interface/node.cpp +++ b/src/interface/node.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -27,6 +30,13 @@ void readConfigFile(const std::string &conf_path) override { gArgs.ReadConfigFile(conf_path); } + bool softSetArg(const std::string &arg, + const std::string &value) override { + return gArgs.SoftSetArg(arg, value); + } + bool softSetBoolArg(const std::string &arg, bool value) override { + return gArgs.SoftSetBoolArg(arg, value); + } void selectParams(const std::string &network) override { SelectParams(network); } @@ -50,6 +60,17 @@ Shutdown(); } void startShutdown() override { StartShutdown(); } + void mapPort(bool use_upnp) override { + if (use_upnp) { + StartMapPort(); + } else { + InterruptMapPort(); + StopMapPort(); + } + } + bool getProxy(Network net, proxyType &proxy_info) override { + return GetProxy(net, proxy_info); + } std::unique_ptr handleInitMessage(InitMessageFn fn) override { return MakeHandler(::uiInterface.InitMessage.connect(fn)); } diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -345,7 +345,7 @@ #endif void BitcoinApplication::createOptionsModel(bool resetSettings) { - optionsModel = new OptionsModel(nullptr, resetSettings); + optionsModel = new OptionsModel(m_node, nullptr, resetSettings); } void BitcoinApplication::createWindow(const Config *config, diff --git a/src/qt/optionsmodel.h b/src/qt/optionsmodel.h --- a/src/qt/optionsmodel.h +++ b/src/qt/optionsmodel.h @@ -9,6 +9,10 @@ #include +namespace interface { +class Node; +} + QT_BEGIN_NAMESPACE class QNetworkProxy; QT_END_NAMESPACE @@ -26,7 +30,8 @@ Q_OBJECT public: - explicit OptionsModel(QObject *parent = 0, bool resetSettings = false); + explicit OptionsModel(interface::Node &node, QObject *parent = 0, + bool resetSettings = false); enum OptionID { StartAtStartup, // bool @@ -80,6 +85,7 @@ bool isRestartRequired() const; private: + interface::Node &m_node; /* Qt-only settings */ bool fHideTrayIcon; bool fMinimizeToTray; diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -12,26 +12,22 @@ #include "guiutil.h" #include "amount.h" -#include "init.h" +#include "interface/node.h" #include "intro.h" #include "net.h" #include "netbase.h" #include "txdb.h" // for -dbcache defaults #include "validation.h" // For DEFAULT_SCRIPTCHECK_THREADS -#ifdef ENABLE_WALLET -#include "wallet/wallet.h" -#include "wallet/walletdb.h" -#endif - #include #include #include const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1"; -OptionsModel::OptionsModel(QObject *parent, bool resetSettings) - : QAbstractListModel(parent) { +OptionsModel::OptionsModel(interface::Node &node, QObject *parent, + bool resetSettings) + : QAbstractListModel(parent), m_node(node) { Init(resetSettings); } @@ -104,7 +100,7 @@ if (!settings.contains("nDatabaseCache")) { settings.setValue("nDatabaseCache", (qint64)nDefaultDbCache); } - if (!gArgs.SoftSetArg( + if (!m_node.softSetArg( "-dbcache", settings.value("nDatabaseCache").toString().toStdString())) { addOverriddenOption("-dbcache"); @@ -113,7 +109,7 @@ if (!settings.contains("nThreadsScriptVerif")) { settings.setValue("nThreadsScriptVerif", DEFAULT_SCRIPTCHECK_THREADS); } - if (!gArgs.SoftSetArg( + if (!m_node.softSetArg( "-par", settings.value("nThreadsScriptVerif").toString().toStdString())) { addOverriddenOption("-par"); @@ -128,7 +124,7 @@ if (!settings.contains("bSpendZeroConfChange")) { settings.setValue("bSpendZeroConfChange", true); } - if (!gArgs.SoftSetBoolArg( + if (!m_node.softSetBoolArg( "-spendzeroconfchange", settings.value("bSpendZeroConfChange").toBool())) { addOverriddenOption("-spendzeroconfchange"); @@ -139,14 +135,14 @@ if (!settings.contains("fUseUPnP")) { settings.setValue("fUseUPnP", DEFAULT_UPNP); } - if (!gArgs.SoftSetBoolArg("-upnp", settings.value("fUseUPnP").toBool())) { + if (!m_node.softSetBoolArg("-upnp", settings.value("fUseUPnP").toBool())) { addOverriddenOption("-upnp"); } if (!settings.contains("fListen")) { settings.setValue("fListen", DEFAULT_LISTEN); } - if (!gArgs.SoftSetBoolArg("-listen", settings.value("fListen").toBool())) { + if (!m_node.softSetBoolArg("-listen", settings.value("fListen").toBool())) { addOverriddenOption("-listen"); } @@ -160,7 +156,7 @@ } // Only try to set -proxy, if user has enabled fUseProxy if (settings.value("fUseProxy").toBool() && - !gArgs.SoftSetArg( + !m_node.softSetArg( "-proxy", settings.value("addrProxy").toString().toStdString())) { addOverriddenOption("-proxy"); } else if (!settings.value("fUseProxy").toBool() && @@ -178,7 +174,7 @@ } // Only try to set -onion, if user has enabled fUseSeparateProxyTor if (settings.value("fUseSeparateProxyTor").toBool() && - !gArgs.SoftSetArg( + !m_node.softSetArg( "-onion", settings.value("addrSeparateProxyTor").toString().toStdString())) { addOverriddenOption("-onion"); @@ -191,7 +187,7 @@ if (!settings.contains("language")) { settings.setValue("language", ""); } - if (!gArgs.SoftSetArg( + if (!m_node.softSetArg( "-lang", settings.value("language").toString().toStdString())) { addOverriddenOption("-lang"); } @@ -336,12 +332,7 @@ break; case MapPortUPnP: // core option - can be changed on-the-fly settings.setValue("fUseUPnP", value.toBool()); - if (value.toBool()) { - StartMapPort(); - } else { - InterruptMapPort(); - StopMapPort(); - } + m_node.mapPort(value.toBool()); break; case MinimizeOnClose: fMinimizeOnClose = value.toBool(); @@ -471,7 +462,7 @@ // Directly query current base proxy, because // GUI settings can be overridden with -proxy. proxyType curProxy; - if (GetProxy(NET_IPV4, curProxy)) { + if (m_node.getProxy(NET_IPV4, curProxy)) { proxy.setType(QNetworkProxy::Socks5Proxy); proxy.setHostName(QString::fromStdString(curProxy.proxy.ToStringIP())); proxy.setPort(curProxy.proxy.GetPort()); diff --git a/src/qt/test/paymentservertests.cpp b/src/qt/test/paymentservertests.cpp --- a/src/qt/test/paymentservertests.cpp +++ b/src/qt/test/paymentservertests.cpp @@ -8,6 +8,7 @@ #include "paymentrequestdata.h" #include "amount.h" +#include "interface/node.h" #include "random.h" #include "script/script.h" #include "script/standard.h" @@ -64,7 +65,8 @@ void PaymentServerTests::paymentServerTests() { SelectParams(CBaseChainParams::MAIN); - OptionsModel optionsModel; + auto node = interface::MakeNode(); + OptionsModel optionsModel(*node); PaymentServer *server = new PaymentServer(nullptr, false); X509_STORE *caStore = X509_STORE_new(); X509_STORE_add_cert(caStore, parse_b64der_cert(caCert1_BASE64)); diff --git a/src/qt/test/wallettests.cpp b/src/qt/test/wallettests.cpp --- a/src/qt/test/wallettests.cpp +++ b/src/qt/test/wallettests.cpp @@ -3,6 +3,7 @@ #include "chainparams.h" #include "config.h" #include "dstencode.h" +#include "interface/node.h" #include "qt/bitcoinamountfield.h" #include "qt/optionsmodel.h" #include "qt/overviewpage.h" @@ -139,7 +140,8 @@ std::unique_ptr platformStyle( PlatformStyle::instantiate("other")); SendCoinsDialog sendCoinsDialog(platformStyle.get()); - OptionsModel optionsModel; + auto node = interface::MakeNode(); + OptionsModel optionsModel(*node); WalletModel walletModel(platformStyle.get(), &wallet, &optionsModel); sendCoinsDialog.setModel(&walletModel);