]>
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 <KIO/ApplicationLauncherJob>
13 #include <KLocalizedString>
17 #include <QMouseEvent>
18 #include <QStorageInfo>
20 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget
*parent
)
21 : KCapacityBar(KCapacityBar::DrawTextInline
, parent
)
24 setCursor(Qt::PointingHandCursor
);
27 StatusBarSpaceInfo::~StatusBarSpaceInfo()
31 void StatusBarSpaceInfo::setShown(bool shown
)
40 void StatusBarSpaceInfo::setUrl(const QUrl
&url
)
46 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
47 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
52 QUrl
StatusBarSpaceInfo::url() const
57 void StatusBarSpaceInfo::update()
64 void StatusBarSpaceInfo::showEvent(QShowEvent
*event
)
68 KCapacityBar::showEvent(event
);
71 if (m_observer
.isNull()) {
72 m_observer
.reset(new SpaceInfoObserver(m_url
, this));
73 connect(m_observer
.data(), &SpaceInfoObserver::valuesChanged
, this, &StatusBarSpaceInfo::slotValuesChanged
);
78 void StatusBarSpaceInfo::hideEvent(QHideEvent
*event
)
84 KCapacityBar::hideEvent(event
);
87 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent
*event
)
89 if (event
->button() == Qt::LeftButton
) {
90 // Creates a menu with tools that help to find out more about free
91 // disk space for the given url.
93 const KService::Ptr filelight
= KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
94 const KService::Ptr kdiskfree
= KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
96 if (!filelight
&& !kdiskfree
) {
101 QMenu
*menu
= new QMenu(this);
104 QAction
*filelightFolderAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
106 menu
->connect(filelightFolderAction
, &QAction::triggered
, menu
, [this, filelight
](bool) {
107 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
108 job
->setUrls({m_url
});
112 // For remote URLs like FTP analyzing the device makes no sense
113 if (m_url
.isLocalFile()) {
114 QAction
*filelightDiskAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current device"));
116 menu
->connect(filelightDiskAction
, &QAction::triggered
, menu
, [this, filelight
](bool) {
117 const QStorageInfo
info(m_url
.toLocalFile());
119 if (info
.isValid() && info
.isReady()) {
120 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
121 job
->setUrls({QUrl::fromLocalFile(info
.rootPath())});
127 QAction
*filelightAllAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
129 menu
->connect(filelightAllAction
, &QAction::triggered
, menu
, [this, filelight
](bool) {
130 const QStorageInfo
info(m_url
.toLocalFile());
132 if (info
.isValid() && info
.isReady()) {
133 auto *job
= new KIO::ApplicationLauncherJob(filelight
);
140 QAction
*kdiskfreeAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
142 connect(kdiskfreeAction
, &QAction::triggered
, this, [kdiskfree
] {
143 auto *job
= new KIO::ApplicationLauncherJob(kdiskfree
);
148 menu
->exec(QCursor::pos());
152 void StatusBarSpaceInfo::slotValuesChanged()
154 Q_ASSERT(m_observer
);
155 const quint64 size
= m_observer
->size();
157 if (!m_shown
|| size
== 0) {
164 const quint64 available
= m_observer
->available();
165 const quint64 used
= size
- available
;
166 const int percentUsed
= qRound(100.0 * qreal(used
) / qreal(size
));
168 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available
)));
169 setToolTip(i18nc("tooltip:status Free disk space", "%1 free out of %2 (%3% used)", KIO::convertSize(available
), KIO::convertSize(size
), percentUsed
));
170 setUpdatesEnabled(false);
171 setValue(percentUsed
);
172 setUpdatesEnabled(true);
181 #include "moc_statusbarspaceinfo.cpp"