-rw-r--r-- | core/applets/irdaapplet/irda.cpp | 134 | ||||
-rw-r--r-- | core/applets/irdaapplet/irda.h | 6 |
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 | |||
@@ -41,30 +41,38 @@ | |||
41 | 41 | ||
42 | //=========================================================================== | 42 | //=========================================================================== |
43 | 43 | ||
44 | IrdaApplet::IrdaApplet ( QWidget *parent, const char *name ) | 44 | IrdaApplet::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 | ||
70 | int IrdaApplet::position() | 78 | int IrdaApplet::position() |
@@ -94,13 +102,22 @@ void IrdaApplet::popup ( QString message, QString icon ) | |||
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 | } |
@@ -115,7 +132,7 @@ bool IrdaApplet::checkIrdaStatus ( ) | |||
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 ); |
@@ -126,19 +143,18 @@ bool IrdaApplet::setIrdaStatus ( bool b ) | |||
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; |
@@ -196,19 +212,27 @@ void IrdaApplet::showDiscovered ( ) | |||
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; |
@@ -223,7 +247,8 @@ void IrdaApplet::showDiscovered ( ) | |||
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 )); |
@@ -231,12 +256,13 @@ void IrdaApplet::showDiscovered ( ) | |||
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 ) { |
@@ -259,8 +285,11 @@ void IrdaApplet::mousePressEvent ( QMouseEvent * ) | |||
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 | } |
@@ -268,17 +297,24 @@ void IrdaApplet::mousePressEvent ( QMouseEvent * ) | |||
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: |
@@ -315,9 +351,12 @@ void IrdaApplet::timerEvent ( QTimerEvent * ) | |||
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 | ||
322 | void IrdaApplet::paintEvent ( QPaintEvent * ) | 361 | void IrdaApplet::paintEvent ( QPaintEvent * ) |
323 | { | 362 | { |
@@ -331,12 +370,12 @@ void IrdaApplet::paintEvent ( QPaintEvent * ) | |||
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 | */ |
341 | void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) { | 380 | void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) { |
342 | if ( str == "enableIrda()") { | 381 | if ( str == "enableIrda()") { |
@@ -368,3 +407,4 @@ void IrdaApplet::slotMessage( const QCString& str, const QByteArray& ) { | |||
368 | } | 407 | } |
369 | 408 | ||
370 | EXPORT_OPIE_APPLET_v1( IrdaApplet ) | 409 | EXPORT_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 | |||
@@ -52,7 +52,6 @@ private: | |||
52 | 52 | ||
53 | void showDiscovered(); | 53 | void showDiscovered(); |
54 | 54 | ||
55 | private: | ||
56 | QPixmap m_irdaOnPixmap; | 55 | QPixmap m_irdaOnPixmap; |
57 | QPixmap m_irdaOffPixmap; | 56 | QPixmap m_irdaOffPixmap; |
58 | QPixmap m_irdaDiscoveryOnPixmap; | 57 | QPixmap m_irdaDiscoveryOnPixmap; |
@@ -69,9 +68,8 @@ private: | |||
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 | |||