Bitcoin Core  0.20.99
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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 <QModelIndex>
15 #include <QVariant>
16 
17 bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
18 {
19  const CCombinedBan* pLeft = &left;
20  const CCombinedBan* pRight = &right;
21 
22  if (order == Qt::DescendingOrder)
23  std::swap(pLeft, pRight);
24 
25  switch(column)
26  {
28  return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
30  return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
31  }
32 
33  return false;
34 }
35 
36 // private implementation
38 {
39 public:
41  QList<CCombinedBan> cachedBanlist;
43  int sortColumn{-1};
45  Qt::SortOrder sortOrder;
46 
49  {
50  banmap_t banMap;
51  node.getBanned(banMap);
52 
53  cachedBanlist.clear();
54  cachedBanlist.reserve(banMap.size());
55  for (const auto& entry : banMap)
56  {
57  CCombinedBan banEntry;
58  banEntry.subnet = entry.first;
59  banEntry.banEntry = entry.second;
60  cachedBanlist.append(banEntry);
61  }
62 
63  if (sortColumn >= 0)
64  // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
65  std::stable_sort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
66  }
67 
68  int size() const
69  {
70  return cachedBanlist.size();
71  }
72 
73  CCombinedBan *index(int idx)
74  {
75  if (idx >= 0 && idx < cachedBanlist.size())
76  return &cachedBanlist[idx];
77 
78  return nullptr;
79  }
80 };
81 
83  QAbstractTableModel(parent),
84  m_node(node)
85 {
86  columns << tr("IP/Netmask") << tr("Banned Until");
87  priv.reset(new BanTablePriv());
88 
89  // load initial data
90  refresh();
91 }
92 
94 {
95  // Intentionally left empty
96 }
97 
98 int BanTableModel::rowCount(const QModelIndex &parent) const
99 {
100  Q_UNUSED(parent);
101  return priv->size();
102 }
103 
104 int BanTableModel::columnCount(const QModelIndex &parent) const
105 {
106  Q_UNUSED(parent);
107  return columns.length();
108 }
109 
110 QVariant BanTableModel::data(const QModelIndex &index, int role) const
111 {
112  if(!index.isValid())
113  return QVariant();
114 
115  CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
116 
117  if (role == Qt::DisplayRole) {
118  switch(index.column())
119  {
120  case Address:
121  return QString::fromStdString(rec->subnet.ToString());
122  case Bantime:
123  QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
124  date = date.addSecs(rec->banEntry.nBanUntil);
125  return date.toString(Qt::SystemLocaleLongDate);
126  }
127  }
128 
129  return QVariant();
130 }
131 
132 QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
133 {
134  if(orientation == Qt::Horizontal)
135  {
136  if(role == Qt::DisplayRole && section < columns.size())
137  {
138  return columns[section];
139  }
140  }
141  return QVariant();
142 }
143 
144 Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
145 {
146  if (!index.isValid()) return Qt::NoItemFlags;
147 
148  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
149  return retval;
150 }
151 
152 QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
153 {
154  Q_UNUSED(parent);
155  CCombinedBan *data = priv->index(row);
156 
157  if (data)
158  return createIndex(row, column, data);
159  return QModelIndex();
160 }
161 
163 {
164  Q_EMIT layoutAboutToBeChanged();
165  priv->refreshBanlist(m_node);
166  Q_EMIT layoutChanged();
167 }
168 
169 void BanTableModel::sort(int column, Qt::SortOrder order)
170 {
171  priv->sortColumn = column;
172  priv->sortOrder = order;
173  refresh();
174 }
175 
177 {
178  return priv->size() > 0;
179 }
QModelIndex index(int row, int column, const QModelIndex &parent) const override
void sort(int column, Qt::SortOrder order) override
NodeContext & m_node
Definition: chain.cpp:411
BanTableModel(interfaces::Node &node, QObject *parent)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QVariant data(const QModelIndex &index, int role) const override
CSubNet subnet
Definition: bantablemodel.h:22
QStringList columns
Definition: bantablemodel.h:75
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:76
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
Qt::ItemFlags flags(const QModelIndex &index) const override
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
void refreshBanlist(interfaces::Node &node)
Pull a full list of banned nodes from CNode into our cache.
interfaces::Node & m_node
Definition: bantablemodel.h:74
int64_t nBanUntil
Definition: addrdb.h:26
BannedNodeLessThan(int nColumn, Qt::SortOrder fOrder)
Definition: bantablemodel.h:29
std::string ToString() const
int rowCount(const QModelIndex &parent) const override
virtual bool getBanned(banmap_t &banmap)=0
Get ban map entries.
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
CBanEntry banEntry
Definition: bantablemodel.h:23
int size() const
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:52
CCombinedBan * index(int idx)
Qt::SortOrder order
Definition: bantablemodel.h:35
int columnCount(const QModelIndex &parent) const override