Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SettingsWidgetFdoSecrets.cpp « widgets « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 920b603d99c9ab2749e0da769a7f7b8d1a14a4bb (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 *  Copyright (C) 2018 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SettingsWidgetFdoSecrets.h"
#include "ui_SettingsWidgetFdoSecrets.h"

#include "fdosecrets/FdoSecretsPlugin.h"
#include "fdosecrets/FdoSecretsSettings.h"
#include "fdosecrets/objects/Collection.h"
#include "fdosecrets/objects/Prompt.h"
#include "fdosecrets/objects/Session.h"

#include "core/DatabaseIcons.h"
#include "core/FilePath.h"
#include "gui/DatabaseWidget.h"

#include <QAction>
#include <QDebug>
#include <QFileInfo>
#include <QHeaderView>
#include <QPushButton>
#include <QTableWidget>
#include <QToolBar>
#include <QVariant>

using FdoSecrets::Collection;
using FdoSecrets::Service;
using FdoSecrets::Session;

SettingsWidgetFdoSecrets::SettingsWidgetFdoSecrets(FdoSecretsPlugin* plugin, QWidget* parent)
    : QWidget(parent)
    , m_ui(new Ui::SettingsWidgetFdoSecrets())
    , m_plugin(plugin)
{
    m_ui->setupUi(this);

    auto sessHeader = m_ui->tableSessions->horizontalHeader();
    sessHeader->setSelectionMode(QAbstractItemView::NoSelection);
    sessHeader->setSectionsClickable(false);
    sessHeader->setSectionResizeMode(0, QHeaderView::Stretch); // application
    sessHeader->setSectionResizeMode(1, QHeaderView::ResizeToContents); // disconnect button

    auto dbHeader = m_ui->tableDatabases->horizontalHeader();
    dbHeader->setSelectionMode(QAbstractItemView::NoSelection);
    dbHeader->setSectionsClickable(false);
    dbHeader->setSectionResizeMode(0, QHeaderView::Stretch); // file name
    dbHeader->setSectionResizeMode(1, QHeaderView::Stretch); // group
    dbHeader->setSectionResizeMode(2, QHeaderView::ResizeToContents); // manage button

    m_ui->tabWidget->setEnabled(m_ui->enableFdoSecretService->isChecked());
    connect(m_ui->enableFdoSecretService, &QCheckBox::toggled, m_ui->tabWidget, &QTabWidget::setEnabled);
}

SettingsWidgetFdoSecrets::~SettingsWidgetFdoSecrets() = default;

void SettingsWidgetFdoSecrets::populateSessions(bool enabled)
{
    m_ui->tableSessions->setRowCount(0);

    auto service = m_plugin->serviceInstance();
    if (!service || !enabled) {
        return;
    }

    for (const auto& sess : service->sessions()) {
        addSessionRow(sess);
    }
}

void SettingsWidgetFdoSecrets::addSessionRow(Session* sess)
{
    auto row = m_ui->tableSessions->rowCount();
    m_ui->tableSessions->insertRow(row);

    // column 0: application name
    auto item = new QTableWidgetItem(sess->peer());
    item->setData(Qt::UserRole, QVariant::fromValue(sess));
    m_ui->tableSessions->setItem(row, 0, item);

    // column 1: disconnect button
    auto btn = new QPushButton(tr("Disconnect"));
    connect(btn, &QPushButton::clicked, sess, &Session::close);
    m_ui->tableSessions->setCellWidget(row, 1, btn);

    // column 2: hidden uuid
    m_ui->tableSessions->setItem(row, 2, new QTableWidgetItem(sess->id()));
}

void SettingsWidgetFdoSecrets::removeSessionRow(Session* sess)
{
    int row = 0;
    while (row != m_ui->tableSessions->rowCount()) {
        auto item = m_ui->tableSessions->item(row, 0);
        const auto itemSess = item->data(Qt::UserRole).value<Session*>();
        if (itemSess == sess) {
            break;
        }
        ++row;
    }
    if (row == m_ui->tableSessions->rowCount()) {
        qWarning() << "Unknown Fdo Secret Service session" << sess->id() << "while removing collection from table";
        return;
    }

    m_ui->tableSessions->removeRow(row);
}

void SettingsWidgetFdoSecrets::populateDatabases(bool enabled)
{
    m_ui->tableDatabases->setRowCount(0);

    auto service = m_plugin->serviceInstance();
    if (!service || !enabled) {
        return;
    }

    auto ret = service->collections();
    if (ret.isError()) {
        return;
    }
    for (const auto& coll : ret.value()) {
        addDatabaseRow(coll);
    }
}

void SettingsWidgetFdoSecrets::addDatabaseRow(Collection* coll)
{
    auto row = m_ui->tableDatabases->rowCount();
    m_ui->tableDatabases->insertRow(row);

    // column 0: File name
    QFileInfo fi(coll->backend()->database()->filePath());
    auto item = new QTableWidgetItem(fi.fileName());
    item->setData(Qt::UserRole, QVariant::fromValue(coll));
    m_ui->tableDatabases->setItem(row, 0, item);

    // column 2: manage button: hboxlayout: unlock/lock settings
    // create this first so we have a widget to bind connection to,
    // which can then be auto deleted when the row is deleted.
    auto widget = createManageButtons(coll);
    m_ui->tableDatabases->setCellWidget(row, 2, widget);

    // column 1: Group name
    auto itemGroupName = new QTableWidgetItem();
    updateExposedGroupItem(itemGroupName, coll);

    connect(coll, &Collection::collectionLockChanged, widget, [this, itemGroupName, coll](bool) {
        updateExposedGroupItem(itemGroupName, coll);
    });

    m_ui->tableDatabases->setItem(row, 1, itemGroupName);
}

QWidget* SettingsWidgetFdoSecrets::createManageButtons(Collection* coll)
{
    auto toolbar = new QToolBar;
    toolbar->setFloatable(false);
    toolbar->setMovable(false);

    // db settings
    auto dbSettingsAct = new QAction(tr("Database settings"), toolbar);
    dbSettingsAct->setIcon(filePath()->icon(QStringLiteral("actions"), QStringLiteral("document-edit")));
    dbSettingsAct->setToolTip(tr("Edit database settings"));
    dbSettingsAct->setEnabled(!coll->locked().value());
    connect(dbSettingsAct, &QAction::triggered, this, [this, coll]() {
        auto db = coll->backend();
        m_plugin->serviceInstance()->doSwitchToChangeDatabaseSettings(db);
    });
    toolbar->addAction(dbSettingsAct);

    // unlock/lock
    auto lockAct = new QAction(tr("Unlock database"), toolbar);
    lockAct->setIcon(filePath()->icon(QStringLiteral("actions"), QStringLiteral("object-locked"), true));
    lockAct->setToolTip(tr("Unlock database to show more information"));
    connect(coll, &Collection::collectionLockChanged, lockAct, [lockAct, dbSettingsAct](bool locked) {
        if (locked) {
            lockAct->setIcon(filePath()->icon(QStringLiteral("actions"), QStringLiteral("object-locked"), true));
            lockAct->setToolTip(tr("Unlock database to show more information"));
        } else {
            lockAct->setIcon(filePath()->icon(QStringLiteral("actions"), QStringLiteral("object-unlocked"), true));
            lockAct->setToolTip(tr("Lock database"));
        }
        dbSettingsAct->setEnabled(!locked);
    });
    connect(lockAct, &QAction::triggered, this, [coll]() {
        if (coll->locked().value()) {
            coll->doUnlock();
        } else {
            coll->doLock();
        }
    });
    toolbar->addAction(lockAct);

    return toolbar;
}

void SettingsWidgetFdoSecrets::updateExposedGroupItem(QTableWidgetItem* item, Collection* coll)
{
    if (coll->locked().value()) {
        item->setText(tr("Unlock to show"));
        item->setIcon(filePath()->icon(QStringLiteral("apps"), QStringLiteral("object-locked"), true));
        QFont font;
        font.setItalic(true);
        item->setFont(font);
        return;
    }

    auto db = coll->backend()->database();
    auto group = db->rootGroup()->findGroupByUuid(FdoSecrets::settings()->exposedGroup(db));
    if (group) {
        item->setText(group->name());
        item->setIcon(group->isExpired() ? databaseIcons()->iconPixmap(DatabaseIcons::ExpiredIconIndex)
                                         : group->iconScaledPixmap());
        if (group->isExpired()) {
            QFont font;
            font.setStrikeOut(true);
            item->setFont(font);
        }
    } else {
        item->setText(tr("None"));
        item->setIcon(filePath()->icon(QStringLiteral("apps"), QStringLiteral("paint-none"), true));
    }
}

void SettingsWidgetFdoSecrets::removeDatabaseRow(Collection* coll)
{
    int row = 0;
    while (row != m_ui->tableDatabases->rowCount()) {
        auto item = m_ui->tableDatabases->item(row, 0);
        const auto itemColl = item->data(Qt::UserRole).value<Collection*>();
        if (itemColl == coll) {
            break;
        }
        ++row;
    }
    if (row == m_ui->tableDatabases->rowCount()) {
        qWarning() << "Unknown Fdo Secret Service collection" << coll->name() << "while removing collection from table";
        return;
    }

    m_ui->tableDatabases->removeRow(row);
}

void SettingsWidgetFdoSecrets::loadSettings()
{
    m_ui->enableFdoSecretService->setChecked(FdoSecrets::settings()->isEnabled());
    m_ui->showNotification->setChecked(FdoSecrets::settings()->showNotification());
    m_ui->noConfirmDeleteItem->setChecked(FdoSecrets::settings()->noConfirmDeleteItem());
}

void SettingsWidgetFdoSecrets::saveSettings()
{
    FdoSecrets::settings()->setEnabled(m_ui->enableFdoSecretService->isChecked());
    FdoSecrets::settings()->setShowNotification(m_ui->showNotification->isChecked());
    FdoSecrets::settings()->setNoConfirmDeleteItem(m_ui->noConfirmDeleteItem->isChecked());
}

void SettingsWidgetFdoSecrets::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);

    QMetaObject::invokeMethod(this, "updateTables", Qt::QueuedConnection, Q_ARG(bool, true));
}

