Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QMimeDatabase Class Reference

\inmodule QtCore More...

#include <qmimedatabase.h>

+ Collaboration diagram for QMimeDatabase:

Public Types

enum  MatchMode { MatchDefault = 0x0 , MatchExtension = 0x1 , MatchContent = 0x2 }
 This enum specifies how matching a file to a MIME type is performed. More...
 

Public Member Functions

 QMimeDatabase ()
 Constructs a QMimeDatabase object.
 
 ~QMimeDatabase ()
 Destroys the QMimeDatabase object.
 
QMimeType mimeTypeForName (const QString &nameOrAlias) const
 Returns a MIME type for nameOrAlias or an invalid one if none found.
 
QMimeType mimeTypeForFile (const QString &fileName, MatchMode mode=MatchDefault) const
 Returns a MIME type for the file named fileName using mode.
 
QMimeType mimeTypeForFile (const QFileInfo &fileInfo, MatchMode mode=MatchDefault) const
 Returns a MIME type for fileInfo.
 
QList< QMimeTypemimeTypesForFileName (const QString &fileName) const
 Returns the MIME types for the file name fileName.
 
QMimeType mimeTypeForData (const QByteArray &data) const
 Returns a MIME type for data.
 
QMimeType mimeTypeForData (QIODevice *device) const
 Returns a MIME type for the data in device.
 
QMimeType mimeTypeForUrl (const QUrl &url) const
 Returns a MIME type for url.
 
QMimeType mimeTypeForFileNameAndData (const QString &fileName, QIODevice *device) const
 Returns a MIME type for the given fileName and device data.
 
QMimeType mimeTypeForFileNameAndData (const QString &fileName, const QByteArray &data) const
 Returns a MIME type for the given fileName and device data.
 
QString suffixForFileName (const QString &fileName) const
 Returns the suffix for the file fileName, as known by the MIME database.
 
QList< QMimeTypeallMimeTypes () const
 Returns the list of all available MIME types.
 

Detailed Description

\inmodule QtCore

The QMimeDatabase class maintains a database of MIME types.

Since
5.0

The MIME type database is provided by the freedesktop.org shared-mime-info project. If the MIME type database cannot be found on the system, as is the case on most Windows, \macos, and iOS systems, Qt will use its own copy of it.

Applications which want to define custom MIME types need to install an XML file into the locations searched for MIME definitions. These locations can be queried with

On a typical Unix system, this will be /usr/share/mime/packages/, but it is also possible to extend the list of directories by setting the environment variable XDG_DATA_DIRS. For instance adding /opt/myapp/share to XDG_DATA_DIRS will result in /opt/myapp/share/mime/packages/ being searched for MIME definitions.

Here is an example of MIME XML:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/vnd.qt.qmakeprofile">
<comment xml:lang="en">Qt qmake Profile</comment>
<glob pattern="*.pro" weight="50"/>
\inmodule QtCore
QMimeType mimeTypeForFile(const QString &fileName, MatchMode mode=MatchDefault) const
Returns a MIME type for the file named fileName using mode.
\inmodule QtCore
Definition qmimetype.h:25
static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
[0]
GLuint GLuint GLfloat weight
GLubyte * pattern

For more details about the syntax of XML MIME definitions, including defining "magic" in order to detect MIME types based on data as well, read the Shared Mime Info specification at http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html

On Unix systems, a binary cache is used for more performance. This cache is generated by the command "update-mime-database path", where path would be /opt/myapp/share/mime in the above example. Make sure to run this command when installing the MIME type definition file.

\threadsafe

if (mime.inherits("text/plain")) {
// The file is plain text, we can display it in a QTextEdit
}
See also
QMimeType, {MIME Type Browser}

Definition at line 22 of file qmimedatabase.h.

Member Enumeration Documentation

◆ MatchMode

This enum specifies how matching a file to a MIME type is performed.

\value MatchDefault Both the file name and content are used to look for a match

\value MatchExtension Only the file name is used to look for a match

\value MatchContent The file content is used to look for a match

Enumerator
MatchDefault 
MatchExtension 
MatchContent 

Definition at line 32 of file qmimedatabase.h.

Constructor & Destructor Documentation

◆ QMimeDatabase()

QMimeDatabase::QMimeDatabase ( )

Constructs a QMimeDatabase object.

It is perfectly OK to create an instance of QMimeDatabase every time you need to perform a lookup. The parsing of mimetypes is done on demand (when shared-mime-info is installed) or when the very first instance is constructed (when parsing XML files directly).

