Bitcoin Core  21.99.0
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 <QLocale>
15 #include <QModelIndex>
16 #include <QVariant>
17 
18 bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
19 {
20  const CCombinedBan* pLeft = &left;
21  const CCombinedBan* pRight = &right;
22 
23  if (order == Qt::DescendingOrder)
24  std::swap(pLeft, pRight);
25 
26  switch(column)
27  {
29  return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
31  return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
32  }
33 
34  return false;
35 }
36 
37 // private implementation
39 {
40 public:
42  QList<CCombinedBan> cachedBanlist;
44  int sortColumn{-1};
46  Qt::SortOrder sortOrder;
47 
50  {
51  banmap_t banMap;
52  node.getBanned(banMap);
53 
54  cachedBanlist.clear();
55  cachedBanlist.reserve(banMap.size());
56  for (const auto& entry : banMap)
57  {
58  CCombinedBan banEntry;
59  banEntry.subnet = entry.first;
60  banEntry.banEntry = entry.second;
61  cachedBanlist.append(banEntry);
62  }
63 
64  if (sortColumn >= 0)
65  // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
66  std::stable_sort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
67  }
68 
69  int size() const
70  {
71  return cachedBanlist.size();
72  }
73 
74  CCombinedBan *index(int idx)
75  {
76  if (idx >= 0 && idx < cachedBanlist.size())
77  return &cachedBanlist[idx];
78 
79  return nullptr;
80  }
81 };
82 
84  QAbstractTableModel(parent),
85  m_node(node)
86 {
87  columns << tr("IP/Netmask") << tr("Banned Until");
88  priv.reset(new BanTablePriv());
89 
90  // load initial data
91  refresh();
92 }
93 
95 {
96  // Intentionally left empty
97 }
98 
99 int BanTableModel::rowCount(const QModelIndex &parent) const
100 {
101  if (parent.isValid()) {
102  return 0;
103  }
104  return priv->size();
105 }
106 
107 int BanTableModel::columnCount(const QModelIndex &parent) const
108 {
109  if (parent.isValid()) {
110  return 0;
111  }
112  return columns.length();
113 }
114 
115 QVariant BanTableModel::data(const QModelIndex &index, int role) const
116 {
117  if(!index.isValid())
118  return QVariant();
119 
120  CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
121 
122  if (role == Qt::DisplayRole) {
123  switch(index.column())
124  {
125  case Address:
126  return QString::fromStdString(rec->subnet.ToString());
127  case Bantime:
128  QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
129  date = date.addSecs(rec->banEntry.nBanUntil);
130  return QLocale::system().toString(date, QLocale::LongFormat);
131  }
132  }
133 
134  return QVariant();
135 }
136 
137 QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
138 {
139  if(orientation == Qt::Horizontal)
140  {
141  if(role == Qt::DisplayRole && section < columns.size())
142  {
143  return columns[section];
144  }
145  }
146  return QVariant();
147 }
148 
149 Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
150 {
151  if (!index.isValid()) return Qt::NoItemFlags;
152 
153  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
154  return retval;
155 }
156 
157 QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
158 {
159  Q_UNUSED(parent);
160  CCombinedBan *data = priv->index(row);
161 
162  if (data)
163  return createIndex(row, column, data);
164  return QModelIndex();
165 }
166 
168 {
169  Q_EMIT layoutAboutToBeChanged();
170  priv->refreshBanlist(m_node);
171  Q_EMIT layoutChanged();
172 }
173 
174 void BanTableModel::sort(int column, Qt::SortOrder order)
175 {
176  priv->sortColumn = column;
177  priv->sortOrder = order;
178  refresh();
179 }
180 
182 {
183  return priv->size() > 0;
184 }
QModelIndex index(int row, int column, const QModelIndex &parent) const override
void sort(int column, Qt::SortOrder order) override
NodeContext & m_node
Definition: interfaces.cpp:660
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