Commit 59a75d7b authored by Sandro Knauß's avatar Sandro Knauß

Move attachment handling to renderer.

Summary: AttachmentStrategy is a part of the Renderer and not of the Parsing.

Test Plan: run tests

Reviewers: vkrause

Subscribers: #kde_pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D8570
parent ece5c76c
......@@ -33,6 +33,7 @@
#include "viewer/csshelperbase.h"
#include "messagepartrenderermanager.h"
#include <MimeTreeParser/AttachmentStrategy>
#include <MimeTreeParser/BufferedHtmlWriter>
#include <MimeTreeParser/MessagePart>
#include <MimeTreeParser/ObjectTreeParser>
......@@ -308,10 +309,11 @@ QString processHtml(const QString &htmlSource, QString &extraHead)
return s;
}
DefaultRendererPrivate::DefaultRendererPrivate(const MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, HtmlWriter *writer, const MessagePartRendererFactory *rendererFactory)
DefaultRendererPrivate::DefaultRendererPrivate(const MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, HtmlWriter *writer, const MessagePartRendererFactory *rendererFactory, bool showOnlyOneMimePart)
: mMsgPart(msgPart)
, mCSSHelper(cssHelper)
, mRendererFactory(rendererFactory)
, mShowOnlyOneMimePart(showOnlyOneMimePart)
{
renderFactory(mMsgPart, writer);
}
......@@ -901,8 +903,126 @@ void DefaultRendererPrivate::renderFactory(const MessagePart::Ptr &msgPart, Html
}
}
DefaultRenderer::DefaultRenderer(const MimeTreeParser::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, MimeTreeParser::HtmlWriter *writer)
: d(new MimeTreeParser::DefaultRendererPrivate(msgPart, cssHelper, writer, MessagePartRendererFactory::instance()))
bool DefaultRendererPrivate::isHiddenHint(const MimeTreeParser::MessagePart::Ptr &msgPart)
{
auto mp = msgPart.dynamicCast<MimeTreeParser::AttachmentMessagePart>();
auto content = msgPart->content();
if (!mp) {
auto _mp = msgPart.dynamicCast<MimeTreeParser::TextMessagePart>();
if (_mp) {
return msgPart->nodeHelper()->isNodeDisplayedHidden(content);
} else {
return false;
}
}
if (mShowOnlyOneMimePart) {
return false;
}
const AttachmentStrategy *const as = msgPart->source()->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(content) == AttachmentStrategy::None);
auto preferredMode = source()->preferredMode();
bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
QByteArray mediaType("text");
if (content->contentType(false) && !content->contentType()->mediaType().isEmpty()
&& !content->contentType()->subType().isEmpty()) {
mediaType = content->contentType()->mediaType();
}
const bool isTextPart = (mediaType == QByteArrayLiteral("text"));
bool defaultAsIcon = true;
if (!mp->neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(content) == AttachmentStrategy::AsIcon;
}
}
// neither image nor text -> show as icon
if (!mp->isImage() && !isTextPart) {
defaultAsIcon = true;
}
bool hidden(false);
if (isTextPart) {
hidden = defaultHidden;
} else {
if (mp->isImage() && isHtmlPreferred
&& content->parent() && content->parent()->contentType(false)->subType() == "related") {
hidden = true;
} else {
hidden = defaultHidden && content->parent();
hidden |= defaultAsIcon && defaultHidden;
}
}
msgPart->nodeHelper()->setNodeDisplayedHidden(content, hidden);
return hidden;
}
MimeTreeParser::IconType MimeTreeParser::DefaultRendererPrivate::displayHint(const MimeTreeParser::MessagePart::Ptr& msgPart)
{
auto mp = msgPart.dynamicCast<MimeTreeParser::AttachmentMessagePart>();
auto content = msgPart->content();
if (!mp) {
return MimeTreeParser::IconType::NoIcon;
}
const AttachmentStrategy *const as = msgPart->source()->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(content) == AttachmentStrategy::None);
const bool showOnlyOneMimePart(mShowOnlyOneMimePart);
auto preferredMode = source()->preferredMode();
bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
QByteArray mediaType("text");
if (content->contentType(false) && !content->contentType()->mediaType().isEmpty()
&& !content->contentType()->subType().isEmpty()) {
mediaType = content->contentType()->mediaType();
}
const bool isTextPart = (mediaType == QByteArrayLiteral("text"));
bool defaultAsIcon = true;
if (!mp->neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(content) == AttachmentStrategy::AsIcon;
}
}
if (mp->isImage() && showOnlyOneMimePart && !mp->neverDisplayInline()) {
defaultAsIcon = false;
}
// neither image nor text -> show as icon
if (!mp->isImage() && !isTextPart) {
defaultAsIcon = true;
}
if (isTextPart) {
if (as && as->defaultDisplay(content) != AttachmentStrategy::Inline) {
return MimeTreeParser::IconExternal;
}
return MimeTreeParser::NoIcon;
} else {
if (mp->isImage() && isHtmlPreferred
&& content->parent() && content->parent()->contentType(false)->subType() == "related") {
return MimeTreeParser::IconInline;
}
if (defaultHidden && !showOnlyOneMimePart && content->parent()) {
return MimeTreeParser::IconInline;
}
if (defaultAsIcon) {
return MimeTreeParser::IconExternal;
} else if (mp->isImage()) {
return MimeTreeParser::IconInline;
}
}
return MimeTreeParser::NoIcon;
}
DefaultRenderer::DefaultRenderer(const MimeTreeParser::MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, MimeTreeParser::HtmlWriter *writer, bool showOnlyOneMimePart)
: d(new MimeTreeParser::DefaultRendererPrivate(msgPart, cssHelper, writer, MessagePartRendererFactory::instance(), showOnlyOneMimePart))
{
}
......
......@@ -35,7 +35,7 @@ class CSSHelperBase;
class DefaultRenderer
{
public:
DefaultRenderer(const MimeTreeParser::MessagePartPtr &msgPart, CSSHelperBase *cssHelder, MimeTreeParser::HtmlWriter *writer);
DefaultRenderer(const MimeTreeParser::MessagePartPtr &msgPart, CSSHelperBase *cssHelder, MimeTreeParser::HtmlWriter *writer, bool showOnlyOneMimePart);
~DefaultRenderer();
private:
......
......@@ -37,7 +37,7 @@ namespace MimeTreeParser {
class DefaultRendererPrivate : public MessageViewer::RenderContext
{
public:
DefaultRendererPrivate(const MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, HtmlWriter *writer, const MessagePartRendererFactory *rendererFactory);
DefaultRendererPrivate(const MessagePart::Ptr &msgPart, CSSHelperBase *cssHelper, HtmlWriter *writer, const MessagePartRendererFactory *rendererFactory, bool showOnlyOneMimePart);
~DefaultRendererPrivate();
CSSHelperBase *cssHelper() const override;
......@@ -60,6 +60,10 @@ public:
using RenderContext::renderWithFactory;
void renderFactory(const MessagePart::Ptr &msgPart, HtmlWriter *htmlWriter);
bool isHiddenHint(const MimeTreeParser::MessagePart::Ptr &msgPart) override;
MimeTreeParser::IconType displayHint(const MimeTreeParser::MessagePart::Ptr &msgPart) override;
bool mShowOnlyOneMimePart;
MessagePart::Ptr mMsgPart;
CSSHelperBase *mCSSHelper = nullptr;
const MessageViewer::MessagePartRendererFactory *mRendererFactory = nullptr;
......
......@@ -65,6 +65,9 @@ public:
virtual void renderSubParts(const MimeTreeParser::MessagePart::Ptr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter) = 0;
virtual bool isHiddenHint(const MimeTreeParser::MessagePart::Ptr &msgPart) = 0;
virtual MimeTreeParser::IconType displayHint(const MimeTreeParser::MessagePart::Ptr &msgPart) = 0;
protected:
virtual bool renderWithFactory(const QMetaObject *mo, const MimeTreeParser::MessagePart::Ptr &msgPart, MimeTreeParser::HtmlWriter *writer) = 0;
};
......
......@@ -50,11 +50,11 @@ bool AttachmentMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr
}
KMime::Content *node = mp->content();
if (mp->isHidden()) {
if (context->isHiddenHint(msgPart)) {
return true;
}
const auto tmpAsIcon = mp->asIcon();
const auto tmpAsIcon = context->displayHint(msgPart);
if (tmpAsIcon == MimeTreeParser::NoIcon) {
return context->renderWithFactory<MimeTreeParser::TextMessagePart>(mp, htmlWriter);
......
......@@ -48,7 +48,7 @@ bool TextMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr &msgPa
return false;
}
if (mp->isHidden()) {
if (context->isHiddenHint(msgPart)) {
return true;
}
......
......@@ -140,7 +140,7 @@ bool EmptySource::isPrinting() const
return false;
}
void EmptySource::render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter)
void EmptySource::render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, bool showOnlyOneMimePart)
{
DefaultRenderer(msgPart, cssHelper(), htmlWriter);
DefaultRenderer(msgPart, cssHelper(), htmlWriter, showOnlyOneMimePart);
}
......@@ -51,7 +51,7 @@ public:
bool showEmoticons() const override;
bool showExpandQuotesMark() const override;
const MimeTreeParser::BodyPartFormatterFactory *bodyPartFormatterFactory() override;
void render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter) override;
void render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, bool showOnlyOneMimePart) override;
bool isPrinting() const override;
private:
EmptySourcePrivate *const d;
......
......@@ -129,7 +129,7 @@ bool MailViewerSource::isPrinting() const
return mViewer->mPrinting;
}
void MailViewerSource::render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter)
void MailViewerSource::render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, bool showOnlyOneMimePart)
{
DefaultRenderer(msgPart, mViewer->cssHelper(), htmlWriter);
DefaultRenderer(msgPart, mViewer->cssHelper(), htmlWriter, showOnlyOneMimePart);
}
......@@ -48,7 +48,7 @@ public:
bool showEmoticons() const override;
bool showExpandQuotesMark() const override;
const MimeTreeParser::BodyPartFormatterFactory *bodyPartFormatterFactory() override;
void render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter) override;
void render(const MimeTreeParser::MessagePartPtr &msgPart, MimeTreeParser::HtmlWriter *htmlWriter, bool showOnlyOneMimePart) override;
bool isPrinting() const override;
private:
ViewerPrivate *mViewer = nullptr;
......
......@@ -148,10 +148,11 @@ public:
return QString(); //do nothing
}
void render(const MessagePart::Ptr &msgPart, HtmlWriter *htmlWriter) override
void render(const MessagePart::Ptr &msgPart, HtmlWriter *htmlWriter, bool showOnlyOneMimePart) override
{
Q_UNUSED(msgPart);
Q_UNUSED(htmlWriter);
Q_UNUSED(showOnlyOneMimePart);
}
bool isPrinting() const override
......
......@@ -89,7 +89,7 @@ public:
virtual const BodyPartFormatterFactory *bodyPartFormatterFactory() = 0;
virtual void render(const MessagePartPtr &msgPart, HtmlWriter *htmlWriter) = 0;
virtual void render(const MessagePartPtr &msgPart, HtmlWriter *htmlWriter, bool showOnlyOneMimePart) = 0;
virtual bool isPrinting() const = 0;
......
......@@ -464,106 +464,6 @@ void AttachmentMessagePart::setIsImage(bool image)
mIsImage = image;
}
IconType AttachmentMessagePart::asIcon() const
{
const AttachmentStrategy *const as = mOtp->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(content()) == AttachmentStrategy::None);
const bool showOnlyOneMimePart(mOtp->showOnlyOneMimePart());
auto preferredMode = source()->preferredMode();
bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
QByteArray mediaType("text");
if (content()->contentType(false) && !content()->contentType()->mediaType().isEmpty()
&& !content()->contentType()->subType().isEmpty()) {
mediaType = content()->contentType()->mediaType();
}
const bool isTextPart = (mediaType == QByteArrayLiteral("text"));
bool defaultAsIcon = true;
if (!neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(content()) == AttachmentStrategy::AsIcon;
}
}
if (isImage() && showOnlyOneMimePart && !neverDisplayInline()) {
defaultAsIcon = false;
}
// neither image nor text -> show as icon
if (!isImage() && !isTextPart) {
defaultAsIcon = true;
}
if (isTextPart) {
if (as && as->defaultDisplay(content()) != AttachmentStrategy::Inline) {
return MimeTreeParser::IconExternal;
}
return MimeTreeParser::NoIcon;
} else {
if (isImage() && isHtmlPreferred
&& content()->parent() && content()->parent()->contentType()->subType() == "related") {
return MimeTreeParser::IconInline;
}
if (defaultHidden && !showOnlyOneMimePart && content()->parent()) {
return MimeTreeParser::IconInline;
}
if (defaultAsIcon) {
return MimeTreeParser::IconExternal;
} else if (isImage()) {
return MimeTreeParser::IconInline;
} else {
return MimeTreeParser::NoIcon;
}
}
}
bool AttachmentMessagePart::isHidden() const
{
if (mOtp->showOnlyOneMimePart()) {
return false; // never hide when only showing one part, otherwise you'll see nothing
}
const AttachmentStrategy *const as = mOtp->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(content()) == AttachmentStrategy::None);
auto preferredMode = source()->preferredMode();
bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
QByteArray mediaType("text");
if (content()->contentType(false) && !content()->contentType()->mediaType().isEmpty()
&& !content()->contentType()->subType().isEmpty()) {
mediaType = content()->contentType()->mediaType();
}
const bool isTextPart = (mediaType == QByteArrayLiteral("text"));
bool defaultAsIcon = true;
if (!neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(content()) == AttachmentStrategy::AsIcon;
}
}
// neither image nor text -> show as icon
if (!isImage() && !isTextPart) {
defaultAsIcon = true;
}
bool hidden(false);
if (isTextPart) {
hidden = defaultHidden;
} else {
if (isImage() && isHtmlPreferred
&& content()->parent() && content()->parent()->contentType()->subType() == "related") {
hidden = true;
} else {
hidden = defaultHidden && content()->parent();
hidden |= defaultAsIcon && defaultHidden;
}
}
mOtp->nodeHelper()->setNodeDisplayedHidden(content(), hidden);
return hidden;
}
//-----HtmlMessageBlock----------------------
HtmlMessagePart::HtmlMessagePart(ObjectTreeParser *otp, KMime::Content *node, Interface::ObjectTreeSource *source)
......
......@@ -228,13 +228,13 @@ public:
AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool decryptMessage);
virtual ~AttachmentMessagePart();
IconType asIcon() const;
//IconType asIcon() const;
bool neverDisplayInline() const;
void setNeverDisplayInline(bool displayInline);
bool isImage() const;
void setIsImage(bool image);
bool isHidden() const override;
//bool isHidden() const override;
private:
bool mIsImage;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment