Bitcoin ABC 0.30.7
P2P Digital Currency
walletcontroller.cpp
Go to the documentation of this file.
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
6
8#include <qt/clientmodel.h>
10#include <qt/guiconstants.h>
11#include <qt/guiutil.h>
12
13#include <interfaces/handler.h>
14#include <interfaces/node.h>
15#include <util/string.h>
16#include <util/threadnames.h>
17#include <util/translation.h>
18#include <wallet/wallet.h>
19
20#include <QApplication>
21#include <QMutexLocker>
22#include <QTimer>
23#include <QWindow>
24
25#include <algorithm>
26
28 const PlatformStyle *platform_style,
29 QObject *parent)
30 : QObject(parent), m_activity_thread(new QThread(this)),
31 m_activity_worker(new QObject), m_client_model(client_model),
32 m_node(client_model.node()), m_platform_style(platform_style),
33 m_options_model(client_model.getOptionsModel()) {
35 [this](std::unique_ptr<interfaces::Wallet> wallet) {
36 getOrCreateWallet(std::move(wallet));
37 });
38
39 for (std::unique_ptr<interfaces::Wallet> &wallet :
41 getOrCreateWallet(std::move(wallet));
42 }
43
45 m_activity_thread->start();
46 QTimer::singleShot(0, m_activity_worker,
47 []() { util::ThreadRename("qt-walletctrl"); });
48}
49
50// Not using the default destructor because not all member types definitions are
51// available in the header, just forward declared.
53 m_activity_thread->quit();
54 m_activity_thread->wait();
55 delete m_activity_worker;
56}
57
58std::vector<WalletModel *> WalletController::getOpenWallets() const {
59 QMutexLocker locker(&m_mutex);
60 return m_wallets;
61}
62
63std::map<std::string, bool> WalletController::listWalletDir() const {
64 QMutexLocker locker(&m_mutex);
65 std::map<std::string, bool> wallets;
66 for (const std::string &name : m_node.walletClient().listWalletDir()) {
67 wallets[name] = false;
68 }
69 for (WalletModel *wallet_model : m_wallets) {
70 auto it = wallets.find(wallet_model->wallet().getWalletName());
71 if (it != wallets.end()) {
72 it->second = true;
73 }
74 }
75 return wallets;
76}
77
78void WalletController::closeWallet(WalletModel *wallet_model, QWidget *parent) {
79 QMessageBox box(parent);
80 box.setWindowTitle(tr("Close wallet"));
81 box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?")
82 .arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
83 box.setInformativeText(
84 tr("Closing the wallet for too long can result in having to resync the "
85 "entire chain if pruning is enabled."));
86 box.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
87 box.setDefaultButton(QMessageBox::Yes);
88 if (box.exec() != QMessageBox::Yes) {
89 return;
90 }
91
92 // First remove wallet from node.
93 wallet_model->wallet().remove();
94 // Now release the model.
95 removeAndDeleteWallet(wallet_model);
96}
97
98void WalletController::closeAllWallets(QWidget *parent) {
99 QMessageBox::StandardButton button = QMessageBox::question(
100 parent, tr("Close all wallets"),
101 tr("Are you sure you wish to close all wallets?"),
102 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);
103 if (button != QMessageBox::Yes) {
104 return;
105 }
106
107 QMutexLocker locker(&m_mutex);
108 for (WalletModel *wallet_model : m_wallets) {
109 wallet_model->wallet().remove();
110 Q_EMIT walletRemoved(wallet_model);
111 delete wallet_model;
112 }
113 m_wallets.clear();
114}
115
117 std::unique_ptr<interfaces::Wallet> wallet) {
118 QMutexLocker locker(&m_mutex);
119
120 // Return model instance if exists.
121 if (!m_wallets.empty()) {
122 std::string name = wallet->getWalletName();
123 for (WalletModel *wallet_model : m_wallets) {
124 if (wallet_model->wallet().getWalletName() == name) {
125 return wallet_model;
126 }
127 }
128 }
129
130 // Instantiate model and register it.
131 WalletModel *wallet_model = new WalletModel(
132 std::move(wallet), m_client_model, m_platform_style, nullptr);
133 // Handler callback runs in a different thread so fix wallet model thread
134 // affinity.
135 wallet_model->moveToThread(thread());
136 wallet_model->setParent(this);
137 m_wallets.push_back(wallet_model);
138
139 // WalletModel::startPollBalance needs to be called in a thread managed by
140 // Qt because of startTimer. Considering the current thread can be a RPC
141 // thread, better delegate the calling to Qt with Qt::AutoConnection.
142 const bool called =
143 QMetaObject::invokeMethod(wallet_model, "startPollBalance");
144 assert(called);
145
146 connect(
147 wallet_model, &WalletModel::unload, this,
148 [this, wallet_model] {
149 // Defer removeAndDeleteWallet when no modal widget is active.
150 // TODO: remove this workaround by removing usage of QDiallog::exec.
151 if (QApplication::activeModalWidget()) {
152 connect(
153 qApp, &QApplication::focusWindowChanged, wallet_model,
154 [this, wallet_model]() {
155 if (!QApplication::activeModalWidget()) {
156 removeAndDeleteWallet(wallet_model);
157 }
158 },
159 Qt::QueuedConnection);
160 } else {
161 removeAndDeleteWallet(wallet_model);
162 }
163 },
164 Qt::QueuedConnection);
165
166 // Re-emit coinsSent signal from wallet model.
167 connect(wallet_model, &WalletModel::coinsSent, this,
169
170 // Notify walletAdded signal on the GUI thread.
171 Q_EMIT walletAdded(wallet_model);
172
173 return wallet_model;
174}
175
177 // Unregister wallet model.
178 {
179 QMutexLocker locker(&m_mutex);
180 m_wallets.erase(
181 std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
182 }
183 Q_EMIT walletRemoved(wallet_model);
184 // Currently this can trigger the unload since the model can hold the last
185 // CWallet shared pointer.
186 delete wallet_model;
187}
188
190 WalletController *wallet_controller, QWidget *parent_widget)
191 : QObject(wallet_controller), m_wallet_controller(wallet_controller),
192 m_parent_widget(parent_widget) {}
193
195 delete m_progress_dialog;
196}
197
198void WalletControllerActivity::showProgressDialog(const QString &label_text) {
200 m_progress_dialog = new QProgressDialog(m_parent_widget);
201
202 m_progress_dialog->setLabelText(label_text);
203 m_progress_dialog->setRange(0, 0);
204 m_progress_dialog->setCancelButton(nullptr);
205 m_progress_dialog->setWindowModality(Qt::ApplicationModal);
207}
208
211 delete m_progress_dialog;
212 m_progress_dialog = nullptr;
213}
214
216 QWidget *parent_widget)
217 : WalletControllerActivity(wallet_controller, parent_widget) {
219}
220
223 delete m_passphrase_dialog;
224}
225
229 m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
230 m_passphrase_dialog->show();
231
232 connect(m_passphrase_dialog, &QObject::destroyed,
233 [this] { m_passphrase_dialog = nullptr; });
234 connect(m_passphrase_dialog, &QDialog::accepted,
235 [this] { createWallet(); });
236 connect(m_passphrase_dialog, &QDialog::rejected,
237 [this] { Q_EMIT finished(); });
238}
239
242 tr("Creating Wallet <b>%1</b>...")
243 .arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
244
245 std::string name = m_create_wallet_dialog->walletName().toStdString();
246 uint64_t flags = 0;
249 }
252 }
255 }
256
257 QTimer::singleShot(500, worker(), [this, name, flags] {
258 std::unique_ptr<interfaces::Wallet> wallet =
261
262 if (wallet) {
265 }
266
267 QTimer::singleShot(500, this, &CreateWalletActivity::finish);
268 });
269}
270
273
274 if (!m_error_message.empty()) {
275 QMessageBox::critical(
276 m_parent_widget, tr("Create wallet failed"),
277 QString::fromStdString(m_error_message.translated));
278 } else if (!m_warning_message.empty()) {
279 QMessageBox::warning(
280 m_parent_widget, tr("Create wallet warning"),
281 QString::fromStdString(
282 Join(m_warning_message, Untranslated("\n")).translated));
283 }
284
285 if (m_wallet_model) {
286 Q_EMIT created(m_wallet_model);
287 }
288
289 Q_EMIT finished();
290}
291
294 m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
296
297 connect(m_create_wallet_dialog, &QObject::destroyed,
298 [this] { m_create_wallet_dialog = nullptr; });
299 connect(m_create_wallet_dialog, &QDialog::rejected,
300 [this] { Q_EMIT finished(); });
301 connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
303 askPassphrase();
304 } else {
305 createWallet();
306 }
307 });
308}
309
311 QWidget *parent_widget)
312 : WalletControllerActivity(wallet_controller, parent_widget) {}
313
316
317 if (!m_error_message.empty()) {
318 QMessageBox::critical(
319 m_parent_widget, tr("Open wallet failed"),
320 QString::fromStdString(m_error_message.translated));
321 } else if (!m_warning_message.empty()) {
322 QMessageBox::warning(
323 m_parent_widget, tr("Open wallet warning"),
324 QString::fromStdString(
325 Join(m_warning_message, Untranslated("\n")).translated));
326 }
327
328 if (m_wallet_model) {
329 Q_EMIT opened(m_wallet_model);
330 }
331
332 Q_EMIT finished();
333}
334
335void OpenWalletActivity::open(const std::string &path) {
336 QString name = path.empty() ? QString("[" + tr("default wallet") + "]")
337 : QString::fromStdString(path);
338
340 tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
341
342 QTimer::singleShot(0, worker(), [this, path] {
343 std::unique_ptr<interfaces::Wallet> wallet =
346
347 if (wallet) {
350 }
351
352 QTimer::singleShot(0, this, &OpenWalletActivity::finish);
353 });
354}
int flags
Definition: bitcoin-tx.cpp:541
Multifunctional dialog to ask for passphrases.
@ Encrypt
Ask passphrase twice and encrypt.
Model for Bitcoin network client.
Definition: clientmodel.h:43
AskPassphraseDialog * m_passphrase_dialog
CreateWalletDialog * m_create_wallet_dialog
CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
void created(WalletModel *wallet_model)
Dialog for creating wallets.
bool isMakeBlankWalletChecked() const
QString walletName() const
bool isDisablePrivateKeysChecked() const
bool isEncryptWalletChecked() const
bool isDescriptorWalletChecked() const
void opened(WalletModel *wallet_model)
OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
void open(const std::string &path)
std::vector< bilingual_str > m_warning_message
WalletController *const m_wallet_controller
QProgressDialog * m_progress_dialog
interfaces::Node & node() const
QObject * worker() const
void showProgressDialog(const QString &label_text)
WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget)
QWidget *const m_parent_widget
Controller between interfaces::Node, WalletModel instances and the GUI.
WalletController(ClientModel &client_model, const PlatformStyle *platform_style, QObject *parent)
WalletModel * getOrCreateWallet(std::unique_ptr< interfaces::Wallet > wallet)
ClientModel & m_client_model
void removeAndDeleteWallet(WalletModel *wallet_model)
void walletAdded(WalletModel *wallet_model)
void closeAllWallets(QWidget *parent=nullptr)
std::unique_ptr< interfaces::Handler > m_handler_load_wallet
QThread *const m_activity_thread
std::map< std::string, bool > listWalletDir() const
Returns all wallet names in the wallet dir mapped to whether the wallet is loaded.
std::vector< WalletModel * > getOpenWallets() const
Returns wallet models currently open.
QObject *const m_activity_worker
void walletRemoved(WalletModel *wallet_model)
const PlatformStyle *const m_platform_style
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
interfaces::Node & m_node
std::vector< WalletModel * > m_wallets
void closeWallet(WalletModel *wallet_model, QWidget *parent=nullptr)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
QString getDisplayName() const
void unload()
virtual WalletClient & walletClient()=0
Get wallet client.
virtual std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
virtual std::unique_ptr< Wallet > createWallet(const std::string &name, const SecureString &passphrase, uint64_t wallet_creation_flags, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Create new wallet.
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
virtual void remove()=0
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:251
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:953
Definition: init.h:28
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:48
NodeContext & m_node
Definition: interfaces.cpp:785
const char * name
Definition: rest.cpp:47
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:63
bool empty() const
Definition: translation.h:27
std::string translated
Definition: translation.h:19
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
assert(!tx.IsCoinBase())
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:70
@ WALLET_FLAG_BLANK_WALLET
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses,...
Definition: walletutil.h:67