Falkon Develop
Cross-platform Qt-based web browser
schememanager.cpp
Go to the documentation of this file.
1/* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2023 Juraj Oravec <jurajoravec@mailo.com>
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 "ui_schememanager.h"
20#include "schememanager.h"
21#include "qzsettings.h"
22
23#include <QInputDialog>
24#include <QMessageBox>
25
27 : QDialog(parent)
28 , m_ui(new Ui::SchemeManager)
29{
30 setAttribute(Qt::WA_DeleteOnClose);
31
32 m_ui->setupUi(this);
33
34 reset();
35
36 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &SchemeManager::accept);
37 connect(m_ui->buttonBox->button(QDialogButtonBox::Reset), &QAbstractButton::clicked, this, &SchemeManager::reset);
38 connect(m_ui->blockButton, &QAbstractButton::clicked, this, &SchemeManager::blockScheme);
39 connect(m_ui->allowedAddButton, &QAbstractButton::clicked, this, &SchemeManager::allowedAdd);
40 connect(m_ui->allowedRemoveButton, &QAbstractButton::clicked, this, &SchemeManager::allowedRemove);
41 connect(m_ui->blockedAddButton, &QAbstractButton::clicked, this, &SchemeManager::blockedAdd);
42 connect(m_ui->blockedRemoveButton, &QAbstractButton::clicked, this, &SchemeManager::blockedRemove);
43}
44
45void SchemeManager::reset()
46{
47 Settings settings;
48 settings.beginGroup(QSL("Web-Browser-Settings"));
49
50 m_ui->allowedList->clear();
51 m_ui->allowedList->addItems(settings.value(QSL("AllowedSchemes"), QStringList()).toStringList());
52
53 m_ui->blockedList->clear();
54 m_ui->blockedList->addItems(settings.value(QSL("BlockedSchemes"), QStringList()).toStringList());
55
56 settings.endGroup();
57}
58
60{
61 QStringList list;
62 Settings settings;
63 settings.beginGroup(QSL("Web-Browser-Settings"));
64
65 for (int i = 0; i < m_ui->allowedList->count(); ++i) {
66 list.append(m_ui->allowedList->item(i)->text().toLower());
67 }
68 list.removeDuplicates();
69 settings.setValue(QSL("AllowedSchemes"), list);
70
71 list.clear();
72 for (int i = 0; i < m_ui->blockedList->count(); ++i) {
73 list.append(m_ui->blockedList->item(i)->text().toLower());
74 }
75 list.removeDuplicates();
76 settings.setValue(QSL("BlockedSchemes"), list);
77 settings.endGroup();
78
79 QDialog::close();
80}
81
82void SchemeManager::allowedAdd()
83{
84 const QString scheme = QInputDialog::getText(this, tr("Add allowed scheme"), tr("Scheme:"));
85
86 if (scheme.isEmpty()) {
87 return;
88 }
89
90 if (m_ui->allowedList->findItems(scheme, Qt::MatchFixedString).isEmpty()) {
91 m_ui->allowedList->addItem(scheme);
92 }
93}
94
95void SchemeManager::allowedRemove()
96{
97 int currentRow = m_ui->allowedList->currentRow();
98 auto* item = m_ui->allowedList->item(currentRow);
99
100 if ((item != nullptr) && (item->isSelected())) {
101 m_ui->allowedList->takeItem(m_ui->allowedList->currentRow());
102 }
103}
104
105void SchemeManager::blockScheme()
106{
107 int currentRow = m_ui->allowedList->currentRow();
108 auto* item = m_ui->allowedList->item(currentRow);
109
110 if ((item != nullptr) && (item->isSelected())) {
111 if (m_ui->blockedList->findItems(item->text(), Qt::MatchFixedString).isEmpty()) {
112 m_ui->blockedList->addItem(item->text());
113 }
114 }
115}
116
117void SchemeManager::blockedAdd()
118{
119 const QString scheme = QInputDialog::getText(this, tr("Add blocked scheme"), tr("Scheme:"));
120
121 if (scheme.isEmpty()) {
122 return;
123 }
124
125 if (m_ui->blockedList->findItems(scheme, Qt::MatchFixedString).isEmpty()) {
126 m_ui->blockedList->addItem(scheme);
127 }
128}
129
130void SchemeManager::blockedRemove()
131{
132 int currentRow = m_ui->blockedList->currentRow();
133 auto* item = m_ui->blockedList->item(currentRow);
134
135 if ((item != nullptr) && (item->isSelected())) {
136 m_ui->blockedList->takeItem(m_ui->blockedList->currentRow());
137 }
138}
139
141{
142 delete m_ui;
143}
void accept() override
SchemeManager(QWidget *parent=nullptr)
~SchemeManager() override
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40