2015-06-20 20:27:03 +02:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "bantablemodel.h"
|
|
|
|
|
|
|
|
#include "clientmodel.h"
|
|
|
|
#include "guiconstants.h"
|
|
|
|
#include "guiutil.h"
|
|
|
|
|
|
|
|
#include "sync.h"
|
|
|
|
#include "utiltime.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QList>
|
|
|
|
|
|
|
|
|
|
|
|
// 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 */
|
|
|
|
void refreshBanlist()
|
|
|
|
{
|
|
|
|
std::map<CSubNet, int64_t> banMap;
|
|
|
|
CNode::GetBanned(banMap);
|
|
|
|
|
|
|
|
cachedBanlist.clear();
|
|
|
|
#if QT_VERSION >= 0x040700
|
|
|
|
cachedBanlist.reserve(banMap.size());
|
|
|
|
#endif
|
2015-06-21 10:44:48 +02:00
|
|
|
foreach (const PAIRTYPE(CSubNet, int64_t)& banentry, banMap)
|
|
|
|
{
|
2015-06-20 20:27:03 +02:00
|
|
|
CCombinedBan banEntry;
|
2015-06-21 10:44:48 +02:00
|
|
|
banEntry.subnet = banentry.first;
|
|
|
|
banEntry.bantil = banentry.second;
|
2015-06-20 20:27:03 +02:00
|
|
|
cachedBanlist.append(banEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BanTableModel::BanTableModel(ClientModel *parent) :
|
|
|
|
QAbstractTableModel(parent),
|
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");
|
|
|
|
priv = new BanTablePriv();
|
|
|
|
// default to unsorted
|
|
|
|
priv->sortColumn = -1;
|
|
|
|
|
|
|
|
// load initial data
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BanTableModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return priv->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BanTableModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return columns.length();;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
date = date.addSecs(rec->bantil);
|
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()
|
|
|
|
{
|
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
priv->refreshBanlist();
|
|
|
|
emit layoutChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BanTableModel::sort(int column, Qt::SortOrder order)
|
|
|
|
{
|
|
|
|
priv->sortColumn = column;
|
|
|
|
priv->sortOrder = order;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BanTableModel::shouldShow()
|
|
|
|
{
|
|
|
|
if (priv->size() > 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|