File indexing completed on 2024-11-24 04:49:35

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     kleo/auditlogentry.cpp
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006     SPDX-FileCopyrightText: 2022 g10 Code GmbH
0007     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include <config-libkleo.h>
0013 
0014 #include "auditlogentry.h"
0015 
0016 #include <libkleo/formatting.h>
0017 #include <libkleo_debug.h>
0018 
0019 #include <QGpgME/Job>
0020 
0021 #include <QUrl>
0022 #include <QUrlQuery>
0023 
0024 #include <gpgme++/error.h>
0025 
0026 using namespace Kleo;
0027 
0028 class AuditLogEntry::Private
0029 {
0030 public:
0031     QString text;
0032     GpgME::Error error;
0033 };
0034 
0035 AuditLogEntry::AuditLogEntry()
0036     : AuditLogEntry{QString{}, GpgME::Error{}}
0037 {
0038 }
0039 
0040 AuditLogEntry::AuditLogEntry(const GpgME::Error &error)
0041     : AuditLogEntry{QString{}, error}
0042 {
0043 }
0044 
0045 AuditLogEntry::AuditLogEntry(const QString &text, const GpgME::Error &error)
0046     : d{new Private{text, error}}
0047 {
0048 }
0049 
0050 AuditLogEntry::~AuditLogEntry() = default;
0051 
0052 AuditLogEntry::AuditLogEntry(const AuditLogEntry &other)
0053     : d{new Private{*other.d}}
0054 {
0055 }
0056 
0057 AuditLogEntry &AuditLogEntry::operator=(const AuditLogEntry &other)
0058 {
0059     *d = *other.d;
0060     return *this;
0061 }
0062 
0063 AuditLogEntry::AuditLogEntry(AuditLogEntry &&other) = default;
0064 AuditLogEntry &AuditLogEntry::operator=(AuditLogEntry &&other) = default;
0065 
0066 AuditLogEntry AuditLogEntry::fromJob(const QGpgME::Job *job)
0067 {
0068     if (job) {
0069         return AuditLogEntry{job->auditLogAsHtml(), job->auditLogError()};
0070     } else {
0071         return AuditLogEntry{};
0072     }
0073 }
0074 
0075 GpgME::Error AuditLogEntry::error() const
0076 {
0077     return d->error;
0078 }
0079 
0080 QString AuditLogEntry::text() const
0081 {
0082     return d->text;
0083 }
0084 
0085 QUrl AuditLogEntry::asUrl(const QUrl &urlTemplate) const
0086 {
0087     // more or less the same as
0088     // kmail/objecttreeparser.cpp:makeShowAuditLogLink(), so any bug
0089     // fixed here equally applies there:
0090     if (const int code = d->error.code()) {
0091         if (code == GPG_ERR_NOT_IMPLEMENTED) {
0092             qCDebug(LIBKLEO_LOG) << "not showing link (not implemented)";
0093         } else if (code == GPG_ERR_NO_DATA) {
0094             qCDebug(LIBKLEO_LOG) << "not showing link (not available)";
0095         } else {
0096             qCDebug(LIBKLEO_LOG) << "Error Retrieving Audit Log:" << Formatting::errorAsString(d->error);
0097         }
0098         return {};
0099     }
0100 
0101     if (d->text.isEmpty()) {
0102         return {};
0103     }
0104 
0105     QUrl url = urlTemplate;
0106     QUrlQuery urlQuery{url};
0107     urlQuery.addQueryItem(QStringLiteral("log"), d->text);
0108     url.setQuery(urlQuery);
0109     return url;
0110 }
0111 
0112 QDebug operator<<(QDebug debug, const AuditLogEntry &auditLog)
0113 {
0114     const bool oldSetting = debug.autoInsertSpaces();
0115     debug.nospace() << "AuditLogEntry(" << Formatting::errorAsString(auditLog.error()) << ", " << auditLog.text() << ')';
0116     debug.setAutoInsertSpaces(oldSetting);
0117     return debug.maybeSpace();
0118 }