Bitcoin ABC  0.24.7
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
39 class BanTablePriv {
40 public:
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  // Intentionally left empty
92 }
93 
94 int BanTableModel::rowCount(const QModelIndex &parent) const {
95  Q_UNUSED(parent);
96  return priv->size();
97 }
98 
99 int BanTableModel::columnCount(const QModelIndex &parent) const {
100  Q_UNUSED(parent);
101  return columns.length();
102 }
103 
104 QVariant BanTableModel::data(const QModelIndex &index, int role) const {
105  if (!index.isValid()) {
106  return QVariant();
107  }
108 
109  CCombinedBan *rec = static_cast<CCombinedBan *>(index.internalPointer());
110 
111  if (role == Qt::DisplayRole) {
112  switch (index.column()) {
113  case Address:
114  return QString::fromStdString(rec->subnet.ToString());
115  case Bantime:
116  QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
117  date = date.addSecs(rec->banEntry.nBanUntil);
118  return date.toString(QLocale().dateTimeFormat());
119  }
120  }
121 
122  return QVariant();
123 }
124 
125 QVariant BanTableModel::headerData(int section, Qt::Orientation orientation,
126  int role) const {
127  if (orientation == Qt::Horizontal) {
128  if (role == Qt::DisplayRole && section < columns.size()) {
129  return columns[section];
130  }
131  }
132  return QVariant();
133 }
134 
135 Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const {
136  if (!index.isValid()) {
137  return Qt::NoItemFlags;
138  }
139 
140  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
141  return retval;
142 }
143 
144 QModelIndex BanTableModel::index(int row, int column,
145  const QModelIndex &parent) const {
146  Q_UNUSED(parent);
147  CCombinedBan *data = priv->index(row);
148 
149  if (data) {
150  return createIndex(row, column, data);
151  }
152  return QModelIndex();
153 }
154 
156  Q_EMIT layoutAboutToBeChanged();
157  priv->refreshBanlist(m_node);
158  Q_EMIT layoutChanged();
159 }
160 
161 void BanTableModel::sort(int column, Qt::SortOrder order) {
162  priv->sortColumn = column;
163  priv->sortOrder = order;
164  refresh();
165 }
166 
168  return priv->size() > 0;
169 }
CCombinedBan
Definition: bantablemodel.h:21
BanTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: bantablemodel.cpp:125
CCombinedBan::banEntry
CBanEntry banEntry
Definition: bantablemodel.h:23
BannedNodeLessThan::operator()
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
Definition: bantablemodel.cpp:18
BanTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: bantablemodel.cpp:135
node.h
BannedNodeLessThan::order
Qt::SortOrder order
Definition: bantablemodel.h:34
BanTablePriv::sortColumn
int sortColumn
Column to sort nodes by (default to unsorted)
Definition: bantablemodel.cpp:44
BanTablePriv::refreshBanlist
void refreshBanlist(interfaces::Node &node)
Pull a full list of banned nodes from CNode into our cache.
Definition: bantablemodel.cpp:49
BanTableModel::Bantime
@ Bantime
Definition: bantablemodel.h:50
BanTableModel::BanTableModel
BanTableModel(interfaces::Node &node, QObject *parent)
Definition: bantablemodel.cpp:81
BanTablePriv::sortOrder
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
Definition: bantablemodel.cpp:46
BanTablePriv::size
int size() const
Definition: bantablemodel.cpp:70
net_types.h
CCombinedBan::subnet
CSubNet subnet
Definition: bantablemodel.h:22
BanTableModel::sort
void sort(int column, Qt::SortOrder order) override
Definition: bantablemodel.cpp:161
BanTableModel::~BanTableModel
~BanTableModel()
Definition: bantablemodel.cpp:90
BanTablePriv::cachedBanlist
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
Definition: bantablemodel.cpp:42
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:55
BanTableModel::shouldShow
bool shouldShow()
Definition: bantablemodel.cpp:167
BanTablePriv::index
CCombinedBan * index(int idx)
Definition: bantablemodel.cpp:72
BanTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const override
Definition: bantablemodel.cpp:144
BannedNodeLessThan
Definition: bantablemodel.h:26
m_node
NodeContext & m_node
Definition: chain.cpp:485
BanTableModel::priv
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:73
BanTableModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: bantablemodel.cpp:99
CSubNet::ToString
std::string ToString() const
Definition: netaddress.cpp:1140
CBanEntry::nBanUntil
int64_t nBanUntil
Definition: addrdb.h:26
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
bantablemodel.h
BanTableModel::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: bantablemodel.cpp:94
BanTableModel::m_node
interfaces::Node & m_node
Definition: bantablemodel.h:71
BanTableModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: bantablemodel.cpp:104
BanTableModel::refresh
void refresh()
Definition: bantablemodel.cpp:155
BannedNodeLessThan::column
int column
Definition: bantablemodel.h:33
BanTableModel::columns
QStringList columns
Definition: bantablemodel.h:72
interfaces::Node::getBanned
virtual bool getBanned(banmap_t &banmap)=0
Get ban map entries.
BanTablePriv
Definition: bantablemodel.cpp:39
BanTableModel::Address
@ Address
Definition: bantablemodel.h:50