Changeset View
Changeset View
Standalone View
Standalone View
messageviewer/src/utils/mimetype.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2005 Till Adam <adam@kde.org> | ||||
3 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | ||||
4 | | ||||
5 | This program is free software; you can redistribute it and/or modify | ||||
6 | it under the terms of the GNU General Public License as published by | ||||
7 | the Free Software Foundation; either version 2 of the License, or | ||||
8 | (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | GNU General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License along | ||||
16 | with this program; if not, write to the Free Software Foundation, Inc., | ||||
17 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "utils/mimetype.h" | ||||
21 | #include "utils/iconnamecache.h" | ||||
22 | #include "messageviewer_debug.h" | ||||
23 | | ||||
24 | #include <QMimeDatabase> | ||||
25 | | ||||
26 | QMimeType MessageViewer::Util::mimetype(const QString &name) | ||||
27 | { | ||||
28 | QMimeDatabase db; | ||||
29 | // consider the filename if mimetype cannot be found by content-type | ||||
30 | auto mimeTypes = db.mimeTypesForFileName(name); | ||||
31 | foreach (const auto &mt, mimeTypes) { | ||||
32 | if (mt.name() != QLatin1String("application/octet-stream")) { | ||||
33 | return mt; | ||||
34 | } | ||||
35 | } | ||||
36 | | ||||
37 | // consider the attachment's contents if neither the Content-Type header | ||||
38 | // nor the filename give us a clue | ||||
39 | return db.mimeTypeForFile(name); | ||||
40 | } | ||||
41 | | ||||
42 | QString MessageViewer::Util::fileNameForMimetype(const QString &mimeType, int iconSize, | ||||
43 | const QString &fallbackFileName1, | ||||
44 | const QString &fallbackFileName2) | ||||
45 | { | ||||
46 | QString fileName; | ||||
47 | QString tMimeType = mimeType; | ||||
48 | | ||||
49 | // convert non-registered types to registered types | ||||
50 | if (mimeType == QLatin1String("application/x-vnd.kolab.contact")) { | ||||
51 | tMimeType = QStringLiteral("text/x-vcard"); | ||||
52 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.event")) { | ||||
53 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event"); | ||||
54 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.task")) { | ||||
55 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo"); | ||||
56 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.journal")) { | ||||
57 | tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal"); | ||||
58 | } else if (mimeType == QLatin1String("application/x-vnd.kolab.note")) { | ||||
59 | tMimeType = QStringLiteral("application/x-vnd.akonadi.note"); | ||||
60 | } | ||||
61 | QMimeDatabase mimeDb; | ||||
62 | auto mime = mimeDb.mimeTypeForName(tMimeType); | ||||
63 | if (mime.isValid()) { | ||||
64 | fileName = mime.iconName(); | ||||
65 | } else { | ||||
66 | fileName = QStringLiteral("unknown"); | ||||
67 | if (!tMimeType.isEmpty()) { | ||||
68 | qCWarning(MESSAGEVIEWER_LOG) << "unknown mimetype" << tMimeType; | ||||
69 | } | ||||
70 | } | ||||
71 | //WorkAround for #199083 | ||||
72 | if (fileName == QLatin1String("text-vcard")) { | ||||
73 | fileName = QStringLiteral("text-x-vcard"); | ||||
74 | } | ||||
75 | if (fileName.isEmpty()) { | ||||
76 | fileName = fallbackFileName1; | ||||
77 | if (fileName.isEmpty()) { | ||||
78 | fileName = fallbackFileName2; | ||||
79 | } | ||||
80 | if (!fileName.isEmpty()) { | ||||
81 | fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName(); | ||||
82 | } | ||||
83 | } | ||||
84 | | ||||
85 | return IconNameCache::instance()->iconPath(fileName, iconSize); | ||||
86 | } | ||||
87 | No newline at end of file |