2011-05-12 14:49:42 +02:00
|
|
|
#include "addresstablemodel.h"
|
2011-06-01 20:08:21 +02:00
|
|
|
#include "guiutil.h"
|
2011-06-26 19:23:24 +02:00
|
|
|
|
|
|
|
#include "headers.h"
|
2011-05-09 20:44:46 +02:00
|
|
|
|
2011-06-02 15:57:23 +02:00
|
|
|
#include <QFont>
|
2011-06-21 19:54:09 +02:00
|
|
|
#include <QColor>
|
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,
|
|
|
|
Receiving
|
|
|
|
};
|
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) {}
|
|
|
|
};
|
|
|
|
|
2011-06-18 11:53:25 +02:00
|
|
|
// Private implementation
|
2011-06-01 15:50:09 +02:00
|
|
|
struct AddressTablePriv
|
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
CWallet *wallet;
|
2011-06-01 15:50:09 +02:00
|
|
|
QList<AddressTableEntry> cachedAddressTable;
|
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
AddressTablePriv(CWallet *wallet):
|
|
|
|
wallet(wallet) {}
|
|
|
|
|
2011-06-01 15:50:09 +02:00
|
|
|
void refreshAddressTable()
|
|
|
|
{
|
|
|
|
cachedAddressTable.clear();
|
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
CRITICAL_BLOCK(wallet->cs_mapKeys)
|
|
|
|
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
|
2011-06-01 15:50:09 +02:00
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
|
2011-06-01 15:50:09 +02:00
|
|
|
{
|
|
|
|
std::string strAddress = item.first;
|
|
|
|
std::string strName = item.second;
|
|
|
|
uint160 hash160;
|
|
|
|
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
|
|
|
|
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
|
|
|
|
QString::fromStdString(strName),
|
|
|
|
QString::fromStdString(strAddress)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-26 19:23:24 +02:00
|
|
|
|
|
|
|
bool isDefaultAddress(const AddressTableEntry *rec)
|
|
|
|
{
|
|
|
|
return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
|
|
|
|
}
|
2011-06-01 15:50:09 +02:00
|
|
|
};
|
|
|
|
|
2011-06-26 19:23:24 +02:00
|
|
|
AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
|
|
|
|
QAbstractTableModel(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");
|
2011-06-26 19:23:24 +02:00
|
|
|
priv = new AddressTablePriv(wallet);
|
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:
|
|
|
|
return rec->label;
|
|
|
|
case Address:
|
|
|
|
return rec->address;
|
2011-06-21 20:34:43 +02:00
|
|
|
case IsDefaultAddress:
|
2011-06-26 19:23:24 +02:00
|
|
|
return priv->isDefaultAddress(rec);
|
2011-06-01 15:50:09 +02:00
|
|
|
}
|
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();
|
|
|
|
}
|
2011-06-26 19:23:24 +02:00
|
|
|
if(priv->isDefaultAddress(rec))
|
2011-06-21 19:54:09 +02:00
|
|
|
{
|
|
|
|
font.setBold(true);
|
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
2011-06-30 19:43:44 +02:00
|
|
|
else if (role == Qt::BackgroundRole)
|
2011-06-21 19:54:09 +02:00
|
|
|
{
|
|
|
|
// Show default address in alternative color
|
2011-06-26 19:23:24 +02:00
|
|
|
if(priv->isDefaultAddress(rec))
|
2011-06-21 19:54:09 +02:00
|
|
|
{
|
2011-06-30 19:43:44 +02:00
|
|
|
return QColor(255,255,128);
|
2011-06-21 19:54:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (role == Qt::ToolTipRole)
|
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
if(priv->isDefaultAddress(rec))
|
2011-06-21 19:54:09 +02:00
|
|
|
{
|
|
|
|
return tr("Default receiving address");
|
2011-06-01 20:08:21 +02:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2011-06-02 17:48:45 +02:00
|
|
|
bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return false;
|
2011-06-03 15:16:11 +02:00
|
|
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
2011-06-02 17:48:45 +02:00
|
|
|
|
|
|
|
if(role == Qt::EditRole)
|
|
|
|
{
|
|
|
|
switch(index.column())
|
|
|
|
{
|
|
|
|
case Label:
|
2011-06-26 19:23:24 +02:00
|
|
|
wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
|
2011-06-03 15:16:11 +02:00
|
|
|
rec->label = value.toString();
|
2011-06-02 17:48:45 +02:00
|
|
|
break;
|
|
|
|
case Address:
|
2011-06-18 11:53:25 +02:00
|
|
|
// Double-check that we're not overwriting receiving address
|
2011-06-03 15:16:11 +02:00
|
|
|
if(rec->type == AddressTableEntry::Sending)
|
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Remove old entry
|
2011-06-26 19:23:24 +02:00
|
|
|
wallet->EraseAddressBookName(rec->address.toStdString());
|
2011-06-18 11:53:25 +02:00
|
|
|
// Add new entry with new address
|
2011-06-26 19:23:24 +02:00
|
|
|
wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
|
2011-06-03 15:16:11 +02:00
|
|
|
|
|
|
|
rec->address = value.toString();
|
|
|
|
}
|
2011-06-02 17:48:45 +02:00
|
|
|
break;
|
2011-06-21 20:34:43 +02:00
|
|
|
case IsDefaultAddress:
|
|
|
|
if(value.toBool())
|
|
|
|
{
|
|
|
|
setDefaultAddress(rec->address);
|
|
|
|
}
|
|
|
|
break;
|
2011-06-02 17:48:45 +02:00
|
|
|
}
|
2011-06-03 15:16:11 +02:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return columns[section];
|
|
|
|
}
|
|
|
|
}
|
2011-05-13 15:58:27 +02:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2011-06-01 20:08:21 +02: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
|
|
|
|
2011-06-01 20:08:21 +02:00
|
|
|
void AddressTableModel::updateList()
|
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Update internal model from Bitcoin core
|
2011-06-01 20:08:21 +02:00
|
|
|
beginResetModel();
|
|
|
|
priv->refreshAddressTable();
|
|
|
|
endResetModel();
|
|
|
|
}
|
2011-06-03 15:16:11 +02:00
|
|
|
|
2011-06-21 20:34:43 +02:00
|
|
|
QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
|
|
|
std::string strLabel = label.toStdString();
|
|
|
|
std::string strAddress = address.toStdString();
|
|
|
|
|
|
|
|
if(type == Send)
|
|
|
|
{
|
2011-06-18 11:53:25 +02:00
|
|
|
// Check for duplicate
|
2011-06-26 19:23:24 +02:00
|
|
|
CRITICAL_BLOCK(wallet->cs_mapAddressBook)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
if(wallet->mapAddressBook.count(strAddress))
|
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
|
|
|
}
|
|
|
|
}
|
2011-06-07 18:59:01 +02:00
|
|
|
}
|
|
|
|
else if(type == Receive)
|
2011-06-03 15:16:11 +02:00
|
|
|
{
|
2011-06-21 20:34:43 +02:00
|
|
|
// Generate a new address to associate with given label, optionally
|
|
|
|
// set as default receiving address.
|
2011-06-26 19:23:24 +02:00
|
|
|
strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
|
2011-06-21 20:34:43 +02:00
|
|
|
if(setAsDefault)
|
|
|
|
{
|
|
|
|
setDefaultAddress(QString::fromStdString(strAddress));
|
|
|
|
}
|
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
|
|
|
}
|
2011-06-18 11:53:25 +02:00
|
|
|
// Add entry and update list
|
2011-06-26 19:23:24 +02:00
|
|
|
wallet->SetAddressBookName(strAddress, strLabel);
|
2011-06-03 15:16:11 +02:00
|
|
|
updateList();
|
2011-06-03 21:03:20 +02:00
|
|
|
return QString::fromStdString(strAddress);
|
2011-06-03 15:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AddressTableModel::removeRows(int row, int count, const QModelIndex & parent)
|
|
|
|
{
|
|
|
|
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-06-26 19:23:24 +02:00
|
|
|
wallet->EraseAddressBookName(rec->address.toStdString());
|
2011-06-03 15:16:11 +02:00
|
|
|
updateList();
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-21 20:34:43 +02:00
|
|
|
|
|
|
|
QString AddressTableModel::getDefaultAddress() const
|
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
return QString::fromStdString(wallet->GetDefaultAddress());
|
2011-06-21 20:34:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
|
|
|
|
{
|
2011-06-26 19:23:24 +02:00
|
|
|
wallet->SetDefaultAddress(defaultAddress.toStdString());
|
2011-06-21 20:34:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressTableModel::update()
|
|
|
|
{
|
|
|
|
emit defaultAddressChanged(getDefaultAddress());
|
|
|
|
}
|