From 89ede4b9a170ffd2f3f155f081630edf3f2998ab Mon Sep 17 00:00:00 2001 From: Chris Holland Date: Fri, 12 Jun 2020 16:06:59 -0400 Subject: [PATCH] Refactor capacity by to use async KIO FreeSpaceJob. Based on this kio filepicker patch: https://github.com/KDE/kio/commit/933887dc334f3498505af7a86d25db7faae91019 --- src/panels/places/placesitemlistwidget.cpp | 67 +++++++++++++++------- src/panels/places/placesitemlistwidget.h | 21 ++++++- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/panels/places/placesitemlistwidget.cpp b/src/panels/places/placesitemlistwidget.cpp index c494469014..4be7ac021a 100644 --- a/src/panels/places/placesitemlistwidget.cpp +++ b/src/panels/places/placesitemlistwidget.cpp @@ -8,10 +8,11 @@ #include +#include #include #include -#include +#include #include #define CAPACITYBAR_HEIGHT 2 @@ -20,9 +21,7 @@ PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) : KStandardItemListWidget(informant, parent) - , m_isMountPoint(false) , m_drawCapacityBar(false) - , m_capacityBarRatio(0) { } @@ -46,31 +45,53 @@ void PlacesItemListWidget::updateCapacityBar() 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() << ")"; + 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(); - // update(); - return; + 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() @@ -129,8 +150,8 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt // 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); @@ -139,4 +160,6 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt painter->restore(); } } + + updateCapacityBar(); } diff --git a/src/panels/places/placesitemlistwidget.h b/src/panels/places/placesitemlistwidget.h index a82616ff3a..f33f2f2c85 100644 --- a/src/panels/places/placesitemlistwidget.h +++ b/src/panels/places/placesitemlistwidget.h @@ -9,6 +9,24 @@ #include "kitemviews/kstandarditemlistwidget.h" +#include +#include + +#include + + +// 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 job; +}; + + /** * @brief Extends KStandardItemListWidget to interpret the hidden * property of the PlacesModel and use the right text color. @@ -31,9 +49,8 @@ protected: void resetCapacityBar(); private: - bool m_isMountPoint; bool m_drawCapacityBar; - qreal m_capacityBarRatio; + PlaceFreeSpaceInfo m_freeSpaceInfo; }; #endif