Files
z-bar-qt/Plugins/ZShell/Services/diskinfo.cpp
AramJonghu 467e44bb9f
C++ / build (pull_request) Failing after 15s
Lint & Format (JS/TS) / lint-format (pull_request) Successful in 28s
Python / lint-format (pull_request) Successful in 45s
Python / test (pull_request) Successful in 38s
Lint & Format (Rust) / lint-format (pull_request) Successful in 1m45s
tidy&format(all): all files formatted and relevant warnings resolved
2026-06-30 02:04:24 +02:00

76 lines
1.5 KiB
C++

#include "diskinfo.hpp"
namespace ZShell::services {
namespace {
constexpr qreal kKib = 1024.0;
} // namespace
DiskInfo::DiskInfo(
QString mount,
quint64 usedBytes,
quint64 totalBytes,
bool hasRoot,
QObject* parent)
: QObject(parent)
, m_mount(std::move(mount))
, m_usedBytes(usedBytes)
, m_totalBytes(totalBytes)
, m_hasRoot(hasRoot) {}
QString DiskInfo::mount() const {
return m_mount;
}
qreal DiskInfo::used() const {
return static_cast<qreal>(m_usedBytes) / kKib;
}
qreal DiskInfo::total() const {
return static_cast<qreal>(m_totalBytes) / kKib;
}
qreal DiskInfo::free() const {
const quint64 freeBytes =
m_totalBytes > m_usedBytes ? m_totalBytes - m_usedBytes : 0;
return static_cast<qreal>(freeBytes) / kKib;
}
qreal DiskInfo::perc() const {
return m_totalBytes > 0 ? static_cast<qreal>(m_usedBytes) /
static_cast<qreal>(m_totalBytes)
: 0.0;
}
bool DiskInfo::hasRoot() const {
return m_hasRoot;
}
void DiskInfo::update(quint64 usedBytes, quint64 totalBytes, bool hasRoot) {
const bool usedDiff = usedBytes != m_usedBytes;
const bool totalDiff = totalBytes != m_totalBytes;
const bool rootDiff = hasRoot != m_hasRoot;
m_usedBytes = usedBytes;
m_totalBytes = totalBytes;
m_hasRoot = hasRoot;
if (usedDiff) {
Q_EMIT usedChanged();
}
if (totalDiff) {
Q_EMIT totalChanged();
}
if (usedDiff || totalDiff) {
Q_EMIT freeChanged();
Q_EMIT percChanged();
}
if (rootDiff) {
Q_EMIT hasRootChanged();
}
}
} // namespace ZShell::services