File indexing completed on 2025-01-05 04:55:50

0001 /*
0002     messagebox.cpp
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-libkleo.h>
0011 
0012 #include "messagebox.h"
0013 
0014 #include "auditlogviewer.h"
0015 
0016 #include <kleo/auditlogentry.h>
0017 
0018 #include <kleo_ui_debug.h>
0019 
0020 #include <KGuiItem>
0021 #include <KLocalizedString>
0022 #include <KSharedConfig>
0023 
0024 #include <QGpgME/Job>
0025 
0026 #include <QDialog>
0027 #include <QDialogButtonBox>
0028 #include <QPushButton>
0029 
0030 #include <gpgme++/encryptionresult.h>
0031 #include <gpgme++/signingresult.h>
0032 
0033 #include <gpg-error.h>
0034 
0035 using namespace Kleo;
0036 using namespace GpgME;
0037 using namespace QGpgME;
0038 
0039 namespace
0040 {
0041 bool showAuditLogButton(const AuditLogEntry &auditLog)
0042 {
0043     if (auditLog.error().code() == GPG_ERR_NOT_IMPLEMENTED) {
0044         qCDebug(KLEO_UI_LOG) << "not showing audit log button (not supported)";
0045         return false;
0046     }
0047     if (auditLog.error().code() == GPG_ERR_NO_DATA) {
0048         qCDebug(KLEO_UI_LOG) << "not showing audit log button (GPG_ERR_NO_DATA)";
0049         return false;
0050     }
0051     if (!auditLog.error() && auditLog.text().isEmpty()) {
0052         qCDebug(KLEO_UI_LOG) << "not showing audit log button (success, but result empty)";
0053         return false;
0054     }
0055     return true;
0056 }
0057 
0058 void showMessageBox(QWidget *parent,
0059                     QMessageBox::Icon icon,
0060                     const QString &text,
0061                     const AuditLogEntry &auditLog,
0062                     const QString &title,
0063                     KMessageBox::Options options)
0064 {
0065     if (showAuditLogButton(auditLog)) {
0066         QDialog *dialog = new QDialog{parent};
0067         dialog->setWindowTitle(title);
0068         QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No, dialog);
0069         KGuiItem::assign(box->button(QDialogButtonBox::Yes), KGuiItem{i18nc("@action:button", "Show Audit Log")});
0070         KGuiItem::assign(box->button(QDialogButtonBox::No), KStandardGuiItem::ok());
0071 
0072         if (options & KMessageBox::WindowModal) {
0073             dialog->setWindowModality(Qt::WindowModal);
0074         }
0075         dialog->setModal(true);
0076 
0077         // Flag as Dangerous to make the Ok button the default button
0078         const auto choice = KMessageBox::createKMessageBox(dialog, box, icon, text, QStringList{}, QString{}, nullptr, options | KMessageBox::Dangerous);
0079         if (choice == QDialogButtonBox::Yes) {
0080             AuditLogViewer::showAuditLog(parent, auditLog);
0081         }
0082     } else {
0083         const auto dialogType = (icon == QMessageBox::Information) ? KMessageBox::Information : KMessageBox::Error;
0084         KMessageBox::messageBox(parent, dialogType, text, title, {}, {}, {}, QString{}, options);
0085     }
0086 }
0087 }
0088 
0089 void MessageBox::information(QWidget *parent, const QString &text, const Kleo::AuditLogEntry &auditLog, const QString &title, KMessageBox::Options options)
0090 {
0091     showMessageBox(parent, QMessageBox::Information, text, auditLog, title.isEmpty() ? i18nc("@title:window", "Information") : title, options);
0092 }
0093 
0094 void MessageBox::error(QWidget *parent, const QString &text, const Kleo::AuditLogEntry &auditLog, const QString &title, KMessageBox::Options options)
0095 {
0096     showMessageBox(parent, QMessageBox::Critical, text, auditLog, title.isEmpty() ? i18nc("@title:window", "Error") : title, options);
0097 }
0098 
0099 void MessageBox::auditLog(QWidget *parent, const QString &log, const QString &title)
0100 {
0101     AuditLogViewer::showAuditLog(parent, AuditLogEntry{log, Error{}}, title);
0102 }