| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/mimetypes/qmimetype.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 "qmimetype.h" | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | #ifndef QT_NO_MIMETYPE | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | #include "qmimetype_p.h" | - | ||||||||||||
| 46 | #include "qmimedatabase_p.h" | - | ||||||||||||
| 47 | #include "qmimeprovider_p.h" | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | #include "qmimeglobpattern_p.h" | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | #include <QtCore/QDebug> | - | ||||||||||||
| 52 | #include <QtCore/QLocale> | - | ||||||||||||
| 53 | #include <QtCore/QHashFunctions> | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | #include <memory> | - | ||||||||||||
| 56 | - | |||||||||||||
| 57 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | QMimeTypePrivate::QMimeTypePrivate() | - | ||||||||||||
| 60 | : loaded(false) | - | ||||||||||||
| 61 | {} executed 7974 times by 24 tests: end of blockExecuted by:
| 7974 | ||||||||||||
| 62 | - | |||||||||||||
| 63 | QMimeTypePrivate::QMimeTypePrivate(const QMimeType &other) | - | ||||||||||||
| 64 | : loaded(other.d->loaded), | - | ||||||||||||
| 65 | name(other.d->name), | - | ||||||||||||
| 66 | localeComments(other.d->localeComments), | - | ||||||||||||
| 67 | genericIconName(other.d->genericIconName), | - | ||||||||||||
| 68 | iconName(other.d->iconName), | - | ||||||||||||
| 69 | globPatterns(other.d->globPatterns) | - | ||||||||||||
| 70 | {} never executed: end of block | 0 | ||||||||||||
| 71 | - | |||||||||||||
| 72 | void QMimeTypePrivate::clear() | - | ||||||||||||
| 73 | { | - | ||||||||||||
| 74 | name.clear(); | - | ||||||||||||
| 75 | localeComments.clear(); | - | ||||||||||||
| 76 | genericIconName.clear(); | - | ||||||||||||
| 77 | iconName.clear(); | - | ||||||||||||
| 78 | globPatterns.clear(); | - | ||||||||||||
| 79 | loaded = false; | - | ||||||||||||
| 80 | } executed 3319 times by 1 test: end of blockExecuted by:
| 3319 | ||||||||||||
| 81 | - | |||||||||||||
| 82 | void QMimeTypePrivate::addGlobPattern(const QString &pattern) | - | ||||||||||||
| 83 | { | - | ||||||||||||
| 84 | globPatterns.append(pattern); | - | ||||||||||||
| 85 | } executed 4136 times by 1 test: end of blockExecuted by:
| 4136 | ||||||||||||
| 86 | - | |||||||||||||
| 87 | /*! | - | ||||||||||||
| 88 | \class QMimeType | - | ||||||||||||
| 89 | \inmodule QtCore | - | ||||||||||||
| 90 | \ingroup shared | - | ||||||||||||
| 91 | \brief The QMimeType class describes types of file or data, represented by a MIME type string. | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | \since 5.0 | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | For instance a file named "readme.txt" has the MIME type "text/plain". | - | ||||||||||||
| 96 | The MIME type can be determined from the file name, or from the file | - | ||||||||||||
| 97 | contents, or from both. MIME type determination can also be done on | - | ||||||||||||
| 98 | buffers of data not coming from files. | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | Determining the MIME type of a file can be useful to make sure your | - | ||||||||||||
| 101 | application supports it. It is also useful in file-manager-like applications | - | ||||||||||||
| 102 | or widgets, in order to display an appropriate \l {QMimeType::iconName}{icon} for the file, or even | - | ||||||||||||
| 103 | the descriptive \l {QMimeType::comment()}{comment} in detailed views. | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | To check if a file has the expected MIME type, you should use inherits() | - | ||||||||||||
| 106 | rather than a simple string comparison based on the name(). This is because | - | ||||||||||||
| 107 | MIME types can inherit from each other: for instance a C source file is | - | ||||||||||||
| 108 | a specific type of plain text file, so text/x-csrc inherits text/plain. | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | \sa QMimeDatabase, {MIME Type Browser Example} | - | ||||||||||||
| 111 | */ | - | ||||||||||||
| 112 | - | |||||||||||||
| 113 | /*! | - | ||||||||||||
| 114 | \fn QMimeType &QMimeType::operator=(QMimeType &&other) | - | ||||||||||||
| 115 | - | |||||||||||||
| 116 | Move-assigns \a other to this QMimeType instance. | - | ||||||||||||
| 117 | - | |||||||||||||
| 118 | \since 5.2 | - | ||||||||||||
| 119 | */ | - | ||||||||||||
| 120 | - | |||||||||||||
| 121 | /*! | - | ||||||||||||
| 122 | \fn QMimeType::QMimeType(); | - | ||||||||||||
| 123 | Constructs this QMimeType object initialized with default property values that indicate an invalid MIME type. | - | ||||||||||||
| 124 | */ | - | ||||||||||||
| 125 | QMimeType::QMimeType() : | - | ||||||||||||
| 126 | d(new QMimeTypePrivate()) | - | ||||||||||||
| 127 | { | - | ||||||||||||
| 128 | } executed 40 times by 3 tests: end of blockExecuted by:
| 40 | ||||||||||||
| 129 | - | |||||||||||||
| 130 | /*! | - | ||||||||||||
| 131 | \fn QMimeType::QMimeType(const QMimeType &other); | - | ||||||||||||
| 132 | Constructs this QMimeType object as a copy of \a other. | - | ||||||||||||
| 133 | */ | - | ||||||||||||
| 134 | QMimeType::QMimeType(const QMimeType &other) : | - | ||||||||||||
| 135 | d(other.d) | - | ||||||||||||
| 136 | { | - | ||||||||||||
| 137 | } executed 14717 times by 22 tests: end of blockExecuted by:
| 14717 | ||||||||||||
| 138 | - | |||||||||||||
| 139 | /*! | - | ||||||||||||
| 140 | \fn QMimeType &QMimeType::operator=(const QMimeType &other); | - | ||||||||||||
| 141 | Assigns the data of \a other to this QMimeType object, and returns a reference to this object. | - | ||||||||||||
| 142 | */ | - | ||||||||||||
| 143 | QMimeType &QMimeType::operator=(const QMimeType &other) | - | ||||||||||||
| 144 | { | - | ||||||||||||
| 145 | if (d != other.d)
| 0-4 | ||||||||||||
| 146 | d = other.d; executed 4 times by 1 test: d = other.d;Executed by:
| 4 | ||||||||||||
| 147 | return *this; executed 4 times by 1 test: return *this;Executed by:
| 4 | ||||||||||||
| 148 | } | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | /*! | - | ||||||||||||
| 151 | \fn QMimeType::QMimeType(const QMimeTypePrivate &dd); | - | ||||||||||||
| 152 | Assigns the data of the QMimeTypePrivate \a dd to this QMimeType object, and returns a reference to this object. | - | ||||||||||||
| 153 | \internal | - | ||||||||||||
| 154 | */ | - | ||||||||||||
| 155 | QMimeType::QMimeType(const QMimeTypePrivate &dd) : | - | ||||||||||||
| 156 | d(new QMimeTypePrivate(dd)) | - | ||||||||||||
| 157 | { | - | ||||||||||||
| 158 | } executed 11236 times by 24 tests: end of blockExecuted by:
| 11236 | ||||||||||||
| 159 | - | |||||||||||||
| 160 | /*! | - | ||||||||||||
| 161 | \fn void QMimeType::swap(QMimeType &other); | - | ||||||||||||
| 162 | Swaps QMimeType \a other with this QMimeType object. | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | This operation is very fast and never fails. | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | The swap() method helps with the implementation of assignment | - | ||||||||||||
| 167 | operators in an exception-safe way. For more information consult | - | ||||||||||||
| 168 | \l {http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap} | - | ||||||||||||
| 169 | {More C++ Idioms - Copy-and-swap}. | - | ||||||||||||
| 170 | */ | - | ||||||||||||
| 171 | - | |||||||||||||
| 172 | /*! | - | ||||||||||||
| 173 | \fn QMimeType::~QMimeType(); | - | ||||||||||||
| 174 | Destroys the QMimeType object, and releases the d pointer. | - | ||||||||||||
| 175 | */ | - | ||||||||||||
| 176 | QMimeType::~QMimeType() | - | ||||||||||||
| 177 | { | - | ||||||||||||
| 178 | } | - | ||||||||||||
| 179 | - | |||||||||||||
| 180 | /*! | - | ||||||||||||
| 181 | \fn bool QMimeType::operator==(const QMimeType &other) const; | - | ||||||||||||
| 182 | Returns \c true if \a other equals this QMimeType object, otherwise returns \c false. | - | ||||||||||||
| 183 | The name is the unique identifier for a mimetype, so two mimetypes with | - | ||||||||||||
| 184 | the same name, are equal. | - | ||||||||||||
| 185 | */ | - | ||||||||||||
| 186 | bool QMimeType::operator==(const QMimeType &other) const | - | ||||||||||||
| 187 | { | - | ||||||||||||
| 188 | return d == other.d || d->name == other.d->name; executed 3 times by 1 test: return d == other.d || d->name == other.d->name;Executed by:
| 3 | ||||||||||||
| 189 | } | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | /*! | - | ||||||||||||
| 192 | \since 5.6 | - | ||||||||||||
| 193 | \relates QMimeType | - | ||||||||||||
| 194 | - | |||||||||||||
| 195 | Returns the hash value for \a key, using | - | ||||||||||||
| 196 | \a seed to seed the calculation. | - | ||||||||||||
| 197 | */ | - | ||||||||||||
| 198 | uint qHash(const QMimeType &key, uint seed) Q_DECL_NOTHROW | - | ||||||||||||
| 199 | { | - | ||||||||||||
| 200 | return qHash(key.d->name, seed); never executed: return qHash(key.d->name, seed); | 0 | ||||||||||||
| 201 | } | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | /*! | - | ||||||||||||
| 204 | \fn bool QMimeType::operator!=(const QMimeType &other) const; | - | ||||||||||||
| 205 | Returns \c true if \a other does not equal this QMimeType object, otherwise returns \c false. | - | ||||||||||||
| 206 | */ | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | /*! | - | ||||||||||||
| 209 | \fn bool QMimeType::isValid() const; | - | ||||||||||||
| 210 | Returns \c true if the QMimeType object contains valid data, otherwise returns \c false. | - | ||||||||||||
| 211 | A valid MIME type has a non-empty name(). | - | ||||||||||||
| 212 | The invalid MIME type is the default-constructed QMimeType. | - | ||||||||||||
| 213 | */ | - | ||||||||||||
| 214 | bool QMimeType::isValid() const | - | ||||||||||||
| 215 | { | - | ||||||||||||
| 216 | return !d->name.isEmpty(); executed 6692 times by 23 tests: return !d->name.isEmpty();Executed by:
| 6692 | ||||||||||||
| 217 | } | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | /*! | - | ||||||||||||
| 220 | \fn bool QMimeType::isDefault() const; | - | ||||||||||||
| 221 | Returns \c true if this MIME type is the default MIME type which | - | ||||||||||||
| 222 | applies to all files: application/octet-stream. | - | ||||||||||||
| 223 | */ | - | ||||||||||||
| 224 | bool QMimeType::isDefault() const | - | ||||||||||||
| 225 | { | - | ||||||||||||
| 226 | return d->name == QMimeDatabasePrivate::instance()->defaultMimeType(); executed 18 times by 1 test: return d->name == QMimeDatabasePrivate::instance()->defaultMimeType();Executed by:
| 18 | ||||||||||||
| 227 | } | - | ||||||||||||
| 228 | - | |||||||||||||
| 229 | /*! | - | ||||||||||||
| 230 | \fn QString QMimeType::name() const; | - | ||||||||||||
| 231 | Returns the name of the MIME type. | - | ||||||||||||
| 232 | */ | - | ||||||||||||
| 233 | QString QMimeType::name() const | - | ||||||||||||
| 234 | { | - | ||||||||||||
| 235 | return d->name; executed 12543 times by 3 tests: return d->name;Executed by:
| 12543 | ||||||||||||
| 236 | } | - | ||||||||||||
| 237 | - | |||||||||||||
| 238 | /*! | - | ||||||||||||
| 239 | Returns the description of the MIME type to be displayed on user interfaces. | - | ||||||||||||
| 240 | - | |||||||||||||
| 241 | The default language (QLocale().name()) is used to select the appropriate translation. | - | ||||||||||||
| 242 | */ | - | ||||||||||||
| 243 | QString QMimeType::comment() const | - | ||||||||||||
| 244 | { | - | ||||||||||||
| 245 | QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d); | - | ||||||||||||
| 246 | - | |||||||||||||
| 247 | QStringList languageList; | - | ||||||||||||
| 248 | languageList << QLocale().name(); | - | ||||||||||||
| 249 | languageList << QLocale().uiLanguages(); | - | ||||||||||||
| 250 | for (const QString &language : qAsConst(languageList)) { | - | ||||||||||||
| 251 | const QString lang = language == QLatin1String("C") ? QLatin1String("en_US") : language;
| 4-14 | ||||||||||||
| 252 | const QString comm = d->localeComments.value(lang); | - | ||||||||||||
| 253 | if (!comm.isEmpty())
| 4-14 | ||||||||||||
| 254 | return comm; executed 14 times by 1 test: return comm;Executed by:
| 14 | ||||||||||||
| 255 | const int pos = lang.indexOf(QLatin1Char('_')); | - | ||||||||||||
| 256 | if (pos != -1) {
| 0-4 | ||||||||||||
| 257 | // "pt_BR" not found? try just "pt" | - | ||||||||||||
| 258 | const QString shortLang = lang.left(pos); | - | ||||||||||||
| 259 | const QString commShort = d->localeComments.value(shortLang); | - | ||||||||||||
| 260 | if (!commShort.isEmpty())
| 0-4 | ||||||||||||
| 261 | return commShort; executed 4 times by 1 test: return commShort;Executed by:
| 4 | ||||||||||||
| 262 | } never executed: end of block | 0 | ||||||||||||
| 263 | } never executed: end of block | 0 | ||||||||||||
| 264 | - | |||||||||||||
| 265 | // Use the mimetype name as fallback | - | ||||||||||||
| 266 | return d->name; never executed: return d->name; | 0 | ||||||||||||
| 267 | } | - | ||||||||||||
| 268 | - | |||||||||||||
| 269 | /*! | - | ||||||||||||
| 270 | \fn QString QMimeType::genericIconName() const; | - | ||||||||||||
| 271 | Returns the file name of a generic icon that represents the MIME type. | - | ||||||||||||
| 272 | - | |||||||||||||
| 273 | This should be used if the icon returned by iconName() cannot be found on | - | ||||||||||||
| 274 | the system. It is used for categories of similar types (like spreadsheets | - | ||||||||||||
| 275 | or archives) that can use a common icon. | - | ||||||||||||
| 276 | The freedesktop.org Icon Naming Specification lists a set of such icon names. | - | ||||||||||||
| 277 | - | |||||||||||||
| 278 | The icon name can be given to QIcon::fromTheme() in order to load the icon. | - | ||||||||||||
| 279 | */ | - | ||||||||||||
| 280 | QString QMimeType::genericIconName() const | - | ||||||||||||
| 281 | { | - | ||||||||||||
| 282 | QMimeDatabasePrivate::instance()->provider()->loadGenericIcon(*d); | - | ||||||||||||
| 283 | if (d->genericIconName.isEmpty()) {
| 4-5 | ||||||||||||
| 284 | // From the spec: | - | ||||||||||||
| 285 | // If the generic icon name is empty (not specified by the mimetype definition) | - | ||||||||||||
| 286 | // then the mimetype is used to generate the generic icon by using the top-level | - | ||||||||||||
| 287 | // media type (e.g. "video" in "video/ogg") and appending "-x-generic" | - | ||||||||||||
| 288 | // (i.e. "video-x-generic" in the previous example). | - | ||||||||||||
| 289 | QString group = name(); | - | ||||||||||||
| 290 | const int slashindex = group.indexOf(QLatin1Char('/')); | - | ||||||||||||
| 291 | if (slashindex != -1)
| 0-4 | ||||||||||||
| 292 | group = group.left(slashindex); executed 4 times by 1 test: group = group.left(slashindex);Executed by:
| 4 | ||||||||||||
| 293 | return group + QLatin1String("-x-generic"); executed 4 times by 1 test: return group + QLatin1String("-x-generic");Executed by:
| 4 | ||||||||||||
| 294 | } | - | ||||||||||||
| 295 | return d->genericIconName; executed 5 times by 2 tests: return d->genericIconName;Executed by:
| 5 | ||||||||||||
| 296 | } | - | ||||||||||||
| 297 | - | |||||||||||||
| 298 | /*! | - | ||||||||||||
| 299 | \fn QString QMimeType::iconName() const; | - | ||||||||||||
| 300 | Returns the file name of an icon image that represents the MIME type. | - | ||||||||||||
| 301 | - | |||||||||||||
| 302 | The icon name can be given to QIcon::fromTheme() in order to load the icon. | - | ||||||||||||
| 303 | */ | - | ||||||||||||
| 304 | QString QMimeType::iconName() const | - | ||||||||||||
| 305 | { | - | ||||||||||||
| 306 | QMimeDatabasePrivate::instance()->provider()->loadIcon(*d); | - | ||||||||||||
| 307 | if (d->iconName.isEmpty()) {
| 3-6 | ||||||||||||
| 308 | // Make default icon name from the mimetype name | - | ||||||||||||
| 309 | d->iconName = name(); | - | ||||||||||||
| 310 | const int slashindex = d->iconName.indexOf(QLatin1Char('/')); | - | ||||||||||||
| 311 | if (slashindex != -1)
| 0-6 | ||||||||||||
| 312 | d->iconName[slashindex] = QLatin1Char('-'); executed 6 times by 1 test: d->iconName[slashindex] = QLatin1Char('-');Executed by:
| 6 | ||||||||||||
| 313 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||||||||
| 314 | return d->iconName; executed 9 times by 2 tests: return d->iconName;Executed by:
| 9 | ||||||||||||
| 315 | } | - | ||||||||||||
| 316 | - | |||||||||||||
| 317 | /*! | - | ||||||||||||
| 318 | \fn QStringList QMimeType::globPatterns() const; | - | ||||||||||||
| 319 | Returns the list of glob matching patterns. | - | ||||||||||||
| 320 | */ | - | ||||||||||||
| 321 | QStringList QMimeType::globPatterns() const | - | ||||||||||||
| 322 | { | - | ||||||||||||
| 323 | QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d); | - | ||||||||||||
| 324 | return d->globPatterns; executed 21 times by 2 tests: return d->globPatterns;Executed by:
| 21 | ||||||||||||
| 325 | } | - | ||||||||||||
| 326 | - | |||||||||||||
| 327 | /*! | - | ||||||||||||
| 328 | A type is a subclass of another type if any instance of the first type is | - | ||||||||||||
| 329 | also an instance of the second. For example, all image/svg+xml files are also | - | ||||||||||||
| 330 | text/xml, text/plain and application/octet-stream files. Subclassing is about | - | ||||||||||||
| 331 | the format, rather than the category of the data (for example, there is no | - | ||||||||||||
| 332 | 'generic spreadsheet' class that all spreadsheets inherit from). | - | ||||||||||||
| 333 | Conversely, the parent mimetype of image/svg+xml is text/xml. | - | ||||||||||||
| 334 | - | |||||||||||||
| 335 | A mimetype can have multiple parents. For instance application/x-perl | - | ||||||||||||
| 336 | has two parents: application/x-executable and text/plain. This makes | - | ||||||||||||
| 337 | it possible to both execute perl scripts, and to open them in text editors. | - | ||||||||||||
| 338 | */ | - | ||||||||||||
| 339 | QStringList QMimeType::parentMimeTypes() const | - | ||||||||||||
| 340 | { | - | ||||||||||||
| 341 | return QMimeDatabasePrivate::instance()->provider()->parents(d->name); executed 16 times by 1 test: return QMimeDatabasePrivate::instance()->provider()->parents(d->name);Executed by:
| 16 | ||||||||||||
| 342 | } | - | ||||||||||||
| 343 | - | |||||||||||||
| 344 | static void collectParentMimeTypes(const QString &mime, QStringList &allParents) | - | ||||||||||||
| 345 | { | - | ||||||||||||
| 346 | const QStringList parents = QMimeDatabasePrivate::instance()->provider()->parents(mime); | - | ||||||||||||
| 347 | for (const QString &parent : parents) { | - | ||||||||||||
| 348 | // I would use QSet, but since order matters I better not | - | ||||||||||||
| 349 | if (!allParents.contains(parent))
| 4-24 | ||||||||||||
| 350 | allParents.append(parent); executed 24 times by 1 test: allParents.append(parent);Executed by:
| 24 | ||||||||||||
| 351 | } executed 28 times by 1 test: end of blockExecuted by:
| 28 | ||||||||||||
| 352 | // We want a breadth-first search, so that the least-specific parent (octet-stream) is last | - | ||||||||||||
| 353 | // This means iterating twice, unfortunately. | - | ||||||||||||
| 354 | for (const QString &parent : parents) | - | ||||||||||||
| 355 | collectParentMimeTypes(parent, allParents); executed 28 times by 1 test: collectParentMimeTypes(parent, allParents);Executed by:
| 28 | ||||||||||||
| 356 | } executed 36 times by 1 test: end of blockExecuted by:
| 36 | ||||||||||||
| 357 | - | |||||||||||||
| 358 | /*! | - | ||||||||||||
| 359 | Return all the parent mimetypes of this mimetype, direct and indirect. | - | ||||||||||||
| 360 | This includes the parent(s) of its parent(s), etc. | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | For instance, for image/svg+xml the list would be: | - | ||||||||||||
| 363 | application/xml, text/plain, application/octet-stream. | - | ||||||||||||
| 364 | - | |||||||||||||
| 365 | Note that application/octet-stream is the ultimate parent for all types | - | ||||||||||||
| 366 | of files (but not directories). | - | ||||||||||||
| 367 | */ | - | ||||||||||||
| 368 | QStringList QMimeType::allAncestors() const | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | QStringList allParents; | - | ||||||||||||
| 371 | collectParentMimeTypes(d->name, allParents); | - | ||||||||||||
| 372 | return allParents; executed 8 times by 1 test: return allParents;Executed by:
| 8 | ||||||||||||
| 373 | } | - | ||||||||||||
| 374 | - | |||||||||||||
| 375 | /*! | - | ||||||||||||
| 376 | Return the list of aliases of this mimetype. | - | ||||||||||||
| 377 | - | |||||||||||||
| 378 | For instance, for text/csv, the returned list would be: | - | ||||||||||||
| 379 | text/x-csv, text/x-comma-separated-values. | - | ||||||||||||
| 380 | - | |||||||||||||
| 381 | Note that all QMimeType instances refer to proper mimetypes, | - | ||||||||||||
| 382 | never to aliases directly. | - | ||||||||||||
| 383 | - | |||||||||||||
| 384 | The order of the aliases in the list is undefined. | - | ||||||||||||
| 385 | */ | - | ||||||||||||
| 386 | QStringList QMimeType::aliases() const | - | ||||||||||||
| 387 | { | - | ||||||||||||
| 388 | return QMimeDatabasePrivate::instance()->provider()->listAliases(d->name); executed 8 times by 1 test: return QMimeDatabasePrivate::instance()->provider()->listAliases(d->name);Executed by:
| 8 | ||||||||||||
| 389 | } | - | ||||||||||||
| 390 | - | |||||||||||||
| 391 | /*! | - | ||||||||||||
| 392 | Returns the known suffixes for the MIME type. | - | ||||||||||||
| 393 | No leading dot is included, so for instance this would return "jpg", "jpeg" for image/jpeg. | - | ||||||||||||
| 394 | */ | - | ||||||||||||
| 395 | QStringList QMimeType::suffixes() const | - | ||||||||||||
| 396 | { | - | ||||||||||||
| 397 | QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d); | - | ||||||||||||
| 398 | - | |||||||||||||
| 399 | QStringList result; | - | ||||||||||||
| 400 | for (const QString &pattern : qAsConst(d->globPatterns)) { | - | ||||||||||||
| 401 | // Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP? | - | ||||||||||||
| 402 | if (pattern.startsWith(QLatin1String("*.")) &&
| 4-2466 | ||||||||||||
| 403 | pattern.length() > 2 &&
| 0-2466 | ||||||||||||
| 404 | pattern.indexOf(QLatin1Char('*'), 2) < 0 && pattern.indexOf(QLatin1Char('?'), 2) < 0) {
| 0-2466 | ||||||||||||
| 405 | const QString suffix = pattern.mid(2); | - | ||||||||||||
| 406 | result.append(suffix); | - | ||||||||||||
| 407 | } executed 2466 times by 23 tests: end of blockExecuted by:
| 2466 | ||||||||||||
| 408 | } executed 2470 times by 23 tests: end of blockExecuted by:
| 2470 | ||||||||||||
| 409 | - | |||||||||||||
| 410 | return result; executed 2448 times by 23 tests: return result;Executed by:
| 2448 | ||||||||||||
| 411 | } | - | ||||||||||||
| 412 | - | |||||||||||||
| 413 | /*! | - | ||||||||||||
| 414 | Returns the preferred suffix for the MIME type. | - | ||||||||||||
| 415 | No leading dot is included, so for instance this would return "pdf" for application/pdf. | - | ||||||||||||
| 416 | The return value can be empty, for mime types which do not have any suffixes associated. | - | ||||||||||||
| 417 | */ | - | ||||||||||||
| 418 | QString QMimeType::preferredSuffix() const | - | ||||||||||||
| 419 | { | - | ||||||||||||
| 420 | const QStringList suffixList = suffixes(); | - | ||||||||||||
| 421 | return suffixList.isEmpty() ? QString() : suffixList.at(0); executed 2447 times by 22 tests: return suffixList.isEmpty() ? QString() : suffixList.at(0);Executed by:
| 2447 | ||||||||||||
| 422 | } | - | ||||||||||||
| 423 | - | |||||||||||||
| 424 | /*! | - | ||||||||||||
| 425 | \fn QString QMimeType::filterString() const; | - | ||||||||||||
| 426 | Returns a filter string usable for a file dialog. | - | ||||||||||||
| 427 | */ | - | ||||||||||||
| 428 | QString QMimeType::filterString() const | - | ||||||||||||
| 429 | { | - | ||||||||||||
| 430 | QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d); | - | ||||||||||||
| 431 | QString filter; | - | ||||||||||||
| 432 | - | |||||||||||||
| 433 | if (!d->globPatterns.empty()) {
| 0 | ||||||||||||
| 434 | filter += comment() + QLatin1String(" ("); | - | ||||||||||||
| 435 | for (int i = 0; i < d->globPatterns.size(); ++i) {
| 0 | ||||||||||||
| 436 | if (i != 0)
| 0 | ||||||||||||
| 437 | filter += QLatin1Char(' '); never executed: filter += QLatin1Char(' '); | 0 | ||||||||||||
| 438 | filter += d->globPatterns.at(i); | - | ||||||||||||
| 439 | } never executed: end of block | 0 | ||||||||||||
| 440 | filter += QLatin1Char(')'); | - | ||||||||||||
| 441 | } never executed: end of block | 0 | ||||||||||||
| 442 | - | |||||||||||||
| 443 | return filter; never executed: return filter; | 0 | ||||||||||||
| 444 | } | - | ||||||||||||
| 445 | - | |||||||||||||
| 446 | /*! | - | ||||||||||||
| 447 | \fn bool QMimeType::inherits(const QString &mimeTypeName) const; | - | ||||||||||||
| 448 | Returns \c true if this mimetype is \a mimeTypeName, | - | ||||||||||||
| 449 | or inherits \a mimeTypeName (see parentMimeTypes()), | - | ||||||||||||
| 450 | or \a mimeTypeName is an alias for this mimetype. | - | ||||||||||||
| 451 | */ | - | ||||||||||||
| 452 | bool QMimeType::inherits(const QString &mimeTypeName) const | - | ||||||||||||
| 453 | { | - | ||||||||||||
| 454 | if (d->name == mimeTypeName)
| 4-52 | ||||||||||||
| 455 | return true; executed 4 times by 1 test: return true;Executed by:
| 4 | ||||||||||||
| 456 | return QMimeDatabasePrivate::instance()->inherits(d->name, mimeTypeName); executed 52 times by 1 test: return QMimeDatabasePrivate::instance()->inherits(d->name, mimeTypeName);Executed by:
| 52 | ||||||||||||
| 457 | } | - | ||||||||||||
| 458 | - | |||||||||||||
| 459 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||
| 460 | QDebug operator<<(QDebug debug, const QMimeType &mime) | - | ||||||||||||
| 461 | { | - | ||||||||||||
| 462 | QDebugStateSaver saver(debug); | - | ||||||||||||
| 463 | if (!mime.isValid()) {
| 1 | ||||||||||||
| 464 | debug.nospace() << "QMimeType(invalid)"; | - | ||||||||||||
| 465 | } else { executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 466 | debug.nospace() << "QMimeType(" << mime.name() << ")"; | - | ||||||||||||
| 467 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 468 | return debug; executed 2 times by 1 test: return debug;Executed by:
| 2 | ||||||||||||
| 469 | } | - | ||||||||||||
| 470 | #endif | - | ||||||||||||
| 471 | - | |||||||||||||
| 472 | QT_END_NAMESPACE | - | ||||||||||||
| 473 | - | |||||||||||||
| 474 | #endif // QT_NO_MIMETYPE | - | ||||||||||||
| Source code | Switch to Preprocessed file |