File indexing completed on 2024-05-05 17:56:56

0001 /*
0002     SPDX-FileCopyrightText: 2017-2022 Krusader Krew <https://krusader.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef SIZECALCULATOR_H
0008 #define SIZECALCULATOR_H
0009 
0010 // QtCore
0011 #include <QPointer>
0012 #include <QUrl>
0013 
0014 #include <KIO/DirectorySizeJob>
0015 
0016 /**
0017  * Calculate the size of files and directories (recursive).
0018  *
0019  * Krusader's virtual filesystem and all KIO protocols are supported.
0020  *
0021  * This calculator will delete itself when its finished (like KJob).
0022  */
0023 class SizeCalculator : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     /**
0028      * Calculate the size of files and directories defined by URLs.
0029      *
0030      * The calculation is automatically started (like KJob).
0031      */
0032     explicit SizeCalculator(const QList<QUrl> &urls);
0033     ~SizeCalculator() override;
0034 
0035     /** Return all URLs (queued and progressed). */
0036     QList<QUrl> urls() const
0037     {
0038         return m_urls;
0039     }
0040     /** Add a URL to the running calculation. */
0041     void add(const QUrl &url);
0042     /** Return the current total size calculation result. */
0043     KIO::filesize_t totalSize() const;
0044     /** Return the current total number of files counted. */
0045     unsigned long totalFiles() const;
0046     /** Return the current number of directories counted. */
0047     unsigned long totalDirs() const;
0048 
0049 public slots:
0050     /** Cancel the calculation and mark for deletion. finished() will be emitted.*/
0051     void cancel();
0052 
0053 signals:
0054     /** Emitted when the calculation starts. */
0055     void started();
0056     /** Emitted when an intermediate size for one of the URLs was calculated. */
0057     void calculated(const QUrl &url, KIO::filesize_t size);
0058     /** Emitted when calculation is finished or was canceled. Calculator will be deleted afterwards. */
0059     void finished(bool canceled);
0060     /** Emitted when the progress changes. */
0061     void progressChanged(int percentage);
0062 
0063 private:
0064     void nextSubUrl();
0065     void done();
0066     void emitProgress();
0067 
0068 private slots:
0069     void start();
0070     void nextUrl();
0071     void slotStatResult(KJob *job);
0072     void slotDirectorySizeResult(KJob *job);
0073 
0074 private:
0075     QList<QUrl> m_urls; // all URLs
0076 
0077     QList<QUrl> m_nextUrls; // URLs not calculated yet
0078     QUrl m_currentUrl;
0079     QList<QUrl> m_nextSubUrls;
0080 
0081     KIO::filesize_t m_currentUrlSize;
0082     KIO::filesize_t m_totalSize;
0083     unsigned long m_totalFiles;
0084     unsigned long m_totalDirs;
0085 
0086     bool m_canceled;
0087 
0088     QPointer<KIO::DirectorySizeJob> m_directorySizeJob;
0089 };
0090 
0091 #endif // SIZECALCULATOR_H