1 | /* |
2 | This file is part of the KDE project |
3 | SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> |
4 | SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org> |
5 | |
6 | SPDX-License-Identifier: LGPL-2.0-or-later |
7 | */ |
8 | |
9 | #ifndef TRASHSIZECACHE_H |
10 | #define TRASHSIZECACHE_H |
11 | |
12 | #include <QString> |
13 | |
14 | #include <KConfig> |
15 | |
16 | class QFileInfo; |
17 | |
18 | /** |
19 | * @short A class that encapsulates the directory size cache. |
20 | * |
21 | * The directory size cache is used to speed up the determination of the trash size. |
22 | * |
23 | * Since version 1.0, https://specifications.freedesktop.org/trash-spec/trashspec-latest.html specifies this cache |
24 | * as a standard way to cache this information. |
25 | * |
26 | */ |
27 | class TrashSizeCache |
28 | { |
29 | public: |
30 | struct SizeAndModTime { |
31 | qint64 size; |
32 | qint64 mtime; |
33 | }; |
34 | |
35 | /** |
36 | * Creates a new trash size cache object for the given trash @p path. |
37 | */ |
38 | explicit TrashSizeCache(const QString &path); |
39 | |
40 | /** |
41 | * Adds a directory to the cache. |
42 | * @param directoryName fileId of the directory |
43 | * @param directorySize size in bytes |
44 | */ |
45 | void add(const QString &directoryName, qint64 directorySize); |
46 | |
47 | /** |
48 | * Removes a directory from the cache. |
49 | */ |
50 | void remove(const QString &directoryName); |
51 | |
52 | /** |
53 | * Renames a directory in the cache. |
54 | */ |
55 | void rename(const QString &oldDirectoryName, const QString &newDirectoryName); |
56 | |
57 | /** |
58 | * Sets the trash size to 0 bytes. |
59 | */ |
60 | void clear(); |
61 | |
62 | /** |
63 | * Calculates and returns the current trash size. |
64 | */ |
65 | qint64 calculateSize(); |
66 | |
67 | /** |
68 | * Calculates and returns the current trash size and its last modification date |
69 | */ |
70 | SizeAndModTime calculateSizeAndLatestModDate(); |
71 | |
72 | /** |
73 | * Returns the space occupied by directories in trash and their latest modification dates |
74 | */ |
75 | QHash<QByteArray, TrashSizeCache::SizeAndModTime> readDirCache(); |
76 | |
77 | private: |
78 | enum ScanFilesInTrashOption { CheckModificationTime, DonTcheckModificationTime }; |
79 | TrashSizeCache::SizeAndModTime scanFilesInTrash(ScanFilesInTrashOption checkDateTime = CheckModificationTime); |
80 | |
81 | QString mTrashSizeCachePath; |
82 | QString mTrashPath; |
83 | QFileInfo getTrashFileInfo(const QString &fileName); |
84 | }; |
85 | |
86 | #endif |
87 | |