twisterp2pbittorrentblockchainnetworkipv6microbloggingdecentralizedsocial-networkdhttwister-coretwisterarmyp2p-networktwister-servertwister-ipv6
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
6.1 KiB
246 lines
6.1 KiB
14 years ago
|
#include "addresstablemodel.h"
|
||
14 years ago
|
#include "guiutil.h"
|
||
14 years ago
|
#include "main.h"
|
||
14 years ago
|
|
||
14 years ago
|
#include <QFont>
|
||
|
|
||
14 years ago
|
const QString AddressTableModel::Send = "S";
|
||
|
const QString AddressTableModel::Receive = "R";
|
||
|
|
||
14 years ago
|
struct AddressTableEntry
|
||
|
{
|
||
|
enum Type {
|
||
|
Sending,
|
||
|
Receiving
|
||
|
};
|
||
14 years ago
|
|
||
14 years ago
|
Type type;
|
||
|
QString label;
|
||
|
QString address;
|
||
|
|
||
|
AddressTableEntry() {}
|
||
|
AddressTableEntry(Type type, const QString &label, const QString &address):
|
||
|
type(type), label(label), address(address) {}
|
||
|
};
|
||
|
|
||
|
/* Private implementation */
|
||
|
struct AddressTablePriv
|
||
|
{
|
||
|
QList<AddressTableEntry> cachedAddressTable;
|
||
|
|
||
|
void refreshAddressTable()
|
||
|
{
|
||
|
cachedAddressTable.clear();
|
||
|
|
||
|
CRITICAL_BLOCK(cs_mapKeys)
|
||
|
CRITICAL_BLOCK(cs_mapAddressBook)
|
||
|
{
|
||
|
BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
|
||
|
{
|
||
|
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];
|
||
14 years ago
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
14 years ago
|
AddressTableModel::AddressTableModel(QObject *parent) :
|
||
14 years ago
|
QAbstractTableModel(parent),priv(0)
|
||
14 years ago
|
{
|
||
14 years ago
|
columns << tr("Label") << tr("Address");
|
||
|
priv = new AddressTablePriv();
|
||
|
priv->refreshAddressTable();
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
AddressTableModel::~AddressTableModel()
|
||
|
{
|
||
|
delete priv;
|
||
14 years ago
|
}
|
||
|
|
||
|
int AddressTableModel::rowCount(const QModelIndex &parent) const
|
||
|
{
|
||
14 years ago
|
Q_UNUSED(parent);
|
||
14 years ago
|
return priv->size();
|
||
14 years ago
|
}
|
||
|
|
||
|
int AddressTableModel::columnCount(const QModelIndex &parent) const
|
||
|
{
|
||
14 years ago
|
Q_UNUSED(parent);
|
||
|
return columns.length();
|
||
14 years ago
|
}
|
||
|
|
||
|
QVariant AddressTableModel::data(const QModelIndex &index, int role) const
|
||
|
{
|
||
|
if(!index.isValid())
|
||
|
return QVariant();
|
||
|
|
||
14 years ago
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
||
|
|
||
14 years ago
|
if(role == Qt::DisplayRole || role == Qt::EditRole)
|
||
14 years ago
|
{
|
||
14 years ago
|
switch(index.column())
|
||
|
{
|
||
|
case Label:
|
||
|
return rec->label;
|
||
|
case Address:
|
||
|
return rec->address;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
else if (role == Qt::FontRole)
|
||
14 years ago
|
{
|
||
|
if(index.column() == Address)
|
||
|
{
|
||
14 years ago
|
return GUIUtil::bitcoinAddressFont();
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
else if (role == TypeRole)
|
||
14 years ago
|
{
|
||
14 years ago
|
switch(rec->type)
|
||
14 years ago
|
{
|
||
14 years ago
|
case AddressTableEntry::Sending:
|
||
|
return Send;
|
||
|
case AddressTableEntry::Receiving:
|
||
|
return Receive;
|
||
|
default: break;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
return QVariant();
|
||
|
}
|
||
|
|
||
14 years ago
|
bool AddressTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
|
||
|
{
|
||
|
if(!index.isValid())
|
||
|
return false;
|
||
14 years ago
|
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
|
||
14 years ago
|
|
||
|
if(role == Qt::EditRole)
|
||
|
{
|
||
|
switch(index.column())
|
||
|
{
|
||
|
case Label:
|
||
14 years ago
|
SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
|
||
|
rec->label = value.toString();
|
||
14 years ago
|
break;
|
||
|
case Address:
|
||
|
/* Double-check that we're not overwriting receiving address */
|
||
14 years ago
|
if(rec->type == AddressTableEntry::Sending)
|
||
|
{
|
||
|
/* Remove old entry */
|
||
|
CWalletDB().EraseName(rec->address.toStdString());
|
||
|
/* Add new entry with new address */
|
||
|
SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
|
||
|
|
||
|
rec->address = value.toString();
|
||
|
}
|
||
14 years ago
|
break;
|
||
|
}
|
||
14 years ago
|
emit dataChanged(index, index);
|
||
|
|
||
14 years ago
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||
|
{
|
||
14 years ago
|
if(orientation == Qt::Horizontal)
|
||
|
{
|
||
|
if(role == Qt::DisplayRole)
|
||
|
{
|
||
|
return columns[section];
|
||
|
}
|
||
|
}
|
||
14 years ago
|
return QVariant();
|
||
|
}
|
||
|
|
||
14 years ago
|
QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
|
||
14 years ago
|
{
|
||
14 years ago
|
Q_UNUSED(parent);
|
||
|
AddressTableEntry *data = priv->index(row);
|
||
|
if(data)
|
||
|
{
|
||
|
return createIndex(row, column, priv->index(row));
|
||
14 years ago
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
return QModelIndex();
|
||
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
void AddressTableModel::updateList()
|
||
|
{
|
||
|
/* Update internal model from Bitcoin core */
|
||
|
beginResetModel();
|
||
|
priv->refreshAddressTable();
|
||
|
endResetModel();
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
|
||
14 years ago
|
{
|
||
|
std::string strLabel = label.toStdString();
|
||
|
std::string strAddress = address.toStdString();
|
||
|
|
||
|
if(type == Send)
|
||
|
{
|
||
|
/* Check for duplicate */
|
||
|
CRITICAL_BLOCK(cs_mapAddressBook)
|
||
|
{
|
||
|
if(mapAddressBook.count(strAddress))
|
||
|
{
|
||
14 years ago
|
return QString();
|
||
14 years ago
|
}
|
||
|
}
|
||
14 years ago
|
}
|
||
|
else if(type == Receive)
|
||
14 years ago
|
{
|
||
|
/* Generate a new address to associate with given label */
|
||
|
strAddress = PubKeyToAddress(GetKeyFromKeyPool());
|
||
14 years ago
|
}
|
||
|
else
|
||
14 years ago
|
{
|
||
14 years ago
|
return QString();
|
||
14 years ago
|
}
|
||
|
/* Add entry and update list */
|
||
|
SetAddressBookName(strAddress, strLabel);
|
||
|
updateList();
|
||
14 years ago
|
return QString::fromStdString(strAddress);
|
||
14 years ago
|
}
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
/* Can only remove one row at a time, and cannot remove rows not in model.
|
||
|
Also refuse to remove receiving addresses.
|
||
|
*/
|
||
|
return false;
|
||
|
}
|
||
|
CWalletDB().EraseName(rec->address.toStdString());
|
||
|
updateList();
|
||
|
return true;
|
||
|
}
|