Bitcoin ABC 0.32.4
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2016 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <qt/bantablemodel.h>
6
7#include <interfaces/node.h>
8#include <net_types.h> // For banmap_t
9
10#include <utility>
11
12#include <QDateTime>
13#include <QList>
14#include <QLocale>
15#include <QModelIndex>
16#include <QVariant>
17
19 const CCombinedBan &right) const {
20 const CCombinedBan *pLeft = &left;
21 const CCombinedBan *pRight = &right;
22
23 if (order == Qt::DescendingOrder) {
24 std::swap(pLeft, pRight);
25 }
26
27 switch (column) {
29 return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) <
30 0;
32 return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
33 }
34
35 return false;
36}
37
38// private implementation
40public:
42 QList<CCombinedBan> cachedBanlist;
44 int sortColumn{-1};
46 Qt::SortOrder sortOrder;
47
50 banmap_t banMap;
51 node.getBanned(banMap);
52
53 cachedBanlist.clear();
54 cachedBanlist.reserve(banMap.size());
55 for (const auto &entry : banMap) {
56 CCombinedBan banEntry;
57 banEntry.subnet = entry.first;
58 banEntry.banEntry = entry.second;
59 cachedBanlist.append(banEntry);
60 }
61
62 if (sortColumn >= 0) {
63 // sort cachedBanlist (use stable sort to prevent rows jumping
64 // around unnecessarily)
65 std::stable_sort(cachedBanlist.begin(), cachedBanlist.end(),
67 }
68 }
69
70 int size() const { return cachedBanlist.size(); }
71
72 CCombinedBan *index(int idx) {
73 if (idx >= 0 && idx < cachedBanlist.size()) {
74 return &cachedBanlist[idx];
75 }
76
77 return nullptr;
78 }
79};
80
82 : QAbstractTableModel(parent), m_node(node) {
83 columns << tr("IP/Netmask") << tr("Banned Until");
84 priv.reset(new BanTablePriv());
85
86 // load initial data
87 refresh();
88}
89
91
92int BanTableModel::rowCount(const QModelIndex &parent) const {
93 Q_UNUSED(parent);
94 return priv->size();
95}
96
97int BanTableModel::columnCount(const QModelIndex &parent) const {
98 Q_UNUSED(parent);
99 return columns.length();
100}
101
102QVariant BanTableModel::data(const QModelIndex &index, int role) const {
103 if (!index.isValid()) {
104 return QVariant();
105 }
106
107 CCombinedBan *rec = static_cast<CCombinedBan *>(index.internalPointer());
108
109 if (role == Qt::DisplayRole) {
110 switch (index.column()) {
111 case Address:
112 return QString::fromStdString(rec->subnet.ToString());
113 case Bantime:
114 QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
115 date = date.addSecs(rec->banEntry.nBanUntil);
116 return date.toString(QLocale().dateTimeFormat());
117 }
118 }
119
120 return QVariant();
121}
122
123QVariant BanTableModel::headerData(int section, Qt::Orientation orientation,
124 int role) const {
125 if (orientation == Qt::Horizontal) {
126 if (role == Qt::DisplayRole && section < columns.size()) {
127 return columns[section];
128 }
129 }
130 return QVariant();
131}
132
133Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const {
134 if (!index.isValid()) {
135 return Qt::NoItemFlags;
136 }
137
138 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
139 return retval;
140}
141
142QModelIndex BanTableModel::index(int row, int column,
143 const QModelIndex &parent) const {
144 Q_UNUSED(parent);
145 CCombinedBan *data = priv->index(row);
146
147 if (data) {
148 return createIndex(row, column, data);
149 }
150 return QModelIndex();
151}
152
154 Q_EMIT layoutAboutToBeChanged();
155 priv->refreshBanlist(m_node);
156 Q_EMIT layoutChanged();
157}
158
159void BanTableModel::sort(int column, Qt::SortOrder order) {
160 priv->sortColumn = column;
161 priv->sortOrder = order;
162 refresh();
163}
164
166 return priv->size() > 0;
167}
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:74
QVariant data(const QModelIndex &index, int role) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
interfaces::Node & m_node
Definition: bantablemodel.h:72
int columnCount(const QModelIndex &parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void sort(int column, Qt::SortOrder order) override
int rowCount(const QModelIndex &parent) const override
QStringList columns
Definition: bantablemodel.h:73
QModelIndex index(int row, int column, const QModelIndex &parent) const override
BanTableModel(interfaces::Node &node, QObject *parent)
int sortColumn
Column to sort nodes by (default to unsorted)
void refreshBanlist(interfaces::Node &node)
Pull a full list of banned nodes from CNode into our cache.
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
CCombinedBan * index(int idx)
int size() const
Qt::SortOrder order
Definition: bantablemodel.h:35
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
int64_t nBanUntil
Definition: addrdb.h:35
std::string ToString() const
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:59
Definition: init.h:31
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
NodeContext & m_node
Definition: interfaces.cpp:822
CBanEntry banEntry
Definition: bantablemodel.h:24
CSubNet subnet
Definition: bantablemodel.h:23