]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
The space info widget now inherits KCapacityBar. Two things to check:
[dolphin.git] / src / 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 <kdiskfreespace.h>
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_gettingSize(false),
34 m_foundMountPoint(false)
35 {
36 setMaximumWidth(200);
37 setMinimumWidth(200); // something to fix on kcapacitybar (ereslibre)
38
39 // Update the space information each 10 seconds. Polling is useful
40 // here, as files can be deleted/added outside the scope of Dolphin.
41 QTimer* timer = new QTimer(this);
42 connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
43 timer->start(10000);
44 }
45
46 StatusBarSpaceInfo::~StatusBarSpaceInfo()
47 {
48 }
49
50 void StatusBarSpaceInfo::setUrl(const KUrl& url)
51 {
52 m_url = url;
53 refresh();
54 }
55
56 void StatusBarSpaceInfo::slotFoundMountPoint(const QString& mountPoint,
57 quint64 kBSize,
58 quint64 kBUsed,
59 quint64 kBAvailable)
60 {
61 Q_UNUSED(mountPoint);
62
63 m_gettingSize = false;
64 m_foundMountPoint = true;
65 const bool valuesChanged = (kBUsed != static_cast<quint64>(value()));
66 if (valuesChanged) {
67 setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(kBAvailable * 1024)));
68 setUpdatesEnabled(false);
69 setValue((kBUsed * 100) / kBSize);
70 setUpdatesEnabled(true);
71 update();
72 }
73 }
74
75 void StatusBarSpaceInfo::slotDiskFreeSpaceDone()
76 {
77 if (m_foundMountPoint) {
78 return;
79 }
80
81 m_gettingSize = false;
82 setText(i18nc("@info:status", "Unknown size"));
83 setValue(0);
84 update();
85 }
86
87 void StatusBarSpaceInfo::refresh()
88 {
89 // KDiskFreeSpace is for local paths only
90 if (!m_url.isLocalFile()) {
91 setText(i18nc("@info:status", "Unknown size"));
92 setValue(0);
93 update();
94 return;
95 }
96
97 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.path());
98 if (!mp) {
99 return;
100 }
101
102 m_gettingSize = true;
103 m_foundMountPoint = false;
104 KDiskFreeSpace* job = new KDiskFreeSpace(this);
105 connect(job, SIGNAL(foundMountPoint(const QString&,
106 quint64,
107 quint64,
108 quint64)),
109 this, SLOT(slotFoundMountPoint(const QString&,
110 quint64,
111 quint64,
112 quint64)));
113 connect(job, SIGNAL(done()), this, SLOT(slotDiskFreeSpaceDone()));
114
115 job->readDF(mp->mountPoint());
116
117 // refresh() is invoked for each directory change. Usually getting
118 // the size information can be done very fast, so to prevent any
119 // flickering the "Getting size..." indication is only shown if
120 // at least 300 ms have been passed.
121 QTimer::singleShot(300, this, SLOT(showGettingSizeInfo()));
122 }
123
124 void StatusBarSpaceInfo::showGettingSizeInfo()
125 {
126 if (m_gettingSize) {
127 setText(i18nc("@info:status", "Getting size..."));
128 update();
129 }
130 }
131
132 #include "statusbarspaceinfo.moc"