File indexing completed on 2025-01-19 04:46:49

0001 /*
0002   SPDX-FileCopyrightText: 2016 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "pgpkeyformatter.h"
0008 #include "pgpkeymemento.h"
0009 #include "pgpkeymessagepart.h"
0010 
0011 #include <QApplication>
0012 #include <QDebug>
0013 #include <QPalette>
0014 
0015 #include <MessageViewer/HtmlWriter>
0016 #include <MimeTreeParser/MessagePart>
0017 
0018 #include <GrantleeTheme/GrantleeKi18nLocalizer>
0019 #include <GrantleeTheme/GrantleeThemeEngine>
0020 
0021 #include <KLocalizedString>
0022 
0023 using namespace MimeTreeParser::Interface;
0024 
0025 MimeTreeParser::MessagePartPtr ApplicationPGPKeyFormatter::process(MimeTreeParser::Interface::BodyPart &part) const
0026 {
0027     auto mp = new PgpKeyMessagePart(&part);
0028     auto m = dynamic_cast<PgpKeyMemento *>(mp->memento());
0029 
0030     if (!m && !mp->fingerprint().isEmpty()) {
0031         auto memento = new PgpKeyMemento();
0032         auto nodeHelper = part.nodeHelper();
0033         if (nodeHelper) {
0034             QObject::connect(memento, &PgpKeyMemento::update, nodeHelper, &MimeTreeParser::NodeHelper::update);
0035             memento->start(mp->fingerprint());
0036         } else {
0037             memento->exec(mp->fingerprint());
0038             m = memento;
0039         }
0040         mp->setMemento(memento);
0041         mp->setSearchRunning(memento->isRunning());
0042     } else if (mp->fingerprint().isEmpty()) {
0043         mp->setError(i18n("No valid key data in file."));
0044     } else if (m && m->isRunning()) {
0045         mp->setSearchRunning(m->isRunning());
0046         m = nullptr;
0047     }
0048 
0049     if (m) {
0050         mp->setSearchRunning(m->isRunning());
0051         Q_ASSERT(!m->isRunning());
0052 
0053         mp->setError(m->error());
0054         mp->setKey(m->key());
0055     }
0056     return MimeTreeParser::MessagePartPtr(mp);
0057 }
0058 
0059 bool ApplicationPGPKeyFormatter::render(const MimeTreeParser::MessagePartPtr &msgPart,
0060                                         MessageViewer::HtmlWriter *htmlWriter,
0061                                         MessageViewer::RenderContext *context) const
0062 {
0063     Q_UNUSED(context)
0064     auto mp = msgPart.dynamicCast<PgpKeyMessagePart>();
0065     if (!mp) {
0066         return false;
0067     }
0068 
0069     GrantleeTheme::Engine engine;
0070     engine.localizer()->setApplicationDomain(QByteArrayLiteral("messageviewer_application_gnupgwks_plugin"));
0071 
0072     auto loader = QSharedPointer<KTextTemplate::FileSystemTemplateLoader>::create();
0073     loader->setTemplateDirs({QStringLiteral(":/")});
0074     engine.addTemplateLoader(loader);
0075     KTextTemplate::Template tpl = engine.loadByName(QStringLiteral("pgpkeymessagepart.html"));
0076     if (tpl->error()) {
0077         qWarning() << tpl->errorString();
0078     }
0079     KTextTemplate::Context ctx;
0080     ctx.setLocalizer(engine.localizer());
0081 
0082     QObject block;
0083 
0084     block.setProperty("showKeyDetails", context->showSignatureDetails());
0085     block.setProperty("error", mp->error());
0086     block.setProperty("importUrl", QString(mp->makeLink(QStringLiteral("pgpkey")) + QStringLiteral("?action=import")));
0087     block.setProperty("searchRunning", mp->searchRunning());
0088     const auto key = mp->key();
0089     block.setProperty("uid", mp->userID());
0090     block.setProperty("fingerprint", mp->fingerprint());
0091     QLocale l;
0092     block.setProperty("created", l.toString(mp->keyDate(), QLocale::ShortFormat));
0093     if (!key.isNull()) {
0094         block.setProperty("keyUrl", QStringLiteral("kmail:showCertificate#GpgME ### gpgme ### %1").arg(QString::fromLatin1(key.keyID())));
0095     }
0096 
0097     QObject style;
0098     const auto bgColor = QApplication::palette().color(QPalette::Base);
0099     if (bgColor.value() < 128) { // HSV value (brightness): 0 = darkest, 255 = brightest
0100         style.setProperty("borderColor", QColor(Qt::white).name());
0101         style.setProperty("frameTextColor", QColor(Qt::black).name());
0102     } else {
0103         style.setProperty("borderColor", QColor(Qt::black).name());
0104         style.setProperty("frameTextColor", QColor(Qt::white).name());
0105     }
0106 
0107     ctx.insert(QStringLiteral("block"), &block);
0108     ctx.insert(QStringLiteral("style"), &style);
0109     KTextTemplate::OutputStream s(htmlWriter->stream());
0110     tpl->render(&s, &ctx);
0111     return true;
0112 }