]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
Only use KActivities on Linux/BSD
[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 "spaceinfoobserver.h"
10
11 #include <KIO/ApplicationLauncherJob>
12 #include <KIO/Global>
13 #include <KLocalizedString>
14 #include <KService>
15
16 #include <QMenu>
17 #include <QMouseEvent>
18 #include <QStorageInfo>
19
20 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget *parent)
21 : KCapacityBar(KCapacityBar::DrawTextInline, parent)
22 , m_observer(nullptr)
23 {
24 setCursor(Qt::PointingHandCursor);
25 }
26
27 StatusBarSpaceInfo::~StatusBarSpaceInfo()
28 {
29 }
30
31 void StatusBarSpaceInfo::setShown(bool shown)
32 {
33 m_shown = shown;
34 if (!m_shown) {
35 hide();
36 m_ready = false;
37 }
38 }
39
40 void StatusBarSpaceInfo::setUrl(const QUrl &url)
41 {
42 if (m_url != url) {
43 m_url = url;
44 m_ready = false;
45 if (m_observer) {
46 m_observer.reset(new SpaceInfoObserver(m_url, this));
47 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
48 }
49 }
50 }
51
52 QUrl StatusBarSpaceInfo::url() const
53 {
54 return m_url;
55 }
56
57 void StatusBarSpaceInfo::update()
58 {
59 if (m_observer) {
60 m_observer->update();
61 }
62 }
63
64 void StatusBarSpaceInfo::showEvent(QShowEvent *event)
65 {
66 if (m_shown) {
67 if (m_ready) {
68 KCapacityBar::showEvent(event);
69 }
70
71 if (m_observer.isNull()) {
72 m_observer.reset(new SpaceInfoObserver(m_url, this));
73 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
74 }
75 }
76 }
77
78 void StatusBarSpaceInfo::hideEvent(QHideEvent *event)
79 {
80 if (m_ready) {
81 m_observer.reset();
82 m_ready = false;
83 }
84 KCapacityBar::hideEvent(event);
85 }
86
87 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent *event)
88 {
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.
92
93 const KService::Ptr filelight = KService::serviceByDesktopName(QStringLiteral("org.kde.filelight"));
94 const KService::Ptr kdiskfree = KService::serviceByDesktopName(QStringLiteral("org.kde.kdf"));
95
96 if (!filelight && !kdiskfree) {
97 // nothing to show
98 return;
99 }
100
101 QMenu *menu = new QMenu(this);
102
103 if (filelight) {
104 QAction *filelightFolderAction = menu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - current folder"));
105
106 menu->connect(filelightFolderAction, &QAction::triggered, menu, [this, filelight](bool) {
107 auto *job = new KIO::ApplicationLauncherJob(filelight);
108 job->setUrls({m_url});
109 job->start();
110 });
111
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"));
115
116 menu->connect(filelightDiskAction, &QAction::triggered, menu, [this, filelight](bool) {
117 const QStorageInfo info(m_url.toLocalFile());
118
119 if (info.isValid() && info.isReady()) {
120 auto *job = new KIO::ApplicationLauncherJob(filelight);
121 job->setUrls({QUrl::fromLocalFile(info.rootPath())});
122 job->start();
123 }
124 });
125 }
126
127 QAction *filelightAllAction = menu->addAction(QIcon::fromTheme(QStringLiteral("filelight")), i18n("Disk Usage Statistics - all devices"));
128
129 menu->connect(filelightAllAction, &QAction::triggered, menu, [this, filelight](bool) {
130 const QStorageInfo info(m_url.toLocalFile());
131
132 if (info.isValid() && info.isReady()) {
133 auto *job = new KIO::ApplicationLauncherJob(filelight);
134 job->start();
135 }
136 });
137 }
138
139 if (kdiskfree) {
140 QAction *kdiskfreeAction = menu->addAction(QIcon::fromTheme(QStringLiteral("kdf")), i18n("KDiskFree"));
141
142 connect(kdiskfreeAction, &QAction::triggered, this, [kdiskfree] {
143 auto *job = new KIO::ApplicationLauncherJob(kdiskfree);
144 job->start();
145 });
146 }
147
148 menu->exec(QCursor::pos());
149 }
150 }
151
152 void StatusBarSpaceInfo::slotValuesChanged()
153 {
154 Q_ASSERT(m_observer);
155 const quint64 size = m_observer->size();
156
157 if (!m_shown || size == 0) {
158 hide();
159 return;
160 }
161
162 m_ready = true;
163
164 const quint64 available = m_observer->available();
165 const quint64 used = size - available;
166 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
167
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);
173
174 if (!isVisible()) {
175 show();
176 } else {
177 update();
178 }
179 }
180
181 #include "moc_statusbarspaceinfo.cpp"