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