Skip to content

Commit

Permalink
[qt] Display more helpful message when adding a send address has failed
Browse files Browse the repository at this point in the history
Addresses bitcoin#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.
  • Loading branch information
jamesob committed Apr 25, 2018
1 parent c5b2770 commit 8cdcaee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
28 changes: 19 additions & 9 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,21 +407,31 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent
return true;
}

/* Look up label for address in address book, if not found return empty string.
*/
QString AddressTableModel::labelForAddress(const QString &address) const
{
{
CTxDestination destination = DecodeDestination(address.toStdString());
std::string name;
if (walletModel->wallet().getAddress(destination, &name))
{
return QString::fromStdString(name);
}
std::string name;
if (getAddressData(address, &name, /* purpose= */ nullptr)) {
return QString::fromStdString(name);
}
return QString();
}

QString AddressTableModel::purposeForAddress(const QString &address) const
{
std::string purpose;
if (getAddressData(address, /* name= */ nullptr, &purpose)) {
return QString::fromStdString(purpose);
}
return QString();
}

bool AddressTableModel::getAddressData(const QString &address,
std::string* name,
std::string* purpose) const {
CTxDestination destination = DecodeDestination(address.toStdString());
return walletModel->wallet().getAddress(destination, name, /* is_mine= */ nullptr, purpose);
}

int AddressTableModel::lookupAddress(const QString &address) const
{
QModelIndexList lst = match(index(0, Address, QModelIndex()),
Expand Down
9 changes: 7 additions & 2 deletions src/qt/addresstablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ class AddressTableModel : public QAbstractTableModel
*/
QString addRow(const QString &type, const QString &label, const QString &address, const OutputType address_type);

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

/** Look up purpose for address in address book, if not found return empty string. */
QString purposeForAddress(const QString &address) const;

/* Look up row index of an address in the model.
Return -1 if not found.
*/
Expand All @@ -86,6 +88,9 @@ class AddressTableModel : public QAbstractTableModel
QStringList columns;
EditStatus editStatus;

/** Look up address book data given an address string. */
bool getAddressData(const QString &address, std::string* name, std::string* purpose) const;

/** Notify listeners that data changed. */
void emitDataChanged(int index);

Expand Down
21 changes: 20 additions & 1 deletion src/qt/editaddressdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void EditAddressDialog::accept()
break;
case AddressTableModel::DUPLICATE_ADDRESS:
QMessageBox::warning(this, windowTitle(),
tr("The entered address \"%1\" is already in the address book.").arg(ui->addressEdit->text()),
getDuplicateAddressWarning(),
QMessageBox::Ok, QMessageBox::Ok);
break;
case AddressTableModel::WALLET_UNLOCK_FAILURE:
Expand All @@ -129,6 +129,25 @@ void EditAddressDialog::accept()
QDialog::accept();
}

QString EditAddressDialog::getDuplicateAddressWarning() const
{
QString dup_address = ui->addressEdit->text();
QString existing_label = model->labelForAddress(dup_address);
QString existing_purpose = model->purposeForAddress(dup_address);

if (existing_purpose == "receive" &&
(mode == NewSendingAddress || mode == EditSendingAddress)) {
return tr(
"Address \"%1\" already exists as a receiving address with label "
"\"%2\" and so cannot be added as a sending address."
).arg(dup_address).arg(existing_label);
}
return tr(
"The entered address \"%1\" is already in the address book with "
"label \"%2\"."
).arg(dup_address).arg(existing_label);
}

QString EditAddressDialog::getAddress() const
{
return address;
Expand Down
3 changes: 3 additions & 0 deletions src/qt/editaddressdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public Q_SLOTS:
private:
bool saveCurrentRow();

/** Return a descriptive string when adding an already-existing address fails. */
QString getDuplicateAddressWarning() const;

Ui::EditAddressDialog *ui;
QDataWidgetMapper *mapper;
Mode mode;
Expand Down

0 comments on commit 8cdcaee

Please sign in to comment.