Remove direct bitcoin calls from qt/splashscreen.cpp

This commit is contained in:
Russell Yanofsky 2017-04-17 15:10:47 -04:00 committed by John Newbery
parent c2f672fb19
commit 5fba3af21e
10 changed files with 139 additions and 29 deletions

View file

@ -7,6 +7,7 @@ DIST_SUBDIRS = secp256k1 univalue
AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(GPROF_LDFLAGS) $(SANITIZER_LDFLAGS) AM_LDFLAGS = $(PTHREAD_CFLAGS) $(LIBTOOL_LDFLAGS) $(HARDENED_LDFLAGS) $(GPROF_LDFLAGS) $(SANITIZER_LDFLAGS)
AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS) $(GPROF_CXXFLAGS) $(SANITIZER_CXXFLAGS) AM_CXXFLAGS = $(HARDENED_CXXFLAGS) $(ERROR_CXXFLAGS) $(GPROF_CXXFLAGS) $(SANITIZER_CXXFLAGS)
AM_CPPFLAGS = $(HARDENED_CPPFLAGS) AM_CPPFLAGS = $(HARDENED_CPPFLAGS)
AM_LIBTOOLFLAGS = --preserve-dup-deps
EXTRA_LIBRARIES = EXTRA_LIBRARIES =
if EMBEDDED_UNIVALUE if EMBEDDED_UNIVALUE
@ -106,6 +107,7 @@ BITCOIN_CORE_H = \
init.h \ init.h \
interface/handler.h \ interface/handler.h \
interface/node.h \ interface/node.h \
interface/wallet.h \
key.h \ key.h \
key_io.h \ key_io.h \
keystore.h \ keystore.h \
@ -247,6 +249,7 @@ endif
libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_wallet_a_SOURCES = \ libbitcoin_wallet_a_SOURCES = \
interface/wallet.cpp \
wallet/crypter.cpp \ wallet/crypter.cpp \
wallet/db.cpp \ wallet/db.cpp \
wallet/feebumper.cpp \ wallet/feebumper.cpp \

View file

@ -402,7 +402,7 @@ if TARGET_WINDOWS
endif endif
qt_bitcoin_qt_LDADD = qt/libbitcoinqt.a $(LIBBITCOIN_SERVER) qt_bitcoin_qt_LDADD = qt/libbitcoinqt.a $(LIBBITCOIN_SERVER)
if ENABLE_WALLET if ENABLE_WALLET
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET) qt_bitcoin_qt_LDADD += $(LIBBITCOIN_UTIL) $(LIBBITCOIN_WALLET)
endif endif
if ENABLE_ZMQ if ENABLE_ZMQ
qt_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS) qt_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)
@ -411,7 +411,7 @@ qt_bitcoin_qt_LDADD += $(LIBBITCOIN_CLI) $(LIBBITCOIN_COMMON) $(LIBBITCOIN_UTIL)
$(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \ $(BOOST_LIBS) $(QT_LIBS) $(QT_DBUS_LIBS) $(QR_LIBS) $(PROTOBUF_LIBS) $(BDB_LIBS) $(SSL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBSECP256K1) \
$(EVENT_PTHREADS_LIBS) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS) $(EVENT_LIBS)
qt_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) qt_bitcoin_qt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(QT_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
qt_bitcoin_qt_LIBTOOLFLAGS = --tag CXX qt_bitcoin_qt_LIBTOOLFLAGS = $(AM_LIBTOOLFLAGS) --tag CXX
#locale/foo.ts -> locale/foo.qm #locale/foo.ts -> locale/foo.qm
QT_QM=$(QT_TS:.ts=.qm) QT_QM=$(QT_TS:.ts=.qm)

View file

@ -52,7 +52,7 @@ nodist_qt_test_test_bitcoin_qt_SOURCES = $(TEST_QT_MOC_CPP)
qt_test_test_bitcoin_qt_LDADD = $(LIBBITCOINQT) $(LIBBITCOIN_SERVER) qt_test_test_bitcoin_qt_LDADD = $(LIBBITCOINQT) $(LIBBITCOIN_SERVER)
if ENABLE_WALLET if ENABLE_WALLET
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_WALLET) qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_UTIL) $(LIBBITCOIN_WALLET)
endif endif
if ENABLE_ZMQ if ENABLE_ZMQ
qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS) qt_test_test_bitcoin_qt_LDADD += $(LIBBITCOIN_ZMQ) $(ZMQ_LIBS)

View file

