Skip to content

Commit 5fba3af

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/splashscreen.cpp
1 parent c2f672f commit 5fba3af

File tree

10 files changed

+139
-29
lines changed

10 files changed

+139
-29
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DIST_SUBDIRS = secp256k1 univalue
77
AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(GPROF_LDFLAGS) $(SANITIZER_LDFLAGS)
88
AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS) $(GPROF_CXXFLAGS) $(SANITIZER_CXXFLAGS)
99
AM_CPPFLAGS = $(HARDENED_CPPFLAGS)
10+
AM_LIBTOOLFLAGS = --preserve-dup-deps
1011
EXTRA_LIBRARIES =
1112

1213
if EMBEDDED_UNIVALUE
@@ -106,6 +107,7 @@ BITCOIN_CORE_H = \
106107
init.h \
107108
interface/handler.h \
108109
interface/node.h \
110+
interface/wallet.h \
109111
key.h \
110112
key_io.h \
111113
keystore.h \
@@ -247,6 +249,7 @@ endif
247249
libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
248250
libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
249251
libbitcoin_wallet_a_SOURCES = \
252+
interface/wallet.cpp \
250253
wallet/crypter.cpp \
251254
wallet/db.cpp \
252255
wallet/feebumper.cpp \

src/Makefile.qt.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ if TARGET_WINDOWS
402402
endif
403403
qt_bitcoin_qt_LDADD = qt/libbitcoinqt.a $(LIBBITCOIN_SERVER)
404404
if ENABLE_WALLET
405-
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET)
405+
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_UTIL) $(LIBBITCOIN_WALLET)
406406
endif
407407
if ENABLE_ZMQ
408408
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
@@ -411,7 +411,7 @@ qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL)
411411
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
412412
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
413413
qt_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
414-
qt_bitcoin_qt_LIBTOOLFLAGS = --tag CXX
414+
qt_bitcoin_qt_LIBTOOLFLAGS = $(AM_LIBTOOLFLAGS) --tag CXX
415415

416416
#locale/foo.ts -> locale/foo.qm
417417
QT_QM=$(QT_TS:.ts=.qm)

src/Makefile.qttest.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ nodist_qt_test_test_bitcoin_qt_SOURCES = $(TEST_QT_MOC_CPP)
5252

5353
qt_test_test_bitcoin_qt_LDADD = $(LIBBITCOINQT) $(LIBBITCOIN_SERVER)
5454
if ENABLE_WALLET
55-
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET)
55+
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_UTIL) $(LIBBITCOIN_WALLET)
5656
endif
5757
if ENABLE_ZMQ
5858
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)

src/interface/node.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chainparams.h>
88
#include <init.h>
99
#include <interface/handler.h>
10+
#include <interface/wallet.h>
1011
#include <net.h>
1112
#include <netaddress.h>
1213
#include <netbase.h>
@@ -15,8 +16,19 @@
1516
#include <util.h>
1617
#include <warnings.h>
1718

19+
#if defined(HAVE_CONFIG_H)
20+
#include <config/bitcoin-config.h>
21+
#endif
22+
#ifdef ENABLE_WALLET
23+
#define CHECK_WALLET(x) x
24+
#else
25+
#define CHECK_WALLET(x) throw std::logic_error("Wallet function called in non-wallet build.")
26+
#endif
27+
1828
#include <boost/thread/thread.hpp>
1929

30+
class CWallet;
31+
2032
namespace interface {
2133
namespace {
2234

@@ -69,6 +81,15 @@ class NodeImpl : public Node
6981
{
7082
return MakeHandler(::uiInterface.ThreadSafeQuestion.connect(fn));
7183
}
84+
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
85+
{
86+
return MakeHandler(::uiInterface.ShowProgress.connect(fn));
87+
}
88+
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
89+
{
90+
CHECK_WALLET(
91+
return MakeHandler(::uiInterface.LoadWallet.connect([fn](CWallet* wallet) { fn(MakeWallet(*wallet)); })));
92+
}
7293
};
7394

7495
} // namespace

