Skip to content

Commit ad204df

Browse files
committed
[Qt] add banlist table below peers table
1 parent 50f0908 commit ad204df

File tree

7 files changed

+375
-10
lines changed

7 files changed

+375
-10
lines changed

src/Makefile.qt.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ QT_MOC_CPP = \
9797
qt/moc_addressbookpage.cpp \
9898
qt/moc_addresstablemodel.cpp \
9999
qt/moc_askpassphrasedialog.cpp \
100+
qt/moc_bantablemodel.cpp \
100101
qt/moc_bitcoinaddressvalidator.cpp \
101102
qt/moc_bitcoinamountfield.cpp \
102103
qt/moc_bitcoingui.cpp \
@@ -162,6 +163,7 @@ BITCOIN_QT_H = \
162163
qt/addressbookpage.h \
163164
qt/addresstablemodel.h \
164165
qt/askpassphrasedialog.h \
166+
qt/bantablemodel.h \
165167
qt/bitcoinaddressvalidator.h \
166168
qt/bitcoinamountfield.h \
167169
qt/bitcoingui.h \
@@ -260,6 +262,7 @@ RES_ICONS = \
260262
qt/res/icons/verify.png
261263

262264
BITCOIN_QT_CPP = \
265+
qt/bantablemodel.cpp \
263266
qt/bitcoinaddressvalidator.cpp \
264267
qt/bitcoinamountfield.cpp \
265268
qt/bitcoingui.cpp \

src/qt/bantablemodel.cpp

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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 "net.h"
12+
#include "sync.h"
13+
#include "utiltime.h"
14+
15+
#include <QDebug>
16+
#include <QList>
17+
#include <QTimer>
18+
19+
#include <boost/date_time/posix_time/posix_time.hpp>
20+
#include <boost/date_time/c_local_time_adjustor.hpp>
21+
22+
// private implementation
23+
class BanTablePriv
24+
{
25+
public:
26+
/** Local cache of peer information */
27+
QList<CCombinedBan> cachedBanlist;
28+
/** Column to sort nodes by */
29+
int sortColumn;
30+
/** Order (ascending or descending) to sort nodes by */
31+
Qt::SortOrder sortOrder;
32+
33+
/** Pull a full list of banned nodes from CNode into our cache */
34+
void refreshBanlist()
35+
{
36+
std::map<CSubNet, int64_t> banMap;
37+
CNode::GetBanned(banMap);
38+
39+
cachedBanlist.clear();
40+
#if QT_VERSION >= 0x040700
41+
cachedBanlist.reserve(banMap.size());
42+
#endif
43+
std::map<CSubNet, int64_t>::iterator iter;
44+
for (iter = banMap.begin(); iter != banMap.end(); ++iter) {
45+
CCombinedBan banEntry;
46+
banEntry.subnet = iter->first;
47+
banEntry.bantil = iter->second;
48+
cachedBanlist.append(banEntry);
49+
}
50+
}
51+
52+
int size()
53+
{
54+
return cachedBanlist.size();
55+
}
56+
57+
CCombinedBan *index(int idx)
58+
{
59+
if(idx >= 0 && idx < cachedBanlist.size()) {
60+
return &cachedBanlist[idx];
61+
} else {
62+
return 0;
63+
}
64+
}
65+
};
66+
67+
BanTableModel::BanTableModel(ClientModel *parent) :
68+
QAbstractTableModel(parent),
69+
clientModel(parent),
70+
timer(0)
71+
{
72+
columns << tr("IP/Netmask") << tr("Banned Until");
73+
priv = new BanTablePriv();
74+
// default to unsorted
75+
priv->sortColumn = -1;
76+
77+
// set up timer for auto refresh
78+
timer = new QTimer();
79+
connect(timer, SIGNAL(timeout()), SLOT(refresh()));
80+
timer->setInterval(MODEL_UPDATE_DELAY);
81+
82+
// load initial data
83+
refresh();
84+
}
85+
86+
void BanTableModel::startAutoRefresh()
87+
{
88+
timer->start();
89+
}
90+
91+
void BanTableModel::stopAutoRefresh()
92+
{
93+
timer->stop();
94+
}
95+
96+
int BanTableModel::rowCount(const QModelIndex &parent) const
97+
{
98+
Q_UNUSED(parent);
99+
return priv->size();
100+
}
101+
102+
int BanTableModel::columnCount(const QModelIndex &parent) const
103+
{
104+
Q_UNUSED(parent);
105+
return columns.length();;
106+
}
107+
108+
QVariant BanTableModel::data(const QModelIndex &index, int role) const
109+
{
110+
if(!index.isValid())
111+
return QVariant();
112+
113+
CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
114+
115+
if (role == Qt::DisplayRole) {
116+
switch(index.column())
117+
{
118+
case Address:
119+
return QString::fromStdString(rec->subnet.ToString());
120+
case Bantime:
121+
//show time in users local timezone, not 64bit compatible!
122+
//TODO find a way to support 64bit timestamps
123+
boost::posix_time::ptime pt1 = boost::posix_time::from_time_t(rec->bantil);
124+
boost::posix_time::ptime pt2 = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(pt1);
125+
std::stringstream ss;
126+
ss << pt2;
127+
return QString::fromStdString(ss.str());
128+
}
129+
} else if (role == Qt::TextAlignmentRole) {
130+
if (index.column() == Bantime)
131+
return (int)(Qt::AlignRight | Qt::AlignVCenter);
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+
{
165+
return createIndex(row, column, data);
166+
}
167+
else
168+
{
169+
return QModelIndex();
170+
}
171+
}
172+
173+
void BanTableModel::refresh()
174+
{
175+
emit layoutAboutToBeChanged();
176+
priv->refreshBanlist();
177+
emit layoutChanged();
178+
}
179+
180+
void BanTableModel::sort(int column, Qt::SortOrder order)
181+
{
182+
priv->sortColumn = column;
183+
priv->sortOrder = order;
184+
refresh();
185+
}
186+
187+
bool BanTableModel::shouldShow()
188+
{
189+
if (priv->size() > 0)
190+
return true;
191+
return false;
192+
}

src/qt/bantablemodel.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) 2011-2013 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+
#ifndef BITCOIN_QT_BANTABLEMODEL_H
6+
#define BITCOIN_QT_BANTABLEMODEL_H
7+
8+
#include "main.h"
9+
#include "net.h"
10+
11+
#include <QAbstractTableModel>
12+
#include <QStringList>
13+
14+
class ClientModel;
15+
class BanTablePriv;
16+
17+
QT_BEGIN_NAMESPACE
18+
class QTimer;
19+
QT_END_NAMESPACE
20+
21+
struct CCombinedBan {
22+
CSubNet subnet;
23+
int64_t bantil;
24+
};
25+
26+
/**
27+
Qt model providing information about connected peers, similar to the
28+
"getpeerinfo" RPC call. Used by the rpc console UI.
29+
*/
30+
class BanTableModel : public QAbstractTableModel
31+
{
32+
Q_OBJECT
33+
34+
public:
35+
explicit BanTableModel(ClientModel *parent = 0);
36+
void startAutoRefresh();
37+
void stopAutoRefresh();
38+
39+
enum ColumnIndex {
40+
Address = 0,
41+
Bantime = 1,
42+
};
43+
44+
/** @name Methods overridden from QAbstractTableModel
45+
@{*/
46+
int rowCount(const QModelIndex &parent) const;
47+
int columnCount(const QModelIndex &parent) const;
48+
QVariant data(const QModelIndex &index, int role) const;
49+
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
50+
QModelIndex index(int row, int column, const QModelIndex &parent) const;
51+
Qt::ItemFlags flags(const QModelIndex &index) const;
52+
void sort(int column, Qt::SortOrder order);
53+
bool shouldShow();
54+
/*@}*/
55+
56+
public slots:
57+
void refresh();
58+
59+
private:
60+
ClientModel *clientModel;
61+
QStringList columns;
62+
BanTablePriv *priv;
63+
QTimer *timer;
64+
};
65+
66+
#endif // BITCOIN_QT_BANTABLEMODEL_H

