Bitcoin Core  22.99.0
P2P Digital Currency
editaddressdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-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/editaddressdialog.h>
6 #include <qt/forms/ui_editaddressdialog.h>
7 
8 #include <qt/addresstablemodel.h>
9 #include <qt/guiutil.h>
10 
11 #include <QDataWidgetMapper>
12 #include <QMessageBox>
13 
14 
15 EditAddressDialog::EditAddressDialog(Mode _mode, QWidget *parent) :
16  QDialog(parent, GUIUtil::dialog_flags),
17  ui(new Ui::EditAddressDialog),
18  mapper(nullptr),
19  mode(_mode),
20  model(nullptr)
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  QString existing_purpose = model->purposeForAddress(dup_address);
143 
144  if (existing_purpose == "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 }
EditAddressDialog::loadRow
void loadRow(int row)
Definition: editaddressdialog.cpp:66
EditAddressDialog::EditReceivingAddress
@ EditReceivingAddress
Definition: editaddressdialog.h:29
AddressTableModel
Qt model of the address book in the core.
Definition: addresstablemodel.h:23
AddressTableModel::labelForAddress
QString labelForAddress(const QString &address) const
Look up label for address in address book, if not found return empty string.
Definition: addresstablemodel.cpp:412
AddressTableModel::KEY_GENERATION_FAILURE
@ KEY_GENERATION_FAILURE
Generating a new public key for a receiving address failed.
Definition: addresstablemodel.h:47
GUIUtil
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
GUIUtil::ItemDelegate
Definition: guiutil.h:285
EditAddressDialog::setModel
void setModel(AddressTableModel *model)
Definition: editaddressdialog.cpp:55
GUIUtil::ItemDelegate::keyEscapePressed
void keyEscapePressed()
GUIUtil::dialog_flags
constexpr auto dialog_flags
Definition: guiutil.h:59
EditAddressDialog::EditSendingAddress
@ EditSendingAddress
Definition: editaddressdialog.h:30
EditAddressDialog::EditAddressDialog
EditAddressDialog(Mode mode, QWidget *parent=nullptr)
Definition: editaddressdialog.cpp:15
EditAddressDialog
Dialog for editing an address and associated information.
Definition: editaddressdialog.h:22
AddressTableModel::addRow
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type)
Definition: addresstablemodel.cpp:343
EditAddressDialog::~EditAddressDialog
~EditAddressDialog()
Definition: editaddressdialog.cpp:50
EditAddressDialog::model
AddressTableModel * model
Definition: editaddressdialog.h:54
EditAddressDialog::mapper
QDataWidgetMapper * mapper
Definition: editaddressdialog.h:52
guiutil.h
AddressTableModel::Label
@ Label
User specified label.
Definition: addresstablemodel.h:32
EditAddressDialog::setAddress
void setAddress(const QString &address)
Definition: editaddressdialog.cpp:162
EditAddressDialog::NewSendingAddress
@ NewSendingAddress
Definition: editaddressdialog.h:28
EditAddressDialog::saveCurrentRow
bool saveCurrentRow()
Definition: editaddressdialog.cpp:71
editaddressdialog.h
EditAddressDialog::address
QString address
Definition: editaddressdialog.h:56
AddressTableModel::getEditStatus
EditStatus getEditStatus() const
Definition: addresstablemodel.h:81
EditAddressDialog::mode
Mode mode
Definition: editaddressdialog.h:53
EditAddressDialog::accept
void accept() override
Definition: editaddressdialog.cpp:96
AddressTableModel::WALLET_UNLOCK_FAILURE
@ WALLET_UNLOCK_FAILURE
Wallet could not be unlocked to create new receiving address.
Definition: addresstablemodel.h:46
Ui
Definition: addressbookpage.h:14
EditAddressDialog::ui
Ui::EditAddressDialog * ui
Definition: editaddressdialog.h:51
EditAddressDialog::getDuplicateAddressWarning
QString getDuplicateAddressWarning() const
Return a descriptive string when adding an already-existing address fails.
Definition: editaddressdialog.cpp:138
EditAddressDialog::Mode
Mode
Definition: editaddressdialog.h:27
AddressTableModel::INVALID_ADDRESS
@ INVALID_ADDRESS
Unparseable address.
Definition: addresstablemodel.h:44
AddressTableModel::OK
@ OK
Everything ok.
Definition: addresstablemodel.h:42
EditAddressDialog::getAddress
QString getAddress() const
Definition: editaddressdialog.cpp:157
AddressTableModel::NO_CHANGES
@ NO_CHANGES
No changes were made during edit operation.
Definition: addresstablemodel.h:43
AddressTableModel::Send
static const QString Send
Specifies send address.
Definition: addresstablemodel.h:50
GUIUtil::handleCloseWindowShortcut
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:402
AddressTableModel::GetDefaultAddressType
OutputType GetDefaultAddressType() const
Definition: addresstablemodel.cpp:451
GUIUtil::setupAddressWidget
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:113
AddressTableModel::purposeForAddress
QString purposeForAddress(const QString &address) const
Look up purpose for address in address book, if not found return empty string.
Definition: addresstablemodel.cpp:421
AddressTableModel::DUPLICATE_ADDRESS
@ DUPLICATE_ADDRESS
Address already in address book.
Definition: addresstablemodel.h:45
addresstablemodel.h
AddressTableModel::Address
@ Address
Bitcoin address.
Definition: addresstablemodel.h:33