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