File indexing completed on 2024-05-05 05:44:50

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
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 2 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, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "ssltrustprompt.h"
0021 #include "ui_ssltrustprompt.h"
0022 
0023 #include <KLocalizedString>
0024 #include <QPointer>
0025 #include <QPushButton>
0026 
0027 SslTrustPrompt::SslTrustPrompt(const QString &host, const QString &text, QWidget *parent)
0028     : KSvnDialog(QLatin1String("trustssldlg"), parent)
0029     , m_ui(new Ui::SslTrustPrompt)
0030 {
0031     m_ui->setupUi(this);
0032     setDefaultButton(m_ui->buttonBox->button(QDialogButtonBox::No));
0033     m_ui->buttonBox->button(QDialogButtonBox::Yes)->setText(i18n("Accept permanently"));
0034     m_ui->buttonBox->button(QDialogButtonBox::No)->setText(i18n("Accept temporarily"));
0035     m_ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(i18n("Reject"));
0036     connect(m_ui->buttonBox->button(QDialogButtonBox::Yes), &QPushButton::clicked, this, [this]() {
0037         done(QDialogButtonBox::Yes);
0038     });
0039     connect(m_ui->buttonBox->button(QDialogButtonBox::No), &QPushButton::clicked, this, [this]() {
0040         done(QDialogButtonBox::No);
0041     });
0042     connect(m_ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, [this]() {
0043         done(QDialogButtonBox::Cancel);
0044     });
0045 
0046     m_ui->m_MainLabel->setText(QLatin1String("<p align=\"center\"><b>") + i18n("Error validating server certificate for '%1'", host)
0047                                + QLatin1String("</b></p>"));
0048     m_ui->m_ContentText->setText(text);
0049 }
0050 
0051 SslTrustPrompt::~SslTrustPrompt()
0052 {
0053     delete m_ui;
0054 }
0055 
0056 bool SslTrustPrompt::sslTrust(const QString &host,
0057                               const QString &fingerprint,
0058                               const QString &validFrom,
0059                               const QString &validUntil,
0060                               const QString &issuerName,
0061                               const QString &realm,
0062                               const QStringList &reasons,
0063                               bool *ok,
0064                               bool *saveit)
0065 {
0066     static QLatin1String rb("<tr><td>");
0067     static QLatin1String rs("</td><td>");
0068     static QLatin1String re("</td></tr>");
0069     QString text = QStringLiteral("<html><body>");
0070     if (!reasons.isEmpty()) {
0071         text += QStringLiteral("<p align=\"center\"><h2>") + i18n("Failure reasons") + QStringLiteral("</h2><hline>");
0072         for (const QString &reason : reasons) {
0073             text += reason + QStringLiteral("<br/><hline>");
0074         }
0075         text += QStringLiteral("</p>");
0076     }
0077 
0078     text += QStringLiteral("<p align=\"center\"><table>");
0079     text += rb + i18n("Realm") + rs + realm + re;
0080     text += rb + i18n("Host") + rs + host + re;
0081     text += rb + i18n("Valid from") + rs + validFrom + re;
0082     text += rb + i18n("Valid until") + rs + validUntil + re;
0083     text += rb + i18n("Issuer name") + rs + issuerName + re;
0084     text += rb + i18n("Fingerprint") + rs + fingerprint + re;
0085     text += QStringLiteral("</table></p></body></html>");
0086 
0087     QPointer<SslTrustPrompt> dlg(new SslTrustPrompt(host, text, QApplication::activeModalWidget()));
0088     int i = dlg->exec();
0089     delete dlg;
0090 
0091     *saveit = i == QDialogButtonBox::Yes;
0092     *ok = (i == QDialogButtonBox::Yes || i == QDialogButtonBox::No);
0093     return *ok;
0094 }
0095 
0096 #include "moc_ssltrustprompt.cpp"