@ -7,6 +7,7 @@
#include <chainparams.h> #include <chainparams.h>
#include <init.h> #include <init.h>
#include <interface/handler.h> #include <interface/handler.h>
#include <interface/wallet.h>
#include <net.h> #include <net.h>
#include <netaddress.h> #include <netaddress.h>
#include <netbase.h> #include <netbase.h>
@ -15,8 +16,19 @@
#include <util.h> #include <util.h>
#include <warnings.h> #include <warnings.h>
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#ifdef ENABLE_WALLET
#define CHECK_WALLET(x) x
#else
#define CHECK_WALLET(x) throw std::logic_error("Wallet function called in non-wallet build.")
#endif
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
class CWallet;
namespace interface { namespace interface {
namespace { namespace {
@ -69,6 +81,15 @@ class NodeImpl : public Node
{ {
return MakeHandler(::uiInterface.ThreadSafeQuestion.connect(fn)); return MakeHandler(::uiInterface.ThreadSafeQuestion.connect(fn));
} }
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
{
return MakeHandler(::uiInterface.ShowProgress.connect(fn));
}
std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
{
CHECK_WALLET(
return MakeHandler(::uiInterface.LoadWallet.connect([fn](CWallet* wallet) { fn(MakeWallet(*wallet)); })));
}
}; };
} // namespace } // namespace

View file

@ -17,6 +17,7 @@ class proxyType;
namespace interface { namespace interface {
class Handler; class Handler;
class Wallet;
//! Top-level interface for a bitcoin node (bitcoind process). //! Top-level interface for a bitcoin node (bitcoind process).
class Node class Node
@ -87,6 +88,14 @@ public:
const std::string& caption, const std::string& caption,
unsigned int style)>; unsigned int style)>;
virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0; virtual std::unique_ptr<Handler> handleQuestion(QuestionFn fn) = 0;
//! Register handler for progress messages.
using ShowProgressFn = std::function<void(const std::string& title, int progress, bool resume_possible)>;
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
//! Register handler for load wallet messages.
using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
}; };
//! Return implementation of Node interface. //! Return implementation of Node interface.

32
src/interface/wallet.cpp Normal file
View file

@ -0,0 +1,32 @@
// Copyright (c) 2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <interface/wallet.h>
#include <interface/handler.h>
#include <wallet/wallet.h>
#include <memory>
namespace interface {
namespace {
class WalletImpl : public Wallet
{
public:
WalletImpl(CWallet& wallet) : m_wallet(wallet) {}
std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
{
return MakeHandler(m_wallet.ShowProgress.connect(fn));
}
CWallet& m_wallet;
};
} // namespace
std::unique_ptr<Wallet> MakeWallet(CWallet& wallet) { return MakeUnique<WalletImpl>(wallet); }
} // namespace interface

35
src/interface/wallet.h Normal file
View file

@ -0,0 +1,35 @@
// Copyright (c) 2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_INTERFACE_WALLET_H
#define BITCOIN_INTERFACE_WALLET_H
#include <functional>
#include <memory>
#include <string>
class CWallet;
namespace interface {
class Handler;
//! Interface for accessing a wallet.
class Wallet
{
public:
virtual ~Wallet() {}
//! Register handler for show progress messages.
using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
};
//! Return implementation of Wallet interface. This function will be undefined
//! in builds where ENABLE_WALLET is false.
std::unique_ptr<Wallet> MakeWallet(CWallet& wallet);
} // namespace interface
#endif // BITCOIN_INTERFACE_WALLET_H

View file

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

View file

