PIVX Core  5.6.99
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2018-2020 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bantablemodel.h"
7 
8 #include "clientmodel.h"
9 #include "guiconstants.h"
10 #include "guiutil.h"
11 
12 #include "sync.h"
13 #include "utiltime.h"
14 
15 #include <algorithm>
16 
17 #include <QDebug>
18 #include <QList>
19 
20 bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
21 {
22  const CCombinedBan* pLeft = &left;
23  const CCombinedBan* pRight = &right;
24 
25  if (order == Qt::DescendingOrder)
26  std::swap(pLeft, pRight);
27 
28  switch(column)
29  {
31  return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
33  return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
34  }
35 
36  return false;
37 }
38 
39 // private implementation
41 {
42 public:
44  QList<CCombinedBan> cachedBanlist;
48  Qt::SortOrder sortOrder;
49 
52  {
53  banmap_t banMap;
54  if(g_connman)
55  g_connman->GetBanned(banMap);
56 
57  cachedBanlist.clear();
58  cachedBanlist.reserve(banMap.size());
59  for (const auto& entry : banMap)
60  {
61  CCombinedBan banEntry;
62  banEntry.subnet = entry.first;
63  banEntry.banEntry = entry.second;
64  cachedBanlist.append(banEntry);
65  }
66 
67  if (sortColumn >= 0)
68  // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
69  std::stable_sort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
70  }
71 
72  int size() const
73  {
74  return cachedBanlist.size();
75  }
76 
77  CCombinedBan *index(int idx)
78  {
79  if (idx >= 0 && idx < cachedBanlist.size())
80  return &cachedBanlist[idx];
81 
82  return 0;
83  }
84 };
85 
87  QAbstractTableModel(parent),
88  clientModel(parent)
89 {
90  columns << tr("IP/Netmask") << tr("Banned Until");
91  priv.reset(new BanTablePriv());
92  // default to unsorted
93  priv->sortColumn = -1;
94 
95  // load initial data
96  refresh();
97 }
98 
100 {
101  // Intentionally left empty
102 }
103 
104 int BanTableModel::rowCount(const QModelIndex &parent) const
105 {
106  Q_UNUSED(parent);
107  return priv->size();
108 }
109 
110 int BanTableModel::columnCount(const QModelIndex &parent) const
111 {
112  Q_UNUSED(parent);
113  return columns.length();
114 }
115 
116 QVariant BanTableModel::data(const QModelIndex &index, int role) const
117 {
118  if(!index.isValid())
119  return QVariant();
120 
121  CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
122 
123  if (role == Qt::DisplayRole) {
124  switch(index.column())
125  {
126  case Address:
127  return QString::fromStdString(rec->subnet.ToString());
128  case Bantime:
129  QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
130  date = date.addSecs(rec->banEntry.nBanUntil);
131  return date.toString(Qt::SystemLocaleLongDate);
132  }
133  }
134 
135  return QVariant();
136 }
137 
138 QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
139 {
140  if(orientation == Qt::Horizontal)
141  {
142  if(role == Qt::DisplayRole && section < columns.size())
143  {
144  return columns[section];
145  }
146  }
147  return QVariant();
148 }
149 
150 Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
151 {
152  if(!index.isValid())
153  return Qt::NoItemFlags;
154 
155  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
156  return retval;
157 }
158 
159 QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
160 {
161  Q_UNUSED(parent);
162  CCombinedBan *data = priv->index(row);
163 
164  if (data)
165  return createIndex(row, column, data);
166  return QModelIndex();
167 }
168 
170 {
171  Q_EMIT layoutAboutToBeChanged();
172  priv->refreshBanlist();
173  Q_EMIT layoutChanged();
174 }
175 
176 void BanTableModel::sort(int column, Qt::SortOrder order)
177 {
178  priv->sortColumn = column;
179  priv->sortOrder = order;
180  refresh();
181 }
182 
184 {
185  if (priv->size() > 0)
186  return true;
187  return false;
188 }
std::map< CSubNet, CBanEntry > banmap_t
Definition: addrdb.h:71
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:71
QVariant data(const QModelIndex &index, int role) const
QVariant headerData(int section, Qt::Orientation orientation, int role) const
int rowCount(const QModelIndex &parent) const
BanTableModel(ClientModel *parent=0)
void sort(int column, Qt::SortOrder order)
Qt::ItemFlags flags(const QModelIndex &index) const
int columnCount(const QModelIndex &parent) const
QStringList columns
Definition: bantablemodel.h:70
QModelIndex index(int row, int column, const QModelIndex &parent) const
int sortColumn
Column to sort nodes by.
void refreshBanlist()
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:31
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
int64_t nBanUntil
Definition: addrdb.h:34
std::string ToString() const
Model for PIVX network client.
Definition: clientmodel.h:50
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:90
CBanEntry banEntry
Definition: bantablemodel.h:19
CSubNet subnet
Definition: bantablemodel.h:18