diff --git a/gui/include/addresstablemodel.h b/gui/include/addresstablemodel.h index 18902609..87994143 100644 --- a/gui/include/addresstablemodel.h +++ b/gui/include/addresstablemodel.h @@ -35,9 +35,9 @@ public: bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); /* Add an address to the model. - Returns true on success, false otherwise. + Returns the added address on success, and an empty string otherwise. */ - bool addRow(const QString &type, const QString &label, const QString &address); + QString addRow(const QString &type, const QString &label, const QString &address); /* Update address list from core. Invalidates any indices. */ diff --git a/gui/include/clientmodel.h b/gui/include/clientmodel.h index 01c0d70f..d68b34fe 100644 --- a/gui/include/clientmodel.h +++ b/gui/include/clientmodel.h @@ -2,7 +2,9 @@ #define CLIENTMODEL_H #include + class OptionsModel; +class AddressTableModel; class ClientModel : public QObject { @@ -22,6 +24,7 @@ public: }; OptionsModel *getOptionsModel(); + AddressTableModel *getAddressTableModel(); qint64 getBalance(); QString getAddress(); @@ -34,7 +37,8 @@ public: /* Send coins */ StatusCode sendCoins(const QString &payTo, qint64 payAmount); private: - OptionsModel *options_model; + OptionsModel *optionsModel; + AddressTableModel *addressTableModel; signals: void balanceChanged(qint64 balance); diff --git a/gui/include/editaddressdialog.h b/gui/include/editaddressdialog.h index dd776695..6f396d04 100644 --- a/gui/include/editaddressdialog.h +++ b/gui/include/editaddressdialog.h @@ -29,7 +29,7 @@ public: void setModel(AddressTableModel *model); void loadRow(int row); - void saveCurrentRow(); + QString saveCurrentRow(); private: Ui::EditAddressDialog *ui; diff --git a/gui/src/addressbookdialog.cpp b/gui/src/addressbookdialog.cpp index 9b9e9bbc..35078d3a 100644 --- a/gui/src/addressbookdialog.cpp +++ b/gui/src/addressbookdialog.cpp @@ -14,9 +14,6 @@ AddressBookDialog::AddressBookDialog(QWidget *parent) : model(0) { ui->setupUi(this); - - model = new AddressTableModel(this); - setModel(model); } AddressBookDialog::~AddressBookDialog() @@ -26,6 +23,9 @@ AddressBookDialog::~AddressBookDialog() void AddressBookDialog::setModel(AddressTableModel *model) { + /* Refresh list from core */ + model->updateList(); + /* Receive filter */ QSortFilterProxyModel *receive_model = new QSortFilterProxyModel(this); receive_model->setSourceModel(model); diff --git a/gui/src/addresstablemodel.cpp b/gui/src/addresstablemodel.cpp index fbb2eb52..1cbc1b5d 100644 --- a/gui/src/addresstablemodel.cpp +++ b/gui/src/addresstablemodel.cpp @@ -191,7 +191,7 @@ void AddressTableModel::updateList() endResetModel(); } -bool AddressTableModel::addRow(const QString &type, const QString &label, const QString &address) +QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address) { std::string strLabel = label.toStdString(); std::string strAddress = address.toStdString(); @@ -203,7 +203,7 @@ bool AddressTableModel::addRow(const QString &type, const QString &label, const { if(mapAddressBook.count(strAddress)) { - return false; + return QString(); } } } else if(type == Receive) @@ -212,12 +212,12 @@ bool AddressTableModel::addRow(const QString &type, const QString &label, const strAddress = PubKeyToAddress(GetKeyFromKeyPool()); } else { - return false; + return QString(); } /* Add entry and update list */ SetAddressBookName(strAddress, strLabel); updateList(); - return true; + return QString::fromStdString(strAddress); } bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent) diff --git a/gui/src/bitcoingui.cpp b/gui/src/bitcoingui.cpp index ba5b1d99..59964960 100644 --- a/gui/src/bitcoingui.cpp +++ b/gui/src/bitcoingui.cpp @@ -11,6 +11,7 @@ #include "aboutdialog.h" #include "clientmodel.h" #include "guiutil.h" +#include "editaddressdialog.h" #include "main.h" @@ -239,6 +240,7 @@ void BitcoinGUI::sendcoinsClicked() void BitcoinGUI::addressbookClicked() { AddressBookDialog dlg; + dlg.setModel(model->getAddressTableModel()); dlg.setTab(AddressBookDialog::SendingTab); dlg.exec(); } @@ -246,6 +248,7 @@ void BitcoinGUI::addressbookClicked() void BitcoinGUI::receivingAddressesClicked() { AddressBookDialog dlg; + dlg.setModel(model->getAddressTableModel()); dlg.setTab(AddressBookDialog::ReceivingTab); dlg.exec(); } @@ -265,8 +268,17 @@ void BitcoinGUI::aboutClicked() void BitcoinGUI::newAddressClicked() { - qDebug() << "New address clicked"; - /* TODO: generate new address */ + EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress); + dlg.setModel(model->getAddressTableModel()); + if(dlg.exec()) + { + QString newAddress = dlg.saveCurrentRow(); + /* Set returned address as new default address */ + if(!newAddress.isEmpty()) + { + model->setAddress(newAddress); + } + } } void BitcoinGUI::copyClipboardClicked() diff --git a/gui/src/clientmodel.cpp b/gui/src/clientmodel.cpp index 7dcbc576..497f8dc3 100644 --- a/gui/src/clientmodel.cpp +++ b/gui/src/clientmodel.cpp @@ -2,11 +2,12 @@ #include "main.h" #include "guiconstants.h" #include "optionsmodel.h" +#include "addresstablemodel.h" #include ClientModel::ClientModel(QObject *parent) : - QObject(parent), options_model(0) + QObject(parent), optionsModel(0), addressTableModel(0) { /* Until we build signal notifications into the bitcoin core, simply update everything using a timer. @@ -15,7 +16,8 @@ ClientModel::ClientModel(QObject *parent) : connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(MODEL_UPDATE_DELAY); - options_model = new OptionsModel(this); + optionsModel = new OptionsModel(this); + addressTableModel = new AddressTableModel(this); } qint64 ClientModel::getBalance() @@ -128,5 +130,10 @@ ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payA OptionsModel *ClientModel::getOptionsModel() { - return options_model; + return optionsModel; +} + +AddressTableModel *ClientModel::getAddressTableModel() +{ + return addressTableModel; } diff --git a/gui/src/editaddressdialog.cpp b/gui/src/editaddressdialog.cpp index ddc7292c..dd054176 100644 --- a/gui/src/editaddressdialog.cpp +++ b/gui/src/editaddressdialog.cpp @@ -54,16 +54,18 @@ void EditAddressDialog::loadRow(int row) mapper->setCurrentIndex(row); } -void EditAddressDialog::saveCurrentRow() +QString EditAddressDialog::saveCurrentRow() { + QString address; switch(mode) { case NewReceivingAddress: case NewSendingAddress: - if(!model->addRow( + address = model->addRow( mode == NewSendingAddress ? AddressTableModel::Send : AddressTableModel::Receive, ui->labelEdit->text(), - ui->addressEdit->text())) + ui->addressEdit->text()); + if(address.isEmpty()) { QMessageBox::warning(this, windowTitle(), tr("The address %1 is already in the address book.").arg(ui->addressEdit->text()), @@ -72,7 +74,11 @@ void EditAddressDialog::saveCurrentRow() break; case EditReceivingAddress: case EditSendingAddress: - mapper->submit(); + if(mapper->submit()) + { + address = ui->addressEdit->text(); + } break; } + return address; }