]>
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 <QHBoxLayout>
19 #include <QMouseEvent>
20 #include <QStorageInfo>
21 #include <QToolButton>
23 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget
*parent
)
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
);
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
);
42 StatusBarSpaceInfo::~StatusBarSpaceInfo()
46 void StatusBarSpaceInfo::setShown(bool shown
)
55 void StatusBarSpaceInfo::setUrl(const QUrl
&url
)
61 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
62 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
67 QUrl
StatusBarSpaceInfo::url() const
72 void StatusBarSpaceInfo::update()
79 void StatusBarSpaceInfo::showEvent(QShowEvent
*event
)
83 QWidget::showEvent(event
);
86 if (m_observer
.isNull()) {
87 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
88 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
93 void StatusBarSpaceInfo::hideEvent(QHideEvent
*event
)
99 QWidget::hideEvent(event
);
102 void StatusBarSpaceInfo::updateMenu()
104 m_buttonMenu
->clear();
106 // Creates a menu with tools that help to find out more about free
107 // disk space for the given url.
109 const KService::Ptr filelight
= KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
110 const KService::Ptr kdiskfree
= KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
112 if (!filelight
&& !kdiskfree
) {
118 QAction
*filelightFolderAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
120 m_buttonMenu
->connect(filelightFolderAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
121 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
122 job
->setUrls({m_url
});
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"));
131 m_buttonMenu
->connect(filelightDiskAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
132 const QStorageInfo
info(m_url
.toLocalFile());
134 if (info
.isValid() && info
.isReady()) {
135 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
136 job
->setUrls({QUrl::fromLocalFile(info
.rootPath())});
142 QAction
*filelightAllAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
144 m_buttonMenu
->connect(filelightAllAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
145 const QStorageInfo
info(m_url
.toLocalFile());
147 if (info
.isValid() && info
.isReady()) {
148 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
155 QAction
*kdiskfreeAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
157 connect(kdiskfreeAction
, &QAction::triggered
, this, [kdiskfree
] {
158 auto *job
= new KIO::ApplicationLauncherJob(kdiskfree
);
164 void StatusBarSpaceInfo::slotValuesChanged()
166 Q_ASSERT(m_observer
);
167 const quint64 size
= m_observer
->size();
169 if (!m_shown
|| size
== 0) {
176 const quint64 available
= m_observer
->available();
177 const quint64 used
= size
- available
;
178 const int percentUsed
= qRound(100.0 * qreal(used
) / qreal(size
));
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);
194 #include "moc_statusbarspaceinfo.cpp"