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