author | harlekin <harlekin> | 2002-06-23 22:05:53 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2002-06-23 22:05:53 (UTC) |
commit | 5762bcc7430739798ed9e7869066255cb1998f1a (patch) (unidiff) | |
tree | e55409db7b3519684087918066129f9a5dd6741f /core/applets/irdaapplet/irda.cpp | |
parent | 64f2b33735d6df230820af5aa0f47d8db29e6e35 (diff) | |
download | opie-5762bcc7430739798ed9e7869066255cb1998f1a.zip opie-5762bcc7430739798ed9e7869066255cb1998f1a.tar.gz opie-5762bcc7430739798ed9e7869066255cb1998f1a.tar.bz2 |
changes by Bruno David Rodrigues <bruno.rodrigues@litux.org>, like multiple device detection
Diffstat (limited to 'core/applets/irdaapplet/irda.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/applets/irdaapplet/irda.cpp | 161 |
1 files changed, 122 insertions, 39 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 | |||
@@ -47,63 +47,94 @@ | |||
47 | 47 | ||
48 | #include <net/if.h> | 48 | #include <net/if.h> |
49 | #include <netinet/in.h> | 49 | #include <netinet/in.h> |
50 | #include <sys/types.h> | 50 | #include <sys/types.h> |
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 | ||
57 | IrdaApplet::IrdaApplet( QWidget *parent, const char *name ) | 57 | IrdaApplet::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 | ||
71 | IrdaApplet::~IrdaApplet() { | 73 | IrdaApplet::~IrdaApplet() { |
72 | close(sockfd); | 74 | close(sockfd); |
75 | if( popupMenu ) { delete popupMenu; } | ||
76 | } | ||
77 | |||
78 | void 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 | |||
99 | void IrdaApplet::popupTimeout() { | ||
100 | popupMenu->hide(); | ||
73 | } | 101 | } |
74 | 102 | ||
75 | int IrdaApplet::checkIrdaStatus() { | 103 | int 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)) |
81 | return -1; | 109 | return -1; |
82 | 110 | ||
83 | return (ifr.ifr_flags & IFF_UP)?1:0; | 111 | return (ifr.ifr_flags & IFF_UP)?1:0; |
84 | } | 112 | } |
85 | 113 | ||
86 | int IrdaApplet::setIrdaStatus(int c) { | 114 | int 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 | ||
105 | int IrdaApplet::checkIrdaDiscoveryStatus() { | 136 | int IrdaApplet::checkIrdaDiscoveryStatus() { |
106 | 137 | ||
107 | QFile discovery("/proc/sys/net/irda/discovery"); | 138 | QFile discovery("/proc/sys/net/irda/discovery"); |
108 | 139 | ||
109 | QString streamIn = "0"; | 140 | QString streamIn = "0"; |
@@ -125,160 +156,212 @@ int IrdaApplet::setIrdaDiscoveryStatus(int d) { | |||
125 | discovery.open( IO_WriteOnly|IO_Raw ); | 156 | discovery.open( IO_WriteOnly|IO_Raw ); |
126 | 157 | ||
127 | if (d) | 158 | if (d) |
128 | discovery.putch('1'); | 159 | discovery.putch('1'); |
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 | ||
168 | int 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 | ||
138 | void IrdaApplet::showDiscovered() { | 185 | void 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 | ||
159 | void IrdaApplet::mousePressEvent( QMouseEvent *) { | 243 | void 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); |
213 | timerEvent(NULL); | 299 | timerEvent(NULL); |
214 | break; | 300 | break; |
215 | case 2: | 301 | case 2: |
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 |
246 | } | 324 | } |
247 | 325 | ||
248 | void IrdaApplet::timerEvent( QTimerEvent * ) { | 326 | void IrdaApplet::timerEvent( QTimerEvent * ) { |
249 | int oldactive = irdaactive; | 327 | int oldactive = irdaactive; |
250 | int olddiscovery = irdaDiscoveryActive; | 328 | int olddiscovery = irdaDiscoveryActive; |
251 | bool receiveUpdate = false; | 329 | bool receiveUpdate = false; |
252 | 330 | ||
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 | ||
267 | void IrdaApplet::paintEvent( QPaintEvent* ) { | 350 | void 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 | } |