Fabcoin Core  0.16.2
P2P Digital Currency
bantablemodel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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 <bantablemodel.h>
6 
7 #include <clientmodel.h>
8 #include <guiconstants.h>
9 #include <guiutil.h>
10 
11 #include <sync.h>
12 #include <utiltime.h>
13 
14 #include <QDebug>
15 #include <QList>
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;
45  Qt::SortOrder sortOrder;
46 
49  {
50  banmap_t banMap;
51  if(g_connman)
52  g_connman->GetBanned(banMap);
53 
54  cachedBanlist.clear();
55 #if QT_VERSION >= 0x040700
56  cachedBanlist.reserve(banMap.size());
57 #endif
58  for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
59  {
60  CCombinedBan banEntry;
61  banEntry.subnet = (*it).first;
62  banEntry.banEntry = (*it).second;
63  cachedBanlist.append(banEntry);
64  }
65 
66  if (sortColumn >= 0)
67  // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
68  qStableSort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
69  }
70 
71  int size() const
72  {
73  return cachedBanlist.size();
74  }
75 
76  CCombinedBan *index(int idx)
77  {
78  if (idx >= 0 && idx < cachedBanlist.size())
79  return &cachedBanlist[idx];
80 
81  return 0;
82  }
83 };
84 
86  QAbstractTableModel(parent),
87  clientModel(parent)
88 {
89  columns << tr("IP/Netmask") << tr("Banned Until");
90  priv.reset(new BanTablePriv());
91  // default to unsorted
92  priv->sortColumn = -1;
93 
94  // load initial data
95  refresh();
96 }
97 
99 {
100  // Intentionally left empty
101 }
102 
103 int BanTableModel::rowCount(const QModelIndex &parent) const
104 {
105  Q_UNUSED(parent);
106  return priv->size();
107 }
108 
109 int BanTableModel::columnCount(const QModelIndex &parent) const
110 {
111  Q_UNUSED(parent);
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 date.toString(Qt::SystemLocaleLongDate);
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())
152  return 0;
153 
154  Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
155  return retval;
156 }
157 
158 QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
159 {
160  Q_UNUSED(parent);
161  CCombinedBan *data = priv->index(row);
162 
163  if (data)
164  return createIndex(row, column, data);
165  return QModelIndex();
166 }
167 
169 {
170  Q_EMIT layoutAboutToBeChanged();
171  priv->refreshBanlist();
172  Q_EMIT layoutChanged();
173 }
174 
175 void BanTableModel::sort(int column, Qt::SortOrder order)
176 {
177  priv->sortColumn = column;
178  priv->sortOrder = order;
179  refresh();
180 }
181 
183 {
184  return priv->size() > 0;
185 }
int size() const
void refreshBanlist()
Pull a full list of banned nodes from CNode into our cache.
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
bool operator()(const CCombinedBan &left, const CCombinedBan &right) const
int sortColumn
Column to sort nodes by.
std::string ToString() const
Definition: netaddress.cpp:683
CSubNet subnet
Definition: bantablemodel.h:17
QVariant headerData(int section, Qt::Orientation orientation, int role) const
QStringList columns
Definition: bantablemodel.h:69
int columnCount(const QModelIndex &parent) const
std::map< CSubNet, CBanEntry > banmap_t
Definition: addrdb.h:77
std::unique_ptr< BanTablePriv > priv
Definition: bantablemodel.h:70
QVariant data(const QModelIndex &index, int role) const
Qt::ItemFlags flags(const QModelIndex &index) const
int rowCount(const QModelIndex &parent) const
Qt::SortOrder sortOrder
Order (ascending or descending) to sort nodes by.
Model for Fabcoin network client.
Definition: clientmodel.h:38
int64_t nBanUntil
Definition: addrdb.h:32
BannedNodeLessThan(int nColumn, Qt::SortOrder fOrder)
Definition: bantablemodel.h:24
QModelIndex index(int row, int column, const QModelIndex &parent) const
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:75
BanTableModel(ClientModel *parent=0)
QList< CCombinedBan > cachedBanlist
Local cache of peer information.
CBanEntry banEntry
Definition: bantablemodel.h:18
void sort(int column, Qt::SortOrder order)
CCombinedBan * index(int idx)
Qt::SortOrder order
Definition: bantablemodel.h:30