Duke Leto
3 years ago
8 changed files with 349 additions and 34 deletions
@ -0,0 +1,185 @@ |
|||
// Copyright 2019-2021 The Hush developers
|
|||
// Released under the GPLv3
|
|||
#include "txtablemodel.h" |
|||
#include "settings.h" |
|||
#include "rpc.h" |
|||
|
|||
PeersTableModel::PeersTableModel(QObject *parent) |
|||
: QAbstractTableModel(parent) { |
|||
headers << QObject::tr("PeerID") << QObject::tr("Address") << QObject::tr("ASN") << QObject::tr("TLS Cipher") << QObject::tr("TLS Verfied") << QObject::tr("Version") << QObject::tr("Protocol Version") << QObject::tr("Ping Time") << QObject::tr("Banscore") << QObject::tr("Bytes received") << QObject::tr("Bytes sent"); |
|||
} |
|||
|
|||
PeersTableModel::~PeersTableModel() { |
|||
delete modeldata; |
|||
} |
|||
|
|||
void PeersTableModel::addData(const QList<PeerItem>& data) { |
|||
peers = new QList<PeerItem>(); |
|||
std::copy(data.begin(), data.end(), std::back_inserter(*peers)); |
|||
|
|||
updateAllData(); |
|||
} |
|||
|
|||
bool PeersTableModel::exportToCsv(QString fileName) const { |
|||
if (!modeldata) |
|||
return false; |
|||
|
|||
QFile file(fileName); |
|||
if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate)) |
|||
return false; |
|||
|
|||
QTextStream out(&file); // we will serialize the data into the file
|
|||
|
|||
// Write headers
|
|||
for (int i = 0; i < headers.length(); i++) { |
|||
out << "\"" << headers[i] << "\","; |
|||
} |
|||
out << "\"Memo\""; |
|||
out << endl; |
|||
|
|||
// Write out each row
|
|||
for (int row = 0; row < modeldata->length(); row++) { |
|||
for (int col = 0; col < headers.length(); col++) { |
|||
out << "\"" << data(index(row, col), Qt::DisplayRole).toString() << "\","; |
|||
} |
|||
} |
|||
|
|||
file.close(); |
|||
return true; |
|||
} |
|||
|
|||
void PeersTableModel::updateAllData() { |
|||
auto newmodeldata = new QList<PeerItem>(); |
|||
|
|||
// Copy peer data so GUI can use it
|
|||
if (peers != nullptr) std::copy( peers->begin(), peers->end(), std::back_inserter(*newmodeldata)); |
|||
|
|||
// Sort by connection time
|
|||
std::sort(newmodeldata->begin(), newmodeldata->end(), [=] (auto a, auto b) { |
|||
return a.conntime > b.conntime; // reverse sort
|
|||
}); |
|||
|
|||
// And then swap out the modeldata with the new one.
|
|||
delete modeldata; |
|||
modeldata = newmodeldata; |
|||
|
|||
// do magic
|
|||
dataChanged(index(0, 0), index(modeldata->size()-1, columnCount(index(0,0))-1)); |
|||
layoutChanged(); |
|||
} |
|||
|
|||
int PeersTableModel::rowCount(const QModelIndex&) const |
|||
{ |
|||
if (modeldata == nullptr) return 0; |
|||
return modeldata->size(); |
|||
} |
|||
|
|||
int PeersTableModel::columnCount(const QModelIndex&) const |
|||
{ |
|||
return headers.size(); |
|||
} |
|||
|
|||
|
|||
QVariant PeersTableModel::data(const QModelIndex &index, int role) const |
|||
{ |
|||
// Align column 4 (amount) right
|
|||
//if (role == Qt::TextAlignmentRole && index.column() == 3) return QVariant(Qt::AlignRight | Qt::AlignVCenter);
|
|||
|
|||
if (role == Qt::ForegroundRole) { |
|||
// peers with banscore >=50 will likely be banned soon, color them red
|
|||
if (modeldata->at(index.row()).banscore >= 50) { |
|||
QBrush b; |
|||
b.setColor(Qt::red); |
|||
return b; |
|||
} |
|||
|
|||
// Else, just return the default brush
|
|||
QBrush b; |
|||
b.setColor(Qt::black); |
|||
return b; |
|||
} |
|||
|
|||
auto dat = modeldata->at(index.row()); |
|||
if (role == Qt::DisplayRole) { |
|||
switch (index.column()) { |
|||
case 0: return dat.peerid; |
|||
case 1: return dat.address; |
|||
case 2: return "AS" + QString::number(dat.asn); |
|||
case 3: return dat.tls_cipher; |
|||
case 4: return dat.tls_verified; |
|||
case 5: return dat.subver; |
|||
case 6: return dat.protocolversion; |
|||
case 7: return dat.pingtime; |
|||
case 8: return dat.banscore; |
|||
case 9: return dat.bytes_received; |
|||
case 10: return dat.bytes_sent; |
|||
} |
|||
} |
|||
|
|||
if (role == Qt::ToolTipRole) { |
|||
switch (index.column()) { |
|||
case 0: return ""; |
|||
} |
|||
} |
|||
|
|||
/*
|
|||
if (role == Qt::DecorationRole && index.column() == 0) { |
|||
if (!dat.memo.isEmpty()) { |
|||
// If the memo is a Payment URI, then show a payment request icon
|
|||
if (dat.memo.startsWith("hush:")) { |
|||
QIcon icon(":/icons/res/paymentreq.gif"); |
|||
return QVariant(icon.pixmap(16, 16)); |
|||
} else { |
|||
// Return the info pixmap to indicate memo
|
|||
QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation); |
|||
return QVariant(icon.pixmap(16, 16)); |
|||
} |
|||
} else { |
|||
// Empty pixmap to make it align
|
|||
QPixmap p(16, 16); |
|||
p.fill(Qt::white); |
|||
return QVariant(p); |
|||
} |
|||
} |
|||
*/ |
|||
|
|||
return QVariant(); |
|||
} |
|||
|
|||
|
|||
QVariant PeersTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|||
{ |
|||
if (role == Qt::TextAlignmentRole && section == 3) return QVariant(Qt::AlignRight | Qt::AlignVCenter); |
|||
|
|||
if (role == Qt::FontRole) { |
|||
QFont f; |
|||
f.setBold(true); |
|||
return f; |
|||
} |
|||
|
|||
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { |
|||
return headers.at(section); |
|||
} |
|||
|
|||
return QVariant(); |
|||
} |
|||
|
|||
QString PeersTableModel::getPeerId(int row) const { |
|||
return QString::number(modeldata->at(row).peerid); |
|||
} |
|||
|
|||
QString PeersTableModel::getAddress(int row) const { |
|||
return modeldata->at(row).address.trimmed(); |
|||
} |
|||
|
|||
qint64 PeersTableModel::getConntime(int row) const { |
|||
return modeldata->at(row).conntime; |
|||
} |
|||
|
|||
QString PeersTableModel::getType(int row) const { |
|||
return modeldata->at(row).type; |
|||
} |
|||
|
|||
QString PeersTableModel::getPingtime(int row) const { |
|||
return Settings::getDecimalString(modeldata->at(row).pingtime); |
|||
} |
@ -0,0 +1,46 @@ |
|||
// Copyright 2019-2021 The Hush developers
|
|||
// Released under the GPLv3
|
|||
#ifndef PEERSTABLEMODEL_H |
|||
#define PEERSTABLEMODEL_H |
|||
|
|||
#include "precompiled.h" |
|||
|
|||
struct PeerItem; |
|||
|
|||
class PeersTableModel: public QAbstractTableModel |
|||
{ |
|||
public: |
|||
PeersTableModel(QObject* parent); |
|||
~PeersTableModel(); |
|||
|
|||
QString getPeerId(int row) const; |
|||
QString getAddress(int row) const; |
|||
QString getType(int row) const; |
|||
qint64 getConntime(int row) const; |
|||
QString getASN(int row) const; |
|||
QString getSubver(int row) const; |
|||
QString getTLSCipher(int row) const; |
|||
bool getTLSVerified(int row) const; |
|||
QString getPingtime(int row) const; |
|||
unsigned int getBanscore(int row) const; |
|||
unsigned int getProtocolVersion(int row) const; |
|||
|
|||
bool exportToCsv(QString fileName) const; |
|||
|
|||
int rowCount(const QModelIndex &parent) const; |
|||
int columnCount(const QModelIndex &parent) const; |
|||
QVariant data(const QModelIndex &index, int role) const; |
|||
QVariant headerData(int section, Qt::Orientation orientation, int role) const; |
|||
|
|||
void addData (const QList<PeerItem>& data); |
|||
|
|||
private: |
|||
void updateAllData(); |
|||
QList<PeerItem>* peers = nullptr; |
|||
QList<PeerItem>* modeldata = nullptr; |
|||
|
|||
QList<QString> headers; |
|||
}; |
|||
|
|||
|
|||
#endif // PEERSTABLEMODEL_H
|
Loading…
Reference in new issue