add more spacing
[personal-kdebase.git] / apps / dolphin / src / statusbarspaceinfo.cpp
blob33017f02bc1b853873b144408e9be7f9708258dc
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 ***************************************************************************/
21 #include "statusbarspaceinfo.h"
23 #include <KDiskFreeSpaceInfo>
24 #include <kmountpoint.h>
25 #include <klocale.h>
26 #include <kio/job.h>
28 #include <QTimer>
29 #include <QKeyEvent>
31 StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
32 KCapacityBar(KCapacityBar::DrawTextInline, parent),
33 m_kBSize(0),
34 m_timer(0)
36 setMaximumWidth(200);
37 setMinimumWidth(200); // something to fix on kcapacitybar (ereslibre)
39 // Use a timer to update the space information. Polling is useful
40 // here, as files can be deleted/added outside the scope of Dolphin.
41 m_timer = new QTimer(this);
42 connect(m_timer, SIGNAL(timeout()), this, SLOT(refresh()));
45 StatusBarSpaceInfo::~StatusBarSpaceInfo()
49 void StatusBarSpaceInfo::setUrl(const KUrl& url)
51 m_url = url;
52 refresh();
55 void StatusBarSpaceInfo::showEvent(QShowEvent* event)
57 KCapacityBar::showEvent(event);
58 if (!event->spontaneous()) {
59 refresh();
60 m_timer->start(10000);
64 void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
66 m_timer->stop();
67 KCapacityBar::hideEvent(event);
70 void StatusBarSpaceInfo::refresh()
72 if (!isVisible()) {
73 return;
76 // KDiskFreeSpace is for local paths only
77 if (!m_url.isLocalFile()) {
78 setText(i18nc("@info:status", "Unknown size"));
79 setValue(0);
80 update();
81 return;
84 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.path());
85 if (!mp) {
86 return;
89 KDiskFreeSpaceInfo job = KDiskFreeSpaceInfo::freeSpaceInfo(mp->mountPoint());
90 if (!job.isValid()) {
91 setText(i18nc("@info:status", "Unknown size"));
92 setValue(0);
93 update();
94 return;
97 KIO::filesize_t kBSize = job.size() / 1024;
98 KIO::filesize_t kBUsed = job.used() / 1024;
100 const bool valuesChanged = (kBUsed != static_cast<quint64>(value())) || (kBSize != m_kBSize);
101 if (valuesChanged) {
102 setText(i18nc("@info:status Free disk space", "%1 free",
103 KIO::convertSize(job.available())));
105 setUpdatesEnabled(false);
106 m_kBSize = kBSize;
107 setValue(kBSize > 0 ? (kBUsed * 100) / kBSize : 0);
108 setUpdatesEnabled(true);
109 update();
113 #include "statusbarspaceinfo.moc"