1 // Copyright (c) 2011-2016 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.
5 #include "bantablemodel.h"
7 #include "clientmodel.h"
8 #include "guiconstants.h"
17 bool BannedNodeLessThan::operator()(const CCombinedBan
& left
, const CCombinedBan
& right
) const
19 const CCombinedBan
* pLeft
= &left
;
20 const CCombinedBan
* pRight
= &right
;
22 if (order
== Qt::DescendingOrder
)
23 std::swap(pLeft
, pRight
);
27 case BanTableModel::Address
:
28 return pLeft
->subnet
.ToString().compare(pRight
->subnet
.ToString()) < 0;
29 case BanTableModel::Bantime
:
30 return pLeft
->banEntry
.nBanUntil
< pRight
->banEntry
.nBanUntil
;
36 // private implementation
40 /** Local cache of peer information */
41 QList
<CCombinedBan
> cachedBanlist
;
42 /** Column to sort nodes by */
44 /** Order (ascending or descending) to sort nodes by */
45 Qt::SortOrder sortOrder
;
47 /** Pull a full list of banned nodes from CNode into our cache */
52 g_connman
->GetBanned(banMap
);
54 cachedBanlist
.clear();
55 #if QT_VERSION >= 0x040700
56 cachedBanlist
.reserve(banMap
.size());
58 for (banmap_t::iterator it
= banMap
.begin(); it
!= banMap
.end(); it
++)
60 CCombinedBan banEntry
;
61 banEntry
.subnet
= (*it
).first
;
62 banEntry
.banEntry
= (*it
).second
;
63 cachedBanlist
.append(banEntry
);
67 // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
68 qStableSort(cachedBanlist
.begin(), cachedBanlist
.end(), BannedNodeLessThan(sortColumn
, sortOrder
));
73 return cachedBanlist
.size();
76 CCombinedBan
*index(int idx
)
78 if (idx
>= 0 && idx
< cachedBanlist
.size())
79 return &cachedBanlist
[idx
];
85 BanTableModel::BanTableModel(ClientModel
*parent
) :
86 QAbstractTableModel(parent
),
89 columns
<< tr("IP/Netmask") << tr("Banned Until");
90 priv
.reset(new BanTablePriv());
91 // default to unsorted
92 priv
->sortColumn
= -1;
98 BanTableModel::~BanTableModel()
100 // Intentionally left empty
103 int BanTableModel::rowCount(const QModelIndex
&parent
) const
109 int BanTableModel::columnCount(const QModelIndex
&parent
) const
112 return columns
.length();
115 QVariant
BanTableModel::data(const QModelIndex
&index
, int role
) const
120 CCombinedBan
*rec
= static_cast<CCombinedBan
*>(index
.internalPointer());
122 if (role
== Qt::DisplayRole
) {
123 switch(index
.column())
126 return QString::fromStdString(rec
->subnet
.ToString());
128 QDateTime date
= QDateTime::fromMSecsSinceEpoch(0);
129 date
= date
.addSecs(rec
->banEntry
.nBanUntil
);
130 return date
.toString(Qt::SystemLocaleLongDate
);
137 QVariant
BanTableModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
139 if(orientation
== Qt::Horizontal
)
141 if(role
== Qt::DisplayRole
&& section
< columns
.size())
143 return columns
[section
];
149 Qt::ItemFlags
BanTableModel::flags(const QModelIndex
&index
) const
154 Qt::ItemFlags retval
= Qt::ItemIsSelectable
| Qt::ItemIsEnabled
;
158 QModelIndex
BanTableModel::index(int row
, int column
, const QModelIndex
&parent
) const
161 CCombinedBan
*data
= priv
->index(row
);
164 return createIndex(row
, column
, data
);
165 return QModelIndex();
168 void BanTableModel::refresh()
170 Q_EMIT
layoutAboutToBeChanged();
171 priv
->refreshBanlist();
172 Q_EMIT
layoutChanged();
175 void BanTableModel::sort(int column
, Qt::SortOrder order
)
177 priv
->sortColumn
= column
;
178 priv
->sortOrder
= order
;
182 bool BanTableModel::shouldShow()
184 return priv
->size() > 0;