Skip to content

Commit 6314433

Browse files
committed
Merge #15101: gui: Add WalletController
0dd9bde gui: Refactor to use WalletController (João Barbosa) 8fa271f gui: Add WalletController (João Barbosa) cefb399 gui: Use AutoConnection for WalletModel::unload signal (João Barbosa) Pull request description: This PR is a subset of the work done in the context of #13100. This change consists in extracting from the application class the code that manages the wallet models. The role of the `WalletController` instance is to coordinate wallet operations and the window. Tree-SHA512: 6a824054376730eb7d16c643dd2003f5f60778e8ad3af707b82bc12c48438db179ca4446316b28fb17b206f4b9aba8998419aab8c5dd1f7c32467015732b5094
2 parents cd42553 + 0dd9bde commit 6314433

File tree

8 files changed

+202
-63
lines changed

8 files changed

+202
-63
lines changed

src/Makefile.qt.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ QT_MOC_CPP = \
157157
qt/moc_transactiontablemodel.cpp \
158158
qt/moc_transactionview.cpp \
159159
qt/moc_utilitydialog.cpp \
160+
qt/moc_walletcontroller.cpp \
160161
qt/moc_walletframe.cpp \
161162
qt/moc_walletmodel.cpp \
162163
qt/moc_walletview.cpp
@@ -237,6 +238,7 @@ BITCOIN_QT_H = \
237238
qt/transactiontablemodel.h \
238239
qt/transactionview.h \
239240
qt/utilitydialog.h \
241+
qt/walletcontroller.h \
240242
qt/walletframe.h \
241243
qt/walletmodel.h \
242244
qt/walletmodeltransaction.h \
@@ -350,6 +352,7 @@ BITCOIN_QT_WALLET_CPP = \
350352
qt/transactionrecord.cpp \
351353
qt/transactiontablemodel.cpp \
352354
qt/transactionview.cpp \
355+
qt/walletcontroller.cpp \
353356
qt/walletframe.cpp \
354357
qt/walletmodel.cpp \
355358
qt/walletmodeltransaction.cpp \

src/qt/bitcoin.cpp

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#ifdef ENABLE_WALLET
2626
#include <qt/paymentserver.h>
27-
#include <qt/walletmodel.h>
27+
#include <qt/walletcontroller.h>
2828
#endif
2929

3030
#include <interfaces/handler.h>
@@ -184,10 +184,6 @@ BitcoinApplication::BitcoinApplication(interfaces::Node& node, int &argc, char *
184184
clientModel(nullptr),
185185
window(nullptr),
186186
pollShutdownTimer(nullptr),
187-
#ifdef ENABLE_WALLET
188-
paymentServer(nullptr),
189-
m_wallet_models(),
190-
#endif
191187
returnValue(0),
192188
platformStyle(nullptr)
193189
{
@@ -315,11 +311,8 @@ void BitcoinApplication::requestShutdown()
315311
pollShutdownTimer->stop();
316312

317313
#ifdef ENABLE_WALLET
318-
window->removeAllWallets();
319-
for (const WalletModel* walletModel : m_wallet_models) {
320-
delete walletModel;
321-
}
322-
m_wallet_models.clear();
314+
delete m_wallet_controller;
315+
m_wallet_controller = nullptr;
323316
#endif
324317
delete clientModel;
325318
clientModel = nullptr;
@@ -330,35 +323,6 @@ void BitcoinApplication::requestShutdown()
330323
Q_EMIT requestedShutdown();
331324
}
332325

333-
void BitcoinApplication::addWallet(WalletModel* walletModel)
334-
{
335-
#ifdef ENABLE_WALLET
336-
window->addWallet(walletModel);
337-
338-
if (m_wallet_models.empty()) {
339-
window->setCurrentWallet(walletModel);
340-
}
341-
342-
#ifdef ENABLE_BIP70
343-
connect(walletModel, &WalletModel::coinsSent,
344-
paymentServer, &PaymentServer::fetchPaymentACK);
345-
#endif
346-
connect(walletModel, &WalletModel::unload, this, &BitcoinApplication::removeWallet);
347-
348-
m_wallet_models.push_back(walletModel);
349-
#endif
350-
}
351-
352-
void BitcoinApplication::removeWallet()
353-
{
354-
#ifdef ENABLE_WALLET
355-
WalletModel* walletModel = static_cast<WalletModel*>(sender());
356-
m_wallet_models.erase(std::find(m_wallet_models.begin(), m_wallet_models.end(), walletModel));
357-
window->removeWallet(walletModel);
358-
walletModel->deleteLater();
359-
#endif
360-
}
361-
362326
void BitcoinApplication::initializeResult(bool success)
363327
{
364328
qDebug() << __func__ << ": Initialization result: " << success;
@@ -369,26 +333,22 @@ void BitcoinApplication::initializeResult(bool success)
369333
// Log this only after AppInitMain finishes, as then logging setup is guaranteed complete
370334
qWarning() << "Platform customization:" << platformStyle->getName();
371335
#ifdef ENABLE_WALLET
336+
m_wallet_controller = new WalletController(m_node, platformStyle, optionsModel, this);
372337
#ifdef ENABLE_BIP70
373338
PaymentServer::LoadRootCAs();
374339
#endif
375-
if (paymentServer) paymentServer->setOptionsModel(optionsModel);
340+
if (paymentServer) {
341+
paymentServer->setOptionsModel(optionsModel);
342+
#ifdef ENABLE_BIP70
343+
connect(m_wallet_controller, &WalletController::coinsSent, paymentServer, &PaymentServer::fetchPaymentACK);
344+
#endif
345+
}
376346
#endif
377347

378348
clientModel = new ClientModel(m_node, optionsModel);
379349
window->setClientModel(clientModel);
380-
381350
#ifdef ENABLE_WALLET
382-
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
383-
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel, nullptr);
384-
// Fix wallet model thread affinity.
385-
wallet_model->moveToThread(thread());
386-
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
387-
});
388-
389-
for (auto& wallet : m_node.getWallets()) {
390-
addWallet(new WalletModel(std::move(wallet), m_node, platformStyle, optionsModel));
391-
}
351+
window->setWalletController(m_wallet_controller);
392352
#endif
393353

