2019-12-30 10:39:22 +01:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-05 18:03:05 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/recentrequeststablemodel.h>
|
2013-12-10 11:57:57 +01:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/bitcoinunits.h>
|
|
|
|
#include <qt/guiutil.h>
|
|
|
|
#include <qt/optionsmodel.h>
|
2019-11-19 12:45:32 +01:00
|
|
|
#include <qt/walletmodel.h>
|
2015-01-30 10:08:46 +01:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <clientversion.h>
|
|
|
|
#include <streams.h>
|
2013-11-05 18:03:05 +01:00
|
|
|
|
2019-11-29 19:15:45 +01:00
|
|
|
#include <utility>
|
2014-08-23 05:09:47 +02:00
|
|
|
|
2017-04-18 22:42:30 +02:00
|
|
|
RecentRequestsTableModel::RecentRequestsTableModel(WalletModel *parent) :
|
2016-11-18 15:47:20 +01:00
|
|
|
QAbstractTableModel(parent), walletModel(parent)
|
2013-11-05 18:03:05 +01:00
|
|
|
{
|
2014-01-14 05:05:43 +01:00
|
|
|
// Load entries from wallet
|
|
|
|
std::vector<std::string> vReceiveRequests;
|
|
|
|
parent->loadReceiveRequests(vReceiveRequests);
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const std::string& request : vReceiveRequests)
|
2014-01-14 05:05:43 +01:00
|
|
|
addNewRequest(request);
|
2013-12-10 11:57:57 +01:00
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
/* These columns must match the indices in the ColumnIndex enumeration */
|
2014-06-07 08:20:22 +02:00
|
|
|
columns << tr("Date") << tr("Label") << tr("Message") << getAmountTitle();
|
|
|
|
|
2018-06-24 17:18:22 +02:00
|
|
|
connect(walletModel->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &RecentRequestsTableModel::updateDisplayUnit);
|
2013-11-05 18:03:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RecentRequestsTableModel::~RecentRequestsTableModel()
|
|
|
|
{
|
|
|
|
/* Intentionally left empty */
|
|
|
|
}
|
|
|
|
|
|
|
|
int RecentRequestsTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
2013-12-10 11:57:57 +01:00
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
return list.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
int RecentRequestsTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
2013-12-10 11:57:57 +01:00
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
return columns.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant RecentRequestsTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(!index.isValid() || index.row() >= list.length())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if(role == Qt::DisplayRole || role == Qt::EditRole)
|
|
|
|
{
|
2017-06-04 22:45:22 +02:00
|
|
|
const RecentRequestEntry *rec = &list[index.row()];
|
2013-11-05 18:03:05 +01:00
|
|
|
switch(index.column())
|
|
|
|
{
|
|
|
|
case Date:
|
|
|
|
return GUIUtil::dateTimeStr(rec->date);
|
|
|
|
case Label:
|
|
|
|
if(rec->recipient.label.isEmpty() && role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return tr("(no label)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return rec->recipient.label;
|
|
|
|
}
|
|
|
|
case Message:
|
|
|
|
if(rec->recipient.message.isEmpty() && role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return tr("(no message)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return rec->recipient.message;
|
|
|
|
}
|
|
|
|
case Amount:
|
2014-01-26 06:37:17 +01:00
|
|
|
if (rec->recipient.amount == 0 && role == Qt::DisplayRole)
|
2016-01-20 14:57:28 +01:00
|
|
|
return tr("(no amount requested)");
|
2014-07-20 04:17:02 +02:00
|
|
|
else if (role == Qt::EditRole)
|
2020-06-18 14:30:15 +02:00
|
|
|
return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount, false, BitcoinUnits::SeparatorStyle::NEVER);
|
2014-01-26 06:37:17 +01:00
|
|
|
else
|
|
|
|
return BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), rec->recipient.amount);
|
2013-11-05 18:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-20 04:17:02 +02:00
|
|
|
else if (role == Qt::TextAlignmentRole)
|
|
|
|
{
|
|
|
|
if (index.column() == Amount)
|
|
|
|
return (int)(Qt::AlignRight|Qt::AlignVCenter);
|
|
|
|
}
|
2013-11-05 18:03:05 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecentRequestsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant RecentRequestsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(orientation == Qt::Horizontal)
|
|
|
|
{
|
|
|
|
if(role == Qt::DisplayRole && section < columns.size())
|
|
|
|
{
|
|
|
|
return columns[section];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2014-06-07 08:20:22 +02:00
|
|
|
/** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
|
|
|
|
void RecentRequestsTableModel::updateAmountColumnTitle()
|
|
|
|
{
|
|
|
|
columns[Amount] = getAmountTitle();
|
2015-07-14 13:59:05 +02:00
|
|
|
Q_EMIT headerDataChanged(Qt::Horizontal,Amount,Amount);
|
2014-06-07 08:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets title for amount column including current display unit if optionsModel reference available. */
|
|
|
|
QString RecentRequestsTableModel::getAmountTitle()
|
|
|
|
{
|
2017-12-27 17:01:45 +01:00
|
|
|
return (this->walletModel->getOptionsModel() != nullptr) ? tr("Requested") + " ("+BitcoinUnits::shortName(this->walletModel->getOptionsModel()->getDisplayUnit()) + ")" : "";
|
2014-06-07 08:20:22 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
QModelIndex RecentRequestsTableModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
2013-12-10 11:57:57 +01:00
|
|
|
Q_UNUSED(parent);
|
|
|
|
|
|
|
|
return createIndex(row, column);
|
2013-11-05 18:03:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RecentRequestsTableModel::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
2013-12-10 11:57:57 +01:00
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
if(count > 0 && row >= 0 && (row+count) <= list.size())
|
|
|
|
{
|
2014-01-14 05:05:43 +01:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2018-03-25 22:49:33 +02:00
|
|
|
const RecentRequestEntry* rec = &list[row+i];
|
2014-01-14 05:05:43 +01:00
|
|
|
if (!walletModel->saveReceiveRequest(rec->recipient.address.toStdString(), rec->id, ""))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:03:05 +01:00
|
|
|
beginRemoveRows(parent, row, row + count - 1);
|
|
|
|
list.erase(list.begin() + row, list.begin() + row + count);
|
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags RecentRequestsTableModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
}
|
|
|
|
|
2014-01-14 05:05:43 +01:00
|
|
|
// called when adding a request from the GUI
|
2013-11-05 18:03:05 +01:00
|
|
|
void RecentRequestsTableModel::addNewRequest(const SendCoinsRecipient &recipient)
|
|
|
|
{
|
|
|
|
RecentRequestEntry newEntry;
|
2014-01-14 05:05:43 +01:00
|
|
|
newEntry.id = ++nReceiveRequestsMaxId;
|
2013-11-05 18:03:05 +01:00
|
|
|
newEntry.date = QDateTime::currentDateTime();
|
|
|
|
newEntry.recipient = recipient;
|
2014-01-14 05:05:43 +01:00
|
|
|
|
|
|
|
CDataStream ss(SER_DISK, CLIENT_VERSION);
|
|
|
|
ss << newEntry;
|
|
|
|
|
|
|
|
if (!walletModel->saveReceiveRequest(recipient.address.toStdString(), newEntry.id, ss.str()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
addNewRequest(newEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from ctor when loading from wallet
|
|
|
|
void RecentRequestsTableModel::addNewRequest(const std::string &recipient)
|
|
|
|
{
|
|
|
|
std::vector<char> data(recipient.begin(), recipient.end());
|
|
|
|
CDataStream ss(data, SER_DISK, CLIENT_VERSION);
|
|
|
|
|
|
|
|
RecentRequestEntry entry;
|
|
|
|
ss >> entry;
|
|
|
|
|
|
|
|
if (entry.id == 0) // should not happen
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (entry.id > nReceiveRequestsMaxId)
|
|
|
|
nReceiveRequestsMaxId = entry.id;
|
|
|
|
|
|
|
|
addNewRequest(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// actually add to table in GUI
|
|
|
|
void RecentRequestsTableModel::addNewRequest(RecentRequestEntry &recipient)
|
|
|
|
{
|
2013-11-05 18:03:05 +01:00
|
|
|
beginInsertRows(QModelIndex(), 0, 0);
|
2014-01-14 05:05:43 +01:00
|
|
|
list.prepend(recipient);
|
2013-11-05 18:03:05 +01:00
|
|
|
endInsertRows();
|
|
|
|
}
|
2014-01-18 00:01:14 +01:00
|
|
|
|
|
|
|
void RecentRequestsTableModel::sort(int column, Qt::SortOrder order)
|
|
|
|
{
|
2019-08-21 03:44:19 +02:00
|
|
|
std::sort(list.begin(), list.end(), RecentRequestEntryLessThan(column, order));
|
2015-07-14 13:59:05 +02:00
|
|
|
Q_EMIT dataChanged(index(0, 0, QModelIndex()), index(list.size() - 1, NUMBER_OF_COLUMNS - 1, QModelIndex()));
|
2014-01-18 00:01:14 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 08:20:22 +02:00
|
|
|
void RecentRequestsTableModel::updateDisplayUnit()
|
|
|
|
{
|
|
|
|
updateAmountColumnTitle();
|
|
|
|
}
|
|
|
|
|
2019-11-29 19:15:45 +01:00
|
|
|
bool RecentRequestEntryLessThan::operator()(const RecentRequestEntry& left, const RecentRequestEntry& right) const
|
2014-01-18 00:01:14 +01:00
|
|
|
{
|
2019-11-29 19:15:45 +01:00
|
|
|
const RecentRequestEntry* pLeft = &left;
|
|
|
|
const RecentRequestEntry* pRight = &right;
|
2014-01-18 00:01:14 +01:00
|
|
|
if (order == Qt::DescendingOrder)
|
|
|
|
std::swap(pLeft, pRight);
|
|
|
|
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case RecentRequestsTableModel::Date:
|
|
|
|
return pLeft->date.toTime_t() < pRight->date.toTime_t();
|
|
|
|
case RecentRequestsTableModel::Label:
|
|
|
|
return pLeft->recipient.label < pRight->recipient.label;
|
|
|
|
case RecentRequestsTableModel::Message:
|
|
|
|
return pLeft->recipient.message < pRight->recipient.message;
|
|
|
|
case RecentRequestsTableModel::Amount:
|
|
|
|
return pLeft->recipient.amount < pRight->recipient.amount;
|
|
|
|
default:
|
|
|
|
return pLeft->id < pRight->id;
|
|
|
|
}
|
|
|
|
}
|