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

0001 /*
0002     This file is part of kdepim.
0003 
0004     SPDX-FileCopyrightText: 2004 Till Adam <adam@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "highlighter.h"
0010 
0011 #include <MessageViewer/MessagePartRenderPlugin>
0012 #include <MessageViewer/MessagePartRendererBase>
0013 #include <MessageViewer/MessagePartRendererManager>
0014 
0015 #include <MessageViewer/HtmlWriter>
0016 #include <MimeTreeParser/MessagePart>
0017 
0018 #include <KSyntaxHighlighting/Definition>
0019 #include <KSyntaxHighlighting/Repository>
0020 #include <KSyntaxHighlighting/Theme>
0021 
0022 #include <KTextTemplate/Template>
0023 
0024 #include <QGuiApplication>
0025 #include <QMimeDatabase>
0026 #include <QPalette>
0027 
0028 namespace
0029 {
0030 class Formatter : public MessageViewer::MessagePartRendererBase
0031 {
0032 public:
0033     bool render(const MimeTreeParser::MessagePartPtr &msgPart, MessageViewer::HtmlWriter *htmlWriter, MessageViewer::RenderContext *context) const override
0034     {
0035         Q_UNUSED(context)
0036         auto mp = msgPart.dynamicCast<MimeTreeParser::AttachmentMessagePart>();
0037         if (!mp || context->isHiddenHint(msgPart) || mp->text().isEmpty() || context->displayHint(msgPart) != MimeTreeParser::NoIcon) {
0038             return false;
0039         }
0040 
0041         QMimeDatabase db;
0042         auto mt = db.mimeTypeForName(QString::fromLatin1(msgPart->content()->contentType()->mimeType().toLower()));
0043         if (!mt.isValid()) {
0044             return false;
0045         }
0046         if (mt.name() != QLatin1StringView("text/plain") && !mt.allAncestors().contains(QLatin1StringView("text/plain"))) {
0047             return false;
0048         }
0049 
0050         const auto def = mRepo.definitionForFileName(mp->label());
0051         if (!def.isValid()) {
0052             return false;
0053         }
0054 
0055         auto c = MessageViewer::MessagePartRendererManager::self()->createContext();
0056         c.insert(QStringLiteral("block"), msgPart.data());
0057         c.insert(QStringLiteral("showOnlyOneMimePart"), context->showOnlyOneMimePart());
0058         c.insert(QStringLiteral("content"), QVariant::fromValue<MessageViewer::KTextTemplateCallback>([=](KTextTemplate::OutputStream *) {
0059                      Highlighter highLighter(htmlWriter->stream());
0060                      highLighter.setDefinition(def);
0061                      highLighter.setTheme(QGuiApplication::palette().color(QPalette::Base).lightness() < 128
0062                                               ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
0063                                               : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
0064                      highLighter.highlight(msgPart->text());
0065                  }));
0066 
0067         auto t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral("textmessagepart.html"));
0068         KTextTemplate::OutputStream s(htmlWriter->stream());
0069         t->render(&s, &c);
0070         return true;
0071     }
0072 
0073 private:
0074     mutable KSyntaxHighlighting::Repository mRepo;
0075 };
0076 
0077 class Plugin : public QObject, public MessageViewer::MessagePartRenderPlugin
0078 {
0079     Q_OBJECT
0080     Q_INTERFACES(MessageViewer::MessagePartRenderPlugin)
0081     Q_PLUGIN_METADATA(IID "com.kde.messageviewer.bodypartformatter" FILE "texthighlighterplugin.json")
0082 public:
0083     MessageViewer::MessagePartRendererBase *renderer(int index) override
0084     {
0085         if (index == 0) {
0086             return new Formatter();
0087         }
0088         return nullptr;
0089     }
0090 };
0091 }
0092 
0093 #include "texthighlighterplugin.moc"