Bitcoin Core  0.20.99
P2P Digital Currency
walletview.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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/walletview.h>
6 
7 #include <qt/addressbookpage.h>
9 #include <qt/clientmodel.h>
10 #include <qt/guiutil.h>
12 #include <qt/optionsmodel.h>
13 #include <qt/overviewpage.h>
14 #include <qt/platformstyle.h>
15 #include <qt/receivecoinsdialog.h>
16 #include <qt/sendcoinsdialog.h>
19 #include <qt/transactionview.h>
20 #include <qt/walletmodel.h>
21 
22 #include <interfaces/node.h>
23 #include <node/ui_interface.h>
24 #include <psbt.h>
25 #include <util/strencodings.h>
26 
27 #include <QAction>
28 #include <QActionGroup>
29 #include <QApplication>
30 #include <QClipboard>
31 #include <QFileDialog>
32 #include <QHBoxLayout>
33 #include <QProgressDialog>
34 #include <QPushButton>
35 #include <QVBoxLayout>
36 
37 WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
38  QStackedWidget(parent),
39  clientModel(nullptr),
40  walletModel(nullptr),
41  platformStyle(_platformStyle)
42 {
43  // Create tabs
45 
46  transactionsPage = new QWidget(this);
47  QVBoxLayout *vbox = new QVBoxLayout();
48  QHBoxLayout *hbox_buttons = new QHBoxLayout();
50  vbox->addWidget(transactionView);
51  QPushButton *exportButton = new QPushButton(tr("&Export"), this);
52  exportButton->setToolTip(tr("Export the data in the current tab to a file"));
54  exportButton->setIcon(platformStyle->SingleColorIcon(":/icons/export"));
55  }
56  hbox_buttons->addStretch();
57  hbox_buttons->addWidget(exportButton);
58  vbox->addLayout(hbox_buttons);
59  transactionsPage->setLayout(vbox);
60 
63 
66 
67  addWidget(overviewPage);
68  addWidget(transactionsPage);
69  addWidget(receiveCoinsPage);
70  addWidget(sendCoinsPage);
71 
73  // Clicking on a transaction on the overview pre-selects the transaction on the transaction history page
75 
77 
79  // Highlight transaction after send
81 
82  // Clicking on "Export" allows to export the transaction list
83  connect(exportButton, &QPushButton::clicked, transactionView, &TransactionView::exportClicked);
84 
85  // Pass through messages from sendCoinsPage
87  // Pass through messages from transactionView
89 
91 }
92 
94 {
95 }
96 
98 {
99  this->clientModel = _clientModel;
100 
101  overviewPage->setClientModel(_clientModel);
102  sendCoinsPage->setClientModel(_clientModel);
103  if (walletModel) walletModel->setClientModel(_clientModel);
104 }
105 
107 {
108  this->walletModel = _walletModel;
109 
110  // Put transaction list in tabs
111  transactionView->setModel(_walletModel);
112  overviewPage->setWalletModel(_walletModel);
113  receiveCoinsPage->setModel(_walletModel);
114  sendCoinsPage->setModel(_walletModel);
115  usedReceivingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);
116  usedSendingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);
117 
118  if (_walletModel)
119  {
120  // Receive and pass through messages from wallet model
121  connect(_walletModel, &WalletModel::message, this, &WalletView::message);
122 
123  // Handle changes in encryption status
126 
127  // update HD status
128  Q_EMIT hdEnabledStatusChanged();
129 
130  // Balloon pop-up for new transaction
131  connect(_walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);
132 
133  // Ask for passphrase if needed
134  connect(_walletModel, &WalletModel::requireUnlock, this, &WalletView::unlockWallet);
135 
136  // Show progress dialog
137  connect(_walletModel, &WalletModel::showProgress, this, &WalletView::showProgress);
138  }
139 }
140 
141 void WalletView::processNewTransaction(const QModelIndex& parent, int start, int /*end*/)
142 {
143  // Prevent balloon-spam when initial block download is in progress
145  return;
146 
148  if (!ttm || ttm->processingQueuedTransactions())
149  return;
150 
151  QString date = ttm->index(start, TransactionTableModel::Date, parent).data().toString();
152  qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent).data(Qt::EditRole).toULongLong();
153  QString type = ttm->index(start, TransactionTableModel::Type, parent).data().toString();
154  QModelIndex index = ttm->index(start, 0, parent);
155  QString address = ttm->data(index, TransactionTableModel::AddressRole).toString();
156  QString label = GUIUtil::HtmlEscape(ttm->data(index, TransactionTableModel::LabelRole).toString());
157 
158  Q_EMIT incomingTransaction(date, walletModel->getOptionsModel()->getDisplayUnit(), amount, type, address, label, GUIUtil::HtmlEscape(walletModel->getWalletName()));
159 }
160 
162 {
163  setCurrentWidget(overviewPage);
164 }
165 
167 {
168  setCurrentWidget(transactionsPage);
169 }
170 
172 {
173  setCurrentWidget(receiveCoinsPage);
174 }
175 
177 {
178  setCurrentWidget(sendCoinsPage);
179 
180  if (!addr.isEmpty())
181  sendCoinsPage->setAddress(addr);
182 }
183 
185 {
186  // calls show() in showTab_SM()
187  SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(platformStyle, this);
188  signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
189  signVerifyMessageDialog->setModel(walletModel);
190  signVerifyMessageDialog->showTab_SM(true);
191 
192  if (!addr.isEmpty())
193  signVerifyMessageDialog->setAddress_SM(addr);
194 }
195 
197 {
198  // calls show() in showTab_VM()
199  SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(platformStyle, this);
200  signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
201  signVerifyMessageDialog->setModel(walletModel);
202  signVerifyMessageDialog->showTab_VM(true);
203 
204  if (!addr.isEmpty())
205  signVerifyMessageDialog->setAddress_VM(addr);
206 }
207 
208 void WalletView::gotoLoadPSBT(bool from_clipboard)
209 {
210  std::string data;
211 
212  if (from_clipboard) {
213  std::string raw = QApplication::clipboard()->text().toStdString();
214  bool invalid;
215  data = DecodeBase64(raw, &invalid);
216  if (invalid) {
217  Q_EMIT message(tr("Error"), tr("Unable to decode PSBT from clipboard (invalid base64)"), CClientUIInterface::MSG_ERROR);
218  return;
219  }
220  } else {
221  QString filename = GUIUtil::getOpenFileName(this,
222  tr("Load Transaction Data"), QString(),
223  tr("Partially Signed Transaction (*.psbt)"), nullptr);
224  if (filename.isEmpty()) return;
225  if (GetFileSize(filename.toLocal8Bit().data(), MAX_FILE_SIZE_PSBT) == MAX_FILE_SIZE_PSBT) {
226  Q_EMIT message(tr("Error"), tr("PSBT file must be smaller than 100 MiB"), CClientUIInterface::MSG_ERROR);
227  return;
228  }
229  std::ifstream in(filename.toLocal8Bit().data(), std::ios::binary);
230  data = std::string(std::istreambuf_iterator<char>{in}, {});
231  }
232 
233  std::string error;
235  if (!DecodeRawPSBT(psbtx, data, error)) {
236  Q_EMIT message(tr("Error"), tr("Unable to decode PSBT") + "\n" + QString::fromStdString(error), CClientUIInterface::MSG_ERROR);
237  return;
238  }
239 
241  dlg->openWithPSBT(psbtx);
242  dlg->setAttribute(Qt::WA_DeleteOnClose);
243  dlg->exec();
244 }
245 
247 {
248  return sendCoinsPage->handlePaymentRequest(recipient);
249 }
250 
252 {
254 }
255 
257 {
258  Q_EMIT encryptionStatusChanged();
259 }
260 
261 void WalletView::encryptWallet(bool status)
262 {
263  if(!walletModel)
264  return;
266  dlg.setModel(walletModel);
267  dlg.exec();
268 
270 }
271 
273 {
274  QString filename = GUIUtil::getSaveFileName(this,
275  tr("Backup Wallet"), QString(),
276  tr("Wallet Data (*.dat)"), nullptr);
277 
278  if (filename.isEmpty())
279  return;
280 
281  if (!walletModel->wallet().backupWallet(filename.toLocal8Bit().data())) {
282  Q_EMIT message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1.").arg(filename),
284  }
285  else {
286  Q_EMIT message(tr("Backup Successful"), tr("The wallet data was successfully saved to %1.").arg(filename),
288  }
289 }
290 
292 {
294  dlg.setModel(walletModel);
295  dlg.exec();
296 }
297 
299 {
300  if(!walletModel)
301  return;
302  // Unlock wallet when requested by wallet model
304  {
306  dlg.setModel(walletModel);
307  dlg.exec();
308  }
309 }
310 
312 {
313  if(!walletModel)
314  return;
315 
317 }
318 
320 {
321  if(!walletModel)
322  return;
323 
325 }
326 
327 void WalletView::showProgress(const QString &title, int nProgress)
328 {
329  if (nProgress == 0) {
330  progressDialog = new QProgressDialog(title, tr("Cancel"), 0, 100);
332  progressDialog->setWindowModality(Qt::ApplicationModal);
333  progressDialog->setMinimumDuration(0);
334  progressDialog->setAutoClose(false);
335  progressDialog->setValue(0);
336  } else if (nProgress == 100) {
337  if (progressDialog) {
338  progressDialog->close();
339  progressDialog->deleteLater();
340  progressDialog = nullptr;
341  }
342  } else if (progressDialog) {
343  if (progressDialog->wasCanceled()) {
345  } else {
346  progressDialog->setValue(nProgress);
347  }
348  }
349 }
350 
352 {
353  Q_EMIT outOfSyncWarningClicked();
354 }
QWidget * transactionsPage
Definition: walletview.h:61
Dialog for requesting payment of bitcoins.
void setWalletModel(WalletModel *walletModel)
interfaces::Wallet & wallet() const
Definition: walletmodel.h:146
virtual bool isInitialBlockDownload()=0
Is initial block download.
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get open filename, convenience wrapper for QFileDialog::getOpenFileName.
Definition: guiutil.cpp:313
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletview.cpp:196
OverviewPage * overviewPage
Definition: walletview.h:60
TransactionView * transactionView
Definition: walletview.h:67
Dialog showing transaction details.
void setPrivacy(bool privacy)
void usedSendingAddresses()
Show used sending addresses.
Definition: walletview.cpp:311
void setAddress_VM(const QString &address)
void setModel(AddressTableModel *model)
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletview.cpp:291
ClientModel * clientModel
Definition: walletview.h:57
void setPrivacy(bool privacy)
fs::ifstream ifstream
Definition: fs.h:90
void focusTransaction(const QModelIndex &)
Ask passphrase twice and encrypt.
virtual bool backupWallet(const std::string &filename)=0
Back up wallet.
void requestedSyncWarningInfo()
User has requested more information about the out of sync state.
Definition: walletview.cpp:351
WalletModel * walletModel
Definition: walletview.h:58
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
std::vector< unsigned char > DecodeBase64(const char *p, bool *pf_invalid)
void showProgress(const QString &title, int nProgress)
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:220
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
A version of CTransaction with the PSBT format.
Definition: psbt.h:390
Open address book for editing.
void outOfSyncWarningClicked()
Notify that the out of sync warning icon has been pressed.
const PlatformStyle * platformStyle
Definition: walletview.h:70
AddressTableModel * getAddressTableModel()
void gotoSendCoinsPage(QString addr="")
Switch to send coins page.
Definition: walletview.cpp:176
void openWithPSBT(PartiallySignedTransaction psbtx)
void updateEncryptionStatus()
Re-emit encryption status signal.
Definition: walletview.cpp:256
void outOfSyncWarningClicked()
void bringToFront(QWidget *w)
Definition: guiutil.cpp:372
EncryptionStatus getEncryptionStatus() const
int getDisplayUnit() const
Definition: optionsmodel.h:82
void processNewTransaction(const QModelIndex &parent, int start, int)
Show incoming transaction notification for new transactions.
Definition: walletview.cpp:141
SendCoinsDialog * sendCoinsPage
Definition: walletview.h:63
void setModel(WalletModel *model)
WalletModel * getWalletModel()
Definition: walletview.h:45
bool processingQueuedTransactions() const
void gotoHistoryPage()
Switch to history (transactions) page.
Definition: walletview.cpp:166
Ask passphrase and unlock.
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletview.cpp:319
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:71
void setWalletModel(WalletModel *walletModel)
Set the wallet model.
Definition: walletview.cpp:106
void setAddress(const QString &address)
void setClientModel(ClientModel *clientModel)
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
Widget showing the transaction list for a wallet, including a filter row.
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
interfaces::Node & node() const
Definition: clientmodel.h:54
const std::streamsize MAX_FILE_SIZE_PSBT
Definition: psbt.h:45
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletview.cpp:298
Dialog for sending bitcoins.
void encryptionStatusChanged()
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QString getWalletName() const
TransactionTableModel * getTransactionTableModel()
Widget that shows a list of sending or receiving addresses.
UI model for the transaction table of a wallet.
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:881
Model for Bitcoin network client.
Definition: clientmodel.h:46
void setModel(WalletModel *model)
void backupWallet()
Backup the wallet.
Definition: walletview.cpp:272
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletview.cpp:246
void transactionClicked(const QModelIndex &index)
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletview.cpp:161
void encryptionStatusChanged()
Encryption status of wallet changed.
void showOutOfSyncWarning(bool fShow)
256-bit opaque blob.
Definition: uint256.h:123
void requireUnlock()
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, const std::string &tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:361
AddressBookPage * usedSendingAddressesPage
Definition: walletview.h:64
void setModel(WalletModel *model)
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletview.cpp:184
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
void showProgress(const QString &title, int nProgress)
Show progress dialog e.g.
Definition: walletview.cpp:327
Multifunctional dialog to ask for passphrases.
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
Definition: guiutil.cpp:267
void gotoLoadPSBT(bool from_clipboard=false)
Load Partially Signed Bitcoin Transaction.
Definition: walletview.cpp:208
void message(const QString &title, const QString &message, unsigned int style)
WalletView(const PlatformStyle *platformStyle, QWidget *parent)
Definition: walletview.cpp:37
void setClientModel(ClientModel *clientModel)
Set the client model.
Definition: walletview.cpp:97
Ask passphrase and decrypt wallet.
void setClientModel(ClientModel *clientModel)
Label of address related to transaction.
std::streampos GetFileSize(const char *path, std::streamsize max)
Get the size of a file by scanning it.
Definition: system.cpp:146
void hdEnabledStatusChanged()
HD-Enabled status of wallet changed (only possible during startup)
virtual void abortRescan()=0
Abort a rescan.
Ask old passphrase + new passphrase twice.
void encryptWallet(bool status)
Encrypt the wallet.
Definition: walletview.cpp:261
QVariant data(const QModelIndex &index, int role) const override
ReceiveCoinsDialog * receiveCoinsPage
Definition: walletview.h:62
void gotoReceiveCoinsPage()
Switch to receive coins page.
Definition: walletview.cpp:171
void message(const QString &title, const QString &message, unsigned int style)
void coinsSent(const uint256 &txid)
void coinsSent()
void setModel(WalletModel *model)
bool error(const char *fmt, const Args &... args)
Definition: system.h:50
void transactionClicked()
Overview ("home") page widget.
Definition: overviewpage.h:28
void showOutOfSyncWarning(bool fShow)
Definition: walletview.cpp:251
void incomingTransaction(const QString &date, int unit, const CAmount &amount, const QString &type, const QString &address, const QString &label, const QString &walletName)
Notify that a new transaction appeared.
QProgressDialog * progressDialog
Definition: walletview.h:69
bool getImagesOnButtons() const
Definition: platformstyle.h:21
OptionsModel * getOptionsModel()
Predefined combinations for certain default usage cases.
Definition: ui_interface.h:66
void setAddress_SM(const QString &address)
AddressBookPage * usedReceivingAddressesPage
Definition: walletview.h:65
void setModel(WalletModel *model)