Bitcoin Core  27.99.0
P2P Digital Currency
editaddressdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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/editaddressdialog.h>
6 #include <qt/forms/ui_editaddressdialog.h>
7 
8 #include <qt/addresstablemodel.h>
9 #include <qt/guiutil.h>
10 
11 #include <wallet/types.h>
12 
13 #include <QDataWidgetMapper>
14 #include <QMessageBox>
15 
16 
18  : QDialog(parent, GUIUtil::dialog_flags),
19  ui(new Ui::EditAddressDialog),
20  mode(_mode)
21 {
22  ui->setupUi(this);
23 
24  GUIUtil::setupAddressWidget(ui->addressEdit, this);
25 
26  switch(mode)
27  {
28  case NewSendingAddress:
29  setWindowTitle(tr("New sending address"));
30  break;
32  setWindowTitle(tr("Edit receiving address"));
33  ui->addressEdit->setEnabled(false);
34  break;
35  case EditSendingAddress:
36  setWindowTitle(tr("Edit sending address"));
37  break;
38  }
39 
40  mapper = new QDataWidgetMapper(this);
41  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
42 
44  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &EditAddressDialog::reject);
45  mapper->setItemDelegate(delegate);
46 
48 }
49 
51 {
52  delete ui;
53 }
54 
56 {
57  this->model = _model;
58  if(!_model)
59  return;
60 
61  mapper->setModel(_model);
62  mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
63  mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
64 }
65 
67 {
68  mapper->setCurrentIndex(row);
69 }
70 
72 {
73  if(!model)
74  return false;
75 
76  switch(mode)
77  {
78  case NewSendingAddress:
79  address = model->addRow(
81  ui->labelEdit->text(),
82  ui->addressEdit->text(),
84  break;
86  case EditSendingAddress:
87  if(mapper->submit())
88  {
89  address = ui->addressEdit->text();
90  }
91  break;
92  }
93  return !address.isEmpty();
94 }
95 
97 {
98  if(!model)
99  return;
100 
101  if(!saveCurrentRow())
102  {
103  switch(model->getEditStatus())
104  {
106  // Failed with unknown reason. Just reject.
107  break;
109  // No changes were made during edit operation. Just reject.
110  break;
112  QMessageBox::warning(this, windowTitle(),
113  tr("The entered address \"%1\" is not a valid Bitcoin address.").arg(ui->addressEdit->text()),
114  QMessageBox::Ok, QMessageBox::Ok);
115  break;
117  QMessageBox::warning(this, windowTitle(),
119  QMessageBox::Ok, QMessageBox::Ok);
120  break;
122  QMessageBox::critical(this, windowTitle(),
123  tr("Could not unlock wallet."),
124  QMessageBox::Ok, QMessageBox::Ok);
125  break;
127  QMessageBox::critical(this, windowTitle(),
128  tr("New key generation failed."),
129  QMessageBox::Ok, QMessageBox::Ok);
130  break;
131 
132  }
133  return;
134  }
135  QDialog::accept();
136 }
137 
139 {
140  QString dup_address = ui->addressEdit->text();
141  QString existing_label = model->labelForAddress(dup_address);
142  auto existing_purpose = model->purposeForAddress(dup_address);
143 
144  if (existing_purpose == wallet::AddressPurpose::RECEIVE &&
146  return tr(
147  "Address \"%1\" already exists as a receiving address with label "
148  "\"%2\" and so cannot be added as a sending address."
149  ).arg(dup_address).arg(existing_label);
150  }
151  return tr(
152  "The entered address \"%1\" is already in the address book with "
153  "label \"%2\"."
154  ).arg(dup_address).arg(existing_label);
155 }
156 
158 {
159  return address;
160 }
161 
162 void EditAddressDialog::setAddress(const QString &_address)
163 {
164  this->address = _address;
165  ui->addressEdit->setText(_address);
166 }
Qt model of the address book in the core.
OutputType GetDefaultAddressType() const
EditStatus getEditStatus() const
@ WALLET_UNLOCK_FAILURE
Wallet could not be unlocked to create new receiving address.
@ NO_CHANGES
No changes were made during edit operation.
@ INVALID_ADDRESS
Unparseable address.
@ KEY_GENERATION_FAILURE
Generating a new public key for a receiving address failed.
@ OK
Everything ok.
@ DUPLICATE_ADDRESS
Address already in address book.
@ Address
Bitcoin address.
@ Label
User specified label.
std::optional< wallet::AddressPurpose > purposeForAddress(const QString &address) const
Look up purpose for address in address book, if not found return empty string.
static const QString Send
Specifies send address.
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
QString labelForAddress(const QString &address) const
Look up label for address in address book, if not found return empty string.
Dialog for editing an address and associated information.
Ui::EditAddressDialog * ui
EditAddressDialog(Mode mode, QWidget *parent=nullptr)
QString getDuplicateAddressWarning() const
Return a descriptive string when adding an already-existing address fails.
void setModel(AddressTableModel *model)
QDataWidgetMapper * mapper
AddressTableModel * model
void accept() override
void setAddress(const QString &address)
QString getAddress() const
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:60
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:427
constexpr auto dialog_flags
Definition: guiutil.h:60
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:134