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 "../messagepartrenderermanager.h"
10 #include "utils/mimetype.h"
11 
12 #include "interfaces/htmlwriter.h"
13 #include <MessageCore/StringUtil>
14 
15 #include <KIconLoader>
16 #include <QUrl>
17 
18 using namespace MessageViewer;
19 
20 AttachmentMessagePartRenderer::AttachmentMessagePartRenderer() = default;
21 
22 AttachmentMessagePartRenderer::~AttachmentMessagePartRenderer() = default;
23 
24 bool AttachmentMessagePartRenderer::render(const MimeTreeParser::MessagePartPtr &msgPart, HtmlWriter *htmlWriter, RenderContext *context) const
25 {
26  auto mp = msgPart.dynamicCast<MimeTreeParser::TextMessagePart>();
27  if (!mp) {
28  return false;
29  }
30 
31  KMime::Content *node = mp->content();
32  if (context->isHiddenHint(msgPart)) {
33  return true;
34  }
35 
36  const auto tmpAsIcon = context->displayHint(msgPart);
37 
38  if (tmpAsIcon == MimeTreeParser::NoIcon) {
39  return context->renderWithFactory<MimeTreeParser::TextMessagePart>(mp, htmlWriter);
40  }
41 
42 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
43  Grantlee::Template t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral("asiconpart.html"));
44  Grantlee::Context c = MessageViewer::MessagePartRendererManager::self()->createContext();
45 #else
46  KTextTemplate::Template t = MessageViewer::MessagePartRendererManager::self()->loadByName(QStringLiteral("asiconpart.html"));
47  KTextTemplate::Context c = MessageViewer::MessagePartRendererManager::self()->createContext();
48 #endif
49  c.insert(QStringLiteral("block"), msgPart.data());
50 
51  msgPart->setProperty("inline", (tmpAsIcon == MimeTreeParser::IconInline));
52 
53  QString iconPath;
54  if (tmpAsIcon == MimeTreeParser::IconInline) {
55  iconPath = mp->temporaryFilePath();
56  } else {
57  iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
58  if (iconPath.right(14) == QLatin1String("mime_empty.png")) {
60  iconPath = MessageViewer::Util::iconPathForContent(node, KIconLoader::Desktop);
61  }
62  }
63  msgPart->setProperty("iconPath", QUrl::fromLocalFile(iconPath).url());
64 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
65  Grantlee::OutputStream s(htmlWriter->stream());
66 #else
67  KTextTemplate::OutputStream s(htmlWriter->stream());
68 #endif
69  t->render(&s, &c);
70  return true;
71 }
Content * content(const ContentIndex &index) const
void insert(const QString &name, const QVariant &variant)
T * data() const const
QUrl fromLocalFile(const QString &localFile)
An interface for HTML sinks.
Definition: htmlwriter.h:28
static void magicSetType(KMime::Content *node, bool autoDecode=true)
Set the 'Content-Type' by mime-magic from the contents of the body.
QString right(int n) const const
QString render(Context *c) const
QTextStream * stream() const
Returns a QTextStream on device().
Definition: htmlwriter.cpp:53
QSharedPointer< X > dynamicCast() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.