diff --git a/.gitignore b/.gitignore index a1efcce..e68e06b 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ workspace.code-workspace zcashd IDEWorkspaceChecks.plist *.sln +node_modules diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 7ec64cb..4ef2d2b 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -122,14 +122,32 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target) { // Add new address button QObject::connect(ab.addNew, &QPushButton::clicked, [&] () { auto addr = ab.addr->text().trimmed(); - if (!addr.isEmpty() && !ab.label->text().isEmpty()) { - // Test if address is valid. - if (!Settings::isValidAddress(addr)) { - QMessageBox::critical(parent, QObject::tr("Address Format Error"), addr + QObject::tr(" doesn't seem to be a valid Zcash address."), QMessageBox::Ok); - } else { - model.addNewLabel(ab.label->text(), ab.addr->text()); - } + QString newLabel = ab.label->text(); + + if (addr.isEmpty() || newLabel.isEmpty()) { + QMessageBox::critical(parent, QObject::tr("Address or Label Error"), + QObject::tr("Address or Label cannot be empty"), QMessageBox::Ok); + return; } + // Test if address is valid. + if (!Settings::isValidAddress(addr)) { + QMessageBox::critical(parent, QObject::tr("Address Format Error"), + QObject::tr("%1 doesn't seem to be a valid Zcash address.") + .arg(addr), + QMessageBox::Ok); + return; + } + + // Don't allow duplicate address labels. + if (!getInstance()->getAddressForLabel(newLabel).isEmpty()) { + QMessageBox::critical(parent, QObject::tr("Label Error"), + QObject::tr("The label '%1' already exists. Please remove the existing label.") + .arg(newLabel), + QMessageBox::Ok); + return; + } + + model.addNewLabel(newLabel, ab.addr->text()); }); // Import Button @@ -344,6 +362,16 @@ QString AddressBook::getLabelForAddress(QString addr) { return ""; } +// Get the address for a label +QString AddressBook::getAddressForLabel(QString label) { + for (auto i: allLabels) { + if (i.first == label) + return i.second; + } + + return ""; +} + QString AddressBook::addLabelToAddress(QString addr) { QString label = AddressBook::getInstance()->getLabelForAddress(addr); if (!label.isEmpty()) diff --git a/src/addressbook.h b/src/addressbook.h index e7a45fc..af64c07 100644 --- a/src/addressbook.h +++ b/src/addressbook.h @@ -52,6 +52,8 @@ public: // Get an address's first label QString getLabelForAddress(QString address); + // Get a Label's address + QString getAddressForLabel(QString label); private: AddressBook();