Commit 7a23bb45 authored by Sandro Knauß's avatar Sandro Knauß

create AttachmentMessagePart and split TextMessagePart

parent e4a9f441
......@@ -548,7 +548,7 @@ public:
return htmlWriter->html;
}
QString render(const TextMessagePart::Ptr &mp)
QString render(const AttachmentMessagePart::Ptr &mp)
{
KMime::Content *node = mp->mNode;
NodeHelper *nodeHelper = mp->mOtp->nodeHelper();
......@@ -557,25 +557,26 @@ public:
return QString();
}
const auto tmpAsIcon = mp->asIcon();
Grantlee::Template t;
Grantlee::Context c = MessageViewer::MessagePartRendererManager::self()->createContext();
QObject block;
c.insert(QStringLiteral("block"), &block);
block.setProperty("showTextFrame", mp->mDrawFrame);
block.setProperty("showTextFrame", mp->showTextFrame());
block.setProperty("label", MessageCore::StringUtil::quoteHtmlChars(NodeHelper::fileName(node), true));
block.setProperty("comment", MessageCore::StringUtil::quoteHtmlChars(node->contentDescription()->asUnicodeString(), true));
block.setProperty("link", nodeHelper->asHREF(node, QStringLiteral("body")));
block.setProperty("showLink", mp->mShowLink);
block.setProperty("showLink", mp->showLink());
block.setProperty("dir", alignText());
if (mp->mAsIcon != MimeTreeParser::NoIcon) {
if (tmpAsIcon != MimeTreeParser::NoIcon) {
t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(":/asiconpart.html"));
block.setProperty("iconSize", MessageViewer::MessagePartRendererManager::self()->iconCurrentSize());
block.setProperty("inline", (mp->mAsIcon == MimeTreeParser::IconInline));
block.setProperty("inline", (tmpAsIcon == MimeTreeParser::IconInline));
QString iconPath;
if (mp->mAsIcon == MimeTreeParser::IconInline) {
if (tmpAsIcon == MimeTreeParser::IconInline) {
iconPath = nodeHelper->writeNodeToTempFile(node);
} else {
iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
......@@ -603,13 +604,48 @@ public:
block.setProperty("label", label);
block.setProperty("comment", comment);
auto htmlWriter = QSharedPointer<CacheHtmlWriter>(new CacheHtmlWriter(mOldWriter));
{
HTMLBlock::Ptr aBlock;
if (mp->isAttachment()) {
aBlock = HTMLBlock::Ptr(new AttachmentMarkBlock(htmlWriter.data(), mp->attachmentNode()));
}
const auto html = t->render(&c);
htmlWriter->queue(html);
}
return htmlWriter->html;
} else {
t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(":/textmessagepart.html"));
auto _htmlWriter = QSharedPointer<CacheHtmlWriter>(new CacheHtmlWriter(mOldWriter));
renderSubParts(mp, _htmlWriter);
c.insert(QStringLiteral("content"), _htmlWriter->html);
return render(mp.dynamicCast<TextMessagePart>());
}
}
QString render(const TextMessagePart::Ptr &mp)
{
KMime::Content *node = mp->mNode;
NodeHelper *nodeHelper = mp->mOtp->nodeHelper();
if (mp->isHidden()) {
return QString();
}
Grantlee::Template t;
Grantlee::Context c = MessageViewer::MessagePartRendererManager::self()->createContext();
QObject block;
c.insert(QStringLiteral("block"), &block);
block.setProperty("showTextFrame", mp->showTextFrame());
block.setProperty("label", MessageCore::StringUtil::quoteHtmlChars(NodeHelper::fileName(node), true));
block.setProperty("comment", MessageCore::StringUtil::quoteHtmlChars(node->contentDescription()->asUnicodeString(), true));
block.setProperty("link", nodeHelper->asHREF(node, QStringLiteral("body")));
block.setProperty("showLink", mp->showLink());
block.setProperty("dir", alignText());
t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(":/textmessagepart.html"));
auto _htmlWriter = QSharedPointer<CacheHtmlWriter>(new CacheHtmlWriter(mOldWriter));
renderSubParts(mp, _htmlWriter);
c.insert(QStringLiteral("content"), _htmlWriter->html);
auto htmlWriter = QSharedPointer<CacheHtmlWriter>(new CacheHtmlWriter(mOldWriter));
{
HTMLBlock::Ptr aBlock;
......@@ -1270,6 +1306,11 @@ public:
if (mp) {
return render(mp);
}
} else if (className == QStringLiteral("MimeTreeParser::AttachmentMessagePart")) {
auto mp = msgPart.dynamicCast<AttachmentMessagePart>();
if (mp) {
return render(mp);
}
} else if (className == QStringLiteral("MimeTreeParser::TextMessagePart")) {
auto mp = msgPart.dynamicCast<TextMessagePart>();
if (mp) {
......
......@@ -4,7 +4,7 @@
* MimeTreeParser::MessagePart
* MimeTreeParser::EncapsulatedRfc822MessagePart
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -5,7 +5,7 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -4,5 +4,5 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::MessagePartList
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -5,5 +5,5 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -5,7 +5,7 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::CryptoMessagePart
* MimeTreeParser::TextMessagePart
......
......@@ -3,7 +3,7 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -3,5 +3,5 @@
* MimeTreeParser::CryptoMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -4,7 +4,7 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -3,8 +3,8 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -3,5 +3,5 @@
* MimeTreeParser::CryptoMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -2,4 +2,4 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
......@@ -3,7 +3,7 @@
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::MessagePartList
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::AlternativeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -55,27 +55,21 @@ Interface::MessagePart::Ptr TextPlainBodyPartFormatter::process(Interface::BodyP
KMime::Content *node = part.content();
const bool isFirstTextPart = (node->topLevel()->textContent() == node);
IconType iconType = NoIcon;
if (!isFirstTextPart && part.objectTreeParser()->attachmentStrategy()->defaultDisplay(node) != AttachmentStrategy::Inline &&
!part.objectTreeParser()->showOnlyOneMimePart()) {
iconType = MimeTreeParser::IconExternal;
}
if (!isFirstTextPart && (part.objectTreeParser()->attachmentStrategy()->defaultDisplay(node) == AttachmentStrategy::None) && !part.objectTreeParser()->showOnlyOneMimePart()) {
part.nodeHelper()->setNodeDisplayedHidden(node, true);
}
part.objectTreeParser()->extractNodeInfos(node, isFirstTextPart);
QString label = NodeHelper::fileName(node);
const bool bDrawFrame = !isFirstTextPart
&& !part.objectTreeParser()->showOnlyOneMimePart()
&& !label.isEmpty()
&& iconType == MimeTreeParser::NoIcon;
&& !label.isEmpty();
const QString fileName = part.nodeHelper()->writeNodeToTempFile(node);
TextMessagePart::Ptr mp(new TextMessagePart(part.objectTreeParser(), node, bDrawFrame, fileName.isEmpty(), part.source()->decryptMessage(), iconType));
TextMessagePart::Ptr mp;
if (isFirstTextPart) {
mp = TextMessagePart::Ptr(new TextMessagePart(part.objectTreeParser(), node, bDrawFrame, fileName.isEmpty(), part.source()->decryptMessage()));
} else {
mp = TextMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, bDrawFrame, fileName.isEmpty(), part.source()->decryptMessage()));
}
part.processResult()->setInlineSignatureState(mp->signatureState());
part.processResult()->setInlineEncryptionState(mp->encryptionState());
......
......@@ -19,6 +19,7 @@
#include "messagepart.h"
#include "mimetreeparser_debug.h"
#include "attachmentstrategy.h"
#include "cryptohelper.h"
#include "objecttreeparser.h"
#include "interfaces/htmlwriter.h"
......@@ -219,14 +220,13 @@ QString MessagePartList::htmlContent() const
//-----TextMessageBlock----------------------
TextMessagePart::TextMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage, IconType asIcon)
TextMessagePart::TextMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage)
: MessagePartList(otp)
, mNode(node)
, mDrawFrame(drawFrame)
, mShowLink(showLink)
, mDecryptMessage(decryptMessage)
, mIsHidden(false)
, mAsIcon(asIcon)
{
if (!mNode) {
qCWarning(MIMETREEPARSER_LOG) << "not a valid node";
......@@ -342,6 +342,152 @@ bool TextMessagePart::isHidden() const
return mIsHidden;
}
bool TextMessagePart::showLink() const
{
return mShowLink;
}
bool TextMessagePart::showTextFrame() const
{
return mDrawFrame;
}
//-----AttachmentMessageBlock----------------------
AttachmentMessagePart::AttachmentMessagePart(ObjectTreeParser* otp, KMime::Content* node, bool drawFrame, bool showLink, bool decryptMessage)
: TextMessagePart(otp, node, drawFrame, showLink, decryptMessage)
, mIsImage(false)
, mNeverDisplayInline(false)
{
}
AttachmentMessagePart::~AttachmentMessagePart()
{
}
bool AttachmentMessagePart::neverDisplayInline() const
{
return mNeverDisplayInline;
}
void AttachmentMessagePart::setNeverDisplayInline(bool displayInline)
{
mNeverDisplayInline = displayInline;
}
bool AttachmentMessagePart::isImage() const
{
return mIsImage;
}
void AttachmentMessagePart::setIsImage(bool image)
{
mIsImage = image;
}
IconType AttachmentMessagePart::asIcon() const
{
const AttachmentStrategy *const as = mOtp->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(mNode) == AttachmentStrategy::None);
const bool showOnlyOneMimePart(mOtp->showOnlyOneMimePart());
QByteArray mediaType("text");
QByteArray subType("plain");
if (mNode->contentType(false) && !mNode->contentType()->mediaType().isEmpty() &&
!mNode->contentType()->subType().isEmpty()) {
mediaType = mNode->contentType()->mediaType();
subType = mNode->contentType()->subType();
}
const bool isTextPart = (mediaType == QByteArray("text"));
bool defaultAsIcon = true;
if (!neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(mNode) == 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(mNode) != AttachmentStrategy::Inline) {
return MimeTreeParser::IconExternal;
}
return MimeTreeParser::NoIcon;
} else {
if (isImage() && source()->htmlMail() &&
mNode->parent() && mNode->parent()->contentType()->subType() == "related") {
return MimeTreeParser::IconInline;
}
if(defaultHidden && !showOnlyOneMimePart && mNode->parent()) {
return MimeTreeParser::IconInline;
}
if (defaultAsIcon) {
return MimeTreeParser::IconExternal;
} else if (isImage()) {
return MimeTreeParser::IconInline;
} else {
return MimeTreeParser::NoIcon;
}
}
}
bool AttachmentMessagePart::isHidden() const
{
const AttachmentStrategy *const as = mOtp->attachmentStrategy();
const bool defaultHidden(as && as->defaultDisplay(mNode) == AttachmentStrategy::None);
const bool showOnlyOneMimePart(mOtp->showOnlyOneMimePart());
QByteArray mediaType("text");
QByteArray subType("plain");
if (mNode->contentType(false) && !mNode->contentType()->mediaType().isEmpty() &&
!mNode->contentType()->subType().isEmpty()) {
mediaType = mNode->contentType()->mediaType();
subType = mNode->contentType()->subType();
}
const bool isTextPart = (mediaType == QByteArray("text"));
bool defaultAsIcon = true;
if (!neverDisplayInline()) {
if (as) {
defaultAsIcon = as->defaultDisplay(mNode) == AttachmentStrategy::AsIcon;
}
}
if (isImage() && showOnlyOneMimePart && !neverDisplayInline()) {
defaultAsIcon = false;
}
// neither image nor text -> show as icon
if (!isImage() && !isTextPart) {
defaultAsIcon = true;
}
bool hidden(false);
if (isTextPart) {
hidden = defaultHidden && !showOnlyOneMimePart;
} else {
if (isImage() && source()->htmlMail() &&
mNode->parent() && mNode->parent()->contentType()->subType() == "related") {
hidden = true;
} else {
hidden = defaultHidden && !showOnlyOneMimePart && mNode->parent();
hidden |= defaultAsIcon && (defaultHidden || showOnlyOneMimePart);
}
}
mOtp->nodeHelper()->setNodeDisplayedHidden(mNode, hidden);
return hidden;
}
//-----HtmlMessageBlock----------------------
HtmlMessagePart::HtmlMessagePart(ObjectTreeParser *otp, KMime::Content *node, Interface::ObjectTreeSource *source)
......
......@@ -159,7 +159,7 @@ class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList
Q_OBJECT
public:
typedef QSharedPointer<TextMessagePart> Ptr;
TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage, IconType asIcon);
TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage);
virtual ~TextMessagePart();
KMMsgSignatureState signatureState() const;
......@@ -169,21 +169,46 @@ public:
bool isHidden() const Q_DECL_OVERRIDE;
bool showLink() const;
bool showTextFrame() const;
protected:
KMime::Content *mNode;
private:
void parseContent();
KMime::Content *mNode;
KMMsgSignatureState mSignatureState;
KMMsgEncryptionState mEncryptionState;
bool mDrawFrame;
bool mShowLink;
bool mDecryptMessage;
bool mIsHidden;
IconType mAsIcon;
friend class DefaultRendererPrivate;
};
class MIMETREEPARSER_EXPORT AttachmentMessagePart : public TextMessagePart
{
Q_OBJECT
public:
typedef QSharedPointer<AttachmentMessagePart> Ptr;
AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage);
virtual ~AttachmentMessagePart();
IconType asIcon() const;
bool neverDisplayInline() const;
void setNeverDisplayInline(bool displayInline);
bool isImage() const;
void setIsImage(bool image);
bool isHidden() const Q_DECL_OVERRIDE;
private:
bool mIsImage;
bool mNeverDisplayInline;
};
class MIMETREEPARSER_EXPORT HtmlMessagePart : public MessagePart
{
Q_OBJECT
......
......@@ -339,25 +339,6 @@ MessagePart::Ptr ObjectTreeParser::parseObjectTreeInternal(KMime::Content *node)
Interface::MessagePart::Ptr ObjectTreeParser::defaultHandling(KMime::Content *node, ProcessResult &result)
{
// ### (mmutz) default handling should go into the respective
// ### bodypartformatters.
if (!htmlWriter()) {
qCWarning(MIMETREEPARSER_LOG) << "no htmlWriter()";
return MessagePart::Ptr();
}
// always show images in multipart/related when showing in html, not with an additional icon
if (result.isImage() && node->parent() &&
node->parent()->contentType()->subType() == "related" && mSource->htmlMail() && !showOnlyOneMimePart()) {
QString fileName = mNodeHelper->writeNodeToTempFile(node);
QString href = QUrl::fromLocalFile(fileName).url();
QByteArray cid = node->contentID()->identifier();
htmlWriter()->embedPart(cid, href);
nodeHelper()->setNodeDisplayedEmbedded(node, true);
mNodeHelper->setNodeDisplayedHidden(node, true);
const auto mp = TextMessagePart::Ptr(new TextMessagePart(this, node, false, true, mSource->decryptMessage(), MimeTreeParser::IconInline));
return mp;
}
Interface::MessagePart::Ptr mp;
ProcessResult processResult(mNodeHelper);
......@@ -370,56 +351,34 @@ Interface::MessagePart::Ptr ObjectTreeParser::defaultHandling(KMime::Content *no
return mp;
}
const AttachmentStrategy *const as = attachmentStrategy();
if (as && as->defaultDisplay(node) == AttachmentStrategy::None &&
!showOnlyOneMimePart() &&
node->parent() /* message is not an attachment */) {
mNodeHelper->setNodeDisplayedHidden(node, true);
const auto mp = TextMessagePart::Ptr(new TextMessagePart(this, node, false, true, mSource->decryptMessage(), MimeTreeParser::IconInline));
return mp;
}
const auto _mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(this, node, false, true, mSource->decryptMessage()));
result.setInlineSignatureState(_mp->signatureState());
result.setInlineEncryptionState(_mp->encryptionState());
_mp->setNeverDisplayInline(result.neverDisplayInline());
_mp->setIsImage(result.isImage());
mp = _mp;
bool asIcon = true;
if (!result.neverDisplayInline()) {
if (as) {
asIcon = as->defaultDisplay(node) == AttachmentStrategy::AsIcon;
// always show images in multipart/related when showing in html, not with an additional icon
if (result.isImage() && node->parent() &&
node->parent()->contentType()->subType() == "related" && mSource->htmlMail() && !showOnlyOneMimePart()) {
QString fileName = mNodeHelper->writeNodeToTempFile(node);
QString href = QUrl::fromLocalFile(fileName).url();
QByteArray cid = node->contentID()->identifier();
if (htmlWriter()) {
htmlWriter()->embedPart(cid, href);
}
nodeHelper()->setNodeDisplayedEmbedded(node, true);
mNodeHelper->setNodeDisplayedHidden(node, true);
return mp;
}
// Show it inline if showOnlyOneMimePart(), which means the user clicked the image
// in the message structure viewer manually, and therefore wants to see the full image
if (result.isImage() && showOnlyOneMimePart() && !result.neverDisplayInline()) {
asIcon = false;
}
// neither image nor text -> show as icon
if (!result.isImage()
&& !node->contentType()->isText()) {
asIcon = true;
}
/*FIXME(Andras) port it
// if the image is not complete do not try to show it inline
if ( result.isImage() && !node->msgPart().isComplete() )
asIcon = true;
*/
if (asIcon) {
bool hidePart = (as && as->defaultDisplay(node) == AttachmentStrategy::None) || showOnlyOneMimePart();
mNodeHelper->setNodeDisplayedHidden(node, hidePart);
return TextMessagePart::Ptr(new TextMessagePart(this, node, false, true, mSource->decryptMessage(), MimeTreeParser::IconExternal));
} else if (result.isImage()) {
// Embed the image
mNodeHelper->setNodeDisplayedEmbedded(node, true);
return TextMessagePart::Ptr(new TextMessagePart(this, node, false, true, mSource->decryptMessage(), MimeTreeParser::IconInline));
} else {
mNodeHelper->setNodeDisplayedEmbedded(node, true);
const auto mp = TextMessagePart::Ptr(new TextMessagePart(this, node, false, true, mSource->decryptMessage(), MimeTreeParser::NoIcon));
result.setInlineSignatureState(mp->signatureState());
result.setInlineEncryptionState(mp->encryptionState());
return mp;
}
return MessagePart::Ptr();
return mp;
}
KMMsgSignatureState ProcessResult::inlineSignatureState() const
......
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