Browse Source

Finish implementation of address book

miguelfreitas
Wladimir J. van der Laan 14 years ago
parent
commit
48208883de
  1. 13
      gui/include/addresstablemodel.h
  2. 3
      gui/include/editaddressdialog.h
  3. 16
      gui/src/addressbookdialog.cpp
  4. 64
      gui/src/addresstablemodel.cpp
  5. 7
      gui/src/bitcoingui.cpp
  6. 32
      gui/src/editaddressdialog.cpp

13
gui/include/addresstablemodel.h

@ -13,10 +13,10 @@ public:
explicit AddressTableModel(QObject *parent = 0); explicit AddressTableModel(QObject *parent = 0);
~AddressTableModel(); ~AddressTableModel();
enum { enum ColumnIndex {
Label = 0, /* User specified label */ Label = 0, /* User specified label */
Address = 1 /* Bitcoin address */ Address = 1 /* Bitcoin address */
} ColumnIndex; };
enum { enum {
TypeRole = Qt::UserRole TypeRole = Qt::UserRole
@ -25,13 +25,22 @@ public:
static const QString Send; /* Send addres */ static const QString Send; /* Send addres */
static const QString Receive; /* Receive address */ static const QString Receive; /* Receive address */
/* Overridden methods from QAbstractTableModel */
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
bool setData(const QModelIndex & index, const QVariant & value, int role); bool setData(const QModelIndex & index, const QVariant & value, int role);
QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex & parent) const; QModelIndex index(int row, int column, const QModelIndex & parent) const;
bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
/* Add an address to the model.
Returns true on success, false otherwise.
*/
bool addRow(const QString &type, const QString &label, const QString &address);
/* Update address list from core. Invalidates any indices.
*/
void updateList(); void updateList();
private: private:
AddressTablePriv *priv; AddressTablePriv *priv;

3
gui/include/editaddressdialog.h

@ -29,10 +29,13 @@ public:
void setModel(AddressTableModel *model); void setModel(AddressTableModel *model);
void loadRow(int row); void loadRow(int row);
void saveCurrentRow();
private: private:
Ui::EditAddressDialog *ui; Ui::EditAddressDialog *ui;
QDataWidgetMapper *mapper; QDataWidgetMapper *mapper;
Mode mode;
AddressTableModel *model;
}; };
#endif // EDITADDRESSDIALOG_H #endif // EDITADDRESSDIALOG_H

16
gui/src/addressbookdialog.cpp

@ -100,7 +100,10 @@ void AddressBookDialog::on_editButton_clicked()
EditAddressDialog::EditReceivingAddress); EditAddressDialog::EditReceivingAddress);
dlg.setModel(model); dlg.setModel(model);
dlg.loadRow(indexes.at(0).row()); dlg.loadRow(indexes.at(0).row());
dlg.exec(); if(dlg.exec())
{
dlg.saveCurrentRow();
}
} }
void AddressBookDialog::on_newAddressButton_clicked() void AddressBookDialog::on_newAddressButton_clicked()
@ -110,7 +113,10 @@ void AddressBookDialog::on_newAddressButton_clicked()
EditAddressDialog::NewSendingAddress : EditAddressDialog::NewSendingAddress :
EditAddressDialog::NewReceivingAddress); EditAddressDialog::NewReceivingAddress);
dlg.setModel(model); dlg.setModel(model);
dlg.exec(); if(dlg.exec())
{
dlg.saveCurrentRow();
}
} }
void AddressBookDialog::on_tabWidget_currentChanged(int index) void AddressBookDialog::on_tabWidget_currentChanged(int index)
@ -130,9 +136,9 @@ void AddressBookDialog::on_deleteButton_clicked()
{ {
QTableView *table = getCurrentTable(); QTableView *table = getCurrentTable();
QModelIndexList indexes = table->selectionModel()->selectedRows(); QModelIndexList indexes = table->selectionModel()->selectedRows();
if(!indexes.isEmpty())
foreach (QModelIndex index, indexes) { {
table->model()->removeRow(index.row()); table->model()->removeRow(indexes.at(0).row());
} }
} }

64
gui/src/addresstablemodel.cpp

