]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemlistwidget.cpp
Fix freeSpaceInfo cache not being reset.
[dolphin.git] / src / panels / places / placesitemlistwidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "placesitemlistwidget.h"
8
9 // #include <QDebug>
10
11 #include <QDateTime>
12 #include <QGraphicsView>
13 #include <QStyleOption>
14
15 #include <KColorScheme>
16 #include <KMountPoint>
17
18 #include <KIO/FileSystemFreeSpaceJob>
19
20 #define CAPACITYBAR_HEIGHT 2
21 #define CAPACITYBAR_MARGIN 2
22
23
24 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
25 KStandardItemListWidget(informant, parent)
26 , m_drawCapacityBar(false)
27 {
28 }
29
30 PlacesItemListWidget::~PlacesItemListWidget()
31 {
32 }
33
34 bool PlacesItemListWidget::isHidden() const
35 {
36 return data().value("isHidden").toBool() ||
37 data().value("isGroupHidden").toBool();
38 }
39
40 QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
41 {
42 return QPalette::WindowText;
43 }
44
45 void PlacesItemListWidget::updateCapacityBar()
46 {
47 const bool isDevice = !data().value("udi").toString().isEmpty();
48 const QUrl url = data().value("url").toUrl();
49 if (isDevice && url.isLocalFile()) {
50 if (!m_freeSpaceInfo.job
51 && (
52 !m_freeSpaceInfo.lastUpdated.isValid()
53 || m_freeSpaceInfo.lastUpdated.secsTo(QDateTime::currentDateTimeUtc()) > 60
54 )
55 ) {
56 // qDebug() << "url:" << url;
57 m_freeSpaceInfo.job = KIO::fileSystemFreeSpace(url);
58 connect(
59 m_freeSpaceInfo.job,
60 &KIO::FileSystemFreeSpaceJob::result,
61 this,
62 [this](KIO::Job *job, KIO::filesize_t size, KIO::filesize_t available) {
63 // even if we receive an error we want to refresh lastUpdated to avoid repeatedly querying in this case
64 m_freeSpaceInfo.lastUpdated = QDateTime::currentDateTimeUtc();
65
66 if (job->error()) {
67 return;
68 }
69
70 m_freeSpaceInfo.size = size;
71 m_freeSpaceInfo.used = size - available;
72 m_freeSpaceInfo.usedRatio = (qreal)m_freeSpaceInfo.used / (qreal)m_freeSpaceInfo.size;
73 m_drawCapacityBar = size > 0;
74 // qDebug() << "job.url:" << data().value("url").toUrl();
75 // qDebug() << "job.size:" << m_freeSpaceInfo.size;
76 // qDebug() << "job.used:" << m_freeSpaceInfo.used;
77 // qDebug() << "job.ratio:" << m_freeSpaceInfo.usedRatio;
78 // qDebug() << "job.draw:" << m_drawCapacityBar;
79
80 update();
81 }
82 );
83 } else {
84 // Job running or cache is still valid.
85 }
86 } else {
87 resetCapacityBar();
88 }
89 }
90
91 void PlacesItemListWidget::resetCapacityBar()
92 {
93 m_drawCapacityBar = false;
94 delete m_freeSpaceInfo.job;
95 m_freeSpaceInfo.lastUpdated = QDateTime();
96 m_freeSpaceInfo.size = 0;
97 m_freeSpaceInfo.used = 0;
98 m_freeSpaceInfo.usedRatio = 0;
99 }
100
101 void PlacesItemListWidget::polishEvent()
102 {
103 updateCapacityBar();
104
105 QGraphicsWidget::polishEvent();
106 }
107
108 void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
109 {
110 KStandardItemListWidget::paint(painter, option, widget);
111
112 if (m_drawCapacityBar) {
113 const TextInfo* textInfo = m_textInfo.value("text");
114 if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
115 painter->save();
116
117 const QRect capacityRect(
118 textInfo->pos.x(),
119 option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
120 qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
121 CAPACITYBAR_HEIGHT
122 );
123
124 const QPalette pal = palette();
125 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
126
127 // Background
128 const QColor bgColor = isSelected()
129 ? pal.color(group, QPalette::Highlight).darker(180)
130 : pal.color(group, QPalette::Window).darker(120);
131
132 painter->fillRect(capacityRect, bgColor);
133
134 // Fill
135 const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_freeSpaceInfo.usedRatio, capacityRect.height());
136 if (m_freeSpaceInfo.usedRatio >= 0.95) { // More than 95% full!
137 const QColor dangerUsedColor = KColorScheme(group, KColorScheme::View).foreground(KColorScheme::NegativeText).color();
138 painter->fillRect(fillRect, dangerUsedColor);
139 } else {
140 const QPalette::ColorRole role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
141 const QColor normalUsedColor = styleOption().palette.color(group, role);
142 painter->fillRect(fillRect, normalUsedColor);
143 }
144
145 painter->restore();
146 }
147 }
148
149 updateCapacityBar();
150 }