]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
There are some extractable strings in subdirs too.
[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(kBSize);
70 Q_UNUSED(mountPoint);
71
72 m_gettingSize = false;
73 m_foundMountPoint = true;
74 const bool valuesChanged = (kBUsed != static_cast<quint64>(value())) ||
75 (kBAvailable != static_cast<quint64>(maximum()));
76 if (valuesChanged) {
77 m_text = i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(kBAvailable * 1024));
78 setMaximum(kBSize);
79 setValue(kBUsed);
80 }
81 }
82
83 void StatusBarSpaceInfo::slotDiskFreeSpaceDone()
84 {
85 if (m_foundMountPoint) {
86 return;
87 }
88
89 m_gettingSize = false;
90 m_text = i18nc("@info:status", "Unknown size");
91 setMinimum(0);
92 setMaximum(0);
93 setValue(0);
94 }
95
96 void StatusBarSpaceInfo::refresh()
97 {
98 // KDiskFreeSpace is for local paths only
99 if (!m_url.isLocalFile()) {
100 m_text = i18nc("@info:status", "Unknown size");
101 update();
102 return;
103 }
104
105 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.path());
106 if (!mp) {
107 return;
108 }
109
110 m_gettingSize = true;
111 m_foundMountPoint = false;
112 KDiskFreeSpace* job = new KDiskFreeSpace(this);
113 connect(job, SIGNAL(foundMountPoint(const QString&,
114 quint64,
115 quint64,
116 quint64)),
117 this, SLOT(slotFoundMountPoint(const QString&,
118 quint64,
119 quint64,
120 quint64)));
121 connect(job, SIGNAL(done()), this, SLOT(slotDiskFreeSpaceDone()));
122
123 job->readDF(mp->mountPoint());
124
125 // refresh() is invoked for each directory change. Usually getting
126 // the size information can be done very fast, so to prevent any
127 // flickering the "Getting size..." indication is only shown if
128 // at least 300 ms have been passed.
129 QTimer::singleShot(300, this, SLOT(showGettingSizeInfo()));
130 }
131
132 void StatusBarSpaceInfo::showGettingSizeInfo()
133 {
134 if (m_gettingSize) {
135 m_text = i18nc("@info:status", "Getting size...");
136 update();
137 setMinimum(0);
138 setMaximum(0);
139 }
140 }
141
142 #include "statusbarspaceinfo.moc"