Skip to content

Commit 8cdcaee

Browse files
committed
[qt] Display more helpful message when adding a send address has failed
Addresses #12796. When we're unable to add a sending address to the address book because it already exists as a receiving address, display a message indicating as much. This should help avoid confusion about an address supposedly already in the book but which isn't currently visible in the interface.
1 parent c5b2770 commit 8cdcaee

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

src/qt/addresstablemodel.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,21 +407,31 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent
407407
return true;
408408
}
409409

410-
/* Look up label for address in address book, if not found return empty string.
411-
*/
412410
QString AddressTableModel::labelForAddress(const QString &address) const
413411
{
414-
{
415-
CTxDestination destination = DecodeDestination(address.toStdString());
416-
std::string name;
417-
if (walletModel->wallet().getAddress(destination, &name))
418-
{
419-
return QString::fromStdString(name);
420-
}
412+
std::string name;
413+
if (getAddressData(address, &name, /* purpose= */ nullptr)) {
414+
return QString::fromStdString(name);
415+
}
416+
return QString();
417+
}
418+
419+
QString AddressTableModel::purposeForAddress(const QString &address) const
420+
{
421+
std::string purpose;
422+
if (getAddressData(address, /* name= */ nullptr, &purpose)) {
423+
return QString::fromStdString(purpose);
421424
}
422425
return QString();
423426
}
424427

428+
bool AddressTableModel::getAddressData(const QString &address,
429+
std::string* name,
430+
std::string* purpose) const {
431+
CTxDestination destination = DecodeDestination(address.toStdString());
432+
return walletModel->wallet().getAddress(destination, name, /* is_mine= */ nullptr, purpose);
433+
}
434+
425435
int AddressTableModel::lookupAddress(const QString &address) const
426436
{
427437
QModelIndexList lst = match(index(0, Address, QModelIndex()),

src/qt/addresstablemodel.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ class AddressTableModel : public QAbstractTableModel
6767
*/
6868
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type);
6969

70-
/* Look up label for address in address book, if not found return empty string.
71-
*/
70+
/** Look up label for address in address book, if not found return empty string. */
7271
QString labelForAddress(const QString &address) const;
7372

73+
/** Look up purpose for address in address book, if not found return empty string. */
74+
QString purposeForAddress(const QString &address) const;
75+
7476
/* Look up row index of an address in the model.
7577
Return -1 if not found.
7678
*/
@@ -86,6 +88,9 @@ class AddressTableModel : public QAbstractTableModel
8688
QStringList columns;
8789
EditStatus editStatus;
8890

91+
/** Look up address book data given an address string. */
92+
bool getAddressData(const QString &address, std::string* name, std::string* purpose) const;
93+
8994
/** Notify listeners that data changed. */
9095
void emitDataChanged(int index);
9196

src/qt/editaddressdialog.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void EditAddressDialog::accept()
109109
break;
110110
case AddressTableModel::DUPLICATE_ADDRESS:
111111
QMessageBox::warning(this, windowTitle(),
112-
tr("The entered address \"%1\" is already in the address book.").arg(ui->addressEdit->text()),
112+
getDuplicateAddressWarning(),
113113
QMessageBox::Ok, QMessageBox::Ok);
114114
break;
115115
case AddressTableModel::WALLET_UNLOCK_FAILURE:
@@ -129,6 +129,25 @@ void EditAddressDialog::accept()
129129
QDialog::accept();
130130
}
131131

132+
QString EditAddressDialog::getDuplicateAddressWarning() const
133+
{
134+
QString dup_address = ui->addressEdit->text();
135+
QString existing_label = model->labelForAddress(dup_address);
136+
QString existing_purpose = model->purposeForAddress(dup_address);
137+
138+
if (existing_purpose == "receive" &&
139+
(mode == NewSendingAddress || mode == EditSendingAddress)) {
140+
return tr(
141+
"Address \"%1\" already exists as a receiving address with label "
142+
"\"%2\" and so cannot be added as a sending address."
143+
).arg(dup_address).arg(existing_label);
144+
}
145+
return tr(
146+
"The entered address \"%1\" is already in the address book with "
147+
"label \"%2\"."
148+
).arg(dup_address).arg(existing_label);
149+
}
150+
132151
QString EditAddressDialog::getAddress() const
133152
{
134153
return address;

src/qt/editaddressdialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public Q_SLOTS:
4545
private:
4646
bool saveCurrentRow();
4747

48+
/** Return a descriptive string when adding an already-existing address fails. */
49+
QString getDuplicateAddressWarning() const;
50+
4851
Ui::EditAddressDialog *ui;
4952
QDataWidgetMapper *mapper;
5053
Mode mode;

0 commit comments

Comments
 (0)