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, 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  });
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  m_progress_dialog = new QProgressDialog(m_parent_widget);
170 
171  m_progress_dialog->setLabelText(label_text);
172  m_progress_dialog->setRange(0, 0);
173  m_progress_dialog->setCancelButton(nullptr);
174  m_progress_dialog->setWindowModality(Qt::ApplicationModal);
176 }
177 
178 CreateWalletActivity::CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
179  : WalletControllerActivity(wallet_controller, parent_widget)
180 {
182 }
183 
185 {
186  delete m_create_wallet_dialog;
187  delete m_passphrase_dialog;
188 }
189 
191 {
193  m_passphrase_dialog->setWindowModality(Qt::ApplicationModal);
194  m_passphrase_dialog->show();
195 
196  connect(m_passphrase_dialog, &QObject::destroyed, [this] {
197  m_passphrase_dialog = nullptr;
198  });
199  connect(m_passphrase_dialog, &QDialog::accepted, [this] {
200  createWallet();
201  });
202  connect(m_passphrase_dialog, &QDialog::rejected, [this] {
203  Q_EMIT finished();
204  });
205 }
206 
208 {
209  showProgressDialog(tr("Creating Wallet <b>%1</b>...").arg(m_create_wallet_dialog->walletName().toHtmlEscaped()));
210 
211  std::string name = m_create_wallet_dialog->walletName().toStdString();
212  uint64_t flags = 0;
215  }
217  flags |= WALLET_FLAG_BLANK_WALLET;
218  }
219 
220  QTimer::singleShot(500, worker(), [this, name, flags] {
221  std::unique_ptr<interfaces::Wallet> wallet;
223 
225 
226  QTimer::singleShot(500, this, &CreateWalletActivity::finish);
227  });
228 }
229 
231 {
232  m_progress_dialog->hide();
233 
234  if (!m_error_message.empty()) {
235  QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message));
236  } else if (!m_warning_message.empty()) {
237  QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
238  }
239 
241 
242  Q_EMIT finished();
243 }
244 
246 {
248  m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal);
249  m_create_wallet_dialog->show();
250 
251  connect(m_create_wallet_dialog, &QObject::destroyed, [this] {
252  m_create_wallet_dialog = nullptr;
253  });
254  connect(m_create_wallet_dialog, &QDialog::rejected, [this] {
255  Q_EMIT finished();
256  });
257  connect(m_create_wallet_dialog, &QDialog::accepted, [this] {
259  askPassphrase();
260  } else {
261  createWallet();
262  }
263  });
264 }
265 
266 OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
267  : WalletControllerActivity(wallet_controller, parent_widget)
268 {
269 }
270 
272 {
273  m_progress_dialog->hide();
274 
275  if (!m_error_message.empty()) {
276  QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message));
277  } else if (!m_warning_message.empty()) {
278  QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(Join(m_warning_message, "\n")));
279  }
280 
281  if (m_wallet_model) Q_EMIT opened(m_wallet_model);
282 
283  Q_EMIT finished();
284 }
285 
286 void OpenWalletActivity::open(const std::string& path)
287 {
288  QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
289 
290  showProgressDialog(tr("Opening Wallet <b>%1</b>...").arg(name.toHtmlEscaped()));
291 
292  QTimer::singleShot(0, worker(), [this, path] {
293  std::unique_ptr<interfaces::Wallet> wallet = node().loadWallet(path, m_error_message, m_warning_message);
294 
295  if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
296 
297  QTimer::singleShot(0, this, &OpenWalletActivity::finish);
298  });
299 }
std::vector< WalletModel * > m_wallets
static const int MAX_PASSPHRASE_SIZE
Definition: guiconstants.h:14
interfaces::Wallet & wallet() const
Definition: walletmodel.h:147
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.
virtual WalletCreationStatus createWallet(const SecureString &passphrase, uint64_t wallet_creation_flags, const std::string &name, std::string &error, std::vector< std::string > &warnings, std::unique_ptr< Wallet > &result)=0
Create a wallet from file.
bool isEncryptWalletChecked() const
CreateWalletDialog * m_create_wallet_dialog
NodeContext & m_node
Definition: chain.cpp:374
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:55
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:32
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:508
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:362
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