Struct qt_core::QMimeDatabase

source ·
#[repr(C)]
pub struct QMimeDatabase { /* private fields */ }
Expand description

The QMimeDatabase class maintains a database of MIME types.

C++ class: QMimeDatabase.

C++ documentation:

The QMimeDatabase class maintains a database of MIME types.

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

QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String(“mime/packages”), QStandardPaths::LocateDirectory);

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”/> </mime-type> </mime-info>

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.

QMimeDatabase db; QMimeType mime = db.mimeTypeForFile(fileName); if (mime.inherits(“text/plain”)) { // The file is plain text, we can display it in a QTextEdit }

Implementations§

source§

impl QMimeDatabase

source

pub unsafe fn all_mime_types(&self) -> CppBox<QListOfQMimeType>

Returns the list of all available MIME types.

Calls C++ function: QList<QMimeType> QMimeDatabase::allMimeTypes() const.

C++ documentation:

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 mimeTypeForXxx() methods for performance reasons.

source

pub unsafe fn mime_type_for_data_q_byte_array( &self, data: impl CastInto<Ref<QByteArray>> ) -> CppBox<QMimeType>

Returns a MIME type for data.

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForData(const QByteArray& data) const.

C++ documentation:

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.

source

pub unsafe fn mime_type_for_data_q_io_device( &self, device: impl CastInto<Ptr<QIODevice>> ) -> CppBox<QMimeType>

Returns a MIME type for the data in device.

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForData(QIODevice* device) const.

C++ documentation:

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.

source

pub unsafe fn mime_type_for_file_q_string_match_mode( &self, file_name: impl CastInto<Ref<QString>>, mode: MatchMode ) -> CppBox<QMimeType>

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

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFile(const QString& fileName, QMimeDatabase::MatchMode mode = …) const.

C++ documentation:

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

This is an overloaded function.

source

pub unsafe fn mime_type_for_file_q_file_info_match_mode( &self, file_info: impl CastInto<Ref<QFileInfo>>, mode: MatchMode ) -> CppBox<QMimeType>

Returns a MIME type for fileInfo.

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFile(const QFileInfo& fileInfo, QMimeDatabase::MatchMode mode = …) const.

C++ documentation:

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() and mimeTypeForData().

source

pub unsafe fn mime_type_for_file_q_string( &self, file_name: impl CastInto<Ref<QString>> ) -> CppBox<QMimeType>

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

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFile(const QString& fileName) const.

C++ documentation:

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

This is an overloaded function.

source

pub unsafe fn mime_type_for_file_q_file_info( &self, file_info: impl CastInto<Ref<QFileInfo>> ) -> CppBox<QMimeType>

Returns a MIME type for fileInfo.

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFile(const QFileInfo& fileInfo) const.

C++ documentation:

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() and mimeTypeForData().

source

pub unsafe fn mime_type_for_file_name_and_data_q_string_q_io_device( &self, file_name: impl CastInto<Ref<QString>>, device: impl CastInto<Ptr<QIODevice>> ) -> CppBox<QMimeType>

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

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString& fileName, QIODevice* device) const.

C++ documentation:

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.

source

pub unsafe fn mime_type_for_file_name_and_data_q_string_q_byte_array( &self, file_name: impl CastInto<Ref<QString>>, data: impl CastInto<Ref<QByteArray>> ) -> CppBox<QMimeType>

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

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForFileNameAndData(const QString& fileName, const QByteArray& data) const.

C++ documentation:

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.

source

pub unsafe fn mime_type_for_name( &self, name_or_alias: impl CastInto<Ref<QString>> ) -> CppBox<QMimeType>

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

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForName(const QString& nameOrAlias) const.

C++ documentation:

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

source

pub unsafe fn mime_type_for_url( &self, url: impl CastInto<Ref<QUrl>> ) -> CppBox<QMimeType>

Returns a MIME type for url.

Calls C++ function: QMimeType QMimeDatabase::mimeTypeForUrl(const QUrl& url) const.

C++ documentation:

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.

source

pub unsafe fn mime_types_for_file_name( &self, file_name: impl CastInto<Ref<QString>> ) -> CppBox<QListOfQMimeType>

Returns the MIME types for the file name fileName.

Calls C++ function: QList<QMimeType> QMimeDatabase::mimeTypesForFileName(const QString& fileName) const.

C++ documentation:

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().

source

pub unsafe fn new() -> CppBox<QMimeDatabase>

Constructs a QMimeDatabase object.

Calls C++ function: [constructor] void QMimeDatabase::QMimeDatabase().

C++ documentation:

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).

source

pub unsafe fn suffix_for_file_name( &self, file_name: impl CastInto<Ref<QString>> ) -> CppBox<QString>

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

Calls C++ function: QString QMimeDatabase::suffixForFileName(const QString& fileName) const.

C++ documentation:

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.

Trait Implementations§

source§

impl CppDeletable for QMimeDatabase

source§

unsafe fn delete(&self)

Destroys the QMimeDatabase object.

Calls C++ function: [destructor] void QMimeDatabase::~QMimeDatabase().

C++ documentation:

Destroys the QMimeDatabase object.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> CastInto<U> for T
where U: CastFrom<T>,

source§

unsafe fn cast_into(self) -> U

Performs the conversion. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> StaticUpcast<T> for T

source§

unsafe fn static_upcast(ptr: Ptr<T>) -> Ptr<T>

Convert type of a const pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.