Allow changing default address (fixes issue #6)

This commit is contained in:
Wladimir J. van der Laan 2011-06-21 20:34:43 +02:00
parent f5927f5b32
commit b9e80983a5
7 changed files with 74 additions and 42 deletions

View file

@ -117,6 +117,8 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
return rec->label;
case Address:
return rec->address;
case IsDefaultAddress:
return rec->isDefaultAddress();
}
}
else if (role == Qt::FontRole)
@ -187,6 +189,12 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
rec->address = value.toString();
}
break;
case IsDefaultAddress:
if(value.toBool())
{
setDefaultAddress(rec->address);
}
break;
}
emit dataChanged(index, index);
@ -229,7 +237,7 @@ void AddressTableModel::updateList()
endResetModel();
}
QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
{
std::string strLabel = label.toStdString();
std::string strAddress = address.toStdString();
@ -247,8 +255,13 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
}
else if(type == Receive)
{
// Generate a new address to associate with given label
// Generate a new address to associate with given label, optionally
// set as default receiving address.
strAddress = PubKeyToAddress(GetKeyFromKeyPool());
if(setAsDefault)
{
setDefaultAddress(QString::fromStdString(strAddress));
}
}
else
{
@ -274,3 +287,32 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
updateList();
return true;
}
QString AddressTableModel::getDefaultAddress() const
{
std::vector<unsigned char> vchPubKey;
if (CWalletDB("r").ReadDefaultKey(vchPubKey))
{
return QString::fromStdString(PubKeyToAddress(vchPubKey));
}
else
{
return QString();
}
}
void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
{
uint160 hash160;
std::string strAddress = defaultAddress.toStdString();
if (!AddressToHash160(strAddress, hash160))
return;
if (!mapPubKeys.count(hash160))
return;
CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
}
void AddressTableModel::update()
{
emit defaultAddressChanged(getDefaultAddress());
}

View file

@ -15,7 +15,8 @@ public:
enum ColumnIndex {
Label = 0, /* User specified label */
Address = 1 /* Bitcoin address */
Address = 1, /* Bitcoin address */
IsDefaultAddress = 2 /* Is default address? */
};
enum {
@ -37,18 +38,25 @@ public:
/* Add an address to the model.
Returns the added address on success, and an empty string otherwise.
*/
QString addRow(const QString &type, const QString &label, const QString &address);
QString addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault);
/* Set and get default address */
QString getDefaultAddress() const;
void setDefaultAddress(const QString &defaultAddress);
/* Update address list from core. Invalidates any indices.
*/
void updateList();
private:
AddressTablePriv *priv;
QStringList columns;
signals:
void defaultAddressChanged(const QString &address);
public slots:
void update();
};
#endif // ADDRESSTABLEMODEL_H

View file

@ -14,6 +14,7 @@
#include "editaddressdialog.h"
#include "optionsmodel.h"
#include "transactiondescdialog.h"
#include "addresstablemodel.h"
#include "main.h"
@ -188,8 +189,8 @@ void BitcoinGUI::setModel(ClientModel *model)
setNumBlocks(model->getNumBlocks());
connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
setAddress(model->getAddress());
connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
setAddress(model->getAddressTableModel()->getDefaultAddress());
connect(model->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
// Report errors from network/worker thread
connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
@ -329,11 +330,6 @@ void BitcoinGUI::newAddressClicked()
if(dlg.exec())
{
QString newAddress = dlg.saveCurrentRow();
// Set returned address as new default addres
if(!newAddress.isEmpty())
{
model->setAddress(newAddress);
}
}
}

View file

@ -27,19 +27,6 @@ qint64 ClientModel::getBalance() const
return GetBalance();
}
QString ClientModel::getAddress() const
{
std::vector<unsigned char> vchPubKey;
if (CWalletDB("r").ReadDefaultKey(vchPubKey))
{
return QString::fromStdString(PubKeyToAddress(vchPubKey));
}
else
{
return QString();
}
}
int ClientModel::getNumConnections() const
{
return vNodes.size();
@ -63,23 +50,14 @@ int ClientModel::getNumTransactions() const
void ClientModel::update()
{
// Plainly emit all signals for now. To be more efficient this should check
// whether the values actually changed first.
// whether the values actually changed first, although it'd be even better if these
// were events coming in from the bitcoin core.
emit balanceChanged(getBalance());
emit addressChanged(getAddress());
emit numConnectionsChanged(getNumConnections());
emit numBlocksChanged(getNumBlocks());
emit numTransactionsChanged(getNumTransactions());
}
void ClientModel::setAddress(const QString &defaultAddress)
{
uint160 hash160;
std::string strAddress = defaultAddress.toStdString();
if (!AddressToHash160(strAddress, hash160))
return;
if (!mapPubKeys.count(hash160))
return;
CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
addressTableModel->update();
}
ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)

View file

@ -29,7 +29,6 @@ public:
TransactionTableModel *getTransactionTableModel();
qint64 getBalance() const;
QString getAddress() const;
int getNumConnections() const;
int getNumBlocks() const;
int getNumTransactions() const;
@ -39,8 +38,6 @@ public:
/* Return conservative estimate of total number of blocks, or 0 if unknown */
int getTotalBlocksEstimate() const;
/* Set default address */
void setAddress(const QString &defaultAddress);
/* Send coins */
StatusCode sendCoins(const QString &payTo, qint64 payAmount);
private:
@ -50,7 +47,6 @@ private:
signals:
void balanceChanged(qint64 balance);
void addressChanged(const QString &address);
void numConnectionsChanged(int count);
void numBlocksChanged(int count);
void numTransactionsChanged(int count);

View file

@ -19,9 +19,11 @@ EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) :
case NewReceivingAddress:
setWindowTitle(tr("New receiving address"));
ui->addressEdit->setEnabled(false);
ui->setAsDefault->setChecked(true);
break;
case NewSendingAddress:
setWindowTitle(tr("New sending address"));
ui->setAsDefault->setVisible(false);
break;
case EditReceivingAddress:
setWindowTitle(tr("Edit receiving address"));
@ -29,6 +31,7 @@ EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) :
break;
case EditSendingAddress:
setWindowTitle(tr("Edit sending address"));
ui->setAsDefault->setVisible(false);
break;
}
@ -47,6 +50,7 @@ void EditAddressDialog::setModel(AddressTableModel *model)
mapper->setModel(model);
mapper->addMapping(ui->labelEdit, AddressTableModel::Label);
mapper->addMapping(ui->addressEdit, AddressTableModel::Address);
mapper->addMapping(ui->setAsDefault, AddressTableModel::IsDefaultAddress);
}
void EditAddressDialog::loadRow(int row)
@ -64,7 +68,8 @@ QString EditAddressDialog::saveCurrentRow()
address = model->addRow(
mode == NewSendingAddress ? AddressTableModel::Send : AddressTableModel::Receive,
ui->labelEdit->text(),
ui->addressEdit->text());
ui->addressEdit->text(),
ui->setAsDefault->isChecked());
if(address.isEmpty())
{
QMessageBox::warning(this, windowTitle(),

View file

@ -53,6 +53,13 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="setAsDefault">
<property name="text">
<string>Set as default address</string>
</property>
</widget>
</item>
</layout>
</item>
<item>