PRCYCoin  2.0.0.7rc1
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2015-2018 The PIVX developers
3 // Copyright (c) 2018-2020 The DAPS Project developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "bantablemodel.h"
8 
9 #include "clientmodel.h"
10 #include "guiconstants.h"
11 #include "guiutil.h"
12 
13 #include "sync.h"
14 #include "utiltime.h"
15 
16 #include <algorithm>
17 
18 #include <QDebug>
19 #include <QList>
20 
21 bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
22 {
23  const CCombinedBan* pLeft = &left;
24  const CCombinedBan* pRight = &right;
25 
26  if (order == Qt::DescendingOrder)
27  std::swap(pLeft, pRight);
28 
29  switch(column)
30  {
32  return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
34  return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
35  }
36 
37  return false;
38 }
39 
40 // private implementation
42 {
43 public:
45  QList<CCombinedBan> cachedBanlist;
49  Qt::SortOrder sortOrder;
50 
53  {
54  banmap_t banMap;
55  CNode::GetBanned(banMap);
56 
57  cachedBanlist.clear();
58  cachedBanlist.reserve(banMap.size());
59  for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
60  {
61  CCombinedBan banEntry;
62  banEntry.subnet = (*it).first;
63  banEntry.banEntry = (*it).second;
64  cachedBanlist.append(banEntry);
65  }
66 
67  if (sortColumn >= 0)
68  // sort cachedBanlist (use stable sort to prevent rows jumping around unneceesarily)
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 0;
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 }
CCombinedBan
Definition: bantablemodel.h:18
utiltime.h
BanTableModel::sort
void sort(int column, Qt::SortOrder order)
Definition: bantablemodel.cpp:176
CCombinedBan::banEntry
CBanEntry banEntry
Definition: bantablemodel.h:20
sync.h
BannedNodeLessThan::operator()
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
Definition: bantablemodel.cpp:21
BannedNodeLessThan::order
Qt::SortOrder order
Definition: bantablemodel.h:32
BanTablePriv::sortColumn
int sortColumn
Column to sort nodes by.
Definition: bantablemodel.cpp:47
BanTableModel::Bantime
@ Bantime
Definition: bantablemodel.h:51
BanTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const
Definition: bantablemodel.cpp:159
BanTablePriv::sortOrder
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
Definition: bantablemodel.cpp:49
BanTablePriv::size
int size() const
Definition: bantablemodel.cpp:72
CCombinedBan::subnet
CSubNet subnet
Definition: bantablemodel.h:19
CNode::GetBanned
static void GetBanned(banmap_t &banmap)
Definition: net.cpp:573
BanTableModel::BanTableModel
BanTableModel(ClientModel *parent=0)
Definition: bantablemodel.cpp:86
BanTableModel::~BanTableModel
~BanTableModel()
Definition: bantablemodel.cpp:99
guiutil.h
BanTablePriv::cachedBanlist
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
Definition: bantablemodel.cpp:45
BanTableModel::rowCount
int rowCount(const QModelIndex &parent) const
Definition: bantablemodel.cpp:104
BanTablePriv::refreshBanlist
void refreshBanlist()
Pull a full list of banned nodes from CNode into our cache.
Definition: bantablemodel.cpp:52
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition: net.h:302
BanTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: bantablemodel.cpp:150
BanTableModel::shouldShow
bool shouldShow()
Definition: bantablemodel.cpp:183
BanTablePriv::index
CCombinedBan * index(int idx)
Definition: bantablemodel.cpp:77
BannedNodeLessThan
Definition: bantablemodel.h:23
BanTableModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: bantablemodel.cpp:116
ClientModel
Model for PRCY network client.
Definition: clientmodel.h:44
BanTableModel::priv
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:72
guiconstants.h
CSubNet::ToString
std::string ToString() const
Definition: netaddress.cpp:660
BanTableModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Definition: bantablemodel.cpp:138
CBanEntry::nBanUntil
int64_t nBanUntil
Definition: net.h:257
bantablemodel.h
BanTableModel::columnCount
int columnCount(const QModelIndex &parent) const
Definition: bantablemodel.cpp:110
BanTableModel::refresh
void refresh()
Definition: bantablemodel.cpp:169
BannedNodeLessThan::column
int column
Definition: bantablemodel.h:31
BanTableModel::columns
QStringList columns
Definition: bantablemodel.h:71
clientmodel.h
BanTablePriv
Definition: bantablemodel.cpp:41
BanTableModel::Address
@ Address
Definition: bantablemodel.h:50