2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2011-2017 The Bitcoin Core developers
|
2015-06-20 20:27:03 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/bantablemodel.h>
|
2015-06-20 20:27:03 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <qt/clientmodel.h>
|
|
|
|
#include <qt/guiconstants.h>
|
|
|
|
#include <qt/guiutil.h>
|
2015-06-20 20:27:03 +02:00
|
|
|
|
2017-04-17 22:02:44 +02:00
|
|
|
#include <interface/node.h>
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <sync.h>
|
|
|
|
#include <utiltime.h>
|
2015-06-20 20:27:03 +02:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QList>
|
|
|
|
|
2015-06-26 14:58:15 +02:00
|
|
|
bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
|
|
|
|
{
|
|
|
|
const CCombinedBan* pLeft = &left;
|
|
|
|
const CCombinedBan* pRight = &right;
|
|
|
|
|
|
|
|
if (order == Qt::DescendingOrder)
|
|
|
|
std::swap(pLeft, pRight);
|
|
|
|
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case BanTableModel::Address:
|
|
|
|
return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
|
|
|
|
case BanTableModel::Bantime:
|
2015-07-03 08:35:14 +02:00
|
|
|
return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
|
2015-06-26 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-20 20:27:03 +02:00
|
|
|
|
|
|
|
// private implementation
|
|
|
|
class BanTablePriv
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Local cache of peer information */
|
|
|
|
QList<CCombinedBan> cachedBanlist;
|
|
|
|
/** Column to sort nodes by */
|
|
|
|
int sortColumn;
|
|
|
|
/** Order (ascending or descending) to sort nodes by */
|
|
|
|
Qt::SortOrder sortOrder;
|
|
|
|
|
|
|
|
/** Pull a full list of banned nodes from CNode into our cache */
|
2017-04-17 22:02:44 +02:00
|
|
|
void refreshBanlist(interface::Node& node)
|
2015-06-20 20:27:03 +02:00
|
|
|
{
|
2015-07-03 08:35:14 +02:00
|
|
|
banmap_t banMap;
|
2017-04-17 22:02:44 +02:00
|
|
|
node.getBanned(banMap);
|
2015-06-20 20:27:03 +02:00
|
|
|
|
|
|
|
cachedBanlist.clear();
|
|
|
|
#if QT_VERSION >= 0x040700
|
|
|
|
cachedBanlist.reserve(banMap.size());
|
|
|
|
#endif
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : banMap)
|
2015-06-21 10:44:48 +02:00
|
|
|
{
|
2015-06-20 20:27:03 +02:00
|
|
|
CCombinedBan banEntry;
|
2017-06-04 22:02:43 +02:00
|
|
|
banEntry.subnet = entry.first;
|
|
|
|
banEntry.banEntry = entry.second;
|
2015-06-20 20:27:03 +02:00
|
|
|
cachedBanlist.append(banEntry);
|
|
|
|
}
|
2015-06-26 14:58:15 +02:00
|
|
|
|
|
|
|
if (sortColumn >= 0)
|
2017-01-18 16:15:37 +01:00
|
|
|
// sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
|
2015-06-26 14:58:15 +02:00
|
|
|
qStableSort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
|
2015-06-21 10:44:48 +02:00
|
|
|
int size() const
|
2015-06-20 20:27:03 +02:00
|
|
|
{
|
|
|
|
return cachedBanlist.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCombinedBan *index(int idx)
|
|
|
|
{
|
2015-06-23 21:10:42 +02:00
|
|
|
if (idx >= 0 && idx < cachedBanlist.size())
|
2015-06-20 20:27:03 +02:00
|
|
|
return &cachedBanlist[idx];
|
2015-06-23 21:10:42 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-17 22:02:44 +02:00
|
|
|
BanTableModel::BanTableModel(interface::Node& node, ClientModel *parent) :
|
2015-06-20 20:27:03 +02:00
|
|
|
QAbstractTableModel(parent),
|
2017-04-17 22:02:44 +02:00
|
|
|
m_node(node),
|
2015-06-26 10:21:13 +02:00
|
|
|
clientModel(parent)
|
2015-06-20 20:27:03 +02:00
|
|
|
{
|
|
|
|
columns << tr("IP/Netmask") << tr("Banned Until");
|
2016-11-18 15:47:20 +01:00
|
|
|
priv.reset(new BanTablePriv());
|
2015-06-20 20:27:03 +02:00
|
|
|
// default to unsorted
|
|
|
|
priv->sortColumn = -1;
|
|
|
|
|
|
|
|
// load initial data
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:47:20 +01:00
|
|
|
BanTableModel::~BanTableModel()
|
|
|
|
{
|
|
|
|
// Intentionally left empty
|
|
|
|
}
|
|
|
|
|
2015-06-20 20:27:03 +02:00
|
|
|
int BanTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return priv->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BanTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
2015-12-30 04:42:27 +01:00
|
|
|
return columns.length();
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant BanTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
|
|
|
|
|
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch(index.column())
|
|
|
|
{
|
|
|
|
case Address:
|
|
|
|
return QString::fromStdString(rec->subnet.ToString());
|
|
|
|
case Bantime:
|
2015-06-21 10:25:00 +02:00
|
|
|
QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
|
2015-07-03 08:35:14 +02:00
|
|
|
date = date.addSecs(rec->banEntry.nBanUntil);
|
2015-06-21 10:44:48 +02:00
|
|
|
return date.toString(Qt::SystemLocaleLongDate);
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant BanTableModel::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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
CCombinedBan *data = priv->index(row);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
return createIndex(row, column, data);
|
2015-06-23 21:10:42 +02:00
|
|
|
return QModelIndex();
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BanTableModel::refresh()
|
|
|
|
{
|
2015-07-29 14:34:14 +02:00
|
|
|
Q_EMIT layoutAboutToBeChanged();
|
2017-04-17 22:02:44 +02:00
|
|
|
priv->refreshBanlist(m_node);
|
2015-07-29 14:34:14 +02:00
|
|
|
Q_EMIT layoutChanged();
|
2015-06-20 20:27:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BanTableModel::sort(int column, Qt::SortOrder order)
|
|
|
|
{
|
|
|
|
priv->sortColumn = column;
|
|
|
|
priv->sortOrder = order;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BanTableModel::shouldShow()
|
|
|
|
{
|
2017-04-10 10:24:49 +02:00
|
|
|
return priv->size() > 0;
|
2015-12-30 04:42:27 +01:00
|
|
|
}
|