Files
pay/WalletHistoryModel.cpp
T

148 lines
4.7 KiB
C++
Raw Permalink Normal View History

2020-05-24 13:20:03 +02:00
/*
* This file is part of the Flowee project
2021-01-05 14:03:30 +01:00
* Copyright (C) 2020 Tom Zander <tom@flowee.org>
2020-05-24 13:20:03 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WalletHistoryModel.h"
#include "Wallet.h"
#include "FloweePay.h"
#include <BlockHeader.h>
#include <QLocale>
#include <QDateTime>
WalletHistoryModel::WalletHistoryModel(Wallet *wallet, QObject *parent)
: QAbstractListModel(parent),
m_wallet(wallet)
{
QMutexLocker locker(&m_wallet->m_lock);
createMap();
connect(wallet, SIGNAL(appendedTransactions(int,int)), SLOT(appendTransactions(int,int)), Qt::QueuedConnection);
connect(wallet, SIGNAL(transactionConfirmed(int)), SLOT(transactionConfirmed(int)), Qt::QueuedConnection);
2020-05-24 13:20:03 +02:00
}
int WalletHistoryModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) // only for the (invalid) root node we return a count, since this is a list not a tree
return 0;
return m_rowsProxy.size();
}
QVariant WalletHistoryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
QMutexLocker locker(&m_wallet->m_lock);
assert(m_rowsProxy.size() > index.row());
auto item = m_wallet->m_walletTransactions.at(m_rowsProxy.at(index.row()));
switch (role) {
case TxId:
return QVariant(QString::fromStdString(item.txid.ToString()));
case MinedHeight:
return QVariant(item.minedBlockHeight);
case MinedDate: {
2020-11-04 18:59:24 +01:00
if (item.minedBlockHeight <= 0)
return QVariant();
2020-05-24 13:20:03 +02:00
auto header = FloweePay::instance()->p2pNet()->blockchain().block(item.minedBlockHeight);
auto format = QLocale::system().dateFormat(QLocale::ShortFormat);
return QVariant(QDateTime::fromTime_t(header.nTime).toString(format));
}
case FundsIn: {
qint64 value = 0;
for (auto input : item.inputToWTX) {
auto key = input.second;
int outputIndex = key & 0xFFFF;
assert(outputIndex >= 0);
key >>= 16;
assert(m_wallet->m_walletTransactions.find(key) != m_wallet->m_walletTransactions.end());
const auto &spendingTx = m_wallet->m_walletTransactions.at(key);
assert(spendingTx.outputs.find(outputIndex) != spendingTx.outputs.end());
auto prevOut = spendingTx.outputs.at(outputIndex);
value += prevOut.value;
}
2020-06-12 20:53:01 +02:00
return QVariant(double(value));
2020-05-24 13:20:03 +02:00
}
case FundsOut: {
qint64 value = 0;
for (auto out : item.outputs) {
value += out.second.value;
}
2020-06-12 20:53:01 +02:00
return QVariant(double(value));
2020-05-24 13:20:03 +02:00
}
2020-12-17 23:12:39 +01:00
case WalletIndex:
return QVariant(m_rowsProxy.at(index.row()));
2020-05-24 13:20:03 +02:00
}
return QVariant();
}
QHash<int, QByteArray> WalletHistoryModel::roleNames() const
{
QHash<int, QByteArray> answer;
answer[TxId] = "txid";
answer[MinedHeight] = "height";
answer[MinedDate] = "date";
answer[FundsIn] = "fundsIn";
answer[FundsOut] = "fundsOut";
2020-12-17 23:12:39 +01:00
answer[WalletIndex] = "walletIndex";
2020-05-24 13:20:03 +02:00
// answer[Comment] = "comment";
// answer[SavedFiatRate] = "savedFiatRate";
return answer;
}
void WalletHistoryModel::appendTransactions(int firstNew, int count)
{
// lets assume sorting newest to oldest here.
beginInsertRows(QModelIndex(), 0, count - 1);
for (auto i = firstNew; i < firstNew + count; ++i) {
m_rowsProxy.insert(0, i);
}
endInsertRows();
}
void WalletHistoryModel::transactionConfirmed(int txIndex)
{
const int row = m_rowsProxy.indexOf(txIndex);
// update row, the 'minedHeight' went from unset to an actual value
beginRemoveRows(QModelIndex(), row, row);
endRemoveRows();
beginInsertRows(QModelIndex(), row, row);
endInsertRows();
}
2020-05-24 13:20:03 +02:00
void WalletHistoryModel::createMap()
{
m_rowsProxy.clear();
m_rowsProxy.resize(m_wallet->m_walletTransactions.size());
// we insert the key used in the m_wallet->m_walletTransaction map
// in the order of how our rows work here.
// the simplest form; reverse order. This assumes the last entry is the newest one
int i = m_rowsProxy.size() - 1;
2020-12-25 23:27:14 +01:00
for (const auto &iter : m_wallet->m_walletTransactions) {
2020-05-24 13:20:03 +02:00
assert(i >= 0);
m_rowsProxy[i--] = iter.first;
}
}