Browse Source

#156 Prevent duplicate labels

Aditya Kulkarni 5 years ago
parent
commit
3f3962708f
  1. 1
      .gitignore
  2. 42
      src/addressbook.cpp
  3. 2
      src/addressbook.h

1
.gitignore

@ -37,3 +37,4 @@ workspace.code-workspace
zcashd
IDEWorkspaceChecks.plist
*.sln
node_modules

42
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())

2
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();

Loading…
Cancel
Save