File indexing completed on 2024-05-12 04:58:20

0001 /* ============================================================
0002  * Falkon - Qt web browser
0003  * Copyright (C) 2023 Juraj Oravec <jurajoravec@mailo.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  * ============================================================ */
0018 
0019 #include "ui_schememanager.h"
0020 #include "schememanager.h"
0021 #include "qzsettings.h"
0022 
0023 #include <QInputDialog>
0024 #include <QMessageBox>
0025 
0026 SchemeManager::SchemeManager(QWidget* parent)
0027     : QDialog(parent)
0028     , m_ui(new Ui::SchemeManager)
0029 {
0030     setAttribute(Qt::WA_DeleteOnClose);
0031 
0032     m_ui->setupUi(this);
0033 
0034     reset();
0035 
0036     connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &SchemeManager::accept);
0037     connect(m_ui->buttonBox->button(QDialogButtonBox::Reset), &QAbstractButton::clicked, this, &SchemeManager::reset);
0038     connect(m_ui->blockButton, &QAbstractButton::clicked, this, &SchemeManager::blockScheme);
0039     connect(m_ui->allowedAddButton, &QAbstractButton::clicked, this, &SchemeManager::allowedAdd);
0040     connect(m_ui->allowedRemoveButton, &QAbstractButton::clicked, this, &SchemeManager::allowedRemove);
0041     connect(m_ui->blockedAddButton, &QAbstractButton::clicked, this, &SchemeManager::blockedAdd);
0042     connect(m_ui->blockedRemoveButton, &QAbstractButton::clicked, this, &SchemeManager::blockedRemove);
0043 }
0044 
0045 void SchemeManager::reset()
0046 {
0047     Settings settings;
0048     settings.beginGroup(QSL("Web-Browser-Settings"));
0049 
0050     m_ui->allowedList->clear();
0051     m_ui->allowedList->addItems(settings.value(QSL("AllowedSchemes"), QStringList()).toStringList());
0052 
0053     m_ui->blockedList->clear();
0054     m_ui->blockedList->addItems(settings.value(QSL("BlockedSchemes"), QStringList()).toStringList());
0055 
0056     settings.endGroup();
0057 }
0058 
0059 void SchemeManager::accept()
0060 {
0061     QStringList list;
0062     Settings settings;
0063     settings.beginGroup(QSL("Web-Browser-Settings"));
0064 
0065     for (int i = 0; i < m_ui->allowedList->count(); ++i) {
0066         list.append(m_ui->allowedList->item(i)->text().toLower());
0067     }
0068     list.removeDuplicates();
0069     settings.setValue(QSL("AllowedSchemes"), list);
0070 
0071     list.clear();
0072     for (int i = 0; i < m_ui->blockedList->count(); ++i) {
0073         list.append(m_ui->blockedList->item(i)->text().toLower());
0074     }
0075     list.removeDuplicates();
0076     settings.setValue(QSL("BlockedSchemes"), list);
0077     settings.endGroup();
0078 
0079     QDialog::close();
0080 }
0081 
0082 void SchemeManager::allowedAdd()
0083 {
0084     const QString scheme = QInputDialog::getText(this, tr("Add allowed scheme"), tr("Scheme:"));
0085 
0086     if (scheme.isEmpty()) {
0087         return;
0088     }
0089 
0090     if (m_ui->allowedList->findItems(scheme, Qt::MatchFixedString).isEmpty()) {
0091         m_ui->allowedList->addItem(scheme);
0092     }
0093 }
0094 
0095 void SchemeManager::allowedRemove()
0096 {
0097     int currentRow = m_ui->allowedList->currentRow();
0098     auto* item = m_ui->allowedList->item(currentRow);
0099 
0100     if ((item != nullptr) && (item->isSelected())) {
0101         m_ui->allowedList->takeItem(m_ui->allowedList->currentRow());
0102     }
0103 }
0104 
0105 void SchemeManager::blockScheme()
0106 {
0107     int currentRow = m_ui->allowedList->currentRow();
0108     auto* item = m_ui->allowedList->item(currentRow);
0109 
0110     if ((item != nullptr) && (item->isSelected())) {
0111         if (m_ui->blockedList->findItems(item->text(), Qt::MatchFixedString).isEmpty()) {
0112             m_ui->blockedList->addItem(item->text());
0113         }
0114     }
0115 }
0116 
0117 void SchemeManager::blockedAdd()
0118 {
0119     const QString scheme = QInputDialog::getText(this, tr("Add blocked scheme"), tr("Scheme:"));
0120 
0121     if (scheme.isEmpty()) {
0122         return;
0123     }
0124 
0125     if (m_ui->blockedList->findItems(scheme, Qt::MatchFixedString).isEmpty()) {
0126         m_ui->blockedList->addItem(scheme);
0127     }
0128 }
0129 
0130 void SchemeManager::blockedRemove()
0131 {
0132     int currentRow = m_ui->blockedList->currentRow();
0133     auto* item = m_ui->blockedList->item(currentRow);
0134 
0135     if ((item != nullptr) && (item->isSelected())) {
0136         m_ui->blockedList->takeItem(m_ui->blockedList->currentRow());
0137     }
0138 }
0139 
0140 SchemeManager::~SchemeManager()
0141 {
0142     delete m_ui;
0143 }