KIO

kfileitem.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 1999-2006 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KFILEITEM_H
9#define KFILEITEM_H
10
11#include "kiocore_export.h"
12#include <QDateTime>
13#include <QFile>
14#include <QUrl>
15#include <kacl.h>
16#include <kio/global.h>
17#include <kio/udsentry.h>
18
19#include <QList>
20#include <QMimeType>
21#include <qplatformdefs.h>
22
23class KFileItemPrivate;
24
25/**
26 * @class KFileItem kfileitem.h <KFileItem>
27 *
28 * A KFileItem is a generic class to handle a file, local or remote.
29 * In particular, it makes it easier to handle the result of KIO::listDir
30 * (UDSEntry isn't very friendly to use).
31 * It includes many file attributes such as MIME type, icon, text, mode, link...
32 *
33 * KFileItem is implicitly shared, i.e. it can be used as a value and copied around at almost no cost.
34 */
35class KIOCORE_EXPORT KFileItem
36{
37 Q_GADGET
38
39 Q_PROPERTY(QUrl url READ url WRITE setUrl)
40 Q_PROPERTY(QString user READ user)
41 Q_PROPERTY(QString group READ group)
42 Q_PROPERTY(bool isLink READ isLink)
43 Q_PROPERTY(bool isDir READ isDir)
44 Q_PROPERTY(bool isFile READ isFile)
45 Q_PROPERTY(bool isReadable READ isReadable)
46 Q_PROPERTY(bool isWritable READ isWritable)
47 Q_PROPERTY(bool isHidden READ isHidden)
48 Q_PROPERTY(bool isSlow READ isSlow)
49 Q_PROPERTY(bool isDesktopFile READ isDesktopFile)
50 Q_PROPERTY(QString linkDest READ linkDest)
51 Q_PROPERTY(QUrl targetUrl READ targetUrl)
52 Q_PROPERTY(QString localPath READ localPath WRITE setLocalPath)
53 Q_PROPERTY(bool isLocalFile READ isLocalFile)
54 Q_PROPERTY(QString text READ text)
55 Q_PROPERTY(QString name READ name WRITE setName)
56 Q_PROPERTY(QString mimetype READ mimetype)
57 Q_PROPERTY(QMimeType determineMimeType READ determineMimeType)
58 Q_PROPERTY(QMimeType currentMimeType READ currentMimeType)
59 Q_PROPERTY(bool isFinalIconKnown READ isFinalIconKnown)
60 Q_PROPERTY(bool isMimeTypeKnown READ isMimeTypeKnown)
61 Q_PROPERTY(QString mimeComment READ mimeComment)
62 Q_PROPERTY(QString iconName READ iconName)
63 Q_PROPERTY(QStringList overlays READ overlays)
64 Q_PROPERTY(QString comment READ comment)
65 Q_PROPERTY(QString getStatusBarInfo READ getStatusBarInfo)
66 Q_PROPERTY(bool isRegularFile READ isRegularFile)
67
68public:
69 enum {
70 Unknown = static_cast<mode_t>(-1)
71 };
72
73 /**
74 * The timestamps associated with a file.
75 * - ModificationTime: the time the file's contents were last modified
76 * - AccessTime: the time the file was last accessed (last read or written to)
77 * - CreationTime: the time the file was created
78 */
79 enum FileTimes {
80 // warning: don't change without looking at the Private class
81 ModificationTime = 0,
82 AccessTime = 1,
83 CreationTime = 2,
84 // ChangeTime
85 };
86 Q_ENUM(FileTimes)
87
88 enum MimeTypeDetermination {
89 NormalMimeTypeDetermination = 0,
90 SkipMimeTypeFromContent,
91 };
92 Q_ENUM(MimeTypeDetermination)
93
94 /**
95 * Null KFileItem. Doesn't represent any file, only exists for convenience.
96 */
97 KFileItem();
98
99 /**
100 * Creates an item representing a file, from a UDSEntry.
101 * This is the preferred constructor when using KIO::listDir().
102 *
103 * @param entry the KIO entry used to get the file, contains info about it
104 * @param itemOrDirUrl the URL of the item or of the directory containing this item (see urlIsDirectory).
105 * @param delayedMimeTypes specifies if the MIME type of the given
106 * URL should be determined immediately or on demand.
107 * See the bool delayedMimeTypes in the KDirLister constructor.
108 * @param urlIsDirectory specifies if the url is just the directory of the
109 * fileitem and the filename from the UDSEntry should be used.
110 *
111 * When creating KFileItems out of the UDSEntry emitted by a KIO list job,
112 * use KFileItem(entry, listjob->url(), delayedMimeTypes, true);
113 */
114 KFileItem(const KIO::UDSEntry &entry, const QUrl &itemOrDirUrl, bool delayedMimeTypes = false, bool urlIsDirectory = false);
115
116 /**
117 * Creates an item representing a file, for which the MIME type is already known.
118 * @param url the file url
119 * @param mimeType the name of the file's MIME type
120 * @param mode the mode (S_IFDIR...)
121 */
122 explicit KFileItem(const QUrl &url, const QString &mimeType = QString(), mode_t mode = KFileItem::Unknown);
123
124 /**
125 * Creates an item representing a file, with the option of skipping MIME type determination.
126 * @param url the file url
127 * @param mimeTypeDetermination the mode of determining the MIME type:
128 * NormalMimeTypeDetermination by content if local file, i.e. access the file,
129 * open and read part of it;
130 * by QMimeDatabase::MatchMode::MatchExtension if not local.
131 * SkipMimeTypeFromContent always by QMimeDatabase::MatchMode::MatchExtension,
132 * i.e. won't access the file by stat() or opening it;
133 * only suitable for files, directories won't be recognized.
134 * @since 5.57
135 */
136 KFileItem(const QUrl &url, KFileItem::MimeTypeDetermination mimeTypeDetermination);
137
138 /**
139 * Copy constructor
140 */
142
143 /**
144 * Destructor
145 */
147
148 /**
149 * Move constructor
150 * @since 5.43
151 */
153
154 /**
155 * Copy assignment
156 */
158
159 /**
160 * Move assignment
161 * @since 5.43
162 */
164
165 /**
166 * Throw away and re-read (for local files) all information about the file.
167 * This is called when the _file_ changes.
168 */
169 void refresh();
170
171 /**
172 * Re-reads MIME type information.
173 * This is called when the MIME type database changes.
174 */
175 void refreshMimeType();
176
177 /**
178 * Sets MIME type determination to be immediate or on demand.
179 * Call this after the constructor, and before using any MIME-type-related method.
180 * @since 5.0
181 */
182 void setDelayedMimeTypes(bool b);
183
184 /**
185 * Returns the url of the file.
186 * @return the url of the file
187 */
188 QUrl url() const;
189
190 /**
191 * Sets the item's URL. Do not call unless you know what you are doing!
192 * (used for example when an item got renamed).
193 * @param url the item's URL
194 */
195 void setUrl(const QUrl &url);
196
197 /**
198 * Sets the item's local path (UDS_LOCAL_PATH). Do not call unless you know what you are doing!
199 * This won't change the item's name or URL.
200 * (used for example when an item got renamed).
201 * @param path the item's local path
202 * @since 5.20
203 */
204 void setLocalPath(const QString &path);
205
206 /**
207 * Sets the item's name (i.e.\ the filename).
208 * This is automatically done by setUrl, to set the name from the URL's fileName().
209 * This method is provided for some special cases like relative paths as names (KFindPart)
210 * @param name the item's name
211 */
212 void setName(const QString &name);
213
214 /**
215 * Returns the permissions of the file (stat.st_mode containing only permissions).
216 * @return the permissions of the file
217 */
218 mode_t permissions() const;
219
220 /**
221 * Returns the access permissions for the file as a string.
222 * @return the access permission as string
223 */
224 QString permissionsString() const;
225
226 /**
227 * Tells if the file has extended access level information ( Posix ACL )
228 * @return true if the file has extend ACL information or false if it hasn't
229 */
230 bool hasExtendedACL() const;
231
232 /**
233 * Returns the access control list for the file.
234 * @return the access control list as a KACL
235 */
236 KACL ACL() const;
237
238 /**
239 * Returns the default access control list for the directory.
240 * @return the default access control list as a KACL
241 */
242 KACL defaultACL() const;
243
244 /**
245 * Returns the file type (stat.st_mode containing only S_IFDIR, S_IFLNK, ...).
246 * @return the file type
247 */
248 mode_t mode() const;
249
250 /**
251 * Returns the file's owner's user id.
252 * Available only on supported protocols.
253 * @since 6.0
254 */
255 int userId() const;
256 /**
257 * Returns the file's owner's group id.
258 * Available only on supported protocols.
259 * @since 6.0
260 */
261 int groupId() const;
262
263 /**
264 * Returns the owner of the file.
265 * @return the file's owner
266 */
267 QString user() const;
268
269 /**
270 * Returns the group of the file.
271 * @return the file's group
272 */
273 QString group() const;
274
275 /**
276 * Returns true if this item represents a link in the UNIX sense of
277 * a link.
278 * @return true if the file is a link
279 */
280 bool isLink() const;
281
282 /**
283 * Returns true if this item represents a directory.
284 * @return true if the item is a directory
285 */
286 bool isDir() const;
287
288 /**
289 * Returns true if this item represents a file (and not a directory)
290 * @return true if the item is a file
291 */
292 bool isFile() const;
293
294 /**
295 * Checks whether the file or directory is readable. In some cases
296 * (remote files), we may return true even though it can't be read.
297 * @return true if the file can be read - more precisely,
298 * false if we know for sure it can't
299 */
300 bool isReadable() const;
301
302 /**
303 * Checks whether the file or directory is writable. In some cases
304 * (remote files), we may return true even though it can't be written to.
305 * @return true if the file or directory can be written to - more precisely,
306 * false if we know for sure it can't
307 */
308 bool isWritable() const;
309
310 /**
311 * Checks whether the file is hidden.
312 * @return true if the file is hidden.
313 */
314 bool isHidden() const;
315
316 /**
317 * @return true if the file is a remote URL, or a local file on a network mount.
318 * It will return false only for really-local file systems.
319 * @since 4.7.4
320 */
321 bool isSlow() const;
322
323 /**
324 * Checks whether the file is a readable local .desktop file,
325 * i.e.\ a file whose path can be given to KDesktopFile
326 * @return true if the file is a desktop file.
327 */
328 bool isDesktopFile() const;
329
330 /**
331 * Returns the link destination if isLink() == true.
332 * @return the link destination. QString() if the item is not a link
333 */
334 QString linkDest() const;
335
336 /**
337 * Returns the target url of the file, which is the same as url()
338 * in cases where the worker doesn't specify UDS_TARGET_URL
339 * @return the target url.
340 */
341 QUrl targetUrl() const;
342
343 /**
344 * Returns the local path if isLocalFile() == true or the KIO item has
345 * a UDS_LOCAL_PATH atom.
346 *
347 * Treat it as a readonly path to open/list contents, use original url to move/delete files.
348 *
349 * @return the item local path, or QString() if not known
350 */
351 QString localPath() const;
352
353 /**
354 * Returns the size of the file, if known.
355 * @return the file size, or 0 if not known
356 */
357 KIO::filesize_t size() const;
358
359 /**
360 * @brief For folders, its recursive size:
361 * the size of its files plus the recursiveSize of its folder
362 *
363 * Initially only implemented for trash:/
364 *
365 * @since 5.70
366 * @return The recursive size
367 */
368 KIO::filesize_t recursiveSize() const;
369
370 /**
371 * Requests the modification, access or creation time, depending on @p which.
372 * @param which the timestamp
373 * @return the time asked for, QDateTime() if not available
374 * @see timeString()
375 */
376 Q_INVOKABLE QDateTime time(KFileItem::FileTimes which) const;
377
378 /**
379 * Requests the modification, access or creation time as a string, depending
380 * on @p which.
381 * @param which the timestamp
382 * @returns a formatted string of the requested time.
383 * @see time
384 */
385 Q_INVOKABLE QString timeString(KFileItem::FileTimes which = ModificationTime) const;
386
387 /**
388 * Returns true if the file is a local file.
389 * @return true if the file is local, false otherwise
390 */
391 bool isLocalFile() const;
392
393 /**
394 * Returns the text of the file item.
395 * It's not exactly the filename since some decoding happens ('%2F'->'/').
396 * @return the text of the file item
397 */
398 QString text() const;
399
400 /**
401 * Return the name of the file item (without a path).
402 * Similar to text(), but unencoded, i.e. the original name.
403 * @param lowerCase if true, the name will be returned in lower case,
404 * which is useful to speed up sorting by name, case insensitively.
405 * @return the file's name
406 */
407 QString name(bool lowerCase = false) const;
408
409 /**
410 * Returns the MIME type of the file item.
411 * If @p delayedMimeTypes was used in the constructor, this will determine
412 * the MIME type first. Equivalent to determineMimeType()->name()
413 * @return the MIME type of the file
414 */
415 QString mimetype() const;
416
417 /**
418 * Returns the MIME type of the file item.
419 * If delayedMimeTypes was used in the constructor, this will determine
420 * the MIME type first.
421 * @return the MIME type
422 */
423 QMimeType determineMimeType() const;
424
425 /**
426 * Returns the currently known MIME type of the file item.
427 * This will not try to determine the MIME type if unknown.
428 * @return the known MIME type
429 */
430 QMimeType currentMimeType() const;
431
432 /**
433 * @return true if we have determined the final icon of this file already.
434 * @since 4.10.2
435 */
436 bool isFinalIconKnown() const;
437
438 /**
439 * @return true if we have determined the MIME type of this file already,
440 * i.e. if determineMimeType() will be fast. Otherwise it will have to
441 * find what the MIME type is, which is a possibly slow operation; usually
442 * this is delayed until necessary.
443 */
444 bool isMimeTypeKnown() const;
445
446 /**
447 * Returns the user-readable string representing the type of this file,
448 * like "OpenDocument Text File".
449 * @return the type of this KFileItem
450 */
451 QString mimeComment() const;
452
453 /**
454 * Returns the full path name to the icon that represents
455 * this MIME type.
456 * @return iconName the name of the file's icon
457 */
458 QString iconName() const;
459
460 /**
461 * Returns the overlays (bitfield of KIconLoader::*Overlay flags) that are used
462 * for this item's pixmap. Overlays are used to show for example, whether
463 * a file can be modified.
464 * @return the overlays of the pixmap
465 */
466 QStringList overlays() const;
467
468 /**
469 * A comment which can contain anything - even rich text. It will
470 * simply be displayed to the user as is.
471 *
472 */
473 QString comment() const;
474
475 /**
476 * Returns the string to be displayed in the statusbar,
477 * e.g.\ when the mouse is over this item.
478 * @return the status bar information
479 */
480 QString getStatusBarInfo() const;
481
482 /**
483 * Returns the UDS entry. Used by the tree view to access all details
484 * by position.
485 * @return the UDS entry
486 */
487 KIO::UDSEntry entry() const;
488
489 /**
490 * Return true if this item is a regular file,
491 * false otherwise (directory, link, character/block device, fifo, socket)
492 */
493 bool isRegularFile() const;
494
495 /**
496 * Returns the file extension
497 * Similar to QFileInfo::suffix except it takes into account UDS_DISPLAY_NAME and saves a stat call
498 * @since 6.0
499 */
500 QString suffix() const;
501
502 /**
503 * Somewhat like a comparison operator, but more explicit,
504 * and it can detect that two fileitems differ if any property of the file item
505 * has changed (file size, modification date, etc.). Two items are equal if
506 * all properties are equal. In contrast, operator== only compares URLs.
507 * @param item the item to compare
508 * @return true if all values are equal
509 */
510 bool cmp(const KFileItem &item) const;
511
512 /**
513 * Returns true if both items share the same URL.
514 */
515 bool operator==(const KFileItem &other) const;
516
517 /**
518 * Returns true if both items do not share the same URL.
519 */
520 bool operator!=(const KFileItem &other) const;
521
522 /**
523 * Returns true if this item's URL is lexically less than other's URL; otherwise returns false
524 * @since 5.48
525 */
526 bool operator<(const KFileItem &other) const;
527
528 /**
529 * Returns true if this item's URL is lexically less than url other; otherwise returns false
530 * @since 5.48
531 */
532 bool operator<(const QUrl &other) const;
533
534 /**
535 * Converts this KFileItem to a QVariant, this allows to use KFileItem
536 * in QVariant() constructor
537 */
538 operator QVariant() const;
539
540 /**
541 * Tries to return a local URL for this file item if possible.
542 * If @p local is not null, it will be set to @c true if the returned url is local,
543 * @c false otherwise.
544 *
545 * Example:
546 * @code
547 * bool isLocal = false;
548 * KFileItem item;
549 * const QUrl url = item.mostLocalUrl(&isLocal);
550 * if (isLocal) {
551 * // Use url
552 * }
553 * @endcode
554 *
555 */
556 QUrl mostLocalUrl(bool *local = nullptr) const;
557
558 struct MostLocalUrlResult {
559 QUrl url;
560 bool local;
561 };
562
563 /**
564 * Returns a MostLocalUrlResult, with the local Url for this item if possible
565 * (otherwise an empty Url), and a bool that is set to @c true if this Url
566 * does represent a local file otherwise @c false.
567 *
568 * Basically this is an alternative to mostLocalUrl(bool*), that does not use an
569 * output parameter.
570 *
571 * Example:
572 * @code
573 * KFileItem item;
574 * const MostLocalUrlResult result = item.isMostLocalUrl();
575 * if (result.local) { // A local file
576 * // Use result.url
577 * }
578 * @endcode
579 * @since 5.84
580 */
581 MostLocalUrlResult isMostLocalUrl() const;
582
583 /**
584 * Return true if default-constructed
585 */
586 bool isNull() const;
587
588 /**
589 * returns whether the KFileItem exists on-disk
590 * Call only after initialization (i.e `KIO::stat` or `refresh()` for local files)
591 * @since 6.0
592 */
593 bool exists() const;
594
595 /**
596 * Return true if the file has executable permission
597 * @since 6.0
598 */
599 bool isExecutable() const;
600
601private:
603
604 /**
605 * Hides the file.
606 */
607 KIOCORE_NO_EXPORT void setHidden();
608
609private:
610 KIOCORE_EXPORT friend QDataStream &operator<<(QDataStream &s, const KFileItem &a);
611 KIOCORE_EXPORT friend QDataStream &operator>>(QDataStream &s, KFileItem &a);
612
613 friend class KFileItemTest;
614 friend class KCoreDirListerCache;
615};
616
617Q_DECLARE_METATYPE(KFileItem)
618Q_DECLARE_TYPEINFO(KFileItem, Q_RELOCATABLE_TYPE);
619
620inline size_t qHash(const KFileItem &item, size_t seed = 0)
621{
622 return qHash(item.url(), seed);
623}
624
625/**
626 * @class KFileItemList kfileitem.h <KFileItem>
627 *
628 * List of KFileItems, which adds a few helper
629 * methods to QList<KFileItem>.
630 */
631class KIOCORE_EXPORT KFileItemList : public QList<KFileItem>
632{
633public:
634 /// Creates an empty list of file items.
636
637 /// Creates a new KFileItemList from a QList of file @p items.
638 KFileItemList(const QList<KFileItem> &items);
639
640 /// Creates a new KFileItemList from an initializer_list of file @p items.
641 /// @since 5.76
642 KFileItemList(std::initializer_list<KFileItem> items);
643
644 /**
645 * Find a KFileItem by name and return it.
646 * @return the item with the given name, or a null-item if none was found
647 * (see KFileItem::isNull())
648 */
649 KFileItem findByName(const QString &fileName) const;
650
651 /**
652 * Find a KFileItem by URL and return it.
653 * @return the item with the given URL, or a null-item if none was found
654 * (see KFileItem::isNull())
655 */
656 KFileItem findByUrl(const QUrl &url) const;
657
658 /// @return the list of URLs that those items represent
659 QList<QUrl> urlList() const;
660
661 /// @return the list of target URLs that those items represent
662 /// @since 4.2
663 QList<QUrl> targetUrlList() const;
664
665 // TODO KDE-5 add d pointer here so that we can merge KFileItemListProperties into KFileItemList
666};
667
668KIOCORE_EXPORT QDataStream &operator<<(QDataStream &s, const KFileItem &a);
669KIOCORE_EXPORT QDataStream &operator>>(QDataStream &s, KFileItem &a);
670
671/**
672 * Support for qDebug() << aFileItem
673 * \since 4.4
674 */
675KIOCORE_EXPORT QDebug operator<<(QDebug stream, const KFileItem &item);
676
677#endif
The KACL class encapsulates a POSIX Access Control List.
Definition kacl.h:38
List of KFileItems, which adds a few helper methods to QList<KFileItem>.
Definition kfileitem.h:632
A KFileItem is a generic class to handle a file, local or remote.
Definition kfileitem.h:36
KFileItem & operator=(const KFileItem &)
Copy assignment.
~KFileItem()
Destructor.
KFileItem(KFileItem &&)
Move constructor.
FileTimes
The timestamps associated with a file.
Definition kfileitem.h:79
KFileItem & operator=(KFileItem &&)
Move assignment.
KFileItem(const KFileItem &)
Copy constructor.
Universal Directory Service.
qulonglong filesize_t
64-bit file size
Definition global.h:35
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.