[test] Replace check_output with low level version
[bitcoinplatinum.git] / src / qt / bantablemodel.cpp
blobf8a99506c131ea35169a73cf7240d4b818599282
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"
9 #include "guiutil.h"
11 #include "sync.h"
12 #include "utiltime.h"
14 #include <QDebug>
15 #include <QList>
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);
25 switch(column)
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;
33 return false;
36 // private implementation
37 class BanTablePriv
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;
47 /** Pull a full list of banned nodes from CNode into our cache */
48 void refreshBanlist()
50 banmap_t banMap;
51 if(g_connman)
52 g_connman->GetBanned(banMap);
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++)
60 CCombinedBan banEntry;
61 banEntry.subnet = (*it).first;
62 banEntry.banEntry = (*it).second;
63 cachedBanlist.append(banEntry);
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));
71 int size() const
73 return cachedBanlist.size();
76 CCombinedBan *index(int idx)
78 if (idx >= 0 && idx < cachedBanlist.size())
79 return &cachedBanlist[idx];
81 return 0;
85 BanTableModel::BanTableModel(ClientModel *parent) :
86 QAbstractTableModel(parent),
87 clientModel(parent)
89 columns << tr("IP/Netmask") << tr("Banned Until");
90 priv.reset(new BanTablePriv());
91 // default to unsorted
92 priv->sortColumn = -1;
94 // load initial data
95 refresh();
98 BanTableModel::~BanTableModel()
100 // Intentionally left empty
103 int BanTableModel::rowCount(const QModelIndex &parent) const
105 Q_UNUSED(parent);
106 return priv->size();
109 int BanTableModel::columnCount(const QModelIndex &parent) const
111 Q_UNUSED(parent);
112 return columns.length();
115 QVariant BanTableModel::data(const QModelIndex &index, int role) const
117 if(!index.isValid())
118 return QVariant();
120 CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
122 if (role == Qt::DisplayRole) {
123 switch(index.column())
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);
134 return QVariant();
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];
146 return QVariant();
149 Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
151 if(!index.isValid())
152 return 0;
154 Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
155 return retval;
158 QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
160 Q_UNUSED(parent);
161 CCombinedBan *data = priv->index(row);
163 if (data)
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;
179 refresh();
182 bool BanTableModel::shouldShow()
184 return priv->size() > 0;