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