@ -12,22 +12,21 @@
#include <clientversion.h> #include <clientversion.h>
#include <init.h> #include <init.h>
#include <interface/handler.h>
#include <interface/node.h>
#include <interface/wallet.h>
#include <util.h> #include <util.h>
#include <ui_interface.h> #include <ui_interface.h>
#include <version.h> #include <version.h>
#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#endif
#include <QApplication> #include <QApplication>
#include <QCloseEvent> #include <QCloseEvent>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QPainter> #include <QPainter>
#include <QRadialGradient> #include <QRadialGradient>
SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) : SplashScreen::SplashScreen(interface::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
QWidget(0, f), curAlignment(0) QWidget(0, f), curAlignment(0), m_node(node)
{ {
// set reference point, paddings // set reference point, paddings
int paddingRight = 50; int paddingRight = 50;
@ -143,7 +142,7 @@ bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
if (ev->type() == QEvent::KeyPress) { if (ev->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
if(keyEvent->text()[0] == 'q') { if(keyEvent->text()[0] == 'q') {
StartShutdown(); m_node.startShutdown();
} }
} }
return QObject::eventFilter(obj, ev); return QObject::eventFilter(obj, ev);
@ -177,35 +176,34 @@ static void ShowProgress(SplashScreen *splash, const std::string &title, int nPr
: _("press q to shutdown")) + : _("press q to shutdown")) +
strprintf("\n%d", nProgress) + "%"); strprintf("\n%d", nProgress) + "%");
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
void SplashScreen::ConnectWallet(CWallet* wallet) void SplashScreen::ConnectWallet(std::unique_ptr<interface::Wallet> wallet)
{ {
wallet->ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, false)); m_connected_wallet_handlers.emplace_back(wallet->handleShowProgress(boost::bind(ShowProgress, this, _1, _2, false)));
connectedWallets.push_back(wallet); m_connected_wallets.emplace_back(std::move(wallet));
} }
#endif #endif
void SplashScreen::subscribeToCoreSignals() void SplashScreen::subscribeToCoreSignals()
{ {
// Connect signals to client // Connect signals to client
uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1)); m_handler_init_message = m_node.handleInitMessage(boost::bind(InitMessage, this, _1));
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2, _3)); m_handler_show_progress = m_node.handleShowProgress(boost::bind(ShowProgress, this, _1, _2, _3));
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1)); m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interface::Wallet> wallet) { ConnectWallet(std::move(wallet)); });
#endif #endif
} }
void SplashScreen::unsubscribeFromCoreSignals() void SplashScreen::unsubscribeFromCoreSignals()
{ {
// Disconnect signals from client // Disconnect signals from client
uiInterface.InitMessage.disconnect(boost::bind(InitMessage, this, _1)); m_handler_init_message->disconnect();
uiInterface.ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, _3)); m_handler_show_progress->disconnect();
#ifdef ENABLE_WALLET for (auto& handler : m_connected_wallet_handlers) {
for (CWallet* const & pwallet : connectedWallets) { handler->disconnect();
pwallet->ShowProgress.disconnect(boost::bind(ShowProgress, this, _1, _2, false));
} }
#endif m_connected_wallet_handlers.clear();
m_connected_wallets.clear();
} }
void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color) void SplashScreen::showMessage(const QString &message, int alignment, const QColor &color)
@ -227,6 +225,6 @@ void SplashScreen::paintEvent(QPaintEvent *event)
void SplashScreen::closeEvent(QCloseEvent *event) void SplashScreen::closeEvent(QCloseEvent *event)
{ {
StartShutdown(); // allows an "emergency" shutdown during startup m_node.startShutdown(); // allows an "emergency" shutdown during startup
event->ignore(); event->ignore();
} }

View file

@ -8,9 +8,16 @@
#include <functional> #include <functional>
#include <QSplashScreen> #include <QSplashScreen>
class CWallet; #include <memory>
class NetworkStyle; class NetworkStyle;
namespace interface {
class Handler;
class Node;
class Wallet;
};
/** Class for the splashscreen with information of the running client. /** Class for the splashscreen with information of the running client.
* *
* @note this is intentionally not a QSplashScreen. Bitcoin Core initialization * @note this is intentionally not a QSplashScreen. Bitcoin Core initialization
@ -22,7 +29,7 @@ class SplashScreen : public QWidget
Q_OBJECT Q_OBJECT
public: public:
explicit SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle); explicit SplashScreen(interface::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle);
~SplashScreen(); ~SplashScreen();
protected: protected:
@ -45,14 +52,19 @@ private:
/** Disconnect core signals to splash screen */ /** Disconnect core signals to splash screen */
void unsubscribeFromCoreSignals(); void unsubscribeFromCoreSignals();
/** Connect wallet signals to splash screen */ /** Connect wallet signals to splash screen */
void ConnectWallet(CWallet*); void ConnectWallet(std::unique_ptr<interface::Wallet> wallet);
QPixmap pixmap; QPixmap pixmap;
QString curMessage; QString curMessage;
QColor curColor; QColor curColor;
int curAlignment; int curAlignment;
QList<CWallet*> connectedWallets; interface::Node& m_node;
std::unique_ptr<interface::Handler> m_handler_init_message;
std::unique_ptr<interface::Handler> m_handler_show_progress;
std::unique_ptr<interface::Handler> m_handler_load_wallet;
std::list<std::unique_ptr<interface::Wallet>> m_connected_wallets;
std::list<std::unique_ptr<interface::Handler>> m_connected_wallet_handlers;
}; };
#endif // BITCOIN_QT_SPLASHSCREEN_H #endif // BITCOIN_QT_SPLASHSCREEN_H