File indexing completed on 2024-05-05 16:13:55

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0004     SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef TRASHSIZECACHE_H
0010 #define TRASHSIZECACHE_H
0011 
0012 #include <QString>
0013 
0014 #include <KConfig>
0015 
0016 class QFileInfo;
0017 
0018 /**
0019  * @short A class that encapsulates the directory size cache.
0020  *
0021  * The directory size cache is used to speed up the determination of the trash size.
0022  *
0023  * Since version 1.0, https://specifications.freedesktop.org/trash-spec/trashspec-latest.html specifies this cache
0024  * as a standard way to cache this information.
0025  *
0026  */
0027 class TrashSizeCache
0028 {
0029 public:
0030     struct SizeAndModTime {
0031         qint64 size;
0032         qint64 mtime;
0033     };
0034 
0035     /**
0036      * Creates a new trash size cache object for the given trash @p path.
0037      */
0038     explicit TrashSizeCache(const QString &path);
0039 
0040     /**
0041      * Adds a directory to the cache.
0042      * @param directoryName fileId of the directory
0043      * @param directorySize size in bytes
0044      */
0045     void add(const QString &directoryName, qint64 directorySize);
0046 
0047     /**
0048      * Removes a directory from the cache.
0049      */
0050     void remove(const QString &directoryName);
0051 
0052     /**
0053      * Renames a directory in the cache.
0054      */
0055     void rename(const QString &oldDirectoryName, const QString &newDirectoryName);
0056 
0057     /**
0058      * Sets the trash size to 0 bytes.
0059      */
0060     void clear();
0061 
0062     /**
0063      * Calculates and returns the current trash size.
0064      */
0065     qint64 calculateSize();
0066 
0067     /**
0068      * Calculates and returns the current trash size and its last modification date
0069      */
0070     SizeAndModTime calculateSizeAndLatestModDate();
0071 
0072     /**
0073      * Returns the space occupied by directories in trash and their latest modification dates
0074      */
0075     QHash<QByteArray, TrashSizeCache::SizeAndModTime> readDirCache();
0076 
0077 private:
0078     enum ScanFilesInTrashOption { CheckModificationTime, DonTcheckModificationTime };
0079     TrashSizeCache::SizeAndModTime scanFilesInTrash(ScanFilesInTrashOption checkDateTime = CheckModificationTime);
0080 
0081     QString mTrashSizeCachePath;
0082     QString mTrashPath;
0083     QFileInfo getTrashFileInfo(const QString &fileName);
0084 };
0085 
0086 #endif