author | harlekin <harlekin> | 2002-05-27 23:48:06 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2002-05-27 23:48:06 (UTC) |
commit | ac5b5e7ecd8972f867aa4c3fa2b055f5789842c1 (patch) (unidiff) | |
tree | eee536a678c92265122f980d94ea5a0f5448d840 /core/applets/cardmon/cardmon.cpp | |
parent | a184f648795a3fa27ad8ce1d1ac3cd4a30c7b8ba (diff) | |
download | opie-ac5b5e7ecd8972f867aa4c3fa2b055f5789842c1.zip opie-ac5b5e7ecd8972f867aa4c3fa2b055f5789842c1.tar.gz opie-ac5b5e7ecd8972f867aa4c3fa2b055f5789842c1.tar.bz2 |
new cf/pcmcia/sd/mmc applet, not finished yet, but working (kind off ,-) ) , will replace the old cardmon and sdmon when ready
Diffstat (limited to 'core/applets/cardmon/cardmon.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/applets/cardmon/cardmon.cpp | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/core/applets/cardmon/cardmon.cpp b/core/applets/cardmon/cardmon.cpp new file mode 100644 index 0000000..9522b88 --- a/dev/null +++ b/core/applets/cardmon/cardmon.cpp | |||
@@ -0,0 +1,230 @@ | |||
1 | /* | ||
2 | * cardmon.cpp | ||
3 | * | ||
4 | * --------------------- | ||
5 | * | ||
6 | * copyright : (c) 2002 by Maximilian Reiss | ||
7 | * email : max.reiss@gmx.de | ||
8 | * based on two apps by Devin Butterfield | ||
9 | */ | ||
10 | /*************************************************************************** | ||
11 | * * | ||
12 | * This program is free software; you can redistribute it and/or modify * | ||
13 | * it under the terms of the GNU General Public License as published by * | ||
14 | * the Free Software Foundation; either version 2 of the License, or * | ||
15 | * (at your option) any later version. * | ||
16 | * * | ||
17 | ***************************************************************************/ | ||
18 | |||
19 | |||
20 | #include "cardmon.h" | ||
21 | |||
22 | #include <qpe/resource.h> | ||
23 | |||
24 | #include <qcopchannel_qws.h> | ||
25 | #include <qpainter.h> | ||
26 | #include <qmessagebox.h> | ||
27 | #include <qpopupmenu.h> | ||
28 | #include <qfile.h> | ||
29 | #include <qtextstream.h> | ||
30 | |||
31 | #include <stdio.h> | ||
32 | #include <unistd.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <string.h> | ||
35 | #include <fcntl.h> | ||
36 | |||
37 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) | ||
38 | #include <sys/vfs.h> | ||
39 | #include <mntent.h> | ||
40 | #endif | ||
41 | |||
42 | CardMonitor::CardMonitor( QWidget *parent ) : QWidget( parent ), | ||
43 | pm( Resource::loadPixmap( "cardmon/pcmcia" ) ) { | ||
44 | |||
45 | QCopChannel* pcmciaChannel = new QCopChannel( "QPE/Card", this ); | ||
46 | connect( pcmciaChannel, SIGNAL(received(const QCString &, const QByteArray &)), | ||
47 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); | ||
48 | |||
49 | QCopChannel* sdChannel = new QCopChannel( "QPE/Card", this ); | ||
50 | connect( sdChannel, SIGNAL(received(const QCString &, const QByteArray &)), | ||
51 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); | ||
52 | |||
53 | |||
54 | setFixedSize( pm.size() ); | ||
55 | hide(); | ||
56 | getStatusPcmcia(); | ||
57 | } | ||
58 | |||
59 | CardMonitor::~CardMonitor() { | ||
60 | } | ||
61 | |||
62 | void CardMonitor::mousePressEvent( QMouseEvent * ) { | ||
63 | QPopupMenu *menu = new QPopupMenu(); | ||
64 | QString cmd; | ||
65 | int err=0; | ||
66 | |||
67 | if ( cardInSd ) { | ||
68 | menu->insertItem( tr("Eject SD/ MMC card"), 0 ); | ||
69 | } | ||
70 | |||
71 | if ( cardInPcmcia0 ) { | ||
72 | menu->insertItem( tr("Eject card (0) %1").arg(cardInPcmcia0Name), 1 ); | ||
73 | } | ||
74 | |||
75 | if ( cardInPcmcia1 ) { | ||
76 | menu->insertItem( tr("Eject card (1) %1").arg(cardInPcmcia1Name), 2 ); | ||
77 | } | ||
78 | |||
79 | QPoint p = mapToGlobal( QPoint(1, -menu->sizeHint().height()-1) ); | ||
80 | |||
81 | if ( menu->exec( p, 1 ) == 1 ) { | ||
82 | |||
83 | cmd = "/sbin/cardctl eject 0"; | ||
84 | err = system( (const char *) cmd ); | ||
85 | if ( ( err == 127 ) || ( err < 0 ) ) { | ||
86 | qDebug("Could not execute `/sbin/cardctl eject 0'! err=%d", err); | ||
87 | QMessageBox::warning( this, tr("CardMonitor"), tr("CF/PCMCIA card eject failed!"), | ||
88 | tr("&OK") ); | ||
89 | } | ||
90 | } else if ( menu->exec( p, 1 ) == 0 ) { | ||
91 | cmd = "/etc/sdcontrol compeject"; | ||
92 | err = system( (const char *) cmd ); | ||
93 | if ( ( err != 0 ) ) { | ||
94 | qDebug("Could not execute `/etc/sdcontrol comeject'! err=%d", err); | ||
95 | QMessageBox::warning( this, tr("CardMonitor"), tr("SD/MMC card eject failed!"), | ||
96 | tr("&OK") ); | ||
97 | } | ||
98 | } else if ( menu->exec( p, 1 ) == 2 ) { | ||
99 | cmd = "/sbin/cardctl eject 1"; | ||
100 | err = system( (const char *) cmd ); | ||
101 | if ( ( err == 127 ) || ( err < 0 ) ) { | ||
102 | qDebug("Could not execute `/sbin/cardctl eject 1'! err=%d", err); | ||
103 | QMessageBox::warning( this, tr("CardMonitor"), tr("CF/PCMCIA card eject failed!"), | ||
104 | tr("&OK") ); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | delete menu; | ||
109 | } | ||
110 | |||
111 | |||
112 | void CardMonitor::cardMessage( const QCString &msg, const QByteArray & ) { | ||
113 | if ( msg == "stabChanged()" ) { | ||
114 | if ( getStatusPcmcia() ) { | ||
115 | repaint(FALSE); | ||
116 | } | ||
117 | } else if ( msg == "mtabChanged()" ) { | ||
118 | if ( getStatusSd() ) { | ||
119 | repaint(FALSE); | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
124 | bool CardMonitor::getStatusPcmcia( void ) { | ||
125 | |||
126 | bool cardWas0 = cardInPcmcia0; // remember last state | ||
127 | bool cardWas1 = cardInPcmcia1; | ||
128 | |||
129 | QString fileName; | ||
130 | |||
131 | // one of these 3 files should exist | ||
132 | if (QFile::exists("/var/run/stab")) { | ||
133 | fileName = "/var/run/stab"; | ||
134 | } else if (QFile::exists("/var/state/pcmcia/stab")) { | ||
135 | fileName="/var/state/pcmcia/stab"; | ||
136 | } else { | ||
137 | fileName="/var/lib/pcmcia/stab"; | ||
138 | } | ||
139 | |||
140 | QFile f(fileName); | ||
141 | |||
142 | if ( f.open(IO_ReadOnly) ) { | ||
143 | QStringList list; | ||
144 | QTextStream stream ( &f); | ||
145 | QString streamIn; | ||
146 | streamIn = stream.read(); | ||
147 | list = QStringList::split("\n", streamIn); | ||
148 | for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) { | ||
149 | if( (*line).startsWith("Socket 0:") ){ | ||
150 | // extendable, maybe read out card name | ||
151 | if( (*line).startsWith("Socket 0: empty") && cardInPcmcia0 ){ | ||
152 | cardInPcmcia0 = FALSE; | ||
153 | hide(); | ||
154 | } else if ( !(*line).startsWith("Socket 0: empty") && !cardInPcmcia0 ){ | ||
155 | cardInPcmcia0Name = (*line).mid(((*line).find(':')+1), (*line).length()-9 ); | ||
156 | cardInPcmcia0 = TRUE; | ||
157 | show(); | ||
158 | } | ||
159 | } | ||
160 | if( (*line).startsWith("Socket 1:") ){ | ||
161 | if( (*line).startsWith("Socket 1: empty") && cardInPcmcia1 ){ | ||
162 | cardInPcmcia1 = FALSE; | ||
163 | hide(); | ||
164 | } else if ( !(*line).startsWith("Socket 1: empty") && !cardInPcmcia1 ){ | ||
165 | cardInPcmcia1Name = (*line).mid(((*line).find(':')+1), (*line).length()-9 ); | ||
166 | cardInPcmcia1 = TRUE; | ||
167 | show(); | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | } else { | ||
172 | // no file found | ||
173 | qDebug("no file found"); | ||
174 | cardInPcmcia0 = FALSE; | ||
175 | cardInPcmcia1 = FALSE; | ||
176 | hide(); | ||
177 | return FALSE; | ||
178 | |||
179 | } | ||
180 | |||
181 | f.close(); | ||
182 | |||
183 | return ((cardWas0 == cardInPcmcia0 || cardWas1 == cardInPcmcia1) ? FALSE : TRUE); | ||
184 | } | ||
185 | |||
186 | |||
187 | bool CardMonitor::getStatusSd( void ) { | ||
188 | |||
189 | bool cardWas=cardInSd; // remember last state | ||
190 | cardInSd=false; | ||
191 | |||
192 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) | ||
193 | struct mntent *me; | ||
194 | FILE *mntfp = setmntent( "/etc/mtab", "r" ); | ||
195 | |||
196 | if ( mntfp ) { | ||
197 | while ( (me = getmntent( mntfp )) != 0 ) { | ||
198 | QString fs = me->mnt_fsname; | ||
199 | if ( fs.left(7)=="/dev/sd" || fs.left(9) == "/dev/mmcd" ) { | ||
200 | cardInSd=true; | ||
201 | } | ||
202 | } | ||
203 | endmntent( mntfp ); | ||
204 | if (cardInSd!=cardWas) { | ||
205 | if (cardInSd) { | ||
206 | show(); | ||
207 | } else { | ||
208 | hide(); | ||
209 | } | ||
210 | } | ||
211 | } else { | ||
212 | hide(); | ||
213 | } | ||
214 | #else | ||
215 | #error "Not on Linux" | ||
216 | #endif | ||
217 | return ((cardWas == cardInSd) ? FALSE : TRUE); | ||
218 | } | ||
219 | |||
220 | void CardMonitor::paintEvent( QPaintEvent * ) { | ||
221 | QPainter p( this ); | ||
222 | |||
223 | if ( cardInPcmcia0 || cardInPcmcia1 || cardInSd ) { | ||
224 | p.drawPixmap( 0, 0, pm ); | ||
225 | } else { | ||
226 | p.eraseRect( rect() ); | ||
227 | } | ||
228 | } | ||
229 | |||
230 | |||