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

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2023 Javier Llorente <javier@opensuse.org>
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 #include "certificatemanager.h"
0019 #include "ui_certificatemanager.h"
0020 #include "mainapplication.h"
0021 #include "networkmanager.h"
0022 #include "settings.h"
0023 
0024 #include <QFormLayout>
0025 #include <QLineEdit>
0026 #include <QLabel>
0027 #include <QMessageBox>
0028 
0029 CertificateManager::CertificateManager(QWidget* parent)
0030     : QDialog(parent)
0031     , ui(new Ui::CertificateManager)
0032 {
0033     setAttribute(Qt::WA_DeleteOnClose);
0034 
0035     ui->setupUi(this);
0036     ui->listWidget->setLayoutDirection(Qt::LeftToRight);
0037 
0038     Settings settings;
0039     settings.beginGroup(QSL("Web-Browser-Settings"));
0040     m_ignoredSslHosts = settings.value(QSL("IgnoredSslHosts"), QStringList()).toStringList();
0041     settings.endGroup();
0042     ui->listWidget->addItems(m_ignoredSslHosts);
0043 
0044     connect(ui->add, &QAbstractButton::clicked, this, &CertificateManager::addException);
0045     connect(ui->remove, &QAbstractButton::clicked, this, &CertificateManager::removeException);
0046 }
0047 
0048 void CertificateManager::addException()
0049 {    
0050     auto *dialog = new QDialog(this);
0051     auto *layout = new QFormLayout(dialog);
0052     auto *lineEdit = new QLineEdit(dialog);
0053 
0054     auto *buttonBox = new QDialogButtonBox(dialog);
0055     buttonBox->addButton(QDialogButtonBox::Ok);
0056     buttonBox->addButton(QDialogButtonBox::Cancel);
0057 
0058     connect(buttonBox, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
0059     connect(buttonBox, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
0060 
0061     layout->addRow(new QLabel(tr("Host: ")), lineEdit);
0062     layout->addRow(buttonBox);
0063     lineEdit->setFocus();
0064 
0065     dialog->setWindowTitle(tr("Add certificate exception"));
0066     dialog->setMinimumSize(400, 100);
0067     dialog->setMaximumHeight(100);
0068     
0069     if (dialog->exec()) {
0070         QString host = lineEdit->text();
0071         if (m_ignoredSslHosts.contains(host)) {
0072             QMessageBox msgBox(this);
0073             msgBox.setIcon(QMessageBox::Warning);
0074             msgBox.setText(tr("Host %1 already in the list").arg(host));
0075             if (msgBox.exec()) {
0076                 addException();
0077             }
0078             return;
0079         }
0080         if (host.isEmpty()) {
0081             QMessageBox msgBox(this);
0082             msgBox.setIcon(QMessageBox::Warning);
0083             msgBox.setText(tr("Empty host"));
0084             if (msgBox.exec()) {
0085                 addException();
0086             }
0087             return;
0088         }
0089         
0090         m_ignoredSslHosts.append(host);
0091         ui->listWidget->addItem(host);
0092     }
0093 }
0094 
0095 void CertificateManager::removeException()
0096 {
0097     m_ignoredSslHosts.removeOne(ui->listWidget->currentItem()->text());
0098     delete ui->listWidget->currentItem();
0099 }
0100 
0101 void CertificateManager::accept()
0102 {
0103     Settings settings;
0104     settings.beginGroup(QSL("Web-Browser-Settings"));
0105     settings.setValue(QSL("IgnoredSslHosts"), m_ignoredSslHosts);
0106     settings.endGroup();
0107 
0108     mApp->networkManager()->loadSettings();
0109 
0110     QDialog::close();
0111 }
0112 
0113 CertificateManager::~CertificateManager()
0114 {
0115     delete ui;
0116 }