#include <QDebug>
+#include <QDateTime>
#include <QGraphicsView>
#include <QStyleOption>
-#include <KDiskFreeSpaceInfo>
+#include <KIO/FileSystemFreeSpaceJob>
#include <KMountPoint>
#define CAPACITYBAR_HEIGHT 2
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
KStandardItemListWidget(informant, parent)
- , m_isMountPoint(false)
, m_drawCapacityBar(false)
- , m_capacityBarRatio(0)
{
}
const bool isDevice = !data().value("udi").toString().isEmpty();
const QUrl url = data().value("url").toUrl();
if (isDevice && url.isLocalFile()) {
- const QString mountPointPath = url.toLocalFile();
- qDebug() << "url:" << mountPointPath;
- KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
- m_isMountPoint = (mp && mp->mountPoint() == mountPointPath);
- qDebug() << " isMountPoint:" << m_isMountPoint;
- if (m_isMountPoint) {
- const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
- m_drawCapacityBar = info.size() != 0;
- m_capacityBarRatio = (qreal)info.used() / (qreal)info.size();
- qDebug() << " capacityBarRatio:" << m_capacityBarRatio << "(" << info.used() << "/" << info.size() << ")";
-
- // update();
- return;
+ if (!m_freeSpaceInfo.job
+ && (
+ !m_freeSpaceInfo.lastUpdated.isValid()
+ || m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
+ )
+ ) {
+ // qDebug() << "url:" << url;
+ m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
+ connect(
+ m_freeSpaceInfo.job,
+ &KIO::FileSystemFreeSpaceJob::result,
+ this,
+ [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
+ // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
+ m_freeSpaceInfo.lastUpdated = QDateTime::currentDateTimeUtc();
+
+ if (job->error()) {
+ return;
+ }
+
+ m_freeSpaceInfo.size = size;
+ m_freeSpaceInfo.used = size - available;
+ m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
+ m_drawCapacityBar = size > 0;
+ // qDebug() << "job.url:" << data().value("url").toUrl();
+ // qDebug() << "job.size:" << m_freeSpaceInfo.size;
+ // qDebug() << "job.used:" << m_freeSpaceInfo.used;
+ // qDebug() << "job.ratio:" << m_freeSpaceInfo.usedRatio;
+ // qDebug() << "job.draw:" << m_drawCapacityBar;
+
+ update();
+ }
+ );
+ } else {
+ // Job running or cache is still valid.
}
+ } else {
+ resetCapacityBar();
}
-
- // else
- resetCapacityBar();
}
void PlacesItemListWidget::resetCapacityBar()
{
- m_isMountPoint = false;
m_drawCapacityBar = false;
- m_capacityBarRatio = 0;
+ m_freeSpaceInfo.size = 0;
+ m_freeSpaceInfo.used = 0;
+ m_freeSpaceInfo.usedRatio = 0;
}
void PlacesItemListWidget::polishEvent()
// painter->drawRect(outlineRect);
// Fill
- const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_capacityBarRatio, capacityRect.height());
- if (m_capacityBarRatio < 0.95) { // Fill
+ const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
+ if (m_freeSpaceInfo.usedRatio < 0.95) { // Fill
painter->fillRect(fillRect, normalUsedColor);
} else {
painter->fillRect(fillRect, dangerUsedColor);
painter->restore();
}
}
+
+ updateCapacityBar();
}
#include "kitemviews/kstandarditemlistwidget.h"
+#include <QDateTime>
+#include <QPointer>
+
+#include <KIO/FileSystemFreeSpaceJob>
+
+
+// The free space / capacity bar is based on KFilePlacesView.
+// https://github.com/KDE/kio/commit/933887dc334f3498505af7a86d25db7faae91019
+struct PlaceFreeSpaceInfo
+{
+ QDateTime lastUpdated;
+ KIO::filesize_t used = 0;
+ KIO::filesize_t size = 0;
+ qreal usedRatio = 0;
+ QPointer<KIO::FileSystemFreeSpaceJob> job;
+};
+
+
/**
* @brief Extends KStandardItemListWidget to interpret the hidden
* property of the PlacesModel and use the right text color.
void resetCapacityBar();
private:
- bool m_isMountPoint;
bool m_drawCapacityBar;
- qreal m_capacityBarRatio;
+ PlaceFreeSpaceInfo m_freeSpaceInfo;
};
#endif