File indexing completed on 2024-12-22 05:05:20

0001 // SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0002 // SPDX-FileCopyrightText: 2023 g10 Code GmbH
0003 // SPDX-FileContributor: Carl Schwan <carl.schwan@gnupg.com>
0004 // SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 #include "utils.h"
0007 
0008 #include <KLocalizedString>
0009 #include <Libkleo/Formatting>
0010 #include <libkleo/dn.h>
0011 
0012 using namespace MimeTreeParser;
0013 
0014 KMime::Content *MimeTreeParser::findTypeInDirectChildren(KMime::Content *content, const QByteArray &mimeType)
0015 {
0016     const auto contents = content->contents();
0017     for (const auto child : contents) {
0018         if ((!child->contentType()->isEmpty()) && (mimeType == child->contentType()->mimeType())) {
0019             return child;
0020         }
0021     }
0022     return nullptr;
0023 }
0024 
0025 QString MimeTreeParser::decryptRecipientsToHtml(const std::vector<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>> &recipients,
0026                                                 const QGpgME::Protocol *cryptoProto)
0027 {
0028     QString text = QStringLiteral("<ul>");
0029     for (const auto &recipientIt : recipients) {
0030         const auto recipient = recipientIt.first;
0031         const auto key = recipientIt.second;
0032         if (key.keyID()) {
0033             QString displayName = QString::fromLatin1(key.userID(0).id());
0034             if (cryptoProto == QGpgME::smime()) {
0035                 Kleo::DN dn(displayName);
0036                 displayName = dnToDisplayName(dn);
0037             }
0038             displayName = displayName.toHtmlEscaped();
0039             const auto link = QStringLiteral("messageviewer:showCertificate#%1 ### %2 ### %3")
0040                                   .arg(cryptoProto->displayName(), cryptoProto->name(), QString::fromLatin1(key.keyID()));
0041             text += QStringLiteral("<li>%1 (<a href=\"%2\">%3</a>)</li>").arg(displayName, link, Kleo::Formatting::prettyID(key.keyID()));
0042         } else {
0043             const auto link = QStringLiteral("messageviewer:showCertificate#%1 ### %2 ### %3")
0044                                   .arg(cryptoProto->displayName(), cryptoProto->name(), QString::fromLatin1(recipient.keyID()));
0045             text +=
0046                 QStringLiteral("<li>%1 (<a href=\"%2\">%3</a>)</li>").arg(i18nc("@info", "Unknown Key"), link, Kleo::Formatting::prettyID(recipient.keyID()));
0047         }
0048     }
0049     text += QStringLiteral("</ul>");
0050     return text;
0051 }
0052 
0053 QString MimeTreeParser::dnToDisplayName(const Kleo::DN &dn)
0054 {
0055     QString displayName = dn[QStringLiteral("CN")];
0056     if (displayName.isEmpty()) {
0057         // In case there is no CN, put the full DN as display name
0058         displayName = dn.prettyDN();
0059     } else if (!dn[QStringLiteral("O")].isEmpty()) {
0060         displayName += i18nc("Separator", " - ") + dn[QStringLiteral("O")];
0061     }
0062     return displayName;
0063 }