File indexing completed on 2024-05-19 05:01:22

0001 /*
0002  * This file is part of the KDE project.
0003  *
0004  * Copyright 2021  Stefano Crocco <posta@stefanocrocco.it>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License or (at your option) version 3 or any later version
0010  * accepted by the membership of KDE e.V. (or its successor approved
0011  * by the membership of KDE e.V.), which shall act as a proxy
0012  * defined in Section 14 of version 3 of the license.
0013  *
0014  * This program is distributed in the hope that it will be useful,
0015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  * GNU General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU General Public License
0020  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  */
0022 
0023 #include "webenginepartcertificateerrordlg.h"
0024 #include "ui_webenginepartcertificateerrordlg.h"
0025 
0026 
0027 #include <KSslCertificateBox>
0028 
0029 #include <QAbstractButton>
0030 #include <QPushButton>
0031 
0032 using namespace KonqWebEnginePart;
0033 
0034 WebEnginePartCertificateErrorDlg::WebEnginePartCertificateErrorDlg(const QWebEngineCertificateError &error, WebEnginePage *page, QWidget* parent):
0035     QDialog(parent),
0036     m_ui(new Ui::WebEnginePartCertificateErrorDlg), m_error(error), m_choice(UserChoice::DontIgnoreError)
0037 {
0038     m_ui->setupUi(this);
0039     connect(m_ui->buttons, &QDialogButtonBox::clicked, this, &WebEnginePartCertificateErrorDlg::updateUserChoice);
0040     connect(m_ui->showDetails, &QCheckBox::toggled, m_ui->details, &QWidget::setVisible);
0041     connect(m_ui->certificateChain, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &WebEnginePartCertificateErrorDlg::displayCertificate);
0042 
0043     m_ui->buttons->button(QDialogButtonBox::No)->setDefault(true);
0044     m_ui->buttons->button(QDialogButtonBox::Yes)->setText(i18nc("Ignore the certificate error for this URL only for now", "Yes, &once"));
0045     m_ui->buttons->button(QDialogButtonBox::YesToAll)->setText(i18nc("Ignore the certificate error for this URL now and in the future", "Yes, &forever"));
0046     m_ui->details->hide();
0047 
0048     //According to the documentation, QWebEngineCertificateError::description() returns a localized string
0049 #if QT_VERSION_MAJOR < 6
0050     QString translatedDesc = m_error.errorDescription().toUtf8();
0051 #else
0052     QString translatedDesc = m_error.description().toUtf8();
0053 #endif
0054     QString text = i18n("<p>The server <tt>%1</tt> failed the authenticity check. The error is:</p><p><tt>%2</tt></p>Do you want to ignore this error?",
0055                         m_error.url().host(), translatedDesc);
0056     m_ui->label->setText(text);
0057     for (const QSslCertificate &cert : m_error.certificateChain()) {
0058         m_ui->certificateChain->addItem(cert.subjectDisplayName());
0059     }
0060     setWindowTitle(i18nc("title of a dialog asking what to do about a SSL certificate error", "Certificate error"));
0061 }
0062 
0063 WebEnginePartCertificateErrorDlg::~WebEnginePartCertificateErrorDlg()
0064 {
0065 }
0066 
0067 QWebEngineCertificateError WebEnginePartCertificateErrorDlg::certificateError() const
0068 {
0069     return m_error;
0070 }
0071 
0072 WebEnginePartCertificateErrorDlg::UserChoice WebEnginePartCertificateErrorDlg::userChoice() const
0073 {
0074     return m_choice;
0075 }
0076 
0077 void WebEnginePartCertificateErrorDlg::displayCertificate(int idx)
0078 {
0079     m_ui->subjectData->setCertificate(m_error.certificateChain().at(idx), KSslCertificateBox::Subject);;
0080     m_ui->issuerData->setCertificate(m_error.certificateChain().at(idx), KSslCertificateBox::Issuer);;
0081 }
0082 
0083 void WebEnginePartCertificateErrorDlg::updateUserChoice(QAbstractButton* btn)
0084 {
0085     QDialogButtonBox::StandardButton code = m_ui->buttons->standardButton(btn);
0086     switch (code) {
0087         case QDialogButtonBox::Yes:
0088             m_choice = UserChoice::IgnoreErrorOnce;
0089             break;
0090         case QDialogButtonBox::YesToAll:
0091             m_choice = UserChoice::IgnoreErrorForever;
0092             break;
0093         default:
0094             m_choice = UserChoice::DontIgnoreError;
0095     }
0096 }