@ -13,6 +13,7 @@ struct AddressTableEntry
Sending, Sending,
Receiving Receiving
}; };
Type type; Type type;
QString label; QString label;
QString address; QString address;
@ -128,21 +129,31 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
{ {
if(!index.isValid()) if(!index.isValid())
return false; return false;
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
if(role == Qt::EditRole) if(role == Qt::EditRole)
{ {
switch(index.column()) switch(index.column())
{ {
case Label: case Label:
/* TODO */ SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
rec->label = value.toString();
break; break;
case Address: case Address:
/* TODO */
/* Double-check that we're not overwriting receiving address */ /* Double-check that we're not overwriting receiving address */
/* Note that changing address changes index in map */ if(rec->type == AddressTableEntry::Sending)
{
/* Remove old entry */
CWalletDB().EraseName(rec->address.toStdString());
/* Add new entry with new address */
SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
rec->address = value.toString();
}
break; break;
} }
/* emit dataChanged(index, index); */ emit dataChanged(index, index);
return true; return true;
} }
return false; return false;
@ -179,3 +190,48 @@ void AddressTableModel::updateList()
priv->refreshAddressTable(); priv->refreshAddressTable();
endResetModel(); endResetModel();
} }
bool AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
{
std::string strLabel = label.toStdString();
std::string strAddress = address.toStdString();
if(type == Send)
{
/* Check for duplicate */
CRITICAL_BLOCK(cs_mapAddressBook)
{
if(mapAddressBook.count(strAddress))
{
return false;
}
}
} else if(type == Receive)
{
/* Generate a new address to associate with given label */
strAddress = PubKeyToAddress(GetKeyFromKeyPool());
} else
{
return false;
}
/* Add entry and update list */
SetAddressBookName(strAddress, strLabel);
updateList();
return true;
}
bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
{
Q_UNUSED(parent);
AddressTableEntry *rec = priv->index(row);
if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
{
/* Can only remove one row at a time, and cannot remove rows not in model.
Also refuse to remove receiving addresses.
*/
return false;
}
CWalletDB().EraseName(rec->address.toStdString());
updateList();
return true;
}

7
gui/src/bitcoingui.cpp

@ -240,12 +240,7 @@ void BitcoinGUI::addressbookClicked()
{ {
AddressBookDialog dlg; AddressBookDialog dlg;
dlg.setTab(AddressBookDialog::SendingTab); dlg.setTab(AddressBookDialog::SendingTab);
/* if an address accepted, do a 'send' to specified address */ dlg.exec();
if(dlg.exec())
{
SendCoinsDialog send(0, dlg.getReturnValue());
send.exec();
}
} }
void BitcoinGUI::receivingAddressesClicked() void BitcoinGUI::receivingAddressesClicked()

32
gui/src/editaddressdialog.cpp

@ -4,11 +4,11 @@
#include "guiutil.h" #include "guiutil.h"
#include <QDataWidgetMapper> #include <QDataWidgetMapper>
#include <QDebug> #include <QMessageBox>
EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) : EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::EditAddressDialog), mapper(0) ui(new Ui::EditAddressDialog), mapper(0), mode(mode), model(0)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -33,7 +33,7 @@ EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) :
} }
mapper = new QDataWidgetMapper(this); mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
} }
EditAddressDialog::~EditAddressDialog() EditAddressDialog::~EditAddressDialog()
@ -43,7 +43,7 @@ EditAddressDialog::~EditAddressDialog()
void EditAddressDialog::setModel(AddressTableModel *model) void EditAddressDialog::setModel(AddressTableModel *model)
{ {
qDebug() << "setModel " << model; this->model = model;
mapper->setModel(model); mapper->setModel(model);
mapper->addMapping(ui->labelEdit, AddressTableModel::Label); mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
mapper->addMapping(ui->addressEdit, AddressTableModel::Address); mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
@ -51,6 +51,28 @@ void EditAddressDialog::setModel(AddressTableModel *model)
void EditAddressDialog::loadRow(int row) void EditAddressDialog::loadRow(int row)
{ {
qDebug() << "loadRow " << row;
mapper->setCurrentIndex(row); mapper->setCurrentIndex(row);
} }
void EditAddressDialog::saveCurrentRow()
{
switch(mode)
{
case NewReceivingAddress:
case NewSendingAddress:
if(!model->addRow(
mode == NewSendingAddress ? AddressTableModel::Send : AddressTableModel::Receive,
ui->labelEdit->text(),
ui->addressEdit->text()))
{
QMessageBox::warning(this, windowTitle(),
tr("The address %1 is already in the address book.").arg(ui->addressEdit->text()),
QMessageBox::Ok, QMessageBox::Ok);
}
break;
case EditReceivingAddress:
case EditSendingAddress:
mapper->submit();
break;
}
}

Loading…
Cancel
Save