void SettingsWidgetFdoSecrets::hideEvent(QHideEvent* event)
{
    QWidget::hideEvent(event);

    QMetaObject::invokeMethod(this, "updateTables", Qt::QueuedConnection, Q_ARG(bool, false));
}

void SettingsWidgetFdoSecrets::updateTables(bool enabled)
{
    if (enabled) {
        // update the table
        populateDatabases(m_ui->enableFdoSecretService->isChecked());
        populateSessions(m_ui->enableFdoSecretService->isChecked());

        // re-layout the widget to adjust the table cell size
        adjustSize();

        connect(m_ui->enableFdoSecretService, &QCheckBox::toggled, this, &SettingsWidgetFdoSecrets::populateSessions);
        connect(m_ui->enableFdoSecretService, &QCheckBox::toggled, this, &SettingsWidgetFdoSecrets::populateDatabases);

        auto service = m_plugin->serviceInstance();
        if (service) {
            connect(service, &Service::sessionOpened, this, &SettingsWidgetFdoSecrets::addSessionRow);
            connect(service, &Service::sessionClosed, this, &SettingsWidgetFdoSecrets::removeSessionRow);
            connect(service, &Service::collectionCreated, this, &SettingsWidgetFdoSecrets::addDatabaseRow);
            connect(service, &Service::collectionDeleted, this, &SettingsWidgetFdoSecrets::removeDatabaseRow);
        }
    } else {
        disconnect(
            m_ui->enableFdoSecretService, &QCheckBox::toggled, this, &SettingsWidgetFdoSecrets::populateSessions);
        disconnect(
            m_ui->enableFdoSecretService, &QCheckBox::toggled, this, &SettingsWidgetFdoSecrets::populateDatabases);

        auto service = m_plugin->serviceInstance();
        if (service) {
            disconnect(service, &Service::sessionOpened, this, &SettingsWidgetFdoSecrets::addSessionRow);
            disconnect(service, &Service::sessionClosed, this, &SettingsWidgetFdoSecrets::removeSessionRow);
            disconnect(service, &Service::collectionCreated, this, &SettingsWidgetFdoSecrets::addDatabaseRow);
            disconnect(service, &Service::collectionDeleted, this, &SettingsWidgetFdoSecrets::removeDatabaseRow);
        }
    }
}