Debian Codegraph @

src/fdosecrets/widgets/SettingsModels.cpp

raw
1 /*
2 * Copyright (C) 2019 Aetf <[email protected]>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "SettingsModels.h"
19
20 #include "fdosecrets/FdoSecretsSettings.h"
21 #include "fdosecrets/dbus/DBusMgr.h"
22
23 #include "gui/DatabaseTabWidget.h"
24 #include "gui/DatabaseWidget.h"
25 #include "gui/Icons.h"
26
27 #include <QFileInfo>
28
29 namespace FdoSecrets
30 {
31 // static constexpr still requires definition before c++17
32 constexpr const char* SettingsDatabaseModel::ColumnNames[];
33
34 SettingsDatabaseModel::SettingsDatabaseModel(DatabaseTabWidget* dbTabs, QObject* parent)
35 : QAbstractTableModel(parent)
36 , m_dbTabs(nullptr)
37 {
38 setTabWidget(dbTabs);
39 }
40
41 void SettingsDatabaseModel::setTabWidget(DatabaseTabWidget* dbTabs)
42 {
43 auto old = m_dbTabs;
44 m_dbTabs = dbTabs;
45 if (old != m_dbTabs) {
46 populateModel();
47 }
48 }
49
50 int SettingsDatabaseModel::rowCount(const QModelIndex& parent) const
51 {
52 if (parent.isValid()) {
53 return 0;
54 }
55 return m_dbs.size();
56 }
57
58 int SettingsDatabaseModel::columnCount(const QModelIndex& parent) const
59 {
60 if (parent.isValid()) {
61 return 0;
62 }
63 return sizeof(ColumnNames) / sizeof(ColumnNames[0]);
64 }
65
66 QVariant SettingsDatabaseModel::headerData(int section, Qt::Orientation orientation, int role) const
67 {
68 if (orientation != Qt::Horizontal) {
69 return {};
70 }
71
72 if (role != Qt::DisplayRole) {
73 return {};
74 }
75
76 if (section < 0 || section >= columnCount({})) {
77 return {};
78 }
79
80 return qApp->translate(metaObject()->className(), ColumnNames[section]);
81 }
82
83 QVariant SettingsDatabaseModel::data(const QModelIndex& index, int role) const
84 {
85 if (!index.isValid()) {
86 return {};
87 }
88 if (index.model() != this) {
89 return {};
90 }
91 if (index.row() >= rowCount({}) || index.column() >= columnCount({})) {
92 return {};
93 }
94
95 const auto& dbWidget = m_dbs[index.row()];
96 if (!dbWidget) {
97 return {};
98 }
99
100 switch (index.column()) {
101 case ColumnFileName:
102 return dataForName(dbWidget, role);
103 case ColumnGroup:
104 return dataForExposedGroup(dbWidget, role);
105 case ColumnManage:
106 return dataForManage(dbWidget, role);
107 default:
108 return {};
109 }
110 }
111
112 QVariant SettingsDatabaseModel::dataForName(DatabaseWidget* db, int role) const
113 {
114 switch (role) {
115 case Qt::DisplayRole: {
116 QFileInfo fi(db->database()->filePath());
117 return fi.fileName();
118 }
119 case Qt::ToolTipRole:
120 return db->database()->filePath();
121 default:
122 return {};
123 }
124 }
125
126 QVariant SettingsDatabaseModel::dataForExposedGroup(DatabaseWidget* dbWidget, int role)
127 {
128 if (dbWidget->isLocked()) {
129 switch (role) {
130 case Qt::DisplayRole:
131 return tr("Unlock to show");
132 case Qt::DecorationRole:
133 return icons()->icon(QStringLiteral("object-locked"));
134 case Qt::FontRole: {
135 QFont font;
136 font.setItalic(true);
137 return font;
138 }
139 default:
140 return {};
141 }
142 }
143 auto db = dbWidget->database();
144 auto group = db->rootGroup()->findGroupByUuid(FdoSecrets::settings()->exposedGroup(db));
145 if (group) {
146 switch (role) {
147 case Qt::DisplayRole:
148 return group->name();
149 case Qt::DecorationRole:
150 return Icons::groupIconPixmap(group);
151 case Qt::FontRole:
152 if (group->isExpired()) {
153 QFont font;
154 font.setStrikeOut(true);
155 return font;
156 } else {
157 return {};
158 }
159 default:
160 return {};
161 }
162 } else {
163 switch (role) {
164 case Qt::DisplayRole:
165 return tr("None");
166 case Qt::DecorationRole:
167 return icons()->icon(QStringLiteral("paint-none"));
168 default:
169 return {};
170 }
171 }
172 }
173
174 QVariant SettingsDatabaseModel::dataForManage(DatabaseWidget* db, int role) const
175 {
176 switch (role) {
177 case Qt::EditRole:
178 return QVariant::fromValue(db);
179 default:
180 return {};
181 }
182 }
183
184 void SettingsDatabaseModel::populateModel()
185 {
186 beginResetModel();
187
188 m_dbs.clear();
189
190 if (m_dbTabs) {
191 // Add existing database tabs
192 for (int idx = 0; idx != m_dbTabs->count(); ++idx) {
193 auto dbWidget = m_dbTabs->databaseWidgetFromIndex(idx);
194 databaseAdded(dbWidget, false);
195 }
196 // connect signals
197 connect(m_dbTabs, &DatabaseTabWidget::databaseOpened, this, [this](DatabaseWidget* db) {
198 databaseAdded(db, true);
199 });
200 connect(m_dbTabs, &DatabaseTabWidget::databaseClosed, this, &SettingsDatabaseModel::databaseRemoved);
201 }
202
203 endResetModel();
204 }
205
206 void SettingsDatabaseModel::databaseAdded(DatabaseWidget* db, bool emitSignals)
207 {
208 int row = m_dbs.size();
209 if (emitSignals) {
210 beginInsertRows({}, row, row);
211 }
212
213 m_dbs.append(db);
214 connect(db, &DatabaseWidget::databaseLocked, this, [row, this]() {
215 emit dataChanged(index(row, 1), index(row, 2));
216 });
217 connect(db, &DatabaseWidget::databaseUnlocked, this, [row, this]() {
218 emit dataChanged(index(row, 1), index(row, 2));
219 });
220 connect(db, &DatabaseWidget::databaseModified, this, [row, this]() {
221 emit dataChanged(index(row, 0), index(row, 2));
222 });
223 connect(db, &DatabaseWidget::databaseFilePathChanged, this, [row, this]() {
224 emit dataChanged(index(row, 0), index(row, 2));
225 });
226
227 if (emitSignals) {
228 endInsertRows();
229 }
230 }
231
232 void SettingsDatabaseModel::databaseRemoved(const QString& filePath)
233 {
234 for (int i = 0; i != m_dbs.size(); i++) {
235 if (m_dbs[i] && m_dbs[i]->database()->filePath() == filePath) {
236 beginRemoveRows({}, i, i);
237
238 m_dbs[i]->disconnect(this);
239 m_dbs.removeAt(i);
240
241 endRemoveRows();
242 break;
243 }
244 }
245 }
246
247 // static constexpr still requires definition before c++17
248 constexpr const char* SettingsClientModel::ColumnNames[];
249
250 SettingsClientModel::SettingsClientModel(DBusMgr& dbus, QObject* parent)
251 : QAbstractTableModel(parent)
252 , m_dbus(dbus)
253 {
254 populateModel();
255 }
256
257 int SettingsClientModel::rowCount(const QModelIndex& parent) const
258 {
259 if (parent.isValid()) {
260 return 0;
261 }
262 return m_clients.size();
263 }
264
265 int SettingsClientModel::columnCount(const QModelIndex& parent) const
266 {
267 if (parent.isValid()) {
268 return 0;
269 }
270 return sizeof(ColumnNames) / sizeof(ColumnNames[0]);
271 }
272
273 QVariant SettingsClientModel::headerData(int section, Qt::Orientation orientation, int role) const
274 {
275 if (orientation != Qt::Horizontal) {
276 return {};
277 }
278
279 if (role != Qt::DisplayRole) {
280 return {};
281 }
282
283 if (section < 0 || section >= columnCount({})) {
284 return {};
285 }
286
287 return qApp->translate(metaObject()->className(), ColumnNames[section]);
288 }
289
290 QVariant SettingsClientModel::data(const QModelIndex& index, int role) const
291 {
292 if (!index.isValid()) {
293 return {};
294 }
295 if (index.model() != this) {
296 return {};
297 }
298 if (index.row() >= rowCount({}) || index.column() >= columnCount({})) {
299 return {};
300 }
301
302 const auto& client = m_clients[index.row()];
303 if (!client) {
304 return {};
305 }
306
307 switch (index.column()) {
308 case ColumnApplication:
309 return dataForApplication(client, role);
310 case ColumnPID:
311 return dataForPID(client, role);
312 case ColumnDBus:
313 return dataForDBus(client, role);
314 case ColumnManage:
315 return dataForManage(client, role);
316 default:
317 return {};
318 }
319 }
320
321 QVariant SettingsClientModel::dataForApplication(const DBusClientPtr& client, int role) const
322 {
323 const auto& info = client->processInfo();
324 switch (role) {
325 case Qt::DisplayRole:
326 if (info.exePath().isEmpty()) {
327 return tr("Unknown");
328 }
329 return info.exePath();
330 case Qt::ToolTipRole:
331 if (!info.valid) {
332 return tr("Non-existing/inaccessible executable path. Please double-check the client is legit.");
333 }
334 return {};
335 case Qt::DecorationRole:
336 // give some visual clues if the path is invalid
337 if (!info.valid) {
338 return icons()->icon(QStringLiteral("dialog-warning"));
339 }
340 return {};
341 default:
342 return {};
343 }
344 }
345
346 QVariant SettingsClientModel::dataForPID(const DBusClientPtr& client, int role) const
347 {
348 switch (role) {
349 case Qt::DisplayRole:
350 return client->pid();
351 default:
352 return {};
353 }
354 }
355
356 QVariant SettingsClientModel::dataForDBus(const DBusClientPtr& client, int role) const
357 {
358 switch (role) {
359 case Qt::DisplayRole:
360 return client->address();
361 default:
362 return {};
363 }
364 }
365
366 QVariant SettingsClientModel::dataForManage(const DBusClientPtr& client, int role) const
367 {
368 switch (role) {
369 case Qt::EditRole: {
370 return QVariant::fromValue(client);
371 }
372 default:
373 return {};
374 }
375 }
376
377 void SettingsClientModel::populateModel()
378 {
379 beginResetModel();
380
381 m_clients.clear();
382
383 // Add existing database tabs
384 for (const auto& client : m_dbus.clients()) {
385 clientConnected(client, false);
386 }
387
388 // connect signals
389 connect(&m_dbus, &DBusMgr::clientConnected, this, [this](const DBusClientPtr& client) {
390 clientConnected(client, true);
391 });
392 connect(&m_dbus, &DBusMgr::clientDisconnected, this, &SettingsClientModel::clientDisconnected);
393
394 endResetModel();
395 }
396
397 void SettingsClientModel::clientConnected(const DBusClientPtr& client, bool emitSignals)
398 {
399 int row = m_clients.size();
400 if (emitSignals) {
401 beginInsertRows({}, row, row);
402 }
403
404 m_clients.append(client);
405
406 if (emitSignals) {
407 endInsertRows();
408 }
409 }
410
411 void SettingsClientModel::clientDisconnected(const DBusClientPtr& client)
412 {
413 for (int i = 0; i != m_clients.size(); i++) {
414 if (m_clients[i] == client) {
415 beginRemoveRows({}, i, i);
416
417 m_clients.removeAt(i);
418
419 endRemoveRows();
420 break;
421 }
422 }
423 }
424
425 } // namespace FdoSecrets