Commit e2468bed authored by Volker Krause's avatar Volker Krause

Expose MessagePart directly to the attachment Grantlee templates

Summary:
This reduces code duplication and makes this easier to re-use for plugins.
Additionally, it fixes two issues with attachment rendering:
- for many attachments we weren't showing links, making it unnecessarily
  hard to save or open them
- we were double-escaping attachment labels and descriptions

Reviewers: knauss

Reviewed By: knauss

Subscribers: #kde_pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D8124
parent d1cc1fc4
......@@ -66,18 +66,10 @@ bool AttachmentMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr&
Grantlee::Template t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(
":/asiconpart.html"));
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("inline", (tmpAsIcon == MimeTreeParser::IconInline));
c.insert(QStringLiteral("block"), msgPart.data());
msgPart->setProperty("link", nodeHelper->asHREF(node, QStringLiteral("body")));
msgPart->setProperty("inline", (tmpAsIcon == MimeTreeParser::IconInline));
QString iconPath;
if (tmpAsIcon == MimeTreeParser::IconInline) {
......@@ -89,24 +81,7 @@ bool AttachmentMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr&
iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
}
}
block.setProperty("iconPath", QUrl::fromLocalFile(iconPath).url());
const QString name = node->contentType()->name();
QString label = name.isEmpty() ? NodeHelper::fileName(node) : name;
QString comment = node->contentDescription()->asUnicodeString();
if (label.isEmpty()) {
label = i18nc("display name for an unnamed attachment", "Unnamed");
}
label = MessageCore::StringUtil::quoteHtmlChars(label, true);
comment = MessageCore::StringUtil::quoteHtmlChars(comment, true);
if (label == comment) {
comment.clear();
}
block.setProperty("label", label);
block.setProperty("comment", comment);
msgPart->setProperty("iconPath", QUrl::fromLocalFile(iconPath).url());
MimeTreeParser::AttachmentMarkBlock attBlock(nullptr, mp->attachmentContent());
if (mp->isAttachment())
......
......@@ -56,18 +56,8 @@ bool TextMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr& msgPa
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(MimeTreeParser::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());
c.insert(QStringLiteral("block"), msgPart.data());
msgPart->setProperty("link", nodeHelper->asHREF(node, QStringLiteral("body")));
t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral(
":/textmessagepart.html"));
......
From: Volker Krause <vkrause@kde.org>
To: vkrause@kde.org
Subject: text attachments trying to mess up escaping
Date: Tue, 03 Oct 2017 12:21:24 +0200
Message-ID: <1807613.y0QX81sLpY@vkpc5>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="nextPart2107984.jr9reY14Qx"
Content-Transfer-Encoding: 7Bit
This is a multi-part message in MIME format.
--nextPart2107984.jr9reY14Qx
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"
hello, this is the body
--nextPart2107984.jr9reY14Qx
Content-Disposition: inline; filename="M<i>essag</i>es.sh"
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="utf-8"; name="M<i>essag</i>es.sh"
#! /bin/sh
$XGETTEXT `find . -name '*.cpp' -o -name '*.h' | grep -v '/tests/' | grep -v '/autotests/'` -o $podir/libmimetreeparser.pot
--nextPart2107984.jr9reY14Qx
Content-Description: A long description with\n <br/> various attempts of <b>breaking</b> stuff.
Content-Disposition: inline; filename="metainfo.yaml"
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="utf-8"; name="metainfo.yaml"
maintainer:
description: MimeTreeParser Library
tier: 3
type: functional
platforms:
- name: All
portingAid: false
deprecated: false
release: false
libraries:
- qmake: MimeTreeParser
cmake: "KF5::MimeTreeParser"
cmakename: KF5MimeTreeParser
--nextPart2107984.jr9reY14Qx--
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att1"/>
<div id="attachmentDiv1">
<div class="noquote">
<div dir="ltr">hello, this is the body</div>
</div>
</div>
<a name="att2"/>
<div id="attachmentDiv2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">
<a href="attachment:2?place=body">M&lt;i&gt;essag&lt;/i&gt;es.sh</a>
</td>
</tr>
<tr class="textAtmB">
<td>
<div class="noquote">
<div dir="ltr">#! /bin/sh</div>
<div dir="ltr">$XGETTEXT `find . -name '*.cpp' -o -name '*.h' | grep -v '/tests/' | grep -v '/autotests/'` -o $podir/libmimetreeparser.pot</div>
</div>
</td>
</tr>
</table>
</div>
<a name="att3"/>
<div id="attachmentDiv3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr"><a href="attachment:3?place=body">metainfo.yaml</a><br/>A long description with\n &lt;br/&gt; various attempts of &lt;b&gt;breaking&lt;/b&gt; stuff.</td>
</tr>
<tr class="textAtmB">
<td>
<div class="noquote">
<div dir="ltr">maintainer:</div>
<div dir="ltr">description: MimeTreeParser Library&nbsp;</div>
<div dir="ltr">tier: 3</div>
<div dir="ltr">type: functional</div>
<div dir="ltr">platforms:</div>
<div dir="ltr">&nbsp;&nbsp;&nbsp;&nbsp;- name: All</div>
<div dir="ltr">portingAid: false</div>
<div dir="ltr">deprecated: false</div>
<div dir="ltr">release: false</div>
<div dir="ltr">libraries:</div>
<div dir="ltr">&nbsp;- qmake: MimeTreeParser</div>
<div dir="ltr">&nbsp;&nbsp;&nbsp;cmake: "KF5::MimeTreeParser"</div>
<div dir="ltr">&nbsp;&nbsp;&nbsp;cmakename: KF5MimeTreeParser</div>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
* MimeTreeParser::MessagePartList
* MimeTreeParser::MimeMessagePart
* MimeTreeParser::TextMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
* MimeTreeParser::AttachmentMessagePart
* MimeTreeParser::MessagePart
......@@ -46,7 +46,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:1:e0:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -44,7 +44,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:e0:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......@@ -59,7 +61,9 @@
<div id="attachmentDiv1.3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment2.txt</td>
<td dir="ltr">
<a href="attachment:e0:1.3?place=body">attachment2.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -35,7 +35,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......@@ -50,7 +52,9 @@
<div id="attachmentDiv1.3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment2.txt</td>
<td dir="ltr">
<a href="attachment:1.3?place=body">attachment2.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att1"/>
<div id="attachmentDiv1">
<div class="noquote">
<div dir="ltr">hello, this is the body</div>
</div>
</div>
</div>
</div>
</body>
</html>
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att1"/>
<div id="attachmentDiv1">
<div class="noquote">
<div dir="ltr">hello, this is the body</div>
</div>
</div>
</div>
</div>
</body>
</html>
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div style="position: relative; word-wrap: break-word">
<a name="att"/>
<div id="attachmentDiv">
<a name="att1"/>
<div id="attachmentDiv1">
<div class="noquote">
<div dir="ltr">hello, this is the body</div>
</div>
</div>
<a name="att2"/>
<div id="attachmentDiv2">
<hr/>
<div>
<a href="attachment:2?place=body"><img align="center" height="48" width="48" src="file:text-plain.svg" border="0" style="max-width: 100%" alt=""/>M&lt;i&gt;essag&lt;/i&gt;es.sh</a>
</div>
<div/>
</div>
<a name="att3"/>
<div id="attachmentDiv3">
<hr/>
<div>
<a href="attachment:3?place=body"><img align="center" height="48" width="48" src="file:text-plain.svg" border="0" style="max-width: 100%" alt=""/>metainfo.yaml</a>
</div>
<div>A long description with\n &lt;br/&gt; various attempts of &lt;b&gt;breaking&lt;/b&gt; stuff.</div>
</div>
</div>
</div>
</body>
</html>
......@@ -33,7 +33,9 @@
<div id="attachmentDiv2.1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment.txt</td>
<td dir="ltr">
<a href="attachment:2.1.2?place=body">attachment.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -43,7 +43,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">file.txt</td>
<td dir="ltr">
<a href="attachment:1:e0:1.2?place=body">file.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -41,7 +41,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">file.txt</td>
<td dir="ltr">
<a href="attachment:e0:1.2?place=body">file.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -36,7 +36,9 @@
<div id="attachmentDiv1.3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">broken.attachment</td>
<td dir="ltr">
<a href="attachment:1.3?place=body">broken.attachment</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -43,7 +43,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:1:e0:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -24,7 +24,9 @@
<div id="attachmentDiv2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:e0:2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......@@ -39,7 +41,9 @@
<div id="attachmentDiv3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment2.txt</td>
<td dir="ltr">
<a href="attachment:e0:3?place=body">attachment2.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -41,7 +41,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:e0:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......@@ -56,7 +58,9 @@
<div id="attachmentDiv1.3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment2.txt</td>
<td dir="ltr">
<a href="attachment:e0:1.3?place=body">attachment2.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -32,7 +32,9 @@
<div id="attachmentDiv1.2">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment1.txt</td>
<td dir="ltr">
<a href="attachment:1.2?place=body">attachment1.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......@@ -47,7 +49,9 @@
<div id="attachmentDiv1.3">
<table cellspacing="1" class="textAtm">
<tr class="textAtmH">
<td dir="ltr">attachment2.txt</td>
<td dir="ltr">
<a href="attachment:1.3?place=body">attachment2.txt</a>
</td>
</tr>
<tr class="textAtmB">
<td>
......
......@@ -53,9 +53,9 @@ MessagePart::Ptr TextPlainBodyPartFormatter::process(Interface::BodyPart &part)
TextMessagePart::Ptr mp;
if (isFirstTextPart) {
mp = TextMessagePart::Ptr(new TextMessagePart(part.objectTreeParser(), node, bDrawFrame, fileName.isEmpty(), part.source()->decryptMessage()));
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()));
mp = TextMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, bDrawFrame, !fileName.isEmpty(), part.source()->decryptMessage()));
}
part.processResult()->setInlineSignatureState(mp->signatureState());
......
......@@ -383,6 +383,24 @@ bool TextMessagePart::showTextFrame() const
return mDrawFrame;
}
QString TextMessagePart::label() const
{
const QString name = content()->contentType()->name();
QString label = name.isEmpty() ? NodeHelper::fileName(content()) : name;
if (label.isEmpty()) {
label = i18nc("display name for an unnamed attachment", "Unnamed");
}
return label;
}
QString TextMessagePart::comment() const
{
const QString comment = content()->contentDescription()->asUnicodeString();
if (comment == label())
return {};
return comment;
}
//-----AttachmentMessageBlock----------------------
AttachmentMessagePart::AttachmentMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage)
......
......@@ -184,6 +184,10 @@ enum IconType {
class MIMETREEPARSER_EXPORT TextMessagePart : public MessagePartList
{
Q_OBJECT
Q_PROPERTY(bool showTextFrame READ showTextFrame CONSTANT)
Q_PROPERTY(bool showLink READ showLink CONSTANT)
Q_PROPERTY(QString label READ label CONSTANT)
Q_PROPERTY(QString comment READ comment CONSTANT)
public:
typedef QSharedPointer<TextMessagePart> Ptr;
TextMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage);
......@@ -199,6 +203,11 @@ public:
bool showLink() const;
bool showTextFrame() const;
/** The attachment filename, or the closest approximation thereof we have. */
QString label() const;
/** A description of this attachment, if provided. */
QString comment() const;
private:
void parseContent();
......
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