-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserverstablemodel.cpp
More file actions
202 lines (165 loc) · 5.1 KB
/
serverstablemodel.cpp
File metadata and controls
202 lines (165 loc) · 5.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright 2020 Sinodun Internet Technologies Ltd.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <QColor>
#include <QFont>
#include "serverstablemodel.h"
ServersTableModel::ServersTableModel(Config& config, Config::NetworkProfile networkProfile, QObject* parent)
: m_config(config), m_networkProfile(networkProfile), QAbstractTableModel(parent)
{
}
ServersTableModel::~ServersTableModel()
{
}
int ServersTableModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 5;
}
int ServersTableModel::rowCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
std::size_t res = 0;
for ( const auto& s : m_config.servers )
if ( s.hidden.find(m_networkProfile) == s.hidden.end() )
res += s.addresses.size();
return static_cast<int>(res);
}
QVariant ServersTableModel::data(const QModelIndex& index, int role) const
{
if ( !index.isValid() )
return QVariant();
int serverIndex, addressIndex;
serverFromRow(index.row(), serverIndex, addressIndex);
if ( serverIndex < 0 )
return QVariant();
const Config::Server& server(m_config.servers[serverIndex]);
const std::string& address(server.addresses[addressIndex]);
if ( role == Qt::DisplayRole )
{
switch (index.column())
{
case 1: // Name
return QString::fromStdString(server.name);
case 2: // Link
return QString::fromStdString(server.link);
case 3: // Address
return QString::fromStdString(address);
case 4: // TLS auth name
return QString::fromStdString(server.tlsAuthName);
case 5: // Key digest value
return QString::fromStdString(server.pubKeyDigestValue);
}
} else if ( role == Qt::CheckStateRole )
{
if ( index.column() == 0 )
{
if ( server.inactive.find(m_networkProfile) == server.inactive.end() )
return Qt::Checked;
else
return Qt::Unchecked;
}
}
else if (role == Qt::FontRole && index.column() == 2) {
QFont font;
font.setUnderline(true);
return font;
} else if (role == Qt::ForegroundRole && index.column() == 2) {
return QColor(Qt::darkBlue);
}
return QVariant();
}
Qt::ItemFlags ServersTableModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags res = Qt::NoItemFlags;
switch ( index.column() )
{
case 0:
res |= Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
break;
case 1:
res |= Qt::ItemIsEnabled;
case 2:
res |= Qt::ItemIsEnabled | Qt::ItemIsSelectable;
break;
}
return res;
}
QVariant ServersTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if ( role != Qt::DisplayRole || orientation != Qt::Horizontal )
return QVariant();
switch (section)
{
case 0: return QString("Active");
case 1: return QString("Name");
case 2: return QString("Website");
case 3: return QString("IP Address");
case 4: return QString("TLS Auth Name");
case 5: return QString("Pin (Key Digest Value)");
}
return QVariant("");
}
bool ServersTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if ( !index.isValid() )
return false;
int serverIndex, addressIndex;
serverFromRow(index.row(), serverIndex, addressIndex);
if ( serverIndex < 0 )
return false;
if ( index.column() == 0 && role == Qt::CheckStateRole )
{
Config::Server& server(m_config.servers[serverIndex]);
if ( static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked )
server.inactive.erase(m_networkProfile);
else
server.inactive.insert(m_networkProfile);
// All rows for this server have changed.
int startRow = index.row() - addressIndex;
int endRow = startRow + static_cast<int>(server.addresses.size()) - 1;
emit(dataChanged(index.siblingAtRow(startRow), index.siblingAtRow(endRow)));
return true;
}
return false;
}
void ServersTableModel::STPConfigChanged()
{
beginResetModel();
endResetModel();
}
void ServersTableModel::serverFromRow(int row, int& serverIndex, int& addressIndex) const
{
serverIndex = 0;
addressIndex = 0;
bool found = false;
for ( auto& s : m_config.servers )
{
if ( s.hidden.find(m_networkProfile) == s.hidden.end() )
{
addressIndex = 0;
for ( auto& a : s.addresses )
{
if ( row == 0 )
{
found = true;
break;
}
--row;
++addressIndex;
}
if ( found )
break;
}
++serverIndex;
}
if ( !found )
{
serverIndex = -1;
addressIndex = -1;
}
}