Browse Source

Allow changing default address (fixes issue #6)

0.8
Wladimir J. van der Laan 14 years ago
parent
commit
b9e80983a5
  1. 46
      src/qt/addresstablemodel.cpp
  2. 14
      src/qt/addresstablemodel.h
  3. 10
      src/qt/bitcoingui.cpp
  4. 28
      src/qt/clientmodel.cpp
  5. 4
      src/qt/clientmodel.h
  6. 7
      src/qt/editaddressdialog.cpp
  7. 7
      src/qt/forms/editaddressdialog.ui

46
src/qt/addresstablemodel.cpp

@ -117,6 +117,8 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
return rec->label; return rec->label;
case Address: case Address:
return rec->address; return rec->address;
case IsDefaultAddress:
return rec->isDefaultAddress();
} }
} }
else if (role == Qt::FontRole) else if (role == Qt::FontRole)
@ -187,6 +189,12 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
rec->address = value.toString(); rec->address = value.toString();
} }
break; break;
case IsDefaultAddress:
if(value.toBool())
{
setDefaultAddress(rec->address);
}
break;
} }
emit dataChanged(index, index); emit dataChanged(index, index);
@ -229,7 +237,7 @@ void AddressTableModel::updateList()
endResetModel(); 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 strLabel = label.toStdString();
std::string strAddress = address.toStdString(); std::string strAddress = address.toStdString();
@ -247,8 +255,13 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
} }
else if(type == Receive) 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()); strAddress = PubKeyToAddress(GetKeyFromKeyPool());
if(setAsDefault)
{
setDefaultAddress(QString::fromStdString(strAddress));
}
} }
else else
{ {
@ -274,3 +287,32 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
updateList(); updateList();
return true; 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());
}

14
src/qt/addresstablemodel.h

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

10
src/qt/bitcoingui.cpp

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

28
src/qt/clientmodel.cpp

@ -27,19 +27,6 @@ qint64 ClientModel::getBalance() const
return GetBalance(); 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 int ClientModel::getNumConnections() const
{ {
return vNodes.size(); return vNodes.size();
@ -63,23 +50,14 @@ int ClientModel::getNumTransactions() const
void ClientModel::update() void ClientModel::update()
{ {
// Plainly emit all signals for now. To be more efficient this should check // 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 balanceChanged(getBalance());
emit addressChanged(getAddress());
emit numConnectionsChanged(getNumConnections()); emit numConnectionsChanged(getNumConnections());
emit numBlocksChanged(getNumBlocks()); emit numBlocksChanged(getNumBlocks());
emit numTransactionsChanged(getNumTransactions()); emit numTransactionsChanged(getNumTransactions());
}
void ClientModel::setAddress(const QString &defaultAddress) addressTableModel->update();
{
uint160 hash160;
std::string strAddress = defaultAddress.toStdString();
if (!AddressToHash160(strAddress, hash160))
return;
if (!mapPubKeys.count(hash160))
return;
CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
} }
ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount) ClientModel::StatusCode ClientModel::sendCoins(const QString &payTo, qint64 payAmount)

4
src/qt/clientmodel.h

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

7
src/qt/editaddressdialog.cpp

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

7
src/qt/forms/editaddressdialog.ui

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

Loading…
Cancel
Save