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