Bitcoin ABC 0.32.12
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
27using util::Join;
28
30 const PlatformStyle *platform_style,
31 QObject *parent)
32 : QObject(parent), m_activity_thread(new QThread(this)),
33 m_activity_worker(new QObject), m_client_model(client_model),
34 m_node(client_model.node()), m_platform_style(platform_style),
35 m_options_model(client_model.getOptionsModel()) {
37 [this](std::unique_ptr<interfaces::Wallet> wallet) {
38 getOrCreateWallet(std::move(wallet));
39 });
40
41 for (std::unique_ptr<interfaces::Wallet> &wallet :
43 getOrCreateWallet(std::move(wallet));
44 }
45
47 m_activity_thread->start();
48 QTimer::singleShot(0, m_activity_worker,
49 []() { util::ThreadRename("qt-walletctrl"); });
50}
51
52// Not using the default destructor because not all member types definitions are
53// available in the header, just forward declared.
55 m_activity_thread->quit();
56 m_activity_thread->wait();
57 delete m_activity_worker;
58}
59
60std::vector<WalletModel *> WalletController::getOpenWallets() const {
61 QMutexLocker locker(&m_mutex);
62 return m_wallets;
63}
64
65std::map<std::string, bool> WalletController::listWalletDir() const {
66 QMutexLocker locker(&m_mutex);
67 std::map<std::string, bool> wallets;
68 for (const std::string &name : m_node.walletClient().listWalletDir()) {
69 wallets[name] = false;
70 }
71 for (WalletModel *wallet_model : m_wallets) {
72 auto it = wallets.find(wallet_model->wallet().getWalletName());
73 if (it != wallets.end()) {
74 it->second = true;
75 }
76 }
77 return wallets;
78}
79
80void WalletController::closeWallet(WalletModel *wallet_model, QWidget *parent) {
81 QMessageBox box(parent);
82 box.setWindowTitle(tr("Close wallet"));
83 box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?")
84 .arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
85 box.setInformativeText(
86 tr("Closing the wallet for too long can result in having to resync the "
87 "entire chain if pruning is enabled."));
88 box.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
89 box.setDefaultButton(QMessageBox::Yes);
90 if (box.exec() != QMessageBox::Yes) {
91 return;
92 }
93
94 // First remove wallet from node.
95 wallet_model->wallet().remove();
96 // Now release the model.
97 removeAndDeleteWallet(wallet_model);
98}
99
101 QMessageBox::StandardButton button = QMessageBox::question(
102 parent, tr("Close all wallets"),
103 tr("Are you sure you wish to close all wallets?"),
104 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);
105 if (button != QMessageBox::Yes) {
106 return;
107 }
108
109 QMutexLocker locker(&m_mutex);
110 for (WalletModel *wallet_model : m_wallets) {
111 wallet_model->wallet().remove();
112 Q_EMIT walletRemoved(wallet_model);
113 delete wallet_model;
114 }
115 m_wallets.clear();
116}
117
119 std::unique_ptr<interfaces::Wallet> wallet) {
120 QMutexLocker locker(&m_mutex);
121
122 // Return model instance if exists.
123 if (!m_wallets.empty()) {
124 std::string name = wallet->getWalletName();
125 for (WalletModel *wallet_model : m_wallets) {
126 if (wallet_model->wallet().getWalletName() == name) {
127 return wallet_model;
128 }
129 }
130 }
131
132 // Instantiate model and register it.
133 WalletModel *wallet_model = new WalletModel(
135 nullptr /* required for the following moveToThread() call */);
136
137 // Move WalletModel object to the thread that created the WalletController
138 // object (GUI main thread), instead of the current thread, which could be
139 // an outside wallet thread or RPC thread sending a LoadWallet notification.
140 // This ensures queued signals sent to the WalletModel object will be
141 // handled on the GUI event loop.
142 wallet_model->moveToThread(thread());
143 QMetaObject::invokeMethod(
144 this, [wallet_model, this] { wallet_model->setParent(this); },
146 m_wallets.push_back(wallet_model);
147
148 // WalletModel::startPollBalance needs to be called in a thread managed by
149 // Qt because of startTimer. Considering the current thread can be a RPC
150 // thread, better delegate the calling to Qt with Qt::AutoConnection.
151 const bool called =
152 QMetaObject::invokeMethod(wallet_model, "startPollBalance");
153 assert(called);
154
155 connect(
156 wallet_model, &WalletModel::unload, this,
157 [this, wallet_model] {
158 // Defer removeAndDeleteWallet when no modal widget is active.
159 // TODO: remove this workaround by removing usage of QDiallog::exec.
160 if (QApplication::activeModalWidget()) {
161 connect(
162 qApp, &QApplication::focusWindowChanged, wallet_model,
163 [this, wallet_model]() {
164 if (!QApplication::activeModalWidget()) {
165 removeAndDeleteWallet(wallet_model);
166 }
167 },
168 Qt::QueuedConnection);
169 } else {
170 removeAndDeleteWallet(wallet_model);
171 }
172 },
173 Qt::QueuedConnection);
174
175 // Re-emit coinsSent signal from wallet model.
176 connect(wallet_model, &WalletModel::coinsSent, this,
178
179 Q_EMIT walletAdded(wallet_model);
180
181 return wallet_model;
182}
183
185 // Unregister wallet model.
186 {
187 QMutexLocker locker(&m_mutex);
188 m_wallets.erase(
189 std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
190 }
191 Q_EMIT walletRemoved(wallet_model);
192 // Currently this can trigger the unload since the model can hold the last
193 // CWallet shared pointer.
194 delete wallet_model;
195}
196
198 WalletController *wallet_controller, QWidget *parent_widget)
199 : QObject(wallet_controller), m_wallet_controller(wallet_controller),
200 m_parent_widget(parent_widget) {}
201
203 delete m_progress_dialog;
204}
205
206void WalletControllerActivity::showProgressDialog(const QString &label_text) {
208 m_progress_dialog = new QProgressDialog(m_parent_widget);
209
210 m_progress_dialog->setLabelText(label_text);
211 m_progress_dialog->setRange(0, 0);
212 m_progress_dialog->setCancelButton(nullptr);
213 m_progress_dialog->setWindowModality(Qt::ApplicationModal);
215}
216
219 delete m_progress_dialog;
220 m_progress_dialog = nullptr;
221}
222
224 QWidget *parent_widget)
225 : WalletControllerActivity(wallet_controller, parent_widget) {
227}
228
231 delete m_passphrase_dialog;
232}
233
237 m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
238 m_passphrase_dialog->show();
239
240 connect(m_passphrase_dialog, &QObject::destroyed,
241 [this] { m_passphrase_dialog = nullptr; });
242 connect(m_passphrase_dialog, &QDialog::accepted,
243 [this] { createWallet(); });
244 connect(m_passphrase_dialog, &QDialog::rejected,
245 [this] { Q_EMIT finished(); });
246}
247
250 tr("Creating Wallet <b>%1</b>...")
251 .arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
252
253 std::string name = m_create_wallet_dialog->walletName().toStdString();
254 uint64_t flags = 0;
257 }
260 }
263 }
264
265 QTimer::singleShot(500ms, worker(), [this, name, flags] {
268
269 if (wallet) {
272 } else {
274 }
275
276 QTimer::singleShot(500, this, &CreateWalletActivity::finish);
277 });
278}
279
282
283 if (!m_error_message.empty()) {
284 QMessageBox::critical(
285 m_parent_widget, tr("Create wallet failed"),
286 QString::fromStdString(m_error_message.translated));
287 } else if (!m_warning_message.empty()) {
288 QMessageBox::warning(
289 m_parent_widget, tr("Create wallet warning"),
290 QString::fromStdString(
291 Join(m_warning_message, Untranslated("\n")).translated));
292 }
293
294 if (m_wallet_model) {
295 Q_EMIT created(m_wallet_model);
296 }
297
298 Q_EMIT finished();
299}
300
303 m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
305
306 connect(m_create_wallet_dialog, &QObject::destroyed,
307 [this] { m_create_wallet_dialog = nullptr; });
308 connect(m_create_wallet_dialog, &QDialog::rejected,
309 [this] { Q_EMIT finished(); });
310 connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
312 askPassphrase();
313 } else {
314 createWallet();
315 }
316 });
317}
318
320 QWidget *parent_widget)
321 : WalletControllerActivity(wallet_controller, parent_widget) {}
322
325
326 if (!m_error_message.empty()) {
327 QMessageBox::critical(
328 m_parent_widget, tr("Open wallet failed"),
329 QString::fromStdString(m_error_message.translated));
330 } else if (!m_warning_message.empty()) {
331 QMessageBox::warning(
332 m_parent_widget, tr("Open wallet warning"),
333 QString::fromStdString(
334 Join(m_warning_message, Untranslated("\n")).translated));
335 }
336
337 if (m_wallet_model) {
338 Q_EMIT opened(m_wallet_model);
339 }
340
341 Q_EMIT finished();
342}
343
344void OpenWalletActivity::open(const std::string &path) {
345 QString name = path.empty() ? QString("[" + tr("default wallet") + "]")
346 : QString::fromStdString(path);
347
349 tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
350
351 QTimer::singleShot(0, worker(), [this, path] {
353
354 if (wallet) {
357 } else {
359 }
360
361 QTimer::singleShot(0, this, &OpenWalletActivity::finish);
362 });
363}
int flags
Definition: bitcoin-tx.cpp:546
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< Handler > handleLoadWallet(LoadWalletFn fn)=0
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
virtual util::Result< std::unique_ptr< Wallet > > loadWallet(const std::string &name, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
virtual util::Result< std::unique_ptr< Wallet > > createWallet(const std::string &name, const SecureString &passphrase, uint64_t wallet_creation_flags, std::vector< bilingual_str > &warnings)=0
Create new wallet.
virtual void remove()=0
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:17
Qt::ConnectionType blockingGUIThreadConnection()
Get connection type to call object slot in GUI thread with invokeMethod.
Definition: guiutil.cpp:369
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:256
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:959
Definition: messages.h:12
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
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:105
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:823
const char * name
Definition: rest.cpp:47
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