summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/applets/irdaapplet/irda.cpp161
-rw-r--r--core/applets/irdaapplet/irda.h15
2 files changed, 131 insertions, 45 deletions
diff --git a/core/applets/irdaapplet/irda.cpp b/core/applets/irdaapplet/irda.cpp
index d986df4..c0f11c0 100644
--- a/core/applets/irdaapplet/irda.cpp
+++ b/core/applets/irdaapplet/irda.cpp
@@ -51,30 +51,58 @@
51#include <sys/socket.h> 51#include <sys/socket.h>
52#include <sys/ioctl.h> 52#include <sys/ioctl.h>
53 53
54 54
55//=========================================================================== 55//===========================================================================
56 56
57IrdaApplet::IrdaApplet( QWidget *parent, const char *name ) 57IrdaApplet::IrdaApplet( QWidget *parent, const char *name )
58 : QWidget( parent, name ) { 58 : QWidget( parent, name ) {
59 setFixedHeight( 18 ); 59 setFixedHeight( 14 );
60 setFixedWidth( 14 ); 60 setFixedWidth( 14 );
61 sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 61 sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
62 irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" ); 62 irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" );
63 irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" ); 63 irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" );
64 irdaDiscoveryOnPixmap = Resource::loadPixmap( "irdaapplet/magglass" ); 64 irdaDiscoveryOnPixmap = Resource::loadPixmap( "irdaapplet/magglass" );
65 receiveActivePixmap = Resource::loadPixmap("irdaapplet/receive"); 65 receiveActivePixmap = Resource::loadPixmap("irdaapplet/receive");
66 receiveActive = false; 66 receiveActive = false;
67 startTimer(5000); 67 startTimer(5000);
68 timerEvent(NULL); 68 timerEvent(NULL);
69 popupMenu = 0;
70 devicesAvailable.setAutoDelete(TRUE);
69} 71}
70 72
71IrdaApplet::~IrdaApplet() { 73IrdaApplet::~IrdaApplet() {
72 close(sockfd); 74 close(sockfd);
75 if( popupMenu ) { delete popupMenu; }
76}
77
78void IrdaApplet::popup(QString message, QString icon="") {
79 if ( ! popupMenu ) {
80 popupMenu = new QPopupMenu();
81 }
82 popupMenu->clear();
83 if( icon == "" ) {
84 popupMenu->insertItem( message, 0 );
85 } else {
86 popupMenu->insertItem( QIconSet ( Resource::loadPixmap ( icon )),
87 message, 0 );
88 }
89
90 QPoint p = mapToGlobal ( QPoint ( 0, 0 ));
91 QSize s = popupMenu->sizeHint ( );
92 popupMenu->popup( QPoint (
93 p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ),
94 p. y ( ) - s. height ( ) ), 0);
95
96 QTimer::singleShot( 2000, this, SLOT(popupTimeout()) );
97}
98
99void IrdaApplet::popupTimeout() {
100 popupMenu->hide();
73} 101}
74 102
75int IrdaApplet::checkIrdaStatus() { 103int IrdaApplet::checkIrdaStatus() {
76 struct ifreq ifr; 104 struct ifreq ifr;
77 105
78 strcpy(ifr.ifr_name, "irda0"); 106 strcpy(ifr.ifr_name, "irda0");
79 107
80 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr)) 108 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
@@ -86,20 +114,23 @@ int IrdaApplet::checkIrdaStatus() {
86int IrdaApplet::setIrdaStatus(int c) { 114int IrdaApplet::setIrdaStatus(int c) {
87 struct ifreq ifr; 115 struct ifreq ifr;
88 116
89 strcpy(ifr.ifr_name, "irda0"); 117 strcpy(ifr.ifr_name, "irda0");
90 118
91 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr)) 119 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
92 return -1; 120 return -1;
93 121
94 if (c) 122 if (c) {
95 ifr.ifr_flags |= IFF_UP; 123 ifr.ifr_flags |= IFF_UP;
96 else 124 } else {
125 setIrdaDiscoveryStatus(0);
126 setIrdaReceiveStatus(0);
97 ifr.ifr_flags &= ~IFF_UP; 127 ifr.ifr_flags &= ~IFF_UP;
128 }
98 129
99 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr)) 130 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr))
100 return -1; 131 return -1;
101 132
102 return 0; 133 return 0;
103} 134}
104 135
105int IrdaApplet::checkIrdaDiscoveryStatus() { 136int IrdaApplet::checkIrdaDiscoveryStatus() {
@@ -129,84 +160,139 @@ int IrdaApplet::setIrdaDiscoveryStatus(int d) {
129 else 160 else
130 discovery.putch('0'); 161 discovery.putch('0');
131 162
132 discovery.close(); 163 discovery.close();
133 164
134 return 0; 165 return 0;
135} 166}
136 167
168int IrdaApplet::setIrdaReceiveStatus(int d) {
169 if(d) {
170 qWarning("Enable receive" );
171 QCopEnvelope e("QPE/Obex", "receive(int)" );
172 e << 1;
173 receiveActive = true;
174 receiveStateChanged = true;
175 } else {
176 qWarning("Disable receive" );
177 QCopEnvelope e("QPE/Obex", "receive(int)" );
178 e << 0;
179 receiveActive = false;
180 receiveStateChanged = true;
181 }
182 return 0;
183}
137 184
138void IrdaApplet::showDiscovered() { 185void IrdaApplet::showDiscovered() {
139 QFile discovery("/proc/net/irda/discovery"); 186 QFile discovery("/proc/net/irda/discovery");
140 187
141 if (discovery.open(IO_ReadOnly) ) { 188 if (discovery.open(IO_ReadOnly) ) {
189 int qcopsend = FALSE;
190
191 QString discoveredDevice;
192 QString deviceAddr;
142 QStringList list; 193 QStringList list;
143 // since it is /proc we _must_ use QTextStream 194 // since it is /proc we _must_ use QTextStream
144 QTextStream stream ( &discovery); 195 QTextStream stream ( &discovery);
145 QString streamIn; 196 QString streamIn;
146 streamIn = stream.read(); 197 streamIn = stream.read();
147 list = QStringList::split("\n", streamIn); 198 list = QStringList::split("\n", streamIn);
148 199
200 QDictIterator<QString> it( devicesAvailable );
201 while ( it.current() ) {
202 devicesAvailable.replace( it.currentKey(), new QString("+++" + *devicesAvailable[it.currentKey()]) );
203 //qDebug("IrdaMon: " + it.currentKey());
204 //qDebug(" =" + *devicesAvailable[it.currentKey()] );
205 ++it;
206 }
207
149 for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) { 208 for(QStringList::Iterator line=list.begin(); line!=list.end(); line++) {
209 qDebug( (*line) );
150 if( (*line).startsWith("nickname:") ){ 210 if( (*line).startsWith("nickname:") ){
151 discoveredDevice = (*line).mid(((*line).find(':'))+1,(*line).find(',')-(*line).find(':')-1); 211 discoveredDevice = (*line).mid(((*line).find(':'))+2,(*line).find(',')-(*line).find(':')-2);
152 qDebug(discoveredDevice); 212 deviceAddr = (*line).mid( (*line).find( "daddr:" )+9, 8 );
213
214 qDebug(discoveredDevice + "(" + deviceAddr + ")");
215
216 if( ! devicesAvailable.find(deviceAddr) ) {
217 popup( tr("Found:") + " " + discoveredDevice );
218 qcopsend = TRUE;
219 }
220 devicesAvailable.replace( deviceAddr, new QString(discoveredDevice) );
221 //qDebug("IrdaMon: " + deviceAddr + "=" + *devicesAvailable[deviceAddr] );
153 } 222 }
154 } 223 }
155 }
156 224
225 it.toFirst();
226 while ( it.current() ) {
227 qDebug("IrdaMon: delete " + it.currentKey() + "=" + *devicesAvailable[it.currentKey()] + "?");
228 if ( (*it.current()).left(3) == "+++" ) {
229 popup( tr("Lost:") + " " + (*devicesAvailable[it.currentKey()]).mid(3) );
230 devicesAvailable.remove( it.currentKey() );
231 qDebug("IrdaMon: delete " + it.currentKey() + "!");
232 qcopsend = TRUE;
233 }
234 ++it;
235 }
236 /* XXX if( qcopsend ) {
237 QCopEnvelope e("QPE/Network", "irdaSend(bool)" );
238 e << (devicesAvailable.count() > 0) ;
239 } */
240 }
157} 241}
158 242
159void IrdaApplet::mousePressEvent( QMouseEvent *) { 243void IrdaApplet::mousePressEvent( QMouseEvent *) {
160 QPopupMenu *menu = new QPopupMenu(); 244 QPopupMenu *menu = new QPopupMenu();
161 QString cmd; 245 QString cmd;
162 int ret=0; 246 int ret=0;
163 showDiscovered();
164 247
165 /* Refresh active state */ 248 /* Refresh active state */
166 timerEvent(NULL); 249 timerEvent(NULL);
167 250
168 //menu->insertItem( tr("More..."), 4 ); 251 //menu->insertItem( tr("More..."), 4 );
169 252
170 menu->insertItem( tr("Discovered Device:"), 9); 253 if (irdaactive && devicesAvailable.count() > 0) {
254 menu->insertItem( tr("Discovered Device:"), 9);
171 255
172 if ( !discoveredDevice.isEmpty() ) { 256 QDictIterator<QString> it( devicesAvailable );
173 menu->insertItem( discoveredDevice ,7 ); 257 while ( it.current() ) {
174 } else { 258 menu->insertItem( *devicesAvailable[it.currentKey()]);
175 menu->insertItem( tr("None"), 8); 259 ++it;
260 }
261 menu->insertSeparator();
176 } 262 }
177 263
178 menu->insertSeparator();
179
180 if (irdaactive) { 264 if (irdaactive) {
181 menu->insertItem( tr("Disable IrDA"), 0 ); 265 menu->insertItem( tr("Disable IrDA"), 0 );
182 } else { 266 } else {
183 menu->insertItem( tr("Enable IrDA"), 1 ); 267 menu->insertItem( tr("Enable IrDA"), 1 );
184 } 268 }
185 269
186 if (irdaDiscoveryActive) { 270 if (irdaactive) {
187 menu->insertItem( tr("Disable Discovery"), 2 ); 271 if (irdaDiscoveryActive) {
188 } else { 272 menu->insertItem( tr("Disable Discovery"), 2 );
189 menu->insertItem( tr("Enable Discovery"), 3 ); 273 } else {
190 } 274 menu->insertItem( tr("Enable Discovery"), 3 );
191 275 }
192 if( receiveActive ){ 276
193 menu->insertItem( tr("Disable Receive"), 5 ); 277 if( receiveActive ){
194 } else { 278 menu->insertItem( tr("Disable Receive"), 4 );
195 menu->insertItem( tr("Enable Receive"), 4 ); 279 } else {
280 menu->insertItem( tr("Enable Receive"), 5 );
281 }
196 } 282 }
197 283
198 QPoint p = mapToGlobal ( QPoint ( 0, 0 )); 284 QPoint p = mapToGlobal ( QPoint ( 0, 0 ));
199 QSize s = menu-> sizeHint ( ); 285 QSize s = menu-> sizeHint ( );
200 ret = menu->exec( QPoint ( 286 ret = menu->exec( QPoint (
201 p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), 287 p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ),
202 p. y ( ) - s. height ( ) ), 0); 288 p. y ( ) - s. height ( ) ), 0);
203 289
204 qDebug("ret was %d\n", ret); 290 // qDebug("ret was %d\n", ret);
205 291
206 switch(ret) { 292 switch(ret) {
207 case 0: 293 case 0:
208 setIrdaStatus(0); 294 setIrdaStatus(0);
209 timerEvent(NULL); 295 timerEvent(NULL);
210 break; 296 break;
211 case 1: 297 case 1:
212 setIrdaStatus(1); 298 setIrdaStatus(1);
@@ -216,30 +302,22 @@ void IrdaApplet::mousePressEvent( QMouseEvent *) {
216 setIrdaDiscoveryStatus(0); 302 setIrdaDiscoveryStatus(0);
217 timerEvent(NULL); 303 timerEvent(NULL);
218 break; 304 break;
219 case 3: 305 case 3:
220 setIrdaDiscoveryStatus(1); 306 setIrdaDiscoveryStatus(1);
221 timerEvent(NULL); // NULL is undefined in c++ use 0 or 0l 307 timerEvent(NULL); // NULL is undefined in c++ use 0 or 0l
222 break; 308 break;
223 case 4: { // enable receive 309 case 4: { // enable receive
224 qWarning("Enable receive" ); 310 setIrdaReceiveStatus(0);
225 QCopEnvelope e("QPE/Obex", "receive(int)" );
226 e << 1;
227 receiveActive = true;
228 receiveStateChanged = true;
229 timerEvent(NULL); 311 timerEvent(NULL);
230 break; 312 break;
231 } 313 }
232 case 5: { // disable receive 314 case 5: { // disable receive
233 qWarning("Disable receive" ); 315 setIrdaReceiveStatus(1);
234 QCopEnvelope e("QPE/Obex", "receive(int)" );
235 e << 0;
236 receiveActive = false;
237 receiveStateChanged = true;
238 timerEvent(NULL); 316 timerEvent(NULL);
239 break; 317 break;
240 } 318 }
241 case 6: 319 case 6:
242 qDebug("FIXME: Bring up pretty menu...\n"); 320 qDebug("FIXME: Bring up pretty menu...\n");
243 // With table of currently-detected devices. 321 // With table of currently-detected devices.
244 } 322 }
245 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 323 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
@@ -253,32 +331,37 @@ void IrdaApplet::timerEvent( QTimerEvent * ) {
253 if (receiveStateChanged) { 331 if (receiveStateChanged) {
254 receiveUpdate = true; 332 receiveUpdate = true;
255 receiveStateChanged = false; 333 receiveStateChanged = false;
256 } 334 }
257 335
258 irdaactive = checkIrdaStatus(); 336 irdaactive = checkIrdaStatus();
259 irdaDiscoveryActive = checkIrdaDiscoveryStatus(); 337 irdaDiscoveryActive = checkIrdaDiscoveryStatus();
260 338
339
340 if (irdaDiscoveryActive) {
341 showDiscovered();
342 }
343
261 if ((irdaactive != oldactive) || (irdaDiscoveryActive != olddiscovery) || receiveUpdate ) { 344 if ((irdaactive != oldactive) || (irdaDiscoveryActive != olddiscovery) || receiveUpdate ) {
262 paintEvent(NULL); 345 paintEvent(NULL);
263 } 346 }
264 347
265} 348}
266 349
267void IrdaApplet::paintEvent( QPaintEvent* ) { 350void IrdaApplet::paintEvent( QPaintEvent* ) {
268 QPainter p(this); 351 QPainter p(this);
269 qDebug("paint irda pixmap"); 352 qDebug("paint irda pixmap");
270 353
271 p.eraseRect ( 0, 0, this->width(), this->height() ); 354 p.eraseRect ( 0, 0, this->width(), this->height() );
272 if (irdaactive > 0) { 355 if (irdaactive > 0) {
273 p.drawPixmap( 0, 1, irdaOnPixmap ); 356 p.drawPixmap( 0, 0, irdaOnPixmap );
274 } else { 357 } else {
275 p.drawPixmap( 0, 1, irdaOffPixmap ); 358 p.drawPixmap( 0, 0, irdaOffPixmap );
276 } 359 }
277 360
278 if (irdaDiscoveryActive > 0) { 361 if (irdaDiscoveryActive > 0) {
279 p.drawPixmap( 0, 1, irdaDiscoveryOnPixmap ); 362 p.drawPixmap( 0, 0, irdaDiscoveryOnPixmap );
280 } 363 }
281 if (receiveActive) { 364 if (receiveActive) {
282 p.drawPixmap( 0, 1, receiveActivePixmap); 365 p.drawPixmap( 0, 0, receiveActivePixmap);
283 } 366 }
284} 367}
diff --git a/core/applets/irdaapplet/irda.h b/core/applets/irdaapplet/irda.h
index 5822afe..7b37847 100644
--- a/core/applets/irdaapplet/irda.h
+++ b/core/applets/irdaapplet/irda.h
@@ -18,17 +18,18 @@
18 18
19 19
20#include <qwidget.h> 20#include <qwidget.h>
21#include <qframe.h> 21#include <qframe.h>
22#include <qpixmap.h> 22#include <qpixmap.h>
23#include <qguardedptr.h> 23#include <qguardedptr.h>
24#include <qtimer.h> 24#include <qtimer.h>
25#include <qfile.h> 25#include <qfile.h>
26 26#include <qpopupmenu.h>
27#include <qdict.h>
27 28
28class IrdaApplet : public QWidget 29class IrdaApplet : public QWidget
29{ 30{
30 Q_OBJECT 31 Q_OBJECT
31public: 32public:
32 IrdaApplet( QWidget *parent = 0, const char *name=0 ); 33 IrdaApplet( QWidget *parent = 0, const char *name=0 );
33 ~IrdaApplet(); 34 ~IrdaApplet();
34 35
@@ -37,29 +38,31 @@ protected:
37 38
38private: 39private:
39 void mousePressEvent( QMouseEvent * ); 40 void mousePressEvent( QMouseEvent * );
40 void paintEvent( QPaintEvent* ); 41 void paintEvent( QPaintEvent* );
41 int checkIrdaStatus(); 42 int checkIrdaStatus();
42 int setIrdaStatus(int); 43 int setIrdaStatus(int);
43 int checkIrdaDiscoveryStatus(); 44 int checkIrdaDiscoveryStatus();
44 int setIrdaDiscoveryStatus(int); 45 int setIrdaDiscoveryStatus(int);
46 int setIrdaReceiveStatus(int);
45 void showDiscovered(); 47 void showDiscovered();
46 int sockfd; 48 int sockfd;
47 49
50private slots:
51 void popupTimeout();
52
48private: 53private:
49 QPixmap irdaOnPixmap; 54 QPixmap irdaOnPixmap;
50 QPixmap irdaOffPixmap; 55 QPixmap irdaOffPixmap;
51 QPixmap irdaDiscoveryOnPixmap; 56 QPixmap irdaDiscoveryOnPixmap;
52 QPixmap receiveActivePixmap; 57 QPixmap receiveActivePixmap;
53 QString discoveredDevice;
54 int irdaactive; // bool and bitfields later bool irdaactive :1 ; 58 int irdaactive; // bool and bitfields later bool irdaactive :1 ;
55 int irdaDiscoveryActive; 59 int irdaDiscoveryActive;
56 bool receiveActive : 1; 60 bool receiveActive : 1;
57 bool receiveStateChanged; 61 bool receiveStateChanged;
58private slots: 62 QPopupMenu *popupMenu;
59 63 void popup(QString message, QString icon="");
60 64 QDict<QString> devicesAvailable;
61}; 65};
62 66
63 67
64#endif // __SCREENSHOT_APPLET_H__ 68#endif // __SCREENSHOT_APPLET_H__
65