mirror of
https://invent.kde.org/system/dolphin
synced 2024-10-28 03:21:56 +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 <QDebug>
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
#include <QGraphicsView>
|
#include <QGraphicsView>
|
||||||
#include <QStyleOption>
|
#include <QStyleOption>
|
||||||
|
|
||||||
#include <KDiskFreeSpaceInfo>
|
#include <KIO/FileSystemFreeSpaceJob>
|
||||||
#include <KMountPoint>
|
#include <KMountPoint>
|
||||||
|
|
||||||
#define CAPACITYBAR_HEIGHT 2
|
#define CAPACITYBAR_HEIGHT 2
|
||||||
|
@ -20,9 +21,7 @@
|
||||||
|
|
||||||
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
|
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
|
||||||
KStandardItemListWidget(informant, parent)
|
KStandardItemListWidget(informant, parent)
|
||||||
, m_isMountPoint(false)
|
|
||||||
, m_drawCapacityBar(false)
|
, m_drawCapacityBar(false)
|
||||||
, m_capacityBarRatio(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,31 +45,53 @@ void PlacesItemListWidget::updateCapacityBar()
|
||||||
const bool isDevice = !data().value("udi").toString().isEmpty();
|
const bool isDevice = !data().value("udi").toString().isEmpty();
|
||||||
const QUrl url = data().value("url").toUrl();
|
const QUrl url = data().value("url").toUrl();
|
||||||
if (isDevice && url.isLocalFile()) {
|
if (isDevice && url.isLocalFile()) {
|
||||||
const QString mountPointPath = url.toLocalFile();
|
if (!m_freeSpaceInfo.job
|
||||||
qDebug() << "url:" << mountPointPath;
|
&& (
|
||||||
KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
|
!m_freeSpaceInfo.lastUpdated.isValid()
|
||||||
m_isMountPoint = (mp && mp->mountPoint() == mountPointPath);
|
|| m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
|
||||||
qDebug() << " isMountPoint:" << m_isMountPoint;
|
)
|
||||||
if (m_isMountPoint) {
|
) {
|
||||||
const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
|
// qDebug() << "url:" << url;
|
||||||
m_drawCapacityBar = info.size() != 0;
|
m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
|
||||||
m_capacityBarRatio = (qreal)info.used() / (qreal)info.size();
|
connect(
|
||||||
qDebug() << " capacityBarRatio:" << m_capacityBarRatio << "(" << info.used() << "/" << info.size() << ")";
|
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();
|
if (job->error()) {
|
||||||
return;
|
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()
|
void PlacesItemListWidget::resetCapacityBar()
|
||||||
{
|
{
|
||||||
m_isMountPoint = false;
|
|
||||||
m_drawCapacityBar = false;
|
m_drawCapacityBar = false;
|
||||||
m_capacityBarRatio = 0;
|
m_freeSpaceInfo.size = 0;
|
||||||
|
m_freeSpaceInfo.used = 0;
|
||||||
|
m_freeSpaceInfo.usedRatio = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemListWidget::polishEvent()
|
void PlacesItemListWidget::polishEvent()
|
||||||
|
@ -129,8 +150,8 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt
|
||||||
// painter->drawRect(outlineRect);
|
// painter->drawRect(outlineRect);
|
||||||
|
|
||||||
// Fill
|
// Fill
|
||||||
const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_capacityBarRatio, capacityRect.height());
|
const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
|
||||||
if (m_capacityBarRatio < 0.95) { // Fill
|
if (m_freeSpaceInfo.usedRatio < 0.95) { // Fill
|
||||||
painter->fillRect(fillRect, normalUsedColor);
|
painter->fillRect(fillRect, normalUsedColor);
|
||||||
} else {
|
} else {
|
||||||
painter->fillRect(fillRect, dangerUsedColor);
|
painter->fillRect(fillRect, dangerUsedColor);
|
||||||
|
@ -139,4 +160,6 @@ void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIt
|
||||||
painter->restore();
|
painter->restore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateCapacityBar();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,24 @@
|
||||||
|
|
||||||
#include "kitemviews/kstandarditemlistwidget.h"
|
#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
|
* @brief Extends KStandardItemListWidget to interpret the hidden
|
||||||
* property of the PlacesModel and use the right text color.
|
* property of the PlacesModel and use the right text color.
|
||||||
|
@ -31,9 +49,8 @@ protected:
|
||||||
void resetCapacityBar();
|
void resetCapacityBar();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_isMountPoint;
|
|
||||||
bool m_drawCapacityBar;
|
bool m_drawCapacityBar;
|
||||||
qreal m_capacityBarRatio;
|
PlaceFreeSpaceInfo m_freeSpaceInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue