File indexing completed on 2024-10-06 12:54:05

0001 // SPDX-FileCopyrightText: 2015 Klaralvdalens Datakonsult AB
0002 // SPDX-FileCopyrightText: 2016 The Qt Company Ltd.
0003 // SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
0004 // SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
0005 
0006 #pragma once
0007 
0008 #include <QFileInfo>
0009 #include <QMimeDatabase>
0010 #include <QObject>
0011 #include <qqml.h>
0012 
0013 class FileTypeSingletonPrivate;
0014 
0015 /**
0016  * @class FileTypeSingleton
0017  *
0018  * Provide a singleton to expose the functionality of QMimeDatabase.
0019  *
0020  * @sa QMimeDatabase
0021  */
0022 class FileTypeSingleton : public QObject
0023 {
0024     Q_OBJECT
0025 
0026     /**
0027      * @brief List of supported image formats.
0028      *
0029      * Returns a list of file extensions, not MIME types.
0030      */
0031     Q_PROPERTY(QStringList supportedImageFormats READ supportedImageFormats CONSTANT FINAL)
0032 
0033     /**
0034      * @brief List of supported animated image formats.
0035      *
0036      * Returns a list of file extensions, not MIME types.
0037      */
0038     Q_PROPERTY(QStringList supportedAnimatedImageFormats READ supportedAnimatedImageFormats CONSTANT FINAL)
0039 
0040     QML_NAMED_ELEMENT(FileType)
0041     QML_SINGLETON
0042 
0043 public:
0044     explicit FileTypeSingleton(QObject *parent = nullptr);
0045     ~FileTypeSingleton();
0046 
0047     /**
0048      * @brief Returns a MIME type for nameOrAlias or an invalid one if none found.
0049      *
0050      * @sa QMimeDatabase
0051      */
0052     Q_INVOKABLE QMimeType mimeTypeForName(const QString &nameOrAlias) const;
0053 
0054     enum MatchMode { MatchDefault, MatchExtension, MatchContent };
0055     Q_ENUM(MatchMode)
0056 
0057     /**
0058      * @brief Returns a MIME type for the file named fileName using mode.
0059      *
0060      * @sa QMimeDatabase
0061      */
0062     Q_INVOKABLE QMimeType mimeTypeForFile(const QString &fileName, FileTypeSingleton::MatchMode mode = MatchDefault) const;
0063 
0064     /**
0065      * @brief Returns a MIME type for fileInfo.
0066      *
0067      * @sa QMimeDatabase
0068      */
0069     Q_INVOKABLE QMimeType mimeTypeForFile(const QFileInfo &fileInfo, FileTypeSingleton::MatchMode mode = MatchDefault) const;
0070 
0071     /**
0072      * @brief Returns the MIME types for the file name fileName.
0073      *
0074      * @sa QMimeDatabase
0075      */
0076     Q_INVOKABLE QList<QMimeType> mimeTypesForFileName(const QString &fileName) const;
0077 
0078     /**
0079      * @brief Returns a MIME type for data.
0080      *
0081      * @sa QMimeDatabase
0082      */
0083     Q_INVOKABLE QMimeType mimeTypeForData(const QByteArray &data) const;
0084 
0085     /**
0086      * @brief Returns a MIME type for the data in device.
0087      *
0088      * @sa QMimeDatabase
0089      */
0090     Q_INVOKABLE QMimeType mimeTypeForData(QIODevice *device) const;
0091 
0092     /**
0093      * @brief Returns a MIME type for url.
0094      *
0095      * @sa QMimeDatabase
0096      */
0097     Q_INVOKABLE QMimeType mimeTypeForUrl(const QUrl &url) const;
0098 
0099     /**
0100      * @brief Returns a MIME type for the given fileName and device data.
0101      *
0102      * @sa QMimeDatabase
0103      */
0104     Q_INVOKABLE QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const;
0105 
0106     /**
0107      * @brief Returns a MIME type for the given fileName and device data.
0108      *
0109      * @sa QMimeDatabase
0110      */
0111     Q_INVOKABLE QMimeType mimeTypeForFileNameAndData(const QString &fileName, const QByteArray &data) const;
0112 
0113     /**
0114      * @brief Returns the suffix for the file fileName, as known by the MIME database.
0115      *
0116      * @sa QMimeDatabase
0117      */
0118     Q_INVOKABLE QString suffixForFileName(const QString &fileName) const;
0119 
0120     QStringList supportedImageFormats() const;
0121     QStringList supportedAnimatedImageFormats() const;
0122 
0123 private:
0124     const QScopedPointer<FileTypeSingletonPrivate> d_ptr;
0125     Q_DECLARE_PRIVATE(FileTypeSingleton)
0126     Q_DISABLE_COPY(FileTypeSingleton)
0127 };
0128 
0129 QML_DECLARE_TYPE(FileTypeSingleton)