Bitcoin Core  0.19.99
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 
9 #include <qt/guiconstants.h>
10 #include <qt/guiutil.h>
11 #include <qt/walletmodel.h>
12 
13 #include <interfaces/handler.h>
14 #include <interfaces/node.h>
15 #include <util/string.h>
16 #include <wallet/wallet.h>
17 
18 #include <algorithm>
19 
20 #include <QApplication>
21 #include <QMessageBox>
22 #include <QMutexLocker>
23 #include <QThread>
24 #include <QTimer>
25 #include <QWindow>
26 
27 WalletController::WalletController(interfaces::Node& node, const PlatformStyle* platform_style, OptionsModel* options_model, QObject* parent)
28  : QObject(parent)
29  , m_activity_thread(new QThread(this))
30  , m_activity_worker(new QObject)
31  , m_node(node)
32  , m_platform_style(platform_style)
33  , m_options_model(options_model)
34 {
35  m_handler_load_wallet = m_node.handleLoadWallet([this](std::unique_ptr<interfaces::Wallet> wallet) {
36  getOrCreateWallet(std::move(wallet));
37  });
38 
39  for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.getWallets()) {
40  getOrCreateWallet(std::move(wallet));
41  }
42 
43  m_activity_worker->moveToThread(m_activity_thread);
44  m_activity_thread->start();
45 }
46 
47 // Not using the default destructor because not all member types definitions are
48 // available in the header, just forward declared.
50 {
51  m_activity_thread->quit();
52  m_activity_thread->wait();
53  delete m_activity_worker;
54 }
55 
56 std::vector<WalletModel*> WalletController::getOpenWallets() const
57 {
58  QMutexLocker locker(&m_mutex);
59  return m_wallets;
60 }
61 
62 std::map<std::string, bool> WalletController::listWalletDir() const
63 {
64  QMutexLocker locker(&m_mutex);
65  std::map<std::string, bool> wallets;
66  for (const std::string& name : m_node.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()) it->second = true;
72  }
73  return wallets;
74 }
75 
76 void WalletController::closeWallet(WalletModel* wallet_model, QWidget* parent)
77 {
78  QMessageBox box(parent);
79  box.setWindowTitle(tr("Close wallet"));
80  box.setText(tr("Are you sure you wish to close the wallet <i>%1</i>?").arg(GUIUtil::HtmlEscape(wallet_model->getDisplayName())));
81  box.setInformativeText(tr("Closing the wallet for too long can result in having to resync the entire chain if pruning is enabled."));
82  box.setStandardButtons(QMessageBox::Yes|QMessageBox::Cancel);
83  box.setDefaultButton(QMessageBox::Yes);
84  if (box.exec() != QMessageBox::Yes) return;
85 
86  // First remove wallet from node.
87  wallet_model->wallet().remove();
88  // Now release the model.
89  removeAndDeleteWallet(wallet_model);
90 }
91 
92 WalletModel* WalletController::getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet)
93 {
94  QMutexLocker locker(&m_mutex);
95 
96  // Return model instance if exists.
97  if (!m_wallets.empty()) {
98  std::string name = wallet->getWalletName();
99  for (WalletModel* wallet_model : m_wallets) {
100  if (wallet_model->wallet().getWalletName() == name) {
101  return wallet_model;
102  }
103  }
104  }
105 
106  // Instantiate model and register it.
107  WalletModel* wallet_model = new WalletModel(std::move(wallet), m_node, m_platform_style, m_options_model, nullptr);
108  // Handler callback runs in a different thread so fix wallet model thread affinity.
109  wallet_model->moveToThread(thread());
110  wallet_model->setParent(this);
111  m_wallets.push_back(wallet_model);
112 
113  // WalletModel::startPollBalance needs to be called in a thread managed by
114  // Qt because of startTimer. Considering the current thread can be a RPC
115  // thread, better delegate the calling to Qt with Qt::AutoConnection.
116  const bool called = QMetaObject::invokeMethod(wallet_model, "startPollBalance");
117  assert(called);
118 
119  connect(wallet_model, &WalletModel::unload, this, [this, wallet_model] {
120  // Defer removeAndDeleteWallet when no modal widget is active.
121  // TODO: remove this workaround by removing usage of QDiallog::exec.
122  if (QApplication::activeModalWidget()) {
123  connect(qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() {
124  if (!QApplication::activeModalWidget()) {
125  removeAndDeleteWallet(wallet_model);
126  }
127  }, Qt::QueuedConnection);
128  } else {
129  removeAndDeleteWallet(wallet_model);
130  }
131  }, Qt::QueuedConnection);
132 
133  // Re-emit coinsSent signal from wallet model.
134  connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent);
135 
136  // Notify walletAdded signal on the GUI thread.
137  Q_EMIT walletAdded(wallet_model);
138 
139  return wallet_model;
140 }
141 
143 {
144  // Unregister wallet model.
145  {
146  QMutexLocker locker(&m_mutex);
147  m_wallets.erase(std::remove(m_wallets.begin(), m_wallets.end(), wallet_model));
148  }
149  Q_EMIT walletRemoved(wallet_model);
150  // Currently this can trigger the unload since the model can hold the last
151  // CWallet shared pointer.
152  delete wallet_model;
153 }
154 
155 WalletControllerActivity::WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget)
156  : QObject(wallet_controller)
157  , m_wallet_controller(wallet_controller)
158  , m_parent_widget(parent_widget)
159 {
160 }
161 
163 {
164  delete m_progress_dialog;
165 }
166 
167 void WalletControllerActivity::showProgressDialog(const QString& label_text)
168 {
169  assert(!m_progress_dialog);
170  m_progress_dialog = new QProgressDialog(m_parent_widget);
171 
172  m_progress_dialog->setLabelText(label_text);
173  m_progress_dialog->setRange(0, 0);
174  m_progress_dialog->setCancelButton(nullptr);
175  m_progress_dialog->setWindowModality(Qt::ApplicationModal);
177 }
178 
180 {
181  assert(m_progress_dialog);
182  delete m_progress_dialog;
183  m_progress_dialog = nullptr;
184 }
185 
186 CreateWalletActivity::CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
187  : WalletControllerActivity(wallet_controller, parent_widget)
188 {
190 }
191 
193 {
194  delete m_create_wallet_dialog;
195  delete m_passphrase_dialog;
196 }
197 
199 {
201  m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
202  m_passphrase_dialog->show();
203 
204  connect(m_passphrase_dialog, &QObject::destroyed, [this] {
205  m_passphrase_dialog = nullptr;
206  });
207  connect(m_passphrase_dialog, &QDialog::accepted, [this] {
208  createWallet();
209  });
210  connect(m_passphrase_dialog, &QDialog::rejected, [this] {
211  Q_EMIT finished();
212  });
213 }
214 
216 {
217  showProgressDialog(tr("Creating Wallet <b>%1</b>...").arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
218 
219  std::string name = m_create_wallet_dialog->walletName().toStdString();
220  uint64_t flags = 0;
223  }
225  flags |= WALLET_FLAG_BLANK_WALLET;
226  }
227 
228  QTimer::singleShot(500, worker(), [this, name, flags] {
229  WalletCreationStatus status;
230  std::unique_ptr<interfaces::Wallet> wallet = node().createWallet(m_passphrase, flags, name, m_error_message, m_warning_message, status);
231 
233 
234  QTimer::singleShot(500, this, &CreateWalletActivity::finish);
235  });
236 }
237 
239 {
241 
242  if (!m_error_message.empty()) {
243  QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message));
244  } else if (!m_warning_message.empty()) {
245  QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
246  }
247 
249 
250  Q_EMIT finished();
251 }
252 
254 {
256  m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
257  m_create_wallet_dialog->show();
258 
259  connect(m_create_wallet_dialog, &QObject::destroyed, [this] {
260  m_create_wallet_dialog = nullptr;
261  });
262  connect(m_create_wallet_dialog, &QDialog::rejected, [this] {
263  Q_EMIT finished();
264  });
265  connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
267  askPassphrase();
268  } else {
269  createWallet();
270  }
271  });
272 }
273 
274 OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
275  : WalletControllerActivity(wallet_controller, parent_widget)
276 {
277 }
278 
280 {
282 
283  if (!m_error_message.empty()) {
284  QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message));
285  } else if (!m_warning_message.empty()) {
286  QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
287  }
288 
289  if (m_wallet_model) Q_EMIT opened(m_wallet_model);
290 
291  Q_EMIT finished();
292 }
293 
294 void OpenWalletActivity::open(const std::string& path)
295 {
296  QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
297 
298  showProgressDialog(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
299 
300  QTimer::singleShot(0, worker(), [this, path] {
301  std::unique_ptr<interfaces::Wallet> wallet = node().loadWallet(path, m_error_message, m_warning_message);
302 
303  if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
304 
305  QTimer::singleShot(0, this, &OpenWalletActivity::finish);
306  });
307 }
std::vector< WalletModel * > m_wallets
virtual std::unique_ptr< Wallet > createWallet(const SecureString &passphrase, uint64_t wallet_creation_flags, const std::string &name, std::string &error, std::vector< std::string > &warnings, WalletCreationStatus &status)=0
Create a wallet from file.
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
interfaces::Wallet & wallet() const
Definition: walletmodel.h:145
void coinsSent(WalletModel *wallet, SendCoinsRecipient recipient, QByteArray transaction)
interfaces::Node & node() const
virtual std::unique_ptr< Wallet > loadWallet(const std::string &name, std::string &error, std::vector< std::string > &warnings)=0
Attempts to load a wallet from file or directory.
bool isEncryptWalletChecked() const
CreateWalletDialog * m_create_wallet_dialog
NodeContext & m_node
Definition: chain.cpp:382
void opened(WalletModel *wallet_model)
const PlatformStyle *const m_platform_style
CreateWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
std::vector< std::string > m_warning_message
virtual std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
void unload()
void showProgressDialog(const QString &label_text)
Ask passphrase twice and encrypt.
QString walletName() const
OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget)
Controller between interfaces::Node, WalletModel instances and the GUI.
QProgressDialog * m_progress_dialog
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:218
WalletController(interfaces::Node &node, const PlatformStyle *platform_style, OptionsModel *options_model, QObject *parent)
bool isDisablePrivateKeysChecked() const
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
WalletCreationStatus
Definition: wallet.h:58
QObject *const m_activity_worker
void coinsSent(WalletModel *wallet_model, SendCoinsRecipient recipient, QByteArray transaction)
std::map< std::string, bool > listWalletDir() const
Returns all wallet names in the wallet dir mapped to whether the wallet is loaded.
std::string Join(const std::vector< T > &list, const std::string &separator, UnaryOp unary_op)
Join a list of items.
Definition: string.h:34
bool isMakeBlankWalletChecked() const
void created(WalletModel *wallet_model)
void closeWallet(WalletModel *wallet_model, QWidget *parent=nullptr)
const char * name
Definition: rest.cpp:40
virtual void remove()=0
QWidget *const m_parent_widget
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses, and other watch only things, and is therefore "blank.".
Definition: walletutil.h:57
void open(const std::string &path)
interfaces::Node & m_node
void walletAdded(WalletModel *wallet_model)
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:867
AskPassphraseDialog * m_passphrase_dialog
int flags
Definition: bitcoin-tx.cpp:509
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
std::vector< WalletModel * > getOpenWallets() const
Returns wallet models currently open.
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:36
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:50
Multifunctional dialog to ask for passphrases.
Dialog for creating wallets.
WalletModel * getOrCreateWallet(std::unique_ptr< interfaces::Wallet > wallet)
void walletRemoved(WalletModel *wallet_model)
QObject * worker() const
WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget)
QString getDisplayName() const
QThread *const m_activity_thread
std::unique_ptr< interfaces::Handler > m_handler_load_wallet
auto it
Definition: validation.cpp:361
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:39
OptionsModel *const m_options_model
void removeAndDeleteWallet(WalletModel *wallet_model)
WalletController *const m_wallet_controller