2
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
3
* Copyright (C) 2013 Francois Ferrand
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.
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.
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/>.
19
#include "BrowserAccessControlDialog.h"
20
#include "ui_BrowserAccessControlDialog.h"
23
#include "core/Entry.h"
26
BrowserAccessControlDialog::BrowserAccessControlDialog(QWidget* parent)
28
, m_ui(new Ui::BrowserAccessControlDialog())
30
setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
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)),
40
SLOT(selectionChanged()));
41
connect(m_ui->itemsTable, SIGNAL(acceptSelections()), SLOT(accept()));
42
connect(m_ui->itemsTable, SIGNAL(focusInWithoutSelections()), this, SLOT(selectionChanged()));
45
BrowserAccessControlDialog::~BrowserAccessControlDialog()
49
void BrowserAccessControlDialog::setEntries(const QList<Entry*>& entriesToConfirm,
50
const QString& urlString,
54
m_ui->siteLabel->setText(m_ui->siteLabel->text().arg(
55
url.toDisplayString(QUrl::RemoveUserInfo | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment)));
57
m_ui->rememberDecisionCheckBox->setVisible(!httpAuth);
58
m_ui->rememberDecisionCheckBox->setChecked(false);
60
m_ui->itemsTable->setRowCount(entriesToConfirm.count());
61
m_ui->itemsTable->setColumnCount(2);
64
for (const auto& entry : entriesToConfirm) {
65
addEntryToList(entry, row);
68
m_ui->itemsTable->resizeColumnsToContents();
69
m_ui->itemsTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
70
m_ui->itemsTable->selectAll();
71
m_ui->allowButton->setFocus();
74
void BrowserAccessControlDialog::addEntryToList(Entry* entry, int row)
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);
82
auto disableButton = new QPushButton();
83
disableButton->setIcon(icons()->icon("entry-delete"));
84
disableButton->setToolTip(tr("Disable for this site"));
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);
92
font.setStrikeOut(false);
95
disableButton->setIcon(icons()->icon("entry-delete"));
96
disableButton->setToolTip(tr("Disable for this site"));
97
m_ui->rememberDecisionCheckBox->setEnabled(true);
99
item->setFlags(Qt::NoItemFlags);
100
item->setSelected(false);
102
font.setStrikeOut(true);
105
disableButton->setIcon(icons()->icon("entry-restore"));
106
disableButton->setToolTip(tr("Undo"));
108
// Disable Remember checkbox if all items are disabled
109
auto areAllDisabled = BrowserAccessControlDialog::areAllDisabled();
110
m_ui->rememberDecisionCheckBox->setEnabled(!areAllDisabled);
114
m_ui->itemsTable->setCellWidget(row, 1, disableButton);
117
bool BrowserAccessControlDialog::remember() const
119
return m_ui->rememberDecisionCheckBox->isChecked();
122
QList<QTableWidgetItem*> BrowserAccessControlDialog::getEntries(SelectionType selectionType) const
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);
136
void BrowserAccessControlDialog::selectionChanged()
138
auto selectedRows = m_ui->itemsTable->selectionModel()->selectedRows();
140
m_ui->allowButton->setEnabled(!selectedRows.isEmpty());
141
m_ui->allowButton->setDefault(!selectedRows.isEmpty());
142
m_ui->allowButton->setAutoDefault(!selectedRows.isEmpty());
144
if (selectedRows.isEmpty()) {
145
m_ui->allowButton->clearFocus();
146
m_ui->denyButton->setFocus();
150
bool BrowserAccessControlDialog::areAllDisabled() const
152
auto areAllDisabled = true;
153
for (const auto& item : getAllItems()) {
154
if (item->flags() != Qt::NoItemFlags) {
155
areAllDisabled = false;
159
return areAllDisabled;
162
QList<QTableWidgetItem*> BrowserAccessControlDialog::getAllItems() const
164
QList<QTableWidgetItem*> items;
165
for (int i = 0; i < m_ui->itemsTable->rowCount(); ++i) {
166
auto item = m_ui->itemsTable->item(i, 0);