src/interface/node.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class proxyType;
1717
namespace interface {
1818

1919
class Handler;
20+
class Wallet;
2021

2122
//! Top-level interface for a bitcoin node (bitcoind process).
2223
class Node
@@ -87,6 +88,14 @@ class Node
8788
const std::string& caption,
8889
unsigned int style)>;
8990
virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
91+
92+
//! Register handler for progress messages.
93+
using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
94+
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
95+
96+
//! Register handler for load wallet messages.
97+
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
98+
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
9099
};
91100

92101
//! Return implementation of Node interface.

src/interface/wallet.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2018 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+
#include <interface/wallet.h>
6+
7+
#include <interface/handler.h>
8+
#include <wallet/wallet.h>
9+
10+
#include <memory>
11+
12+
namespace interface {
13+
namespace {
14+
15+
class WalletImpl : public Wallet
16+
{
17+
public:
18+
WalletImpl(CWallet& wallet) : m_wallet(wallet) {}
19+
20+
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
21+
{
22+
return MakeHandler(m_wallet.ShowProgress.connect(fn));
23+
}
24+
25+
CWallet& m_wallet;
26+
};
27+
28+
} // namespace
29+
30+
std::unique_ptr<Wallet> MakeWallet(CWallet& wallet) { return MakeUnique<WalletImpl>(wallet); }
31+
32+
} // namespace interface

src/interface/wallet.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2018 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_INTERFACE_WALLET_H
6+
#define BITCOIN_INTERFACE_WALLET_H
7+
8+
#include <functional>
9+
#include <memory>
10+
#include <string>
11+
12+
class CWallet;
13+
14+
namespace interface {
15+
16+
class Handler;
17+
18+
//! Interface for accessing a wallet.
19+
class Wallet
20+
{
21+
public:
22+
virtual ~Wallet() {}
23+
24+
//! Register handler for show progress messages.
25+
using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
26+
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
27+
};
28+
29+
//! Return implementation of Wallet interface. This function will be undefined
30+
//! in builds where ENABLE_WALLET is false.
31+
std::unique_ptr<Wallet> MakeWallet(CWallet& wallet);
32+
33+
} // namespace interface
34+
35+
#endif // BITCOIN_INTERFACE_WALLET_H

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
375375

376376
void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
377377
{
378-
SplashScreen *splash = new SplashScreen(0, networkStyle);
378+
SplashScreen *splash = new SplashScreen(m_node, 0, networkStyle);
379379
// We don't hold a direct pointer to the splash screen after creation, but the splash
380380
// screen will take care of deleting itself when slotFinish happens.
381381
splash->show();

src/qt/splashscreen.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@
1212

1313
#include <clientversion.h>
1414
#include <init.h>
15+
#include <interface/handler.h>
16+
#include <interface/node.h>
17+
#include <interface/wallet.h>
1518
#include <util.h>
1619
#include <ui_interface.h>
1720
#include <version.h>
1821

19-
#ifdef ENABLE_WALLET
20-
#include <wallet/wallet.h>
21-
#endif
22-
2322
#include <QApplication>
2423
#include <QCloseEvent>
2524
#include <QDesktopWidget>
2625
#include <QPainter>
2726
#include <QRadialGradient>
2827

29-
SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
30-
QWidget(0, f), curAlignment(0)
28+
SplashScreen::SplashScreen(interface::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
29+
QWidget(0, f), curAlignment(0), m_node(node)
3130
{
3231
// set reference point, paddings
3332
int paddingRight = 50;
@@ -143,7 +142,7 @@ bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
143142
if (ev->type() == QEvent::KeyPress) {
144143
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
145144
if(keyEvent->text()[0] == 'q') {
146-
StartShutdown();
145+
m_node.startShutdown();
147146
}
148147
}
149148
return QObject::eventFilter(obj, ev);
@@ -177,35 +176,34 @@ static void ShowProgress(SplashScreen *splash, const std::string &title, int nPr
177176
: _("press q to shutdown")) +
178177
strprintf("\n%d", nProgress) + "%");
179178
}
180-
181179
#ifdef ENABLE_WALLET
182-
void SplashScreen::ConnectWallet(CWallet* wallet)
180+
void SplashScreen::ConnectWallet(std::unique_ptr<interface::Wallet> wallet)
183181
{
184-
wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, false));
185-
connectedWallets.push_back(wallet);
182+
m_connected_wallet_handlers.emplace_back(wallet->handleShowProgress(boost::bind(ShowProgress, this, _1, _2, false)));
183+
m_connected_wallets.emplace_back(std::move(wallet));
186184
}
187185
#endif
188186

