Bitcoin ABC  0.24.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 
5 #include <qt/walletcontroller.h>
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 
44  m_activity_worker->moveToThread(m_activity_thread);
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 
58 std::vector<WalletModel *> WalletController::getOpenWallets() const {
59  QMutexLocker locker(&m_mutex);
60  return m_wallets;
61 }
62 
63 std::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 
78 void 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 
98 void 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 
198 void WalletControllerActivity::showProgressDialog(const QString &label_text) {
199  assert(!m_progress_dialog);
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 
210  assert(m_progress_dialog);
211  delete m_progress_dialog;
212  m_progress_dialog = nullptr;
213 }
214 
216  QWidget *parent_widget)
217  : WalletControllerActivity(wallet_controller, parent_widget) {
219 }
220 
222  delete m_create_wallet_dialog;
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) {
264  m_wallet_controller->getOrCreateWallet(std::move(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);
295  m_create_wallet_dialog->show();
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 
335 void 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) {
349  m_wallet_controller->getOrCreateWallet(std::move(wallet));
350  }
351 
352  QTimer::singleShot(0, this, &OpenWalletActivity::finish);
353  });
354 }
WalletController::walletRemoved
void walletRemoved(WalletModel *wallet_model)
CreateWalletActivity::~CreateWalletActivity
virtual ~CreateWalletActivity()
Definition: walletcontroller.cpp:221
WalletController::m_handler_load_wallet
std::unique_ptr< interfaces::Handler > m_handler_load_wallet
Definition: walletcontroller.h:86
WalletModel::unload
void unload()
WalletControllerActivity::m_warning_message
std::vector< bilingual_str > m_warning_message
Definition: walletcontroller.h:114
wallet.h
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
WalletControllerActivity::finished
void finished()
WalletController::removeAndDeleteWallet
void removeAndDeleteWallet(WalletModel *wallet_model)
Definition: walletcontroller.cpp:176
CreateWalletDialog::isMakeBlankWalletChecked
bool isMakeBlankWalletChecked() const
Definition: createwalletdialog.cpp:57
flags
int flags
Definition: bitcoin-tx.cpp:532
CreateWalletActivity::CreateWalletActivity
CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
Definition: walletcontroller.cpp:215
walletcontroller.h
CreateWalletDialog::isDisablePrivateKeysChecked
bool isDisablePrivateKeysChecked() const
Definition: createwalletdialog.cpp:53
interfaces::WalletClient::createWallet
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.
WalletControllerActivity::worker
QObject * worker() const
Definition: walletcontroller.h:104
string.h
WalletControllerActivity::m_error_message
bilingual_str m_error_message
Definition: walletcontroller.h:113
CreateWalletActivity::m_create_wallet_dialog
CreateWalletDialog * m_create_wallet_dialog
Definition: walletcontroller.h:136
node.h
handler.h
AskPassphraseDialog
Multifunctional dialog to ask for passphrases.
Definition: askpassphrasedialog.h:21
WalletController::listWalletDir
std::map< std::string, bool > listWalletDir() const
Returns all wallet names in the wallet dir mapped to whether the wallet is loaded.
Definition: walletcontroller.cpp:63
WalletController::walletAdded
void walletAdded(WalletModel *wallet_model)
WalletController::m_client_model
ClientModel & m_client_model
Definition: walletcontroller.h:80
OpenWalletActivity::OpenWalletActivity
OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
Definition: walletcontroller.cpp:310
WalletModel::coinsSent
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
interfaces::WalletClient::handleLoadWallet
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
MAX_PASSPHRASE_SIZE
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
CreateWalletActivity::createWallet
void createWallet()
Definition: walletcontroller.cpp:240
OpenWalletActivity::finish
void finish()
Definition: walletcontroller.cpp:314
bilingual_str::empty
bool empty() const
Definition: translation.h:27
WalletControllerActivity::destroyProgressDialog
void destroyProgressDialog()
Definition: walletcontroller.cpp:209
CreateWalletActivity::askPassphrase
void askPassphrase()
Definition: walletcontroller.cpp:226
interfaces::WalletClient::getWallets
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
interfaces::Wallet::remove
virtual void remove()=0
WalletModel::wallet
interfaces::Wallet & wallet() const
Definition: walletmodel.h:150
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
interfaces::WalletClient::loadWallet
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
CreateWalletDialog::isEncryptWalletChecked
bool isEncryptWalletChecked() const
Definition: createwalletdialog.cpp:49
WalletController::getOpenWallets
std::vector< WalletModel * > getOpenWallets() const
Returns wallet models currently open.
Definition: walletcontroller.cpp:58
OpenWalletActivity::opened
void opened(WalletModel *wallet_model)
interfaces::WalletClient::listWalletDir
virtual std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
WalletControllerActivity::m_parent_widget
QWidget *const m_parent_widget
Definition: walletcontroller.h:110
CreateWalletActivity::m_passphrase
SecureString m_passphrase
Definition: walletcontroller.h:135
util::ThreadRename
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
WalletControllerActivity::m_wallet_model
WalletModel * m_wallet_model
Definition: walletcontroller.h:112
WalletController::m_platform_style
const PlatformStyle *const m_platform_style
Definition: walletcontroller.h:82
WalletController
Controller between interfaces::Node, WalletModel instances and the GUI.
Definition: walletcontroller.h:48
interfaces::Node::walletClient
virtual WalletClient & walletClient()=0
Get wallet client.
createwalletdialog.h
CreateWalletActivity::created
void created(WalletModel *wallet_model)
GUIUtil::PolishProgressDialog
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:919
WalletController::coinsSent
void coinsSent(interfaces::Wallet &wallet, SendCoinsRecipient recipient, QByteArray transaction)
guiutil.h
bilingual_str::translated
std::string translated
Definition: translation.h:19
WalletControllerActivity::node
interfaces::Node & node() const
Definition: walletcontroller.h:103
WALLET_FLAG_BLANK_WALLET
@ WALLET_FLAG_BLANK_WALLET
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses,...
Definition: walletutil.h:67
WalletController::getOrCreateWallet
WalletModel * getOrCreateWallet(std::unique_ptr< interfaces::Wallet > wallet)
Definition: walletcontroller.cpp:116
WalletController::~WalletController
~WalletController()
Definition: walletcontroller.cpp:52
WALLET_FLAG_DISABLE_PRIVATE_KEYS
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
name
const char * name
Definition: rest.cpp:43
m_node
NodeContext & m_node
Definition: chain.cpp:485
WalletControllerActivity::m_wallet_controller
WalletController *const m_wallet_controller
Definition: walletcontroller.h:109
ClientModel
Model for Bitcoin network client.
Definition: clientmodel.h:36
Join
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:36
WalletController::closeWallet
void closeWallet(WalletModel *wallet_model, QWidget *parent=nullptr)
Definition: walletcontroller.cpp:78
guiconstants.h
GUIUtil::HtmlEscape
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:240
WalletControllerActivity::~WalletControllerActivity
virtual ~WalletControllerActivity()
Definition: walletcontroller.cpp:194
CreateWalletActivity::m_passphrase_dialog
AskPassphraseDialog * m_passphrase_dialog
Definition: walletcontroller.h:137
CreateWalletActivity::finish
void finish()
Definition: walletcontroller.cpp:271
translation.h
CreateWalletDialog
Dialog for creating wallets.
Definition: createwalletdialog.h:19
WalletController::m_activity_worker
QObject *const m_activity_worker
Definition: walletcontroller.h:79
WalletController::WalletController
WalletController(ClientModel &client_model, const PlatformStyle *platform_style, QObject *parent)
Definition: walletcontroller.cpp:27
CreateWalletDialog::isDescriptorWalletChecked
bool isDescriptorWalletChecked() const
Definition: createwalletdialog.cpp:61
WalletController::m_node
interfaces::Node & m_node
Definition: walletcontroller.h:81
WalletControllerActivity::WalletControllerActivity
WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget)
Definition: walletcontroller.cpp:189
WalletControllerActivity::m_progress_dialog
QProgressDialog * m_progress_dialog
Definition: walletcontroller.h:111
WalletController::m_activity_thread
QThread *const m_activity_thread
Definition: walletcontroller.h:78
WalletController::m_mutex
QMutex m_mutex
Definition: walletcontroller.h:84
WalletController::m_wallets
std::vector< WalletModel * > m_wallets
Definition: walletcontroller.h:85
OpenWalletActivity::open
void open(const std::string &path)
Definition: walletcontroller.cpp:335
WalletModel::getDisplayName
QString getDisplayName() const
Definition: walletmodel.cpp:522
PlatformStyle
Definition: platformstyle.h:13
CreateWalletDialog::walletName
QString walletName() const
Definition: createwalletdialog.cpp:45
threadnames.h
AskPassphraseDialog::Encrypt
@ Encrypt
Ask passphrase twice and encrypt.
Definition: askpassphrasedialog.h:26
askpassphrasedialog.h
WalletController::closeAllWallets
void closeAllWallets(QWidget *parent=nullptr)
Definition: walletcontroller.cpp:98
WalletControllerActivity::showProgressDialog
void showProgressDialog(const QString &label_text)
Definition: walletcontroller.cpp:198
CreateWalletActivity::create
void create()
Definition: walletcontroller.cpp:292
WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:70
clientmodel.h
WalletControllerActivity
Definition: walletcontroller.h:91