Messagelib

attachmentmessagepartrenderer.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Sandro KnauƟ <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "attachmentmessagepartrenderer.h"
8 
9 #include "quotehtml.h"
10 
11 #include "../messagepartrenderermanager.h"
12 #include "utils/mimetype.h"
13 
14 #include "interfaces/htmlwriter.h"
15 #include <MessageCore/StringUtil>
16 
17 #include <KIconLoader>
18 #include <QUrl>
19 
20 using namespace MessageViewer;
21 
22 AttachmentMessagePartRenderer::AttachmentMessagePartRenderer() = default;
23 
24 AttachmentMessagePartRenderer::~AttachmentMessagePartRenderer() = default;
25 
26 bool AttachmentMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr &msgPart, HtmlWriter *htmlWriter, RenderContext *context) const
27 {
28  auto mp = msgPart.dynamicCast<MimeTreeParser::TextMessagePart>();
29  if (!mp) {
30  return false;
31  }
32 
33  KMime::Content *node = mp->content();
34  if (context->isHiddenHint(msgPart)) {
35  return true;
36  }
37 
38  const auto tmpAsIcon = context->displayHint(msgPart);
39 
40  if (tmpAsIcon == MimeTreeParser::NoIcon) {
41  return context->renderWithFactory<MimeTreeParser::TextMessagePart>(mp, htmlWriter);
42  }
43 
44  Grantlee::Template t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral("asiconpart.html"));
45  Grantlee::Context c = MessageViewer::MessagePartRendererManager::self()->createContext();
46  c.insert(QStringLiteral("block"), msgPart.data());
47 
48  msgPart->setProperty("inline", (tmpAsIcon == MimeTreeParser::IconInline));
49 
50  QString iconPath;
51  if (tmpAsIcon == MimeTreeParser::IconInline) {
52  iconPath = mp->temporaryFilePath();
53  } else {
54  iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
55  if (iconPath.right(14) == QLatin1String("mime_empty.png")) {
57  iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
58  }
59  }
60  msgPart->setProperty("iconPath", QUrl::fromLocalFile(iconPath).url());
61 
62  Grantlee::OutputStream s(htmlWriter->stream());
63  t->render(&s, &c);
64  return true;
65 }
T * data() const const
static void magicSetType(KMime::Content *node, bool autoDecode=true)
Set the &#39;Content-Type&#39; by mime-magic from the contents of the body.
Content * content(const ContentIndex &index) const
QString right(int n) const const
QTextStream * stream() const
Returns a QTextStream on device().
Definition: htmlwriter.cpp:53
QSharedPointer< X > dynamicCast() const const
An interface for HTML sinks.
Definition: htmlwriter.h:28
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.