You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bantablemodel.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "bantablemodel.h"
  5. #include "clientmodel.h"
  6. #include "guiconstants.h"
  7. #include "guiutil.h"
  8. #include "sync.h"
  9. #include "utiltime.h"
  10. #include <QDebug>
  11. #include <QList>
  12. bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan& right) const
  13. {
  14. const CCombinedBan* pLeft = &left;
  15. const CCombinedBan* pRight = &right;
  16. if (order == Qt::DescendingOrder)
  17. std::swap(pLeft, pRight);
  18. switch(column)
  19. {
  20. case BanTableModel::Address:
  21. return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
  22. case BanTableModel::Bantime:
  23. return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
  24. }
  25. return false;
  26. }
  27. // private implementation
  28. class BanTablePriv
  29. {
  30. public:
  31. /** Local cache of peer information */
  32. QList<CCombinedBan> cachedBanlist;
  33. /** Column to sort nodes by */
  34. int sortColumn;
  35. /** Order (ascending or descending) to sort nodes by */
  36. Qt::SortOrder sortOrder;
  37. /** Pull a full list of banned nodes from CNode into our cache */
  38. void refreshBanlist()
  39. {
  40. banmap_t banMap;
  41. if(g_connman)
  42. g_connman->GetBanned(banMap);
  43. cachedBanlist.clear();
  44. #if QT_VERSION >= 0x040700
  45. cachedBanlist.reserve(banMap.size());
  46. #endif
  47. for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
  48. {
  49. CCombinedBan banEntry;
  50. banEntry.subnet = (*it).first;
  51. banEntry.banEntry = (*it).second;
  52. cachedBanlist.append(banEntry);
  53. }
  54. if (sortColumn >= 0)
  55. // sort cachedBanlist (use stable sort to prevent rows jumping around unnecessarily)
  56. qStableSort(cachedBanlist.begin(), cachedBanlist.end(), BannedNodeLessThan(sortColumn, sortOrder));
  57. }
  58. int size() const
  59. {
  60. return cachedBanlist.size();
  61. }
  62. CCombinedBan *index(int idx)
  63. {
  64. if (idx >= 0 && idx < cachedBanlist.size())
  65. return &cachedBanlist[idx];
  66. return 0;
  67. }
  68. };
  69. BanTableModel::BanTableModel(ClientModel *parent) :
  70. QAbstractTableModel(parent),
  71. clientModel(parent)
  72. {
  73. columns << tr("IP/Netmask") << tr("Banned Until");
  74. priv.reset(new BanTablePriv());
  75. // default to unsorted
  76. priv->sortColumn = -1;
  77. // load initial data
  78. refresh();
  79. }
  80. BanTableModel::~BanTableModel()
  81. {
  82. // Intentionally left empty
  83. }
  84. int BanTableModel::rowCount(const QModelIndex &parent) const
  85. {
  86. Q_UNUSED(parent);
  87. return priv->size();
  88. }
  89. int BanTableModel::columnCount(const QModelIndex &parent) const
  90. {
  91. Q_UNUSED(parent);
  92. return columns.length();
  93. }
  94. QVariant BanTableModel::data(const QModelIndex &index, int role) const
  95. {
  96. if(!index.isValid())
  97. return QVariant();
  98. CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
  99. if (role == Qt::DisplayRole) {
  100. switch(index.column())
  101. {
  102. case Address:
  103. return QString::fromStdString(rec->subnet.ToString());
  104. case Bantime:
  105. QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
  106. date = date.addSecs(rec->banEntry.nBanUntil);
  107. return date.toString(Qt::SystemLocaleLongDate);
  108. }
  109. }
  110. return QVariant();
  111. }
  112. QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  113. {
  114. if(orientation == Qt::Horizontal)
  115. {
  116. if(role == Qt::DisplayRole && section < columns.size())
  117. {
  118. return columns[section];
  119. }
  120. }
  121. return QVariant();
  122. }
  123. Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
  124. {
  125. if(!index.isValid())
  126. return 0;
  127. Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  128. return retval;
  129. }
  130. QModelIndex BanTableModel::index(int row, int column, const QModelIndex &parent) const
  131. {
  132. Q_UNUSED(parent);
  133. CCombinedBan *data = priv->index(row);
  134. if (data)
  135. return createIndex(row, column, data);
  136. return QModelIndex();
  137. }
  138. void BanTableModel::refresh()
  139. {
  140. Q_EMIT layoutAboutToBeChanged();
  141. priv->refreshBanlist();
  142. Q_EMIT layoutChanged();
  143. }
  144. void BanTableModel::sort(int column, Qt::SortOrder order)
  145. {
  146. priv->sortColumn = column;
  147. priv->sortOrder = order;
  148. refresh();
  149. }
  150. bool BanTableModel::shouldShow()
  151. {
  152. return priv->size() > 0;
  153. }