diff options
Diffstat (limited to 'src/qt/addresstablemodel.cpp')
-rw-r--r-- | src/qt/addresstablemodel.cpp | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 1801444521..921c4443a9 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -7,6 +7,7 @@ #include "base58.h" #include <QFont> +#include <QDebug> const QString AddressTableModel::Send = "S"; const QString AddressTableModel::Receive = "R"; @@ -15,7 +16,8 @@ struct AddressTableEntry { enum Type { Sending, - Receiving + Receiving, + Hidden /* QSortFilterProxyModel will filter these out */ }; Type type; @@ -43,6 +45,20 @@ struct AddressTableEntryLessThan } }; +/* Determine address type from address purpose */ +static AddressTableEntry::Type translateTransactionType(const QString &strPurpose, bool isMine) +{ + AddressTableEntry::Type addressType = AddressTableEntry::Hidden; + // "refund" addresses aren't shown, and change addresses aren't in mapAddressBook at all. + if (strPurpose == "send") + addressType = AddressTableEntry::Sending; + else if (strPurpose == "receive") + addressType = AddressTableEntry::Receiving; + else if (strPurpose == "unknown" || strPurpose == "") // if purpose not set, guess + addressType = (isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending); + return addressType; +} + // Private implementation class AddressTablePriv { @@ -59,21 +75,25 @@ public: cachedAddressTable.clear(); { LOCK(wallet->cs_wallet); - BOOST_FOREACH(const PAIRTYPE(CTxDestination, std::string)& item, wallet->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, wallet->mapAddressBook) { const CBitcoinAddress& address = item.first; - const std::string& strName = item.second; bool fMine = IsMine(*wallet, address.Get()); - cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending, + AddressTableEntry::Type addressType = translateTransactionType( + QString::fromStdString(item.second.purpose), fMine); + const std::string& strName = item.second.name; + cachedAddressTable.append(AddressTableEntry(addressType, QString::fromStdString(strName), QString::fromStdString(address.ToString()))); } } // qLowerBound() and qUpperBound() require our cachedAddressTable list to be sorted in asc order + // Even though the map is already sorted this re-sorting step is needed because the originating map + // is sorted by binary address, not by base58() address. qSort(cachedAddressTable.begin(), cachedAddressTable.end(), AddressTableEntryLessThan()); } - void updateEntry(const QString &address, const QString &label, bool isMine, int status) + void updateEntry(const QString &address, const QString &label, bool isMine, const QString &purpose, int status) { // Find address / label in model QList<AddressTableEntry>::iterator lower = qLowerBound( @@ -83,14 +103,14 @@ public: int lowerIndex = (lower - cachedAddressTable.begin()); int upperIndex = (upper - cachedAddressTable.begin()); bool inModel = (lower != upper); - AddressTableEntry::Type newEntryType = isMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending; + AddressTableEntry::Type newEntryType = translateTransactionType(purpose, isMine); switch(status) { case CT_NEW: if(inModel) { - OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_NOW, but entry is already in model\n"); + qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_NOW, but entry is already in model"; break; } parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex); @@ -100,7 +120,7 @@ public: case CT_UPDATED: if(!inModel) { - OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_UPDATED, but entry is not in model\n"); + qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_UPDATED, but entry is not in model"; break; } lower->type = newEntryType; @@ -110,7 +130,7 @@ public: case CT_DELETED: if(!inModel) { - OutputDebugStringF("Warning: AddressTablePriv::updateEntry: Got CT_DELETED, but entry is not in model\n"); + qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_DELETED, but entry is not in model"; break; } parent->beginRemoveRows(QModelIndex(), lowerIndex, upperIndex-1); @@ -215,7 +235,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, if(!index.isValid()) return false; AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer()); - + std::string strPurpose = (rec->type == AddressTableEntry::Sending ? "send" : "receive"); editStatus = OK; if(role == Qt::EditRole) @@ -229,7 +249,7 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, editStatus = NO_CHANGES; return false; } - wallet->SetAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get(), value.toString().toStdString()); + wallet->SetAddressBook(CBitcoinAddress(rec->address.toStdString()).Get(), value.toString().toStdString(), strPurpose); break; case Address: // Do nothing, if old address == new address @@ -257,9 +277,9 @@ bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, { LOCK(wallet->cs_wallet); // Remove old entry - wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get()); + wallet->DelAddressBook(CBitcoinAddress(rec->address.toStdString()).Get()); // Add new entry with new address - wallet->SetAddressBookName(CBitcoinAddress(value.toString().toStdString()).Get(), rec->label.toStdString()); + wallet->SetAddressBook(CBitcoinAddress(value.toString().toStdString()).Get(), rec->label.toStdString(), strPurpose); } } break; @@ -312,10 +332,11 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex &par } } -void AddressTableModel::updateEntry(const QString &address, const QString &label, bool isMine, int status) +void AddressTableModel::updateEntry(const QString &address, + const QString &label, bool isMine, const QString &purpose, int status) { // Update address book model from Bitcoin core - priv->updateEntry(address, label, isMine, status); + priv->updateEntry(address, label, isMine, purpose, status); } QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address) @@ -345,18 +366,21 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con else if(type == Receive) { // Generate a new address to associate with given label - WalletModel::UnlockContext ctx(walletModel->requestUnlock()); - if(!ctx.isValid()) - { - // Unlock wallet failed or was cancelled - editStatus = WALLET_UNLOCK_FAILURE; - return QString(); - } CPubKey newKey; - if(!wallet->GetKeyFromPool(newKey, true)) + if(!wallet->GetKeyFromPool(newKey)) { - editStatus = KEY_GENERATION_FAILURE; - return QString(); + WalletModel::UnlockContext ctx(walletModel->requestUnlock()); + if(!ctx.isValid()) + { + // Unlock wallet failed or was cancelled + editStatus = WALLET_UNLOCK_FAILURE; + return QString(); + } + if(!wallet->GetKeyFromPool(newKey)) + { + editStatus = KEY_GENERATION_FAILURE; + return QString(); + } } strAddress = CBitcoinAddress(newKey.GetID()).ToString(); } @@ -368,7 +392,8 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con // Add entry { LOCK(wallet->cs_wallet); - wallet->SetAddressBookName(CBitcoinAddress(strAddress).Get(), strLabel); + wallet->SetAddressBook(CBitcoinAddress(strAddress).Get(), strLabel, + (type == Send ? "send" : "receive")); } return QString::fromStdString(strAddress); } @@ -385,7 +410,7 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent } { LOCK(wallet->cs_wallet); - wallet->DelAddressBookName(CBitcoinAddress(rec->address.toStdString()).Get()); + wallet->DelAddressBook(CBitcoinAddress(rec->address.toStdString()).Get()); } return true; } @@ -397,10 +422,10 @@ QString AddressTableModel::labelForAddress(const QString &address) const { LOCK(wallet->cs_wallet); CBitcoinAddress address_parsed(address.toStdString()); - std::map<CTxDestination, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get()); + std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get()); if (mi != wallet->mapAddressBook.end()) { - return QString::fromStdString(mi->second); + return QString::fromStdString(mi->second.name); } } return QString(); |