]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbarspaceinfo.cpp
SVN_SILENT made messages (.desktop file)
[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 <KDiskFreeSpaceInfo>
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_kBSize(0)
34 {
35 setMaximumWidth(200);
36 setMinimumWidth(200); // something to fix on kcapacitybar (ereslibre)
37
38 // Update the space information each 10 seconds. Polling is useful
39 // here, as files can be deleted/added outside the scope of Dolphin.
40 QTimer* timer = new QTimer(this);
41 connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
42 timer->start(10000);
43 }
44
45 StatusBarSpaceInfo::~StatusBarSpaceInfo()
46 {
47 }
48
49 void StatusBarSpaceInfo::setUrl(const KUrl& url)
50 {
51 m_url = url;
52 refresh();
53 }
54
55 void StatusBarSpaceInfo::refresh()
56 {
57 // KDiskFreeSpace is for local paths only
58 if (!m_url.isLocalFile()) {
59 setText(i18nc("@info:status", "Unknown size"));
60 setValue(0);
61 update();
62 return;
63 }
64
65 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(m_url.path());
66 if (!mp) {
67 return;
68 }
69
70 KDiskFreeSpaceInfo job = KDiskFreeSpaceInfo::freeSpaceInfo(mp->mountPoint());
71 if (!job.isValid()) {
72 setText(i18nc("@info:status", "Unknown size"));
73 setValue(0);
74 update();
75 return;
76 }
77
78 KIO::filesize_t kBSize = job.size() / 1024;
79 KIO::filesize_t kBUsed = job.used() / 1024;
80
81 const bool valuesChanged = (kBUsed != static_cast<quint64>(value())) || (kBSize != m_kBSize);
82 if (valuesChanged) {
83 setText(i18nc("@info:status Free disk space", "%1 free",
84 KIO::convertSize(job.available())));
85
86 setUpdatesEnabled(false);
87 m_kBSize = kBSize;
88 setValue(kBSize > 0 ? (kBUsed * 100) / kBSize : 0);
89 setUpdatesEnabled(true);
90 update();
91 }
92 }
93
94 #include "statusbarspaceinfo.moc"