394354
// If -min option passed, start window minimized (iconified) or minimized to tray
@@ -492,9 +452,6 @@ int GuiMain(int argc, char* argv[])
492452
// IMPORTANT if it is no longer a typedef use the normal variant above
493453
qRegisterMetaType< CAmount >("CAmount");
494454
qRegisterMetaType< std::function<void()> >("std::function<void()>");
495-
#ifdef ENABLE_WALLET
496-
qRegisterMetaType<WalletModel*>("WalletModel*");
497-
#endif
498455

499456
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
500457
// Command-line options take precedence:

src/qt/bitcoin.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class NetworkStyle;
1919
class OptionsModel;
2020
class PaymentServer;
2121
class PlatformStyle;
22+
class WalletController;
2223
class WalletModel;
2324

2425
namespace interfaces {
@@ -93,8 +94,6 @@ public Q_SLOTS:
9394
void shutdownResult();
9495
/// Handle runaway exceptions. Shows a message box with the problem and quits the program.
9596
void handleRunawayException(const QString &message);
96-
void addWallet(WalletModel* walletModel);
97-
void removeWallet();
9897

9998
Q_SIGNALS:
10099
void requestedInitialize();
@@ -110,9 +109,8 @@ public Q_SLOTS:
110109
BitcoinGUI *window;
111110
QTimer *pollShutdownTimer;
112111
#ifdef ENABLE_WALLET
113-
PaymentServer* paymentServer;
114-
std::vector<WalletModel*> m_wallet_models;
115-
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
112+
PaymentServer* paymentServer{nullptr};
113+
WalletController* m_wallet_controller{nullptr};
116114
#endif
117115
int returnValue;
118116
const PlatformStyle *platformStyle;

src/qt/bitcoingui.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <qt/utilitydialog.h>
2020

2121
#ifdef ENABLE_WALLET
22+
#include <qt/walletcontroller.h>
2223
#include <qt/walletframe.h>
2324
#include <qt/walletmodel.h>
2425
#include <qt/walletview.h>
@@ -565,18 +566,33 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
565566
}
566567

567568
#ifdef ENABLE_WALLET
569+
void BitcoinGUI::setWalletController(WalletController* wallet_controller)
570+
{
571+
assert(!m_wallet_controller);
572+
assert(wallet_controller);
573+
574+
m_wallet_controller = wallet_controller;
575+
576+
connect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
577+
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);
578+
579+
for (WalletModel* wallet_model : m_wallet_controller->getWallets()) {
580+
addWallet(wallet_model);
581+
}
582+
}
583+
568584
void BitcoinGUI::addWallet(WalletModel* walletModel)
569585
{
570586
if (!walletFrame) return;
571587
const QString display_name = walletModel->getDisplayName();
572588
setWalletActionsEnabled(true);
589+
rpcConsole->addWallet(walletModel);
590+
walletFrame->addWallet(walletModel);
573591
m_wallet_selector->addItem(display_name, QVariant::fromValue(walletModel));
574592
if (m_wallet_selector->count() == 2) {
575593
m_wallet_selector_label_action->setVisible(true);
576594
m_wallet_selector_action->setVisible(true);
577595
}
578-
rpcConsole->addWallet(walletModel);
579-
walletFrame->addWallet(walletModel);
580596
}
581597

582598
void BitcoinGUI::removeWallet(WalletModel* walletModel)
@@ -599,13 +615,19 @@ void BitcoinGUI::setCurrentWallet(WalletModel* wallet_model)
599615
{
600616
if (!walletFrame) return;
601617
walletFrame->setCurrentWallet(wallet_model);
618+
for (int index = 0; index < m_wallet_selector->count(); ++index) {
619+
if (m_wallet_selector->itemData(index).value<WalletModel*>() == wallet_model) {
620+
m_wallet_selector->setCurrentIndex(index);
621+
break;
622+
}
623+
}
602624
updateWindowTitle();
603625
}
604626

