Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tor icon #86

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ RES_ICONS = \
qt/res/icons/remove.png \
qt/res/icons/send.png \
qt/res/icons/synced.png \
qt/res/icons/tor_connected.png \
qt/res/icons/transaction0.png \
qt/res/icons/transaction2.png \
qt/res/icons/transaction_abandoned.png \
Expand Down
11 changes: 9 additions & 2 deletions src/interfaces/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ class NodeImpl : public Node
}
}
bool getProxy(Network net, proxyType& proxy_info) override { return GetProxy(net, proxy_info); }
size_t getNodeCount(CConnman::NumConnections flags) override

size_t peerCount() override
{
return m_context->connman ? m_context->connman->GetNodeCount(flags) : 0;
return m_context->connman ? m_context->connman->PeerCount() : 0;
}

ConnCounts connectionCounts() override
{
return m_context->connman ? m_context->connman->ConnectionCounts() : ConnCounts();
}

bool getNodesStats(NodesStats& stats) override
{
stats.clear();
Expand Down
13 changes: 8 additions & 5 deletions src/interfaces/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define BITCOIN_INTERFACES_NODE_H

#include <amount.h> // For CAmount
#include <net.h> // For CConnman::NumConnections
#include <net.h> // For ConnCounts
#include <net_types.h> // For banmap_t
#include <netaddress.h> // For Network
#include <support/allocators/secure.h> // For SecureString
Expand Down Expand Up @@ -87,8 +87,11 @@ class Node
//! Get proxy.
virtual bool getProxy(Network net, proxyType& proxy_info) = 0;

//! Get number of connections.
virtual size_t getNodeCount(CConnman::NumConnections flags) = 0;
//! Get number of peers.
virtual size_t peerCount() = 0;

//! Get numbers of connections of different types.
virtual ConnCounts connectionCounts() = 0;

//! Get stats for connected nodes.
using NodesStats = std::vector<std::tuple<CNodeStats, bool, CNodeStateStats>>;
Expand Down Expand Up @@ -196,11 +199,11 @@ class Node
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;

//! Register handler for number of connections changed messages.
using NotifyNumConnectionsChangedFn = std::function<void(int new_num_connections)>;
using NotifyNumConnectionsChangedFn = std::function<void()>;
virtual std::unique_ptr<Handler> handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0;

//! Register handler for network active messages.
using NotifyNetworkActiveChangedFn = std::function<void(bool network_active)>;
using NotifyNetworkActiveChangedFn = std::function<void()>;
virtual std::unique_ptr<Handler> handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0;

//! Register handler for notify alert messages.
Expand Down
44 changes: 22 additions & 22 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,15 +1213,12 @@ void CConnman::DisconnectNodes()

void CConnman::NotifyNumConnectionsChanged()
{
size_t vNodesSize;
{
LOCK(cs_vNodes);
vNodesSize = vNodes.size();
}
if(vNodesSize != nPrevNodeCount) {
const size_t vNodesSize = WITH_LOCK(cs_vNodes, return vNodes.size());
if (vNodesSize != nPrevNodeCount) {
nPrevNodeCount = vNodesSize;
if(clientInterface)
clientInterface->NotifyNumConnectionsChanged(vNodesSize);
if (clientInterface) {
clientInterface->NotifyNumConnectionsChanged();
}
}
}

Expand Down Expand Up @@ -2353,8 +2350,9 @@ void CConnman::SetNetworkActive(bool active)
}

fNetworkActive = active;

uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
if (clientInterface) {
clientInterface->NotifyNetworkActiveChanged();
}
}

CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, bool network_active)
Expand Down Expand Up @@ -2471,7 +2469,9 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
LogPrintf("%i block-relay-only anchors will be tried for connections.\n", m_anchors.size());
}

uiInterface.InitMessage(_("Starting network threads...").translated);
if (clientInterface) {
clientInterface->InitMessage(_("Starting network threads...").translated);
}

fAddressesInitialized = true;

Expand Down Expand Up @@ -2729,20 +2729,20 @@ bool CConnman::RemoveAddedNode(const std::string& strNode)
return false;
}

