]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Offer installing Filelight if no disk usage analyzer was found
[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 <QDesktopServices>
18 #include <QHBoxLayout>
19 #include <QMenu>
20 #include <QMouseEvent>
21 #include <QStorageInfo>
22 #include <QToolButton>
23
24 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
25 : QWidget(parent)
26 , m_observer(nullptr)
27 {
28 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
29 m_textInfoButton = new QToolButton(this);
30 m_textInfoButton->setAutoRaise(true);
31 m_textInfoButton->setPopupMode(QToolButton::InstantPopup);
32 m_buttonMenu = new QMenu(this);
33 m_textInfoButton->setMenu(m_buttonMenu);
34 connect(m_buttonMenu, &QMenu::aboutToShow, this, &StatusBarSpaceInfo::updateMenu);
35
36 auto layout = new QHBoxLayout(this);
37 // 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.
38 layout->setContentsMargins(2, -1, 0, -1); // "-1" makes it so the fixed height won't be ignored.
39 layout->addWidget(m_capacityBar);
40 layout->addWidget(m_textInfoButton);
41 }
42
43 StatusBarSpaceInfo::~StatusBarSpaceInfo()
44 {
45 }
46
47 void StatusBarSpaceInfo::setShown(bool shown)
48 {
49 m_shown = shown;
50 if (!m_shown) {
51 hide();
52 m_ready = false;
53 }
54 }
55
56 void StatusBarSpaceInfo::setUrl(const QUrl &url)
57 {
58 if (m_url != url) {
59 m_url = url;
60 m_ready = false;
61 if (m_observer) {
62 m_observer.reset(new SpaceInfoObserver(m_url, this));
63 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
64 }
65 }
66 }
67
68 QUrl StatusBarSpaceInfo::url() const
69 {
70 return m_url;
71 }
72
73 void StatusBarSpaceInfo::update()
74 {
75 if (m_observer) {
76 m_observer->update();
77 }
78 }
79
80 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
81 {
82 if (m_shown) {
83 if (m_ready) {
84 QWidget::showEvent(event);
85 }
86
87 if (m_observer.isNull()) {
88 m_observer.reset(new SpaceInfoObserver(m_url, this));
89 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
90 }
91 }
92 }
93
94 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
95 {
96 if (m_ready) {
97 m_observer.reset();
98 m_ready = false;
99 }
100 QWidget::hideEvent(event);
101 }
102
103 QSize StatusBarSpaceInfo::minimumSizeHint() const
104 {
105 return QSize();
106 }
107
108 void StatusBarSpaceInfo::updateMenu()
109 {
110 m_buttonMenu->clear();
111
112 // Creates a menu with tools that help to find out more about free
113 // disk space for the given url.
114
115 const KService::Ptr filelight = KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
116 const KService::Ptr kdiskfree = KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
117
118 if (!filelight && !kdiskfree) {
119 QAction *installFilelight =
120 m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Install Filelight to View Disk Usage Statistics…"));
121
122 connect(installFilelight, &QAction::triggered, this, [] {
123 #ifdef Q_OS_WIN
124 QDesktopServices::openUrl(QUrl("https://apps.kde.org/filelight"));
125 #else
126 QDesktopServices::openUrl(QUrl("appstream://org.kde.filelight.desktop"));
127 #endif
128 });
129
130 return;
131 }
132
133 if (filelight) {
134 QAction *filelightFolderAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
135
136 m_buttonMenu->connect(filelightFolderAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
137 auto *job = new KIO::ApplicationLauncherJob(filelight);
138 job->setUrls({m_url});
139 job->start();
140 });
141
142 // For remote URLs like FTP analyzing the device makes no sense
143 if (m_url.isLocalFile()) {
144 QAction *filelightDiskAction =
145 m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
146
147 m_buttonMenu->connect(filelightDiskAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
148 const QStorageInfo info(m_url.toLocalFile());
149
150 if (info.isValid() && info.isReady()) {
151 auto *job = new KIO::ApplicationLauncherJob(filelight);
152 job->setUrls({QUrl::fromLocalFile(info.rootPath())});
153 job->start();
154 }
155 });
156 }
157
158 QAction *filelightAllAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
159
160 m_buttonMenu->connect(filelightAllAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
161 const QStorageInfo info(m_url.toLocalFile());
162
163 if (info.isValid() && info.isReady()) {
164 auto *job = new KIO::ApplicationLauncherJob(filelight);
165 job->start();
166 }
167 });
168 }
169
170 if (kdiskfree) {
171 QAction *kdiskfreeAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
172
173 connect(kdiskfreeAction, &QAction::triggered, this, [kdiskfree] {
174 auto *job = new KIO::ApplicationLauncherJob(kdiskfree);
175 job->start();
176 });
177 }
178 }
179
180 void StatusBarSpaceInfo::slotValuesChanged()
181 {
182 Q_ASSERT(m_observer);
183 const quint64 size = m_observer->size();
184
185 if (!m_shown || size == 0) {
186 hide();
187 return;
188 }
189
190 m_ready = true;
191
192 const quint64 available = m_observer->available();
193 const quint64 used = size - available;
194 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
195
196 m_textInfoButton->setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
197 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available), KIO::convertSize(size), percentUsed));
198 m_textInfoButton->setToolTip(i18nc("@info:tooltip for the free disk space button",
199 "%1 free out of %2 (%3% used)\nPress to manage disk space usage.",
200 KIO::convertSize(available),
201 KIO::convertSize(size),
202 percentUsed));
203 setUpdatesEnabled(false);
204 m_capacityBar->setValue(percentUsed);
205 setUpdatesEnabled(true);
206
207 if (!isVisible()) {
208 show();
209 } else {
210 update();
211 }
212 }
213
214 #include "moc_statusbarspaceinfo.cpp"