From b9e80983a5c076fed655a7c3c67b53bd9ecc3dda Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Tue, 21 Jun 2011 20:34:43 +0200 Subject: Allow changing default address (fixes issue #6) --- src/qt/addresstablemodel.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'src/qt/addresstablemodel.cpp') diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 0558bfa453..1cd82b7626 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -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 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()); +} -- cgit v1.2.3