diff --git a/src/contactswidget.cpp b/src/contactswidget.cpp index 8093fc6..005413f 100644 --- a/src/contactswidget.cpp +++ b/src/contactswidget.cpp @@ -5,17 +5,17 @@ #include "ui_contactswidget.h" #include "dialog/contactsdialog.h" #include "model/ModelUtils.h" -#include "utils/utils.h" +#include "mainwindow.h" +#include "libwalletqt/AddressBook.h" -#include -#include -#include +#include ContactsWidget::ContactsWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ContactsWidget) { ui->setupUi(this); + m_ctx = MainWindow::getContext(); // header context menu ui->contacts->header()->setContextMenuPolicy(Qt::CustomContextMenu); @@ -28,15 +28,16 @@ ContactsWidget::ContactsWidget(QWidget *parent) : // context menu ui->contacts->setContextMenuPolicy(Qt::CustomContextMenu); m_contextMenu = new QMenu(ui->contacts); - m_contextMenu->addAction(QIcon(":/assets/images/person.svg"), "New contact", this, &ContactsWidget::newContact); - + m_contextMenu->addAction(QIcon(":/assets/images/person.svg"), "New contact", [this]{ + this->newContact(); + }); // row context menu m_rowMenu = new QMenu(ui->contacts); m_rowMenu->addAction(QIcon(":/assets/images/copy.png"), "Copy address", this, &ContactsWidget::copyAddress); m_rowMenu->addAction(QIcon(":/assets/images/copy.png"), "Copy name", this, &ContactsWidget::copyName); - m_rowMenu->addAction(QIcon(":/assets/images/appicons/128x128.png"), "Pay to", this, &ContactsWidget::payTo); - m_deleteEntryAction = m_rowMenu->addAction("Delete", this, &ContactsWidget::deleteContact); + m_rowMenu->addAction("Pay to", this, &ContactsWidget::payTo); + m_rowMenu->addAction("Delete", this, &ContactsWidget::deleteContact); connect(ui->contacts, &QTreeView::customContextMenuRequested, [=](const QPoint & point){ QModelIndex index = ui->contacts->indexAt(point); @@ -95,16 +96,44 @@ void ContactsWidget::showHeaderMenu(const QPoint& position) m_headerMenu->exec(QCursor::pos()); } -void ContactsWidget::newContact() +void ContactsWidget::newContact(QString address, QString name) { - auto * dialog = new ContactsDialog(this); + auto * dialog = new ContactsDialog(this, address, name); int ret = dialog->exec(); if (!ret) return; - QString address = dialog->getAddress(); - QString name = dialog->getName(); + address = dialog->getAddress(); + name = dialog->getName(); - emit addContact(address, name); + bool addressValid = WalletManager::addressValid(address, m_ctx->currentWallet->nettype()); + if (!addressValid) { + QMessageBox::warning(this, "Invalid address", "Invalid address"); + return; + } + + int num_addresses = m_ctx->currentWallet->addressBook()->count(); + QString address_entry; + QString name_entry; + for (int i=0; icurrentWallet->addressBook()->getRow(i, [&address_entry, &name_entry](const AddressBookInfo &entry){ + address_entry = entry.address(); + name_entry = entry.description(); + }); + + if (address == address_entry) { + QMessageBox::warning(this, "Unable to add contact", "Duplicate address"); + ui->contacts->setCurrentIndex(m_model->index(i,0)); // Highlight duplicate address + return; + } + if (name == name_entry) { + QMessageBox::warning(this, "Unable to add contact", "Duplicate label"); + this->newContact(address, name); + return; + } + } + + m_ctx->currentWallet->addressBook()->addRow(address, "", name); + m_ctx->storeWallet(); } void ContactsWidget::deleteContact() diff --git a/src/contactswidget.h b/src/contactswidget.h index 3859d15..9d76bd5 100644 --- a/src/contactswidget.h +++ b/src/contactswidget.h @@ -6,6 +6,7 @@ #include "model/AddressBookModel.h" #include "model/AddressBookProxyModel.h" +#include "appcontext.h" #include #include @@ -21,29 +22,28 @@ class ContactsWidget : public QWidget public: explicit ContactsWidget(QWidget *parent = nullptr); void setModel(AddressBookModel * model); - ~ContactsWidget(); + ~ContactsWidget() override; public slots: void copyAddress(); void copyName(); void payTo(); - void newContact(); + void newContact(QString address = "", QString name = ""); void deleteContact(); void setShowFullAddresses(bool show); void setSearchFilter(const QString &filter); signals: - void addContact(QString &address, QString &name); void fillAddress(QString &address); private slots: - void showHeaderMenu(const QPoint& position); + void showHeaderMenu(const QPoint &position); private: Ui::ContactsWidget *ui; + AppContext *m_ctx; QAction *m_showFullAddressesAction; - QAction *m_deleteEntryAction; QMenu *m_rowMenu; QMenu *m_contextMenu; QMenu *m_headerMenu; diff --git a/src/dialog/contactsdialog.cpp b/src/dialog/contactsdialog.cpp index bf0e207..0ef59a4 100644 --- a/src/dialog/contactsdialog.cpp +++ b/src/dialog/contactsdialog.cpp @@ -4,13 +4,19 @@ #include "ui_contactsdialog.h" #include "contactsdialog.h" -ContactsDialog::ContactsDialog(QWidget *parent) +ContactsDialog::ContactsDialog(QWidget *parent, const QString &address, const QString &name) : QDialog(parent) , ui(new Ui::ContactsDialog) { ui->setupUi(this); setMinimumWidth(400); + ui->lineEdit_address->setText(address); + ui->lineEdit_name->setText(name); + if (!name.isEmpty()) { + ui->lineEdit_name->setFocus(); + } + connect(ui->buttonBox, &QDialogButtonBox::accepted, [&](){ m_address = ui->lineEdit_address->text(); m_name = ui->lineEdit_name->text(); diff --git a/src/dialog/contactsdialog.h b/src/dialog/contactsdialog.h index 1bb231e..2aa9ab1 100644 --- a/src/dialog/contactsdialog.h +++ b/src/dialog/contactsdialog.h @@ -15,7 +15,7 @@ class ContactsDialog : public QDialog Q_OBJECT public: - explicit ContactsDialog(QWidget *parent = nullptr); + explicit ContactsDialog(QWidget *parent = nullptr, const QString &address = "", const QString &name = ""); ~ContactsDialog() override; QString getAddress(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index da669e0..31c39ff 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -289,7 +289,6 @@ MainWindow::MainWindow(AppContext *ctx, QWidget *parent) : connect(ui->historyWidget, &HistoryWidget::resendTransaction, this, &MainWindow::onResendTransaction); // Contacts - connect(ui->contactWidget, &ContactsWidget::addContact, this, &MainWindow::onAddContact); connect(ui->contactWidget, &ContactsWidget::fillAddress, ui->sendWidget, &SendWidget::fillAddress); // Open alias @@ -1055,16 +1054,6 @@ void MainWindow::onResendTransaction(const QString &txid) { dialog->deleteLater(); } -void MainWindow::onAddContact(const QString &address, const QString &name) { - bool addressValid = WalletManager::addressValid(address, m_ctx->currentWallet->nettype()); - if (!addressValid) - QMessageBox::warning(this, "Invalid address", "Invalid address"); - else { - m_ctx->currentWallet->addressBook()->addRow(address, "", name); - m_ctx->storeWallet(); - } -} - void MainWindow::importContacts() { const QString targetFile = QFileDialog::getOpenFileName(this, "Import CSV file", QDir::homePath(), "CSV Files (*.csv)"); if(targetFile.isEmpty()) return; diff --git a/src/mainwindow.h b/src/mainwindow.h index d6234be..366a1d8 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -118,7 +118,6 @@ public slots: void onWalletOpenPasswordRequired(bool invalidPassword, const QString &path); void onViewOnBlockExplorer(const QString &txid); void onResendTransaction(const QString &txid); - void onAddContact(const QString &address, const QString &name); void importContacts(); void showRestoreHeightDialog(); void importTransaction();