]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Improve Filelight installation UX
[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 "config-dolphin.h"
10 #include "dolphinpackageinstaller.h"
11 #include "global.h"
12 #include "spaceinfoobserver.h"
13
14 #include <KCapacityBar>
15 #include <KIO/ApplicationLauncherJob>
16 #include <KIO/Global>
17 #include <KLocalizedString>
18 #include <KService>
19
20 #include <QDesktopServices>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QMenu>
24 #include <QMouseEvent>
25 #include <QPushButton>
26 #include <QStorageInfo>
27 #include <QToolButton>
28 #include <QVBoxLayout>
29 #include <QWidgetAction>
30
31 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
32 : QWidget(parent)
33 , m_observer(nullptr)
34 , m_installFilelightWidgetAction{nullptr}
35 {
36 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
37 m_textInfoButton = new QToolButton(this);
38 m_textInfoButton->setAutoRaise(true);
39 m_textInfoButton->setPopupMode(QToolButton::InstantPopup);
40 m_buttonMenu = new QMenu(this);
41 m_textInfoButton->setMenu(m_buttonMenu);
42 connect(m_buttonMenu, &QMenu::aboutToShow, this, &StatusBarSpaceInfo::updateMenu);
43
44 auto layout = new QHBoxLayout(this);
45 // 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.
46 layout->setContentsMargins(2, -1, 0, -1); // "-1" makes it so the fixed height won't be ignored.
47 layout->addWidget(m_capacityBar);
48 layout->addWidget(m_textInfoButton);
49 }
50
51 StatusBarSpaceInfo::~StatusBarSpaceInfo()
52 {
53 }
54
55 void StatusBarSpaceInfo::setShown(bool shown)
56 {
57 m_shown = shown;
58 if (!m_shown) {
59 hide();
60 m_ready = false;
61 }
62 }
63
64 void StatusBarSpaceInfo::setUrl(const QUrl &url)
65 {
66 if (m_url != url) {
67 m_url = url;
68 m_ready = false;
69 if (m_observer) {
70 m_observer.reset(new SpaceInfoObserver(m_url, this));
71 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
72 }
73 }
74 }
75
76 QUrl StatusBarSpaceInfo::url() const
77 {
78 return m_url;
79 }
80
81 void StatusBarSpaceInfo::update()
82 {
83 if (m_observer) {
84 m_observer->update();
85 }
86 }
87
88 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
89 {
90 if (m_shown) {
91 if (m_ready) {
92 QWidget::showEvent(event);
93 }
94
95 if (m_observer.isNull()) {
96 m_observer.reset(new SpaceInfoObserver(m_url, this));
97 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
98 }
99 }
100 }
101
102 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
103 {
104 if (m_ready) {
105 m_observer.reset();
106 m_ready = false;
107 }
108 QWidget::hideEvent(event);
109 }
110
111 QSize StatusBarSpaceInfo::minimumSizeHint() const
112 {
113 return QSize();
114 }
115
116 void StatusBarSpaceInfo::updateMenu()
117 {
118 m_buttonMenu->clear();
119
120 // Creates a menu with tools that help to find out more about free
121 // disk space for the given url.
122
123 const KService::Ptr filelight = KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
124 const KService::Ptr kdiskfree = KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
125
126 if (!filelight && !kdiskfree) {
127 // Show an UI to install a tool to free up disk space because this is what a user pressing on a "free space" button would want.
128 if (!m_installFilelightWidgetAction) {
129 initialiseInstallFilelightWidgetAction();
130 }
131 m_buttonMenu->addAction(m_installFilelightWidgetAction);
132 return;
133 }
134
135 if (filelight) {
136 QAction *filelightFolderAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
137
138 m_buttonMenu->connect(filelightFolderAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
139 auto *job = new KIO::ApplicationLauncherJob(filelight);
140 job->setUrls({m_url});
141 job->start();
142 });
143
144 // For remote URLs like FTP analyzing the device makes no sense
145 if (m_url.isLocalFile()) {
146 QAction *filelightDiskAction =
147 m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
148
149 m_buttonMenu->connect(filelightDiskAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
150 const QStorageInfo info(m_url.toLocalFile());
151
152 if (info.isValid() && info.isReady()) {
153 auto *job = new KIO::ApplicationLauncherJob(filelight);
154 job->setUrls({QUrl::fromLocalFile(info.rootPath())});
155 job->start();
156 }
157 });
158 }
159
160 QAction *filelightAllAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
161
162 m_buttonMenu->connect(filelightAllAction, &QAction::triggered, m_buttonMenu, [this, filelight](bool) {
163 const QStorageInfo info(m_url.toLocalFile());
164
165 if (info.isValid() && info.isReady()) {
166 auto *job = new KIO::ApplicationLauncherJob(filelight);
167 job->start();
168 }
169 });
170 }
171
172 if (kdiskfree) {
173 QAction *kdiskfreeAction = m_buttonMenu->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
174
175 connect(kdiskfreeAction, &QAction::triggered, this, [kdiskfree] {
176 auto *job = new KIO::ApplicationLauncherJob(kdiskfree);
177 job->start();
178 });
179 }
180 }
181
182 void StatusBarSpaceInfo::slotInstallFilelightButtonClicked()
183 {
184 #ifdef Q_OS_WIN
185 QDesktopServices::openUrl(QUrl("https://apps.kde.org/filelight"));
186 #else
187 auto packageInstaller = new DolphinPackageInstaller(
188 FILELIGHT_PACKAGE_NAME,
189 QUrl("appstream://org.kde.filelight.desktop"),
190 []() {
191 return KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
192 },
193 this);
194 connect(packageInstaller, &KJob::result, this, [this](KJob *job) {
195 Q_EMIT showInstallationProgress(QString(), 100); // Hides the progress information in the status bar.
196 if (job->error()) {
197 Q_EMIT showMessage(job->errorString(), KMessageWidget::Error);
198 } else {
199 Q_EMIT showMessage(xi18nc("@info", "<application>Filelight</application> installed successfully."), KMessageWidget::Positive);
200 if (m_textInfoButton->menu()->isVisible()) {
201 m_textInfoButton->menu()->hide();
202 updateMenu();
203 m_textInfoButton->menu()->show();
204 }
205 }
206 });
207 const auto installationTaskText{i18nc("@info:status", "Installing Filelight…")};
208 Q_EMIT showInstallationProgress(installationTaskText, -1);
209 connect(packageInstaller, &KJob::percentChanged, this, [this, installationTaskText](KJob * /* job */, long unsigned int percent) {
210 if (percent < 100) { // Ignore some weird reported values.
211 Q_EMIT showInstallationProgress(installationTaskText, percent);
212 }
213 });
214 packageInstaller->start();
215 #endif
216 }
217
218 void StatusBarSpaceInfo::slotValuesChanged()
219 {
220 Q_ASSERT(m_observer);
221 const quint64 size = m_observer->size();
222
223 if (!m_shown || size == 0) {
224 hide();
225 return;
226 }
227
228 m_ready = true;
229
230 const quint64 available = m_observer->available();
231 const quint64 used = size - available;
232 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
233
234 m_textInfoButton->setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
235 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available), KIO::convertSize(size), percentUsed));
236 m_textInfoButton->setToolTip(i18nc("@info:tooltip for the free disk space button",
237 "%1 free out of %2 (%3% used)\nPress to manage disk space usage.",
238 KIO::convertSize(available),
239 KIO::convertSize(size),
240 percentUsed));
241 setUpdatesEnabled(false);
242 m_capacityBar->setValue(percentUsed);
243 setUpdatesEnabled(true);
244
245 if (!isVisible()) {
246 show();
247 } else {
248 update();
249 }
250 }
251
252 void StatusBarSpaceInfo::initialiseInstallFilelightWidgetAction()
253 {
254 Q_ASSERT(!m_installFilelightWidgetAction);
255
256 auto containerWidget = new QWidget{this};
257 containerWidget->setContentsMargins(Dolphin::VERTICAL_SPACER_HEIGHT,
258 Dolphin::VERTICAL_SPACER_HEIGHT,
259 Dolphin::VERTICAL_SPACER_HEIGHT, // Using the same value for every spacing in this containerWidget looks nice.
260 Dolphin::VERTICAL_SPACER_HEIGHT);
261 auto vLayout = new QVBoxLayout(containerWidget);
262
263 auto installFilelightTitle = new QLabel(i18nc("@title", "Free Up Disk Space"), containerWidget);
264 installFilelightTitle->setAlignment(Qt::AlignCenter);
265 installFilelightTitle->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::LinksAccessibleByKeyboard);
266 QFont titleFont{installFilelightTitle->font()};
267 titleFont.setPointSize(titleFont.pointSize() + 2);
268 installFilelightTitle->setFont(titleFont);
269 vLayout->addWidget(installFilelightTitle);
270
271 vLayout->addSpacing(Dolphin::VERTICAL_SPACER_HEIGHT);
272
273 auto installFilelightBody =
274 // i18n: The new line ("<nl/>") tag is only there to format this text visually pleasing, i.e. to avoid having one very long line.
275 new QLabel(xi18nc("@title", "<para>Install additional software to view disk usage statistics<nl/>and identify big files and folders.</para>"),
276 containerWidget);
277 installFilelightBody->setAlignment(Qt::AlignCenter);
278 installFilelightBody->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard | Qt::LinksAccessibleByKeyboard);
279 vLayout->addWidget(installFilelightBody);
280
281 vLayout->addSpacing(Dolphin::VERTICAL_SPACER_HEIGHT);
282
283 auto installFilelightButton =
284 new QPushButton(QIcon::fromTheme(QStringLiteral("filelight")), i18nc("@action:button", "Install Filelight…"), containerWidget);
285 installFilelightButton->setMinimumWidth(std::max(installFilelightButton->sizeHint().width(), installFilelightTitle->sizeHint().width()));
286 auto buttonLayout = new QHBoxLayout; // The parent is automatically set on addLayout() below.
287 buttonLayout->addWidget(installFilelightButton, 0, Qt::AlignHCenter);
288 vLayout->addLayout(buttonLayout);
289
290 // Make sure one Tab press focuses the button after the UI opened.
291 m_buttonMenu->setFocusProxy(installFilelightButton);
292 containerWidget->setFocusPolicy(Qt::TabFocus);
293 containerWidget->setFocusProxy(installFilelightButton);
294 installFilelightButton->setAccessibleDescription(installFilelightBody->text());
295 connect(installFilelightButton, &QAbstractButton::clicked, this, &StatusBarSpaceInfo::slotInstallFilelightButtonClicked);
296
297 m_installFilelightWidgetAction = new QWidgetAction{this};
298 m_installFilelightWidgetAction->setDefaultWidget(containerWidget); // transfers ownership of containerWidget
299 }
300
301 #include "moc_statusbarspaceinfo.cpp"