Forwardport fix for bug 139701: Away msg polling too frequently.
[kdenetwork.git] / wifi / kwifi_ifaceimpl.cpp
blob0003c31cf033e7d0a60bfd267e91422523a963f8
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 // done with painting
87 bla.end();
89 temp.setMask ( temp.createHeuristicMask ( ) );
90 trayicon->setIcon ( temp );
91 trayicon->setToolTip ( "SSID: " + device->get_essid ( ) );
92 this->setWindowIcon ( temp );
95 KWiFi_Iface::KWiFi_Iface ( KWiFiManagerApp* parent )
98 device = new Interface_wireless_wirelessextensions ( );
100 led = new Led ( );
102 trayicon = new KSystemTrayIcon ( parent );
103 trayicon->show ( );
105 zentrallayout = new QGridLayout ( this );
107 iface_container = new QWidget ( this );
108 iface_ui.setupUi ( iface_container );
110 statsContainer = new QWidget ( this );
111 stats_ui.setupUi ( statsContainer );
113 statistik = new Statistics ( device, stats_ui.groupBox );
114 statistik->setFixedSize ( 480, 200 );
115 statistik->setObjectName ( QString::fromUtf8 ( "statsPlaceholder" ) );
116 stats_ui.statsPlaceholder = statistik;
117 stats_ui.gridLayout1->addWidget ( statistik, 0, 1, 3, 3 );
118 statsContainer->hide ( );
120 zentrallayout->addWidget ( iface_container, 0, 0 );
122 connect ( device, SIGNAL ( notifyInterfaceChanged()), this, SLOT ( slotChangeInterfaceChangeNotification ()));
124 connect ( device, SIGNAL ( statsUpdated ( ) ), statistik, SLOT ( update ( ) ) );
125 connect ( device, SIGNAL ( statsUpdated ( ) ), this, SLOT ( updateStatsSurroundings ( ) ) );
127 connect ( device, SIGNAL ( strengthChanged ( int, int, int ) ), this, SLOT ( updateTrayIcon ( int, int, int ) ) );
128 connect ( device, SIGNAL ( strengthChanged ( int, int, int ) ), this, SLOT ( updateStrengthBar ( int, int, int ) ) );
129 connect ( device, SIGNAL ( statusChanged ( ) ), this, SLOT ( updateStatus ( ) ) );
130 connect ( device, SIGNAL ( speedChanged ( signed long ) ), this, SLOT ( updateSpeed ( signed long ) ) );
131 connect ( device, SIGNAL ( modeChanged ( CARDSTATES ) ), this, SLOT ( updateCardState ( CARDSTATES ) ) );
132 connect ( device, SIGNAL ( essidChanged ( QString ) ), parent, SLOT ( slotLogESSID ( QString ) ) );
133 connect ( device, SIGNAL ( txPowerChanged ( ) ), this, SLOT ( slotTXPowerChanged ( ) ) );
134 connect ( iface_ui.networkScan, SIGNAL ( clicked ( ) ), this, SLOT ( slotNetworkScan ( ) ) );
135 connect ( iface_ui.statsButton, SIGNAL ( clicked ( ) ), this, SLOT ( toggleStats ( ) ) );
137 highestExperiencedSpeed = 0;
139 dataUpdateTrigger = new QTimer ( this );
140 connect ( dataUpdateTrigger, SIGNAL ( timeout ( ) ), device, SLOT ( poll_device_info ( ) ) );
141 dataUpdateTrigger->start ( KWiFiSettings::self ( )->refreshRate ( ) );
143 tricorderTrigger = new QTimer ( );
144 connect ( tricorderTrigger, SIGNAL ( timeout ( ) ), this, SLOT ( tricorderBeep ( ) ) );
146 device->poll_device_info();
149 void
150 KWiFi_Iface::slotChangeInterfaceChangeNotification ()
153 kDebug() << "slotChangeInterfaceChangeNotification ()" << endl;
154 emit interfaceChanged( this );
157 void
158 KWiFi_Iface::slotTXPowerChanged ( )
160 iface_ui.disableRadio->setChecked ( device->get_txpower_disabled ( ) );
163 void
164 KWiFi_Iface::updateStatsSurroundings ( )
166 int top = statistik->determineTop ( );
167 int bot = statistik->determineBottom ( );
169 if ( top < 0 && bot < 0 )
171 stats_ui.maxLevelLabel->setText ( i18n ( "%1 dBm", top ) );
172 stats_ui.minLevelLabel->setText ( i18n ( "%1 dBm", bot ) );
174 else
176 stats_ui.maxLevelLabel->setText ( ( ( QString ) "%1" ).arg ( top ) );
177 stats_ui.minLevelLabel->setText ( ( ( QString ) "%1" ).arg ( bot ) );
180 stats_ui.historyTime->setText ( i18n ( "-%1 s", KWiFiSettings::self ( )->refreshRate ( ) * 240 / 1000 ) );
184 void
185 KWiFi_Iface::updateSpeed ( signed long newSpeed )
187 // the following is on record only for cards that can't report the maxBitrate
188 if ( newSpeed > highestExperiencedSpeed )
189 highestExperiencedSpeed = newSpeed;
190 signed long max;
191 if ( device->getMaxBitrate ( ) < 0 )
192 max = highestExperiencedSpeed;
193 else
194 max = device->getMaxBitrate ( );
195 iface_ui.maxSpeed->setText ( i18n ( "%1 MBit/s", max / 1000000 ) );
196 iface_ui.speedIndicator->setMinimum ( 0 );
197 iface_ui.speedIndicator->setMaximum ( max );
198 iface_ui.speedIndicator->setValue ( newSpeed );
201 void
202 KWiFi_Iface::updateStatus ( )
204 switch ( device->getDeviceState ( ) )
206 case::AP_CONNECT:
207 iface_ui.connLabel->setText ( i18n ( "Connected to network:" ) );
208 break;
209 case::NO_CONNECTION:
210 iface_ui.connLabel->setText ( i18n ( "Searching for network:" ) );
211 break;
212 case::AD_HOC:
213 iface_ui.connLabel->setText ( i18n ( "Ad-hoc connection to:" ) );
214 break;
215 default:
216 iface_ui.connLabel->setText ( i18n ( "SSID:" ) );
219 iface_ui.networkName->setText ( device->get_essid ( ) );
220 QString mac, ip;
221 device->get_AP_info ( mac, ip );
222 if ( mac == "44:44:44:44:44:44" || mac == "00:00:00:00:00:00" )
223 mac = i18n ( "- no access point -" );
224 iface_ui.accessPoint->setText ( mac + '\n' + whois ( ( const char * ) mac.toLatin1 ( ), KWiFiManagerApp::APs ) );
225 QString IPs = device->get_IP_info ( ).join ( "\n" );
226 iface_ui.ipList->setText ( IPs );
227 long freq;
228 bool has_freq = device->get_device_freq ( freq );
229 if ( !has_freq )
230 iface_ui.frequency->setText ( i18n ( "no information" ) );
231 else
233 if ( freq <= 1000 ) // then the card reported its channels
234 iface_ui.frequency->setText ( QString ( "%1 [%2]" ).arg ( device->toggleFreqChannel ( freq ) ).arg ( freq ) );
235 else
236 iface_ui.frequency->setText ( QString ( "%1 [%2]" ).arg ( freq ).arg ( device->toggleFreqChannel ( freq ) ) );
239 QString key;
240 int size = 0, flags = 0;
241 if ( device->get_key ( key, size, flags ) )
242 if ( size == 0 )
243 iface_ui.encryption->setText ( i18n ( "off" ) );
244 else
245 iface_ui.encryption->setText ( i18n ( "active, %1 Bit", size * 8 ) );
248 void
249 KWiFi_Iface::updateCardState ( CARDSTATES state )
251 switch ( state )
253 case::NO_CARD:
254 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::NO_CARD );
255 break;
256 case::NO_CONNECTION:
257 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::ALL_ALONE );
258 break;
259 case::AP_CONNECT:
260 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::AP_CONNECT );
261 break;
262 case::AD_HOC:
263 iface_ui.connTypePic->setPixmap ( *KWiFiManagerApp::AD_HOC );
264 break;
268 void
269 KWiFi_Iface::updateStrengthBar ( int newQual, int newSig, int newNoise )
271 int newStrength = KWiFiSettings::self ( )->alternateStrength ( ) ? newSig - newNoise : newQual;
272 int qualityClass = newStrength / 5 + 2;
273 if ( qualityClass > 5 )
274 qualityClass = 5;
275 if ( qualityClass < 0 )
276 qualityClass = 0;
277 switch ( device->getDeviceState ( ) )
279 case::AP_CONNECT:
280 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[qualityClass] ) );
281 iface_ui.strengthNumber->setText ( QString ( "%1" ).arg ( newStrength ) );
282 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[qualityClass] );
283 break;
284 case::NO_CONNECTION:
285 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[1] ) );
286 iface_ui.strengthNumber->setText ( "0" );
287 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[1] );
288 break;
289 default:
290 iface_ui.strengthPic->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC[0] ) );
291 iface_ui.strengthNumber->setText ( i18n ( "N/A" ) );
292 iface_ui.category->setText ( KWiFiManagerApp::strengthClassName[2] );
296 void
297 KWiFi_Iface::slotNetworkScan ( )
299 iface_ui.networkScan->setText ( i18n ( "Scan in progress..." ) );
300 iface_ui.networkScan->setEnabled ( false );
301 scanwidget = new NetworkScanning ( device );
302 iface_ui.networkScan->setText ( i18n ( "Scan for &Networks..." ) );
303 iface_ui.networkScan->setEnabled ( true );
306 void
307 KWiFi_Iface::toggleStats ( )
309 if ( statsContainer->isVisible ( ) )
311 statsContainer->hide ( );
312 zentrallayout->removeWidget ( statsContainer );
313 statsContainer->setParent ( 0 );
315 else
317 statsContainer->setParent ( this );
318 zentrallayout->addWidget ( statsContainer, 1, 0 );
319 statsContainer->show ( );
321 resize ( minimumSizeHint ( ) );
324 void
325 KWiFi_Iface::slotDisableRadio ( )
327 iface_ui.disableRadio->setChecked ( device->get_txpower_disabled ( ) );
329 if ( disablePower != 0 && disablePower->isRunning ( ) )
331 return;
333 QString interface = device->get_interface_name ( );
335 if ( interface.isEmpty ( ) )
337 return;
340 QString onOrOff;
341 if ( iface_ui.disableRadio->isChecked ( ) )
343 onOrOff = "on";
345 else
347 onOrOff = "off";
350 disablePower = new KProcess;
352 *disablePower << "kdesu" << "iwconfig" << interface << "txpower" << onOrOff;
353 connect ( disablePower, SIGNAL ( processExited ( KProcess * ) ), this, SLOT ( slotDisablePowerProcessExited ( ) ) );
354 disablePower->start ( KProcess::NotifyOnExit );
357 void
358 KWiFi_Iface::slotDisablePowerProcessExited ( )
360 if ( !disablePower->normalExit ( ) || disablePower->exitStatus ( ) != 0 )
362 delete disablePower;
363 disablePower = 0;
364 return;
367 if ( iface_ui.disableRadio->isChecked ( ) )
369 iface_ui.disableRadio->setChecked ( false );
371 else
373 iface_ui.disableRadio->setChecked ( true );
376 delete disablePower;
377 disablePower = 0;
379 iface_ui.disableRadio->setChecked ( device->get_txpower_disabled ( ) );
382 void
383 KWiFi_Iface::slotToggleTric ( )
385 if ( iface_ui.tricorder->isChecked ( ) )
387 tricorderTrigger->start ( 250 );
389 else
391 tricorderTrigger->stop ( );
395 void
396 KWiFi_Iface::tricorderBeep ( )
398 int sig, noi, qual;
399 device->get_current_quality ( sig, noi, qual );
400 #ifndef WITHOUT_ARTS
401 sine_wave ( 150.0 + qual * 20 );
402 #endif
403 if ( qual == 0 )
405 tricorderTrigger->start ( 2000 );
407 else if ( qual < 10 )
409 tricorderTrigger->start ( 1000 );
411 else
413 tricorderTrigger->start ( 500 );
417 #include "kwifi_ifaceimpl.moc"