Skip to content

Commit 8fa271f

Browse files
committed
gui: Add WalletController
1 parent cefb399 commit 8fa271f

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
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/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+
}

src/qt/walletcontroller.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#ifndef BITCOIN_QT_WALLETCONTROLLER_H
6+
#define BITCOIN_QT_WALLETCONTROLLER_H
7+
8+
#include <qt/walletmodel.h>
9+
#include <sync.h>
10+
11+
#include <list>
12+
#include <memory>
13+
#include <vector>
14+
15+
#include <QMutex>
16+
17+
class OptionsModel;
18+
class PlatformStyle;
19+
20+
namespace interfaces {
21+
class Handler;
22+
class Node;
23+
} // namespace interfaces
24+
25+
/**
26+
* Controller between interfaces::Node, WalletModel instances and the GUI.
27+
*/
28+
class WalletController : public QObject
29+
{
30+
Q_OBJECT
31+
32+
WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
33+
void removeAndDeleteWallet(WalletModel* wallet_model);
34+
35+
public:
36+
WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent);
37+
~WalletController();
38+
39+
std::vector<WalletModel*> getWallets() const;
40+
41+
private Q_SLOTS:
42+
void addWallet(WalletModel* wallet_model);
43+
44+
Q_SIGNALS:
45+
void walletAdded(WalletModel* wallet_model);
46+
void walletRemoved(WalletModel* wallet_model);
47+
48+
void coinsSent(WalletModel* wallet_model, SendCoinsRecipient recipient, QByteArray transaction);
49+
50+
private:
51+
interfaces::Node& m_node;
52+
const PlatformStyle* const m_platform_style;
53+
OptionsModel* const m_options_model;
54+
mutable QMutex m_mutex;
55+
std::vector<WalletModel*> m_wallets;
56+
std::unique_ptr<interfaces::Handler> m_handler_load_wallet;
57+
};
58+
59+
#endif // BITCOIN_QT_WALLETCONTROLLER_H

0 commit comments

Comments
 (0)