forked from shadowsocks/shadowsocks-qt5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectiontablemodel.cpp
159 lines (140 loc) · 4.62 KB
/
connectiontablemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "connectiontablemodel.h"
ConnectionTableModel::ConnectionTableModel(QObject *parent) :
QAbstractTableModel(parent)
{}
ConnectionTableModel::~ConnectionTableModel()
{}
ConnectionItem *ConnectionTableModel::getItem(const int &row) const
{
return items.at(row);
}
int ConnectionTableModel::rowCount(const QModelIndex &) const
{
return items.count();
}
int ConnectionTableModel::columnCount(const QModelIndex &) const
{
return ConnectionItem::columnCount();
}
QVariant ConnectionTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
ConnectionItem *item = getItem(index.row());
return item->data(index.column(), role);
}
QVariant ConnectionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical || role != Qt::DisplayRole) {
return QVariant();
}
switch (section) {
case 0:
return QVariant(tr("Name"));
case 1:
return QVariant(tr("Server"));
case 2:
return QVariant(tr("Status"));
case 3:
return QVariant(tr("Latency"));
case 4:
return QVariant(tr("Local Port"));
case 5:
return QVariant(tr("Term Usage"));
case 6:
return QVariant(tr("Total Usage"));
case 7:
return QVariant(tr("Reset Date"));
case 8:
return QVariant(tr("Last Used"));
default:
return QVariant();
}
}
QModelIndex ConnectionTableModel::index(int row, int column, const QModelIndex &) const
{
if (row < 0 || row >= items.size()) {
return QModelIndex();
} else {
ConnectionItem* item = items.at(row);
return createIndex(row, column, item);//column is ignored (all columns have the same effect)
}
}
bool ConnectionTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (row < 0 || count <= 0 || count + row > items.count()) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
items.erase(items.begin() + row, items.begin() + row + count);
endRemoveRows();
return true;
}
bool ConnectionTableModel::move(int row, int target, const QModelIndex &parent)
{
if (row < 0 || row >= rowCount() || target < 0 || target >= rowCount() || row == target) {
return false;
}
//http://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows
int movTarget = target;
if (target - row > 0) {
movTarget++;
}
beginMoveRows(parent, row, row, parent, movTarget);
items.move(row, target);
endMoveRows();
return true;
}
bool ConnectionTableModel::appendConnection(Connection *con, const QModelIndex &parent)
{
ConnectionItem* newItem = new ConnectionItem(con, this);
connect(newItem, &ConnectionItem::message, this, &ConnectionTableModel::message);
connect(newItem, &ConnectionItem::stateChanged, this, &ConnectionTableModel::onConnectionStateChanged);
connect(newItem, &ConnectionItem::latencyChanged, this, &ConnectionTableModel::onConnectionLatencyChanged);
connect(newItem, &ConnectionItem::dataUsageChanged, this, &ConnectionTableModel::onConnectionDataUsageChanged);
beginInsertRows(parent, items.count(), items.count());
items.append(newItem);
endInsertRows();
return true;
}
void ConnectionTableModel::disconnectConnectionsAt(const QString &addr, quint16 port)
{
bool anyAddr = (addr.compare("0.0.0.0") == 0);
for (auto &i : items) {
Connection *con = i->getConnection();
if (con->isRunning() && con->getProfile().localPort == port) {
if ((con->getProfile().localAddress == addr) ||
(con->getProfile().localAddress.compare("0.0.0.0") == 0) ||
anyAddr) {
con->stop();
}
}
}
}
void ConnectionTableModel::testAllLatency()
{
for (auto &i : items) {
i->testLatency();
}
}
void ConnectionTableModel::onConnectionStateChanged(bool running)
{
ConnectionItem *item = qobject_cast<ConnectionItem*>(sender());
int row = items.indexOf(item);
emit dataChanged(this->index(row, 0),
this->index(row, ConnectionItem::columnCount() - 1));
emit rowStatusChanged(row, running);
}
void ConnectionTableModel::onConnectionLatencyChanged()
{
ConnectionItem *item = qobject_cast<ConnectionItem*>(sender());
int row = items.indexOf(item);
emit dataChanged(this->index(row, 3), this->index(row, 3));
}
void ConnectionTableModel::onConnectionDataUsageChanged()
{
ConnectionItem *item = qobject_cast<ConnectionItem*>(sender());
int row = items.indexOf(item);
emit dataChanged(this->index(row, 5), this->index(row, 6));
}