]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
f364fcf5bc3d9ee28d2248ae85422a7e90da9021
[dolphin.git] / src / statusbar / statusbarspaceinfo.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * and Patrice Tremblay *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "statusbarspaceinfo.h"
22
23 #include "spaceinfoobserver.h"
24
25 #include <QMouseEvent>
26
27 #include <KLocalizedString>
28 #include <KNS3/KMoreToolsMenuFactory>
29 #include <KIO/Job>
30 #include <knewstuff_version.h>
31
32 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
33 KCapacityBar(KCapacityBar::DrawTextInline, parent),
34 m_observer(0)
35 {
36 setCursor(Qt::PointingHandCursor);
37 }
38
39 StatusBarSpaceInfo::~StatusBarSpaceInfo()
40 {
41 }
42
43 void StatusBarSpaceInfo::setUrl(const QUrl& url)
44 {
45 if (m_url != url) {
46 m_url = url;
47 if (m_observer) {
48 m_observer->setUrl(url);
49 }
50 }
51 }
52
53 QUrl StatusBarSpaceInfo::url() const
54 {
55 return m_url;
56 }
57
58 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
59 {
60 KCapacityBar::showEvent(event);
61 m_observer.reset(new SpaceInfoObserver(m_url, this));
62 slotValuesChanged();
63 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
64 }
65
66 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
67 {
68 m_observer.reset();
69 KCapacityBar::hideEvent(event);
70 }
71
72 void StatusBarSpaceInfo::mousePressEvent(QMouseEvent* event)
73 {
74 if (event->button() == Qt::LeftButton) {
75 // Creates a menu with tools that help to find out more about free
76 // disk space for the given url.
77
78 // Note that this object must live long enough in case the user opens
79 // the "Configure..." dialog
80 KMoreToolsMenuFactory menuFactory(QStringLiteral("dolphin/statusbar-diskspace-menu"));
81 #if KNEWSTUFF_VERSION >= QT_VERSION_CHECK(5, 37, 0)
82 menuFactory.setParentWidget(this);
83 #endif
84 auto menu = menuFactory.createMenuFromGroupingNames(
85 { "disk-usage", "more:", "disk-partitions" }, m_url);
86
87 menu->exec(QCursor::pos());
88 }
89 }
90
91 void StatusBarSpaceInfo::slotValuesChanged()
92 {
93 Q_ASSERT(m_observer);
94 const quint64 size = m_observer->size();
95 if (size == 0) {
96 setText(i18nc("@info:status", "Unknown size"));
97 setValue(0);
98 update();
99 } else {
100 const quint64 available = m_observer->available();
101 const quint64 used = size - available;
102 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
103
104 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
105 setUpdatesEnabled(false);
106 setValue(percentUsed);
107 setUpdatesEnabled(true);
108 update();
109 }
110 }
111