1 /***************************************************************************
2 kwifi_ifaceimpl.cpp - description
4 begin : Sun Jan 22 17:44:20 CEST 2001
5 copyright : (C) 2006 by Stefan Winter
6 email : swinter@kde.org
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
25 #include <ksystemtray.h>
27 #include "kwifi_ifaceimpl.h"
28 #include "kwifi_settings.h"
30 #include "statistics.h"
31 #include "kwifimanager.h"
32 #include "networkscanning.h"
33 #include "interface_wireless_wirelessextensions.h"
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, backgroundBrush ( ) );
43 QLabel
strengthNumber ( "", 0, 0 );
45 int qualityClass
= newStrength
/ 5 + 2;
46 if ( qualityClass
> 5 )
48 if (qualityClass
< 0 )
50 switch ( device
->getDeviceState ( ) )
53 ICON
= KWiFiManagerApp::STRENGTHPIC
[qualityClass
];
54 strengthNumber
.setText ( QString ( "%1" ).arg ( newStrength
) );
55 if ( led
->state
== false )
59 ICON
= KWiFiManagerApp::STRENGTHPIC
[1];
60 strengthNumber
.setText ( "0" );
61 if ( led
->state
== true )
65 ICON
= KWiFiManagerApp::STRENGTHPIC
[0];
66 strengthNumber
.setText ( i18n ( "N/A" ) );
67 if ( led
->state
== true )
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
->setPixmap ( temp
);
88 trayicon
->setToolTip ( "SSID: " + device
->get_essid ( ) );
89 this->setIcon ( temp
);
92 KWiFi_Iface::KWiFi_Iface ( QStringList
* ignoreList
)
95 device
= new Interface_wireless_wirelessextensions ( ignoreList
);
96 device
->poll_device_info();
100 trayicon
= new KSystemTray ( this );
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 ( ) ) );
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
) );
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 ) );
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
;
173 if ( device
->getMaxBitrate ( ) < 0 )
174 max
= highestExperiencedSpeed
;
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
);
184 KWiFi_Iface::updateStatus ( )
186 switch ( device
->getDeviceState ( ) )
189 iface_ui
.connLabel
->setText ( i18n ( "Connected to network:" ) );
192 iface_ui
.connLabel
->setText ( i18n ( "Searching for network:" ) );
195 iface_ui
.connLabel
->setText ( i18n ( "Ad-hoc connection to:" ) );
198 iface_ui
.connLabel
->setText ( i18n ( "SSID:" ) );
201 iface_ui
.networkName
->setText ( device
->get_essid ( ) );
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
);
210 bool has_freq
= device
->get_device_freq ( freq
);
212 iface_ui
.frequency
->setText ( i18n ( "no information" ) );
215 if ( freq
<= 1000 ) // then the card reported its channels
216 iface_ui
.frequency
->setText ( QString ( "%1 [%2]" ).arg ( device
->toggleFreqChannel ( freq
) ).arg ( freq
) );
218 iface_ui
.frequency
->setText ( QString ( "%1 [%2]" ).arg ( freq
).arg ( device
->toggleFreqChannel ( freq
) ) );
222 int size
= 0, flags
= 0;
223 if ( device
->get_key ( key
, size
, flags
) )
225 iface_ui
.encryption
->setText ( i18n ( "off" ) );
227 iface_ui
.encryption
->setText ( i18n ( "active, %1 Bit", size
* 8 ) );
231 KWiFi_Iface::updateCardState ( CARDSTATES state
)
236 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::NO_CARD
);
239 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::ALL_ALONE
);
242 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::AP_CONNECT
);
245 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::AD_HOC
);
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 )
257 if ( qualityClass
< 0 )
259 switch ( device
->getDeviceState ( ) )
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
] );
267 iface_ui
.strengthPic
->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC
[1] ) );
268 iface_ui
.strengthNumber
->setText ( "0" );
269 iface_ui
.category
->setText ( KWiFiManagerApp::strengthClassName
[1] );
272 iface_ui
.strengthPic
->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC
[0] ) );
273 iface_ui
.strengthNumber
->setText ( i18n ( "N/A" ) );
274 iface_ui
.category
->setText ( KWiFiManagerApp::strengthClassName
[2] );
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 );
289 KWiFi_Iface::toggleStats ( )
291 if ( statsContainer
->isVisible ( ) )
293 statsContainer
->hide ( );
294 zentrallayout
->removeWidget ( statsContainer
);
295 statsContainer
->setParent ( 0 );
299 statsContainer
->setParent ( this );
300 zentrallayout
->addWidget ( statsContainer
, 1, 0 );
301 statsContainer
->show ( );
303 resize ( minimumSizeHint ( ) );
307 KWiFi_Iface::slotDisableRadio ( )
309 iface_ui
.disableRadio
->setChecked ( device
->get_txpower_disabled ( ) );
311 if ( disablePower
!= 0 && disablePower
->isRunning ( ) )
315 QString interface
= device
->get_interface_name ( );
317 if ( interface
.isEmpty ( ) )
323 if ( iface_ui
.disableRadio
->isChecked ( ) )
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
);
340 KWiFi_Iface::slotDisablePowerProcessExited ( )
342 if ( !disablePower
->normalExit ( ) || disablePower
->exitStatus ( ) != 0 )
349 if ( iface_ui
.disableRadio
->isChecked ( ) )
351 iface_ui
.disableRadio
->setChecked ( false );
355 iface_ui
.disableRadio
->setChecked ( true );
361 iface_ui
.disableRadio
->setChecked ( device
->get_txpower_disabled ( ) );
365 KWiFi_Iface::slotToggleTric ( )
367 if ( iface_ui
.tricorder
->isChecked ( ) )
369 tricorderTrigger
->start ( 250 );
373 tricorderTrigger
->stop ( );
378 KWiFi_Iface::tricorderBeep ( )
381 device
->get_current_quality ( sig
, noi
, qual
);
383 sine_wave ( 150.0 + qual
* 20 );
387 tricorderTrigger
->start ( 2000 );
389 else if ( qual
< 10 )
391 tricorderTrigger
->start ( 1000 );
395 tricorderTrigger
->start ( 500 );
399 #include "kwifi_ifaceimpl.moc"