File indexing completed on 2024-10-06 06:41:14

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2007 Norbert Frese <nf2@scheinwelt.at>
0005     SPDX-FileCopyrightText: 2007 Thiago Macieira <thiago@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-only
0008 */
0009 
0010 #ifndef UDSENTRY_H
0011 #define UDSENTRY_H
0012 
0013 #include <QList>
0014 #include <QMetaType>
0015 #include <QSharedData>
0016 #include <QString>
0017 #include <QtGlobal>
0018 #include <qplatformdefs.h>
0019 
0020 #include "kiocore_export.h"
0021 
0022 namespace KIO
0023 {
0024 class UDSEntry;
0025 }
0026 
0027 KIOCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KIO::UDSEntry &a);
0028 KIOCORE_EXPORT QDataStream &operator>>(QDataStream &s, KIO::UDSEntry &a);
0029 
0030 /**
0031  * Support for qDebug() << aUDSEntry
0032  * \since 5.22
0033  */
0034 KIOCORE_EXPORT QDebug operator<<(QDebug stream, const KIO::UDSEntry &entry);
0035 
0036 /**
0037  * Returns true if the entry contains the same data as the other
0038  * @since 5.63
0039  */
0040 KIOCORE_EXPORT bool operator==(const KIO::UDSEntry &entry, const KIO::UDSEntry &other);
0041 
0042 /**
0043  * Returns true if the entry does not contain the same data as the other
0044  * @since 5.63
0045  */
0046 KIOCORE_EXPORT bool operator!=(const KIO::UDSEntry &entry, const KIO::UDSEntry &other);
0047 
0048 namespace KIO
0049 {
0050 class UDSEntryPrivate;
0051 /**
0052  * @class KIO::UDSEntry udsentry.h <KIO/UDSEntry>
0053  *
0054  * Universal Directory Service
0055  *
0056  * UDS entry is the data structure representing all the fields about a given URL
0057  * (file or directory).
0058  *
0059  * The KIO::listDir() and KIO:stat() operations use this data structure.
0060  *
0061  * KIO defines a number of standard fields, see the UDS_XXX enums (see StandardFieldTypes).
0062  * at the moment UDSEntry only provides fields with numeric indexes,
0063  * but there might be named fields with string indexes in the future.
0064  *
0065  * For instance, to retrieve the name of the entry, use:
0066  * \code
0067  * QString displayName = entry.stringValue( KIO::UDSEntry::UDS_NAME );
0068  * \endcode
0069  *
0070  * To know the modification time of the file/url:
0071  * \code
0072  *  QDateTime mtime = QDateTime::fromSecsSinceEpoch(entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0));
0073  *  if (mtime.isValid())
0074  *      ...
0075  * \endcode
0076  */
0077 class KIOCORE_EXPORT UDSEntry
0078 {
0079 public:
0080     UDSEntry();
0081 
0082     /**
0083      * Create a UDSEntry by QT_STATBUF
0084      * @param buff QT_STATBUF object
0085      * @param name filename
0086      * @since 5.0
0087      */
0088     UDSEntry(const QT_STATBUF &buff, const QString &name = QString());
0089 
0090     /**
0091      * Copy constructor
0092      */
0093     UDSEntry(const UDSEntry &);
0094 
0095     /**
0096      * Destructor
0097      */
0098     ~UDSEntry();
0099 
0100     /**
0101      * Move constructor
0102      * @since 5.44
0103      */
0104     UDSEntry(UDSEntry &&);
0105 
0106     /**
0107      * Copy assignment
0108      */
0109     UDSEntry &operator=(const UDSEntry &);
0110 
0111     /**
0112      * Move assignment
0113      * @since 5.44
0114      */
0115     UDSEntry &operator=(UDSEntry &&);
0116 
0117     /**
0118      * @return value of a textual field
0119      */
0120     QString stringValue(uint field) const;
0121 
0122     /**
0123      * @return value of a numeric field
0124      */
0125     long long numberValue(uint field, long long defaultValue = 0) const;
0126 
0127     // Convenience methods.
0128     // Let's not add one method per field, only methods that have some more logic
0129     // than just calling stringValue(field) or numberValue(field).
0130 
0131     /// @return true if this entry is a directory (or a link to a directory)
0132     bool isDir() const;
0133     /// @return true if this entry is a link
0134     bool isLink() const;
0135 
0136     /**
0137      * Calling this function before inserting items into an empty UDSEntry may save time and memory.
0138      * @param size number of items for which memory will be pre-allocated
0139      */
0140     void reserve(int size);
0141 
0142     /**
0143      * insert field with string value, it will assert if the field is already inserted. In that case, use replace() instead.
0144      * @param field numeric field id
0145      * @param value to set
0146      * @since 5.48
0147      */
0148     void fastInsert(uint field, const QString &value);
0149 
0150     /**
0151      * insert field with numeric value, it will assert if the field is already inserted. In that case, use replace() instead.
0152      * @param field numeric field id
0153      * @param l value to set
0154      * @since 5.48
0155      */
0156     void fastInsert(uint field, long long l);
0157 
0158     /**
0159      * count fields
0160      * @return the number of fields
0161      */
0162     int count() const;
0163 
0164     /**
0165      * check existence of a field
0166      * @param field numeric field id
0167      */
0168     bool contains(uint field) const;
0169 
0170     /**
0171      * A vector of fields being present for the current entry.
0172      * @return all fields for the current entry.
0173      * @since 5.8
0174      */
0175     QList<uint> fields() const;
0176 
0177     /**
0178      * remove all fields
0179      */
0180     void clear();
0181 
0182     /**
0183      * Bit field used to specify the item type of a StandardFieldTypes.
0184      */
0185     enum ItemTypes {
0186         // Those are a bit field
0187         /// Indicates that the field is a QString
0188         UDS_STRING = 0x01000000,
0189         /// Indicates that the field is a number (long long)
0190         UDS_NUMBER = 0x02000000,
0191         /// Indicates that the field represents a time, which is modelled by a long long
0192         UDS_TIME = 0x04000000 | UDS_NUMBER,
0193     };
0194 
0195     /**
0196      * Constants used to specify the type of a UDSEntry’s field.
0197      */
0198     enum StandardFieldTypes {
0199 
0200         // The highest bit is reserved to store the used FieldTypes
0201 
0202         /// Size of the file
0203         UDS_SIZE = 1 | UDS_NUMBER,
0204         /// @internal
0205         UDS_SIZE_LARGE = 2 | UDS_NUMBER,
0206         /// User Name of the file owner
0207         /// Not present on local fs, use UDS_LOCAL_USER_ID
0208         UDS_USER = 3 | UDS_STRING,
0209         /// Name of the icon, that should be used for displaying.
0210         /// It overrides all other detection mechanisms
0211         UDS_ICON_NAME = 4 | UDS_STRING,
0212         /// Group Name of the file owner
0213         /// Not present on local fs, use UDS_LOCAL_GROUP_ID
0214         UDS_GROUP = 5 | UDS_STRING,
0215         /// Filename - as displayed in directory listings etc.
0216         /// "." has the usual special meaning of "current directory"
0217         /// UDS_NAME must always be set and never be empty, neither contain '/'.
0218         ///
0219         /// Note that KIO will append the UDS_NAME to the url of their
0220         /// parent directory, so all KIO workers must use that naming scheme
0221         /// ("url_of_parent/filename" will be the full url of that file).
0222         /// To customize the appearance of files without changing the url
0223         /// of the items, use UDS_DISPLAY_NAME.
0224         UDS_NAME = 6 | UDS_STRING,
0225         /// A local file path if the KIO worker display files sitting
0226         /// on the local filesystem (but in another hierarchy, e.g.\ settings:/ or remote:/)
0227         UDS_LOCAL_PATH = 7 | UDS_STRING,
0228         /// Treat the file as a hidden file (if set to 1) or as a normal file (if set to 0).
0229         /// This field overrides the default behavior (the check for a leading dot in the filename).
0230         UDS_HIDDEN = 8 | UDS_NUMBER,
0231         /// Access permissions (part of the mode returned by stat)
0232         UDS_ACCESS = 9 | UDS_NUMBER,
0233         /// The last time the file was modified. Required time format: seconds since UNIX epoch.
0234         UDS_MODIFICATION_TIME = 10 | UDS_TIME,
0235         /// The last time the file was opened. Required time format: seconds since UNIX epoch.
0236         UDS_ACCESS_TIME = 11 | UDS_TIME,
0237         /// The time the file was created. Required time format: seconds since UNIX epoch.
0238         UDS_CREATION_TIME = 12 | UDS_TIME,
0239         /// File type, part of the mode returned by stat
0240         /// (for a link, this returns the file type of the pointed item)
0241         /// check UDS_LINK_DEST to know if this is a link
0242         UDS_FILE_TYPE = 13 | UDS_NUMBER,
0243         /// Name of the file where the link points to
0244         /// Allows to check for a symlink (don't use S_ISLNK !)
0245         UDS_LINK_DEST = 14 | UDS_STRING,
0246         /// An alternative URL (If different from the caption).
0247         /// Can be used to mix different hierarchies.
0248         ///
0249         /// Use UDS_DISPLAY_NAME if you simply want to customize the user-visible filenames, or use
0250         /// UDS_TARGET_URL if you want "links" to unrelated urls.
0251         UDS_URL = 15 | UDS_STRING,
0252         /// A MIME type; the KIO worker should set it if it's known.
0253         UDS_MIME_TYPE = 16 | UDS_STRING,
0254         /// A MIME type to be used for displaying only.
0255         /// But when 'running' the file, the MIME type is re-determined
0256         /// This is for special cases like symlinks in FTP; you probably don't want to use this one.
0257         UDS_GUESSED_MIME_TYPE = 17 | UDS_STRING,
0258         /// XML properties, e.g.\ for WebDAV
0259         UDS_XML_PROPERTIES = 18 | UDS_STRING,
0260 
0261         /// Indicates that the entry has extended ACL entries
0262         UDS_EXTENDED_ACL = 19 | UDS_NUMBER,
0263         /// The access control list serialized into a single string.
0264         UDS_ACL_STRING = 20 | UDS_STRING,
0265         /// The default access control list serialized into a single string.
0266         /// Only available for directories.
0267         UDS_DEFAULT_ACL_STRING = 21 | UDS_STRING,
0268 
0269         /// If set, contains the label to display instead of
0270         /// the 'real name' in UDS_NAME
0271         /// @since 4.1
0272         UDS_DISPLAY_NAME = 22 | UDS_STRING,
0273         /// This file is a shortcut or mount, pointing to an
0274         /// URL in a different hierarchy
0275         /// @since 4.1
0276         UDS_TARGET_URL = 23 | UDS_STRING,
0277 
0278         /// User-readable type of file (if not specified,
0279         /// the MIME type's description is used)
0280         /// @since 4.4
0281         UDS_DISPLAY_TYPE = 24 | UDS_STRING,
0282 
0283         /// A comma-separated list of supplementary icon overlays
0284         /// which will be added to the list of overlays created
0285         /// by KFileItem.
0286         ///
0287         /// @since 4.5
0288         UDS_ICON_OVERLAY_NAMES = 25 | UDS_STRING,
0289 
0290         /// A comment which will be displayed as is to the user. The string
0291         /// value may contain plain text or Qt-style rich-text extensions.
0292         ///
0293         /// @since 4.6
0294         UDS_COMMENT = 26 | UDS_STRING,
0295 
0296         /// Device number for this file, used to detect hardlinks
0297         /// @since 4.7.3
0298         UDS_DEVICE_ID = 27 | UDS_NUMBER,
0299         /// Inode number for this file, used to detect hardlinks
0300         /// @since 4.7.3
0301         UDS_INODE = 28 | UDS_NUMBER,
0302 
0303         /// For folders, the recursize size of its content
0304         /// @since 5.70
0305         UDS_RECURSIVE_SIZE = 29 | UDS_NUMBER,
0306 
0307         /// User ID of the file owner
0308         /// @since 6.0
0309         UDS_LOCAL_USER_ID = 30 | UDS_NUMBER,
0310         /// Group ID of the file owner
0311         /// @since 6.0
0312         UDS_LOCAL_GROUP_ID = 31 | UDS_NUMBER,
0313 
0314         /// Extra data (used only if you specified Columns/ColumnsTypes)
0315         /// NB: you cannot repeat this entry; use UDS_EXTRA + i
0316         /// until UDS_EXTRA_END.
0317         UDS_EXTRA = 100 | UDS_STRING,
0318         /// Extra data (used only if you specified Columns/ColumnsTypes)
0319         /// NB: you cannot repeat this entry; use UDS_EXTRA + i
0320         /// until UDS_EXTRA_END.
0321         UDS_EXTRA_END = 140 | UDS_STRING,
0322     };
0323 
0324 private:
0325     QSharedDataPointer<UDSEntryPrivate> d;
0326     friend KIOCORE_EXPORT QDataStream & ::operator<<(QDataStream &s, const KIO::UDSEntry &a);
0327     friend KIOCORE_EXPORT QDataStream & ::operator>>(QDataStream &s, KIO::UDSEntry &a);
0328     friend KIOCORE_EXPORT QDebug(::operator<<)(QDebug stream, const KIO::UDSEntry &entry);
0329 
0330 public:
0331     /**
0332      * Replace or insert field with string value
0333      * @param field numeric field id
0334      * @param value to set
0335      * @since 5.47
0336      */
0337     void replace(uint field, const QString &value);
0338 
0339     /**
0340      * Replace or insert field with numeric value
0341      * @param field numeric field id
0342      * @param l value to set
0343      * @since 5.47
0344      */
0345     void replace(uint field, long long l);
0346 };
0347 
0348 // allows operator ^ and | between UDSEntry::StandardFieldTypes and UDSEntry::ItemTypes
0349 inline constexpr UDSEntry::StandardFieldTypes operator|(UDSEntry::StandardFieldTypes fieldType, UDSEntry::ItemTypes type)
0350 {
0351     return static_cast<UDSEntry::StandardFieldTypes>((char)fieldType | (char)type);
0352 }
0353 inline constexpr UDSEntry::StandardFieldTypes operator^(UDSEntry::StandardFieldTypes fieldType, UDSEntry::ItemTypes type)
0354 {
0355     return static_cast<UDSEntry::StandardFieldTypes>((char)fieldType ^ (char)type);
0356 }
0357 }
0358 
0359 Q_DECLARE_TYPEINFO(KIO::UDSEntry, Q_RELOCATABLE_TYPE);
0360 
0361 namespace KIO
0362 {
0363 /**
0364  * A directory listing is a list of UDSEntry instances.
0365  *
0366  * To list the name and size of all the files in a directory listing you would do:
0367  * \code
0368  *   KIO::UDSEntryList::ConstIterator it = entries.begin();
0369  *   const KIO::UDSEntryList::ConstIterator end = entries.end();
0370  *   for (; it != end; ++it) {
0371  *     const KIO::UDSEntry& entry = *it;
0372  *     QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME );
0373  *     bool isDir = entry.isDir();
0374  *     KIO::filesize_t size = entry.numberValue( KIO::UDSEntry::UDS_SIZE, -1 );
0375  *     ...
0376  *   }
0377  * \endcode
0378  */
0379 typedef QList<UDSEntry> UDSEntryList;
0380 } // end namespace
0381 
0382 Q_DECLARE_METATYPE(KIO::UDSEntry)
0383 
0384 #endif /*UDSENTRY_H*/