summaryrefslogtreecommitdiff
path: root/library/storage.cpp
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /library/storage.cpp
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'library/storage.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/storage.cpp188
1 files changed, 188 insertions, 0 deletions
diff --git a/library/storage.cpp b/library/storage.cpp
new file mode 100644
index 0000000..bd34a5f
--- a/dev/null
+++ b/library/storage.cpp
@@ -0,0 +1,188 @@
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 <qpe/storage.h>
22#ifdef QT_QWS_CUSTOM
23#include <qpe/custom.h>
24#endif
25
26#include <qtimer.h>
27#include <qcopchannel_qws.h>
28
29#include <stdio.h>
30
31#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
32#include <sys/vfs.h>
33#include <mntent.h>
34#endif
35
36#include <qstringlist.h>
37
38static bool isCF(const QString& m)
39{
40 FILE* f = fopen("/var/run/stab", "r");
41 if (!f) f = fopen("/var/state/pcmcia/stab", "r");
42 if (!f) f = fopen("/var/lib/pcmcia/stab", "r");
43 if ( f ) {
44 char line[1024];
45 char devtype[80];
46 char devname[80];
47 while ( fgets( line, 1024, f ) ) {
48 // 0 ide ide-cs 0 hda 3 0
49 if ( sscanf(line,"%*d %s %*s %*s %s", devtype, devname )==2 )
50 {
51 if ( QString(devtype) == "ide" && m.find(devname)>0 ) {
52 fclose(f);
53 return TRUE;
54 }
55 }
56 }
57 fclose(f);
58 }
59 return FALSE;
60}
61
62StorageInfo::StorageInfo( QObject *parent )
63 : QObject( parent )
64{
65 mFileSystems.setAutoDelete( TRUE );
66 channel = new QCopChannel( "QPE/Card", this );
67 connect( channel, SIGNAL(received(const QCString &, const QByteArray &)),
68 this, SLOT(cardMessage( const QCString &, const QByteArray &)) );
69 update();
70}
71
72const FileSystem *StorageInfo::fileSystemOf( const QString &filename )
73{
74 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i) {
75 if ( filename.startsWith( (*i)->path() ) )
76 return (*i);
77 }
78 return 0;
79}
80
81
82void StorageInfo::cardMessage( const QCString& msg, const QByteArray& )
83{
84 if ( msg == "mtabChanged()" )
85 update();
86}
87
88void StorageInfo::update()
89{
90 //qDebug("StorageInfo::updating");
91#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
92 struct mntent *me;
93 FILE *mntfp = setmntent( "/etc/mtab", "r" );
94
95 QStringList curdisks;
96 QStringList curopts;
97 QStringList curfs;
98 bool rebuild = FALSE;
99 int n=0;
100 if ( mntfp ) {
101 while ( (me = getmntent( mntfp )) != 0 ) {
102 QString fs = me->mnt_fsname;
103 if ( fs.left(7)=="/dev/hd" || fs.left(7)=="/dev/sd"
104 || fs.left(8)=="/dev/mtd" || fs.left(9) == "/dev/mmcd" )
105 {
106 n++;
107 curdisks.append(fs);
108 curopts.append( me->mnt_opts );
109 //qDebug("-->fs %s opts %s", fs.latin1(), me->mnt_opts );
110 curfs.append( me->mnt_dir );
111 bool found = FALSE;
112 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i) {
113 if ( (*i)->disk() == fs ) {
114 found = TRUE;
115 break;
116 }
117 }
118 if ( !found )
119 rebuild = TRUE;
120 }
121 }
122 endmntent( mntfp );
123 }
124 if ( rebuild || n != (int)mFileSystems.count() ) {
125 mFileSystems.clear();
126 QStringList::ConstIterator it=curdisks.begin();
127 QStringList::ConstIterator fsit=curfs.begin();
128 QStringList::ConstIterator optsIt=curopts.begin();
129 for (; it!=curdisks.end(); ++it, ++fsit, ++optsIt) {
130 QString opts = *optsIt;
131
132 QString disk = *it;
133 QString humanname;
134 bool removable = FALSE;
135 if ( isCF(disk) ) {
136 humanname = tr("CF Card");
137 removable = TRUE;
138 } else if ( disk == "/dev/hda1" ) {
139 humanname = tr("Hard Disk");
140 } else if ( disk.left(9) == "/dev/mmcd" ) {
141 humanname = tr("SD Card");
142 removable = TRUE;
143 } else if ( disk.left(7) == "/dev/hd" )
144 humanname = tr("Hard Disk") + " " + humanname.mid(7);
145 else if ( disk.left(7) == "/dev/sd" )
146 humanname = tr("SCSI Hard Disk") + " " + humanname.mid(7);
147 else if ( disk == "/dev/mtdblock1" || humanname == "/dev/mtdblock/1" )
148 humanname = tr("Internal Storage");
149 else if ( disk.left(14) == "/dev/mtdblock/" )
150 humanname = tr("Internal Storage") + " " + humanname.mid(14);
151 else if ( disk.left(13) == "/dev/mtdblock" )
152 humanname = tr("Internal Storage") + " " + humanname.mid(13);
153 FileSystem *fs = new FileSystem( disk, *fsit, humanname, removable, opts );
154 mFileSystems.append( fs );
155 }
156 emit disksChanged();
157 } else {
158 // just update them
159 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
160 i.current()->update();
161 }
162#endif
163}
164
165//---------------------------------------------------------------------------
166
167FileSystem::FileSystem( const QString &disk, const QString &path, const QString &name, bool rem, const QString &o )
168 : fsdisk( disk ), fspath( path ), humanname( name ), blkSize(512), totalBlks(0), availBlks(0), removable( rem ), opts( o )
169{
170 update();
171}
172
173void FileSystem::update()
174{
175#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
176 struct statfs fs;
177 if ( !statfs( fspath.latin1(), &fs ) ) {
178 blkSize = fs.f_bsize;
179 totalBlks = fs.f_blocks;
180 availBlks = fs.f_bavail;
181 } else {
182 blkSize = 0;
183 totalBlks = 0;
184 availBlks = 0;
185 }
186#endif
187}
188