size_t CConnman::GetNodeCount(NumConnections flags)
ConnCounts CConnman::ConnectionCounts()
{
LOCK(cs_vNodes);
if (flags == CConnman::CONNECTIONS_ALL) // Shortcut if we want total
return vNodes.size();

int nNum = 0;
for (const auto& pnode : vNodes) {
if (flags & (pnode->IsInboundConn() ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
nNum++;
int num_in{0};
int num_out{0};
bool onion_only{false};
{
LOCK(cs_vNodes);
if (!vNodes.empty()) onion_only = true;
for (const auto& pnode : vNodes) {
pnode->IsInboundConn() ? ++num_in : ++num_out;
if (pnode->ConnectedThroughNetwork() != NET_ONION) onion_only = false;
}
}

return nNum;
return {num_in, num_out, onion_only};
}

void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
Expand Down
27 changes: 19 additions & 8 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,28 @@ enum class ConnectionType {
ADDR_FETCH,
};

struct ConnCounts
{
const int all{0};
const int in{0};
const int out{0};
const bool onion_only{false};

ConnCounts()
{
}

ConnCounts(int num_in, int num_out, bool is_onion_only)
: all(num_in + num_out), in(num_in), out(num_out), onion_only(is_onion_only)
{
}
};

class NetEventsInterface;
class CConnman
{
public:

enum NumConnections {
CONNECTIONS_NONE = 0,
CONNECTIONS_IN = (1U << 0),
CONNECTIONS_OUT = (1U << 1),
CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT),
};

struct Options
{
ServiceFlags nLocalServices = NODE_NONE;
Expand Down Expand Up @@ -346,7 +356,8 @@ class CConnman
bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo();

size_t GetNodeCount(NumConnections num);
size_t PeerCount() { return WITH_LOCK(cs_vNodes, return vNodes.size()); }
ConnCounts ConnectionCounts();
void GetNodeStats(std::vector<CNodeStats>& vstats);
bool DisconnectNode(const std::string& node);
bool DisconnectNode(const CSubNet& subnet);
Expand Down
4 changes: 2 additions & 2 deletions src/node/ui_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ ADD_SIGNALS_IMPL_WRAPPER(BannedListChanged);
bool CClientUIInterface::ThreadSafeMessageBox(const bilingual_str& message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeMessageBox(message, caption, style).value_or(false);}
bool CClientUIInterface::ThreadSafeQuestion(const bilingual_str& message, const std::string& non_interactive_message, const std::string& caption, unsigned int style) { return g_ui_signals.ThreadSafeQuestion(message, non_interactive_message, caption, style).value_or(false);}
void CClientUIInterface::InitMessage(const std::string& message) { return g_ui_signals.InitMessage(message); }
void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { return g_ui_signals.NotifyNumConnectionsChanged(newNumConnections); }
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
void CClientUIInterface::NotifyNumConnectionsChanged() { return g_ui_signals.NotifyNumConnectionsChanged(); }
void CClientUIInterface::NotifyNetworkActiveChanged() { return g_ui_signals.NotifyNetworkActiveChanged(); }
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
Expand Down
4 changes: 2 additions & 2 deletions src/node/ui_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ class CClientUIInterface
ADD_SIGNALS_DECL_WRAPPER(InitMessage, void, const std::string& message);

/** Number of network connections changed. */
ADD_SIGNALS_DECL_WRAPPER(NotifyNumConnectionsChanged, void, int newNumConnections);
ADD_SIGNALS_DECL_WRAPPER(NotifyNumConnectionsChanged, void, void);

/** Network activity state changed. */
ADD_SIGNALS_DECL_WRAPPER(NotifyNetworkActiveChanged, void, bool networkActive);
ADD_SIGNALS_DECL_WRAPPER(NotifyNetworkActiveChanged, void, void);

/**
* Status bar alerts changed.
Expand Down
1 change: 1 addition & 0 deletions src/qt/bitcoin.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<file alias="hd_enabled">res/icons/hd_enabled.png</file>
<file alias="hd_disabled">res/icons/hd_disabled.png</file>
<file alias="network_disabled">res/icons/network_disabled.png</file>
<file alias="tor_connected">res/icons/tor_connected.png</file>
<file alias="proxy">res/icons/proxy.png</file>
</qresource>
<qresource prefix="/animation">
Expand Down
29 changes: 16 additions & 13 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const std::string BitcoinGUI::DEFAULT_UIPLATFORM =
BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformStyle, const NetworkStyle *networkStyle, QWidget *parent) :
QMainWindow(parent),
m_node(node),
m_tor_icon{new GUIUtil::ClickableLabel()},
trayIconMenu{new QMenu()},
platformStyle(_platformStyle),
m_network_style(networkStyle)
Expand Down Expand Up @@ -160,6 +161,7 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
frameBlocksLayout->addWidget(labelWalletEncryptionIcon);
frameBlocksLayout->addWidget(labelWalletHDStatusIcon);
}
frameBlocksLayout->addWidget(m_tor_icon);
frameBlocksLayout->addWidget(labelProxyIcon);
frameBlocksLayout->addStretch();
frameBlocksLayout->addWidget(connectionsControl);
Expand Down Expand Up @@ -202,6 +204,9 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
connect(labelProxyIcon, &GUIUtil::ClickableLabel::clicked, [this] {
openOptionsDialogWithTab(OptionsDialog::TAB_NETWORK);
});
connect(m_tor_icon, &GUIUtil::ClickableLabel::clicked, [this] {
openOptionsDialogWithTab(OptionsDialog::TAB_NETWORK);
});

connect(labelBlocksIcon, &GUIUtil::ClickableLabel::clicked, this, &BitcoinGUI::showModalOverlay);
connect(progressBar, &GUIUtil::ClickableProgressBar::clicked, this, &BitcoinGUI::showModalOverlay);
Expand Down Expand Up @@ -585,8 +590,8 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel, interfaces::BlockAndH

// Keep up to date with client
updateNetworkState();
connect(_clientModel, &ClientModel::numConnectionsChanged, this, &BitcoinGUI::setNumConnections);
connect(_clientModel, &ClientModel::networkActiveChanged, this, &BitcoinGUI::setNetworkActive);
connect(_clientModel, &ClientModel::numConnectionsChanged, this, &BitcoinGUI::updateNetworkState);
connect(_clientModel, &ClientModel::networkActiveChanged, this, &BitcoinGUI::updateNetworkState);

modalOverlay->setKnownBestHeight(tip_info->header_height, QDateTime::fromTime_t(tip_info->header_time));
setNumBlocks(tip_info->block_height, QDateTime::fromTime_t(tip_info->block_time), tip_info->verification_progress, false, SynchronizationState::INIT_DOWNLOAD);
Expand Down Expand Up @@ -898,7 +903,8 @@ void BitcoinGUI::gotoLoadPSBT(bool from_clipboard)

void BitcoinGUI::updateNetworkState()
{
int count = clientModel->getNumConnections();
const ConnCounts conn_counts = m_node.connectionCounts();
const int count = conn_counts.all;
QString icon;
switch(count)
{
Expand All @@ -921,18 +927,15 @@ void BitcoinGUI::updateNetworkState()
// Don't word-wrap this (fixed-width) tooltip
tooltip = QString("<nobr>") + tooltip + QString("</nobr>");
connectionsControl->setToolTip(tooltip);

connectionsControl->setPixmap(platformStyle->SingleColorIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
}

void BitcoinGUI::setNumConnections(int count)
{
updateNetworkState();
}

void BitcoinGUI::setNetworkActive(bool networkActive)
{
updateNetworkState();
if (conn_counts.onion_only) {
m_tor_icon->setPixmap(platformStyle->SingleColorIcon(":/icons/tor_connected").pixmap(STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE));
m_tor_icon->setToolTip(tr("All connections are <b>via Tor</b> only"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "All current connections are via Tor only"?

m_tor_icon->show();
} else {
m_tor_icon->hide();
}
}

void BitcoinGUI::updateHeadersSyncProgressLabel()
Expand Down
10 changes: 3 additions & 7 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class BitcoinGUI : public QMainWindow
UnitDisplayStatusBarControl* unitDisplayControl = nullptr;
QLabel* labelWalletEncryptionIcon = nullptr;
QLabel* labelWalletHDStatusIcon = nullptr;
GUIUtil::ClickableLabel* m_tor_icon{nullptr};
GUIUtil::ClickableLabel* labelProxyIcon = nullptr;
GUIUtil::ClickableLabel* connectionsControl = nullptr;
GUIUtil::ClickableLabel* labelBlocksIcon = nullptr;
Expand Down Expand Up @@ -201,9 +202,6 @@ class BitcoinGUI : public QMainWindow
/** Connect core signals to GUI client */
void subscribeToCoreSignals();

/** Update UI with latest network info from model. */
void updateNetworkState();

void updateHeadersSyncProgressLabel();

/** Open the OptionsDialog on the specified tab index */
Expand All @@ -217,10 +215,8 @@ class BitcoinGUI : public QMainWindow
void setPrivacy(bool privacy);

public Q_SLOTS:
/** Set number of connections shown in the UI */
void setNumConnections(int count);
/** Set network state shown in the UI */
void setNetworkActive(bool networkActive);
/** Update UI with latest network info from model. */
void updateNetworkState();
/** Set number of blocks and last block date shown in the UI */
void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers, SynchronizationState sync_state);

Expand Down
Loading