Skip to content

Commit 0dd9bde

Browse files
committed
gui: Refactor to use WalletController
1 parent 8fa271f commit 0dd9bde

File tree

4 files changed

+44
-62
lines changed

4 files changed

+44
-62
lines changed

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
{
@@ -316,11 +312,8 @@ void BitcoinApplication::requestShutdown()
316312
pollShutdownTimer->stop();
317313

318314
#ifdef ENABLE_WALLET
319-
window->removeAllWallets();
320-
for (const WalletModel* walletModel : m_wallet_models) {
321-
delete walletModel;
322-
}
323-
m_wallet_models.clear();
315+
delete m_wallet_controller;
316+
m_wallet_controller = nullptr;
324317
#endif
325318
delete clientModel;
326319
clientModel = nullptr;
@@ -331,35 +324,6 @@ void BitcoinApplication::requestShutdown()
331324
Q_EMIT requestedShutdown();
332325
}
333326

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

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

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

500457
/// 2. Parse command-line options. We do this after qt in order to show an error if there are problems parsing these
501458
// 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();
@@ -111,9 +110,8 @@ public Q_SLOTS:
111110
BitcoinGUI *window;
112111
QTimer *pollShutdownTimer;
113112
#ifdef ENABLE_WALLET
114-
PaymentServer* paymentServer;
115-
std::vector<WalletModel*> m_wallet_models;
116-
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
113+
PaymentServer* paymentServer{nullptr};
114+
WalletController* m_wallet_controller{nullptr};
117115
#endif
118116
int returnValue;
119117
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;

0 commit comments

Comments
 (0)