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();
37  QString label = name.isEmpty() ? NodeHelper::fileName(node) : 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  } else if (mimeType == QLatin1String("text/x-moz-deleted")) {
83  // Avoid debug warning about unknown mimetype
84  // Bug: 468801
85  // We need to show unknown icon
86  tMimeType.clear();
87  fileName = QStringLiteral("unknown");
88  }
89  QMimeDatabase mimeDb;
90  if (!tMimeType.isEmpty()) {
91  auto mime = mimeDb.mimeTypeForName(tMimeType);
92  if (mime.isValid()) {
93  fileName = mime.iconName();
94  } else {
95  fileName = QStringLiteral("unknown");
96  if (!tMimeType.isEmpty()) {
97  qCWarning(MIMETREEPARSER_LOG) << "unknown mimetype" << tMimeType;
98  }
99  }
100  }
101  // WorkAround for #199083
102  if (fileName == QLatin1String("text-vcard")) {
103  fileName = QStringLiteral("text-x-vcard");
104  }
105 
106  if (fileName.isEmpty()) {
107  fileName = fallbackFileName1;
108  if (fileName.isEmpty()) {
109  fileName = fallbackFileName2;
110  }
111  if (!fileName.isEmpty()) {
112  fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName();
113  }
114  }
115 
116  return fileName;
117 }
118 
119 QString MimeTreeParser::Util::iconNameForContent(KMime::Content *node)
120 {
121  if (!node) {
122  return {};
123  }
124 
125  auto ct = node->contentType(); // Create
126  QByteArray mimeType = ct->mimeType();
127  if (mimeType.isNull() || mimeType == "application/octet-stream") {
128  const QString fileName = node->contentDisposition()->filename();
129  if (!fileName.isEmpty()) {
130  const QString mime = MimeTreeParser::Util::mimetype(fileName).name();
131  mimeType = mime.toLatin1();
132  }
133  }
135  return MimeTreeParser::Util::iconNameForMimetype(QLatin1String(mimeType), node->contentDisposition()->filename(), ct->name());
136 }
137 
139 {
140  switch (mode) {
141  case Normal: ///< A normal plaintext message, non-multipart
142  return QStringLiteral("Normal PlainText Message, non-multipart");
143  case Html: ///< A HTML message, non-multipart
144  return QStringLiteral("A HTML message, non-multipart");
145  case MultipartPlain: ///< A multipart/alternative message, the plain text part is currently displayed
146  return QStringLiteral("A multipart/alternative message, the plain text part is currently displayed");
147  case MultipartHtml: ///< A multipart/alternative message, the HTML part is currently displayed
148  return QStringLiteral("A multipart/alternative message, the HTML part is currently displayed");
149  case MultipartIcal: ///< A multipart/alternative message, the ICal part is currently displayed
150  return QStringLiteral("A multipart/alternative message, the ICal part is currently displayed");
151  }
152  return {};
153 }
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)
void clear()
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.
const char * name(StandardAction 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 Thu Nov 30 2023 03:56:27 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.