]>
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 QSize
StatusBarSpaceInfo::minimumSizeHint() const
107 void StatusBarSpaceInfo::updateMenu()
109 m_buttonMenu
->clear();
111 // Creates a menu with tools that help to find out more about free
112 // disk space for the given url.
114 const KService::Ptr filelight
= KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
115 const KService::Ptr kdiskfree
= KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
117 if (!filelight
&& !kdiskfree
) {
123 QAction
*filelightFolderAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
125 m_buttonMenu
->connect(filelightFolderAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
126 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
127 job
->setUrls({m_url
});
131 // For remote URLs like FTP analyzing the device makes no sense
132 if (m_url
.isLocalFile()) {
133 QAction
*filelightDiskAction
=
134 m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
136 m_buttonMenu
->connect(filelightDiskAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
137 const QStorageInfo
info(m_url
.toLocalFile());
139 if (info
.isValid() && info
.isReady()) {
140 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
141 job
->setUrls({QUrl::fromLocalFile(info
.rootPath())});
147 QAction
*filelightAllAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
149 m_buttonMenu
->connect(filelightAllAction
, &QAction::triggered
, m_buttonMenu
, [this, filelight
](bool) {
150 const QStorageInfo
info(m_url
.toLocalFile());
152 if (info
.isValid() && info
.isReady()) {
153 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
160 QAction
*kdiskfreeAction
= m_buttonMenu
->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
162 connect(kdiskfreeAction
, &QAction::triggered
, this, [kdiskfree
] {
163 auto *job
= new KIO::ApplicationLauncherJob(kdiskfree
);
169 void StatusBarSpaceInfo::slotValuesChanged()
171 Q_ASSERT(m_observer
);
172 const quint64 size
= m_observer
->size();
174 if (!m_shown
|| size
== 0) {
181 const quint64 available
= m_observer
->available();
182 const quint64 used
= size
- available
;
183 const int percentUsed
= qRound(100.0 * qreal(used
) / qreal(size
));
185 m_textInfoButton
->setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available
)));
186 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available
), KIO::convertSize(size
), percentUsed
));
187 m_textInfoButton
->setToolTip(i18nc("@info:tooltip for the free disk space button",
188 "%1 free out of %2 (%3% used)\nPress to manage disk space usage.",
189 KIO::convertSize(available
),
190 KIO::convertSize(size
),
192 setUpdatesEnabled(false);
193 m_capacityBar
->setValue(percentUsed
);
194 setUpdatesEnabled(true);
203 #include "moc_statusbarspaceinfo.cpp"