Definition at line 573 of file qmimedatabase.cpp.

◆ ~QMimeDatabase()

QMimeDatabase::~QMimeDatabase ( )

Destroys the QMimeDatabase object.

Definition at line 582 of file qmimedatabase.cpp.

Member Function Documentation

◆ allMimeTypes()

QList< QMimeType > QMimeDatabase::allMimeTypes ( ) const

Returns the list of all available MIME types.

This can be useful for showing all MIME types to the user, for instance in a MIME type editor. Do not use unless really necessary in other cases though, prefer using the \l {mimeTypeForData()}{mimeTypeForXxx()} methods for performance reasons.

Definition at line 806 of file qmimedatabase.cpp.

References QMimeDatabasePrivate::allMimeTypes(), and QMimeDatabasePrivate::mutex.

+ Here is the call graph for this function:

◆ mimeTypeForData() [1/2]

QMimeType QMimeDatabase::mimeTypeForData ( const QByteArray & data) const

Returns a MIME type for data.

A valid MIME type is always returned. If data doesn't match any known MIME type data, the default MIME type (application/octet-stream) is returned.

Definition at line 693 of file qmimedatabase.cpp.

References QMimeDatabasePrivate::findByData(), and QMimeDatabasePrivate::mutex.

Referenced by QWasmMediaPlayer::setMedia(), QWasmAudioOutput::setSource(), QWasmVideoOutput::setSource(), and AVFAudioDecoder::setSourceDevice().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mimeTypeForData() [2/2]

QMimeType QMimeDatabase::mimeTypeForData ( QIODevice * device) const

Returns a MIME type for the data in device.

A valid MIME type is always returned. If the data in device doesn't match any known MIME type data, the default MIME type (application/octet-stream) is returned.

Definition at line 708 of file qmimedatabase.cpp.

References device, QMimeDatabasePrivate::mimeTypeForData(), and QMimeDatabasePrivate::mutex.

+ Here is the call graph for this function:

◆ mimeTypeForFile() [1/2]

QMimeType QMimeDatabase::mimeTypeForFile ( const QFileInfo & fileInfo,
MatchMode mode = MatchDefault ) const

Returns a MIME type for fileInfo.

A valid MIME type is always returned.

The default matching algorithm looks at both the file name and the file contents, if necessary. The file extension has priority over the contents, but the contents will be used if the file extension is unknown, or matches multiple MIME types. If fileInfo is a Unix symbolic link, the file that it refers to will be used instead. If the file doesn't match any known pattern or data, the default MIME type (application/octet-stream) is returned.

When mode is set to MatchExtension, only the file name is used, not the file contents. The file doesn't even have to exist. If the file name doesn't match any known pattern, the default MIME type (application/octet-stream) is returned. If multiple MIME types match this file, the first one (alphabetically) is returned.

When mode is set to MatchContent, and the file is readable, only the file contents are used to determine the MIME type. This is equivalent to calling mimeTypeForData with a QFile as input device.

fileInfo may refer to an absolute or relative path.

See also
QMimeType::isDefault(), mimeTypeForData()

Definition at line 626 of file qmimedatabase.cpp.

References QFileInfo::filePath(), QMimeDatabasePrivate::mimeTypeForFile(), and QMimeDatabasePrivate::mutex.

+ Here is the call graph for this function:

◆ mimeTypeForFile() [2/2]

QMimeType QMimeDatabase::mimeTypeForFile ( const QString & fileName,
MatchMode mode = MatchDefault ) const

Returns a MIME type for the file named fileName using mode.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 638 of file qmimedatabase.cpp.

References fileName, MatchExtension, QMimeDatabasePrivate::mimeTypeForFile(), QMimeDatabasePrivate::mimeTypeForFileExtension(), and QMimeDatabasePrivate::mutex.

Referenced by QIcon::addFile(), fileType(), QAbstractFileIconProviderPrivate::getFileType(), QQuickTextDocumentPrivate::load(), mimeTypeForUrl(), QWasmMediaPlayer::setMedia(), and QWasmAudioOutput::setSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mimeTypeForFileNameAndData() [1/2]

QMimeType QMimeDatabase::mimeTypeForFileNameAndData ( const QString & fileName,
const QByteArray & data ) const

Returns a MIME type for the given fileName and device data.

