-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQQmlMimeIconsHelper.cpp
201 lines (166 loc) · 8.5 KB
/
QQmlMimeIconsHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "QQmlMimeIconsHelper.h"
#include <QIcon>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QUrl>
QQmlMimeIconsHelper::MetaDataCache QQmlMimeIconsHelper::s_cache;
QQmlMimeIconsHelper::QQmlMimeIconsHelper (QObject * parent) : QObject (parent) { }
QString QQmlMimeIconsHelper::getSvgIconPathForUrl (const QString & url) {
QString ret;
const QString path = QUrl (url).toLocalFile ();
const QFileInfo info (path);
if (info.exists ()) {
if (info.isDir ()) {
ret = s_cache.getSpecialFolderIconForPath (path);
if (ret.isEmpty ()) {
ret = "folder-closed";
}
}
else {
const QMimeType type = s_cache.getMimeTypeForFile (path);
const QString tmp = type.name ();
ret = s_cache.getSvgIconForMimeType (tmp);
if (ret.isEmpty ()) {
ret = "file";
//qWarning () << "No icon for this MIME-type" << tmp;
}
}
}
return QStringLiteral ("qrc:///QtQmlTricks/icons/filetypes/%1.svg").arg (ret);
}
QString QQmlMimeIconsHelper::getThemeIconNameForUrl (const QString & url) {
QString ret;
const QString path = QUrl (url).toLocalFile ();
const QFileInfo info (path);
if (info.exists ()) {
if (info.isDir ()) {
ret = s_cache.getSpecialFolderIconForPath (path);
if (ret.isEmpty ()) {
ret = "folder";
}
}
else {
const QMimeType type = s_cache.getMimeTypeForFile (path);
const QString tmp = type.name ();
ret = s_cache.getThemeIconForMimeType (tmp);
if (ret.isEmpty ()) {
if (QIcon::hasThemeIcon (type.iconName ())) {
s_cache.registerThemeIconForMimeType (tmp, type.iconName ());
}
else if (QIcon::hasThemeIcon (type.genericIconName ())) {
s_cache.registerThemeIconForMimeType (tmp, type.genericIconName ());
}
else {
s_cache.registerThemeIconForMimeType (tmp, "empty");
}
}
ret = s_cache.getThemeIconForMimeType (tmp);
}
}
return ret;
}
void QQmlMimeIconsHelper::MetaDataCache::registerSpecialFolderIconForPath (const QString & path, const QString & icon) {
specialFoldersIconNames.insert (path, icon);
}
void QQmlMimeIconsHelper::MetaDataCache::registerSvgIconForMimeType (const QString & type, const QString & icon) {
svgIconForMimetype.insert (type, icon);
}
void QQmlMimeIconsHelper::MetaDataCache::registerThemeIconForMimeType (const QString & type, const QString & icon) {
themeIconNameForMimeType.insert (type, icon);
}
QMimeType QQmlMimeIconsHelper::MetaDataCache::getMimeTypeForFile (const QString & path) {
return mimeDb.mimeTypeForFile (path);
}
QString QQmlMimeIconsHelper::MetaDataCache::getSpecialFolderIconForPath (const QString & path) {
return specialFoldersIconNames.value (path);
}
QString QQmlMimeIconsHelper::MetaDataCache::getSvgIconForMimeType (const QString & type) {
return svgIconForMimetype.value (type);
}
QString QQmlMimeIconsHelper::MetaDataCache::getThemeIconForMimeType (const QString & type) {
return themeIconNameForMimeType.value (type);
}
QQmlMimeIconsHelper::MetaDataCache::MetaDataCache (void) {
svgIconForMimetype.reserve (100);
specialFoldersIconNames.reserve (20);
themeIconNameForMimeType.reserve (100);
/// register special folders
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::HomeLocation)) {
registerSpecialFolderIconForPath (path, "folder-home");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::DocumentsLocation)) {
registerSpecialFolderIconForPath (path, "folder-documents");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::MusicLocation)) {
registerSpecialFolderIconForPath (path, "folder-music");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::PicturesLocation)) {
registerSpecialFolderIconForPath (path, "folder-images");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::MoviesLocation)) {
registerSpecialFolderIconForPath (path, "folder-videos");
}
foreach (const QString & path, QStandardPaths::standardLocations (QStandardPaths::DownloadLocation)) {
registerSpecialFolderIconForPath (path, "folder-downloads");
}
/// register SVG icons for MIME-types
registerSvgIconForMimeType ("image/png", "image");
registerSvgIconForMimeType ("image/jpeg", "image");
registerSvgIconForMimeType ("image/gif", "image");
registerSvgIconForMimeType ("image/svg", "drawing");
registerSvgIconForMimeType ("image/svg+xml", "drawing");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.graphics", "drawing");
registerSvgIconForMimeType ("audio/mpeg", "sound");
registerSvgIconForMimeType ("audio/x-wav", "sound");
registerSvgIconForMimeType ("audio/midi", "sound");
registerSvgIconForMimeType ("video/mp4", "video");
registerSvgIconForMimeType ("text/x-csrc", "code");
registerSvgIconForMimeType ("text/x-chdr", "code");
registerSvgIconForMimeType ("text/x-c++src", "code");
registerSvgIconForMimeType ("text/x-c++hdr", "code");
registerSvgIconForMimeType ("text/x-qml", "code");
registerSvgIconForMimeType ("text/x-java", "code");
registerSvgIconForMimeType ("text/css", "code");
registerSvgIconForMimeType ("application/javascript", "code");
registerSvgIconForMimeType ("application/xml", "xml");
registerSvgIconForMimeType ("application/x-shellscript", "script");
registerSvgIconForMimeType ("application/x-perl", "script");
registerSvgIconForMimeType ("application/x-object", "binary");
registerSvgIconForMimeType ("application/octet-stream", "binary");
registerSvgIconForMimeType ("application/x-cd-image", "disk-image");
registerSvgIconForMimeType ("application/zip", "archive");
registerSvgIconForMimeType ("application/x-xz-compressed-tar", "archive");
registerSvgIconForMimeType ("application/x-compressed-tar", "archive");
registerSvgIconForMimeType ("application/x-rar", "archive");
registerSvgIconForMimeType ("application/x-rpm", "archive");
registerSvgIconForMimeType ("application/gzip", "archive");
registerSvgIconForMimeType ("application/vnd.debian.binary-package", "archive");
registerSvgIconForMimeType ("application/vnd.android.package-archive", "archive");
registerSvgIconForMimeType ("application/x-7z-compressed", "archive");
registerSvgIconForMimeType ("application/x-bzip-compressed-tar", "archive");
registerSvgIconForMimeType ("text/x-makefile", "text");
registerSvgIconForMimeType ("text/x-log", "text");
registerSvgIconForMimeType ("text/x-theme", "text");
registerSvgIconForMimeType ("text/csv", "text");
registerSvgIconForMimeType ("text/plain", "text");
registerSvgIconForMimeType ("text/vcard", "text");
registerSvgIconForMimeType ("text/markdown", "text");
registerSvgIconForMimeType ("application/json", "text");
registerSvgIconForMimeType ("application/pdf", "pdf");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.text", "document");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document");
registerSvgIconForMimeType ("application/msword", "document");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.spreadsheet", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.ms-excel", "spreadsheet");
registerSvgIconForMimeType ("application/ms-excel", "spreadsheet");
registerSvgIconForMimeType ("application/vnd.oasis.opendocument.presentation", "slideshow");
registerSvgIconForMimeType ("application/vnd.openxmlformats-officedocument.presentationml.presentation", "slideshow");
registerSvgIconForMimeType ("application/vnd.ms-powerpoint", "slideshow");
registerSvgIconForMimeType ("text/html", "webpage");
registerSvgIconForMimeType ("application/sql", "database");
registerSvgIconForMimeType ("application/x-sqlite3", "database");
registerSvgIconForMimeType ("application/x-executable", "executable");
registerSvgIconForMimeType ("application/x-ms-dos-executable", "executable");
}