keepassxc

Форк
0
/
BrowserAccessControlDialog.cpp 
170 строк · 5.9 Кб
1
/*
2
 *  Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
3
 *  Copyright (C) 2013 Francois Ferrand
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 3 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "BrowserAccessControlDialog.h"
20
#include "ui_BrowserAccessControlDialog.h"
21
#include <QUrl>
22

23
#include "core/Entry.h"
24
#include "gui/Icons.h"
25

26
BrowserAccessControlDialog::BrowserAccessControlDialog(QWidget* parent)
27
    : QDialog(parent)
28
    , m_ui(new Ui::BrowserAccessControlDialog())
29
{
30
    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
31

32
    m_ui->setupUi(this);
33

34
    connect(m_ui->allowButton, SIGNAL(clicked()), SLOT(accept()));
35
    connect(m_ui->denyButton, SIGNAL(clicked()), SLOT(reject()));
36
    connect(m_ui->itemsTable, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(accept()));
37
    connect(m_ui->itemsTable->selectionModel(),
38
            SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
39
            this,
40
            SLOT(selectionChanged()));
41
    connect(m_ui->itemsTable, SIGNAL(acceptSelections()), SLOT(accept()));
42
    connect(m_ui->itemsTable, SIGNAL(focusInWithoutSelections()), this, SLOT(selectionChanged()));
43
}
44

45
BrowserAccessControlDialog::~BrowserAccessControlDialog()
46
{
47
}
48

49
void BrowserAccessControlDialog::setEntries(const QList<Entry*>& entriesToConfirm,
50
                                            const QString& urlString,
51
                                            bool httpAuth)
52
{
53
    QUrl url(urlString);
54
    m_ui->siteLabel->setText(m_ui->siteLabel->text().arg(
55
        url.toDisplayString(QUrl::RemoveUserInfo | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment)));
56

57
    m_ui->rememberDecisionCheckBox->setVisible(!httpAuth);
58
    m_ui->rememberDecisionCheckBox->setChecked(false);
59

60
    m_ui->itemsTable->setRowCount(entriesToConfirm.count());
61
    m_ui->itemsTable->setColumnCount(2);
62

63
    int row = 0;
64
    for (const auto& entry : entriesToConfirm) {
65
        addEntryToList(entry, row);
66
        ++row;
67
    }
68
    m_ui->itemsTable->resizeColumnsToContents();
69
    m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
70
    m_ui->itemsTable->selectAll();
71
    m_ui->allowButton->setFocus();
72
}
73

74
void BrowserAccessControlDialog::addEntryToList(Entry* entry, int row)
75
{
76
    auto item = new QTableWidgetItem();
77
    item->setText(entry->title() + " - " + entry->username());
78
    item->setData(Qt::UserRole, row);
79
    item->setFlags(item->flags() | Qt::ItemIsSelectable);
80
    m_ui->itemsTable->setItem(row, 0, item);
81

82
    auto disableButton = new QPushButton();
83
    disableButton->setIcon(icons()->icon("entry-delete"));
84
    disableButton->setToolTip(tr("Disable for this site"));
85

86
    connect(disableButton, &QAbstractButton::pressed, [&, item, disableButton] {
87
        auto font = item->font();
88
        if (item->flags() == Qt::NoItemFlags) {
89
            item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
90
            item->setSelected(true);
91

92
            font.setStrikeOut(false);
93
            item->setFont(font);
94

95
            disableButton->setIcon(icons()->icon("entry-delete"));
96
            disableButton->setToolTip(tr("Disable for this site"));
97
            m_ui->rememberDecisionCheckBox->setEnabled(true);
98
        } else {
99
            item->setFlags(Qt::NoItemFlags);
100
            item->setSelected(false);
101

102
            font.setStrikeOut(true);
103
            item->setFont(font);
104

105
            disableButton->setIcon(icons()->icon("entry-restore"));
106
            disableButton->setToolTip(tr("Undo"));
107

108
            // Disable Remember checkbox if all items are disabled
109
            auto areAllDisabled = BrowserAccessControlDialog::areAllDisabled();
110
            m_ui->rememberDecisionCheckBox->setEnabled(!areAllDisabled);
111
        }
112
    });
113

114
    m_ui->itemsTable->setCellWidget(row, 1, disableButton);
115
}
116

117
bool BrowserAccessControlDialog::remember() const
118
{
119
    return m_ui->rememberDecisionCheckBox->isChecked();
120
}
121

122
QList<QTableWidgetItem*> BrowserAccessControlDialog::getEntries(SelectionType selectionType) const
123
{
124
    QList<QTableWidgetItem*> selected;
125
    for (auto& item : getAllItems()) {
126
        // Add to list depending on selection type and item status
127
        if ((selectionType == SelectionType::Selected && item->isSelected())
128
            || (selectionType == SelectionType::NonSelected && !item->isSelected())
129
            || (selectionType == SelectionType::Disabled && item->flags() == Qt::NoItemFlags)) {
130
            selected.append(item);
131
        }
132
    }
133
    return selected;
134
}
135

136
void BrowserAccessControlDialog::selectionChanged()
137
{
138
    auto selectedRows = m_ui->itemsTable->selectionModel()->selectedRows();
139

140
    m_ui->allowButton->setEnabled(!selectedRows.isEmpty());
141
    m_ui->allowButton->setDefault(!selectedRows.isEmpty());
142
    m_ui->allowButton->setAutoDefault(!selectedRows.isEmpty());
143

144
    if (selectedRows.isEmpty()) {
145
        m_ui->allowButton->clearFocus();
146
        m_ui->denyButton->setFocus();
147
    }
148
}
149

150
bool BrowserAccessControlDialog::areAllDisabled() const
151
{
152
    auto areAllDisabled = true;
153
    for (const auto& item : getAllItems()) {
154
        if (item->flags() != Qt::NoItemFlags) {
155
            areAllDisabled = false;
156
        }
157
    }
158

159
    return areAllDisabled;
160
}
161

162
QList<QTableWidgetItem*> BrowserAccessControlDialog::getAllItems() const
163
{
164
    QList<QTableWidgetItem*> items;
165
    for (int i = 0; i < m_ui->itemsTable->rowCount(); ++i) {
166
        auto item = m_ui->itemsTable->item(i, 0);
167
        items.append(item);
168
    }
169
    return items;
170
}
171

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.