Skip to content

Commit 7ad2adf

Browse files
committed
GUI: Event handler generic interface.
1 parent 06494f9 commit 7ad2adf

File tree

14 files changed

+167
-39
lines changed

14 files changed

+167
-39
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ set(SERVER_SOURCES
194194
./src/httprpc.cpp
195195
./src/httpserver.cpp
196196
./src/init.cpp
197+
./src/interfaces/handler.cpp
197198
./src/interfaces/wallet.cpp
198199
./src/dbwrapper.cpp
199200
./src/legacy/validation_zerocoin_legacy.cpp

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ BITCOIN_CORE_H = \
190190
httprpc.h \
191191
httpserver.h \
192192
init.h \
193+
interfaces/handler.h \
193194
interfaces/wallet.h \
194195
invalid.h \
195196
invalid_outpoints.json.h \
@@ -533,6 +534,7 @@ libbitcoin_util_a_SOURCES = \
533534
compat/glibcxx_sanity.cpp \
534535
compat/strnlen.cpp \
535536
fs.cpp \
537+
interfaces/handler.cpp \
536538
logging.cpp \
537539
random.cpp \
538540
rpc/protocol.cpp \

src/interfaces/handler.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2018-2020 The Bitcoin Core developers
2+
// Copyright (c) 2020 The PIVX Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
5+
6+
#include <interfaces/handler.h>
7+
8+
#include <util/memory.h>
9+
10+
#include <boost/signals2/connection.hpp>
11+
#include <utility>
12+
13+
namespace interfaces {
14+
namespace {
15+
16+
class HandlerImpl : public Handler
17+
{
18+
public:
19+
explicit HandlerImpl(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
20+
21+
void disconnect() override { m_connection.disconnect(); }
22+
23+
boost::signals2::scoped_connection m_connection;
24+
};
25+
26+
} // namespace
27+
28+
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection)
29+
{
30+
return MakeUnique<HandlerImpl>(std::move(connection));
31+
}
32+
33+
} // namespace interfaces

src/interfaces/handler.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2018-2020 The Bitcoin Core developers
2+
// Copyright (c) 2020 The PIVX Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef PIVX_INTERFACES_HANDLER_H
7+
#define PIVX_INTERFACES_HANDLER_H
8+
9+
#include <memory>
10+
11+
namespace boost {
12+
namespace signals2 {
13+
class connection;
14+
} // namespace signals2
15+
} // namespace boost
16+
17+
namespace interfaces {
18+
19+
//! Generic interface for managing an event handler or callback function
20+
//! registered with another interface. Has a single disconnect method to cancel
21+
//! the registration and prevent any future notifications.
22+
class Handler
23+
{
24+
public:
25+
virtual ~Handler() {}
26+
27+
//! Disconnect the handler.
28+
virtual void disconnect() = 0;
29+
};
30+
31+
//! Return handler wrapping a boost signal connection.
32+
std::unique_ptr<Handler> MakeHandler(boost::signals2::connection connection);
33+
34+
} // namespace interfaces
35+
36+
#endif // PIVX_INTERFACES_HANDLER_H

src/qt/clientmodel.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "chainparams.h"
1515
#include "checkpoints.h"
1616
#include "clientversion.h"
17-
#include "masternode-sync.h"
17+
#include "interfaces/handler.h"
1818
#include "masternodeman.h"
1919
#include "net.h"
2020
#include "netbase.h"
@@ -310,21 +310,21 @@ static void BannedListChanged(ClientModel *clientmodel)
310310
void ClientModel::subscribeToCoreSignals()
311311
{
312312
// Connect signals to client
313-
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
314-
uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
315-
uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this));
316-
uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this));
317-
uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, boost::placeholders::_1, boost::placeholders::_2));
313+
m_handler_show_progress = interfaces::MakeHandler(uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2)));
314+
m_handler_notify_num_connections_changed = interfaces::MakeHandler(uiInterface.NotifyNumConnectionsChanged.connect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1)));
315+
m_handler_notify_alert_changed = interfaces::MakeHandler(uiInterface.NotifyAlertChanged.connect(boost::bind(NotifyAlertChanged, this)));
316+
m_handler_banned_list_changed = interfaces::MakeHandler(uiInterface.BannedListChanged.connect(boost::bind(BannedListChanged, this)));
317+
m_handler_notify_block_tip = interfaces::MakeHandler(uiInterface.NotifyBlockTip.connect(boost::bind(BlockTipChanged, this, boost::placeholders::_1, boost::placeholders::_2)));
318318
}
319319