189187
void SplashScreen::subscribeToCoreSignals()
190188
{
191189
// Connect signals to client
192-
uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
193-
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, _3));
190+
m_handler_init_message = m_node.handleInitMessage(boost::bind(InitMessage, this, _1));
191+
m_handler_show_progress = m_node.handleShowProgress(boost::bind(ShowProgress, this, _1, _2, _3));
194192
#ifdef ENABLE_WALLET
195-
uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
193+
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interface::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
196194
#endif
197195
}
198196

199197
void SplashScreen::unsubscribeFromCoreSignals()
200198
{
201199
// Disconnect signals from client
202-
uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1));
203-
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, _3));
204-
#ifdef ENABLE_WALLET
205-
for (CWallet* const & pwallet : connectedWallets) {
206-
pwallet->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, false));
200+
m_handler_init_message->disconnect();
201+
m_handler_show_progress->disconnect();
202+
for (auto& handler : m_connected_wallet_handlers) {
203+
handler->disconnect();
207204
}
208-
#endif
205+
m_connected_wallet_handlers.clear();
206+
m_connected_wallets.clear();
209207
}
210208

211209
void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
@@ -227,6 +225,6 @@ void SplashScreen::paintEvent(QPaintEvent *event)
227225

228226
void SplashScreen::closeEvent(QCloseEvent *event)
229227
{
230-
StartShutdown(); // allows an "emergency" shutdown during startup
228+
m_node.startShutdown(); // allows an "emergency" shutdown during startup
231229
event->ignore();
232230
}

src/qt/splashscreen.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
#include <functional>
99
#include <QSplashScreen>
1010

11-
class CWallet;
11+
#include <memory>
12+
1213
class NetworkStyle;
1314

15+
namespace interface {
16+
class Handler;
17+
class Node;
18+
class Wallet;
19+
};
20+
1421
/** Class for the splashscreen with information of the running client.
1522
*
1623
* @note this is intentionally not a QSplashScreen. Bitcoin Core initialization
@@ -22,7 +29,7 @@ class SplashScreen : public QWidget
2229
Q_OBJECT
2330

2431
public:
25-
explicit SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle);
32+
explicit SplashScreen(interface::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle);
2633
~SplashScreen();
2734

2835
protected:
@@ -45,14 +52,19 @@ public Q_SLOTS:
4552
/** Disconnect core signals to splash screen */
4653
void unsubscribeFromCoreSignals();
4754
/** Connect wallet signals to splash screen */
48-
void ConnectWallet(CWallet*);
55+
void ConnectWallet(std::unique_ptr<interface::Wallet> wallet);
4956

5057
QPixmap pixmap;
5158
QString curMessage;
5259
QColor curColor;
5360
int curAlignment;
5461

55-
QList<CWallet*> connectedWallets;
62+
interface::Node& m_node;
63+
std::unique_ptr<interface::Handler> m_handler_init_message;
64+
std::unique_ptr<interface::Handler> m_handler_show_progress;
65+
std::unique_ptr<interface::Handler> m_handler_load_wallet;
66+
std::list<std::unique_ptr<interface::Wallet>> m_connected_wallets;
67+
std::list<std::unique_ptr<interface::Handler>> m_connected_wallet_handlers;
5668
};
5769

5870
#endif // BITCOIN_QT_SPLASHSCREEN_H

0 commit comments

Comments
 (0)