dogecoin/gui/src/transactiontablemodel.cpp

556 lines
16 KiB
C++
Raw Normal View History

2011-05-12 14:49:42 +02:00
#include "transactiontablemodel.h"
2011-05-27 18:38:30 +02:00
#include "guiutil.h"
#include "main.h"
2011-05-08 16:30:10 +02:00
2011-05-10 19:03:10 +02:00
#include <QLocale>
2011-05-27 08:20:23 +02:00
#include <QDebug>
#include <QList>
2011-05-10 19:03:10 +02:00
const QString TransactionTableModel::Sent = "s";
const QString TransactionTableModel::Received = "r";
const QString TransactionTableModel::Generated = "g";
2011-05-27 18:38:30 +02:00
const QString TransactionTableModel::Other = "o";
2011-05-27 18:38:30 +02:00
/* TODO: look up address in address book
when showing.
Color based on confirmation status.
(fConfirmed ? wxColour(0,0,0) : wxColour(128,128,128))
2011-05-27 08:20:23 +02:00
*/
2011-05-27 18:38:30 +02:00
class TransactionStatus
{
public:
TransactionStatus():
confirmed(false), sortKey(""), maturity(Mature),
matures_in(0), status(Offline), depth(0), open_for(0)
{ }
enum Maturity
{
Immature,
Mature,
MaturesIn,
MaturesWarning, /* Will probably not mature because no nodes have confirmed */
NotAccepted
};
enum Status {
OpenUntilDate,
OpenUntilBlock,
Offline,
Unconfirmed,
HaveConfirmations
};
bool confirmed;
std::string sortKey;
/* For "Generated" transactions */
Maturity maturity;
int matures_in;
/* Reported status */
Status status;
int64 depth;
int64 open_for; /* Timestamp if status==OpenUntilDate, otherwise number of blocks */
};
2011-05-27 08:20:23 +02:00
class TransactionRecord
{
public:
2011-05-27 18:38:30 +02:00
enum Type
{
Other,
Generated,
SendToAddress,
SendToIP,
RecvFromAddress,
RecvFromIP,
SendToSelf
};
TransactionRecord():
hash(), time(0), type(Other), address(""), debit(0), credit(0)
{
}
TransactionRecord(uint256 hash, int64 time, const TransactionStatus &status):
hash(hash), time(time), type(Other), address(""), debit(0),
credit(0), status(status)
{
}
TransactionRecord(uint256 hash, int64 time, const TransactionStatus &status,
Type type, const std::string &address,
int64 debit, int64 credit):
hash(hash), time(time), type(type), address(address), debit(debit), credit(credit),
status(status)
{
}
/* Fixed */
2011-05-27 08:20:23 +02:00
uint256 hash;
int64 time;
2011-05-27 18:38:30 +02:00
Type type;
std::string address;
2011-05-27 08:20:23 +02:00
int64 debit;
2011-05-27 18:38:30 +02:00
int64 credit;
2011-05-27 08:20:23 +02:00
2011-05-27 18:38:30 +02:00
/* Status: can change with block chain update */
TransactionStatus status;
};
/* Return positive answer if transaction should be shown in list.
*/
bool showTransaction(const CWalletTx &wtx)
{
if (wtx.IsCoinBase())
2011-05-27 08:20:23 +02:00
{
2011-05-27 18:38:30 +02:00
// Don't show generated coin until confirmed by at least one block after it
// so we don't get the user's hopes up until it looks like it's probably accepted.
//
// It is not an error when generated blocks are not accepted. By design,
// some percentage of blocks, like 10% or more, will end up not accepted.
// This is the normal mechanism by which the network copes with latency.
//
// We display regular transactions right away before any confirmation
// because they can always get into some block eventually. Generated coins
// are special because if their block is not accepted, they are not valid.
//
if (wtx.GetDepthInMainChain() < 2)
{
return false;
}
}
return true;
}
/* Decompose CWallet transaction to model transaction records.
*/
QList<TransactionRecord> decomposeTransaction(const CWalletTx &wtx)
{
QList<TransactionRecord> parts;
int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
int64 nCredit = wtx.GetCredit(true);
int64 nDebit = wtx.GetDebit();
int64 nNet = nCredit - nDebit;
uint256 hash = wtx.GetHash();
std::map<std::string, std::string> mapValue = wtx.mapValue;
2011-05-27 08:20:23 +02:00
2011-05-27 18:38:30 +02:00
// Find the block the tx is in
CBlockIndex* pindex = NULL;
std::map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
if (mi != mapBlockIndex.end())
pindex = (*mi).second;
// Determine transaction status
TransactionStatus status;
// Sort order, unrecorded transactions sort to the top
status.sortKey = strprintf("%010d-%01d-%010u",
(pindex ? pindex->nHeight : INT_MAX),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived);
status.confirmed = wtx.IsConfirmed();
status.depth = wtx.GetDepthInMainChain();
if (!wtx.IsFinal())
{
if (wtx.nLockTime < 500000000)
{
status.status = TransactionStatus::OpenUntilBlock;
status.open_for = nBestHeight - wtx.nLockTime;
} else {
status.status = TransactionStatus::OpenUntilDate;
status.open_for = wtx.nLockTime;
}
}
else
{
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
{
status.status = TransactionStatus::Offline;
} else if (status.depth < 6)
{
status.status = TransactionStatus::Unconfirmed;
} else
{
status.status = TransactionStatus::HaveConfirmations;
}
2011-05-27 08:20:23 +02:00
}
2011-05-27 18:38:30 +02:00
if (showTransaction(wtx))
2011-05-27 08:20:23 +02:00
{
2011-05-27 18:38:30 +02:00
if (nNet > 0 || wtx.IsCoinBase())
{
//
// Credit
//
TransactionRecord sub(hash, nTime, status);
sub.credit = nNet;
if (wtx.IsCoinBase())
{
// Generated
sub.type = TransactionRecord::Generated;
if (nCredit == 0)
{
sub.status.maturity = TransactionStatus::Immature;
int64 nUnmatured = 0;
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
nUnmatured += txout.GetCredit();
sub.credit = nUnmatured;
if (wtx.IsInMainChain())
{
sub.status.maturity = TransactionStatus::MaturesIn;
sub.status.matures_in = wtx.GetBlocksToMaturity();
// Check if the block was requested by anyone
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
sub.status.maturity = TransactionStatus::MaturesWarning;
}
else
{
sub.status.maturity = TransactionStatus::NotAccepted;
}
}
}
else if (!mapValue["from"].empty() || !mapValue["message"].empty())
{
// Received by IP connection
sub.type = TransactionRecord::RecvFromIP;
if (!mapValue["from"].empty())
sub.address = mapValue["from"];
}
else
{
// Received by Bitcoin Address
sub.type = TransactionRecord::RecvFromAddress;
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
{
if (txout.IsMine())
{
std::vector<unsigned char> vchPubKey;
if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
{
sub.address = PubKeyToAddress(vchPubKey);
}
break;
}
}
}
parts.append(sub);
}
else
{
bool fAllFromMe = true;
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
fAllFromMe = fAllFromMe && txin.IsMine();
bool fAllToMe = true;
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
fAllToMe = fAllToMe && txout.IsMine();
if (fAllFromMe && fAllToMe)
{
// Payment to self
int64 nChange = wtx.GetChange();
parts.append(TransactionRecord(hash, nTime, status, TransactionRecord::SendToSelf, "",
-(nDebit - nChange), nCredit - nChange));
}
else if (fAllFromMe)
{
//
// Debit
//
int64 nTxFee = nDebit - wtx.GetValueOut();
for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
{
const CTxOut& txout = wtx.vout[nOut];
TransactionRecord sub(hash, nTime, status);
if (txout.IsMine())
{
// Sent to self
sub.type = TransactionRecord::SendToSelf;
sub.credit = txout.nValue;
} else if (!mapValue["to"].empty())
{
// Sent to IP
sub.type = TransactionRecord::SendToIP;
sub.address = mapValue["to"];
} else {
// Sent to Bitcoin Address
sub.type = TransactionRecord::SendToAddress;
uint160 hash160;
if (ExtractHash160(txout.scriptPubKey, hash160))
sub.address = Hash160ToAddress(hash160);
}
int64 nValue = txout.nValue;
/* Add fee to first output */
if (nTxFee > 0)
{
nValue += nTxFee;
nTxFee = 0;
}
sub.debit = nValue;
sub.status.sortKey += strprintf("-%d", nOut);
parts.append(sub);
}
} else {
//
// Mixed debit transaction, can't break down payees
//
bool fAllMine = true;
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
fAllMine = fAllMine && txout.IsMine();
BOOST_FOREACH(const CTxIn& txin, wtx.vin)
fAllMine = fAllMine && txin.IsMine();
parts.append(TransactionRecord(hash, nTime, status, TransactionRecord::Other, "", nNet, 0));
}
}
2011-05-27 08:20:23 +02:00
}
2011-05-27 18:38:30 +02:00
return parts;
}
2011-05-27 08:20:23 +02:00
/* Internal implementation */
class TransactionTableImpl
{
public:
QList<TransactionRecord> cachedWallet;
/* Update our model of the wallet */
void updateWallet()
{
QList<int> insertedIndices;
QList<int> removedIndices;
cachedWallet.clear();
/* Query wallet from core, and compare with our own
representation.
*/
CRITICAL_BLOCK(cs_mapWallet)
{
for(std::map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
/* TODO: Make note of new and removed transactions */
/* insertedIndices */
/* removedIndices */
2011-05-27 18:38:30 +02:00
cachedWallet.append(decomposeTransaction(it->second));
2011-05-27 08:20:23 +02:00
}
}
/* beginInsertRows(QModelIndex(), first, last) */
/* endInsertRows */
/* beginRemoveRows(QModelIndex(), first, last) */
/* beginEndRows */
}
int size()
{
return cachedWallet.size();
}
TransactionRecord *index(int idx)
{
if(idx >= 0 && idx < cachedWallet.size())
{
return &cachedWallet[idx];
} else {
return 0;
}
}
};
2011-05-08 22:23:31 +02:00
/* Credit and Debit columns are right-aligned as they contain numbers */
2011-05-10 19:03:10 +02:00
static int column_alignments[] = {
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter
2011-05-08 22:23:31 +02:00
};
2011-05-08 16:30:10 +02:00
TransactionTableModel::TransactionTableModel(QObject *parent):
2011-05-27 08:20:23 +02:00
QAbstractTableModel(parent),
impl(new TransactionTableImpl())
2011-05-08 16:30:10 +02:00
{
columns << tr("Status") << tr("Date") << tr("Description") << tr("Debit") << tr("Credit");
2011-05-27 08:20:23 +02:00
impl->updateWallet();
}
TransactionTableModel::~TransactionTableModel()
{
delete impl;
2011-05-08 16:30:10 +02:00
}
2011-05-27 08:20:23 +02:00
2011-05-08 16:30:10 +02:00
int TransactionTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
2011-05-27 08:20:23 +02:00
return impl->size();
2011-05-08 16:30:10 +02:00
}
int TransactionTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return columns.length();
}
2011-05-27 08:20:23 +02:00
QVariant TransactionTableModel::formatTxStatus(const TransactionRecord *wtx) const
{
2011-05-27 18:38:30 +02:00
QString status;
switch(wtx->status.status)
2011-05-27 08:20:23 +02:00
{
2011-05-27 18:38:30 +02:00
case TransactionStatus::OpenUntilBlock:
status = tr("Open for %n block(s)","",wtx->status.open_for);
break;
case TransactionStatus::OpenUntilDate:
status = tr("Open until ") + DateTimeStr(wtx->status.open_for);
break;
case TransactionStatus::Offline:
status = tr("%1/offline").arg(wtx->status.depth);
break;
case TransactionStatus::Unconfirmed:
status = tr("%1/unconfirmed").arg(wtx->status.depth);
break;
case TransactionStatus::HaveConfirmations:
status = tr("%1 confirmations").arg(wtx->status.depth);
break;
2011-05-27 08:20:23 +02:00
}
2011-05-27 18:38:30 +02:00
return QVariant(status);
2011-05-27 08:20:23 +02:00
}
QVariant TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const
{
2011-05-27 18:38:30 +02:00
if(wtx->time)
{
return QVariant(DateTimeStr(wtx->time));
} else {
return QVariant();
}
2011-05-27 08:20:23 +02:00
}
QVariant TransactionTableModel::formatTxDescription(const TransactionRecord *wtx) const
{
return QVariant();
}
QVariant TransactionTableModel::formatTxDebit(const TransactionRecord *wtx) const
{
2011-05-27 18:38:30 +02:00
if(wtx->debit)
{
QString str = QString::fromStdString(FormatMoney(wtx->debit));
if(!wtx->status.confirmed)
{
str = QString("[") + str + QString("]");
}
return QVariant(str);
} else {
return QVariant();
}
2011-05-27 08:20:23 +02:00
}
QVariant TransactionTableModel::formatTxCredit(const TransactionRecord *wtx) const
{
2011-05-27 18:38:30 +02:00
if(wtx->credit)
{
QString str = QString::fromStdString(FormatMoney(wtx->credit));
if(!wtx->status.confirmed)
{
str = QString("[") + str + QString("]");
}
return QVariant(str);
} else {
return QVariant();
}
2011-05-27 08:20:23 +02:00
}
2011-05-08 16:30:10 +02:00
QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
2011-05-27 08:20:23 +02:00
TransactionRecord *rec = static_cast<TransactionRecord*>(index.internalPointer());
2011-05-08 16:30:10 +02:00
if(role == Qt::DisplayRole)
{
2011-05-27 08:20:23 +02:00
switch(index.column())
{
case Status:
return formatTxStatus(rec);
case Date:
return formatTxDate(rec);
case Description:
return formatTxDescription(rec);
case Debit:
return formatTxDebit(rec);
case Credit:
return formatTxCredit(rec);
}
2011-05-08 22:23:31 +02:00
} else if (role == Qt::TextAlignmentRole)
{
return column_alignments[index.column()];
} else if (role == TypeRole)
2011-05-10 19:03:10 +02:00
{
/* user role: transaction type for filtering
"s" (sent)
"r" (received)
"g" (generated)
*/
switch(index.row() % 3)
{
case 0: return QString("s");
case 1: return QString("r");
case 2: return QString("o");
}
2011-05-08 16:30:10 +02:00
}
return QVariant();
}
QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2011-05-08 22:23:31 +02:00
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
return columns[section];
}
} else if (role == Qt::TextAlignmentRole)
2011-05-08 16:30:10 +02:00
{
2011-05-08 22:23:31 +02:00
return column_alignments[section];
2011-05-08 16:30:10 +02:00
}
return QVariant();
}
Qt::ItemFlags TransactionTableModel::flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index);
}
2011-05-27 08:20:23 +02:00
QModelIndex TransactionTableModel::index ( int row, int column, const QModelIndex & parent ) const
{
Q_UNUSED(parent);
TransactionRecord *data = impl->index(row);
if(data)
{
return createIndex(row, column, impl->index(row));
} else {
return QModelIndex();
}
}