File indexing completed on 2025-01-05 04:29:55

0001 /**
0002  * SPDX-FileCopyrightText: 2021 Bart De Vries <bart@mogwai.be>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QFile>
0010 #include <QObject>
0011 #include <QQmlEngine>
0012 #include <QString>
0013 #include <QUrl>
0014 
0015 #include <KFormat>
0016 
0017 #include "error.h"
0018 
0019 class StorageManager : public QObject
0020 {
0021     Q_OBJECT
0022     QML_ELEMENT
0023     QML_SINGLETON
0024 
0025     Q_PROPERTY(int storageMoveProgress MEMBER m_storageMoveProgress NOTIFY storageMoveProgressChanged)
0026     Q_PROPERTY(int storageMoveTotal MEMBER m_storageMoveTotal NOTIFY storageMoveTotalChanged)
0027     Q_PROPERTY(QString storagePath READ storagePath NOTIFY storagePathChanged)
0028     Q_PROPERTY(qint64 enclosureDirSize READ enclosureDirSize NOTIFY enclosureDirSizeChanged)
0029     Q_PROPERTY(qint64 imageDirSize READ imageDirSize NOTIFY imageDirSizeChanged)
0030     Q_PROPERTY(QString formattedEnclosureDirSize READ formattedEnclosureDirSize NOTIFY enclosureDirSizeChanged)
0031     Q_PROPERTY(QString formattedImageDirSize READ formattedImageDirSize NOTIFY imageDirSizeChanged)
0032 
0033 public:
0034     static StorageManager &instance()
0035     {
0036         static StorageManager _instance;
0037         return _instance;
0038     }
0039     static StorageManager *create(QQmlEngine *engine, QJSEngine *)
0040     {
0041         engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
0042         return &instance();
0043     }
0044 
0045     static const int maxFilenameLength = 200;
0046 
0047     QString storagePath() const;
0048     Q_INVOKABLE void setStoragePath(QUrl url);
0049 
0050     QString imageDirPath() const;
0051     QString imagePath(const QString &url) const;
0052 
0053     QString enclosureDirPath() const;
0054     QString enclosureDirPath(const QString &feedname) const;
0055     QString enclosurePath(const QString &name, const QString &url, const QString &feedname) const;
0056 
0057     qint64 enclosureDirSize() const;
0058     qint64 imageDirSize() const;
0059     QString formattedEnclosureDirSize() const;
0060     QString formattedImageDirSize() const;
0061 
0062     void removeImage(const QString &url);
0063     Q_INVOKABLE void clearImageCache();
0064 
0065     QString passwordFilePath(const QString &username) const;
0066 
0067     QString sanitizedFilePath(const QString &path) const;
0068 
0069 Q_SIGNALS:
0070     void error(Error::Type type, const QString &url, const QString &id, const int errorId, const QString &errorString, const QString &title);
0071 
0072     void storagePathChanged(QString path);
0073     void storageMoveStarted();
0074     void storageMoveFinished();
0075     void storageMoveProgressChanged(int progress);
0076     void storageMoveTotalChanged(int nrOfFeeds);
0077     void cancelStorageMove();
0078 
0079     void enclosureDirSizeChanged();
0080     void imageDirSizeChanged();
0081 
0082 private:
0083     StorageManager();
0084 
0085     qint64 dirSize(const QString &path) const;
0086 
0087     int m_storageMoveProgress;
0088     int m_storageMoveTotal;
0089     KFormat m_kformat;
0090 };