author | sandman <sandman> | 2002-06-27 12:56:22 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-06-27 12:56:22 (UTC) |
commit | 56d22ac17fdc194a20fd0deba673943f85e940ea (patch) (side-by-side diff) | |
tree | 564c333bfa77e1dbae9a553e2c208eecefdeea1e | |
parent | 88b644d1c106008fc15f6a3c496e40296e9029d4 (diff) | |
download | opie-56d22ac17fdc194a20fd0deba673943f85e940ea.zip opie-56d22ac17fdc194a20fd0deba673943f85e940ea.tar.gz opie-56d22ac17fdc194a20fd0deba673943f85e940ea.tar.bz2 |
Fix an endless loop error when device is lost.
-rw-r--r-- | core/applets/irdaapplet/irda.cpp | 376 | ||||
-rw-r--r-- | core/applets/irdaapplet/irda.h | 73 |
2 files changed, 197 insertions, 252 deletions
diff --git a/core/applets/irdaapplet/irda.cpp b/core/applets/irdaapplet/irda.cpp index 42bed5e..c3ddff2 100644 --- a/core/applets/irdaapplet/irda.cpp +++ b/core/applets/irdaapplet/irda.cpp @@ -2,4 +2,4 @@ ** Copyright (C) 2002 David Woodhouse <dwmw2@infradead.org> -** Heavily based on volumeapplet, (C) 2002 L.J. Potter ljp@llornkcor.com -** All rights reserved. +** Max Reiss <harlekin@handhelds.org> [trivial stuff] +** Robert Griebl <sandman@handhelds.org> ** @@ -15,29 +15,9 @@ -#include "irda.h" -#include <qapplication.h> -#include <stdlib.h> - - -#include <qpe/resource.h> #include <qpe/qpeapplication.h> -#include <qpe/timestring.h> #include <qpe/resource.h> -#include <qpe/config.h> -#include <qpe/applnk.h> -#include <qpe/config.h> #include <qpe/ir.h> #include <qpe/qcopenvelope_qws.h> +#include <qpe/sound.h> -#include <qdir.h> -#include <qfileinfo.h> -#include <qpoint.h> -#include <qpushbutton.h> #include <qpainter.h> -#include <qcombobox.h> -#include <qspinbox.h> -#include <qslider.h> -#include <qlayout.h> -#include <qframe.h> -#include <qpixmap.h> -#include <qstring.h> #include <qfile.h> @@ -46,4 +26,4 @@ #include <qpopupmenu.h> -#include <qsound.h> +#include <unistd.h> #include <net/if.h> @@ -54,2 +34,3 @@ +#include "irda.h" @@ -58,16 +39,23 @@ IrdaApplet::IrdaApplet( QWidget *parent, const char *name ) - : QWidget( parent, name ) { - setFixedHeight( 14 ); + : QWidget ( parent, name ) +{ + setFixedHeight ( 18 ); setFixedWidth( 14 ); - sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" ); - irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" ); - irdaDiscoveryOnPixmap = Resource::loadPixmap( "irdaapplet/magglass" ); - receiveActivePixmap = Resource::loadPixmap("irdaapplet/receive"); - receiveActive = false; - popupMenu = 0; - devicesAvailable.setAutoDelete(TRUE); + + m_sockfd = ::socket ( PF_INET, SOCK_DGRAM, IPPROTO_IP ); + + m_irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" ); + m_irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" ); + m_irdaDiscoveryOnPixmap = Resource::loadPixmap( "irdaapplet/magglass" ); + m_receiveActivePixmap = Resource::loadPixmap( "irdaapplet/receive" ); + + m_irda_active = false; + m_irda_discovery_active = false; + m_receive_active = false; + m_receive_state_changed = false; + m_popup = 0; } -void IrdaApplet::show() { +void IrdaApplet::show() +{ QWidget::show(); @@ -76,24 +64,24 @@ void IrdaApplet::show() { -IrdaApplet::~IrdaApplet() { - close(sockfd); - if( popupMenu ) { delete popupMenu; } +IrdaApplet::~IrdaApplet() +{ + if ( m_sockfd >= 0 ) + ::close ( m_sockfd ); } -void IrdaApplet::popup(QString message, QString icon="") { - if ( ! popupMenu ) { - popupMenu = new QPopupMenu(); - } - popupMenu->clear(); - if( icon == "" ) { - popupMenu->insertItem( message, 0 ); - } else { - popupMenu->insertItem( QIconSet ( Resource::loadPixmap ( icon )), - message, 0 ); - } +void IrdaApplet::popup ( QString message, QString icon ) +{ + if ( !m_popup ) + m_popup = new QPopupMenu ( this ); + + m_popup-> clear ( ); + + if ( icon. isEmpty ( )) + m_popup-> insertItem ( message, 0 ); + else + m_popup-> insertItem ( QIconSet ( Resource::loadPixmap ( icon )), message, 0 ); QPoint p = mapToGlobal ( QPoint ( 0, 0 )); - QSize s = popupMenu->sizeHint ( ); - popupMenu->popup( QPoint ( - p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), - p. y ( ) - s. height ( ) ), 0); + QSize s = m_popup-> sizeHint ( ); + m_popup-> popup ( QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), + p. y ( ) - s. height ( ))); @@ -102,28 +90,30 @@ void IrdaApplet::popup(QString message, QString icon="") { -void IrdaApplet::popupTimeout() { - popupMenu->hide(); +void IrdaApplet::popupTimeout ( ) +{ + m_popup-> hide ( ); } -int IrdaApplet::checkIrdaStatus() { +bool IrdaApplet::checkIrdaStatus ( ) +{ struct ifreq ifr; - strcpy(ifr.ifr_name, "irda0"); - if (ioctl(sockfd, SIOCGIFFLAGS, &ifr)) - return -1; + if ( ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 ) + return false; - return (ifr.ifr_flags & IFF_UP)?1:0; + return ( ifr. ifr_flags & IFF_UP ); } -int IrdaApplet::setIrdaStatus(int c) { +bool IrdaApplet::setIrdaStatus ( bool b ) +{ struct ifreq ifr; - strcpy(ifr.ifr_name, "irda0"); - if (ioctl(sockfd, SIOCGIFFLAGS, &ifr)) - return -1; + if ( ::ioctl ( m_sockfd, SIOCGIFFLAGS, &ifr ) < 0 ) + return false; - if (c) { + if ( b ) { ifr.ifr_flags |= IFF_UP; - } else { + } + else { setIrdaDiscoveryStatus(0); @@ -133,10 +123,10 @@ int IrdaApplet::setIrdaStatus(int c) { - if (ioctl(sockfd, SIOCSIFFLAGS, &ifr)) - return -1; + if ( ::ioctl ( m_sockfd, SIOCSIFFLAGS, &ifr ) < 0 ) + return false; - return 0; + return true; } -int IrdaApplet::checkIrdaDiscoveryStatus() { - +bool IrdaApplet::checkIrdaDiscoveryStatus ( ) +{ QFile discovery("/proc/sys/net/irda/discovery"); @@ -150,5 +140,3 @@ int IrdaApplet::checkIrdaDiscoveryStatus() { - discovery.close(); - - return( streamIn.toInt() ); + return streamIn. toInt ( ) > 0; } @@ -156,35 +144,31 @@ int IrdaApplet::checkIrdaDiscoveryStatus() { -int IrdaApplet::setIrdaDiscoveryStatus(int d) { +bool IrdaApplet::setIrdaDiscoveryStatus ( bool d ) +{ QFile discovery("/proc/sys/net/irda/discovery"); - discovery.open( IO_WriteOnly|IO_Raw ); + if ( discovery. open ( IO_WriteOnly | IO_Raw )) { + discovery.putch ( d ? '1' : '0' ); + return true; + } + return false; +} - if (d) - discovery.putch('1'); - else - discovery.putch('0'); - discovery.close(); +bool IrdaApplet::setIrdaReceiveStatus ( bool d ) +{ + QCopEnvelope e ( "QPE/Obex", "receive(int)" ); + e << ( d ? 1 : 0 ); - return 0; -} + m_receive_active = d; + m_receive_state_changed = true; -int IrdaApplet::setIrdaReceiveStatus(int d) { - if(d) { - qWarning("Enable receive" ); - QCopEnvelope e("QPE/Obex", "receive(int)" ); - e << 1; - receiveActive = true; - receiveStateChanged = true; - } else { - qWarning("Disable receive" ); - QCopEnvelope e("QPE/Obex", "receive(int)" ); - e << 0; - receiveActive = false; - receiveStateChanged = true; - } - return 0; + return true; } -void IrdaApplet::showDiscovered() { + +void IrdaApplet::showDiscovered ( ) +{ + static Sound snd_found ( "irdaapplet/irdaon" ); + static Sound snd_lost ( "irdaapplet/irdaoff" ); + QFile discovery("/proc/net/irda/discovery"); @@ -192,3 +176,3 @@ void IrdaApplet::showDiscovered() { if (discovery.open(IO_ReadOnly) ) { - int qcopsend = FALSE; + bool qcopsend = false; @@ -196,22 +180,17 @@ void IrdaApplet::showDiscovered() { QString deviceAddr; - QStringList list; + // since it is /proc we _must_ use QTextStream - QTextStream stream ( &discovery); - QString streamIn; - streamIn = stream.read(); - list = QStringList::split("\n", streamIn); - - QDictIterator<QString> it( devicesAvailable ); - while ( it.current() ) { - devicesAvailable.replace( it.currentKey(), new QString("+++" + *devicesAvailable[it.currentKey()]) ); - //qDebug("IrdaMon: " + it.currentKey()); - //qDebug(" =" + *devicesAvailable[it.currentKey()] ); - ++it; - } + QStringList list = QStringList::split ( "\n", QTextStream ( &discovery ). read ( )); + + QMap <QString, QString>::Iterator it; + + for ( it = m_devices. begin ( ); it != m_devices. end ( ); ++it ) + it. data ( ). prepend ( "+++" ); + + for ( QStringList::Iterator lit = list. begin ( ); lit != list. end ( ); ++lit ) { + const QString &line = *lit; - for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) { - // qDebug( (*line) ); - if( (*line).startsWith("nickname:") ){ - discoveredDevice = (*line).mid(((*line).find(':'))+2,(*line).find(',')-(*line).find(':')-2); - deviceAddr = (*line).mid( (*line).find( "daddr:" )+9, 8 ); + if ( line. startsWith ( "nickname:" )) { + discoveredDevice = line. mid ( line. find ( ':' ) + 2, line. find ( ',' ) - line. find ( ':' ) - 2 ); + deviceAddr = line. mid ( line. find ( "daddr:" ) + 9, 8 ); @@ -219,9 +198,8 @@ void IrdaApplet::showDiscovered() { - if( ! devicesAvailable.find(deviceAddr) ) { + if ( !m_devices. contains ( deviceAddr )) { popup( tr("Found:") + " " + discoveredDevice ); - QSound::play(Resource::findSound("irdaapplet/irdaon")); - qcopsend = TRUE; + snd_found. play ( ); + qcopsend = true; } - devicesAvailable.replace( deviceAddr, new QString(discoveredDevice) ); - // qDebug("IrdaMon: " + deviceAddr + "=" + *devicesAvailable[deviceAddr] ); + m_devices. replace ( deviceAddr, discoveredDevice ); } @@ -229,18 +207,23 @@ void IrdaApplet::showDiscovered() { - it.toFirst(); - while ( it.current() ) { + for ( it = m_devices. begin ( ); it != m_devices. end ( ); ) { // qDebug("IrdaMon: delete " + it.currentKey() + "=" + *devicesAvailable[it.currentKey()] + "?"); - if ( (*it.current()).left(3) == "+++" ) { - popup( tr("Lost:") + " " + (*devicesAvailable[it.currentKey()]).mid(3) ); - QSound::play(Resource::findSound("irdaapplet/irdaoff")); - devicesAvailable.remove( it.currentKey() ); - // qDebug("IrdaMon: delete " + it.currentKey() + "!"); - qcopsend = TRUE; + + if ( it. data ( ). left ( 3 ) == "+++" ) { + popup ( tr( "Lost:" ) + " " + it. data ( ). mid ( 3 )); + snd_lost. play ( ); + + QMap <QString, QString>::Iterator tmp = it; + tmp++; + m_devices. remove ( it ); // in contrast to QValueListIterator this remove doesn't return the next Iterator + it = tmp; + + qcopsend = true; } - ++it; + else + it++; } - /* XXX if( qcopsend ) { + // XXX if( qcopsend ) { QCopEnvelope e("QPE/Network", "irdaSend(bool)" ); - e << (devicesAvailable.count() > 0) ; - } */ + e << ( m_devices. count ( ) > 0 ); + // } } @@ -248,9 +231,9 @@ void IrdaApplet::showDiscovered() { -void IrdaApplet::mousePressEvent( QMouseEvent *) { - QPopupMenu *menu = new QPopupMenu(); +void IrdaApplet::mousePressEvent ( QMouseEvent * ) +{ + QPopupMenu *menu = new QPopupMenu ( this ); QString cmd; - int ret=0; /* Refresh active state */ - timerEvent(NULL); + timerEvent ( 0 ); @@ -258,30 +241,16 @@ void IrdaApplet::mousePressEvent( QMouseEvent *) { - if (irdaactive && devicesAvailable.count() > 0) { + if ( m_irda_active && !m_devices. isEmpty ( )) { menu->insertItem( tr("Discovered Device:"), 9); - QDictIterator<QString> it( devicesAvailable ); - while ( it.current() ) { - menu->insertItem( *devicesAvailable[it.currentKey()]); - ++it; - } - menu->insertSeparator(); - } - if (irdaactive) { - menu->insertItem( tr("Disable IrDA"), 0 ); - } else { - menu->insertItem( tr("Enable IrDA"), 1 ); - } + for ( QMap<QString, QString>::Iterator it = m_devices. begin ( ); it != m_devices. end ( ); ++it ) + menu-> insertItem ( *it ); - if (irdaactive) { - if (irdaDiscoveryActive) { - menu->insertItem( tr("Disable Discovery"), 2 ); - } else { - menu->insertItem( tr("Enable Discovery"), 3 ); + menu-> insertSeparator ( ); } - if( receiveActive ){ - menu->insertItem( tr("Disable Receive"), 4 ); - } else { - menu->insertItem( tr("Enable Receive"), 5 ); - } + menu-> insertItem ( m_irda_active ? tr( "Disable IrDA" ) : tr( "Enable IrDA" ), 0 ); + + if ( m_irda_active ) { + menu-> insertItem ( m_irda_discovery_active ? tr( "Disable Discovery" ) : tr( "Enable Discovery" ), 1 ); + menu-> insertItem ( m_receive_active ? tr( "Disable Receive" ) : tr( "Enable Receive" ), 2 ); } @@ -290,80 +259,55 @@ void IrdaApplet::mousePressEvent( QMouseEvent *) { QSize s = menu-> sizeHint ( ); - ret = menu->exec( QPoint ( - p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), - p. y ( ) - s. height ( ) ), 0); - // qDebug("ret was %d\n", ret); + p = QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), p. y ( ) - s. height ( )); - switch(ret) { + switch ( menu-> exec ( p )) { case 0: - setIrdaStatus(0); - timerEvent(NULL); + setIrdaStatus ( !m_irda_active ); + timerEvent ( 0 ); break; case 1: - setIrdaStatus(1); - timerEvent(NULL); + setIrdaDiscoveryStatus ( !m_irda_discovery_active ); + timerEvent ( 0 ); break; case 2: - setIrdaDiscoveryStatus(0); - timerEvent(NULL); + setIrdaReceiveStatus ( !m_receive_active ); + timerEvent( 0 ); break; - case 3: - setIrdaDiscoveryStatus(1); - timerEvent(NULL); // NULL is undefined in c++ use 0 or 0l - break; - case 4: { // enable receive - setIrdaReceiveStatus(0); - timerEvent(NULL); - break; - } - case 5: { // disable receive - setIrdaReceiveStatus(1); - timerEvent(NULL); - break; - } } - delete menu; // Can somebody explain why use a QPopupMenu* and not QPopupMenu nor QAction. with out delete we will leak cause QPopupMenu doesn't have a parent in this case + + delete menu; } -void IrdaApplet::timerEvent( QTimerEvent * ) { - int oldactive = irdaactive; - int olddiscovery = irdaDiscoveryActive; +void IrdaApplet::timerEvent ( QTimerEvent * ) +{ + bool oldactive = m_irda_active; + bool olddiscovery = m_irda_discovery_active; bool receiveUpdate = false; - if (receiveStateChanged) { + if ( m_receive_state_changed ) { receiveUpdate = true; - receiveStateChanged = false; + m_receive_state_changed = false; } - irdaactive = checkIrdaStatus(); - irdaDiscoveryActive = checkIrdaDiscoveryStatus(); - + m_irda_active = checkIrdaStatus ( ); + m_irda_discovery_active = checkIrdaDiscoveryStatus ( ); - if (irdaDiscoveryActive) { + if ( m_irda_discovery_active ) showDiscovered(); - } - - if ((irdaactive != oldactive) || (irdaDiscoveryActive != olddiscovery) || receiveUpdate ) { - paintEvent(NULL); - } + if (( m_irda_active != oldactive ) || ( m_irda_discovery_active != olddiscovery ) || receiveUpdate ) + update ( ); } -void IrdaApplet::paintEvent( QPaintEvent* ) { +void IrdaApplet::paintEvent ( QPaintEvent * ) +{ QPainter p(this); - // qDebug("paint irda pixmap"); - p.eraseRect ( 0, 0, this->width(), this->height() ); - if (irdaactive > 0) { - p.drawPixmap( 0, 0, irdaOnPixmap ); - } else { - p.drawPixmap( 0, 0, irdaOffPixmap ); - } + p. drawPixmap ( 0, 1, m_irda_active ? m_irdaOnPixmap : m_irdaOffPixmap ); - if (irdaDiscoveryActive > 0) { - p.drawPixmap( 0, 0, irdaDiscoveryOnPixmap ); - } - if (receiveActive) { - p.drawPixmap( 0, 0, receiveActivePixmap); - } + if ( m_irda_discovery_active ) + p. drawPixmap( 0, 1, m_irdaDiscoveryOnPixmap ); + + if ( m_receive_active ) + p. drawPixmap( 0, 1, m_receiveActivePixmap ); } diff --git a/core/applets/irdaapplet/irda.h b/core/applets/irdaapplet/irda.h index e878946..1b5faa6 100644 --- a/core/applets/irdaapplet/irda.h +++ b/core/applets/irdaapplet/irda.h @@ -1,3 +1,4 @@ /********************************************************************** -** Copyright (C) 2002 L.J. Potter ljp@llornkcor.com +** Copyright (C) 2002 L.J. Potter ljp@llornkcor.com, +** Robert Griebl sandman@handhelds.org ** All rights reserved. @@ -14,15 +15,9 @@ -#ifndef SCREENSHOT_APPLET_H__ -#define SCREENSHOT_APPLET_H__ - - +#ifndef __OPIE_IRDA_APPLET_H__ +#define __OPIE_IRDA_APPLET_H__ #include <qwidget.h> -#include <qframe.h> #include <qpixmap.h> -#include <qguardedptr.h> -#include <qtimer.h> -#include <qfile.h> #include <qpopupmenu.h> -#include <qdict.h> +#include <qmap.h> @@ -31,2 +26,3 @@ class IrdaApplet : public QWidget Q_OBJECT + public: @@ -35,34 +31,39 @@ public: + virtual void show ( ); + protected: - void timerEvent(QTimerEvent *te ); + virtual void timerEvent ( QTimerEvent * ); + virtual void mousePressEvent ( QMouseEvent * ); + virtual void paintEvent ( QPaintEvent* ); + +private slots: + void popupTimeout ( ); private: - void mousePressEvent( QMouseEvent * ); - void paintEvent( QPaintEvent* ); - int checkIrdaStatus(); - int setIrdaStatus(int); - int checkIrdaDiscoveryStatus(); - int setIrdaDiscoveryStatus(int); - int setIrdaReceiveStatus(int); - void showDiscovered(); - int sockfd; + void popup( QString message, QString icon = QString::null ); -public slots: - void show(); + bool checkIrdaStatus ( ); + bool setIrdaStatus ( bool ); + bool checkIrdaDiscoveryStatus (); + bool setIrdaDiscoveryStatus ( bool ); + bool setIrdaReceiveStatus ( bool ); -private slots: - void popupTimeout(); + void showDiscovered(); private: - QPixmap irdaOnPixmap; - QPixmap irdaOffPixmap; - QPixmap irdaDiscoveryOnPixmap; - QPixmap receiveActivePixmap; - int irdaactive; // bool and bitfields later bool irdaactive :1 ; - int irdaDiscoveryActive; - bool receiveActive : 1; - bool receiveStateChanged; - QPopupMenu *popupMenu; - void popup(QString message, QString icon=""); - QDict<QString> devicesAvailable; + QPixmap m_irdaOnPixmap; + QPixmap m_irdaOffPixmap; + QPixmap m_irdaDiscoveryOnPixmap; + QPixmap m_receiveActivePixmap; + + bool m_irda_active; + bool m_irda_discovery_active; + bool m_receive_active; + bool m_receive_state_changed; + + QPopupMenu *m_popup; + + int m_sockfd; + + QMap <QString, QString> m_devices; }; @@ -70,2 +71,2 @@ private: -#endif // __SCREENSHOT_APPLET_H__ +#endif // __OPIE_IRDA_APPLET_H__ |