Files
z-bar-qt/Plugins/ZShell/Services/storage.hpp
T

97 lines
2.7 KiB
C++

#pragma once
#include "diskinfo.hpp"
#include "tickingservice.hpp"
#include <qbytearrayview.h>
#include <qpointer.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qtimer.h>
#include <qtypes.h>
#include <qvariant.h>
#include <qvector.h>
template <typename T> class QFutureWatcher;
namespace ZShell::services {
struct StorageEntry {
// Key for the entry: sysfs block device name, or mount point for a mount with
// no backing block device (network mount). This is what DiskInfo reports as
// its mount, so the UI can show either.
QString mount;
quint64 usedBytes = 0;
quint64 totalBytes = 0;
bool hasRoot = false;
};
using StorageScan = QVector<StorageEntry>;
class Storage : public TickingService {
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
Q_PROPERTY(
QQmlListProperty<ZShell::services::DiskInfo> disks READ disksProp NOTIFY
disksChanged)
Q_PROPERTY(
ZShell::services::DiskInfo* manualPrimaryDisk READ manualPrimaryDisk
WRITE setManualPrimaryDisk NOTIFY manualPrimaryDiskChanged)
Q_PROPERTY(
ZShell::services::DiskInfo* primaryDisk READ primaryDisk NOTIFY
primaryDiskChanged)
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
public:
explicit Storage(QObject* parent = nullptr);
[[nodiscard]] qreal percentage() const;
[[nodiscard]] QQmlListProperty<DiskInfo> disksProp();
[[nodiscard]] DiskInfo* manualPrimaryDisk() const;
void setManualPrimaryDisk(DiskInfo* disk);
[[nodiscard]] DiskInfo* primaryDisk() const;
[[nodiscard]] bool loading() const;
signals:
void disksChanged();
void percentageChanged();
void manualPrimaryDiskChanged();
void primaryDiskChanged();
void loadingChanged();
protected:
void tick() override;
private:
[[nodiscard]] static QStringList resolveToPhysicalDisks(
const QString& devicePath);
[[nodiscard]] static bool isPseudoFs(QByteArrayView fsType);
[[nodiscard]] static bool sameOrder(
const QList<DiskInfo*>& a, const QList<DiskInfo*>& b);
static qsizetype disksCount(QQmlListProperty<DiskInfo>* prop);
static DiskInfo* disksAt(QQmlListProperty<DiskInfo>* prop, qsizetype i);
// Runs on a worker thread. statvfs on a mount whose backing storage is
// unreachable can block for the whole network timeout, so the scan must not
// run on the GUI thread. The result is plain data, applied on this object's
// thread once it arrives.
[[nodiscard]] static StorageScan scanVolumes();
void beginScan();
void onScanFinished();
void applyScan(const StorageScan& scan);
void setLoading(bool value);
QList<DiskInfo*> m_disks;
QPointer<DiskInfo> m_manualPrimaryDisk;
QFutureWatcher<StorageScan>* m_watcher = nullptr;
QTimer m_loadingTimer;
bool m_loading = false;
};
} // namespace ZShell::services