File indexing completed on 2024-04-28 17:06:00

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 #ifndef FILEITEM_H
0009 #define FILEITEM_H
0010 
0011 #include <sys/types.h>
0012 
0013 // QtCore
0014 #include <QString>
0015 #include <QUrl>
0016 
0017 #include <KIO/Global>
0018 #include <KIO/UDSEntry>
0019 
0020 /**
0021  * A file item gives access all meta information of a (virtual, dummy or real) file or directory in
0022  * the filesystem.
0023  *
0024  * NOTE: The name of a file item is supposed to be unique within a directory.
0025  */
0026 class FileItem
0027 {
0028 public:
0029     /**
0030      * Create a new file item.
0031      *
0032      * Don't use this constructor outside of FileSystem! If you really need to, create a new static
0033      * factory method below.
0034      *
0035      * NOTE: According to Unix standard uid and gid CAN have signed or unsigned type. We use (e.g.)
0036      * "(uid_t) -1" as a special invalid user ID for non-local files.
0037      * NOTE: ACLs are currently only used by Synchronizer.
0038      *
0039      * @param name the display name of this file. Don't have to be the real filename.
0040      * @param url (real) absolute URL of this file
0041      * @param isDir true if this file is a directory. Else false.
0042      * @param size size of file
0043      * @param mode mode of file (file type and permissions)
0044      * @param mtime file modification time
0045      * @param ctime file change time. Use -1 if unknown.
0046      * @param atime file access time
0047      * @param btime file creation time. Use -1 if unknown.
0048      * @param uid Unix user id of file owner. Use -1 here and provide an owner name for non-local files.
0049      * @param gid Unix group id of file group. Use -1 here and provide a group name for non-local files.
0050      * @param owner user name of file owner. Can be empty for local files
0051      * @param group group name of file group. Can be empty for local files.
0052      * @param isLink true if file is a symbolic link. Else false.
0053      * @param linkDest link destination path if file is a link. Relative or absolute. Empty by default.
0054      * @param isBrokenLink true if file is a symbolic link and destination file does not exists. Else false.
0055      * @param acl ACL string of file. Can be empty and is loaded on demand.
0056      * @param defaultAcl default ACL string of file (only for directories). Can be empty and is loaded on demand.
0057      */
0058     FileItem(const QString &name,
0059              const QUrl &url,
0060              bool isDir,
0061              KIO::filesize_t size,
0062              mode_t mode,
0063              time_t mtime,
0064              time_t ctime,
0065              time_t atime,
0066              time_t btime,
0067              uid_t uid = -1,
0068              gid_t gid = -1,
0069              const QString &owner = QString(),
0070              const QString &group = QString(),
0071              bool isLink = false,
0072              const QString &linkDest = QString(),
0073              bool isBrokenLink = false,
0074              const QString &acl = QString(),
0075              const QString &defaultAcl = QString());
0076 
0077     /** Create a new ".." dummy file item. */
0078     static FileItem *createDummy();
0079     /** Create a file item for a broken file which metadata could not be read. */
0080     static FileItem *createBroken(const QString &name, const QUrl &url);
0081     /** Create a new virtual directory. */
0082     static FileItem *createVirtualDir(const QString &name, const QUrl &url);
0083     /** Create a new file item copy with a different name. */
0084     static FileItem *createCopy(const FileItem &file, const QString &newName);
0085 
0086     // following functions give-out file details
0087     inline const QString &getName() const
0088     {
0089         return m_name;
0090     }
0091     /** Return the file size. Returns 0 for directories with unknown size. */
0092     inline KIO::filesize_t getSize() const
0093     {
0094         return m_size == (KIO::filesize_t)-1 ? 0 : m_size;
0095     }
0096     /** Return the file size. Returns (KIO::filesize_t)-1 for directories with unknown size. */
0097     inline KIO::filesize_t getUISize() const
0098     {
0099         return m_size;
0100     }
0101     inline const QString &getPerm() const
0102     {
0103         return m_permissions;
0104     }
0105     /** Return true if the file is a directory or a symlink to a directory, otherwise false. */
0106     inline bool isDir() const
0107     {
0108         return m_isDir;
0109     }
0110     inline bool isSymLink() const
0111     {
0112         return m_isLink;
0113     }
0114     inline bool isBrokenLink() const
0115     {
0116         return m_isBrokenLink;
0117     }
0118     inline const QString &getSymDest() const
0119     {
0120         return m_linkDest;
0121     }
0122     inline mode_t getMode() const
0123     {
0124         return m_mode;
0125     }
0126     /** Return the file modification time (mtime) or -1 if unknown. */
0127     inline time_t getModificationTime() const
0128     {
0129         return m_mtime;
0130     }
0131     /** Return the file change time (ctime) or -1 if unknown. */
0132     inline time_t getChangeTime() const
0133     {
0134         return m_ctime;
0135     }
0136     /** Return the file access time (atime) or -1 if unknown. */
0137     inline time_t getAccessTime() const
0138     {
0139         return m_atime;
0140     }
0141     /** Return the file creation time (btime) or -1 if unknown. */
0142     inline time_t getCreationTime() const
0143     {
0144         return m_btime;
0145     }
0146     inline const QUrl &getUrl() const
0147     {
0148         return m_url;
0149     }
0150     inline const QString &getOwner() const
0151     {
0152         return m_owner;
0153     }
0154     inline const QString &getGroup() const
0155     {
0156         return m_group;
0157     }
0158 
0159     const QString &getMime();
0160     const QString &getIcon();
0161 
0162     const QString &getACL();
0163     const QString &getDefaultACL();
0164     const KIO::UDSEntry getEntry(); //< return the UDSEntry from the file item
0165     char isReadable() const;
0166     char isWriteable() const;
0167     char isExecutable() const;
0168     /**
0169      * Set the file size.
0170      * used ONLY when calculating a directory's space, needs to change the
0171      * displayed size of the viewitem and thus the file item. For INTERNAL USE !
0172      */
0173     void setSize(KIO::filesize_t size);
0174 
0175     inline static void loadUserDefinedFolderIcons(bool load)
0176     {
0177         userDefinedFolderIcons = load;
0178     }
0179 
0180 private:
0181     void setIconName(const QString &icon)
0182     {
0183         m_iconName = icon;
0184         m_mimeType = "?";
0185     }
0186     void loadACL();
0187 
0188     QString m_name; //< file name
0189     QUrl m_url; //< file URL
0190     bool m_isDir; //< flag, true if it's a directory
0191 
0192     KIO::filesize_t m_size; //< file size
0193     mode_t m_mode; //< file mode (file type and permissions)
0194 
0195     time_t m_mtime; //< file modification time
0196     time_t m_ctime; //< file change time
0197     time_t m_atime; //< file access time
0198     time_t m_btime; //< file creation time
0199 
0200     uid_t m_uid; //< file owner id
0201     gid_t m_gid; //< file group id
0202     QString m_owner; //< file owner name
0203     QString m_group; //< file group name
0204 
0205     bool m_isLink; //< true if the file is a symlink
0206     QString m_linkDest; //< if it's a symlink - its destination
0207     bool m_isBrokenLink; //< true if the link destination does not exist
0208 
0209     QString m_permissions; //< file permissions string
0210 
0211     QString m_acl; //< ACL permission string, may lazy initialized
0212     QString m_defaulfAcl; //< ACL default string, may lazy initialized
0213     bool m_AclLoaded; //< flag, indicates that ACL permissions already loaded
0214 
0215     QString m_mimeType; //< file mimetype, lazy initialized
0216     QString m_iconName; //< the name of the icon file, lazy initialized
0217 
0218     static bool userDefinedFolderIcons;
0219 };
0220 
0221 #endif