Messagelib

mimetreeparser/src/utils/util.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Sandro Knauß <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "util.h"
8 
9 #include "mimetreeparser_debug.h"
10 
11 #include "nodehelper.h"
12 
13 #include <KMime/Content>
14 
15 #include <QMimeDatabase>
16 
17 using namespace MimeTreeParser;
18 using namespace MimeTreeParser::Util;
19 
20 bool MimeTreeParser::Util::isTypeBlacklisted(KMime::Content *node)
21 {
22  const auto contentType = node->contentType(); // Create
23  const QByteArray mediaTypeLower = contentType->mediaType().toLower();
24  bool typeBlacklisted = mediaTypeLower == QByteArrayLiteral("multipart");
25  if (!typeBlacklisted) {
26  typeBlacklisted = KMime::isCryptoPart(node);
27  }
28  typeBlacklisted = typeBlacklisted || node == node->topLevel();
29  const bool firstTextChildOfEncapsulatedMsg = mediaTypeLower == "text" && contentType->subType().toLower() == "plain" && node->parent()
30  && node->parent()->contentType()->mediaType().toLower() == "message";
31  return typeBlacklisted || firstTextChildOfEncapsulatedMsg;
32 }
33 
34 QString MimeTreeParser::Util::labelForContent(KMime::Content *node)
35 {
36  const QString name = node->contentType()->name();
38  if (label.isEmpty()) {
40  }
41  return label;
42 }
43 
44 QMimeType MimeTreeParser::Util::mimetype(const QString &name)
45 {
46  QMimeDatabase db;
47  // consider the filename if mimetype cannot be found by content-type
48  const auto mimeTypes = db.mimeTypesForFileName(name);
49  for (const auto &mt : mimeTypes) {
50  if (mt.name() != QLatin1String("application/octet-stream")) {
51  return mt;
52  }
53  }
54 
55  // consider the attachment's contents if neither the Content-Type header
56  // nor the filename give us a clue
57  return db.mimeTypeForFile(name);
58 }
59 
60 QString MimeTreeParser::Util::iconNameForMimetype(const QString &mimeType, const QString &fallbackFileName1, const QString &fallbackFileName2)
61 {
62  QString fileName;
63  QString tMimeType = mimeType;
64 
65  // convert non-registered types to registered types
66  if (mimeType == QLatin1String("application/x-vnd.kolab.contact")) {
67  tMimeType = QStringLiteral("text/x-vcard");
68  } else if (mimeType == QLatin1String("application/x-vnd.kolab.event")) {
69  tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event");
70  } else if (mimeType == QLatin1String("application/x-vnd.kolab.task")) {
71  tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo");
72  } else if (mimeType == QLatin1String("application/x-vnd.kolab.journal")) {
73  tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal");
74  } else if (mimeType == QLatin1String("application/x-vnd.kolab.note")) {
75  tMimeType = QStringLiteral("application/x-vnd.akonadi.note");
76  } else if (mimeType == QLatin1String("image/jpg")) {
77  tMimeType = QStringLiteral("image/jpeg");
78  } else if (mimeType == QLatin1String("application/x-pkcs7-signature")) {
79  tMimeType = QStringLiteral("application/pkcs7-signature");
80  } else if (mimeType == QLatin1String("message/global")) {
81  tMimeType = QStringLiteral("message/rfc822");
82  }
83  QMimeDatabase mimeDb;
84  auto mime = mimeDb.mimeTypeForName(tMimeType);
85  if (mime.isValid()) {
86  fileName = mime.iconName();
87  } else {
88  fileName = QStringLiteral("unknown");
89  if (!tMimeType.isEmpty()) {
90  qCWarning(MIMETREEPARSER_LOG) << "unknown mimetype" << tMimeType;
91  }
92  }
93  // WorkAround for #199083
94  if (fileName == QLatin1String("text-vcard")) {
95  fileName = QStringLiteral("text-x-vcard");
96  }
97 
98  if (fileName.isEmpty()) {
99  fileName = fallbackFileName1;
100  if (fileName.isEmpty()) {
101  fileName = fallbackFileName2;
102  }
103  if (!fileName.isEmpty()) {
104  fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName();
105  }
106  }
107 
108  return fileName;
109 }
110 
111 QString MimeTreeParser::Util::iconNameForContent(KMime::Content *node)
112 {
113  if (!node) {
114  return {};
115  }
116 
117  auto ct = node->contentType(); // Create
118  QByteArray mimeType = ct->mimeType();
119  if (mimeType.isNull() || mimeType == "application/octet-stream") {
120  const QString fileName = node->contentDisposition()->filename();
121  if (!fileName.isEmpty()) {
122  const QString mime = MimeTreeParser::Util::mimetype(fileName).name();
123  mimeType = mime.toLatin1();
124  }
125  }
127  return MimeTreeParser::Util::iconNameForMimetype(QLatin1String(mimeType), node->contentDisposition()->filename(), ct->name());
128 }
129 
131 {
132  switch (mode) {
133  case Normal: ///< A normal plaintext message, non-multipart
134  return QStringLiteral("Normal PlainText Message, non-multipart");
135  case Html: ///< A HTML message, non-multipart
136  return QStringLiteral("A HTML message, non-multipart");
137  case MultipartPlain: ///< A multipart/alternative message, the plain text part is currently displayed
138  return QStringLiteral("A multipart/alternative message, the plain text part is currently displayed");
139  case MultipartHtml: ///< A multipart/alternative message, the HTML part is currently displayed
140  return QStringLiteral("A multipart/alternative message, the HTML part is currently displayed");
141  case MultipartIcal: ///< A multipart/alternative message, the ICal part is currently displayed
142  return QStringLiteral("A multipart/alternative message, the ICal part is currently displayed");
143  }
144  return {};
145 }
bool isNull() const const
QByteArray toLower() const const
static QString fileName(const KMime::Content *node)
Returns a usable filename for a node, that can be the filename from the content disposition header,...
Content * parent() const
MIMETREEPARSER_EXPORT QString htmlModeToString(Util::HtmlMode mode)
QString asUnicodeString() const override
@ Html
A HTML message, non-multipart.
@ MultipartHtml
A multipart/alternative message, the HTML part is currently displayed.
QByteArray toLatin1() const const
KCALUTILS_EXPORT QString mimeType()
Content * topLevel() const
QByteArray mediaType() const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool isEmpty() const const
The Util namespace contains a collection of helper functions use in various places.
Headers::ContentDisposition * contentDisposition(bool create=true)
HtmlMode
Describes the type of the displayed message.
QString label(StandardShortcut id)
QString toLower() const const
@ MultipartIcal
A multipart/alternative message, the ICal part is currently displayed.
QString name(StandardShortcut id)
QMimeType mimeTypeForFile(const QString &fileName, QMimeDatabase::MatchMode mode) const const
QStringList mimeTypes(Mode mode=Writing)
Headers::ContentType * contentType(bool create=true)
@ Normal
A normal plaintext message, non-multipart.
@ MultipartPlain
A multipart/alternative message, the plain text part is currently displayed.
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const const
Headers::ContentDescription * contentDescription(bool create=true)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Mar 21 2023 04:07:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.