]>
cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
2 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Patrice Tremblay
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "statusbarspaceinfo.h"
9 #include "spaceinfoobserver.h"
11 #include <KCapacityBar>
12 #include <KIO/ApplicationLauncherJob>
14 #include <KLocalizedString>
17 #include <QDesktopServices>
18 #include <QHBoxLayout>
20 #include <QMouseEvent>
21 #include <QStorageInfo>
22 #include <QToolButton>
24 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget
*parent
)
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
);
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
);
43 StatusBarSpaceInfo::~StatusBarSpaceInfo()
47 void StatusBarSpaceInfo::setShown(bool shown
)
56 void StatusBarSpaceInfo::setUrl(const QUrl
&url
)
62 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
63 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
68 QUrl
StatusBarSpaceInfo::url() const
73 void StatusBarSpaceInfo::update()
80 void StatusBarSpaceInfo::showEvent(QShowEvent
*event
)
84 QWidget::showEvent(event
);
87 if (m_observer
.isNull()) {
88 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
89 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
94 void StatusBarSpaceInfo::hideEvent(QHideEvent
*event
)
100 QWidget::hideEvent(event
);
103 QSize
StatusBarSpaceInfo::minimumSizeHint() const
108 void StatusBarSpaceInfo::updateMenu()
110 m_buttonMenu
->clear();
112 // Creates a menu with tools that help to find out more about free
113 // disk space for the given url.
115 const KService::Ptr filelight
= KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
116 const KService::Ptr kdiskfree
= KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
118 if (!filelight
&& !kdiskfree
) {
119 QAction
*installFilelight
=
120 m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Install Filelight to View Disk Usage Statistics…"));
122 connect(installFilelight
, &QAction::triggered
, this, [] {
124 QDesktopServices::openUrl(QUrl("https://apps.kde.org/filelight"));
126 QDesktopServices::openUrl(QUrl("appstream://org.kde.filelight.desktop"));
134 QAction
*filelightFolderAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
136 m_buttonMenu
->connect(filelightFolderAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
137 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
138 job
->setUrls({m_url
});
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"));
147 m_buttonMenu
->connect(filelightDiskAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
148 const QStorageInfo
info(m_url
.toLocalFile());
150 if (info
.isValid() && info
.isReady()) {
151 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
152 job
->setUrls({QUrl::fromLocalFile(info
.rootPath())});
158 QAction
*filelightAllAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
160 m_buttonMenu
->connect(filelightAllAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
161 const QStorageInfo
info(m_url
.toLocalFile());
163 if (info
.isValid() && info
.isReady()) {
164 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
171 QAction
*kdiskfreeAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
173 connect(kdiskfreeAction
, &QAction::triggered
, this, [kdiskfree
] {
174 auto *job
= new KIO::ApplicationLauncherJob(kdiskfree
);
180 void StatusBarSpaceInfo::slotValuesChanged()
182 Q_ASSERT(m_observer
);
183 const quint64 size
= m_observer
->size();
185 if (!m_shown
|| size
== 0) {
192 const quint64 available
= m_observer
->available();
193 const quint64 used
= size
- available
;
194 const int percentUsed
= qRound(100.0 * qreal(used
) / qreal(size
));
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
),
203 setUpdatesEnabled(false);
204 m_capacityBar
->setValue(percentUsed
);
205 setUpdatesEnabled(true);
214 #include "moc_statusbarspaceinfo.cpp"