320320
void ClientModel::unsubscribeFromCoreSignals()
321321
{
322322
// Disconnect signals from client
323-
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
324-
uiInterface.NotifyNumConnectionsChanged.disconnect(boost::bind(NotifyNumConnectionsChanged, this, boost::placeholders::_1));
325-
uiInterface.NotifyAlertChanged.disconnect(boost::bind(NotifyAlertChanged, this));
326-
uiInterface.BannedListChanged.disconnect(boost::bind(BannedListChanged, this));
327-
uiInterface.NotifyBlockTip.disconnect(boost::bind(BlockTipChanged, this, boost::placeholders::_1, boost::placeholders::_2));
323+
m_handler_show_progress->disconnect();
324+
m_handler_notify_num_connections_changed->disconnect();
325+
m_handler_notify_alert_changed->disconnect();
326+
m_handler_banned_list_changed->disconnect();
327+
m_handler_notify_block_tip->disconnect();
328328
}
329329

330330
bool ClientModel::getTorInfo(std::string& ip_port) const

src/qt/clientmodel.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
#include <QObject>
1313
#include <QDateTime>
1414

15+
#include <memory>
16+
1517
class AddressTableModel;
1618
class BanTableModel;
1719
class OptionsModel;
1820
class PeerTableModel;
1921
class TransactionTableModel;
2022

21-
class CWallet;
23+
namespace interfaces {
24+
class Handler;
25+
}
2226

2327
QT_BEGIN_NAMESPACE
2428
class QDateTime;
@@ -94,6 +98,13 @@ class ClientModel : public QObject
9498
QString getMasternodesCount();
9599

96100
private:
101+
// Listeners
102+
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
103+
std::unique_ptr<interfaces::Handler> m_handler_notify_num_connections_changed;
104+
std::unique_ptr<interfaces::Handler> m_handler_notify_alert_changed;
105+
std::unique_ptr<interfaces::Handler> m_handler_banned_list_changed;
106+
std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;
107+
97108
QString getMasternodeCountString() const;
98109
OptionsModel* optionsModel;
99110
PeerTableModel* peerTableModel;

