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