Raven Core  3.0.0
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven 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 <QDebug>
16 #include <QList>
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;
46  Qt::SortOrder sortOrder;
47 
50  {
51  banmap_t banMap;
52  if(g_connman)
53  g_connman->GetBanned(banMap);
54 
55  cachedBanlist.clear();
56 #if QT_VERSION >= 0x040700
57  cachedBanlist.reserve(banMap.size());
58 #endif
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 unnecessarily)
69  qStableSort(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  return priv->size() > 0;
186 }
void refreshBanlist()
Pull a full list of banned nodes from CNode into our cache.
int columnCount(const QModelIndex &parent) const
int sortColumn
Column to sort nodes by.
CSubNet subnet
Definition: bantablemodel.h:18
QVariant data(const QModelIndex &index, int role) const
QStringList columns
Definition: bantablemodel.h:70
QVariant headerData(int section, Qt::Orientation orientation, int role) const
std::map< CSubNet, CBanEntry > banmap_t
Definition: addrdb.h:78
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:71
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
Model for Raven network client.
Definition: clientmodel.h:39
int64_t nBanUntil
Definition: addrdb.h:33
BannedNodeLessThan(int nColumn, Qt::SortOrder fOrder)
Definition: bantablemodel.h:25
std::string ToString() const
Definition: netaddress.cpp:684
int rowCount(const QModelIndex &parent) const
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:81
BanTableModel(ClientModel *parent=0)
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
CBanEntry banEntry
Definition: bantablemodel.h:19
int size() const
QModelIndex index(int row, int column, const QModelIndex &parent) const
void sort(int column, Qt::SortOrder order)
CCombinedBan * index(int idx)
Qt::SortOrder order
Definition: bantablemodel.h:31
Qt::ItemFlags flags(const QModelIndex &index) const