Port KProgressBar's to QProgressBar's.
[kdenetwork.git] / wifi / kwifi_ifaceimpl.cpp
bloba1a8348f7437a3534df9bd65f33bf1302e3bf19d
1 /***************************************************************************
2 kwifi_ifaceimpl.cpp - description
3 -------------------
4 begin : Sun Jan 22 17:44:20 CEST 2001
5 copyright : (C) 2006 by Stefan Winter
6 email : swinter@kde.org
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 <QPixmap>
19 #include <QBitmap>
20 #include <QPainter>
21 #include <QString>
22 #include <QFont>
23 #include <QTimer>
24 #include <kdebug.h>
25 #include <ksystemtrayicon.h>
26 #include <kprocess.h>
27 #include "kwifi_ifaceimpl.h"
28 #include "kwifi_settings.h"
29 #include "asusled.h"
30 #include "statistics.h"
31 #include "kwifimanager.h"
32 #include "networkscanning.h"
33 #include "interface_wireless_wirelessextensions.h"
35 void
36 KWiFi_Iface::updateTrayIcon ( int newQual, int newSig, int newNoise )
38 int newStrength = KWiFiSettings::self ( )->alternateStrength ( ) ? newSig - newNoise : newQual;
39 QPixmap temp ( 30, 30 );
40 QPainter bla ( &temp );
41 bla.fillRect ( 0, 0, 30, 30, palette().brush(QPalette::Window) );
42 QPixmap *ICON;
43 QLabel strengthNumber ( "" );
45 int qualityClass = newStrength / 5 + 2;
46 if ( qualityClass > 5 )
47 qualityClass = 5;
48 if (qualityClass < 0 )
49 qualityClass = 0;
50 switch ( device->getDeviceState ( ) )
52 case AP_CONNECT:
53 ICON = KWiFiManagerApp::STRENGTHPIC[qualityClass];
54 strengthNumber.setText ( QString ( "%1" ).arg ( newStrength ) );
55 if ( led->state == false )
56 led->On ( );
57 break;
58 case NO_CONNECTION:
59 ICON = KWiFiManagerApp::STRENGTHPIC[1];
60 strengthNumber.setText ( "0" );
61 if ( led->state == true )
62 led->Off ( );
63 break;
64 default:
65 ICON = KWiFiManagerApp::STRENGTHPIC[0];
66 strengthNumber.setText ( i18n ( "N/A" ) );
67 if ( led->state == true )
68 led->Off ( );
71 bla.drawPixmap ( 1, 4, *ICON );
73 static QFont labelfont ( "Helvetica", 10 );
75 if ( KWiFiSettings::self()->showStrengthNumberInTray() )
77 labelfont.setStyleHint ( QFont::SansSerif, QFont::NoAntialias );
78 labelfont.setStretch ( QFont::Condensed );
79 strengthNumber.setFont ( labelfont );
80 strengthNumber.setFixedSize ( strengthNumber.sizeHint ( ) );
81 QPixmap labeltemp = QPixmap::grabWidget ( &strengthNumber );
82 labeltemp.setMask ( labeltemp.createHeuristicMask ( ) );
83 bla.drawPixmap ( 2, 2, labeltemp );
86 temp.setMask ( temp.createHeuristicMask ( ) );
87 trayicon->setIcon ( temp );
88 trayicon->setToolTip ( "SSID: " + device->get_essid ( ) );
89 this->setWindowIcon ( temp );
92 KWiFi_Iface::KWiFi_Iface ( QStringList* ignoreList )
95 device = new Interface_wireless_wirelessextensions ( ignoreList );
96 device->poll_device_info();
98 led = new Led ( );
100 trayicon = new KSystemTrayIcon ( this );
101 trayicon->show ( );
103 zentrallayout = new QGridLayout ( this );
105 iface_container = new QWidget ( this );
106 iface_ui.setupUi ( iface_container );
108 statsContainer = new QWidget ( this );
109 stats_ui.setupUi ( statsContainer );
111 statistik = new Statistics ( device, stats_ui.groupBox );
112 statistik->setFixedSize ( 480, 200 );
113 statistik->setObjectName ( QString::fromUtf8 ( "statsPlaceholder" ) );
114 stats_ui.statsPlaceholder = statistik;
115 stats_ui.gridLayout1->addWidget ( statistik, 0, 1, 3, 3 );
116 statsContainer->hide ( );
118 zentrallayout->addWidget ( iface_container, 0, 0 );
120 connect ( device, SIGNAL ( statsUpdated ( ) ), statistik, SLOT ( update ( ) ) );
121 connect ( device, SIGNAL ( statsUpdated ( ) ), this, SLOT ( updateStatsSurroundings ( ) ) );
123 connect ( device, SIGNAL ( interfaceChanged ( ) ), this, SLOT ( slotChangeWindowCaption ( ) ) );
124 connect ( device, SIGNAL ( strengthChanged ( int, int, int ) ), this, SLOT ( updateTrayIcon ( int, int, int ) ) );
125 connect ( device, SIGNAL ( strengthChanged ( int, int, int ) ), this, SLOT ( updateStrengthBar ( int, int, int ) ) );
126 connect ( device, SIGNAL ( statusChanged ( ) ), this, SLOT ( updateStatus ( ) ) );
127 connect ( device, SIGNAL ( speedChanged ( signed long ) ), this, SLOT ( updateSpeed ( signed long ) ) );
128 connect ( device, SIGNAL ( modeChanged ( CARDSTATES ) ), this, SLOT ( updateCardState ( CARDSTATES ) ) );
129 connect ( device, SIGNAL ( essidChanged ( QString ) ), this, SLOT ( slotLogESSID ( QString ) ) );
130 connect ( device, SIGNAL ( txPowerChanged ( ) ), this, SLOT ( slotTXPowerChanged ( ) ) );
131 connect ( iface_ui.networkScan, SIGNAL ( clicked ( ) ), this, SLOT ( slotNetworkScan ( ) ) );
132 connect ( iface_ui.statsButton, SIGNAL ( clicked ( ) ), this, SLOT ( toggleStats ( ) ) );
134 highestExperiencedSpeed = 0;
136 dataUpdateTrigger = new QTimer ( this );
137 connect ( dataUpdateTrigger, SIGNAL ( timeout ( ) ), device, SLOT ( poll_device_info ( ) ) );
138 dataUpdateTrigger->start ( KWiFiSettings::self ( )->refreshRate ( ) );
140 tricorderTrigger = new QTimer ( );
141 connect ( tricorderTrigger, SIGNAL ( timeout ( ) ), this, SLOT ( tricorderBeep ( ) ) );
145 void
146 KWiFi_Iface::updateStatsSurroundings ( )
148 int top = statistik->determineTop ( );
149 int bot = statistik->determineBottom ( );
151 if ( top < 0 && bot < 0 )
153 stats_ui.maxLevelLabel->setText ( i18n ( "%1 dBm", top ) );
154 stats_ui.minLevelLabel->setText ( i18n ( "%1 dBm", bot ) );
156 else
158 stats_ui.maxLevelLabel->setText ( ( ( QString ) "%1" ).arg ( top ) );
159 stats_ui.minLevelLabel->setText ( ( ( QString ) "%1" ).arg ( bot ) );
162 stats_ui.historyTime->setText ( i18n ( "-%1 s", KWiFiSettings::self ( )->refreshRate ( ) * 240 / 1000 ) );
166 void
167 KWiFi_Iface::updateSpeed ( signed long newSpeed )
169 // the following is on record only for cards that can't report the maxBitrate
170 if ( newSpeed > highestExperiencedSpeed )
171 highestExperiencedSpeed = newSpeed;
172 signed long max;
173 if ( device->getMaxBitrate ( ) < 0 )
174 max = highestExperiencedSpeed;
175 else
176 max = device->getMaxBitrate ( );
177 iface_ui.maxSpeed->setText ( i18n ( "%1 MBit/s", max / 1000000 ) );
178 iface_ui.speedIndicator->setMinimum ( 0 );
179 iface_ui.speedIndicator->setMaximum ( max );
180 iface_ui.speedIndicator->setValue ( newSpeed );
183 void
184 KWiFi_Iface::updateStatus ( )
186 switch ( device->getDeviceState ( ) )
188 case::AP_CONNECT:
189 iface_ui.connLabel->setText ( i18n ( "Connected to network:" ) );
190 break;
191 case::NO_CONNECTION:
192 iface_ui.connLabel->setText ( i18n ( "Searching for network:" ) );
193 break;
194 case::AD_HOC:
195 iface_ui.connLabel->setText ( i18n ( "Ad-hoc connection to:" ) );
196 break;
197 default:
198 iface_ui.connLabel->setText ( i18n ( "SSID:" ) );
201 iface_ui.networkName->setText ( device->get_essid ( ) );
202 QString mac, ip;
203 device->get_AP_info ( mac, ip );
204 if ( mac == "44:44:44:44:44:44" || mac == "00:00:00:00:00:00" )
205 mac = i18n ( "- no access point -" );
206 iface_ui.accessPoint->setText ( mac + "\n" + whois ( ( const char * ) mac.toLatin1 ( ), KWiFiManagerApp::APs ) );
207 QString IPs = device->get_IP_info ( ).join ( "\n" );
208 iface_ui.ipList->setText ( IPs );
209 long freq;
210 bool has_freq = device->get_device_freq ( freq );
211 if ( !has_freq )
212 iface_ui.frequency->setText ( i18n ( "no information" ) );
213 else
215 if ( freq <= 1000 ) // then the card reported its channels
216 iface_ui.frequency->setText ( QString ( "%1 [%2]" ).arg ( device->toggleFreqChannel ( freq ) ).arg ( freq ) );
217 else
218 iface_ui.frequency->setText ( QString ( "%1 [%2]" ).arg ( freq ).arg ( device->toggleFreqChannel ( freq ) ) );
221 QString key;
222 int size = 0, flags = 0;
223 if ( device->get_key ( key, size, flags ) )
224 if ( size == 0 )
225 iface_ui.encryption->setText ( i18n ( "off" ) );
226 else
227 iface_ui.encryption->setText ( i18n ( "active, %1 Bit", size * 8 ) );
230 void
231 KWiFi_Iface::updateCardState ( CARDSTATES state )
233 switch ( state )
235 case::NO_CARD:
236 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::NO_CARD );
237 break;
238 case::NO_CONNECTION:
239 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::ALL_ALONE );
240 break;
241 case::AP_CONNECT:
242 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::AP_CONNECT );
243 break;
244 case::AD_HOC:
245 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::AD_HOC );
246 break;
250 void
251 KWiFi_Iface::updateStrengthBar ( int newQual, int newSig, int newNoise )
253 int newStrength = KWiFiSettings::self ( )->alternateStrength ( ) ? newSig - newNoise : newQual;
254 int qualityClass = newStrength / 5 + 2;
255 if ( qualityClass > 5 )
256 qualityClass = 5;
257 if ( qualityClass < 0 )
258 qualityClass = 0;
259 switch ( device->getDeviceState ( ) )
261 case::AP_CONNECT:
262 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[qualityClass] ) );
263 iface_ui.strengthNumber->setText ( QString ( "%1" ).arg ( newStrength ) );
264 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[qualityClass] );
265 break;
266 case::NO_CONNECTION:
267 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[1] ) );
268 iface_ui.strengthNumber->setText ( "0" );
269 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[1] );
270 break;
271 default:
272 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[0] ) );
273 iface_ui.strengthNumber->setText ( i18n ( "N/A" ) );
274 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[2] );
278 void
279 KWiFi_Iface::slotNetworkScan ( )
281 iface_ui.networkScan->setText ( i18n ( "Scan in progress..." ) );
282 iface_ui.networkScan->setEnabled ( false );
283 scanwidget = new NetworkScanning ( device );
284 iface_ui.networkScan->setText ( i18n ( "Scan for &Networks..." ) );
285 iface_ui.networkScan->setEnabled ( true );
288 void
289 KWiFi_Iface::toggleStats ( )
291 if ( statsContainer->isVisible ( ) )
293 statsContainer->hide ( );
294 zentrallayout->removeWidget ( statsContainer );
295 statsContainer->setParent ( 0 );
297 else
299 statsContainer->setParent ( this );
300 zentrallayout->addWidget ( statsContainer, 1, 0 );
301 statsContainer->show ( );
303 resize ( minimumSizeHint ( ) );
306 void
307 KWiFi_Iface::slotDisableRadio ( )
309 iface_ui.disableRadio->setChecked ( device->get_txpower_disabled ( ) );
311 if ( disablePower != 0 && disablePower->isRunning ( ) )
313 return;
315 QString interface = device->get_interface_name ( );
317 if ( interface.isEmpty ( ) )
319 return;
322 QString onOrOff;
323 if ( iface_ui.disableRadio->isChecked ( ) )
325 onOrOff = "on";
327 else
329 onOrOff = "off";
332 disablePower = new KProcess;
334 *disablePower << "kdesu" << "iwconfig" << interface << "txpower" << onOrOff;
335 connect ( disablePower, SIGNAL ( processExited ( KProcess * ) ), this, SLOT ( slotDisablePowerProcessExited ( ) ) );
336 disablePower->start ( KProcess::NotifyOnExit );
339 void
340 KWiFi_Iface::slotDisablePowerProcessExited ( )
342 if ( !disablePower->normalExit ( ) || disablePower->exitStatus ( ) != 0 )
344 delete disablePower;
345 disablePower = 0;
346 return;
349 if ( iface_ui.disableRadio->isChecked ( ) )
351 iface_ui.disableRadio->setChecked ( false );
353 else
355 iface_ui.disableRadio->setChecked ( true );
358 delete disablePower;
359 disablePower = 0;
361 iface_ui.disableRadio->setChecked ( device->get_txpower_disabled ( ) );
364 void
365 KWiFi_Iface::slotToggleTric ( )
367 if ( iface_ui.tricorder->isChecked ( ) )
369 tricorderTrigger->start ( 250 );
371 else
373 tricorderTrigger->stop ( );
377 void
378 KWiFi_Iface::tricorderBeep ( )
380 int sig, noi, qual;
381 device->get_current_quality ( sig, noi, qual );
382 #ifndef WITHOUT_ARTS
383 sine_wave ( 150.0 + qual * 20 );
384 #endif
385 if ( qual == 0 )
387 tricorderTrigger->start ( 2000 );
389 else if ( qual < 10 )
391 tricorderTrigger->start ( 1000 );
393 else
395 tricorderTrigger->start ( 500 );
399 #include "kwifi_ifaceimpl.moc"