Changeset View
Changeset View
Standalone View
Standalone View
src/qt/addresstablemodel.cpp
Show First 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | AddressTablePriv(CWallet *_wallet, AddressTableModel *_parent) | ||||
: wallet(_wallet), parent(_parent) {} | : wallet(_wallet), parent(_parent) {} | ||||
void refreshAddressTable() { | void refreshAddressTable() { | ||||
cachedAddressTable.clear(); | cachedAddressTable.clear(); | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
for (const std::pair<CTxDestination, CAddressBookData> &item : | for (const std::pair<CTxDestination, CAddressBookData> &item : | ||||
wallet->mapAddressBook) { | wallet->mapAddressBook) { | ||||
const CBitcoinAddress &address = item.first; | const CTxDestination &address = item.first; | ||||
bool fMine = IsMine(*wallet, address.Get()); | bool fMine = IsMine(*wallet, address); | ||||
AddressTableEntry::Type addressType = translateTransactionType( | AddressTableEntry::Type addressType = translateTransactionType( | ||||
QString::fromStdString(item.second.purpose), fMine); | QString::fromStdString(item.second.purpose), fMine); | ||||
const std::string &strName = item.second.name; | const std::string &strName = item.second.name; | ||||
cachedAddressTable.append(AddressTableEntry( | cachedAddressTable.append(AddressTableEntry( | ||||
addressType, QString::fromStdString(strName), | addressType, QString::fromStdString(strName), | ||||
QString::fromStdString(address.ToString()))); | QString::fromStdString(EncodeDestination(address)))); | ||||
} | } | ||||
} | } | ||||
// qLowerBound() and qUpperBound() require our cachedAddressTable list | // qLowerBound() and qUpperBound() require our cachedAddressTable list | ||||
// to be sorted in asc order. Even though the map is already sorted this | // 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 | // re-sorting step is needed because the originating map is sorted by | ||||
// binary address, not by base58() address. | // binary address, not by base58() address. | ||||
qSort(cachedAddressTable.begin(), cachedAddressTable.end(), | qSort(cachedAddressTable.begin(), cachedAddressTable.end(), | ||||
AddressTableEntryLessThan()); | AddressTableEntryLessThan()); | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, | ||||
if (!index.isValid()) return false; | if (!index.isValid()) return false; | ||||
AddressTableEntry *rec = | AddressTableEntry *rec = | ||||
static_cast<AddressTableEntry *>(index.internalPointer()); | static_cast<AddressTableEntry *>(index.internalPointer()); | ||||
std::string strPurpose = | std::string strPurpose = | ||||
(rec->type == AddressTableEntry::Sending ? "send" : "receive"); | (rec->type == AddressTableEntry::Sending ? "send" : "receive"); | ||||
editStatus = OK; | editStatus = OK; | ||||
if (role == Qt::EditRole) { | if (role == Qt::EditRole) { | ||||
/* For SetAddressBook / DelAddressBook */ | LOCK(wallet->cs_wallet); /* For SetAddressBook / DelAddressBook */ | ||||
LOCK(wallet->cs_wallet); | |||||
CTxDestination curAddress = | CTxDestination curAddress = | ||||
CBitcoinAddress(rec->address.toStdString()).Get(); | DecodeDestination(rec->address.toStdString()); | ||||
if (index.column() == Label) { | if (index.column() == Label) { | ||||
// Do nothing, if old label == new label | // Do nothing, if old label == new label | ||||
if (rec->label == value.toString()) { | if (rec->label == value.toString()) { | ||||
editStatus = NO_CHANGES; | editStatus = NO_CHANGES; | ||||
return false; | return false; | ||||
} | } | ||||
wallet->SetAddressBook(curAddress, value.toString().toStdString(), | wallet->SetAddressBook(curAddress, value.toString().toStdString(), | ||||
strPurpose); | strPurpose); | ||||
} else if (index.column() == Address) { | } else if (index.column() == Address) { | ||||
CTxDestination newAddress = | CTxDestination newAddress = | ||||
CBitcoinAddress(value.toString().toStdString()).Get(); | DecodeDestination(value.toString().toStdString()); | ||||
// Refuse to set invalid address, set error status and return false | // Refuse to set invalid address, set error status and return false | ||||
if (boost::get<CNoDestination>(&newAddress)) { | if (boost::get<CNoDestination>(&newAddress)) { | ||||
editStatus = INVALID_ADDRESS; | editStatus = INVALID_ADDRESS; | ||||
return false; | return false; | ||||
} | } | ||||
// Do nothing, if old address == new address | // Do nothing, if old address == new address | ||||
else if (newAddress == curAddress) { | else if (newAddress == curAddress) { | ||||
editStatus = NO_CHANGES; | editStatus = NO_CHANGES; | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | QString AddressTableModel::addRow(const QString &type, const QString &label, | ||||
if (type == Send) { | if (type == Send) { | ||||
if (!walletModel->validateAddress(address)) { | if (!walletModel->validateAddress(address)) { | ||||
editStatus = INVALID_ADDRESS; | editStatus = INVALID_ADDRESS; | ||||
return QString(); | return QString(); | ||||
} | } | ||||
// Check for duplicate addresses | // Check for duplicate addresses | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
if (wallet->mapAddressBook.count( | if (wallet->mapAddressBook.count(DecodeDestination(strAddress))) { | ||||
CBitcoinAddress(strAddress).Get())) { | |||||
editStatus = DUPLICATE_ADDRESS; | editStatus = DUPLICATE_ADDRESS; | ||||
return QString(); | return QString(); | ||||
} | } | ||||
} | } | ||||
} 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 | ||||
CPubKey newKey; | CPubKey newKey; | ||||
if (!wallet->GetKeyFromPool(newKey)) { | if (!wallet->GetKeyFromPool(newKey)) { | ||||
WalletModel::UnlockContext ctx(walletModel->requestUnlock()); | WalletModel::UnlockContext ctx(walletModel->requestUnlock()); | ||||
if (!ctx.isValid()) { | if (!ctx.isValid()) { | ||||
// Unlock wallet failed or was cancelled | // Unlock wallet failed or was cancelled | ||||
editStatus = WALLET_UNLOCK_FAILURE; | editStatus = WALLET_UNLOCK_FAILURE; | ||||
return QString(); | return QString(); | ||||
} | } | ||||
if (!wallet->GetKeyFromPool(newKey)) { | if (!wallet->GetKeyFromPool(newKey)) { | ||||
editStatus = KEY_GENERATION_FAILURE; | editStatus = KEY_GENERATION_FAILURE; | ||||
return QString(); | return QString(); | ||||
} | } | ||||
} | } | ||||
strAddress = CBitcoinAddress(newKey.GetID()).ToString(); | strAddress = EncodeDestination(newKey.GetID()); | ||||
} else { | } else { | ||||
return QString(); | return QString(); | ||||
} | } | ||||
// Add entry | // Add entry | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
wallet->SetAddressBook(CBitcoinAddress(strAddress).Get(), strLabel, | wallet->SetAddressBook(DecodeDestination(strAddress), strLabel, | ||||
(type == Send ? "send" : "receive")); | (type == Send ? "send" : "receive")); | ||||
} | } | ||||
return QString::fromStdString(strAddress); | return QString::fromStdString(strAddress); | ||||
} | } | ||||
bool AddressTableModel::removeRows(int row, int count, | bool AddressTableModel::removeRows(int row, int count, | ||||
const QModelIndex &parent) { | const QModelIndex &parent) { | ||||
Q_UNUSED(parent); | Q_UNUSED(parent); | ||||
AddressTableEntry *rec = priv->index(row); | AddressTableEntry *rec = priv->index(row); | ||||
if (count != 1 || !rec || rec->type == AddressTableEntry::Receiving) { | if (count != 1 || !rec || rec->type == AddressTableEntry::Receiving) { | ||||
// Can only remove one row at a time, and cannot remove rows not in | // Can only remove one row at a time, and cannot remove rows not in | ||||
// model. | // model. | ||||
// Also refuse to remove receiving addresses. | // Also refuse to remove receiving addresses. | ||||
return false; | return false; | ||||
} | } | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
wallet->DelAddressBook( | wallet->DelAddressBook(DecodeDestination(rec->address.toStdString())); | ||||
CBitcoinAddress(rec->address.toStdString()).Get()); | |||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
/* Look up label for address in address book, if not found return empty string. | /* Look up label for address in address book, if not found return empty string. | ||||
*/ | */ | ||||
QString AddressTableModel::labelForAddress(const QString &address) const { | QString AddressTableModel::labelForAddress(const QString &address) const { | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
CBitcoinAddress address_parsed(address.toStdString()); | CTxDestination destination = DecodeDestination(address.toStdString()); | ||||
std::map<CTxDestination, CAddressBookData>::iterator mi = | std::map<CTxDestination, CAddressBookData>::iterator mi = | ||||
wallet->mapAddressBook.find(address_parsed.Get()); | wallet->mapAddressBook.find(destination); | ||||
if (mi != wallet->mapAddressBook.end()) { | if (mi != wallet->mapAddressBook.end()) { | ||||
return QString::fromStdString(mi->second.name); | return QString::fromStdString(mi->second.name); | ||||
} | } | ||||
} | } | ||||
return QString(); | return QString(); | ||||
} | } | ||||
int AddressTableModel::lookupAddress(const QString &address) const { | int AddressTableModel::lookupAddress(const QString &address) const { | ||||
Show All 13 Lines |