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  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();
37  QString label = name.isEmpty() ? NodeHelper::fileName(node) : name;
38  if (label.isEmpty()) {
39  label = node->contentDescription()->asUnicodeString();
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  }
126  mimeType = mimeType.toLower();
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 }
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const const
HtmlMode
Describes the type of the displayed message.
QByteArray toLower() const const
The Util namespace contains a collection of helper functions use in various places.
bool isNull() const const
A HTML message, non-multipart.
Headers::ContentDisposition * contentDisposition(bool create=true)
A multipart/alternative message, the HTML part is currently displayed.
const QLatin1String name
QMimeType mimeTypeForFile(const QString &fileName, QMimeDatabase::MatchMode mode) const const
QString label(StandardShortcut id)
bool isEmpty() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QStringList mimeTypes(Types)
Headers::ContentType * contentType(bool create=true)
MIMETREEPARSER_EXPORT QString htmlModeToString(Util::HtmlMode mode)
Content * topLevel() const
QByteArray toLatin1() 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...
A multipart/alternative message, the ICal part is currently displayed.
Content * parent() const
QByteArray mediaType() const
Headers::ContentDescription * contentDescription(bool create=true)
A normal plaintext message, non-multipart.
QString asUnicodeString() const override
A multipart/alternative message, the plain text part is currently displayed.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.