1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org> |
4 | SPDX-FileCopyrightText: 2007 Norbert Frese <nf2@scheinwelt.at> |
5 | SPDX-FileCopyrightText: 2007 Thiago Macieira <thiago@kde.org> |
6 | |
7 | SPDX-License-Identifier: LGPL-2.0-only |
8 | */ |
9 | |
10 | #ifndef UDSENTRY_H |
11 | #define UDSENTRY_H |
12 | |
13 | #include <QList> |
14 | #include <QMetaType> |
15 | #include <QSharedData> |
16 | #include <QString> |
17 | #include <QtGlobal> |
18 | #include <qplatformdefs.h> |
19 | |
20 | #include "kiocore_export.h" |
21 | |
22 | namespace KIO |
23 | { |
24 | class UDSEntry; |
25 | } |
26 | |
27 | KIOCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KIO::UDSEntry &a); |
28 | KIOCORE_EXPORT QDataStream &operator>>(QDataStream &s, KIO::UDSEntry &a); |
29 | |
30 | /** |
31 | * Support for qDebug() << aUDSEntry |
32 | * \since 5.22 |
33 | */ |
34 | KIOCORE_EXPORT QDebug operator<<(QDebug stream, const KIO::UDSEntry &entry); |
35 | |
36 | /** |
37 | * Returns true if the entry contains the same data as the other |
38 | * @since 5.63 |
39 | */ |
40 | KIOCORE_EXPORT bool operator==(const KIO::UDSEntry &entry, const KIO::UDSEntry &other); |
41 | |
42 | /** |
43 | * Returns true if the entry does not contain the same data as the other |
44 | * @since 5.63 |
45 | */ |
46 | KIOCORE_EXPORT bool operator!=(const KIO::UDSEntry &entry, const KIO::UDSEntry &other); |
47 | |
48 | namespace KIO |
49 | { |
50 | class UDSEntryPrivate; |
51 | /** |
52 | * @class KIO::UDSEntry udsentry.h <KIO/UDSEntry> |
53 | * |
54 | * Universal Directory Service |
55 | * |
56 | * UDS entry is the data structure representing all the fields about a given URL |
57 | * (file or directory). |
58 | * |
59 | * The KIO::listDir() and KIO:stat() operations use this data structure. |
60 | * |
61 | * KIO defines a number of standard fields, see the UDS_XXX enums (see StandardFieldTypes). |
62 | * at the moment UDSEntry only provides fields with numeric indexes, |
63 | * but there might be named fields with string indexes in the future. |
64 | * |
65 | * For instance, to retrieve the name of the entry, use: |
66 | * \code |
67 | * QString displayName = entry.stringValue( KIO::UDSEntry::UDS_NAME ); |
68 | * \endcode |
69 | * |
70 | * To know the modification time of the file/url: |
71 | * \code |
72 | * QDateTime mtime = QDateTime::fromSecsSinceEpoch(entry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0)); |
73 | * if (mtime.isValid()) |
74 | * ... |
75 | * \endcode |
76 | */ |
77 | class KIOCORE_EXPORT UDSEntry |
78 | { |
79 | public: |
80 | UDSEntry(); |
81 | |
82 | /** |
83 | * Create a UDSEntry by QT_STATBUF |
84 | * @param buff QT_STATBUF object |
85 | * @param name filename |
86 | * @since 5.0 |
87 | */ |
88 | UDSEntry(const QT_STATBUF &buff, const QString &name = QString()); |
89 | |
90 | /** |
91 | * Copy constructor |
92 | */ |
93 | UDSEntry(const UDSEntry &); |
94 | |
95 | /** |
96 | * Destructor |
97 | */ |
98 | ~UDSEntry(); |
99 | |
100 | /** |
101 | * Move constructor |
102 | * @since 5.44 |
103 | */ |
104 | UDSEntry(UDSEntry &&); |
105 | |
106 | /** |
107 | * Copy assignment |
108 | */ |
109 | UDSEntry &operator=(const UDSEntry &); |
110 | |
111 | /** |
112 | * Move assignment |
113 | * @since 5.44 |
114 | */ |
115 | UDSEntry &operator=(UDSEntry &&); |
116 | |
117 | /** |
118 | * @return value of a textual field |
119 | */ |
120 | QString stringValue(uint field) const; |
121 | |
122 | /** |
123 | * @return value of a numeric field |
124 | */ |
125 | long long numberValue(uint field, long long defaultValue = 0) const; |
126 | |
127 | // Convenience methods. |
128 | // Let's not add one method per field, only methods that have some more logic |
129 | // than just calling stringValue(field) or numberValue(field). |
130 | |
131 | /// @return true if this entry is a directory (or a link to a directory) |
132 | bool isDir() const; |
133 | /// @return true if this entry is a link |
134 | bool isLink() const; |
135 | |
136 | /** |
137 | * Calling this function before inserting items into an empty UDSEntry may save time and memory. |
138 | * @param size number of items for which memory will be pre-allocated |
139 | */ |
140 | void reserve(int size); |
141 | |
142 | /** |
143 | * insert field with string value, it will assert if the field is already inserted. In that case, use replace() instead. |
144 | * @param field numeric field id |
145 | * @param value to set |
146 | * @since 5.48 |
147 | */ |
148 | void fastInsert(uint field, const QString &value); |
149 | |
150 | /** |
151 | * insert field with numeric value, it will assert if the field is already inserted. In that case, use replace() instead. |
152 | * @param field numeric field id |
153 | * @param l value to set |
154 | * @since 5.48 |
155 | */ |
156 | void fastInsert(uint field, long long l); |
157 | |
158 | /** |
159 | * count fields |
160 | * @return the number of fields |
161 | */ |
162 | int count() const; |
163 | |
164 | /** |
165 | * check existence of a field |
166 | * @param field numeric field id |
167 | */ |
168 | bool contains(uint field) const; |
169 | |
170 | /** |
171 | * A vector of fields being present for the current entry. |
172 | * @return all fields for the current entry. |
173 | * @since 5.8 |
174 | */ |
175 | QList<uint> fields() const; |
176 | |
177 | /** |
178 | * remove all fields |
179 | */ |
180 | void clear(); |
181 | |
182 | /** |
183 | * Bit field used to specify the item type of a StandardFieldTypes. |
184 | */ |
185 | enum ItemTypes { |
186 | // Those are a bit field |
187 | /// Indicates that the field is a QString |
188 | UDS_STRING = 0x01000000, |
189 | /// Indicates that the field is a number (long long) |
190 | UDS_NUMBER = 0x02000000, |
191 | /// Indicates that the field represents a time, which is modelled by a long long |
192 | UDS_TIME = 0x04000000 | UDS_NUMBER, |
193 | }; |
194 | |
195 | /** |
196 | * Constants used to specify the type of a UDSEntry’s field. |
197 | */ |
198 | enum StandardFieldTypes { |
199 | |
200 | // The highest bit is reserved to store the used FieldTypes |
201 | |
202 | /// Size of the file |
203 | UDS_SIZE = 1 | UDS_NUMBER, |
204 | /// @internal |
205 | UDS_SIZE_LARGE = 2 | UDS_NUMBER, |
206 | /// User Name of the file owner |
207 | /// Not present on local fs, use UDS_LOCAL_USER_ID |
208 | UDS_USER = 3 | UDS_STRING, |
209 | /// Name of the icon, that should be used for displaying. |
210 | /// It overrides all other detection mechanisms |
211 | UDS_ICON_NAME = 4 | UDS_STRING, |
212 | /// Group Name of the file owner |
213 | /// Not present on local fs, use UDS_LOCAL_GROUP_ID |
214 | UDS_GROUP = 5 | UDS_STRING, |
215 | /// Filename - as displayed in directory listings etc. |
216 | /// "." has the usual special meaning of "current directory" |
217 | /// UDS_NAME must always be set and never be empty, neither contain '/'. |
218 | /// |
219 | /// Note that KIO will append the UDS_NAME to the url of their |
220 | /// parent directory, so all KIO workers must use that naming scheme |
221 | /// ("url_of_parent/filename" will be the full url of that file). |
222 | /// To customize the appearance of files without changing the url |
223 | /// of the items, use UDS_DISPLAY_NAME. |
224 | UDS_NAME = 6 | UDS_STRING, |
225 | /// A local file path if the KIO worker display files sitting |
226 | /// on the local filesystem (but in another hierarchy, e.g.\ settings:/ or remote:/) |
227 | UDS_LOCAL_PATH = 7 | UDS_STRING, |
228 | /// Treat the file as a hidden file (if set to 1) or as a normal file (if set to 0). |
229 | /// This field overrides the default behavior (the check for a leading dot in the filename). |
230 | UDS_HIDDEN = 8 | UDS_NUMBER, |
231 | /// Access permissions (part of the mode returned by stat) |
232 | UDS_ACCESS = 9 | UDS_NUMBER, |
233 | /// The last time the file was modified. Required time format: seconds since UNIX epoch. |
234 | UDS_MODIFICATION_TIME = 10 | UDS_TIME, |
235 | /// The last time the file was opened. Required time format: seconds since UNIX epoch. |
236 | UDS_ACCESS_TIME = 11 | UDS_TIME, |
237 | /// The time the file was created. Required time format: seconds since UNIX epoch. |
238 | UDS_CREATION_TIME = 12 | UDS_TIME, |
239 | /// File type, part of the mode returned by stat |
240 | /// (for a link, this returns the file type of the pointed item) |
241 | /// check UDS_LINK_DEST to know if this is a link |
242 | UDS_FILE_TYPE = 13 | UDS_NUMBER, |
243 | /// Name of the file where the link points to |
244 | /// Allows to check for a symlink (don't use S_ISLNK !) |
245 | UDS_LINK_DEST = 14 | UDS_STRING, |
246 | /// An alternative URL (If different from the caption). |
247 | /// Can be used to mix different hierarchies. |
248 | /// |
249 | /// Use UDS_DISPLAY_NAME if you simply want to customize the user-visible filenames, or use |
250 | /// UDS_TARGET_URL if you want "links" to unrelated urls. |
251 | UDS_URL = 15 | UDS_STRING, |
252 | /// A MIME type; the KIO worker should set it if it's known. |
253 | UDS_MIME_TYPE = 16 | UDS_STRING, |
254 | /// A MIME type to be used for displaying only. |
255 | /// But when 'running' the file, the MIME type is re-determined |
256 | /// This is for special cases like symlinks in FTP; you probably don't want to use this one. |
257 | UDS_GUESSED_MIME_TYPE = 17 | UDS_STRING, |
258 | /// XML properties, e.g.\ for WebDAV |
259 | UDS_XML_PROPERTIES = 18 | UDS_STRING, |
260 | |
261 | /// Indicates that the entry has extended ACL entries |
262 | UDS_EXTENDED_ACL = 19 | UDS_NUMBER, |
263 | /// The access control list serialized into a single string. |
264 | UDS_ACL_STRING = 20 | UDS_STRING, |
265 | /// The default access control list serialized into a single string. |
266 | /// Only available for directories. |
267 | UDS_DEFAULT_ACL_STRING = 21 | UDS_STRING, |
268 | |
269 | /// If set, contains the label to display instead of |
270 | /// the 'real name' in UDS_NAME |
271 | /// @since 4.1 |
272 | UDS_DISPLAY_NAME = 22 | UDS_STRING, |
273 | /// This file is a shortcut or mount, pointing to an |
274 | /// URL in a different hierarchy |
275 | /// @since 4.1 |
276 | UDS_TARGET_URL = 23 | UDS_STRING, |
277 | |
278 | /// User-readable type of file (if not specified, |
279 | /// the MIME type's description is used) |
280 | /// @since 4.4 |
281 | UDS_DISPLAY_TYPE = 24 | UDS_STRING, |
282 | |
283 | /// A comma-separated list of supplementary icon overlays |
284 | /// which will be added to the list of overlays created |
285 | /// by KFileItem. |
286 | /// |
287 | /// @since 4.5 |
288 | UDS_ICON_OVERLAY_NAMES = 25 | UDS_STRING, |
289 | |
290 | /// A comment which will be displayed as is to the user. The string |
291 | /// value may contain plain text or Qt-style rich-text extensions. |
292 | /// |
293 | /// @since 4.6 |
294 | = 26 | UDS_STRING, |
295 | |
296 | /// Device number for this file, used to detect hardlinks |
297 | /// @since 4.7.3 |
298 | UDS_DEVICE_ID = 27 | UDS_NUMBER, |
299 | /// Inode number for this file, used to detect hardlinks |
300 | /// @since 4.7.3 |
301 | UDS_INODE = 28 | UDS_NUMBER, |
302 | |
303 | /// For folders, the recursize size of its content |
304 | /// @since 5.70 |
305 | UDS_RECURSIVE_SIZE = 29 | UDS_NUMBER, |
306 | |
307 | /// User ID of the file owner |
308 | /// @since 6.0 |
309 | UDS_LOCAL_USER_ID = 30 | UDS_NUMBER, |
310 | /// Group ID of the file owner |
311 | /// @since 6.0 |
312 | UDS_LOCAL_GROUP_ID = 31 | UDS_NUMBER, |
313 | |
314 | /// Extra data (used only if you specified Columns/ColumnsTypes) |
315 | /// NB: you cannot repeat this entry; use UDS_EXTRA + i |
316 | /// until UDS_EXTRA_END. |
317 | = 100 | UDS_STRING, |
318 | /// Extra data (used only if you specified Columns/ColumnsTypes) |
319 | /// NB: you cannot repeat this entry; use UDS_EXTRA + i |
320 | /// until UDS_EXTRA_END. |
321 | = 140 | UDS_STRING, |
322 | }; |
323 | |
324 | private: |
325 | QSharedDataPointer<UDSEntryPrivate> d; |
326 | friend KIOCORE_EXPORT QDataStream & ::operator<<(QDataStream &s, const KIO::UDSEntry &a); |
327 | friend KIOCORE_EXPORT QDataStream & ::operator>>(QDataStream &s, KIO::UDSEntry &a); |
328 | friend KIOCORE_EXPORT QDebug(::operator<<)(QDebug stream, const KIO::UDSEntry &entry); |
329 | |
330 | public: |
331 | /** |
332 | * Replace or insert field with string value |
333 | * @param field numeric field id |
334 | * @param value to set |
335 | * @since 5.47 |
336 | */ |
337 | void replace(uint field, const QString &value); |
338 | |
339 | /** |
340 | * Replace or insert field with numeric value |
341 | * @param field numeric field id |
342 | * @param l value to set |
343 | * @since 5.47 |
344 | */ |
345 | void replace(uint field, long long l); |
346 | }; |
347 | |
348 | // allows operator ^ and | between UDSEntry::StandardFieldTypes and UDSEntry::ItemTypes |
349 | inline constexpr UDSEntry::StandardFieldTypes operator|(UDSEntry::StandardFieldTypes fieldType, UDSEntry::ItemTypes type) |
350 | { |
351 | return static_cast<UDSEntry::StandardFieldTypes>((char)fieldType | (char)type); |
352 | } |
353 | inline constexpr UDSEntry::StandardFieldTypes operator^(UDSEntry::StandardFieldTypes fieldType, UDSEntry::ItemTypes type) |
354 | { |
355 | return static_cast<UDSEntry::StandardFieldTypes>((char)fieldType ^ (char)type); |
356 | } |
357 | } |
358 | |
359 | Q_DECLARE_TYPEINFO(KIO::UDSEntry, Q_RELOCATABLE_TYPE); |
360 | |
361 | namespace KIO |
362 | { |
363 | /** |
364 | * A directory listing is a list of UDSEntry instances. |
365 | * |
366 | * To list the name and size of all the files in a directory listing you would do: |
367 | * \code |
368 | * KIO::UDSEntryList::ConstIterator it = entries.begin(); |
369 | * const KIO::UDSEntryList::ConstIterator end = entries.end(); |
370 | * for (; it != end; ++it) { |
371 | * const KIO::UDSEntry& entry = *it; |
372 | * QString name = entry.stringValue( KIO::UDSEntry::UDS_NAME ); |
373 | * bool isDir = entry.isDir(); |
374 | * KIO::filesize_t size = entry.numberValue( KIO::UDSEntry::UDS_SIZE, -1 ); |
375 | * ... |
376 | * } |
377 | * \endcode |
378 | */ |
379 | typedef QList<UDSEntry> UDSEntryList; |
380 | } // end namespace |
381 | |
382 | Q_DECLARE_METATYPE(KIO::UDSEntry) |
383 | |
384 | #endif /*UDSENTRY_H*/ |
385 | |