src/qt/pivx/pivxgui.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "qt/guiutil.h"
1212
#include "clientmodel.h"
13+
#include "interfaces/handler.h"
1314
#include "optionsmodel.h"
1415
#include "networkstyle.h"
1516
#include "notificator.h"
@@ -689,11 +690,11 @@ static bool ThreadSafeMessageBox(PIVXGUI* gui, const std::string& message, const
689690
void PIVXGUI::subscribeToCoreSignals()
690691
{
691692
// Connect signals to client
692-
uiInterface.ThreadSafeMessageBox.connect(boost::bind(ThreadSafeMessageBox, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3));
693+
m_handler_message_box = interfaces::MakeHandler(uiInterface.ThreadSafeMessageBox.connect(boost::bind(ThreadSafeMessageBox, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3)));
693694
}
694695

695696
void PIVXGUI::unsubscribeFromCoreSignals()
696697
{
697698
// Disconnect signals from client
698-
uiInterface.ThreadSafeMessageBox.disconnect(boost::bind(ThreadSafeMessageBox, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3));
699+
m_handler_message_box->disconnect();
699700
}

src/qt/pivx/pivxgui.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "qt/pivx/settings/settingsfaqwidget.h"
2828
#include "qt/rpcconsole.h"
2929

30+
namespace interfaces {
31+
class Handler;
32+
}
3033

3134
class ClientModel;
3235
class NetworkStyle;
@@ -114,14 +117,16 @@ public Q_SLOTS:
114117
*/
115118

116119
private:
120+
// Handlers
121+
std::unique_ptr<interfaces::Handler> m_handler_message_box;
122+
117123
bool enableWallet;
118124
ClientModel* clientModel = nullptr;
119125

120126
// Actions
121127
QAction* quitAction = nullptr;
122128
QAction* toggleHideAction = nullptr;
123129

124-
125130
// Frame
126131
NavMenuWidget *navMenu = nullptr;
127132
TopBar *topBar = nullptr;

src/qt/pivx/splash.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "qt/pivx/forms/ui_splash.h"
77
#include "QFile"
88

9+
#include "interfaces/handler.h"
910
#include "init.h"
1011
#include "guiinterface.h"
1112
#include "networkstyle.h"
@@ -76,27 +77,30 @@ static void ShowProgress(Splash* splash, const std::string& title, int nProgress
7677
}
7778

7879
#ifdef ENABLE_WALLET
80+
std::unique_ptr<interfaces::Handler> m_handler_show_progress_wallet;
7981
static void ConnectWallet(Splash* splash, CWallet* wallet){
80-
wallet->ShowProgress.connect(boost::bind(ShowProgress, splash, boost::placeholders::_1, boost::placeholders::_2));
82+
m_handler_show_progress_wallet = interfaces::MakeHandler(wallet->ShowProgress.connect(boost::bind(ShowProgress, splash, boost::placeholders::_1, boost::placeholders::_2)));
8183
}
8284
#endif
8385

8486
void Splash::subscribeToCoreSignals(){
8587
// Connect signals to client
86-
uiInterface.InitMessage.connect(boost::bind(InitMessage, this, boost::placeholders::_1));
87-
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
88+
m_handler_init_message = interfaces::MakeHandler(uiInterface.InitMessage.connect(boost::bind(InitMessage, this, boost::placeholders::_1)));
89+
m_handler_show_progress = interfaces::MakeHandler(uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2)));
8890
#ifdef ENABLE_WALLET
89-
uiInterface.LoadWallet.connect(boost::bind(ConnectWallet, this, boost::placeholders::_1));
91+
m_handler_load_wallet = interfaces::MakeHandler(uiInterface.LoadWallet.connect(boost::bind(ConnectWallet, this, boost::placeholders::_1)));
9092
#endif
9193
}
9294

9395
void Splash::unsubscribeFromCoreSignals(){
9496
// Disconnect signals from client
95-
uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, boost::placeholders::_1));
96-
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
97+
m_handler_init_message->disconnect();
98+
m_handler_show_progress->disconnect();
9799
#ifdef ENABLE_WALLET
98-
if (pwalletMain)
99-
pwalletMain->ShowProgress.disconnect(boost::bind(ShowProgress, this, boost::placeholders::_1, boost::placeholders::_2));
100+
if (pwalletMain) {
101+
m_handler_load_wallet->disconnect();
102+
if (m_handler_show_progress_wallet) m_handler_show_progress_wallet->disconnect();
103+
}
100104
#endif
101105
}
102106

src/qt/pivx/splash.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
#include <QWidget>
99

10+
#include <memory>
11+
1012
class NetworkStyle;
1113

14+
namespace interfaces {
15+
class Handler;
16+
};
17+
1218
namespace Ui {
1319
class Splash;
1420
}
@@ -34,6 +40,11 @@ public Q_SLOTS:
3440
private:
3541
Ui::Splash *ui;
3642

43+
// Listeners
44+
std::unique_ptr<interfaces::Handler> m_handler_init_message;
45+
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
46+
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
47+
3748
/** Connect core signals to splash screen */
3849
void subscribeToCoreSignals();
3950
/** Disconnect core signals to splash screen */

0 commit comments

Comments
 (0)