[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.
This commit is contained in:
James O'Beirne 2018-04-10 16:27:40 -04:00
parent c5b277033a
commit 8cdcaee4c7
4 changed files with 49 additions and 12 deletions

View file

@ -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))
{
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()),

View file

@ -67,10 +67,12 @@ public:
*/
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.
*/
@ -86,6 +88,9 @@ private:
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);

View file

@ -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:
@ -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;

View file

@ -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;