summaryrefslogtreecommitdiff
path: root/noncore/settings/sysinfo/storage.cpp
Unidiff
Diffstat (limited to 'noncore/settings/sysinfo/storage.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/sysinfo/storage.cpp220
1 files changed, 220 insertions, 0 deletions
diff --git a/noncore/settings/sysinfo/storage.cpp b/noncore/settings/sysinfo/storage.cpp
new file mode 100644
index 0000000..4e81170
--- a/dev/null
+++ b/noncore/settings/sysinfo/storage.cpp
@@ -0,0 +1,220 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <qlabel.h>
22#include <qlayout.h>
23#include <qtimer.h>
24#include <qlayout.h>
25#include "graph.h"
26#include "storage.h"
27
28#include <stdio.h>
29#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
30#include <sys/vfs.h>
31#include <mntent.h>
32#endif
33
34StorageInfo::StorageInfo( QWidget *parent, const char *name )
35 : QWidget( parent, name )
36{
37 vb = 0;
38 disks.setAutoDelete(TRUE);
39 lines.setAutoDelete(TRUE);
40 updateMounts();
41 startTimer( 5000 );
42}
43
44void StorageInfo::timerEvent(QTimerEvent*)
45{
46 updateMounts();
47}
48
49static bool isCF(const QString& m)
50{
51 FILE* f = fopen("/var/run/stab", "r");
52 if (!f) f = fopen("/var/state/pcmcia/stab", "r");
53 if (!f) f = fopen("/var/lib/pcmcia/stab", "r");
54 if ( f ) {
55 char line[1024];
56 char devtype[80];
57 char devname[80];
58 while ( fgets( line, 1024, f ) ) {
59 // 0 ide ide-cs 0 hda 3 0
60 if ( sscanf(line,"%*d %s %*s %*s %s", devtype, devname )==2 )
61 {
62 if ( QString(devtype) == "ide" && m.find(devname)>0 ) {
63 fclose(f);
64 return TRUE;
65 }
66 }
67 }
68 fclose(f);
69 }
70 return FALSE;
71}
72
73void StorageInfo::updateMounts()
74{
75#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
76 struct mntent *me;
77 FILE *mntfp = setmntent( "/etc/mtab", "r" );
78 QStringList curdisks;
79 QStringList curfs;
80 bool rebuild = FALSE;
81 int n=0;
82 if ( mntfp ) {
83 while ( (me = getmntent( mntfp )) != 0 ) {
84 QString fs = me->mnt_fsname;
85 if ( fs.left(7)=="/dev/hd" || fs.left(7)=="/dev/sd"
86 || fs.left(8)=="/dev/mtd" || fs.left(9) == "/dev/mmcd" )
87 {
88 n++;
89 curdisks.append(fs);
90 QString d = me->mnt_dir;
91 curfs.append(d);
92 if ( !disks.find(d) )
93 rebuild = TRUE;
94 }
95 }
96 endmntent( mntfp );
97 }
98 if ( rebuild || n != (int)disks.count() ) {
99 disks.clear();
100 lines.clear();
101 delete vb;
102 vb = new QVBoxLayout( this, n > 3 ? 1 : 5 );
103 bool frst=TRUE;
104 QStringList::ConstIterator it=curdisks.begin();
105 QStringList::ConstIterator fsit=curfs.begin();
106 for (; it!=curdisks.end(); ++it, ++fsit) {
107 if ( !frst ) {
108 QFrame *f = new QFrame( this );
109 vb->addWidget(f);
110 f->setFrameStyle( QFrame::HLine | QFrame::Sunken );
111 lines.append(f);
112 f->show();
113 } frst=FALSE;
114 QString humanname=*it;
115 if ( isCF(humanname) )
116 humanname = tr("CF Card");
117 else if ( humanname == "/dev/hda1" )
118 humanname = tr("Hard Disk");
119 else if ( humanname.left(9) == "/dev/mmcd" )
120 humanname = tr("SD Card");
121 else if ( humanname.left(7) == "/dev/hd" )
122 humanname = tr("Hard Disk") + " " + humanname.mid(7);
123 else if ( humanname.left(7) == "/dev/sd" )
124 humanname = tr("SCSI Hard Disk") + " " + humanname.mid(7);
125 else if ( humanname == "/dev/mtdblock1" || humanname == "/dev/mtdblock/1" )
126 humanname = tr("Internal Storage");
127 else if ( humanname.left(14) == "/dev/mtdblock/" )
128 humanname = tr("Internal Storage") + " " + humanname.mid(14);
129 else if ( humanname.left(13) == "/dev/mtdblock" )
130 humanname = tr("Internal Storage") + " " + humanname.mid(13);
131 // etc.
132 MountInfo* mi = new MountInfo( *fsit, humanname, this );
133 vb->addWidget(mi);
134 disks.insert(*fsit,mi);
135 mi->show();
136 }
137 vb->addStretch();
138 } else {
139 // just update them
140 for (QDictIterator<MountInfo> i(disks); i.current(); ++i)
141 i.current()->updateData();
142 }
143#endif
144}
145
146
147MountInfo::MountInfo( const QString &path, const QString &ttl, QWidget *parent, const char *name )
148 : QWidget( parent, name ), title(ttl)
149{
150 fs = new FileSystem( path );
151 QVBoxLayout *vb = new QVBoxLayout( this, 3 );
152
153 totalSize = new QLabel( this );
154 vb->addWidget( totalSize );
155
156 data = new GraphData();
157 graph = new BarGraph( this );
158 graph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
159 vb->addWidget( graph, 1 );
160 graph->setData( data );
161
162 legend = new GraphLegend( this );
163 legend->setOrientation(Horizontal);
164 vb->addWidget( legend );
165 legend->setData( data );
166
167 updateData();
168}
169
170MountInfo::~MountInfo()
171{
172 delete data;
173 delete fs;
174}
175
176void MountInfo::updateData()
177{
178 fs->update();
179
180 long mult = fs->blockSize() / 1024;
181 long div = 1024 / fs->blockSize();
182 if ( !mult ) mult = 1;
183 if ( !div ) div = 1;
184 long total = fs->totalBlocks() * mult / div;
185 long avail = fs->availBlocks() * mult / div;
186 long used = total - avail;
187 totalSize->setText( title + tr(" total: %1 kB").arg( total ) );
188 data->clear();
189 data->addItem( tr("Used (%1 kB)").arg(used), used );
190 data->addItem( tr("Available (%1 kB)").arg(avail), avail );
191 graph->repaint( FALSE );
192 legend->update();
193 graph->show();
194 legend->show();
195}
196
197//---------------------------------------------------------------------------
198
199FileSystem::FileSystem( const QString &p )
200 : fspath( p ), blkSize(512), totalBlks(0), availBlks(0)
201{
202 update();
203}
204
205void FileSystem::update()
206{
207#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
208 struct statfs fs;
209 if ( !statfs( fspath.latin1(), &fs ) ) {
210 blkSize = fs.f_bsize;
211 totalBlks = fs.f_blocks;
212 availBlks = fs.f_bavail;
213 } else {
214 blkSize = 0;
215 totalBlks = 0;
216 availBlks = 0;
217 }
218#endif
219}
220