-rw-r--r-- | core/applets/cardmon/cardmon.cpp | 45 | ||||
-rw-r--r-- | core/applets/cardmon/cardmon.h | 7 |
2 files changed, 40 insertions, 12 deletions
diff --git a/core/applets/cardmon/cardmon.cpp b/core/applets/cardmon/cardmon.cpp index ac2aea2..3dd9f54 100644 --- a/core/applets/cardmon/cardmon.cpp +++ b/core/applets/cardmon/cardmon.cpp | |||
@@ -3,130 +3,155 @@ | |||
3 | * | 3 | * |
4 | * --------------------- | 4 | * --------------------- |
5 | * | 5 | * |
6 | * copyright : (c) 2002 by Maximilian Reiss | 6 | * copyright : (c) 2002 by Maximilian Reiss |
7 | * email : max.reiss@gmx.de | 7 | * email : max.reiss@gmx.de |
8 | * based on two apps by Devin Butterfield | 8 | * based on two apps by Devin Butterfield |
9 | */ | 9 | */ |
10 | /*************************************************************************** | 10 | /*************************************************************************** |
11 | * * | 11 | * * |
12 | * This program is free software; you can redistribute it and/or modify * | 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 * | 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 * | 14 | * the Free Software Foundation; either version 2 of the License, or * |
15 | * (at your option) any later version. * | 15 | * (at your option) any later version. * |
16 | * * | 16 | * * |
17 | ***************************************************************************/ | 17 | ***************************************************************************/ |
18 | 18 | ||
19 | 19 | ||
20 | #include "cardmon.h" | 20 | #include "cardmon.h" |
21 | 21 | ||
22 | #include <qpe/resource.h> | 22 | #include <qpe/resource.h> |
23 | 23 | ||
24 | #include <qcopchannel_qws.h> | 24 | #include <qcopchannel_qws.h> |
25 | #include <qpainter.h> | 25 | #include <qpainter.h> |
26 | #include <qmessagebox.h> | 26 | #include <qmessagebox.h> |
27 | #include <qpopupmenu.h> | ||
28 | #include <qfile.h> | 27 | #include <qfile.h> |
29 | #include <qtextstream.h> | 28 | #include <qtextstream.h> |
29 | #include <qtimer.h> | ||
30 | 30 | ||
31 | #include <stdio.h> | 31 | #include <stdio.h> |
32 | #include <unistd.h> | 32 | #include <unistd.h> |
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <string.h> | 34 | #include <string.h> |
35 | #include <fcntl.h> | 35 | #include <fcntl.h> |
36 | 36 | ||
37 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) | 37 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) |
38 | #include <sys/vfs.h> | 38 | #include <sys/vfs.h> |
39 | #include <mntent.h> | 39 | #include <mntent.h> |
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | CardMonitor::CardMonitor( QWidget *parent ) : QWidget( parent ), | 42 | CardMonitor::CardMonitor( QWidget *parent ) : QWidget( parent ), |
43 | pm( Resource::loadPixmap( "cardmon/pcmcia" ) ) { | 43 | pm( Resource::loadPixmap( "cardmon/pcmcia" ) ) { |
44 | 44 | ||
45 | QCopChannel* pcmciaChannel = new QCopChannel( "QPE/Card", this ); | 45 | QCopChannel* pcmciaChannel = new QCopChannel( "QPE/Card", this ); |
46 | connect( pcmciaChannel, SIGNAL(received(const QCString &, const QByteArray &)), | 46 | connect( pcmciaChannel, SIGNAL(received(const QCString &, const QByteArray &)), |
47 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); | 47 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); |
48 | 48 | ||
49 | QCopChannel* sdChannel = new QCopChannel( "QPE/Card", this ); | 49 | QCopChannel* sdChannel = new QCopChannel( "QPE/Card", this ); |
50 | connect( sdChannel, SIGNAL(received(const QCString &, const QByteArray &)), | 50 | connect( sdChannel, SIGNAL(received(const QCString &, const QByteArray &)), |
51 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); | 51 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); |
52 | 52 | ||
53 | setFixedSize( pm.size() ); | 53 | setFixedSize( pm.size() ); |
54 | getStatusPcmcia(TRUE); | 54 | getStatusPcmcia(TRUE); |
55 | getStatusSd(TRUE); | 55 | getStatusSd(TRUE); |
56 | repaint(FALSE); | 56 | repaint(FALSE); |
57 | popUpMenu = 0; | ||
58 | popUpMenuTimer = 0; | ||
57 | } | 59 | } |
58 | 60 | ||
59 | CardMonitor::~CardMonitor() { | 61 | CardMonitor::~CardMonitor() { |
60 | } | 62 | } |
61 | 63 | ||
64 | void CardMonitor::popUp(QString message) { | ||
65 | if ( ! popUpMenu ) { | ||
66 | popUpMenu = new QPopupMenu(); | ||
67 | } | ||
68 | popUpMenu->clear(); | ||
69 | popUpMenu->insertItem( message, 0 ); | ||
70 | |||
71 | QPoint p = mapToGlobal ( QPoint ( 0, 0 )); | ||
72 | QSize s = popUpMenu->sizeHint ( ); | ||
73 | popUpMenu->popup( QPoint ( | ||
74 | p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), | ||
75 | p. y ( ) - s. height ( ) ), 0); | ||
76 | |||
77 | if ( ! popUpMenuTimer ) { | ||
78 | popUpMenuTimer = new QTimer( this ); | ||
79 | connect( popUpMenuTimer, SIGNAL(timeout()), this, SLOT(popUpTimeout()) ); | ||
80 | } | ||
81 | timerEvent(0); | ||
82 | popUpMenuTimer->start( 2000 ); | ||
83 | } | ||
84 | |||
85 | void CardMonitor::popUpTimeout() { | ||
86 | popUpMenu->hide(); | ||
87 | popUpMenuTimer->stop(); | ||
88 | } | ||
89 | |||
62 | void CardMonitor::mousePressEvent( QMouseEvent * ) { | 90 | void CardMonitor::mousePressEvent( QMouseEvent * ) { |
63 | QPopupMenu *menu = new QPopupMenu(); | 91 | QPopupMenu *menu = new QPopupMenu(); |
64 | QString cmd; | 92 | QString cmd; |
65 | int err=0; | 93 | int err=0; |
66 | 94 | ||
67 | if ( cardInSd ) { | 95 | if ( cardInSd ) { |
68 | menu->insertItem( tr("Eject SD/MMC card"), 0 ); | 96 | menu->insertItem( tr("Eject SD/MMC card"), 0 ); |
69 | } | 97 | } |
70 | 98 | ||
71 | if ( cardInPcmcia0 ) { | 99 | if ( cardInPcmcia0 ) { |
72 | menu->insertItem( tr("Eject card 0: %1").arg(cardInPcmcia0Name), 1 ); | 100 | menu->insertItem( tr("Eject card 0: %1").arg(cardInPcmcia0Name), 1 ); |
73 | } | 101 | } |
74 | 102 | ||
75 | if ( cardInPcmcia1 ) { | 103 | if ( cardInPcmcia1 ) { |
76 | menu->insertItem( tr("Eject card 1: %1").arg(cardInPcmcia1Name), 2 ); | 104 | menu->insertItem( tr("Eject card 1: %1").arg(cardInPcmcia1Name), 2 ); |
77 | } | 105 | } |
78 | 106 | ||
79 | QPoint p = mapToGlobal ( QPoint ( 0, 0 )); | 107 | QPoint p = mapToGlobal ( QPoint ( 0, 0 )); |
80 | QSize s = menu->sizeHint ( ); | 108 | QSize s = menu->sizeHint ( ); |
81 | int opt = menu->exec( QPoint ( | 109 | int opt = menu->exec( QPoint ( |
82 | p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), | 110 | p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), |
83 | p. y ( ) - s. height ( ) ), 0); | 111 | p. y ( ) - s. height ( ) ), 0); |
84 | 112 | ||
85 | if ( opt == 1 ) { | 113 | if ( opt == 1 ) { |
86 | 114 | ||
87 | cmd = "/sbin/cardctl eject 0"; | 115 | cmd = "/sbin/cardctl eject 0"; |
88 | err = system( (const char *) cmd ); | 116 | err = system( (const char *) cmd ); |
89 | if ( ( err == 127 ) || ( err < 0 ) ) { | 117 | if ( ( err == 127 ) || ( err < 0 ) ) { |
90 | qDebug("Could not execute `/sbin/cardctl eject 0'! err=%d", err); | 118 | qDebug("Could not execute `/sbin/cardctl eject 0'! err=%d", err); |
91 | QMessageBox::warning( this, tr("CardMonitor"), tr("CF/PCMCIA card eject failed!"), | 119 | popUp( tr("CF/PCMCIA card eject failed!")); |
92 | tr("&OK") ); | ||
93 | } | 120 | } |
94 | } else if ( opt == 0 ) { | 121 | } else if ( opt == 0 ) { |
95 | cmd = "/etc/sdcontrol compeject"; | 122 | cmd = "/etc/sdcontrol compeject"; |
96 | err = system( (const char *) cmd ); | 123 | err = system( (const char *) cmd ); |
97 | if ( ( err != 0 ) ) { | 124 | if ( ( err != 0 ) ) { |
98 | qDebug("Could not execute `/etc/sdcontrol comeject'! err=%d", err); | 125 | qDebug("Could not execute `/etc/sdcontrol comeject'! err=%d", err); |
99 | QMessageBox::warning( this, tr("CardMonitor"), tr("SD/MMC card eject failed!"), | 126 | popUp( tr("SD/MMC card eject failed!")); |
100 | tr("&OK") ); | ||
101 | } | 127 | } |
102 | } else if ( opt == 2 ) { | 128 | } else if ( opt == 2 ) { |
103 | cmd = "/sbin/cardctl eject 1"; | 129 | cmd = "/sbin/cardctl eject 1"; |
104 | err = system( (const char *) cmd ); | 130 | err = system( (const char *) cmd ); |
105 | if ( ( err == 127 ) || ( err < 0 ) ) { | 131 | if ( ( err == 127 ) || ( err < 0 ) ) { |
106 | qDebug("Could not execute `/sbin/cardctl eject 1'! err=%d", err); | 132 | qDebug("Could not execute `/sbin/cardctl eject 1'! err=%d", err); |
107 | QMessageBox::warning( this, tr("CardMonitor"), tr("CF/PCMCIA card eject failed!"), | 133 | popUp( tr("CF/PCMCIA card eject failed!")); |
108 | tr("&OK") ); | ||
109 | } | 134 | } |
110 | } | 135 | } |
111 | 136 | ||
112 | delete menu; | 137 | delete menu; |
113 | } | 138 | } |
114 | 139 | ||
115 | 140 | ||
116 | void CardMonitor::cardMessage( const QCString &msg, const QByteArray & ) { | 141 | void CardMonitor::cardMessage( const QCString &msg, const QByteArray & ) { |
117 | if ( msg == "stabChanged()" ) { | 142 | if ( msg == "stabChanged()" ) { |
118 | // qDebug("Pcmcia: stabchanged"); | 143 | // qDebug("Pcmcia: stabchanged"); |
119 | if ( getStatusPcmcia() ) { | 144 | if ( getStatusPcmcia() ) { |
120 | repaint(FALSE); | 145 | repaint(FALSE); |
121 | } | 146 | } |
122 | } else if ( msg == "mtabChanged()" ) { | 147 | } else if ( msg == "mtabChanged()" ) { |
123 | // qDebug("Pcmcia: mtabchanged"); | 148 | // qDebug("Pcmcia: mtabchanged"); |
124 | if ( getStatusSd() ) { | 149 | if ( getStatusSd() ) { |
125 | repaint(FALSE); | 150 | repaint(FALSE); |
126 | } | 151 | } |
127 | } | 152 | } |
128 | } | 153 | } |
129 | 154 | ||
130 | bool CardMonitor::getStatusPcmcia( int showPopUp = FALSE ) { | 155 | bool CardMonitor::getStatusPcmcia( int showPopUp = FALSE ) { |
131 | 156 | ||
132 | bool cardWas0 = cardInPcmcia0; // remember last state | 157 | bool cardWas0 = cardInPcmcia0; // remember last state |
@@ -180,79 +205,77 @@ bool CardMonitor::getStatusPcmcia( int showPopUp = FALSE ) { | |||
180 | cardInPcmcia1 = FALSE; | 205 | cardInPcmcia1 = FALSE; |
181 | return FALSE; | 206 | return FALSE; |
182 | 207 | ||
183 | } | 208 | } |
184 | 209 | ||
185 | if(!cardInPcmcia0 && !cardInPcmcia1) { | 210 | if(!cardInPcmcia0 && !cardInPcmcia1) { |
186 | qDebug("Pcmcia: no cards"); | 211 | qDebug("Pcmcia: no cards"); |
187 | } | 212 | } |
188 | 213 | ||
189 | if( !showPopUp && (cardWas0 != cardInPcmcia0 || cardWas1 != cardInPcmcia1)) { | 214 | if( !showPopUp && (cardWas0 != cardInPcmcia0 || cardWas1 != cardInPcmcia1)) { |
190 | QString text = ""; | 215 | QString text = ""; |
191 | if(cardWas0 != cardInPcmcia0) { | 216 | if(cardWas0 != cardInPcmcia0) { |
192 | if(cardInPcmcia0) { text += tr("New card: "); } | 217 | if(cardInPcmcia0) { text += tr("New card: "); } |
193 | else { text += tr("Ejected: "); } | 218 | else { text += tr("Ejected: "); } |
194 | text += cardInPcmcia0Name; | 219 | text += cardInPcmcia0Name; |
195 | } | 220 | } |
196 | if(cardWas0 != cardInPcmcia0 && cardWas1 != cardInPcmcia1) { | 221 | if(cardWas0 != cardInPcmcia0 && cardWas1 != cardInPcmcia1) { |
197 | text += "\n"; | 222 | text += "\n"; |
198 | } | 223 | } |
199 | if(cardWas1 != cardInPcmcia1) { | 224 | if(cardWas1 != cardInPcmcia1) { |
200 | if(cardInPcmcia1) { text += tr("New card: "); } | 225 | if(cardInPcmcia1) { text += tr("New card: "); } |
201 | else { text += tr("Ejected: "); } | 226 | else { text += tr("Ejected: "); } |
202 | text += cardInPcmcia1Name; | 227 | text += cardInPcmcia1Name; |
203 | } | 228 | } |
204 | QMessageBox::warning( this, tr("CardMonitor"), text, | 229 | popUp( text ); |
205 | tr("&OK") ); | ||
206 | } | 230 | } |
207 | 231 | ||
208 | f.close(); | 232 | f.close(); |
209 | 233 | ||
210 | return ((cardWas0 == cardInPcmcia0 || cardWas1 == cardInPcmcia1) ? FALSE : TRUE); | 234 | return ((cardWas0 == cardInPcmcia0 || cardWas1 == cardInPcmcia1) ? FALSE : TRUE); |
211 | } | 235 | } |
212 | 236 | ||
213 | 237 | ||
214 | bool CardMonitor::getStatusSd( int showPopUp = FALSE ) { | 238 | bool CardMonitor::getStatusSd( int showPopUp = FALSE ) { |
215 | 239 | ||
216 | bool cardWas=cardInSd; // remember last state | 240 | bool cardWas=cardInSd; // remember last state |
217 | cardInSd=false; | 241 | cardInSd=false; |
218 | 242 | ||
219 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) | 243 | #if defined(_OS_LINUX_) || defined(Q_OS_LINUX) |
220 | struct mntent *me; | 244 | struct mntent *me; |
221 | FILE *mntfp = setmntent( "/etc/mtab", "r" ); | 245 | FILE *mntfp = setmntent( "/etc/mtab", "r" ); |
222 | 246 | ||
223 | if ( mntfp ) { | 247 | if ( mntfp ) { |
224 | while ( (me = getmntent( mntfp )) != 0 ) { | 248 | while ( (me = getmntent( mntfp )) != 0 ) { |
225 | QString fs = me->mnt_fsname; | 249 | QString fs = me->mnt_fsname; |
226 | if ( fs.left(7)=="/dev/sd" || fs.left(9) == "/dev/mmcd" ) { | 250 | if ( fs.left(7)=="/dev/sd" || fs.left(9) == "/dev/mmcd" ) { |
227 | cardInSd=true; | 251 | cardInSd=true; |
228 | } | 252 | } |
229 | } | 253 | } |
230 | endmntent( mntfp ); | 254 | endmntent( mntfp ); |
231 | } | 255 | } |
232 | if(!showPopUp && cardWas != cardInSd) { | 256 | if(!showPopUp && cardWas != cardInSd) { |
233 | QString text = ""; | 257 | QString text = ""; |
234 | if(cardInSd) { text += "SD Inserted"; } | 258 | if(cardInSd) { text += "SD Inserted"; } |
235 | else { text += "SD Removed"; } | 259 | else { text += "SD Removed"; } |
236 | QMessageBox::warning( this, tr("CardMonitor"), text, | 260 | popUp( text ); |
237 | tr("&OK") ); | ||
238 | } | 261 | } |
239 | 262 | ||
240 | #else | 263 | #else |
241 | #error "Not on Linux" | 264 | #error "Not on Linux" |
242 | #endif | 265 | #endif |
243 | return ((cardWas == cardInSd) ? FALSE : TRUE); | 266 | return ((cardWas == cardInSd) ? FALSE : TRUE); |
244 | } | 267 | } |
245 | 268 | ||
246 | void CardMonitor::paintEvent( QPaintEvent * ) { | 269 | void CardMonitor::paintEvent( QPaintEvent * ) { |
247 | QPainter p( this ); | 270 | QPainter p( this ); |
248 | 271 | ||
249 | if ( cardInPcmcia0 || cardInPcmcia1 || cardInSd ) { | 272 | if ( cardInPcmcia0 || cardInPcmcia1 || cardInSd ) { |
250 | p.drawPixmap( 0, 0, pm ); | 273 | p.drawPixmap( 0, 0, pm ); |
251 | show(); | 274 | show(); |
252 | } else { | 275 | } else { |
253 | p.eraseRect( rect() ); | 276 | p.eraseRect( rect() ); |
254 | hide(); | 277 | hide(); |
255 | } | 278 | } |
256 | } | 279 | } |
257 | 280 | ||
258 | 281 | ||
diff --git a/core/applets/cardmon/cardmon.h b/core/applets/cardmon/cardmon.h index f9e3819..34b8d95 100644 --- a/core/applets/cardmon/cardmon.h +++ b/core/applets/cardmon/cardmon.h | |||
@@ -1,53 +1,58 @@ | |||
1 | /* | 1 | /* |
2 | * cardmon.h | 2 | * cardmon.h |
3 | * | 3 | * |
4 | * --------------------- | 4 | * --------------------- |
5 | * | 5 | * |
6 | * copyright : (c) 2002 by Maximilian Reiss | 6 | * copyright : (c) 2002 by Maximilian Reiss |
7 | * email : max.reiss@gmx.de | 7 | * email : max.reiss@gmx.de |
8 | * based on two apps by Devin Butterfield | 8 | * based on two apps by Devin Butterfield |
9 | */ | 9 | */ |
10 | /*************************************************************************** | 10 | /*************************************************************************** |
11 | * * | 11 | * * |
12 | * This program is free software; you can redistribute it and/or modify * | 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 * | 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 * | 14 | * the Free Software Foundation; either version 2 of the License, or * |
15 | * (at your option) any later version. * | 15 | * (at your option) any later version. * |
16 | * * | 16 | * * |
17 | ***************************************************************************/ | 17 | ***************************************************************************/ |
18 | 18 | ||
19 | #ifndef CARDMON_H | 19 | #ifndef CARDMON_H |
20 | #define CARDMON_H | 20 | #define CARDMON_H |
21 | 21 | ||
22 | #include <qwidget.h> | 22 | #include <qwidget.h> |
23 | #include <qpixmap.h> | 23 | #include <qpixmap.h> |
24 | #include <qpopupmenu.h> | ||
25 | #include <qtimer.h> | ||
24 | 26 | ||
25 | class CardMonitor : public QWidget { | 27 | class CardMonitor : public QWidget { |
26 | Q_OBJECT | 28 | Q_OBJECT |
27 | public: | 29 | public: |
28 | CardMonitor( QWidget *parent = 0 ); | 30 | CardMonitor( QWidget *parent = 0 ); |
29 | ~CardMonitor(); | 31 | ~CardMonitor(); |
30 | bool getStatusPcmcia( int showPopUp = FALSE ); | 32 | bool getStatusPcmcia( int showPopUp = FALSE ); |
31 | bool getStatusSd( int showPopUp = FALSE ); | 33 | bool getStatusSd( int showPopUp = FALSE ); |
32 | 34 | ||
33 | private slots: | 35 | private slots: |
34 | void cardMessage( const QCString &msg, const QByteArray & ); | 36 | void cardMessage( const QCString &msg, const QByteArray & ); |
37 | void popUpTimeout(); | ||
35 | 38 | ||
36 | protected: | 39 | protected: |
37 | void paintEvent( QPaintEvent* ); | 40 | void paintEvent( QPaintEvent* ); |
38 | void mousePressEvent( QMouseEvent * ); | 41 | void mousePressEvent( QMouseEvent * ); |
39 | private: | 42 | private: |
40 | QPixmap pm; | 43 | QPixmap pm; |
44 | QPopupMenu *popUpMenu; | ||
45 | QTimer *popUpMenuTimer; | ||
41 | // pcmcia socket 0 | 46 | // pcmcia socket 0 |
42 | bool cardInPcmcia0; | 47 | bool cardInPcmcia0; |
43 | QString cardInPcmcia0Name; | 48 | QString cardInPcmcia0Name; |
44 | // pcmcia socket 1 | 49 | // pcmcia socket 1 |
45 | bool cardInPcmcia1; | 50 | bool cardInPcmcia1; |
46 | QString cardInPcmcia1Name; | 51 | QString cardInPcmcia1Name; |
47 | bool cardInSd; | 52 | bool cardInSd; |
48 | void iconShow(); | 53 | void iconShow(); |
49 | 54 | void popUp(QString message); | |
50 | }; | 55 | }; |
51 | 56 | ||
52 | #endif | 57 | #endif |
53 | 58 | ||