1 // Copyright (c) 2011-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_QT_RPCCONSOLE_H
6 #define BITCOIN_QT_RPCCONSOLE_H
7 
8 #include <qt/guiutil.h>
9 #include <qt/peertablemodel.h>
10 
11 #include <net.h>
12 
13 #include <QWidget>
14 #include <QCompleter>
15 #include <QThread>
16 
17 class ClientModel;
18 class PlatformStyle;
19 class RPCTimerInterface;
20 class WalletModel;
21 
22 namespace interfaces {
23     class Node;
24 }
25 
26 namespace Ui {
27     class RPCConsole;
28 }
29 
30 QT_BEGIN_NAMESPACE
31 class QMenu;
32 class QItemSelection;
33 QT_END_NAMESPACE
34 
35 /** Local Bitcoin RPC console. */
36 class RPCConsole: public QWidget
37 {
38     Q_OBJECT
39 
40 public:
41     explicit RPCConsole(interfaces::Node& node, const PlatformStyle *platformStyle, QWidget *parent);
42     ~RPCConsole();
43 
44     static bool RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr);
45     static bool RPCExecuteCommandLine(interfaces::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr) {
46         return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, wallet_model);
47     }
48 
49     void setClientModel(ClientModel *model);
50     void addWallet(WalletModel * const walletModel);
51     void removeWallet(WalletModel* const walletModel);
52 
53     enum MessageClass {
54         MC_ERROR,
55         MC_DEBUG,
56         CMD_REQUEST,
57         CMD_REPLY,
58         CMD_ERROR
59     };
60 
61     enum TabTypes {
62         TAB_INFO = 0,
63         TAB_CONSOLE = 1,
64         TAB_GRAPH = 2,
65         TAB_PEERS = 3
66     };
67 
tabs()68     std::vector<TabTypes> tabs() const { return {TAB_INFO, TAB_CONSOLE, TAB_GRAPH, TAB_PEERS}; }
69 
70     TabTypes tabFocus() const;
71     QString tabTitle(TabTypes tab_type) const;
72 
73 protected:
74     virtual bool eventFilter(QObject* obj, QEvent *event);
75     void keyPressEvent(QKeyEvent *);
76 
77 private Q_SLOTS:
78     void on_lineEdit_returnPressed();
79     void on_tabWidget_currentChanged(int index);
80     /** open the debug.log from the current datadir */
81     void on_openDebugLogfileButton_clicked();
82     /** change the time range of the network traffic graph */
83     void on_sldGraphRange_valueChanged(int value);
84     /** update traffic statistics */
85     void updateTrafficStats(quint64 totalBytesIn, quint64 totalBytesOut);
86     void resizeEvent(QResizeEvent *event);
87     void showEvent(QShowEvent *event);
88     void hideEvent(QHideEvent *event);
89     /** Show custom context menu on Peers tab */
90     void showPeersTableContextMenu(const QPoint& point);
91     /** Show custom context menu on Bans tab */
92     void showBanTableContextMenu(const QPoint& point);
93     /** Hides ban table if no bans are present */
94     void showOrHideBanTableIfRequired();
95     /** clear the selected node */
96     void clearSelectedNode();
97 
98 public Q_SLOTS:
99     void clear(bool clearHistory = true);
100     void fontBigger();
101     void fontSmaller();
102     void setFontSize(int newSize);
103     /** Append the message to the message widget */
message(int category,const QString & msg)104     void message(int category, const QString &msg) { message(category, msg, false); }
105     void message(int category, const QString &message, bool html);
106     /** Set number of connections shown in the UI */
107     void setNumConnections(int count);
108     /** Set network state shown in the UI */
109     void setNetworkActive(bool networkActive);
110     /** Set number of blocks and last block date shown in the UI */
111     void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers);
112     /** Set size (number of transactions and memory usage) of the mempool in the UI */
113     void setMempoolSize(long numberOfTxs, size_t dynUsage);
114     /** Go forward or back in history */
115     void browseHistory(int offset);
116     /** Scroll console view to end */
117     void scrollToEnd();
118     /** Handle selection of peer in peers list */
119     void peerSelected(const QItemSelection &selected, const QItemSelection &deselected);
120     /** Handle selection caching before update */
121     void peerLayoutAboutToChange();
122     /** Handle updated peer information */
123     void peerLayoutChanged();
124     /** Disconnect a selected node on the Peers tab */
125     void disconnectSelectedNode();
126     /** Ban a selected node on the Peers tab */
127     void banSelectedNode(int bantime);
128     /** Unban a selected node on the Bans tab */
129     void unbanSelectedNode();
130     /** set which tab has the focus (is visible) */
131     void setTabFocus(enum TabTypes tabType);
132 
133 Q_SIGNALS:
134     // For RPC command executor
135     void cmdRequest(const QString &command, const WalletModel* wallet_model);
136 
137 private:
138     void startExecutor();
139     void setTrafficGraphRange(int mins);
140     /** show detailed information on ui about selected node */
141     void updateNodeDetail(const CNodeCombinedStats *stats);
142 
143     enum ColumnWidths
144     {
145         ADDRESS_COLUMN_WIDTH = 200,
146         SUBVERSION_COLUMN_WIDTH = 150,
147         PING_COLUMN_WIDTH = 80,
148         BANSUBNET_COLUMN_WIDTH = 200,
149         BANTIME_COLUMN_WIDTH = 250
150 
151     };
152 
153     interfaces::Node& m_node;
154     Ui::RPCConsole* const ui;
155     ClientModel *clientModel = nullptr;
156     QStringList history;
157     int historyPtr = 0;
158     QString cmdBeforeBrowsing;
159     QList<NodeId> cachedNodeids;
160     const PlatformStyle* const platformStyle;
161     RPCTimerInterface *rpcTimerInterface = nullptr;
162     QMenu *peersTableContextMenu = nullptr;
163     QMenu *banTableContextMenu = nullptr;
164     int consoleFontSize = 0;
165     QCompleter *autoCompleter = nullptr;
166     QThread thread;
167     WalletModel* m_last_wallet_model{nullptr};
168 
169     /** Update UI with latest network info from model. */
170     void updateNetworkState();
171 };
172 
173 #endif // BITCOIN_QT_RPCCONSOLE_H
174