Fix crash on logout
[kdenetwork.git] / wifi / interface_wireless.cpp
blob929de7e3609758014456b5d54d1c3ea55b2a5526
1 /***************************************************************************
2 stuff.cpp - description
3 -------------------
4 begin : Sun May 6 2001
5 copyright : (C) 2001 by Stefan Winter
6 email : mail@stefan-winter.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "interface_wireless.h"
19 #include <QDir>
20 #include <QFile>
21 #include <QString>
22 #include <QStringList>
23 #include <QColor>
24 #include <QRegExp>
26 #ifndef WITHOUT_ARTS
27 #include <arts/artsflow.h>
28 #include <arts/connect.h>
29 #include <arts/iomanager.h>
30 #include <arts/referenceclean.h>
31 #endif // WITHOUT_ARTS
33 #include <iostream>
34 #include <string>
35 #include <klocale.h>
36 #include <kprocio.h>
37 #include <kdebug.h>
38 #include <arpa/inet.h>
40 Interface_wireless::Interface_wireless ( QStringList * ignoreInterfaces )
42 this->ignoreInterfaces = ignoreInterfaces;
43 has_frequency = false;
44 frequency = 0;
45 has_mode = false;
46 mode = 0;
47 has_key = 0;
48 key = "";
49 key_size = 0;
50 key_flags = 0;
51 essid = "";
52 access_point_address = "";
53 ip_addresses.clear ( );
54 bitrate = 0;
55 socket = -1;
56 has_range = false;
57 for ( int i = 0; i < MAX_HISTORY; i++ )
59 sigLevel[i] = -255;
60 noiseLevel[i] = -255;
61 qual[i] = -255;
62 valid[i] = false;
64 current = 0;
65 sigLevelMin = 32000;
66 noiseLevelMin = 32000;
67 sigLevelMax = -32000;
68 noiseLevelMax = -32000;
70 already_warned = false;
73 bool
74 Interface_wireless::get_device_freq ( long &freq )
76 if ( has_frequency )
78 freq = frequency;
79 return true;
81 else
83 return false;
87 bool
88 Interface_wireless::get_mode ( int &ext_mode )
90 if ( has_mode )
92 ext_mode = mode;
93 return true;
95 else
97 return false;
101 bool
102 Interface_wireless::get_key ( QString & ext_key, int &ext_size, int &ext_flags )
104 if ( has_key )
106 ext_key = key;
107 ext_size = key_size;
108 ext_flags = key_flags;
109 return true;
111 else
113 return false;
117 QString
118 Interface_wireless::get_essid ( )
120 return essid;
123 bool
124 Interface_wireless::get_has_txpower ( )
126 return has_txpower;
130 Interface_wireless::get_txpower_disabled ( )
132 return txpower_disabled;
135 bool
136 Interface_wireless::get_AP_info ( QString & mac, QString & /*ip */ )
138 mac = access_point_address;
139 return false;
142 QStringList
143 Interface_wireless::get_IP_info ( )
145 return ip_addresses;
148 signed long
149 Interface_wireless::get_bitrate ( )
151 return bitrate;
154 signed long
155 Interface_wireless::getMaxBitrate ( )
157 return maxBitrate;
160 QString
161 Interface_wireless::get_interface_name ( )
163 return interface_name;
166 bool
167 Interface_wireless::get_current_quality ( int &sig, int &noi, int &qua )
169 if ( valid[current] )
171 sig = sigLevel[current];
172 noi = noiseLevel[current];
173 qua = qual[current];
174 return true;
176 else
177 return false;
180 CARDSTATES
181 Interface_wireless::getDeviceState ( )
183 if ( !has_frequency || txpower_disabled )
185 return NO_CARD;
187 else if ( mode == 1 )
189 return AD_HOC;
191 else if ( qual[current] == 0 )
193 return NO_CONNECTION;
195 else if ( qual[current] > 0 )
197 return AP_CONNECT;
199 else
200 return NO_CARD;
203 #ifndef WITHOUT_ARTS
205 void
206 sine_wave ( double frequency )
208 using namespace Arts;
209 StdIOManager *limiter = new StdIOManager;
210 Dispatcher dispatcher ( limiter );
211 MyTimeNotify *zeit = new MyTimeNotify ( &dispatcher );
212 Synth_FREQUENCY freq;
213 Synth_WAVE_SIN sin;
214 Synth_PLAY play;
215 setValue ( freq, frequency );
216 connect ( freq, sin );
217 connect ( sin, play, "invalue_left" );
218 freq.start ( );
219 sin.start ( );
220 play.start ( );
221 limiter->addTimer ( 240, zeit );
222 dispatcher.run ( );
223 play.stop ( );
224 sin.stop ( );
225 freq.stop ( );
228 void
229 MyTimeNotify::notifyTime ( )
231 test->terminate ( );
234 #endif
236 QString
237 whois ( const char *MAC_ADR, QStringList APList )
239 for ( QStringList::Iterator it = APList.begin ( ); it != APList.end ( ); ( it++ )++ )
241 if ( ( *it ) == ( QString ) MAC_ADR )
242 return *( ++it );
244 return i18n ( "UNKNOWN" );
248 NetworkParameterDataModel::rowCount ( const QModelIndex & parent ) const
250 return dataContainer.size ( );
254 NetworkParameterDataModel::columnCount ( const QModelIndex & parent ) const
256 return 4;
259 QVariant
260 NetworkParameterDataModel::data ( const QModelIndex & index, int role ) const
262 if ( index.row ( ) >= dataContainer.size ( ) )
263 return QVariant ( );
265 if ( role != Qt::DisplayRole && role != Qt::BackgroundColorRole )
266 return QVariant ( );
268 if ( role == Qt::DisplayRole )
269 switch ( index.column ( ) )
271 case 0:
272 return dataContainer[index.row ( )].ssid;
273 case 1:
274 return dataContainer[index.row ( )].mode;
275 case 2:
276 return ( QString ( "%1" ) ).arg ( dataContainer[index.row ( )].quality );
277 case 3:
278 if ( dataContainer[index.row ( )].encryption == "off" )
279 return QString ( i18n ( "off" ) );
280 if ( dataContainer[index.row ( )].encryption == "on" )
281 return QString ( i18n ( "on" ) );
282 return dataContainer[index.row ( )].encryption;
283 default:
284 return QVariant ( );
287 if ( role == Qt::BackgroundColorRole )
288 switch ( index.column ( ) )
290 case 0:
291 return ( dataContainer[index.row ( )].ssid == "" ? QColor ( 255, 255, 0 ) : QColor ( 0, 255, 0 ) );
292 case 1:
293 return QColor ( 0, 255, 0 );
294 case 2:
295 return QColor ( 0, 255, 0 );
296 case 3:
297 if ( dataContainer[index.row ( )].encryption == "on" )
298 return QColor ( 255, 255, 0 );
299 if ( checkWEP ( dataContainer[index.row ( )].encryption ) == NONE )
300 return QColor ( 0, 255, 0 );
301 if ( checkWEP ( dataContainer[index.row ( )].encryption ) == VALID_HEX ||
302 checkWEP ( dataContainer[index.row ( )].encryption ) == VALID_STRING )
303 return QColor ( 0, 255, 0 );
304 if ( checkWEP ( dataContainer[index.row ( )].encryption ) == INVALID )
305 return QColor ( 255, 0, 0 );
306 return QVariant ( );
307 default:
308 return QVariant ( );
311 // we will never get here, but let's make the compiler happy
312 return QVariant ();
315 WEP_KEY
316 NetworkParameterDataModel::checkWEP ( const QString & key ) const
319 kDebug ( ) << "In checkWEP()\n";
320 if ( key == "off" )
321 return NONE;
323 if ( key.length ( ) == 5 )
324 return VALID_STRING;
325 if ( key.length ( ) == 13 )
326 return VALID_STRING;
327 if ( key.length ( ) == 32 )
328 return VALID_STRING;
330 // a more restrictive check: is it really a hex number?
332 QRegExp hexNumber ( "[A-F,a-f,0-9]*" );
334 if ( key.length ( ) == 10 && hexNumber.exactMatch ( key ) )
335 return VALID_HEX;
336 if ( key.length ( ) == 26 && hexNumber.exactMatch ( key ) )
337 return VALID_HEX;
338 if ( key.length ( ) == 64 && hexNumber.exactMatch ( key ) )
339 return VALID_HEX;
341 return INVALID;
345 QVariant
346 NetworkParameterDataModel::data ( int row, int col )
348 return data ( createIndex ( row, col, 0 ), Qt::DisplayRole );
351 QVariant
352 NetworkParameterDataModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const
354 if ( orientation == Qt::Vertical )
355 return QVariant ( );
356 switch ( section )
358 case 0:
359 return i18n ( "SSID" );
360 break;
361 case 1:
362 return i18n ( "Mode" );
363 break;
364 case 2:
365 return i18n ( "Quality" );
366 break;
367 case 3:
368 return i18n ( "Encryption" );
369 break;
370 case 4:
371 return "reserved";
372 break;
373 default:
374 return "???";
375 break;
379 QVariant
380 NetworkParameterDataModel::headerData ( int col )
382 return headerData ( col, Qt::Horizontal );
385 bool
386 NetworkParameterDataModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
388 if ( role != Qt::EditRole )
389 return false;
390 if ( dataContainer.size ( ) == 0 )
391 insertColumns ( 0, 4 );
392 for ( int i = dataContainer.size ( ); i <= index.row ( ); i++ )
394 kDebug ( ) << "intialising intermediate row " << i << ".\n";
395 struct NetworkParameterData *newRow = new NetworkParameterData;
396 newRow->ssid = "";
397 newRow->mode = "";
398 newRow->quality = 0;
399 newRow->encryption = "off";
400 dataContainer.append ( *newRow );
401 insertRow ( i );
403 kDebug ( ) << "Setting value at row " << index.row ( ) << ", col " << index.column ( ) << " to " << value.
404 toString ( ) << ".\n";
406 switch ( index.column ( ) )
408 case 0:
409 dataContainer[index.row ( )].ssid = value.toString ( );
410 break;
411 case 1:
412 dataContainer[index.row ( )].mode = value.toString ( );
413 break;
414 case 2:
415 dataContainer[index.row ( )].quality = value.toString ( ).toInt ( );
416 break;
417 case 3:
418 dataContainer[index.row ( )].encryption = value.toString ( );
419 break;
420 default:
421 return false;
423 // emit dataChanged(index,index);
424 return true;
427 void
428 NetworkParameterDataModel::gotFinishedIndicator ( )
430 emit finishedScanning ( );
433 bool
434 NetworkParameterDataModel::setData ( int row, int col, const QVariant & value )
436 return setData ( createIndex ( row, col ), value );
439 Qt::ItemFlags NetworkParameterDataModel::flags ( const QModelIndex & index ) const
441 if ( index.row ( ) >= dataContainer.size ( ) )
442 return 0;
443 if ( index.column ( ) > 3 )
444 return 0;
445 if ( index.column ( ) != 0 && index.column ( ) != 3 )
446 return Qt::ItemIsSelectable;
447 if ( index.column ( ) == 0 && !dataContainer[index.row ( )].ssid.isEmpty() )
448 return Qt::ItemIsSelectable;
449 if ( index.column ( ) == 3 && dataContainer[index.row ( )].encryption == "off" )
450 return Qt::ItemIsSelectable;
451 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
454 #include "interface_wireless.moc"