]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
1c904830474b6bdc139ae6a6cfebc3740b3d6376
[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 <KLocalizedString>
26 #include <KIO/Job>
27
28
29 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
30 KCapacityBar(KCapacityBar::DrawTextInline, parent),
31 m_observer(0)
32 {
33 }
34
35 StatusBarSpaceInfo::~StatusBarSpaceInfo()
36 {
37 }
38
39 void StatusBarSpaceInfo::setUrl(const KUrl& url)
40 {
41 if (m_url != url) {
42 m_url = url;
43 if (m_observer) {
44 m_observer->setUrl(url);
45 }
46 }
47 }
48
49 KUrl StatusBarSpaceInfo::url() const
50 {
51 return m_url;
52 }
53
54 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
55 {
56 KCapacityBar::showEvent(event);
57 m_observer.reset(new SpaceInfoObserver(m_url, this));
58 slotValuesChanged();
59 connect(m_observer.data(), &SpaceInfoObserver::valuesChanged, this, &StatusBarSpaceInfo::slotValuesChanged);
60 }
61
62 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
63 {
64 m_observer.reset();
65 KCapacityBar::hideEvent(event);
66 }
67
68 void StatusBarSpaceInfo::slotValuesChanged()
69 {
70 Q_ASSERT(m_observer);
71 const quint64 size = m_observer->size();
72 if (size == 0) {
73 setText(i18nc("@info:status", "Unknown size"));
74 setValue(0);
75 update();
76 } else {
77 const quint64 available = m_observer->available();
78 const quint64 used = size - available;
79 const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
80
81 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
82 setUpdatesEnabled(false);
83 setValue(percentUsed);
84 setUpdatesEnabled(true);
85 update();
86 }
87 }
88