]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/statusbarspaceinfo.cpp
a601d4367e39e4ae2bc0a04e97249a0220605fd6
[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 <KDiskFreeSpaceInfo>
24 #include <kmountpoint.h>
25 #include <klocale.h>
26 #include <kio/job.h>
27
28 #include <QTimer>
29 #include <QKeyEvent>
30
31 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
32 KCapacityBar(KCapacityBar::DrawTextInline, parent),
33 m_kBSize(0),
34 m_timer(0)
35 {
36 // Use a timer to update the space information. Polling is useful
37 // here, as files can be deleted/added outside the scope of Dolphin.
38 m_timer = new QTimer(this);
39 connect(m_timer, SIGNAL(timeout()), this, SLOT(refresh()));
40 }
41
42 StatusBarSpaceInfo::~StatusBarSpaceInfo()
43 {
44 }
45
46 void StatusBarSpaceInfo::setUrl(const KUrl& url)
47 {
48 m_url = url;
49 refresh();
50 }
51
52 KUrl StatusBarSpaceInfo::url() const
53 {
54 return m_url;
55 }
56
57 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
58 {
59 KCapacityBar::showEvent(event);
60 if (!event->spontaneous()) {
61 refresh();
62 m_timer->start(10000);
63 }
64 }
65
66 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
67 {
68 m_timer->stop();
69 KCapacityBar::hideEvent(event);
70 }
71
72 void StatusBarSpaceInfo::refresh()
73 {
74 if (!isVisible()) {
75 return;
76 }
77
78 // KDiskFreeSpace is for local paths only
79 if (!m_url.isLocalFile()) {
80 setText(i18nc("@info:status", "Unknown size"));
81 setValue(0);
82 update();
83 return;
84 }
85
86 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.toLocalFile());
87 if (!mp) {
88 return;
89 }
90
91 KDiskFreeSpaceInfo job = KDiskFreeSpaceInfo::freeSpaceInfo(mp->mountPoint());
92 if (!job.isValid()) {
93 setText(i18nc("@info:status", "Unknown size"));
94 setValue(0);
95 update();
96 return;
97 }
98
99 KIO::filesize_t kBSize = job.size() / 1024;
100 KIO::filesize_t kBUsed = job.used() / 1024;
101
102 const bool valuesChanged = (kBUsed != static_cast<quint64>(value())) || (kBSize != m_kBSize);
103 if (valuesChanged) {
104 setText(i18nc("@info:status Free disk space", "%1 free",
105 KIO::convertSize(job.available())));
106
107 setUpdatesEnabled(false);
108 m_kBSize = kBSize;
109 setValue(kBSize > 0 ? (kBUsed * 100) / kBSize : 0);
110 setUpdatesEnabled(true);
111 update();
112 }
113 }
114
115 #include "statusbarspaceinfo.moc"