Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimedatabase.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com> | - | ||||||||||||||||||||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
6 | ** | - | ||||||||||||||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||||||||
8 | ** | - | ||||||||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||||||||
17 | ** | - | ||||||||||||||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||||||||
20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||||||||
26 | ** | - | ||||||||||||||||||||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||||||||
30 | ** | - | ||||||||||||||||||||||||
31 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
32 | ** | - | ||||||||||||||||||||||||
33 | ****************************************************************************/ | - | ||||||||||||||||||||||||
34 | - | |||||||||||||||||||||||||
35 | #include <qplatformdefs.h> // always first | - | ||||||||||||||||||||||||
36 | - | |||||||||||||||||||||||||
37 | #ifndef QT_NO_MIMETYPE | - | ||||||||||||||||||||||||
38 | - | |||||||||||||||||||||||||
39 | #include "qmimedatabase.h" | - | ||||||||||||||||||||||||
40 | #include "qmimedatabase_p.h" | - | ||||||||||||||||||||||||
41 | - | |||||||||||||||||||||||||
42 | #include "qmimeprovider_p.h" | - | ||||||||||||||||||||||||
43 | #include "qmimetype_p.h" | - | ||||||||||||||||||||||||
44 | - | |||||||||||||||||||||||||
45 | #include <QtCore/QFile> | - | ||||||||||||||||||||||||
46 | #include <QtCore/QFileInfo> | - | ||||||||||||||||||||||||
47 | #include <QtCore/QSet> | - | ||||||||||||||||||||||||
48 | #include <QtCore/QBuffer> | - | ||||||||||||||||||||||||
49 | #include <QtCore/QUrl> | - | ||||||||||||||||||||||||
50 | #include <QtCore/QStack> | - | ||||||||||||||||||||||||
51 | #include <QtCore/QDebug> | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | #include <algorithm> | - | ||||||||||||||||||||||||
54 | #include <functional> | - | ||||||||||||||||||||||||
55 | - | |||||||||||||||||||||||||
56 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
57 | - | |||||||||||||||||||||||||
58 | Q_GLOBAL_STATIC(QMimeDatabasePrivate, staticQMimeDatabase) executed 4 times by 4 tests: end of block Executed by:
executed 4 times by 4 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 351 times by 3 tests: return &holder.value; Executed by:
| 0-351 | ||||||||||||||||||||||||
59 | - | |||||||||||||||||||||||||
60 | QMimeDatabasePrivate *QMimeDatabasePrivate::instance() | - | ||||||||||||||||||||||||
61 | { | - | ||||||||||||||||||||||||
62 | return staticQMimeDatabase(); executed 206 times by 2 tests: return staticQMimeDatabase(); Executed by:
| 206 | ||||||||||||||||||||||||
63 | } | - | ||||||||||||||||||||||||
64 | - | |||||||||||||||||||||||||
65 | QMimeDatabasePrivate::QMimeDatabasePrivate() | - | ||||||||||||||||||||||||
66 | : m_provider(0), m_defaultMimeType(QLatin1String("application/octet-stream")) | - | ||||||||||||||||||||||||
67 | { | - | ||||||||||||||||||||||||
68 | } executed 4 times by 3 tests: end of block Executed by:
| 4 | ||||||||||||||||||||||||
69 | - | |||||||||||||||||||||||||
70 | QMimeDatabasePrivate::~QMimeDatabasePrivate() | - | ||||||||||||||||||||||||
71 | { | - | ||||||||||||||||||||||||
72 | delete m_provider; | - | ||||||||||||||||||||||||
73 | m_provider = 0; | - | ||||||||||||||||||||||||
74 | } executed 4 times by 4 tests: end of block Executed by:
| 4 | ||||||||||||||||||||||||
75 | - | |||||||||||||||||||||||||
76 | QMimeProviderBase *QMimeDatabasePrivate::provider() | - | ||||||||||||||||||||||||
77 | { | - | ||||||||||||||||||||||||
78 | if (!m_provider) {
| 4-9124 | ||||||||||||||||||||||||
79 | QMimeProviderBase *binaryProvider = new QMimeBinaryProvider(this); | - | ||||||||||||||||||||||||
80 | if (binaryProvider->isValid()) {
| 2 | ||||||||||||||||||||||||
81 | m_provider = binaryProvider; | - | ||||||||||||||||||||||||
82 | } else { executed 2 times by 2 tests: end of block Executed by:
| 2 | ||||||||||||||||||||||||
83 | delete binaryProvider; | - | ||||||||||||||||||||||||
84 | m_provider = new QMimeXMLProvider(this); | - | ||||||||||||||||||||||||
85 | } executed 2 times by 2 tests: end of block Executed by:
| 2 | ||||||||||||||||||||||||
86 | } | - | ||||||||||||||||||||||||
87 | return m_provider; executed 9128 times by 3 tests: return m_provider; Executed by:
| 9128 | ||||||||||||||||||||||||
88 | } | - | ||||||||||||||||||||||||
89 | - | |||||||||||||||||||||||||
90 | void QMimeDatabasePrivate::setProvider(QMimeProviderBase *theProvider) | - | ||||||||||||||||||||||||
91 | { | - | ||||||||||||||||||||||||
92 | delete m_provider; | - | ||||||||||||||||||||||||
93 | m_provider = theProvider; | - | ||||||||||||||||||||||||
94 | } never executed: end of block | 0 | ||||||||||||||||||||||||
95 | - | |||||||||||||||||||||||||
96 | /*! | - | ||||||||||||||||||||||||
97 | \internal | - | ||||||||||||||||||||||||
98 | Returns a MIME type or an invalid one if none found | - | ||||||||||||||||||||||||
99 | */ | - | ||||||||||||||||||||||||
100 | QMimeType QMimeDatabasePrivate::mimeTypeForName(const QString &nameOrAlias) | - | ||||||||||||||||||||||||
101 | { | - | ||||||||||||||||||||||||
102 | return provider()->mimeTypeForName(provider()->resolveAlias(nameOrAlias)); executed 4303 times by 2 tests: return provider()->mimeTypeForName(provider()->resolveAlias(nameOrAlias)); Executed by:
| 4303 | ||||||||||||||||||||||||
103 | } | - | ||||||||||||||||||||||||
104 | - | |||||||||||||||||||||||||
105 | QStringList QMimeDatabasePrivate::mimeTypeForFileName(const QString &fileName, QString *foundSuffix) | - | ||||||||||||||||||||||||
106 | { | - | ||||||||||||||||||||||||
107 | if (fileName.endsWith(QLatin1Char('/')))
| 4-192 | ||||||||||||||||||||||||
108 | return QStringList() << QLatin1String("inode/directory"); executed 4 times by 1 test: return QStringList() << QLatin1String("inode/directory"); Executed by:
| 4 | ||||||||||||||||||||||||
109 | - | |||||||||||||||||||||||||
110 | const QStringList matchingMimeTypes = provider()->findByFileName(QFileInfo(fileName).fileName(), foundSuffix); | - | ||||||||||||||||||||||||
111 | return matchingMimeTypes; executed 192 times by 1 test: return matchingMimeTypes; Executed by:
| 192 | ||||||||||||||||||||||||
112 | } | - | ||||||||||||||||||||||||
113 | - | |||||||||||||||||||||||||
114 | static inline bool isTextFile(const QByteArray &data) | - | ||||||||||||||||||||||||
115 | { | - | ||||||||||||||||||||||||
116 | // UTF16 byte order marks | - | ||||||||||||||||||||||||
117 | static const char bigEndianBOM[] = "\xFE\xFF"; | - | ||||||||||||||||||||||||
118 | static const char littleEndianBOM[] = "\xFF\xFE"; | - | ||||||||||||||||||||||||
119 | if (data.startsWith(bigEndianBOM) || data.startsWith(littleEndianBOM))
| 0-18 | ||||||||||||||||||||||||
120 | return true; never executed: return true; | 0 | ||||||||||||||||||||||||
121 | - | |||||||||||||||||||||||||
122 | // Check the first 32 bytes (see shared-mime spec) | - | ||||||||||||||||||||||||
123 | const char *p = data.constData(); | - | ||||||||||||||||||||||||
124 | const char *e = p + qMin(32, data.size()); | - | ||||||||||||||||||||||||
125 | for ( ; p < e; ++p) {
| 12-138 | ||||||||||||||||||||||||
126 | if ((unsigned char)(*p) < 32 && *p != 9 && *p !=10 && *p != 13)
| 0-132 | ||||||||||||||||||||||||
127 | return false; executed 6 times by 1 test: return false; Executed by:
| 6 | ||||||||||||||||||||||||
128 | } executed 132 times by 1 test: end of block Executed by:
| 132 | ||||||||||||||||||||||||
129 | - | |||||||||||||||||||||||||
130 | return true; executed 12 times by 1 test: return true; Executed by:
| 12 | ||||||||||||||||||||||||
131 | } | - | ||||||||||||||||||||||||
132 | - | |||||||||||||||||||||||||
133 | QMimeType QMimeDatabasePrivate::findByData(const QByteArray &data, int *accuracyPtr) | - | ||||||||||||||||||||||||
134 | { | - | ||||||||||||||||||||||||
135 | if (data.isEmpty()) {
| 0-64 | ||||||||||||||||||||||||
136 | *accuracyPtr = 100; | - | ||||||||||||||||||||||||
137 | return mimeTypeForName(QLatin1String("application/x-zerosize")); never executed: return mimeTypeForName(QLatin1String("application/x-zerosize")); | 0 | ||||||||||||||||||||||||
138 | } | - | ||||||||||||||||||||||||
139 | - | |||||||||||||||||||||||||
140 | *accuracyPtr = 0; | - | ||||||||||||||||||||||||
141 | QMimeType candidate = provider()->findByMagic(data, accuracyPtr); | - | ||||||||||||||||||||||||
142 | - | |||||||||||||||||||||||||
143 | if (candidate.isValid())
| 18-46 | ||||||||||||||||||||||||
144 | return candidate; executed 46 times by 1 test: return candidate; Executed by:
| 46 | ||||||||||||||||||||||||
145 | - | |||||||||||||||||||||||||
146 | if (isTextFile(data)) {
| 6-12 | ||||||||||||||||||||||||
147 | *accuracyPtr = 5; | - | ||||||||||||||||||||||||
148 | return mimeTypeForName(QLatin1String("text/plain")); executed 12 times by 1 test: return mimeTypeForName(QLatin1String("text/plain")); Executed by:
| 12 | ||||||||||||||||||||||||
149 | } | - | ||||||||||||||||||||||||
150 | - | |||||||||||||||||||||||||
151 | return mimeTypeForName(defaultMimeType()); executed 6 times by 1 test: return mimeTypeForName(defaultMimeType()); Executed by:
| 6 | ||||||||||||||||||||||||
152 | } | - | ||||||||||||||||||||||||
153 | - | |||||||||||||||||||||||||
154 | QMimeType QMimeDatabasePrivate::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device, int *accuracyPtr) | - | ||||||||||||||||||||||||
155 | { | - | ||||||||||||||||||||||||
156 | // First, glob patterns are evaluated. If there is a match with max weight, | - | ||||||||||||||||||||||||
157 | // this one is selected and we are done. Otherwise, the file contents are | - | ||||||||||||||||||||||||
158 | // evaluated and the match with the highest value (either a magic priority or | - | ||||||||||||||||||||||||
159 | // a glob pattern weight) is selected. Matching starts from max level (most | - | ||||||||||||||||||||||||
160 | // specific) in both cases, even when there is already a suffix matching candidate. | - | ||||||||||||||||||||||||
161 | *accuracyPtr = 0; | - | ||||||||||||||||||||||||
162 | - | |||||||||||||||||||||||||
163 | // Pass 1) Try to match on the file name | - | ||||||||||||||||||||||||
164 | QStringList candidatesByName = mimeTypeForFileName(fileName); | - | ||||||||||||||||||||||||
165 | if (candidatesByName.count() == 1) {
| 26-32 | ||||||||||||||||||||||||
166 | *accuracyPtr = 100; | - | ||||||||||||||||||||||||
167 | const QMimeType mime = mimeTypeForName(candidatesByName.at(0)); | - | ||||||||||||||||||||||||
168 | if (mime.isValid())
| 0-26 | ||||||||||||||||||||||||
169 | return mime; executed 26 times by 1 test: return mime; Executed by:
| 26 | ||||||||||||||||||||||||
170 | candidatesByName.clear(); | - | ||||||||||||||||||||||||
171 | } never executed: end of block | 0 | ||||||||||||||||||||||||
172 | - | |||||||||||||||||||||||||
173 | // Extension is unknown, or matches multiple mimetypes. | - | ||||||||||||||||||||||||
174 | // Pass 2) Match on content, if we can read the data | - | ||||||||||||||||||||||||
175 | if (device->isOpen()) {
| 6-26 | ||||||||||||||||||||||||
176 | - | |||||||||||||||||||||||||
177 | // Read 16K in one go (QIODEVICE_BUFFERSIZE in qiodevice_p.h). | - | ||||||||||||||||||||||||
178 | // This is much faster than seeking back and forth into QIODevice. | - | ||||||||||||||||||||||||
179 | const QByteArray data = device->peek(16384); | - | ||||||||||||||||||||||||
180 | - | |||||||||||||||||||||||||
181 | int magicAccuracy = 0; | - | ||||||||||||||||||||||||
182 | QMimeType candidateByData(findByData(data, &magicAccuracy)); | - | ||||||||||||||||||||||||
183 | - | |||||||||||||||||||||||||
184 | // Disambiguate conflicting extensions (if magic matching found something) | - | ||||||||||||||||||||||||
185 | if (candidateByData.isValid() && magicAccuracy > 0) {
| 0-26 | ||||||||||||||||||||||||
186 | // "for glob_match in glob_matches:" | - | ||||||||||||||||||||||||
187 | // "if glob_match is subclass or equal to sniffed_type, use glob_match" | - | ||||||||||||||||||||||||
188 | const QString sniffedMime = candidateByData.name(); | - | ||||||||||||||||||||||||
189 | foreach (const QString &m, candidatesByName) { | - | ||||||||||||||||||||||||
190 | if (inherits(m, sniffedMime)) {
| 0 | ||||||||||||||||||||||||
191 | // We have magic + pattern pointing to this, so it's a pretty good match | - | ||||||||||||||||||||||||
192 | *accuracyPtr = 100; | - | ||||||||||||||||||||||||
193 | return mimeTypeForName(m); never executed: return mimeTypeForName(m); | 0 | ||||||||||||||||||||||||
194 | } | - | ||||||||||||||||||||||||
195 | } never executed: end of block | 0 | ||||||||||||||||||||||||
196 | *accuracyPtr = magicAccuracy; | - | ||||||||||||||||||||||||
197 | return candidateByData; executed 26 times by 1 test: return candidateByData; Executed by:
| 26 | ||||||||||||||||||||||||
198 | } | - | ||||||||||||||||||||||||
199 | } never executed: end of block | 0 | ||||||||||||||||||||||||
200 | - | |||||||||||||||||||||||||
201 | if (candidatesByName.count() > 1) {
| 0-6 | ||||||||||||||||||||||||
202 | *accuracyPtr = 20; | - | ||||||||||||||||||||||||
203 | candidatesByName.sort(); // to make it deterministic | - | ||||||||||||||||||||||||
204 | const QMimeType mime = mimeTypeForName(candidatesByName.at(0)); | - | ||||||||||||||||||||||||
205 | if (mime.isValid())
| 0 | ||||||||||||||||||||||||
206 | return mime; never executed: return mime; | 0 | ||||||||||||||||||||||||
207 | } never executed: end of block | 0 | ||||||||||||||||||||||||
208 | - | |||||||||||||||||||||||||
209 | return mimeTypeForName(defaultMimeType()); executed 6 times by 1 test: return mimeTypeForName(defaultMimeType()); Executed by:
| 6 | ||||||||||||||||||||||||
210 | } | - | ||||||||||||||||||||||||
211 | - | |||||||||||||||||||||||||
212 | QList<QMimeType> QMimeDatabasePrivate::allMimeTypes() | - | ||||||||||||||||||||||||
213 | { | - | ||||||||||||||||||||||||
214 | return provider()->allMimeTypes(); executed 10 times by 1 test: return provider()->allMimeTypes(); Executed by:
| 10 | ||||||||||||||||||||||||
215 | } | - | ||||||||||||||||||||||||
216 | - | |||||||||||||||||||||||||
217 | bool QMimeDatabasePrivate::inherits(const QString &mime, const QString &parent) | - | ||||||||||||||||||||||||
218 | { | - | ||||||||||||||||||||||||
219 | const QString resolvedParent = provider()->resolveAlias(parent); | - | ||||||||||||||||||||||||
220 | //Q_ASSERT(provider()->resolveAlias(mime) == mime); | - | ||||||||||||||||||||||||
221 | QStack<QString> toCheck; | - | ||||||||||||||||||||||||
222 | toCheck.push(mime); | - | ||||||||||||||||||||||||
223 | while (!toCheck.isEmpty()) {
| 4-116 | ||||||||||||||||||||||||
224 | const QString current = toCheck.pop(); | - | ||||||||||||||||||||||||
225 | if (current == resolvedParent)
| 48-68 | ||||||||||||||||||||||||
226 | return true; executed 48 times by 1 test: return true; Executed by:
| 48 | ||||||||||||||||||||||||
227 | foreach (const QString &par, provider()->parents(current)) | - | ||||||||||||||||||||||||
228 | toCheck.push(par); executed 68 times by 1 test: toCheck.push(par); Executed by:
| 68 | ||||||||||||||||||||||||
229 | } executed 68 times by 1 test: end of block Executed by:
| 68 | ||||||||||||||||||||||||
230 | return false; executed 4 times by 1 test: return false; Executed by:
| 4 | ||||||||||||||||||||||||
231 | } | - | ||||||||||||||||||||||||
232 | - | |||||||||||||||||||||||||
233 | /*! | - | ||||||||||||||||||||||||
234 | \class QMimeDatabase | - | ||||||||||||||||||||||||
235 | \inmodule QtCore | - | ||||||||||||||||||||||||
236 | \brief The QMimeDatabase class maintains a database of MIME types. | - | ||||||||||||||||||||||||
237 | - | |||||||||||||||||||||||||
238 | \since 5.0 | - | ||||||||||||||||||||||||
239 | - | |||||||||||||||||||||||||
240 | The MIME type database is provided by the freedesktop.org shared-mime-info | - | ||||||||||||||||||||||||
241 | project. If the MIME type database cannot be found on the system, as is the case | - | ||||||||||||||||||||||||
242 | on most Windows, \macos, and iOS systems, Qt will use its own copy of it. | - | ||||||||||||||||||||||||
243 | - | |||||||||||||||||||||||||
244 | Applications which want to define custom MIME types need to install an | - | ||||||||||||||||||||||||
245 | XML file into the locations searched for MIME definitions. | - | ||||||||||||||||||||||||
246 | These locations can be queried with | - | ||||||||||||||||||||||||
247 | \code | - | ||||||||||||||||||||||||
248 | QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("mime/packages"), | - | ||||||||||||||||||||||||
249 | QStandardPaths::LocateDirectory); | - | ||||||||||||||||||||||||
250 | \endcode | - | ||||||||||||||||||||||||
251 | On a typical Unix system, this will be /usr/share/mime/packages/, but it is also | - | ||||||||||||||||||||||||
252 | possible to extend the list of directories by setting the environment variable | - | ||||||||||||||||||||||||
253 | \c XDG_DATA_DIRS. For instance adding /opt/myapp/share to \c XDG_DATA_DIRS will result | - | ||||||||||||||||||||||||
254 | in /opt/myapp/share/mime/packages/ being searched for MIME definitions. | - | ||||||||||||||||||||||||
255 | - | |||||||||||||||||||||||||
256 | Here is an example of MIME XML: | - | ||||||||||||||||||||||||
257 | \code | - | ||||||||||||||||||||||||
258 | <?xml version="1.0" encoding="UTF-8"?> | - | ||||||||||||||||||||||||
259 | <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> | - | ||||||||||||||||||||||||
260 | <mime-type type="application/vnd.qt.qmakeprofile"> | - | ||||||||||||||||||||||||
261 | <comment xml:lang="en">Qt qmake Profile</comment> | - | ||||||||||||||||||||||||
262 | <glob pattern="*.pro" weight="50"/> | - | ||||||||||||||||||||||||
263 | </mime-type> | - | ||||||||||||||||||||||||
264 | </mime-info> | - | ||||||||||||||||||||||||
265 | \endcode | - | ||||||||||||||||||||||||
266 | - | |||||||||||||||||||||||||
267 | For more details about the syntax of XML MIME definitions, including defining | - | ||||||||||||||||||||||||
268 | "magic" in order to detect MIME types based on data as well, read the | - | ||||||||||||||||||||||||
269 | Shared Mime Info specification at | - | ||||||||||||||||||||||||
270 | http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html | - | ||||||||||||||||||||||||
271 | - | |||||||||||||||||||||||||
272 | On Unix systems, a binary cache is used for more performance. This cache is generated | - | ||||||||||||||||||||||||
273 | by the command "update-mime-database path", where path would be /opt/myapp/share/mime | - | ||||||||||||||||||||||||
274 | in the above example. Make sure to run this command when installing the MIME type | - | ||||||||||||||||||||||||
275 | definition file. | - | ||||||||||||||||||||||||
276 | - | |||||||||||||||||||||||||
277 | \threadsafe | - | ||||||||||||||||||||||||
278 | - | |||||||||||||||||||||||||
279 | \snippet code/src_corelib_mimetype_qmimedatabase.cpp 0 | - | ||||||||||||||||||||||||
280 | - | |||||||||||||||||||||||||
281 | \sa QMimeType | - | ||||||||||||||||||||||||
282 | */ | - | ||||||||||||||||||||||||
283 | - | |||||||||||||||||||||||||
284 | /*! | - | ||||||||||||||||||||||||
285 | \fn QMimeDatabase::QMimeDatabase(); | - | ||||||||||||||||||||||||
286 | Constructs a QMimeDatabase object. | - | ||||||||||||||||||||||||
287 | - | |||||||||||||||||||||||||
288 | It is perfectly OK to create an instance of QMimeDatabase every time you need to | - | ||||||||||||||||||||||||
289 | perform a lookup. | - | ||||||||||||||||||||||||
290 | The parsing of mimetypes is done on demand (when shared-mime-info is installed) | - | ||||||||||||||||||||||||
291 | or when the very first instance is constructed (when parsing XML files directly). | - | ||||||||||||||||||||||||
292 | */ | - | ||||||||||||||||||||||||
293 | QMimeDatabase::QMimeDatabase() : | - | ||||||||||||||||||||||||
294 | d(staticQMimeDatabase()) | - | ||||||||||||||||||||||||
295 | { | - | ||||||||||||||||||||||||
296 | } executed 145 times by 2 tests: end of block Executed by:
| 145 | ||||||||||||||||||||||||
297 | - | |||||||||||||||||||||||||
298 | /*! | - | ||||||||||||||||||||||||
299 | \fn QMimeDatabase::~QMimeDatabase(); | - | ||||||||||||||||||||||||
300 | Destroys the QMimeDatabase object. | - | ||||||||||||||||||||||||
301 | */ | - | ||||||||||||||||||||||||
302 | QMimeDatabase::~QMimeDatabase() | - | ||||||||||||||||||||||||
303 | { | - | ||||||||||||||||||||||||
304 | d = 0; | - | ||||||||||||||||||||||||
305 | } executed 145 times by 2 tests: end of block Executed by:
| 145 | ||||||||||||||||||||||||
306 | - | |||||||||||||||||||||||||
307 | /*! | - | ||||||||||||||||||||||||
308 | \fn QMimeType QMimeDatabase::mimeTypeForName(const QString &nameOrAlias) const; | - | ||||||||||||||||||||||||
309 | Returns a MIME type for \a nameOrAlias or an invalid one if none found. | - | ||||||||||||||||||||||||
310 | */ | - | ||||||||||||||||||||||||
311 | QMimeType QMimeDatabase::mimeTypeForName(const QString &nameOrAlias) const | - | ||||||||||||||||||||||||
312 | { | - | ||||||||||||||||||||||||
313 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
314 | - | |||||||||||||||||||||||||
315 | return d->mimeTypeForName(nameOrAlias); executed 4121 times by 2 tests: return d->mimeTypeForName(nameOrAlias); Executed by:
| 4121 | ||||||||||||||||||||||||
316 | } | - | ||||||||||||||||||||||||
317 | - | |||||||||||||||||||||||||
318 | /*! | - | ||||||||||||||||||||||||
319 | Returns a MIME type for \a fileInfo. | - | ||||||||||||||||||||||||
320 | - | |||||||||||||||||||||||||
321 | A valid MIME type is always returned. | - | ||||||||||||||||||||||||
322 | - | |||||||||||||||||||||||||
323 | The default matching algorithm looks at both the file name and the file | - | ||||||||||||||||||||||||
324 | contents, if necessary. The file extension has priority over the contents, | - | ||||||||||||||||||||||||
325 | but the contents will be used if the file extension is unknown, or | - | ||||||||||||||||||||||||
326 | matches multiple MIME types. | - | ||||||||||||||||||||||||
327 | If \a fileInfo is a Unix symbolic link, the file that it refers to | - | ||||||||||||||||||||||||
328 | will be used instead. | - | ||||||||||||||||||||||||
329 | If the file doesn't match any known pattern or data, the default MIME type | - | ||||||||||||||||||||||||
330 | (application/octet-stream) is returned. | - | ||||||||||||||||||||||||
331 | - | |||||||||||||||||||||||||
332 | When \a mode is set to MatchExtension, only the file name is used, not | - | ||||||||||||||||||||||||
333 | the file contents. The file doesn't even have to exist. If the file name | - | ||||||||||||||||||||||||
334 | doesn't match any known pattern, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
335 | is returned. | - | ||||||||||||||||||||||||
336 | If multiple MIME types match this file, the first one (alphabetically) is returned. | - | ||||||||||||||||||||||||
337 | - | |||||||||||||||||||||||||
338 | When \a mode is set to MatchContent, and the file is readable, only the | - | ||||||||||||||||||||||||
339 | file contents are used to determine the MIME type. This is equivalent to | - | ||||||||||||||||||||||||
340 | calling mimeTypeForData with a QFile as input device. | - | ||||||||||||||||||||||||
341 | - | |||||||||||||||||||||||||
342 | \a fileInfo may refer to an absolute or relative path. | - | ||||||||||||||||||||||||
343 | - | |||||||||||||||||||||||||
344 | \sa QMimeType::isDefault(), mimeTypeForData() | - | ||||||||||||||||||||||||
345 | */ | - | ||||||||||||||||||||||||
346 | QMimeType QMimeDatabase::mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const | - | ||||||||||||||||||||||||
347 | { | - | ||||||||||||||||||||||||
348 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
349 | - | |||||||||||||||||||||||||
350 | if (fileInfo.isDir())
| 4-36 | ||||||||||||||||||||||||
351 | return d->mimeTypeForName(QLatin1String("inode/directory")); executed 4 times by 1 test: return d->mimeTypeForName(QLatin1String("inode/directory")); Executed by:
| 4 | ||||||||||||||||||||||||
352 | - | |||||||||||||||||||||||||
353 | QFile file(fileInfo.absoluteFilePath()); | - | ||||||||||||||||||||||||
354 | - | |||||||||||||||||||||||||
355 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||||||||
356 | // Cannot access statBuf.st_mode from the filesystem engine, so we have to stat again. | - | ||||||||||||||||||||||||
357 | // In addition we want to follow symlinks. | - | ||||||||||||||||||||||||
358 | const QByteArray nativeFilePath = QFile::encodeName(file.fileName()); | - | ||||||||||||||||||||||||
359 | QT_STATBUF statBuffer; | - | ||||||||||||||||||||||||
360 | if (QT_STAT(nativeFilePath.constData(), &statBuffer) == 0) {
| 12-24 | ||||||||||||||||||||||||
361 | if (S_ISCHR(statBuffer.st_mode))
| 0-24 | ||||||||||||||||||||||||
362 | return d->mimeTypeForName(QLatin1String("inode/chardevice")); never executed: return d->mimeTypeForName(QLatin1String("inode/chardevice")); | 0 | ||||||||||||||||||||||||
363 | if (S_ISBLK(statBuffer.st_mode))
| 0-24 | ||||||||||||||||||||||||
364 | return d->mimeTypeForName(QLatin1String("inode/blockdevice")); never executed: return d->mimeTypeForName(QLatin1String("inode/blockdevice")); | 0 | ||||||||||||||||||||||||
365 | if (S_ISFIFO(statBuffer.st_mode))
| 4-20 | ||||||||||||||||||||||||
366 | return d->mimeTypeForName(QLatin1String("inode/fifo")); executed 4 times by 1 test: return d->mimeTypeForName(QLatin1String("inode/fifo")); Executed by:
| 4 | ||||||||||||||||||||||||
367 | if (S_ISSOCK(statBuffer.st_mode))
| 0-20 | ||||||||||||||||||||||||
368 | return d->mimeTypeForName(QLatin1String("inode/socket")); never executed: return d->mimeTypeForName(QLatin1String("inode/socket")); | 0 | ||||||||||||||||||||||||
369 | } executed 20 times by 1 test: end of block Executed by:
| 20 | ||||||||||||||||||||||||
370 | #endif | - | ||||||||||||||||||||||||
371 | - | |||||||||||||||||||||||||
372 | int priority = 0; | - | ||||||||||||||||||||||||
373 | switch (mode) { | - | ||||||||||||||||||||||||
374 | case MatchDefault: executed 28 times by 1 test: case MatchDefault: Executed by:
| 28 | ||||||||||||||||||||||||
375 | file.open(QIODevice::ReadOnly); // isOpen() will be tested by method below | - | ||||||||||||||||||||||||
376 | return d->mimeTypeForFileNameAndData(fileInfo.absoluteFilePath(), &file, &priority); executed 28 times by 1 test: return d->mimeTypeForFileNameAndData(fileInfo.absoluteFilePath(), &file, &priority); Executed by:
| 28 | ||||||||||||||||||||||||
377 | case MatchExtension: never executed: case MatchExtension: | 0 | ||||||||||||||||||||||||
378 | locker.unlock(); | - | ||||||||||||||||||||||||
379 | return mimeTypeForFile(fileInfo.absoluteFilePath(), mode); never executed: return mimeTypeForFile(fileInfo.absoluteFilePath(), mode); | 0 | ||||||||||||||||||||||||
380 | case MatchContent: executed 4 times by 1 test: case MatchContent: Executed by:
| 4 | ||||||||||||||||||||||||
381 | if (file.open(QIODevice::ReadOnly)) {
| 0-4 | ||||||||||||||||||||||||
382 | locker.unlock(); | - | ||||||||||||||||||||||||
383 | return mimeTypeForData(&file); executed 4 times by 1 test: return mimeTypeForData(&file); Executed by:
| 4 | ||||||||||||||||||||||||
384 | } else { | - | ||||||||||||||||||||||||
385 | return d->mimeTypeForName(d->defaultMimeType()); never executed: return d->mimeTypeForName(d->defaultMimeType()); | 0 | ||||||||||||||||||||||||
386 | } | - | ||||||||||||||||||||||||
387 | default: never executed: default: | 0 | ||||||||||||||||||||||||
388 | Q_ASSERT(false); | - | ||||||||||||||||||||||||
389 | } never executed: end of block | 0 | ||||||||||||||||||||||||
390 | return d->mimeTypeForName(d->defaultMimeType()); never executed: return d->mimeTypeForName(d->defaultMimeType()); | 0 | ||||||||||||||||||||||||
391 | } | - | ||||||||||||||||||||||||
392 | - | |||||||||||||||||||||||||
393 | /*! | - | ||||||||||||||||||||||||
394 | Returns a MIME type for the file named \a fileName using \a mode. | - | ||||||||||||||||||||||||
395 | - | |||||||||||||||||||||||||
396 | \overload | - | ||||||||||||||||||||||||
397 | */ | - | ||||||||||||||||||||||||
398 | QMimeType QMimeDatabase::mimeTypeForFile(const QString &fileName, MatchMode mode) const | - | ||||||||||||||||||||||||
399 | { | - | ||||||||||||||||||||||||
400 | if (mode == MatchExtension) {
| 40-68 | ||||||||||||||||||||||||
401 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
402 | QStringList matches = d->mimeTypeForFileName(fileName); | - | ||||||||||||||||||||||||
403 | const int matchCount = matches.count(); | - | ||||||||||||||||||||||||
404 | if (matchCount == 0) {
| 16-52 | ||||||||||||||||||||||||
405 | return d->mimeTypeForName(d->defaultMimeType()); executed 16 times by 1 test: return d->mimeTypeForName(d->defaultMimeType()); Executed by:
| 16 | ||||||||||||||||||||||||
406 | } else if (matchCount == 1) {
| 0-52 | ||||||||||||||||||||||||
407 | return d->mimeTypeForName(matches.first()); executed 52 times by 1 test: return d->mimeTypeForName(matches.first()); Executed by:
| 52 | ||||||||||||||||||||||||
408 | } else { | - | ||||||||||||||||||||||||
409 | // We have to pick one. | - | ||||||||||||||||||||||||
410 | matches.sort(); // Make it deterministic | - | ||||||||||||||||||||||||
411 | return d->mimeTypeForName(matches.first()); never executed: return d->mimeTypeForName(matches.first()); | 0 | ||||||||||||||||||||||||
412 | } | - | ||||||||||||||||||||||||
413 | } else { | - | ||||||||||||||||||||||||
414 | // Implemented as a wrapper around mimeTypeForFile(QFileInfo), so no mutex. | - | ||||||||||||||||||||||||
415 | QFileInfo fileInfo(fileName); | - | ||||||||||||||||||||||||
416 | return mimeTypeForFile(fileInfo, mode); executed 40 times by 1 test: return mimeTypeForFile(fileInfo, mode); Executed by:
| 40 | ||||||||||||||||||||||||
417 | } | - | ||||||||||||||||||||||||
418 | } | - | ||||||||||||||||||||||||
419 | - | |||||||||||||||||||||||||
420 | /*! | - | ||||||||||||||||||||||||
421 | Returns the MIME types for the file name \a fileName. | - | ||||||||||||||||||||||||
422 | - | |||||||||||||||||||||||||
423 | If the file name doesn't match any known pattern, an empty list is returned. | - | ||||||||||||||||||||||||
424 | If multiple MIME types match this file, they are all returned. | - | ||||||||||||||||||||||||
425 | - | |||||||||||||||||||||||||
426 | This function does not try to open the file. To also use the content | - | ||||||||||||||||||||||||
427 | when determining the MIME type, use mimeTypeForFile() or | - | ||||||||||||||||||||||||
428 | mimeTypeForFileNameAndData() instead. | - | ||||||||||||||||||||||||
429 | - | |||||||||||||||||||||||||
430 | \sa mimeTypeForFile() | - | ||||||||||||||||||||||||
431 | */ | - | ||||||||||||||||||||||||
432 | QList<QMimeType> QMimeDatabase::mimeTypesForFileName(const QString &fileName) const | - | ||||||||||||||||||||||||
433 | { | - | ||||||||||||||||||||||||
434 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
435 | - | |||||||||||||||||||||||||
436 | QStringList matches = d->mimeTypeForFileName(fileName); | - | ||||||||||||||||||||||||
437 | QList<QMimeType> mimes; | - | ||||||||||||||||||||||||
438 | matches.sort(); // Make it deterministic | - | ||||||||||||||||||||||||
439 | mimes.reserve(matches.count()); | - | ||||||||||||||||||||||||
440 | foreach (const QString &mime, matches) | - | ||||||||||||||||||||||||
441 | mimes.append(d->mimeTypeForName(mime)); executed 52 times by 1 test: mimes.append(d->mimeTypeForName(mime)); Executed by:
| 52 | ||||||||||||||||||||||||
442 | return mimes; executed 54 times by 1 test: return mimes; Executed by:
| 54 | ||||||||||||||||||||||||
443 | } | - | ||||||||||||||||||||||||
444 | /*! | - | ||||||||||||||||||||||||
445 | Returns the suffix for the file \a fileName, as known by the MIME database. | - | ||||||||||||||||||||||||
446 | - | |||||||||||||||||||||||||
447 | This allows to pre-select "tar.bz2" for foo.tar.bz2, but still only | - | ||||||||||||||||||||||||
448 | "txt" for my.file.with.dots.txt. | - | ||||||||||||||||||||||||
449 | */ | - | ||||||||||||||||||||||||
450 | QString QMimeDatabase::suffixForFileName(const QString &fileName) const | - | ||||||||||||||||||||||||
451 | { | - | ||||||||||||||||||||||||
452 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
453 | QString foundSuffix; | - | ||||||||||||||||||||||||
454 | d->mimeTypeForFileName(fileName, &foundSuffix); | - | ||||||||||||||||||||||||
455 | return foundSuffix; executed 16 times by 1 test: return foundSuffix; Executed by:
| 16 | ||||||||||||||||||||||||
456 | } | - | ||||||||||||||||||||||||
457 | - | |||||||||||||||||||||||||
458 | /*! | - | ||||||||||||||||||||||||
459 | Returns a MIME type for \a data. | - | ||||||||||||||||||||||||
460 | - | |||||||||||||||||||||||||
461 | A valid MIME type is always returned. If \a data doesn't match any | - | ||||||||||||||||||||||||
462 | known MIME type data, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
463 | is returned. | - | ||||||||||||||||||||||||
464 | */ | - | ||||||||||||||||||||||||
465 | QMimeType QMimeDatabase::mimeTypeForData(const QByteArray &data) const | - | ||||||||||||||||||||||||
466 | { | - | ||||||||||||||||||||||||
467 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
468 | - | |||||||||||||||||||||||||
469 | int accuracy = 0; | - | ||||||||||||||||||||||||
470 | return d->findByData(data, &accuracy); executed 10 times by 1 test: return d->findByData(data, &accuracy); Executed by:
| 10 | ||||||||||||||||||||||||
471 | } | - | ||||||||||||||||||||||||
472 | - | |||||||||||||||||||||||||
473 | /*! | - | ||||||||||||||||||||||||
474 | Returns a MIME type for the data in \a device. | - | ||||||||||||||||||||||||
475 | - | |||||||||||||||||||||||||
476 | A valid MIME type is always returned. If the data in \a device doesn't match any | - | ||||||||||||||||||||||||
477 | known MIME type data, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
478 | is returned. | - | ||||||||||||||||||||||||
479 | */ | - | ||||||||||||||||||||||||
480 | QMimeType QMimeDatabase::mimeTypeForData(QIODevice *device) const | - | ||||||||||||||||||||||||
481 | { | - | ||||||||||||||||||||||||
482 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
483 | - | |||||||||||||||||||||||||
484 | int accuracy = 0; | - | ||||||||||||||||||||||||
485 | const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
| 4-18 | ||||||||||||||||||||||||
486 | if (device->isOpen()) {
| 4-28 | ||||||||||||||||||||||||
487 | // Read 16K in one go (QIODEVICE_BUFFERSIZE in qiodevice_p.h). | - | ||||||||||||||||||||||||
488 | // This is much faster than seeking back and forth into QIODevice. | - | ||||||||||||||||||||||||
489 | const QByteArray data = device->peek(16384); | - | ||||||||||||||||||||||||
490 | const QMimeType result = d->findByData(data, &accuracy); | - | ||||||||||||||||||||||||
491 | if (openedByUs)
| 14 | ||||||||||||||||||||||||
492 | device->close(); executed 14 times by 1 test: device->close(); Executed by:
| 14 | ||||||||||||||||||||||||
493 | return result; executed 28 times by 1 test: return result; Executed by:
| 28 | ||||||||||||||||||||||||
494 | } | - | ||||||||||||||||||||||||
495 | return d->mimeTypeForName(d->defaultMimeType()); executed 4 times by 1 test: return d->mimeTypeForName(d->defaultMimeType()); Executed by:
| 4 | ||||||||||||||||||||||||
496 | } | - | ||||||||||||||||||||||||
497 | - | |||||||||||||||||||||||||
498 | /*! | - | ||||||||||||||||||||||||
499 | Returns a MIME type for \a url. | - | ||||||||||||||||||||||||
500 | - | |||||||||||||||||||||||||
501 | If the URL is a local file, this calls mimeTypeForFile. | - | ||||||||||||||||||||||||
502 | - | |||||||||||||||||||||||||
503 | Otherwise the matching is done based on the file name only, | - | ||||||||||||||||||||||||
504 | except for schemes where file names don't mean much, like HTTP. | - | ||||||||||||||||||||||||
505 | This method always returns the default mimetype for HTTP URLs, | - | ||||||||||||||||||||||||
506 | use QNetworkAccessManager to handle HTTP URLs properly. | - | ||||||||||||||||||||||||
507 | - | |||||||||||||||||||||||||
508 | A valid MIME type is always returned. If \a url doesn't match any | - | ||||||||||||||||||||||||
509 | known MIME type data, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
510 | is returned. | - | ||||||||||||||||||||||||
511 | */ | - | ||||||||||||||||||||||||
512 | QMimeType QMimeDatabase::mimeTypeForUrl(const QUrl &url) const | - | ||||||||||||||||||||||||
513 | { | - | ||||||||||||||||||||||||
514 | if (url.isLocalFile())
| 0-10 | ||||||||||||||||||||||||
515 | return mimeTypeForFile(url.toLocalFile()); never executed: return mimeTypeForFile(url.toLocalFile()); | 0 | ||||||||||||||||||||||||
516 | - | |||||||||||||||||||||||||
517 | const QString scheme = url.scheme(); | - | ||||||||||||||||||||||||
518 | if (scheme.startsWith(QLatin1String("http")) || scheme == QLatin1String("mailto"))
| 2-8 | ||||||||||||||||||||||||
519 | return mimeTypeForName(d->defaultMimeType()); executed 6 times by 1 test: return mimeTypeForName(d->defaultMimeType()); Executed by:
| 6 | ||||||||||||||||||||||||
520 | - | |||||||||||||||||||||||||
521 | return mimeTypeForFile(url.path()); executed 4 times by 1 test: return mimeTypeForFile(url.path()); Executed by:
| 4 | ||||||||||||||||||||||||
522 | } | - | ||||||||||||||||||||||||
523 | - | |||||||||||||||||||||||||
524 | /*! | - | ||||||||||||||||||||||||
525 | Returns a MIME type for the given \a fileName and \a device data. | - | ||||||||||||||||||||||||
526 | - | |||||||||||||||||||||||||
527 | This overload can be useful when the file is remote, and we started to | - | ||||||||||||||||||||||||
528 | download some of its data in a device. This allows to do full MIME type | - | ||||||||||||||||||||||||
529 | matching for remote files as well. | - | ||||||||||||||||||||||||
530 | - | |||||||||||||||||||||||||
531 | If the device is not open, it will be opened by this function, and closed | - | ||||||||||||||||||||||||
532 | after the MIME type detection is completed. | - | ||||||||||||||||||||||||
533 | - | |||||||||||||||||||||||||
534 | A valid MIME type is always returned. If \a device data doesn't match any | - | ||||||||||||||||||||||||
535 | known MIME type data, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
536 | is returned. | - | ||||||||||||||||||||||||
537 | - | |||||||||||||||||||||||||
538 | This method looks at both the file name and the file contents, | - | ||||||||||||||||||||||||
539 | if necessary. The file extension has priority over the contents, | - | ||||||||||||||||||||||||
540 | but the contents will be used if the file extension is unknown, or | - | ||||||||||||||||||||||||
541 | matches multiple MIME types. | - | ||||||||||||||||||||||||
542 | */ | - | ||||||||||||||||||||||||
543 | QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const | - | ||||||||||||||||||||||||
544 | { | - | ||||||||||||||||||||||||
545 | int accuracy = 0; | - | ||||||||||||||||||||||||
546 | const bool openedByUs = !device->isOpen() && device->open(QIODevice::ReadOnly);
| 0-10 | ||||||||||||||||||||||||
547 | const QMimeType result = d->mimeTypeForFileNameAndData(fileName, device, &accuracy); | - | ||||||||||||||||||||||||
548 | if (openedByUs)
| 10 | ||||||||||||||||||||||||
549 | device->close(); executed 10 times by 1 test: device->close(); Executed by:
| 10 | ||||||||||||||||||||||||
550 | return result; executed 20 times by 1 test: return result; Executed by:
| 20 | ||||||||||||||||||||||||
551 | } | - | ||||||||||||||||||||||||
552 | - | |||||||||||||||||||||||||
553 | /*! | - | ||||||||||||||||||||||||
554 | Returns a MIME type for the given \a fileName and device \a data. | - | ||||||||||||||||||||||||
555 | - | |||||||||||||||||||||||||
556 | This overload can be useful when the file is remote, and we started to | - | ||||||||||||||||||||||||
557 | download some of its data. This allows to do full MIME type matching for | - | ||||||||||||||||||||||||
558 | remote files as well. | - | ||||||||||||||||||||||||
559 | - | |||||||||||||||||||||||||
560 | A valid MIME type is always returned. If \a data doesn't match any | - | ||||||||||||||||||||||||
561 | known MIME type data, the default MIME type (application/octet-stream) | - | ||||||||||||||||||||||||
562 | is returned. | - | ||||||||||||||||||||||||
563 | - | |||||||||||||||||||||||||
564 | This method looks at both the file name and the file contents, | - | ||||||||||||||||||||||||
565 | if necessary. The file extension has priority over the contents, | - | ||||||||||||||||||||||||
566 | but the contents will be used if the file extension is unknown, or | - | ||||||||||||||||||||||||
567 | matches multiple MIME types. | - | ||||||||||||||||||||||||
568 | */ | - | ||||||||||||||||||||||||
569 | QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const | - | ||||||||||||||||||||||||
570 | { | - | ||||||||||||||||||||||||
571 | QBuffer buffer(const_cast<QByteArray *>(&data)); | - | ||||||||||||||||||||||||
572 | buffer.open(QIODevice::ReadOnly); | - | ||||||||||||||||||||||||
573 | int accuracy = 0; | - | ||||||||||||||||||||||||
574 | return d->mimeTypeForFileNameAndData(fileName, &buffer, &accuracy); executed 10 times by 1 test: return d->mimeTypeForFileNameAndData(fileName, &buffer, &accuracy); Executed by:
| 10 | ||||||||||||||||||||||||
575 | } | - | ||||||||||||||||||||||||
576 | - | |||||||||||||||||||||||||
577 | /*! | - | ||||||||||||||||||||||||
578 | Returns the list of all available MIME types. | - | ||||||||||||||||||||||||
579 | - | |||||||||||||||||||||||||
580 | This can be useful for showing all MIME types to the user, for instance | - | ||||||||||||||||||||||||
581 | in a MIME type editor. Do not use unless really necessary in other cases | - | ||||||||||||||||||||||||
582 | though, prefer using the \l {mimeTypeForData()}{mimeTypeForXxx()} methods for performance reasons. | - | ||||||||||||||||||||||||
583 | */ | - | ||||||||||||||||||||||||
584 | QList<QMimeType> QMimeDatabase::allMimeTypes() const | - | ||||||||||||||||||||||||
585 | { | - | ||||||||||||||||||||||||
586 | QMutexLocker locker(&d->mutex); | - | ||||||||||||||||||||||||
587 | - | |||||||||||||||||||||||||
588 | return d->allMimeTypes(); executed 10 times by 1 test: return d->allMimeTypes(); Executed by:
| 10 | ||||||||||||||||||||||||
589 | } | - | ||||||||||||||||||||||||
590 | - | |||||||||||||||||||||||||
591 | /*! | - | ||||||||||||||||||||||||
592 | \enum QMimeDatabase::MatchMode | - | ||||||||||||||||||||||||
593 | - | |||||||||||||||||||||||||
594 | This enum specifies how matching a file to a MIME type is performed. | - | ||||||||||||||||||||||||
595 | - | |||||||||||||||||||||||||
596 | \value MatchDefault Both the file name and content are used to look for a match | - | ||||||||||||||||||||||||
597 | - | |||||||||||||||||||||||||
598 | \value MatchExtension Only the file name is used to look for a match | - | ||||||||||||||||||||||||
599 | - | |||||||||||||||||||||||||
600 | \value MatchContent The file content is used to look for a match | - | ||||||||||||||||||||||||
601 | */ | - | ||||||||||||||||||||||||
602 | - | |||||||||||||||||||||||||
603 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
604 | - | |||||||||||||||||||||||||
605 | #endif // QT_NO_MIMETYPE | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |