diff --git a/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.cpp b/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.cpp --- a/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.cpp +++ b/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.cpp @@ -32,21 +32,25 @@ #include "diffhighlighter.h" #include +#include +#include #include #include +#include #include #include namespace { // TODO: Show filename header to make it possible to save the patch. // FIXME: The box should only be as wide as necessary. -class Formatter : public MimeTreeParser::Interface::BodyPartFormatter +class Formatter : public MimeTreeParser::Interface::BodyPartFormatter, public MessageViewer::MessagePartRendererBase { public: Result format(MimeTreeParser::Interface::BodyPart *bodyPart, MimeTreeParser::HtmlWriter *writer) const override { + return Failed; if (!writer) { return Ok; } @@ -68,14 +72,35 @@ return Ok; } - // unhide the overload with three arguments - using MimeTreeParser::Interface::BodyPartFormatter::format; + bool render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, MessageViewer::RenderContext *context) const override + { + auto mp = msgPart.dynamicCast(); + if (!mp || mp->isHidden()) // TODO check for inline vs. icon + return false; + + const bool diffMimeType = msgPart->content()->contentType()->mimeType() == "text/x-patch" + || msgPart->content()->contentType()->mimeType() == "text/x-diff"; + const bool diffFileName = msgPart->content()->contentType()->name().endsWith(QLatin1String(".diff")) + || msgPart->content()->contentType()->name().endsWith(QLatin1String(".patch")); + if (!diffMimeType && !diffFileName) + return false; + + // TODO attachment block + + DiffHighlighter highLighter; + highLighter.highlightDiff(msgPart->text()); + const QString html = highLighter.outputDiff(); + htmlWriter->write(html); + + return true; + } }; -class Plugin : public QObject, public MimeTreeParser::Interface::BodyPartFormatterPlugin +class Plugin : public QObject, public MimeTreeParser::Interface::BodyPartFormatterPlugin, public MessageViewer::MessagePartRenderPlugin { Q_OBJECT Q_INTERFACES(MimeTreeParser::Interface::BodyPartFormatterPlugin) + Q_INTERFACES(MessageViewer::MessagePartRenderPlugin) Q_PLUGIN_METADATA(IID "com.kde.messageviewer.bodypartformatter" FILE "text_xdiff.json") public: const MimeTreeParser::Interface::BodyPartFormatter *bodyPartFormatter(int idx) const override @@ -85,6 +110,13 @@ } return nullptr; } + + MessageViewer::MessagePartRendererBase* renderer(int index) override + { + if (index == 0) + return new Formatter(); + return nullptr; + } }; } diff --git a/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.json b/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.json --- a/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.json +++ b/plugins/messageviewer/bodypartformatter/xdiff/text_xdiff.json @@ -1,5 +1,4 @@ -{ "formatter": - [ - { "mimetype": "text/x-patch" } - ] +{ + "formatter": [ { "mimetype": "text/x-patch" } ], + "renderer": [ { "type": "MimeTreeParser::AttachmentMessagePart" } ] }