Line data Source code
1 : // Copyright (c) 2011-2015 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 0 : bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
18 : {
19 0 : const CCombinedBan* pLeft = &left;
20 0 : const CCombinedBan* pRight = &right;
21 :
22 0 : if (order == Qt::DescendingOrder)
23 : std::swap(pLeft, pRight);
24 :
25 0 : switch(column)
26 : {
27 : case BanTableModel::Address:
28 0 : return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
29 : case BanTableModel::Bantime:
30 0 : return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
31 : }
32 :
33 : return false;
34 : }
35 :
36 : // private implementation
37 0 : class BanTablePriv
38 : {
39 : public:
40 : /** Local cache of peer information */
41 : QList<CCombinedBan> cachedBanlist;
42 : /** Column to sort nodes by */
43 : int sortColumn;
44 : /** Order (ascending or descending) to sort nodes by */
45 : Qt::SortOrder sortOrder;
46 :
47 : /** Pull a full list of banned nodes from CNode into our cache */
48 0 : void refreshBanlist()
49 : {
50 : banmap_t banMap;
51 0 : CNode::GetBanned(banMap);
52 :
53 0 : cachedBanlist.clear();
54 : #if QT_VERSION >= 0x040700
55 0 : cachedBanlist.reserve(banMap.size());
56 : #endif
57 0 : for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
58 : {
59 : CCombinedBan banEntry;
60 0 : banEntry.subnet = (*it).first;
61 0 : banEntry.banEntry = (*it).second;
62 0 : cachedBanlist.append(banEntry);
63 : }
64 :
65 0 : if (sortColumn >= 0)
66 : // sort cachedBanlist (use stable sort to prevent rows jumping around unneceesarily)
67 0 : qStableSort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
68 0 : }
69 :
70 : int size() const
71 : {
72 0 : return cachedBanlist.size();
73 : }
74 :
75 0 : CCombinedBan *index(int idx)
76 : {
77 0 : if (idx >= 0 && idx < cachedBanlist.size())
78 0 : return &cachedBanlist[idx];
79 :
80 : return 0;
81 : }
82 : };
83 :
84 0 : BanTableModel::BanTableModel(ClientModel *parent) :
85 : QAbstractTableModel(parent),
86 0 : clientModel(parent)
87 : {
88 0 : columns << tr("IP/Netmask") << tr("Banned Until");
89 0 : priv = new BanTablePriv();
90 : // default to unsorted
91 0 : priv->sortColumn = -1;
92 :
93 : // load initial data
94 0 : refresh();
95 0 : }
96 :
97 0 : int BanTableModel::rowCount(const QModelIndex &parent) const
98 : {
99 : Q_UNUSED(parent);
100 0 : return priv->size();
101 : }
102 :
103 0 : int BanTableModel::columnCount(const QModelIndex &parent) const
104 : {
105 : Q_UNUSED(parent);
106 0 : return columns.length();;
107 : }
108 :
109 0 : QVariant BanTableModel::data(const QModelIndex &index, int role) const
110 : {
111 0 : if(!index.isValid())
112 : return QVariant();
113 :
114 0 : CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
115 :
116 0 : if (role == Qt::DisplayRole) {
117 0 : switch(index.column())
118 : {
119 : case Address:
120 0 : return QString::fromStdString(rec->subnet.ToString());
121 : case Bantime:
122 0 : QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
123 0 : date = date.addSecs(rec->banEntry.nBanUntil);
124 0 : return date.toString(Qt::SystemLocaleLongDate);
125 : }
126 : }
127 :
128 : return QVariant();
129 : }
130 :
131 0 : QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
132 : {
133 0 : if(orientation == Qt::Horizontal)
134 : {
135 0 : if(role == Qt::DisplayRole && section < columns.size())
136 : {
137 0 : return columns[section];
138 : }
139 : }
140 : return QVariant();
141 : }
142 :
143 0 : Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
144 : {
145 0 : if(!index.isValid())
146 : return 0;
147 :
148 : Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
149 : return retval;
150 : }
151 :
152 0 : QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
153 : {
154 : Q_UNUSED(parent);
155 0 : CCombinedBan *data = priv->index(row);
156 :
157 0 : if (data)
158 0 : return createIndex(row, column, data);
159 : return QModelIndex();
160 : }
161 :
162 0 : void BanTableModel::refresh()
163 : {
164 0 : Q_EMIT layoutAboutToBeChanged();
165 0 : priv->refreshBanlist();
166 0 : Q_EMIT layoutChanged();
167 0 : }
168 :
169 0 : void BanTableModel::sort(int column, Qt::SortOrder order)
170 : {
171 0 : priv->sortColumn = column;
172 0 : priv->sortOrder = order;
173 0 : refresh();
174 0 : }
175 :
176 0 : bool BanTableModel::shouldShow()
177 : {
178 0 : if (priv->size() > 0)
179 : return true;
180 0 : return false;
181 0 : }
|