605627
void BitcoinGUI::setCurrentWalletBySelectorIndex(int index)
606628
{
607629
WalletModel* wallet_model = m_wallet_selector->itemData(index).value<WalletModel*>();
608-
setCurrentWallet(wallet_model);
630+
if (wallet_model) setCurrentWallet(wallet_model);
609631
}
610632

611633
void BitcoinGUI::removeAllWallets()

src/qt/bitcoingui.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class PlatformStyle;
3333
class RPCConsole;
3434
class SendCoinsRecipient;
3535
class UnitDisplayStatusBarControl;
36+
class WalletController;
3637
class WalletFrame;
3738
class WalletModel;
3839
class HelpMessageDialog;
@@ -74,6 +75,9 @@ class BitcoinGUI : public QMainWindow
7475
The client model represents the part of the core that communicates with the P2P network, and is wallet-agnostic.
7576
*/
7677
void setClientModel(ClientModel *clientModel);
78+
#ifdef ENABLE_WALLET
79+
void setWalletController(WalletController* wallet_controller);
80+
#endif
7781

7882
#ifdef ENABLE_WALLET
7983
/** Set the wallet model.
@@ -101,6 +105,7 @@ class BitcoinGUI : public QMainWindow
101105

102106
private:
103107
interfaces::Node& m_node;
108+
WalletController* m_wallet_controller{nullptr};
104109
std::unique_ptr<interfaces::Handler> m_handler_message_box;
105110
std::unique_ptr<interfaces::Handler> m_handler_question;
106111
ClientModel* clientModel = nullptr;

src/qt/walletcontroller.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) 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+
#include <qt/walletcontroller.h>
6+
7+
#include <interfaces/handler.h>
8+
#include <interfaces/node.h>
9+
10+
#include <algorithm>
11+
12+
#include <QMutexLocker>
13+
#include <QThread>
14+
15+
WalletController::WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent)
16+
: QObject(parent)
17+
, m_node(node)
18+
, m_platform_style(platform_style)
19+
, m_options_model(options_model)
20+
{
21+
m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
22+
getOrCreateWallet(std::move(wallet));
23+
});
24+
25+
for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.getWallets()) {
26+
getOrCreateWallet(std::move(wallet));
27+
}
28+
}
29+
30+
// Not using the default destructor because not all member types definitions are
31+
// available in the header, just forward declared.
32+
WalletController::~WalletController() {}
33+
34+
std::vector<WalletModel*> WalletController::getWallets() const
35+
{
36+
QMutexLocker locker(&m_mutex);
37+
return m_wallets;
38+
}
39+
40+
WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
41+
{
42+
QMutexLocker locker(&m_mutex);
43+
44+
// Return model instance if exists.
45+
if (!m_wallets.empty()) {
46+
std::string name = wallet->getWalletName();
47+
for (WalletModel* wallet_model : m_wallets) {
48+
if (wallet_model->wallet().getWalletName() == name) {
49+
return wallet_model;
50+
}
51+
}
52+
}
53+
54+
// Instantiate model and register it.
55+
WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, m_platform_style, m_options_model, nullptr);
56+
m_wallets.push_back(wallet_model);
57+
58+
connect(wallet_model, &WalletModel::unload, [this, wallet_model] {
59+
removeAndDeleteWallet(wallet_model);
60+
});
61+
62+
// Re-emit coinsSent signal from wallet model.
63+
connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
64+
65+
// Notify walletAdded signal on the GUI thread.
66+
if (QThread::currentThread() == thread()) {
67+
addWallet(wallet_model);
68+
} else {
69+
// Handler callback runs in a different thread so fix wallet model thread affinity.
70+
wallet_model->moveToThread(thread());
71+
QMetaObject::invokeMethod(this, "addWallet", Qt::QueuedConnection, Q_ARG(WalletModel*, wallet_model));
72+
}
73+
74+
return wallet_model;
75+
}
76+
77+
void WalletController::addWallet(WalletModel* wallet_model)
78+
{
79+
// Take ownership of the wallet model and register it.
80+
wallet_model->setParent(this);
81+
Q_EMIT walletAdded(wallet_model);
82+
}
83+
84+
void WalletController::removeAndDeleteWallet(WalletModel* wallet_model)
85+
{
86+
// Unregister wallet model.
87+
{
88+
QMutexLocker locker(&m_mutex);
89+
m_wallets.erase(std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
90+
}
91+
Q_EMIT walletRemoved(wallet_model);
92+
// Currently this can trigger the unload since the model can hold the last
93+
// CWallet shared pointer.
94+
delete wallet_model;
95+
}

0 commit comments

Comments
 (0)