Bitcoin ABC 0.32.4
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(
133 nullptr /* required for the following moveToThread() call */);
134
135 // Move WalletModel object to the thread that created the WalletController
136 // object (GUI main thread), instead of the current thread, which could be
137 // an outside wallet thread or RPC thread sending a LoadWallet notification.
138 // This ensures queued signals sent to the WalletModel object will be
139 // handled on the GUI event loop.
140 wallet_model->moveToThread(thread());
141 QMetaObject::invokeMethod(
142 this, [wallet_model, this] { wallet_model->setParent(this); },
144 m_wallets.push_back(wallet_model);
145
146 // WalletModel::startPollBalance needs to be called in a thread managed by
147 // Qt because of startTimer. Considering the current thread can be a RPC
148 // thread, better delegate the calling to Qt with Qt::AutoConnection.
149 const bool called =
150 QMetaObject::invokeMethod(wallet_model, "startPollBalance");
151 assert(called);
152
153 connect(
154 wallet_model, &WalletModel::unload, this,
155 [this, wallet_model] {
156 // Defer removeAndDeleteWallet when no modal widget is active.
157 // TODO: remove this workaround by removing usage of QDiallog::exec.
158 if (QApplication::activeModalWidget()) {
159 connect(
160 qApp, &QApplication::focusWindowChanged, wallet_model,
161 [this, wallet_model]() {
162 if (!QApplication::activeModalWidget()) {
163 removeAndDeleteWallet(wallet_model);
164 }
165 },
166 Qt::QueuedConnection);
167 } else {
168 removeAndDeleteWallet(wallet_model);
169 }
170 },
171 Qt::QueuedConnection);
172
173 // Re-emit coinsSent signal from wallet model.
174 connect(wallet_model, &WalletModel::coinsSent, this,
176
177 Q_EMIT walletAdded(wallet_model);
178
179 return wallet_model;
180}
181
183 // Unregister wallet model.
184 {
185 QMutexLocker locker(&m_mutex);
186 m_wallets.erase(
187 std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
188 }
189 Q_EMIT walletRemoved(wallet_model);
190 // Currently this can trigger the unload since the model can hold the last
191 // CWallet shared pointer.
192 delete wallet_model;
193}
194
196 WalletController *wallet_controller, QWidget *parent_widget)
197 : QObject(wallet_controller), m_wallet_controller(wallet_controller),
198 m_parent_widget(parent_widget) {}
199
201 delete m_progress_dialog;
202}
203
204void WalletControllerActivity::showProgressDialog(const QString &label_text) {
206 m_progress_dialog = new QProgressDialog(m_parent_widget);
207
208 m_progress_dialog->setLabelText(label_text);
209 m_progress_dialog->setRange(0, 0);
210 m_progress_dialog->setCancelButton(nullptr);
211 m_progress_dialog->setWindowModality(Qt::ApplicationModal);
213}
214
217 delete m_progress_dialog;
218 m_progress_dialog = nullptr;
219}
220
222 QWidget *parent_widget)
223 : WalletControllerActivity(wallet_controller, parent_widget) {
225}
226
229 delete m_passphrase_dialog;
230}
231
235 m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
236 m_passphrase_dialog->show();
237
238 connect(m_passphrase_dialog, &QObject::destroyed,
239 [this] { m_passphrase_dialog = nullptr; });
240 connect(m_passphrase_dialog, &QDialog::accepted,
241 [this] { createWallet(); });
242 connect(m_passphrase_dialog, &QDialog::rejected,
243 [this] { Q_EMIT finished(); });
244}
245
248 tr("Creating Wallet <b>%1</b>...")
249 .arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
250
251 std::string name = m_create_wallet_dialog->walletName().toStdString();
252 uint64_t flags = 0;
255 }
258 }
261 }
262
263 QTimer::singleShot(500ms, worker(), [this, name, flags] {
266
267 if (wallet) {
270 } else {
272 }
273
274 QTimer::singleShot(500, this, &CreateWalletActivity::finish);
275 });
276}
277
280
281 if (!m_error_message.empty()) {
282 QMessageBox::critical(
283 m_parent_widget, tr("Create wallet failed"),
284 QString::fromStdString(m_error_message.translated));
285 } else if (!m_warning_message.empty()) {
286 QMessageBox::warning(
287 m_parent_widget, tr("Create wallet warning"),
288 QString::fromStdString(
289 Join(m_warning_message, Untranslated("\n")).translated));
290 }
291
292 if (m_wallet_model) {
293 Q_EMIT created(m_wallet_model);
294 }
295
296 Q_EMIT finished();
297}
298
301 m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
303
304 connect(m_create_wallet_dialog, &QObject::destroyed,
305 [this] { m_create_wallet_dialog = nullptr; });
306 connect(m_create_wallet_dialog, &QDialog::rejected,
307 [this] { Q_EMIT finished(); });
308 connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
310 askPassphrase();
311 } else {
312 createWallet();
313 }
314 });
315}
316
318 QWidget *parent_widget)
319 : WalletControllerActivity(wallet_controller, parent_widget) {}
320
323
324 if (!m_error_message.empty()) {
325 QMessageBox::critical(
326 m_parent_widget, tr("Open wallet failed"),
327 QString::fromStdString(m_error_message.translated));
328 } else if (!m_warning_message.empty()) {
329 QMessageBox::warning(
330 m_parent_widget, tr("Open wallet warning"),
331 QString::fromStdString(
332 Join(m_warning_message, Untranslated("\n")).translated));
333 }
334
335 if (m_wallet_model) {
336 Q_EMIT opened(m_wallet_model);
337 }
338
339 Q_EMIT finished();
340}
341
342void OpenWalletActivity::open(const std::string &path) {
343 QString name = path.empty() ? QString("[" + tr("default wallet") + "]")
344 : QString::fromStdString(path);
345
347 tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
348
349 QTimer::singleShot(0, worker(), [this, path] {
351
352 if (wallet) {
355 } else {
357 }
358
359 QTimer::singleShot(0, this, &OpenWalletActivity::finish);
360 });
361}
int flags
Definition: bitcoin-tx.cpp:542
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: init.h:31
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
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:822
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