mirror of
https://invent.kde.org/system/dolphin
synced 2024-09-12 12:54:29 +00:00
Use QDeadlineTimer instead of QDateTime.
Use early returns instead of deeply nested if/else.
This commit is contained in:
parent
60c3fa8d39
commit
70656d08f9
|
@ -6,7 +6,6 @@
|
||||||
|
|
||||||
#include "placesitemlistwidget.h"
|
#include "placesitemlistwidget.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
|
||||||
#include <QStyleOption>
|
#include <QStyleOption>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
|
@ -16,6 +15,7 @@
|
||||||
|
|
||||||
#define CAPACITYBAR_HEIGHT 2
|
#define CAPACITYBAR_HEIGHT 2
|
||||||
#define CAPACITYBAR_MARGIN 2
|
#define CAPACITYBAR_MARGIN 2
|
||||||
|
#define CAPACITYBAR_CACHE_TTL 60000
|
||||||
|
|
||||||
|
|
||||||
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
|
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
|
||||||
|
@ -43,47 +43,44 @@ 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())) {
|
||||||
if (!m_freeSpaceInfo.job
|
|
||||||
&& (
|
|
||||||
!m_freeSpaceInfo.lastUpdated.isValid()
|
|
||||||
|| m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
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;
|
|
||||||
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Job running or cache is still valid.
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resetCapacityBar();
|
resetCapacityBar();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_freeSpaceInfo.job || !m_freeSpaceInfo.lastUpdated.hasExpired()) {
|
||||||
|
// Job running or cache is still valid.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.setRemainingTime(CAPACITYBAR_CACHE_TTL);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlacesItemListWidget::resetCapacityBar()
|
void PlacesItemListWidget::resetCapacityBar()
|
||||||
{
|
{
|
||||||
m_drawCapacityBar = false;
|
m_drawCapacityBar = false;
|
||||||
delete m_freeSpaceInfo.job;
|
delete m_freeSpaceInfo.job;
|
||||||
m_freeSpaceInfo.lastUpdated = QDateTime();
|
m_freeSpaceInfo.lastUpdated.setRemainingTime(0);
|
||||||
m_freeSpaceInfo.size = 0;
|
m_freeSpaceInfo.size = 0;
|
||||||
m_freeSpaceInfo.used = 0;
|
m_freeSpaceInfo.used = 0;
|
||||||
m_freeSpaceInfo.usedRatio = 0;
|
m_freeSpaceInfo.usedRatio = 0;
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
#include "kitemviews/kstandarditemlistwidget.h"
|
#include "kitemviews/kstandarditemlistwidget.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDeadlineTimer>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPointer>
|
#include <QPointer>
|
||||||
#include <QStyleOptionGraphicsItem>
|
#include <QStyleOptionGraphicsItem>
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
// https://invent.kde.org/frameworks/kio/-/commit/933887dc334f3498505af7a86d25db7faae91019
|
// https://invent.kde.org/frameworks/kio/-/commit/933887dc334f3498505af7a86d25db7faae91019
|
||||||
struct PlaceFreeSpaceInfo
|
struct PlaceFreeSpaceInfo
|
||||||
{
|
{
|
||||||
QDateTime lastUpdated;
|
QDeadlineTimer lastUpdated;
|
||||||
KIO::filesize_t used = 0;
|
KIO::filesize_t used = 0;
|
||||||
KIO::filesize_t size = 0;
|
KIO::filesize_t size = 0;
|
||||||
qreal usedRatio = 0;
|
qreal usedRatio = 0;
|
||||||
|
|
Loading…
Reference in a new issue