From b2b33d9017cd7bc92099338773313fdf4572dc4a Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 19 Nov 2014 13:33:34 +0100 Subject: [PATCH] Overhaul network activity toggle - Rename RPC command "togglenetwork" to "setnetworkactive (true|false)" - Add simple test case - GUI toggle added to connections icon in statusbar --- contrib/debian/copyright | 1 + src/Makefile.qt.include | 1 + src/qt/bitcoin.qrc | 1 + src/qt/bitcoingui.cpp | 30 ++++++++++++++++++++------ src/qt/bitcoingui.h | 16 +++++++++++++- src/qt/res/icons/network_disabled.png | Bin 0 -> 435 bytes src/rpc/client.cpp | 1 + src/rpc/net.cpp | 17 +++++++++------ src/test/rpc_tests.cpp | 22 +++++++++++++++++++ 9 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 src/qt/res/icons/network_disabled.png diff --git a/contrib/debian/copyright b/contrib/debian/copyright index cc4606ca88..011cb4a4d9 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -34,6 +34,7 @@ Files: src/qt/res/icons/add.png src/qt/res/icons/info.png src/qt/res/icons/key.png src/qt/res/icons/lock_*.png + src/qt/res/icons/network_disabled.png src/qt/res/icons/open.png src/qt/res/icons/overview.png src/qt/res/icons/quit.png diff --git a/src/Makefile.qt.include b/src/Makefile.qt.include index 8947aeaca0..277cbb9deb 100644 --- a/src/Makefile.qt.include +++ b/src/Makefile.qt.include @@ -264,6 +264,7 @@ RES_ICONS = \ qt/res/icons/key.png \ qt/res/icons/lock_closed.png \ qt/res/icons/lock_open.png \ + qt/res/icons/network_disabled.png \ qt/res/icons/open.png \ qt/res/icons/overview.png \ qt/res/icons/quit.png \ diff --git a/src/qt/bitcoin.qrc b/src/qt/bitcoin.qrc index ca5b1fa673..451d391237 100644 --- a/src/qt/bitcoin.qrc +++ b/src/qt/bitcoin.qrc @@ -52,6 +52,7 @@ res/icons/transaction_abandoned.png res/icons/hd_enabled.png res/icons/hd_disabled.png + res/icons/network_disabled.png res/movies/spinner-000.png diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 69cdfc9c07..4cc30454a2 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -83,7 +83,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle * unitDisplayControl(0), labelWalletEncryptionIcon(0), labelWalletHDStatusIcon(0), - labelConnectionsIcon(0), + connectionsControl(0), labelBlocksIcon(0), progressBarLabel(0), progressBar(0), @@ -195,7 +195,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle * unitDisplayControl = new UnitDisplayStatusBarControl(platformStyle); labelWalletEncryptionIcon = new QLabel(); labelWalletHDStatusIcon = new QLabel(); - labelConnectionsIcon = new QLabel(); + connectionsControl = new NetworkToggleStatusBarControl(); labelBlocksIcon = new QLabel(); if(enableWallet) { @@ -206,7 +206,7 @@ BitcoinGUI::BitcoinGUI(const PlatformStyle *_platformStyle, const NetworkStyle * frameBlocksLayout->addWidget(labelWalletHDStatusIcon); } frameBlocksLayout->addStretch(); - frameBlocksLayout->addWidget(labelConnectionsIcon); + frameBlocksLayout->addWidget(connectionsControl); frameBlocksLayout->addStretch(); frameBlocksLayout->addWidget(labelBlocksIcon); frameBlocksLayout->addStretch(); @@ -480,6 +480,7 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel) } #endif // ENABLE_WALLET unitDisplayControl->setOptionsModel(_clientModel->getOptionsModel()); + connectionsControl->setClientModel(_clientModel); OptionsModel* optionsModel = _clientModel->getOptionsModel(); if(optionsModel) @@ -699,13 +700,15 @@ void BitcoinGUI::updateNetworkState() case 7: case 8: case 9: icon = ":/icons/connect_3"; break; default: icon = ":/icons/connect_4"; break; } - labelConnectionsIcon->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE)); if (clientModel->getNetworkActive()) { - labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); + connectionsControl->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count)); } else { - labelConnectionsIcon->setToolTip(tr("Network activity disabled")); + connectionsControl->setToolTip(tr("Network activity disabled")); + icon = ":/icons/network_disabled"; } + + connectionsControl->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE)); } void BitcoinGUI::setNumConnections(int count) @@ -1220,3 +1223,18 @@ void UnitDisplayStatusBarControl::onMenuSelection(QAction* action) optionsModel->setDisplayUnit(action->data()); } } + +void NetworkToggleStatusBarControl::mousePressEvent(QMouseEvent *event) +{ + if (clientModel) { + clientModel->setNetworkActive(!clientModel->getNetworkActive()); + } +} + +/** Lets the control know about the Client Model */ +void NetworkToggleStatusBarControl::setClientModel(ClientModel *_clientModel) +{ + if (_clientModel) { + this->clientModel = _clientModel; + } +} diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index 2eae60d41b..83634b9597 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -26,6 +26,7 @@ class PlatformStyle; class RPCConsole; class SendCoinsRecipient; class UnitDisplayStatusBarControl; +class NetworkToggleStatusBarControl; class WalletFrame; class WalletModel; class HelpMessageDialog; @@ -84,7 +85,7 @@ private: UnitDisplayStatusBarControl *unitDisplayControl; QLabel *labelWalletEncryptionIcon; QLabel *labelWalletHDStatusIcon; - QLabel *labelConnectionsIcon; + NetworkToggleStatusBarControl *connectionsControl; QLabel *labelBlocksIcon; QLabel *progressBarLabel; QProgressBar *progressBar; @@ -265,4 +266,17 @@ private Q_SLOTS: void onMenuSelection(QAction* action); }; +class NetworkToggleStatusBarControl : public QLabel +{ + Q_OBJECT + +public: + void setClientModel(ClientModel *clientModel); +protected: + void mousePressEvent(QMouseEvent *event); + +private: + ClientModel *clientModel; +}; + #endif // BITCOIN_QT_BITCOINGUI_H diff --git a/src/qt/res/icons/network_disabled.png b/src/qt/res/icons/network_disabled.png new file mode 100644 index 0000000000000000000000000000000000000000..c802e3981866fc4e431ece80c5295f66f518d41c GIT binary patch literal 435 zcmV;k0ZjghP)W1Ka?^9sc2tHf@s@G4JmEQQ-RW)g(#@1OkCTK!X}+fEGBe(9X}) zTRQj!*f|cp#%_MM7>*umV`=AaWw-i3aQH8WoZe^%558oR!}23DgFJBvjQJ_?D$jE- z;0^d#{H}p#LSPxE(3fl1gNAy6Z#mMs}N7%xr#x0IpeLuj4Hh)^C89pw!hlZ;qT zxQs(`V;fJ*r;5;y_YG(A_6Q4NMRc6W?6VGq2$z@H1n7WR_KDD30xG0{R1upHkRn2t z4xv;LMFg0H`o0vwn#4BprVJ6$bCyvP5k?^*2JVZjW^NZo?pDIr%(cjB7Sj-BYo}WV zPms^Z7GW_=PV@@5sVFv5K7sd@k@AXIp|FjVn;h!@ZmqJ5#nvuXi@R9v?q1002ovPDHLkV1n7kw<-Vt literal 0 HcmV?d00001 diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index c14d9d6747..66648b6853 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -107,6 +107,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "prioritisetransaction", 2 }, { "setban", 2 }, { "setban", 3 }, + { "setnetworkactive", 0 }, { "getmempoolancestors", 1 }, { "getmempooldescendants", 1 }, }; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 7f5b107997..045089e2e5 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -401,6 +401,7 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) " \"localrelay\": true|false, (bool) true if transaction relay is requested from peers\n" " \"timeoffset\": xxxxx, (numeric) the time offset\n" " \"connections\": xxxxx, (numeric) the number of connections\n" + " \"networkactive\": x, (numeric) the number of connections\n" " \"networks\": [ (array) information per network\n" " {\n" " \"name\": \"xxx\", (string) network (ipv4, ipv6 or onion)\n" @@ -435,8 +436,10 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) obj.push_back(Pair("localservices", strprintf("%016x", g_connman->GetLocalServices()))); obj.push_back(Pair("localrelay", fRelayTxes)); obj.push_back(Pair("timeoffset", GetTimeOffset())); - if(g_connman) + if (g_connman) { + obj.push_back(Pair("networkactive", (int)g_connman->GetNetworkActive())); obj.push_back(Pair("connections", (int)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL))); + } obj.push_back(Pair("networks", GetNetworksInfo())); obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); UniValue localAddresses(UniValue::VARR); @@ -571,12 +574,12 @@ UniValue clearbanned(const UniValue& params, bool fHelp) return NullUniValue; } -UniValue togglenetwork(const JSONRPCRequest& request) +UniValue setnetworkactive(const JSONRPCRequest& request) { - if (request.fHelp || request.params.size() != 0) { + if (request.fHelp || request.params.size() != 1) { throw runtime_error( - "togglenetwork\n" - "Toggle all network activity temporarily." + "setnetworkactive \"true|false\"\n" + "Disable/Re-Enable all network activity temporarily." ); } @@ -584,7 +587,7 @@ UniValue togglenetwork(const JSONRPCRequest& request) throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled"); } - g_connman->SetNetworkActive(!g_connman->GetNetworkActive()); + g_connman->SetNetworkActive(request.params[0].get_bool()); return g_connman->GetNetworkActive(); } @@ -603,7 +606,7 @@ static const CRPCCommand commands[] = { "network", "setban", &setban, true }, { "network", "listbanned", &listbanned, true }, { "network", "clearbanned", &clearbanned, true }, - { "network", "togglenetwork", &togglenetwork, true, }, + { "network", "setnetworkactive", &setnetworkactive, true, }, }; void RegisterNetRPCCommands(CRPCTable &t) diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index a15915aad2..03594878ed 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -81,6 +81,28 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams) BOOST_CHECK_THROW(CallRPC(string("sendrawtransaction ")+rawtx+" extra"), runtime_error); } +BOOST_AUTO_TEST_CASE(rpc_togglenetwork) +{ + UniValue r; + + r = CallRPC("getnetworkinfo"); + int netState = find_value(r.get_obj(), "networkactive").get_int(); + BOOST_CHECK_EQUAL(netState, 1); + + BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive false")); + r = CallRPC("getnetworkinfo"); + int numConnection = find_value(r.get_obj(), "connections").get_int(); + BOOST_CHECK_EQUAL(numConnection, 0); + + netState = find_value(r.get_obj(), "networkactive").get_int(); + BOOST_CHECK_EQUAL(netState, 0); + + BOOST_CHECK_NO_THROW(CallRPC("setnetworkactive true")); + r = CallRPC("getnetworkinfo"); + netState = find_value(r.get_obj(), "networkactive").get_int(); + BOOST_CHECK_EQUAL(netState, 1); +} + BOOST_AUTO_TEST_CASE(rpc_rawsign) { UniValue r;