src/qt/clientmodel.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "clientmodel.h"
66

7+
#include "bantablemodel.h"
78
#include "guiconstants.h"
89
#include "peertablemodel.h"
910

@@ -26,13 +27,15 @@ ClientModel::ClientModel(OptionsModel *optionsModel, QObject *parent) :
2627
QObject(parent),
2728
optionsModel(optionsModel),
2829
peerTableModel(0),
30+
banTableModel(0),
2931
cachedNumBlocks(0),
3032
cachedBlockDate(QDateTime()),
3133
cachedReindexing(0),
3234
cachedImporting(0),
3335
pollTimer(0)
3436
{
3537
peerTableModel = new PeerTableModel(this);
38+
banTableModel = new BanTableModel(this);
3639
pollTimer = new QTimer(this);
3740
connect(pollTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
3841
pollTimer->start(MODEL_UPDATE_DELAY);
@@ -176,6 +179,11 @@ PeerTableModel *ClientModel::getPeerTableModel()
176179
return peerTableModel;
177180
}
178181

182+
BanTableModel *ClientModel::getBanTableModel()
183+
{
184+
return banTableModel;
185+
}
186+
179187
QString ClientModel::formatFullVersion() const
180188
{
181189
return QString::fromStdString(FormatFullVersion());
@@ -206,6 +214,11 @@ QString ClientModel::formatClientStartupTime() const
206214
return QDateTime::fromTime_t(nClientStartupTime).toString();
207215
}
208216

217+
void ClientModel::updateBanlist()
218+
{
219+
banTableModel->refresh();
220+
}
221+
209222
// Handlers for core signals
210223
static void ShowProgress(ClientModel *clientmodel, const std::string &title, int nProgress)
211224
{

src/qt/clientmodel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QDateTime>
1010

1111
class AddressTableModel;
12+
class BanTableModel;
1213
class OptionsModel;
1314
class PeerTableModel;
1415
class TransactionTableModel;
@@ -44,6 +45,7 @@ class ClientModel : public QObject
4445

4546
OptionsModel *getOptionsModel();
4647
PeerTableModel *getPeerTableModel();
48+
BanTableModel *getBanTableModel();
4749

4850
//! Return number of connections, default is in- and outbound (total)
4951
int getNumConnections(unsigned int flags = CONNECTIONS_ALL) const;
@@ -72,6 +74,7 @@ class ClientModel : public QObject
7274
private:
7375
OptionsModel *optionsModel;
7476
PeerTableModel *peerTableModel;
77+
BanTableModel *banTableModel;
7578

7679
int cachedNumBlocks;
7780
QDateTime cachedBlockDate;
@@ -99,6 +102,7 @@ public Q_SLOTS:
99102
void updateTimer();
100103
void updateNumConnections(int numConnections);
101104
void updateAlert(const QString &hash, int status);
105+
void updateBanlist();
102106
};
103107

104108
#endif // BITCOIN_QT_CLIENTMODEL_H

0 commit comments

Comments
 (0)