This overload can be useful when the file is remote, and we started to download some of its data. This allows to do full MIME type matching for remote files as well.

A valid MIME type is always returned. If data doesn't match any known MIME type data, the default MIME type (application/octet-stream) is returned.

This method looks at both the file name and the file contents, if necessary. The file extension has priority over the contents, but the contents will be used if the file extension is unknown, or matches multiple MIME types.

Definition at line 787 of file qmimedatabase.cpp.

References directoryMimeType(), fileName, QMimeDatabasePrivate::mimeTypeForFileNameAndData(), QMimeDatabasePrivate::mimeTypeForName(), QMimeDatabasePrivate::mutex, and QIODeviceBase::ReadOnly.

+ Here is the call graph for this function:

◆ mimeTypeForFileNameAndData() [2/2]

QMimeType QMimeDatabase::mimeTypeForFileNameAndData ( const QString & fileName,
QIODevice * device ) const

Returns a MIME type for the given fileName and device data.

This overload can be useful when the file is remote, and we started to download some of its data in a device. This allows to do full MIME type matching for remote files as well.

If the device is not open, it will be opened by this function, and closed after the MIME type detection is completed.

A valid MIME type is always returned. If device data doesn't match any known MIME type data, the default MIME type (application/octet-stream) is returned.

This method looks at both the file name and the file contents, if necessary. The file extension has priority over the contents, but the contents will be used if the file extension is unknown, or matches multiple MIME types.

Definition at line 760 of file qmimedatabase.cpp.

References device, directoryMimeType(), fileName, QMimeDatabasePrivate::mimeTypeForFileNameAndData(), QMimeDatabasePrivate::mimeTypeForName(), and QMimeDatabasePrivate::mutex.

+ Here is the call graph for this function:

◆ mimeTypeForName()

QMimeType QMimeDatabase::mimeTypeForName ( const QString & nameOrAlias) const

Returns a MIME type for nameOrAlias or an invalid one if none found.

Definition at line 591 of file qmimedatabase.cpp.

References QMimeDatabasePrivate::mimeTypeForName(), and QMimeDatabasePrivate::mutex.

Referenced by QMediaFormat::mimeType(), and mimeTypeForUrl().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mimeTypeForUrl()

QMimeType QMimeDatabase::mimeTypeForUrl ( const QUrl & url) const

Returns a MIME type for url.

If the URL is a local file, this calls mimeTypeForFile.

Otherwise the matching is done based on the file name only, except for schemes where file names don't mean much, like HTTP. This method always returns the default mimetype for HTTP URLs, use QNetworkAccessManager to handle HTTP URLs properly.

A valid MIME type is always returned. If url doesn't match any known MIME type data, the default MIME type (application/octet-stream) is returned.

Definition at line 729 of file qmimedatabase.cpp.

References QMimeDatabasePrivate::defaultMimeType(), QUrl::isLocalFile(), MatchExtension, mimeTypeForFile(), mimeTypeForName(), QUrl::path(), QUrl::scheme(), QString::startsWith(), QUrl::toLocalFile(), and url.

Referenced by QAndroidPlatformServices::openUrl(), and QQuickTextDocumentPrivate::writeTo().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mimeTypesForFileName()

QList< QMimeType > QMimeDatabase::mimeTypesForFileName ( const QString & fileName) const

Returns the MIME types for the file name fileName.

If the file name doesn't match any known pattern, an empty list is returned. If multiple MIME types match this file, they are all returned.

This function does not try to open the file. To also use the content when determining the MIME type, use mimeTypeForFile() or mimeTypeForFileNameAndData() instead.

See also
mimeTypeForFile()

Definition at line 662 of file qmimedatabase.cpp.

References fileName, matches(), mime, QMimeDatabasePrivate::mimeTypeForFileName(), QMimeDatabasePrivate::mimeTypeForName(), and QMimeDatabasePrivate::mutex.

Referenced by AndroidContentFileEngine::open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ suffixForFileName()

QString QMimeDatabase::suffixForFileName ( const QString & fileName) const

Returns the suffix for the file fileName, as known by the MIME database.

This allows to pre-select "tar.bz2" for foo.tar.bz2, but still only "txt" for my.file.with.dots.txt.

Definition at line 679 of file qmimedatabase.cpp.

References fileName, QMimeDatabasePrivate::findByFileName(), QMimeGlobMatchResult::m_knownSuffixLength, and QMimeDatabasePrivate::mutex.

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: