2013-11-04 16:20:43 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-05-12 14:49:42 +02:00
|
|
|
#include "addresstablemodel.h"
|
2013-01-23 21:51:02 +01:00
|
|
|
|
2011-06-01 20:08:21 +02:00
|
|
|
#include "guiutil.h"
|
2011-07-16 19:01:05 +02:00
|
|
|
#include "walletmodel.h"
|
2011-06-26 19:23:24 +02:00
|
|
|
|
2012-05-14 23:44:52 +02:00
|
|
|
#include "base58.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "wallet.h"
|
2011-05-09 20:44:46 +02:00
|
|
|
|
2011-06-02 15:57:23 +02:00
|
|
|
#include <QFont>
|
2013-09-04 11:52:45 +02:00
|
|
|
#include <QDebug>
|
2011-06-02 15:57:23 +02:00
|
|
|
|
2011-05-13 15:58:27 +02:00
|
|
|
const QString AddressTableModel::Send = "S";
|
|
|
|
const QString AddressTableModel::Receive = "R";
|
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
struct AddressTableEntry
|
|
|
|
{
|
|
|
|
enum Type {
|
|
|
|
Sending,
|
2013-08-29 16:19:43 +02:00
|
|
|
Receiving,
|
|
|
|
Hidden /* QSortFilterProxyModel will filter these out */
|
2011-06-01 15:50:09 +02:00
|
|
|
};
|
2011-06-03 15:16:11 +02:00
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
Type type;
|
|
|
|
QString label;
|
|
|
|
QString address;
|
|
|
|
|
|
|
|
AddressTableEntry() {}
|
|
|
|
AddressTableEntry(Type type, const QString &label, const QString &address):
|
|
|
|
type(type), label(label), address(address) {}
|
|
|
|
};
|
|
|
|
|
2012-05-06 22:41:35 +02:00
|
|
|
struct AddressTableEntryLessThan
|
|
|
|
{
|
|
|
|
bool operator()(const AddressTableEntry &a, const AddressTableEntry &b) const
|
|
|
|
{
|
|
|
|
return a.address < b.address;
|
|
|
|
}
|
|
|
|
bool operator()(const AddressTableEntry &a, const QString &b) const
|
|
|
|
{
|
|
|
|
return a.address < b;
|
|
|
|
}
|
|
|
|
bool operator()(const QString &a, const AddressTableEntry &b) const
|
|
|
|
{
|
|
|
|
return a < b.address;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-29 16:19:43 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-06-18 11:53:25 +02:00
|
|
|
// Private implementation
|
2012-04-15 12:31:56 +02:00
|
|
|
class AddressTablePriv
|
2011-06-01 15:50:09 +02:00
|
|
|
{
|
2012-04-15 12:31:56 +02:00
|
|
|
public:
|
2011-06-26 19:23:24 +02:00
|
|
|
CWallet *wallet;
|
2011-06-01 15:50:09 +02:00
|
|
|
QList<AddressTableEntry> cachedAddressTable;
|
2012-05-06 22:41:35 +02:00
|
|
|
AddressTableModel *parent;
|
2011-06-01 15:50:09 +02:00
|
|
|
|
2012-05-06 22:41:35 +02:00
|
|
|
AddressTablePriv(CWallet *wallet, AddressTableModel *parent):
|
|
|
|
wallet(wallet), parent(parent) {}
|
2011-06-26 19:23:24 +02:00
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
void refreshAddressTable()
|
|
|
|
{
|
|
|
|
cachedAddressTable.clear();
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(wallet->cs_wallet);
|
2013-07-15 07:20:50 +02:00
|
|
|
BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, wallet->mapAddressBook)
|
2011-06-01 15:50:09 +02:00
|
|
|
{
|
2011-07-26 15:38:31 +02:00
|
|
|
const CBitcoinAddress& address = item.first;
|
2013-08-29 16:19:43 +02:00
|
|
|
bool fMine = IsMine(*wallet, address.Get());
|
|
|
|
AddressTableEntry::Type addressType = translateTransactionType(
|
|
|
|
QString::fromStdString(item.second.purpose), fMine);
|
2013-07-15 07:20:50 +02:00
|
|
|
const std::string& strName = item.second.name;
|
2013-07-22 08:50:39 +02:00
|
|
|
cachedAddressTable.append(AddressTableEntry(addressType,
|
2011-06-01 15:50:09 +02:00
|
|
|
QString::fromStdString(strName),
|
2011-07-26 15:38:31 +02:00
|
|
|
QString::fromStdString(address.ToString())));
|
2011-06-01 15:50:09 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 08:17:58 +01:00
|
|
|
// qLowerBound() and qUpperBound() require our cachedAddressTable list to be sorted in asc order
|
2013-08-30 08:25:25 +02:00
|
|
|
// 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.
|
2013-01-08 08:17:58 +01:00
|
|
|
qSort(cachedAddressTable.begin(), cachedAddressTable.end(), AddressTableEntryLessThan());
|
2011-06-01 15:50:09 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 16:19:43 +02:00
|
|
|
void updateEntry(const QString &address, const QString &label, bool isMine, const QString &purpose, int status)
|
2012-05-06 22:41:35 +02:00
|
|
|
{
|
|
|
|
// Find address / label in model
|
|
|
|
QList<AddressTableEntry>::iterator lower = qLowerBound(
|
|
|
|
cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
|
|
|
|
QList<AddressTableEntry>::iterator upper = qUpperBound(
|
|
|
|
cachedAddressTable.begin(), cachedAddressTable.end(), address, AddressTableEntryLessThan());
|
|
|
|
int lowerIndex = (lower - cachedAddressTable.begin());
|
|
|
|
int upperIndex = (upper - cachedAddressTable.begin());
|
|
|
|
bool inModel = (lower != upper);
|
2013-08-29 16:19:43 +02:00
|
|
|
AddressTableEntry::Type newEntryType = translateTransactionType(purpose, isMine);
|
2012-05-06 22:41:35 +02:00
|
|
|
|
|
|
|
switch(status)
|
|
|
|
{
|
|
|
|
case CT_NEW:
|
|
|
|
if(inModel)
|
|
|
|
{
|
2014-03-12 07:17:35 +01:00
|
|
|
qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_NEW, but entry is already in model";
|
2012-05-06 22:41:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent->beginInsertRows(QModelIndex(), lowerIndex, lowerIndex);
|
|
|
|
cachedAddressTable.insert(lowerIndex, AddressTableEntry(newEntryType, label, address));
|
|
|
|
parent->endInsertRows();
|
|
|
|
break;
|
|
|
|
case CT_UPDATED:
|
|
|
|
if(!inModel)
|
|
|
|
{
|
2013-09-04 11:52:45 +02:00
|
|
|
qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_UPDATED, but entry is not in model";
|
2012-05-06 22:41:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
lower->type = newEntryType;
|
|
|
|
lower->label = label;
|
|
|
|
parent->emitDataChanged(lowerIndex);
|
|
|
|
break;
|
|
|
|
case CT_DELETED:
|
|
|
|
if(!inModel)
|
|
|
|
{
|
2013-09-04 11:52:45 +02:00
|
|
|
qDebug() << "AddressTablePriv::updateEntry : Warning: Got CT_DELETED, but entry is not in model";
|
2012-05-06 22:41:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
parent->beginRemoveRows(QModelIndex(), lowerIndex, upperIndex-1);
|
|
|
|
cachedAddressTable.erase(lower, upper);
|
|
|
|
parent->endRemoveRows();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
int size()
|
|
|
|
{
|
|
|
|
return cachedAddressTable.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressTableEntry *index(int idx)
|
|
|
|
{
|
|
|
|
if(idx >= 0 && idx < cachedAddressTable.size())
|
|
|
|
{
|
|
|
|
return &cachedAddressTable[idx];
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
|
|
|
|
QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
|
2011-05-09 20:44:46 +02:00
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
columns << tr("Label") << tr("Address");
|
2012-05-06 22:41:35 +02:00
|
|
|
priv = new AddressTablePriv(wallet, this);
|
2011-06-01 15:50:09 +02:00
|
|
|
priv->refreshAddressTable();
|
|
|
|
}
|
2011-05-13 15:58:27 +02:00
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
AddressTableModel::~AddressTableModel()
|
|
|
|
{
|
|
|
|
delete priv;
|
2011-05-13 15:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int AddressTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2011-05-27 08:20:23 +02:00
|
|
|
Q_UNUSED(parent);
|
2011-06-01 15:50:09 +02:00
|
|
|
return priv->size();
|
2011-05-13 15:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int AddressTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
Q_UNUSED(parent);
|
|
|
|
return columns.length();
|
2011-05-13 15:58:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant AddressTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
|
|
|
|
2011-06-02 17:48:45 +02:00
|
|
|
if(role == Qt::DisplayRole || role == Qt::EditRole)
|
2011-05-13 15:58:27 +02:00
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
switch(index.column())
|
|
|
|
{
|
|
|
|
case Label:
|
2011-07-02 17:31:27 +02:00
|
|
|
if(rec->label.isEmpty() && role == Qt::DisplayRole)
|
2011-06-30 21:29:20 +02:00
|
|
|
{
|
|
|
|
return tr("(no label)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return rec->label;
|
|
|
|
}
|
2011-06-01 15:50:09 +02:00
|
|
|
case Address:
|
|
|
|
return rec->address;
|
|
|
|
}
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else if (role == Qt::FontRole)
|
2011-06-01 20:08:21 +02:00
|
|
|
{
|
2011-06-21 19:54:09 +02:00
|
|
|
QFont font;
|
2011-06-01 20:08:21 +02:00
|
|
|
if(index.column() == Address)
|
|
|
|
{
|
2011-06-21 19:54:09 +02:00
|
|
|
font = GUIUtil::bitcoinAddressFont();
|
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
2011-06-07 18:59:01 +02:00
|
|
|
else if (role == TypeRole)
|
2011-05-13 15:58:27 +02:00
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
switch(rec->type)
|
2011-05-13 15:58:27 +02:00
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
case AddressTableEntry::Sending:
|
|
|
|
return Send;
|
|
|
|
case AddressTableEntry::Receiving:
|
|
|
|
return Receive;
|
|
|
|
default: break;
|
2011-05-13 15:58:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:17:58 +01:00
|
|
|
bool AddressTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
2011-06-02 17:48:45 +02:00
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return false;
|
2011-06-03 15:16:11 +02:00
|
|
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
2013-07-22 08:50:39 +02:00
|
|
|
std::string strPurpose = (rec->type == AddressTableEntry::Sending ? "send" : "receive");
|
2011-07-16 19:01:05 +02:00
|
|
|
editStatus = OK;
|
|
|
|
|
2011-06-02 17:48:45 +02:00
|
|
|
if(role == Qt::EditRole)
|
|
|
|
{
|
2013-12-12 10:12:27 +01:00
|
|
|
LOCK(wallet->cs_wallet); /* For SetAddressBook / DelAddressBook */
|
|
|
|
CTxDestination curAddress = CBitcoinAddress(rec->address.toStdString()).Get();
|
|
|
|
if(index.column() == Label)
|
2011-06-02 17:48:45 +02:00
|
|
|
{
|
2013-01-08 08:17:58 +01:00
|
|
|
// Do nothing, if old label == new label
|
|
|
|
if(rec->label == value.toString())
|
|
|
|
{
|
|
|
|
editStatus = NO_CHANGES;
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-12 10:12:27 +01:00
|
|
|
wallet->SetAddressBook(curAddress, value.toString().toStdString(), strPurpose);
|
|
|
|
} else if(index.column() == Address) {
|
|
|
|
CTxDestination newAddress = CBitcoinAddress(value.toString().toStdString()).Get();
|
|
|
|
// Refuse to set invalid address, set error status and return false
|
|
|
|
if(boost::get<CNoDestination>(&newAddress))
|
2013-01-08 08:17:58 +01:00
|
|
|
{
|
2013-12-12 10:12:27 +01:00
|
|
|
editStatus = INVALID_ADDRESS;
|
2013-01-08 08:17:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-12 10:12:27 +01:00
|
|
|
// Do nothing, if old address == new address
|
|
|
|
else if(newAddress == curAddress)
|
2011-07-16 19:01:05 +02:00
|
|
|
{
|
2013-12-12 10:12:27 +01:00
|
|
|
editStatus = NO_CHANGES;
|
2011-07-02 17:31:27 +02:00
|
|
|
return false;
|
2011-07-16 19:01:05 +02:00
|
|
|
}
|
2013-01-08 08:17:58 +01:00
|
|
|
// Check for duplicate addresses to prevent accidental deletion of addresses, if you try
|
|
|
|
// to paste an existing address over another address (with a different label)
|
2013-12-12 10:12:27 +01:00
|
|
|
else if(wallet->mapAddressBook.count(newAddress))
|
2013-01-08 08:17:58 +01:00
|
|
|
{
|
|
|
|
editStatus = DUPLICATE_ADDRESS;
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-08 17:38:17 +02:00
|
|
|
// Double-check that we're not overwriting a receiving address
|
2013-01-08 08:17:58 +01:00
|
|
|
else if(rec->type == AddressTableEntry::Sending)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2013-12-12 10:12:27 +01:00
|
|
|
// Remove old entry
|
|
|
|
wallet->DelAddressBook(curAddress);
|
|
|
|
// Add new entry with new address
|
|
|
|
wallet->SetAddressBook(newAddress, rec->label.toStdString(), strPurpose);
|
2011-06-03 15:16:11 +02:00
|
|
|
}
|
2011-06-02 17:48:45 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-13 15:58:27 +02:00
|
|
|
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
if(orientation == Qt::Horizontal)
|
|
|
|
{
|
2013-11-05 18:03:05 +01:00
|
|
|
if(role == Qt::DisplayRole && section < columns.size())
|
2011-06-01 15:50:09 +02:00
|
|
|
{
|
|
|
|
return columns[section];
|
|
|
|
}
|
|
|
|
}
|
2011-05-13 15:58:27 +02:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:17:58 +01:00
|
|
|
Qt::ItemFlags AddressTableModel::flags(const QModelIndex &index) const
|
2011-07-02 17:31:27 +02:00
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return 0;
|
|
|
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
|
|
|
|
|
|
|
Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
// Can edit address and label for sending addresses,
|
|
|
|
// and only label for receiving addresses.
|
|
|
|
if(rec->type == AddressTableEntry::Sending ||
|
|
|
|
(rec->type == AddressTableEntry::Receiving && index.column()==Label))
|
|
|
|
{
|
|
|
|
retval |= Qt::ItemIsEditable;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-01-08 08:17:58 +01:00
|
|
|
QModelIndex AddressTableModel::index(int row, int column, const QModelIndex &parent) const
|
2011-05-13 15:58:27 +02:00
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
Q_UNUSED(parent);
|
|
|
|
AddressTableEntry *data = priv->index(row);
|
|
|
|
if(data)
|
|
|
|
{
|
|
|
|
return createIndex(row, column, priv->index(row));
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 15:50:09 +02:00
|
|
|
return QModelIndex();
|
|
|
|
}
|
2011-05-09 20:44:46 +02:00
|
|
|
}
|
2011-06-01 15:50:09 +02:00
|
|
|
|
2013-08-29 16:19:43 +02:00
|
|
|
void AddressTableModel::updateEntry(const QString &address,
|
|
|
|
const QString &label, bool isMine, const QString &purpose, int status)
|
2011-06-01 20:08:21 +02:00
|
|
|
{
|
2011-08-08 17:38:17 +02:00
|
|
|
// Update address book model from Bitcoin core
|
2013-08-29 16:19:43 +02:00
|
|
|
priv->updateEntry(address, label, isMine, purpose, status);
|
2011-06-01 20:08:21 +02:00
|
|
|
}
|
2011-06-03 15:16:11 +02:00
|
|
|
|
2011-07-02 13:45:59 +02:00
|
|
|
QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
|
|
|
std::string strLabel = label.toStdString();
|
|
|
|
std::string strAddress = address.toStdString();
|
|
|
|
|
2011-07-16 19:01:05 +02:00
|
|
|
editStatus = OK;
|
|
|
|
|
2011-06-03 15:16:11 +02:00
|
|
|
if(type == Send)
|
|
|
|
{
|
2011-07-16 19:01:05 +02:00
|
|
|
if(!walletModel->validateAddress(address))
|
|
|
|
{
|
|
|
|
editStatus = INVALID_ADDRESS;
|
|
|
|
return QString();
|
|
|
|
}
|
2011-08-08 17:38:17 +02:00
|
|
|
// Check for duplicate addresses
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(wallet->cs_wallet);
|
2012-05-14 23:44:52 +02:00
|
|
|
if(wallet->mapAddressBook.count(CBitcoinAddress(strAddress).Get()))
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2011-07-16 19:01:05 +02:00
|
|
|
editStatus = DUPLICATE_ADDRESS;
|
2011-06-03 21:03:20 +02:00
|
|
|
return QString();
|
2011-06-03 15:16:11 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else if(type == Receive)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2011-07-26 15:38:31 +02:00
|
|
|
// Generate a new address to associate with given label
|
2012-05-14 19:07:52 +02:00
|
|
|
CPubKey newKey;
|
2013-08-23 21:54:50 +02:00
|
|
|
if(!wallet->GetKeyFromPool(newKey))
|
2011-09-02 18:02:22 +02:00
|
|
|
{
|
2013-08-16 06:53:21 +02:00
|
|
|
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
|
|
|
|
if(!ctx.isValid())
|
|
|
|
{
|
|
|
|
// Unlock wallet failed or was cancelled
|
|
|
|
editStatus = WALLET_UNLOCK_FAILURE;
|
|
|
|
return QString();
|
|
|
|
}
|
2013-08-23 21:54:50 +02:00
|
|
|
if(!wallet->GetKeyFromPool(newKey))
|
2013-08-16 06:53:21 +02:00
|
|
|
{
|
|
|
|
editStatus = KEY_GENERATION_FAILURE;
|
|
|
|
return QString();
|
|
|
|
}
|
2011-09-02 18:02:22 +02:00
|
|
|
}
|
2012-05-14 23:44:52 +02:00
|
|
|
strAddress = CBitcoinAddress(newKey.GetID()).ToString();
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2011-06-03 21:03:20 +02:00
|
|
|
return QString();
|
2011-06-03 15:16:11 +02:00
|
|
|
}
|
2013-01-08 08:17:58 +01:00
|
|
|
|
2012-03-24 18:48:18 +01:00
|
|
|
// Add entry
|
2012-04-06 18:39:12 +02:00
|
|
|
{
|
|
|
|
LOCK(wallet->cs_wallet);
|
2013-07-22 08:50:39 +02:00
|
|
|
wallet->SetAddressBook(CBitcoinAddress(strAddress).Get(), strLabel,
|
|
|
|
(type == Send ? "send" : "receive"));
|
2012-04-06 18:39:12 +02:00
|
|
|
}
|
2011-06-03 21:03:20 +02:00
|
|
|
return QString::fromStdString(strAddress);
|
2011-06-03 15:16:11 +02:00
|
|
|
}
|
|
|
|
|
2013-01-08 08:17:58 +01:00
|
|
|
bool AddressTableModel::removeRows(int row, int count, const QModelIndex &parent)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
AddressTableEntry *rec = priv->index(row);
|
|
|
|
if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
|
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Can only remove one row at a time, and cannot remove rows not in model.
|
|
|
|
// Also refuse to remove receiving addresses.
|
2011-06-03 15:16:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-07 15:22:54 +02:00
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(wallet->cs_wallet);
|
2013-07-22 08:50:39 +02:00
|
|
|
wallet->DelAddressBook(CBitcoinAddress(rec->address.toStdString()).Get());
|
2011-07-07 15:22:54 +02:00
|
|
|
}
|
2011-06-03 15:16:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
2011-06-21 20:34:43 +02:00
|
|
|
|
2011-07-08 22:27:36 +02:00
|
|
|
/* Look up label for address in address book, if not found return empty string.
|
|
|
|
*/
|
|
|
|
QString AddressTableModel::labelForAddress(const QString &address) const
|
|
|
|
{
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(wallet->cs_wallet);
|
2011-07-26 15:38:31 +02:00
|
|
|
CBitcoinAddress address_parsed(address.toStdString());
|
2013-07-15 07:20:50 +02:00
|
|
|
std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get());
|
2011-07-08 22:27:36 +02:00
|
|
|
if (mi != wallet->mapAddressBook.end())
|
|
|
|
{
|
2013-07-15 07:20:50 +02:00
|
|
|
return QString::fromStdString(mi->second.name);
|
2011-07-08 22:27:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
int AddressTableModel::lookupAddress(const QString &address) const
|
|
|
|
{
|
|
|
|
QModelIndexList lst = match(index(0, Address, QModelIndex()),
|
|
|
|
Qt::EditRole, address, 1, Qt::MatchExactly);
|
|
|
|
if(lst.isEmpty())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return lst.at(0).row();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 22:41:35 +02:00
|
|
|
void AddressTableModel::emitDataChanged(int idx)
|
|
|
|
{
|
|
|
|
emit dataChanged(index(idx, 0, QModelIndex()), index(idx, columns.length()-1, QModelIndex()));
|
|
|
|
}
|