summaryrefslogtreecommitdiff
authorerik <erik>2007-03-06 01:25:46 (UTC)
committer erik <erik>2007-03-06 01:25:46 (UTC)
commitf6b4fc71812aa4db9c7209e28667010d1f04d771 (patch) (unidiff)
tree77bc5b50326ba2d525684eaf46b671b82defcf3c
parent3efe70df6fdaf32a8213ceadbdd1679a0a658934 (diff)
downloadopie-f6b4fc71812aa4db9c7209e28667010d1f04d771.zip
opie-f6b4fc71812aa4db9c7209e28667010d1f04d771.tar.gz
opie-f6b4fc71812aa4db9c7209e28667010d1f04d771.tar.bz2
Reworked to fit into the Opie style guidelines.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--core/applets/irdaapplet/irda.cpp134
-rw-r--r--core/applets/irdaapplet/irda.h6
2 files changed, 89 insertions, 51 deletions
diff --git a/core/applets/irdaapplet/irda.cpp b/core/applets/irdaapplet/irda.cpp
index cea836e..d57104b 100644
--- a/core/applets/irdaapplet/irda.cpp
+++ b/core/applets/irdaapplet/irda.cpp
@@ -38,36 +38,44 @@
38#include <sys/types.h> 38#include <sys/types.h>
39#include <sys/socket.h> 39#include <sys/socket.h>
40#include <sys/ioctl.h> 40#include <sys/ioctl.h>
41 41
42//=========================================================================== 42//===========================================================================
43 43
44IrdaApplet::IrdaApplet ( QWidget *parent, const char *name ) 44IrdaApplet::IrdaApplet ( QWidget *parent, const char *name ) :
45 : QWidget ( parent, name ) 45 QWidget ( parent, name ),
46 m_irda_active( false ),
47 m_irda_discovery_active( false ),
48 m_receive_active( false ),
49 m_receive_state_changed( false ),
50 m_popup( 0 ),
51 m_wasOn( false ),
52 m_wasDiscover( false )
46{ 53{
47 setFixedHeight( AppLnk::smallIconSize() ); 54 setFixedHeight( AppLnk::smallIconSize() );
48 setFixedWidth( AppLnk::smallIconSize() ); 55 setFixedWidth( AppLnk::smallIconSize() );
49 56
50 m_sockfd = ::socket ( PF_INET, SOCK_DGRAM, IPPROTO_IP ); 57 if (m_sockfd = ::socket ( PF_INET, SOCK_DGRAM, IPPROTO_IP ) == -1)
51 58 perror ( "failed grabbing IrDA socket" );
52 m_irdaOnPixmap = Opie::Core::OResource::loadPixmap( "irdaapplet/irdaon", Opie::Core::OResource::SmallIcon ); 59
53 m_irdaOffPixmap = Opie::Core::OResource::loadPixmap( "irdaapplet/irdaoff", Opie::Core::OResource::SmallIcon ); 60 m_irdaOnPixmap =
54 m_irdaDiscoveryOnPixmap = Opie::Core::OResource::loadPixmap( "irdaapplet/magglass", Opie::Core::OResource::SmallIcon ); 61 Opie::Core::OResource::loadPixmap( "irdaapplet/irdaon",
55 m_receiveActivePixmap = Opie::Core::OResource::loadPixmap( "irdaapplet/receive", Opie::Core::OResource::SmallIcon ); 62 Opie::Core::OResource::SmallIcon );
56 63 m_irdaOffPixmap =
57 m_irda_active = false; 64 Opie::Core::OResource::loadPixmap( "irdaapplet/irdaoff",
58 m_irda_discovery_active = false; 65 Opie::Core::OResource::SmallIcon );
59 m_receive_active = false; 66 m_irdaDiscoveryOnPixmap =
60 m_receive_state_changed = false; 67 Opie::Core::OResource::loadPixmap( "irdaapplet/magglass",
61 m_popup = 0; 68 Opie::Core::OResource::SmallIcon );
62 m_wasOn = false; 69 m_receiveActivePixmap =
63 m_wasDiscover = false; 70 Opie::Core::OResource::loadPixmap( "irdaapplet/receive",
71 Opie::Core::OResource::SmallIcon );
64 72
65 QCopChannel* chan = new QCopChannel("QPE/IrDaApplet", this ); 73 QCopChannel* chan = new QCopChannel("QPE/IrDaApplet", this );
66 connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ), 74 connect(chan, SIGNAL(received(const QCString&,const QByteArray&) ), this,
67 this, SLOT(slotMessage(const QCString&,const QByteArray&) ) ); 75 SLOT(slotMessage(const QCString&,const QByteArray&) ) );
68} 76}
69 77
70int IrdaApplet::position() 78int IrdaApplet::position()
71{ 79{
72 return 6; 80 return 6;
73} 81}
@@ -91,19 +99,28 @@ void IrdaApplet::popup ( QString message, QString icon )
91 99
92 m_popup-> clear ( ); 100 m_popup-> clear ( );
93 101
94 if ( icon. isEmpty ( )) 102 if ( icon. isEmpty ( ))
95 m_popup-> insertItem ( message, 0 ); 103 m_popup-> insertItem ( message, 0 );
96 else 104 else
97 m_popup-> insertItem ( QIconSet ( Opie::Core::OResource::loadPixmap ( icon, Opie::Core::OResource::SmallIcon )), 105 m_popup-> insertItem ( QIconSet (
98 message, 0 ); 106 Opie::Core::OResource::loadPixmap (
107 icon,
108 Opie::Core::OResource::SmallIcon
109 )
110 ),
111 message, 0
112 );
99 113
100 QPoint p = mapToGlobal ( QPoint ( 0, 0 )); 114 QPoint p = mapToGlobal ( QPoint ( 0, 0 ));
101 QSize s = m_popup-> sizeHint ( ); 115 QSize s = m_popup-> sizeHint ( );
102 m_popup-> popup ( QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), 116 m_popup-> popup ( QPoint (
103 p. y ( ) - s. height ( ))); 117 p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ),
118 p. y ( ) - s. height ( )
119 )
120 );
104 121
105 QTimer::singleShot ( 2000, this, SLOT( popupTimeout())); 122 QTimer::singleShot ( 2000, this, SLOT( popupTimeout()));
106} 123}
107 124
108void IrdaApplet::popupTimeout ( ) 125void IrdaApplet::popupTimeout ( )
109{ 126{
@@ -112,36 +129,35 @@ void IrdaApplet::popupTimeout ( )
112 129
113bool IrdaApplet::checkIrdaStatus ( ) 130bool IrdaApplet::checkIrdaStatus ( )
114{ 131{
115 struct ifreq ifr; 132 struct ifreq ifr;
116 strcpy ( ifr. ifr_name, "irda0" ); 133 strcpy ( ifr. ifr_name, "irda0" );
117 134
118 if ( ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 ) 135 if ( m_sockfd < 0 || ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 )
119 return false; 136 return false;
120 137
121 return ( ifr. ifr_flags & IFF_UP ); 138 return ( ifr. ifr_flags & IFF_UP );
122} 139}
123 140
124bool IrdaApplet::setIrdaStatus ( bool b ) 141bool IrdaApplet::setIrdaStatus ( bool b )
125{ 142{
126 struct ifreq ifr; 143 struct ifreq ifr;
127 strcpy ( ifr. ifr_name, "irda0" ); 144 strcpy ( ifr. ifr_name, "irda0" );
128 145
129 if ( ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 ) 146 if ( m_sockfd < 0 || ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 )
130 return false; 147 return false;
131 148
132 if ( b ) { 149 if ( b )
133 ifr. ifr_flags |= IFF_UP; 150 ifr. ifr_flags |= IFF_UP;
134 }
135 else { 151 else {
136 setIrdaDiscoveryStatus ( 0 ); 152 setIrdaDiscoveryStatus ( 0 );
137 setIrdaReceiveStatus ( 0 ); 153 setIrdaReceiveStatus ( 0 );
138 ifr. ifr_flags &= ~IFF_UP; 154 ifr. ifr_flags &= ~IFF_UP;
139 } 155 }
140 156
141 if ( ::ioctl ( m_sockfd, SIOCSIFFLAGS, &ifr ) < 0 ) 157 if ( m_sockfd < 0 || ::ioctl ( m_sockfd, SIOCSIFFLAGS, &ifr ) < 0 )
142 return false; 158 return false;
143 159
144 return true; 160 return true;
145} 161}
146 162
147bool IrdaApplet::checkIrdaDiscoveryStatus ( ) 163bool IrdaApplet::checkIrdaDiscoveryStatus ( )
@@ -193,25 +209,33 @@ void IrdaApplet::showDiscovered ( )
193 if ( discovery. open ( IO_ReadOnly )) { 209 if ( discovery. open ( IO_ReadOnly )) {
194 bool qcopsend = false; 210 bool qcopsend = false;
195 211
196 QString discoveredDevice; 212 QString discoveredDevice;
197 QString deviceAddr; 213 QString deviceAddr;
198 214
199 // since it is /proc we _must_ use QTextStream 215 /*!
200 QStringList list = QStringList::split ( "\n", QTextStream ( &discovery ). read ( )); 216 * @note To read /proc, it makes more sense to use QTextStream.
217 */
218 QStringList list =
219 QStringList::split ( "\n", QTextStream ( &discovery ). read ( ));
201 220
202 QMap <QString, QString>::Iterator it; 221 QMap <QString, QString>::Iterator it;
203 222
204 for ( it = m_devices. begin ( ); it != m_devices. end ( ); ++it ) 223 for ( it = m_devices. begin ( ); it != m_devices. end ( ); ++it )
205 it. data ( ). prepend ( "+++" ); 224 it. data ( ). prepend ( "+++" );
206 225
207 for ( QStringList::Iterator lit = list. begin ( ); lit != list. end ( ); ++lit ) { 226 for ( QStringList::Iterator lit = list. begin ( );
227 lit != list. end ( ); ++lit
228 )
229 {
208 const QString &line = *lit; 230 const QString &line = *lit;
209 231
210 if ( line. startsWith ( "nickname:" )) { 232 if ( line. startsWith ( "nickname:" )) {
211 discoveredDevice = line. mid ( line. find ( ':' ) + 2, line. find ( ',' ) - line. find ( ':' ) - 2 ); 233 discoveredDevice =
234 line. mid ( line. find ( ':' ) + 2, line. find ( ',' ) -
235 line. find ( ':' ) - 2 );
212 deviceAddr = line. mid ( line. find ( "daddr:" ) + 9, 8 ); 236 deviceAddr = line. mid ( line. find ( "daddr:" ) + 9, 8 );
213 237
214 // odebug << discoveredDevice + "(" + deviceAddr + ")" << oendl; 238 // odebug << discoveredDevice + "(" + deviceAddr + ")" << oendl;
215 239
216 if ( !m_devices. contains ( deviceAddr )) { 240 if ( !m_devices. contains ( deviceAddr )) {
217 popup ( tr( "Found:" ) + " " + discoveredDevice ); 241 popup ( tr( "Found:" ) + " " + discoveredDevice );
@@ -220,26 +244,28 @@ void IrdaApplet::showDiscovered ( )
220 } 244 }
221 m_devices. replace ( deviceAddr, discoveredDevice ); 245 m_devices. replace ( deviceAddr, discoveredDevice );
222 } 246 }
223 } 247 }
224 248
225 for ( it = m_devices. begin ( ); it != m_devices. end ( ); ) { 249 for ( it = m_devices. begin ( ); it != m_devices. end ( ); ) {
226 // odebug << "IrdaMon: delete " + it.currentKey() + "=" + *devicesAvailable[it.currentKey()] + "?" << oendl; 250 // odebug << "IrdaMon: delete " + it.currentKey() + "=" +
251 // *devicesAvailable[it.currentKey()] + "?" << oendl;
227 252
228 if ( it. data ( ). left ( 3 ) == "+++" ) { 253 if ( it. data ( ). left ( 3 ) == "+++" ) {
229 popup ( tr( "Lost:" ) + " " + it. data ( ). mid ( 3 )); 254 popup ( tr( "Lost:" ) + " " + it. data ( ). mid ( 3 ));
230 //snd_lost. play ( ); 255 //snd_lost. play ( );
231 256
232 QMap <QString, QString>::Iterator tmp = it; 257 QMap <QString, QString>::Iterator tmp = it;
233 tmp++; 258 tmp++;
234 m_devices. remove ( it ); // in contrast to QValueListIterator this remove doesn't return the next Iterator 259 m_devices. remove ( it );
235 it = tmp; 260 ///< @note In contrast to QValueListIterator this remove doesn't
261 ///< return the next Iterator
236 262
263 it = tmp;
237 qcopsend = true; 264 qcopsend = true;
238 } 265 } else
239 else
240 it++; 266 it++;
241 } 267 }
242 // XXX if( qcopsend ) { 268 // XXX if( qcopsend ) {
243 QCopEnvelope e ( "QPE/Network", "irdaSend(bool)" ); 269 QCopEnvelope e ( "QPE/Network", "irdaSend(bool)" );
244 e << ( m_devices. count ( ) > 0 ); 270 e << ( m_devices. count ( ) > 0 );
245 // } 271 // }
@@ -256,32 +282,42 @@ void IrdaApplet::mousePressEvent ( QMouseEvent * )
256 282
257 //menu->insertItem( tr("More..."), 4 ); 283 //menu->insertItem( tr("More..."), 4 );
258 284
259 if ( m_irda_active && !m_devices. isEmpty ( )) { 285 if ( m_irda_active && !m_devices. isEmpty ( )) {
260 menu-> insertItem ( tr( "Discovered Device:" ), 9 ); 286 menu-> insertItem ( tr( "Discovered Device:" ), 9 );
261 287
262 for ( QMap<QString, QString>::Iterator it = m_devices. begin ( ); it != m_devices. end ( ); ++it ) 288 for ( QMap<QString, QString>::Iterator it = m_devices. begin ( );
289 it != m_devices. end ( ); ++it )
290 {
263 menu-> insertItem ( *it ); 291 menu-> insertItem ( *it );
292 }
264 293
265 menu-> insertSeparator ( ); 294 menu-> insertSeparator ( );
266 } 295 }
267 296
268 menu-> insertItem ( m_irda_active ? tr( "Disable IrDA" ) : tr( "Enable IrDA" ), 0 ); 297 menu-> insertItem ( m_irda_active ? tr( "Disable IrDA" ) : tr( "Enable IrDA" ), 0 );
269 298
270 if ( m_irda_active ) { 299 if ( m_irda_active ) {
271 menu-> insertItem ( m_irda_discovery_active ? tr( "Disable Discovery" ) : tr( "Enable Discovery" ), 1 ); 300 menu-> insertItem (
301 m_irda_discovery_active ? tr( "Disable Discovery" ) :
302 tr( "Enable Discovery" ), 1
303 );
272 304
273 /* Only Receive if OBEX is installed */ 305 /* Only Receive if OBEX is installed */
274 if( Ir::supported() ) 306 if( Ir::supported() )
275 menu-> insertItem ( m_receive_active ? tr( "Disable Receive" ) : tr( "Enable Receive" ), 2 ); 307 menu-> insertItem (
308 m_receive_active ? tr( "Disable Receive" ) :
309 tr( "Enable Receive" ), 2
310 );
276 } 311 }
277 312
278 QPoint p = mapToGlobal ( QPoint ( 0, 0 ) ); 313 QPoint p = mapToGlobal ( QPoint ( 0, 0 ) );
279 QSize s = menu-> sizeHint ( ); 314 QSize s = menu-> sizeHint ( );
280 315
281 p = QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), p. y ( ) - s. height ( )); 316 p = QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), p. y ( ) -
317 s. height ( ));
282 318
283 switch ( menu-> exec ( p )) { 319 switch ( menu-> exec ( p )) {
284 case 0: 320 case 0:
285 setIrdaStatus ( !m_irda_active ); 321 setIrdaStatus ( !m_irda_active );
286 timerEvent ( 0 ); 322 timerEvent ( 0 );
287 break; 323 break;
@@ -312,15 +348,18 @@ void IrdaApplet::timerEvent ( QTimerEvent * )
312 m_irda_active = checkIrdaStatus ( ); 348 m_irda_active = checkIrdaStatus ( );
313 m_irda_discovery_active = checkIrdaDiscoveryStatus ( ); 349 m_irda_discovery_active = checkIrdaDiscoveryStatus ( );
314 350
315 if ( m_irda_discovery_active ) 351 if ( m_irda_discovery_active )
316 showDiscovered ( ); 352 showDiscovered ( );
317 353
318 if (( m_irda_active != oldactive ) || ( m_irda_discovery_active != olddiscovery ) || receiveUpdate ) 354 if (( m_irda_active != oldactive ) ||
355 ( m_irda_discovery_active != olddiscovery ) || receiveUpdate )
356 {
319 update ( ); 357 update ( );
320} 358}
359}
321 360
322void IrdaApplet::paintEvent ( QPaintEvent * ) 361void IrdaApplet::paintEvent ( QPaintEvent * )
323{ 362{
324 QPainter p( this ); 363 QPainter p( this );
325 364
326 p.drawPixmap( 0, 1, m_irda_active ? m_irdaOnPixmap : m_irdaOffPixmap ); 365 p.drawPixmap( 0, 1, m_irda_active ? m_irdaOnPixmap : m_irdaOffPixmap );
@@ -328,18 +367,18 @@ void IrdaApplet::paintEvent ( QPaintEvent * )
328 if ( m_irda_discovery_active ) 367 if ( m_irda_discovery_active )
329 p.drawPixmap( 0, 1, m_irdaDiscoveryOnPixmap ); 368 p.drawPixmap( 0, 1, m_irdaDiscoveryOnPixmap );
330 369
331 if ( m_receive_active ) 370 if ( m_receive_active )
332 p.drawPixmap( 0, 1, m_receiveActivePixmap ); 371 p.drawPixmap( 0, 1, m_receiveActivePixmap );
333} 372}
334/* 373
335 * We know 3 calls 374/*!
336 * a) enable 375 * We recognize 3 events:
337 * b) disable 376 * - enable: Attempt to enable the IrDA device
338 * a and b will temp enable the IrDa device and disable will disable it again if it wasn't on 377 * - disable: Attempt to disable the IrDA device if it was already enabled
339 * c) listDevices: We will return a list of known devices 378 * - listDevices: Returns a list of known IrDA devices
340 */ 379 */
341void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) { 380void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) {
342 if ( str == "enableIrda()") { 381 if ( str == "enableIrda()") {
343 m_wasOn = checkIrdaStatus(); 382 m_wasOn = checkIrdaStatus();
344 m_wasDiscover = checkIrdaDiscoveryStatus(); 383 m_wasDiscover = checkIrdaDiscoveryStatus();
345 if (!m_wasOn) { 384 if (!m_wasOn) {
@@ -365,6 +404,7 @@ void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) {
365 404
366 e << list; 405 e << list;
367 } 406 }
368} 407}
369 408
370EXPORT_OPIE_APPLET_v1( IrdaApplet ) 409EXPORT_OPIE_APPLET_v1( IrdaApplet )
410
diff --git a/core/applets/irdaapplet/irda.h b/core/applets/irdaapplet/irda.h
index f713bbe..2cbf4f7 100644
--- a/core/applets/irdaapplet/irda.h
+++ b/core/applets/irdaapplet/irda.h
@@ -49,13 +49,12 @@ private:
49 bool checkIrdaDiscoveryStatus (); 49 bool checkIrdaDiscoveryStatus ();
50 bool setIrdaDiscoveryStatus ( bool ); 50 bool setIrdaDiscoveryStatus ( bool );
51 bool setIrdaReceiveStatus ( bool ); 51 bool setIrdaReceiveStatus ( bool );
52 52
53 void showDiscovered(); 53 void showDiscovered();
54 54
55private:
56 QPixmap m_irdaOnPixmap; 55 QPixmap m_irdaOnPixmap;
57 QPixmap m_irdaOffPixmap; 56 QPixmap m_irdaOffPixmap;
58 QPixmap m_irdaDiscoveryOnPixmap; 57 QPixmap m_irdaDiscoveryOnPixmap;
59 QPixmap m_receiveActivePixmap; 58 QPixmap m_receiveActivePixmap;
60 59
61 bool m_irda_active; 60 bool m_irda_active;
@@ -66,12 +65,11 @@ private:
66 QPopupMenu *m_popup; 65 QPopupMenu *m_popup;
67 66
68 int m_sockfd; 67 int m_sockfd;
69 68
70 QMap <QString, QString> m_devices; 69 QMap <QString, QString> m_devices;
71 70
72 bool m_wasOn; // if IrDa was enabled 71 bool m_wasOn; ///< If IrDA was enabled this is true
73 bool m_wasDiscover; 72 bool m_wasDiscover;
74}; 73};
75
76
77#endif // __OPIE_IRDA_APPLET_H__ 74#endif // __OPIE_IRDA_APPLET_H__
75