File indexing completed on 2024-05-12 15:54:49

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef FILEINFO_H
0008 #define FILEINFO_H
0009 
0010 #include <memory>
0011 
0012 #include <QObject>
0013 #include <QUrl>
0014 
0015 struct FileInfoCacheEntry;
0016 
0017 /**
0018  * An object that provides information about a file.
0019  *
0020  * This provides several properties with information about a specified source
0021  * file.
0022  *
0023  * The actual information is cached and shared. This means it is cheap to create
0024  * multiple instances of this and use them in various places. Information
0025  * retrieval happens in a background thread and will not block any other thread.
0026  */
0027 class FileInfo : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     /**
0033      * The status of information retrieval.
0034      */
0035     enum Status {
0036         Initial, ///< Initial state, no source has been provided yet.
0037         Reading, ///< Information retrieval is happening in the background.
0038         Ready, ///< Information retrieval has finished and we have valid data.
0039         Error, ///< Information retrieval failed for some reason.
0040     };
0041     Q_ENUM(Status)
0042 
0043     /**
0044      * The type of file.
0045      */
0046     enum Type {
0047         UnknownType, ///< This is an unknown file, we don't recognise it as anything we can display.
0048         RasterImageType, ///< A static raster image.
0049         VectorImageType, ///< A vector image.
0050         AnimatedImageType, ///< An animated raster image.
0051         VideoType, ///< A video.
0052     };
0053     Q_ENUM(Type)
0054 
0055     FileInfo(QObject *parent = nullptr);
0056     ~FileInfo() override;
0057 
0058     /**
0059      * The URL of the file to check.
0060      *
0061      * Note that only local files are currently supported.
0062      */
0063     Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
0064     QUrl source() const;
0065     void setSource(const QUrl &newSource);
0066     Q_SIGNAL void sourceChanged();
0067 
0068     /**
0069      * The status of information retrieval.
0070      *
0071      * \see Status
0072      */
0073     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
0074     Status status() const;
0075     Q_SIGNAL void statusChanged();
0076 
0077     /**
0078      * The name of the mime type of the file.
0079      *
0080      * If we don't have any information about the file, this will be an empty
0081      * string.
0082      */
0083     Q_PROPERTY(QString mimeType READ mimeType NOTIFY infoChanged)
0084     QString mimeType() const;
0085 
0086     /**
0087      * What type of file this is.
0088      */
0089     Q_PROPERTY(Type type READ type NOTIFY infoChanged)
0090     Type type() const;
0091 
0092     /**
0093      * The width of the file.
0094      *
0095      * This will only be retrieved for files that are not of type Video. In those
0096      * cases, if we have valid information, this will be the width in pixels of
0097      * the file. Otherwise, this will be -1.
0098      */
0099     Q_PROPERTY(int width READ width NOTIFY infoChanged)
0100     int width() const;
0101 
0102     /**
0103      * The height of the file.
0104      *
0105      * This will only be retrieved for files that are not of type Video. In those
0106      * cases, if we have valid information, this will be the height in pixels of
0107      * the file. Otherwise, this will be -1.
0108      */
0109     Q_PROPERTY(int height READ height NOTIFY infoChanged)
0110     int height() const;
0111 
0112     /**
0113      * Emitted whenever we receive valid data about a file.
0114      */
0115     Q_SIGNAL void infoChanged();
0116 
0117 private:
0118     void setStatus(Status newStatus);
0119     void onCacheUpdated(const QUrl &source);
0120 
0121     QUrl m_source;
0122     Status m_status = Initial;
0123     std::shared_ptr<FileInfoCacheEntry> m_info;
0124 };
0125 
0126 #endif // FILEINFO_H