|
| 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