File indexing completed on 2024-05-19 04:23:46

0001 // SPDX-FileCopyrightText: 2012-2020 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 
0006 #ifndef KPABASE_FILENAME_H
0007 #define KPABASE_FILENAME_H
0008 
0009 #include <QSet>
0010 #include <QString>
0011 #include <QUrl>
0012 #include <QtCore/qmetatype.h>
0013 
0014 namespace DB
0015 {
0016 
0017 /**
0018  * @brief The FileName class represents a file inside the image root directory.
0019  * For performance reasons, the class stores both the relative and absolute file name.
0020  */
0021 class FileName
0022 {
0023 public:
0024     /**
0025      * @brief FileName constructs a null FileName.
0026      */
0027     FileName();
0028     /**
0029      * @brief fromAbsolutePath creates a FileName from an absolute path within the image root.
0030      * \attention fromAbsolutePath depends on the DB::SettingsData singleton!
0031      * @param fileName the absolute path to a file inside the image root
0032      * @return a FileName for the given path, or a null FileName if the \p fileName was invalid or outside the image root
0033      */
0034     static FileName fromAbsolutePath(const QString &fileName);
0035     /**
0036      * @brief fromRelativePath creates a FileName from a from a path that is relative to the image root.
0037      * \attention fromAbsolutePath depends on the DB::SettingsData singleton!
0038      * @param fileName the relative (compared to the image root) path to a file
0039      * @return a FileName for the given path, or a null FileName if the \p fileName was invalid
0040      */
0041     static FileName fromRelativePath(const QString &fileName);
0042     /**
0043      * @brief absolute
0044      * @attention Do not call this function if isNull() is true!
0045      * @return the absolute path if the FileName is not null
0046      */
0047     QString absolute() const;
0048     /**
0049      * @brief relative
0050      * @attention Do not call this function if isNull() is true!
0051      * @return the relative path if the FileName is not null
0052      */
0053     QString relative() const;
0054     /**
0055      * @brief isNull
0056      * @return \c true if the FileName is null, \c false if it is valid.
0057      */
0058     bool isNull() const;
0059     /**
0060      * @brief isValid
0061      * @return \c true if the FileName is valid, \c false if it is null.
0062      */
0063     bool isValid() const;
0064     bool operator==(const FileName &other) const;
0065     bool operator!=(const FileName &other) const;
0066     /**
0067      * @brief operator < as required for sorting filenames
0068      * Null FileNames are considered to be "smallest", but really you shouldn't be comparing the ordinality of null FileNames.
0069      * @param other
0070      * @return relative() < other.relative()
0071      */
0072     bool operator<(const FileName &other) const;
0073     /**
0074      * @brief exists
0075      * @return \c true if the file currently exists on the disk, \c false otherwise.
0076      */
0077     bool exists() const;
0078 
0079     /**
0080      * @brief Conversion to absolute local file url.
0081      */
0082     explicit operator QUrl() const;
0083 
0084 private:
0085     // During previous profilation it showed that converting between absolute and relative took quite some time,
0086     // so to avoid that, I store both.
0087     QString m_relativePath;
0088     QString m_absoluteFilePath;
0089     bool m_isNull;
0090 };
0091 
0092 uint qHash(const DB::FileName &fileName);
0093 typedef QSet<DB::FileName> FileNameSet;
0094 }
0095 
0096 Q_DECLARE_METATYPE(DB::FileName)
0097 
0098 #endif // KPABASE_FILENAME_H
0099 // vi:expandtab:tabstop=4 shiftwidth=4: