diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-23 19:13:13 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-23 19:13:13 +0200 |
commit | b968ea8ed364238c57c3e74cf2c122cb897cfbea (patch) | |
tree | 7a2dca2199906413a2d0b7d075ded0e4d5ffb69f /framework/src/domain/mimetreeparser/otp/util.cpp | |
parent | c1ca732bafc60f5c140ef5516e32bd46503bf68c (diff) | |
download | kube-b968ea8ed364238c57c3e74cf2c122cb897cfbea.tar.gz kube-b968ea8ed364238c57c3e74cf2c122cb897cfbea.zip |
Builds but doesn't link, no formatters yet
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/util.cpp')
-rw-r--r-- | framework/src/domain/mimetreeparser/otp/util.cpp | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/util.cpp b/framework/src/domain/mimetreeparser/otp/util.cpp new file mode 100644 index 00000000..5ca8d828 --- /dev/null +++ b/framework/src/domain/mimetreeparser/otp/util.cpp | |||
@@ -0,0 +1,136 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License along | ||
15 | with this program; if not, write to the Free Software Foundation, Inc., | ||
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
17 | */ | ||
18 | |||
19 | #include "util.h" | ||
20 | |||
21 | #include "mimetreeparser_debug.h" | ||
22 | |||
23 | #include "nodehelper.h" | ||
24 | |||
25 | #include <KMime/Content> | ||
26 | |||
27 | #include <QMimeDatabase> | ||
28 | #include <QString> | ||
29 | |||
30 | using namespace MimeTreeParser::Util; | ||
31 | |||
32 | bool MimeTreeParser::Util::isTypeBlacklisted(KMime::Content *node) | ||
33 | { | ||
34 | const QByteArray mediaTypeLower = node->contentType()->mediaType().toLower(); | ||
35 | bool typeBlacklisted = mediaTypeLower == "multipart"; | ||
36 | if (!typeBlacklisted) { | ||
37 | typeBlacklisted = KMime::isCryptoPart(node); | ||
38 | } | ||
39 | typeBlacklisted = typeBlacklisted || node == node->topLevel(); | ||
40 | const bool firstTextChildOfEncapsulatedMsg = | ||
41 | mediaTypeLower == "text" && | ||
42 | node->contentType()->subType().toLower() == "plain" && | ||
43 | node->parent() && node->parent()->contentType()->mediaType().toLower() == "message"; | ||
44 | return typeBlacklisted || firstTextChildOfEncapsulatedMsg; | ||
45 | } | ||
46 | |||
47 | QString MimeTreeParser::Util::labelForContent(KMime::Content *node) | ||
48 | { | ||
49 | const QString name = node->contentType()->name(); | ||
50 | QString label = name.isEmpty() ? NodeHelper::fileName(node) : name; | ||
51 | if (label.isEmpty()) { | ||
52 | label = node->contentDescription()->asUnicodeString(); | ||
53 | } | ||
54 | return label; | ||
55 | } | ||
56 | |||
57 | QMimeType MimeTreeParser::Util::mimetype(const QString &name) | ||
58 | { | ||
59 | QMimeDatabase db; | ||
60 | // consider the filename if mimetype cannot be found by content-type | ||
61 | const auto mimeTypes = db.mimeTypesForFileName(name); | ||
62 | for (const auto &mt : mimeTypes) { | ||
63 | if (mt.name() != QLatin1String("application/octet-stream")) { | ||
64 | return mt; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | // consider the attachment's contents if neither the Content-Type header | ||
69 | // nor the filename give us a clue | ||
70 | return db.mimeTypeForFile(name); | ||
71 | } | ||
72 | |||
73 | QString MimeTreeParser::Util::iconNameForMimetype(const QString &mimeType, | ||
74 | const QString &fallbackFileName1, | ||
75 | const QString &fallbackFileName2) | ||
76 | { | ||
77 | QString fileName; | ||
78 | QString tMimeType = mimeType; | ||
79 | |||
80 | // convert non-registered types to registered types | ||
81 | if (mimeType == QLatin1String("application/x-vnd.kolab.contact")) { | ||
82 | tMimeType = QStringLiteral("text/x-vcard"); | ||
83 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.event")) { | ||
84 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event"); | ||
85 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.task")) { | ||
86 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo"); | ||
87 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.journal")) { | ||
88 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal"); | ||
89 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.note")) { | ||
90 | tMimeType = QStringLiteral("application/x-vnd.akonadi.note"); | ||
91 | } else if (mimeType == QLatin1String("image/jpg")) { | ||
92 | tMimeType = QStringLiteral("image/jpeg"); | ||
93 | } | ||
94 | QMimeDatabase mimeDb; | ||
95 | auto mime = mimeDb.mimeTypeForName(tMimeType); | ||
96 | if (mime.isValid()) { | ||
97 | fileName = mime.iconName(); | ||
98 | } else { | ||
99 | fileName = QStringLiteral("unknown"); | ||
100 | if (!tMimeType.isEmpty()) { | ||
101 | qCWarning(MIMETREEPARSER_LOG) << "unknown mimetype" << tMimeType; | ||
102 | } | ||
103 | } | ||
104 | //WorkAround for #199083 | ||
105 | if (fileName == QLatin1String("text-vcard")) { | ||
106 | fileName = QStringLiteral("text-x-vcard"); | ||
107 | } | ||
108 | |||
109 | if (fileName.isEmpty()) { | ||
110 | fileName = fallbackFileName1; | ||
111 | if (fileName.isEmpty()) { | ||
112 | fileName = fallbackFileName2; | ||
113 | } | ||
114 | if (!fileName.isEmpty()) { | ||
115 | fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName(); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return fileName; | ||
120 | } | ||
121 | |||
122 | QString MimeTreeParser::Util::iconNameForContent(KMime::Content *node) | ||
123 | { | ||
124 | if (!node) { | ||
125 | return QString(); | ||
126 | } | ||
127 | |||
128 | QByteArray mimeType = node->contentType()->mimeType(); | ||
129 | if (mimeType.isNull() || mimeType == "application/octet-stream") { | ||
130 | const QString mime = mimetype(node->contentDisposition()->filename()).name(); | ||
131 | mimeType = mime.toLatin1(); | ||
132 | } | ||
133 | mimeType = mimeType.toLower(); | ||
134 | return iconNameForMimetype(QLatin1String(mimeType), node->contentDisposition()->filename(), | ||
135 | node->contentType()->name()); | ||
136 | } | ||