mirror of
https://invent.kde.org/system/dolphin
synced 2024-08-22 01:40:02 +00:00
Refactor capacity by to use async KIO FreeSpaceJob.
Based on this kio filepicker patch:
933887dc33
This commit is contained in:
parent
8f76df41da
commit
89ede4b9a1
|
@ -8,10 +8,11 @@
|
|||
|
||||
#include <QDebug>
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QGraphicsView>
|
||||
#include <QStyleOption>
|
||||
|
||||
#include <KDiskFreeSpaceInfo>
|
||||
#include <KIO/FileSystemFreeSpaceJob>
|
||||
#include <KMountPoint>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
|
|
@ -9,6 +9,24 @@
|
|||
|
||||
#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.
|
||||
|
@ -31,9 +49,8 @@ protected:
|
|||
void resetCapacityBar();
|
||||
|
||||
private:
|
||||
bool m_isMountPoint;
|
||||
bool m_drawCapacityBar;
|
||||
qreal m_capacityBarRatio;
|
||||
PlaceFreeSpaceInfo m_freeSpaceInfo;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue