]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Turn free space label into a flat button
[dolphin.git] / src / statusbar / statusbarspaceinfo.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "statusbarspaceinfo.h"
8
9 #include "spaceinfoobserver.h"
10
11 #include <KCapacityBar>
12 #include <KIO/ApplicationLauncherJob>
13 #include <KIO/Global>
14 #include <KLocalizedString>
15 #include <KService>
16
17 #include <QHBoxLayout>
18 #include <QMenu>
19 #include <QMouseEvent>
20 #include <QStorageInfo>
21 #include <QToolButton>
22
23 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
24 : QWidget(parent)
25 , m_observer(nullptr)
26 {
27 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
28 m_textInfoButton = new QToolButton(this);
29 m_textInfoButton->setAutoRaise(true);
30 m_textInfoButton->setPopupMode(QToolButton::InstantPopup);
31 m_buttonMenu = new QMenu(this);
32 m_textInfoButton->setMenu(m_buttonMenu);
33 connect(m_buttonMenu, &QMenu::aboutToShow, this, &StatusBarSpaceInfo::updateMenu);
34
35 auto layout = new QHBoxLayout(this);
36 // We reduce the outside margin of the flat button so it visually has the same margin as the status bar text label on the other end of the bar.
37 layout->setContentsMargins(2, -1, 0, -1); // "-1" makes it so the fixed height won't be ignored.
38 layout->addWidget(m_capacityBar);
39 layout->addWidget(m_textInfoButton);
40 }
41
42 StatusBarSpaceInfo::~StatusBarSpaceInfo()
43 {
44 }
45
46 void StatusBarSpaceInfo::setShown(bool shown)
47 {
48 m_shown = shown;
49 if (!m_shown) {
50 hide();
51 m_ready = false;
52 }
53 }
54
55 void StatusBarSpaceInfo::setUrl(const QUrl &url)
56 {
57 if (m_url != url) {
58 m_url = url;
59 m_ready = false;
60 if (m_observer) {
61 m_observer.reset(new SpaceInfoObserver(m_url, this));
62 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
63 }
64 }
65 }
66
67 QUrl StatusBarSpaceInfo::url() const
68 {
69 return m_url;
70 }
71
72 void StatusBarSpaceInfo::update()
73 {
74 if (m_observer) {
75 m_observer->update();
76 }
77 }
78
79 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
80 {
81 if (m_shown) {
82 if (m_ready) {
83 QWidget::showEvent(event);
84 }
85
86 if (m_observer.isNull()) {
87 m_observer.reset(new SpaceInfoObserver(m_url, this));
88 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
89 }
90 }
91 }
92
93 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
94 {
95 if (m_ready) {
96 m_observer.reset();
97 m_ready = false;
98 }
99 QWidget::hideEvent(event);
100 }
101
102 void StatusBarSpaceInfo::updateMenu()
103 {
104 m_buttonMenu->clear();
105
106 // Creates a menu with tools that help to find out more about free
107 // disk space for the given url.
108
109 const KService::Ptr filelight = KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
110 const KService::Ptr kdiskfree = KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
111
112 if (!filelight && !kdiskfree) {
113 // nothing to show
114 return;
115 }
116
117 if (filelight) {
118 QAction *filelightFolderAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
119
120 m_buttonMenu->connect(filelightFolderAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
121 auto *job = new KIO::ApplicationLauncherJob(filelight);
122 job->setUrls({m_url});
123 job->start();
124 });
125
126 // For remote URLs like FTP analyzing the device makes no sense
127 if (m_url.isLocalFile()) {
128 QAction *filelightDiskAction =
129 m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
130
131 m_buttonMenu->connect(filelightDiskAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
132 const QStorageInfo info(m_url.toLocalFile());
133
134 if (info.isValid() && info.isReady()) {
135 auto *job = new KIO::ApplicationLauncherJob(filelight);
136 job->setUrls({QUrl::fromLocalFile(info.rootPath())});
137 job->start();
138 }
139 });
140 }
141
142 QAction *filelightAllAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
143
144 m_buttonMenu->connect(filelightAllAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
145 const QStorageInfo info(m_url.toLocalFile());
146
147 if (info.isValid() && info.isReady()) {
148 auto *job = new KIO::ApplicationLauncherJob(filelight);
149 job->start();
150 }
151 });
152 }
153
154 if (kdiskfree) {
155 QAction *kdiskfreeAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
156
157 connect(kdiskfreeAction, &QAction::triggered, this, [kdiskfree] {
158 auto *job = new KIO::ApplicationLauncherJob(kdiskfree);
159 job->start();
160 });
161 }
162 }
163
164 void StatusBarSpaceInfo::slotValuesChanged()
165 {
166 Q_ASSERT(m_observer);
167 const quint64 size = m_observer->size();
168
169 if (!m_shown || size == 0) {
170 hide();
171 return;
172 }
173
174 m_ready = true;
175
176 const quint64 available = m_observer->available();
177 const quint64 used = size - available;
178 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
179
180 m_textInfoButton->setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
181 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available), KIO::convertSize(size), percentUsed));
182 m_textInfoButton->setToolTip(toolTip());
183 setUpdatesEnabled(false);
184 m_capacityBar->setValue(percentUsed);
185 setUpdatesEnabled(true);
186
187 if (!isVisible()) {
188 show();
189 } else {
190 update();
191 }
192 }
193
194 #include "moc_statusbarspaceinfo.cpp"