Messagelib

mimetreeparser/src/utils/util.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
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
17using namespace MimeTreeParser;
18using namespace MimeTreeParser::Util;
19
20bool 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
34QString MimeTreeParser::Util::labelForContent(KMime::Content *node)
35{
36 const QString name = node->contentType()->name();
38 if (label.isEmpty()) {
40 }
41 return label;
42}
43
44QMimeType MimeTreeParser::Util::mimetype(const QString &name)
45{
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() != QLatin1StringView("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
60QString 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 == QLatin1StringView("application/x-vnd.kolab.contact")) {
67 tMimeType = QStringLiteral("text/x-vcard");
68 } else if (mimeType == QLatin1StringView("application/x-vnd.kolab.event")) {
69 tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event");
70 } else if (mimeType == QLatin1StringView("application/x-vnd.kolab.task")) {
71 tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo");
72 } else if (mimeType == QLatin1StringView("application/x-vnd.kolab.journal")) {
73 tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal");
74 } else if (mimeType == QLatin1StringView("application/x-vnd.kolab.note")) {
75 tMimeType = QStringLiteral("application/x-vnd.akonadi.note");
76 } else if (mimeType == QLatin1StringView("image/jpg")) {
77 tMimeType = QStringLiteral("image/jpeg");
78 } else if (mimeType == QLatin1StringView("application/x-pkcs7-signature")) {
79 tMimeType = QStringLiteral("application/pkcs7-signature");
80 } else if (mimeType == QLatin1StringView("message/global")) {
81 tMimeType = QStringLiteral("message/rfc822");
82 } else if (mimeType == QLatin1StringView("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 == QLatin1StringView("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(QLatin1StringView("/tmp/") + fileName).iconName();
113 }
114 }
115
116 return fileName;
117}
118
119QString 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(QLatin1StringView(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}
const Headers::ContentType * contentType() const
Content * parent()
const Headers::ContentDisposition * contentDisposition() const
Content * topLevel()
const Headers::ContentDescription * contentDescription() const
QByteArray mediaType() const
QString asUnicodeString() const override
static QString fileName(const KMime::Content *node)
Returns a usable filename for a node, that can be the filename from the content disposition header,...
KCALUTILS_EXPORT QString mimeType()
QString name(StandardAction id)
QString label(StandardShortcut id)
The Util namespace contains a collection of helper functions use in various places.
MIMETREEPARSER_EXPORT QString htmlModeToString(Util::HtmlMode mode)
HtmlMode
Describes the type of the displayed message.
@ Html
A HTML message, non-multipart.
@ Normal
A normal plaintext message, non-multipart.
@ MultipartPlain
A multipart/alternative message, the plain text part is currently displayed.
@ MultipartIcal
A multipart/alternative message, the ICal part is currently displayed.
@ MultipartHtml
A multipart/alternative message, the HTML part is currently displayed.
QByteArray toLower() const const
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const const
void clear()
bool isEmpty() const const
bool isNull() const const
QByteArray toLatin1() const const
QString toLower() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.