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 <ksystemtrayicon.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, palette().brush(QPalette::Window
) );
43 QLabel
strengthNumber ( "" );
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
);
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 ( );
102 trayicon
= new KSystemTrayIcon ( parent
);
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();
150 KWiFi_Iface::slotChangeInterfaceChangeNotification ()
153 kDebug() << "slotChangeInterfaceChangeNotification ()" << endl
;
154 emit
interfaceChanged( this );
158 KWiFi_Iface::slotTXPowerChanged ( )
160 iface_ui
.disableRadio
->setChecked ( device
->get_txpower_disabled ( ) );
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
) );
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 ) );
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
;
191 if ( device
->getMaxBitrate ( ) < 0 )
192 max
= highestExperiencedSpeed
;
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
);
202 KWiFi_Iface::updateStatus ( )
204 switch ( device
->getDeviceState ( ) )
207 iface_ui
.connLabel
->setText ( i18n ( "Connected to network:" ) );
210 iface_ui
.connLabel
->setText ( i18n ( "Searching for network:" ) );
213 iface_ui
.connLabel
->setText ( i18n ( "Ad-hoc connection to:" ) );
216 iface_ui
.connLabel
->setText ( i18n ( "SSID:" ) );
219 iface_ui
.networkName
->setText ( device
->get_essid ( ) );
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
);
228 bool has_freq
= device
->get_device_freq ( freq
);
230 iface_ui
.frequency
->setText ( i18n ( "no information" ) );
233 if ( freq
<= 1000 ) // then the card reported its channels
234 iface_ui
.frequency
->setText ( QString ( "%1 [%2]" ).arg ( device
->toggleFreqChannel ( freq
) ).arg ( freq
) );
236 iface_ui
.frequency
->setText ( QString ( "%1 [%2]" ).arg ( freq
).arg ( device
->toggleFreqChannel ( freq
) ) );
240 int size
= 0, flags
= 0;
241 if ( device
->get_key ( key
, size
, flags
) )
243 iface_ui
.encryption
->setText ( i18n ( "off" ) );
245 iface_ui
.encryption
->setText ( i18n ( "active, %1 Bit", size
* 8 ) );
249 KWiFi_Iface::updateCardState ( CARDSTATES state
)
254 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::NO_CARD
);
257 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::ALL_ALONE
);
260 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::AP_CONNECT
);
263 iface_ui
.connTypePic
->setPixmap ( *KWiFiManagerApp::AD_HOC
);
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 )
275 if ( qualityClass
< 0 )
277 switch ( device
->getDeviceState ( ) )
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
] );
285 iface_ui
.strengthPic
->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC
[1] ) );
286 iface_ui
.strengthNumber
->setText ( "0" );
287 iface_ui
.category
->setText ( KWiFiManagerApp::strengthClassName
[1] );
290 iface_ui
.strengthPic
->setPixmap ( *( KWiFiManagerApp::STRENGTHPIC
[0] ) );
291 iface_ui
.strengthNumber
->setText ( i18n ( "N/A" ) );
292 iface_ui
.category
->setText ( KWiFiManagerApp::strengthClassName
[2] );
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 );
307 KWiFi_Iface::toggleStats ( )
309 if ( statsContainer
->isVisible ( ) )
311 statsContainer
->hide ( );
312 zentrallayout
->removeWidget ( statsContainer
);
313 statsContainer
->setParent ( 0 );
317 statsContainer
->setParent ( this );
318 zentrallayout
->addWidget ( statsContainer
, 1, 0 );
319 statsContainer
->show ( );
321 resize ( minimumSizeHint ( ) );
325 KWiFi_Iface::slotDisableRadio ( )
327 iface_ui
.disableRadio
->setChecked ( device
->get_txpower_disabled ( ) );
329 if ( disablePower
!= 0 && disablePower
->isRunning ( ) )
333 QString interface
= device
->get_interface_name ( );
335 if ( interface
.isEmpty ( ) )
341 if ( iface_ui
.disableRadio
->isChecked ( ) )
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
);
358 KWiFi_Iface::slotDisablePowerProcessExited ( )
360 if ( !disablePower
->normalExit ( ) || disablePower
->exitStatus ( ) != 0 )
367 if ( iface_ui
.disableRadio
->isChecked ( ) )
369 iface_ui
.disableRadio
->setChecked ( false );
373 iface_ui
.disableRadio
->setChecked ( true );
379 iface_ui
.disableRadio
->setChecked ( device
->get_txpower_disabled ( ) );
383 KWiFi_Iface::slotToggleTric ( )
385 if ( iface_ui
.tricorder
->isChecked ( ) )
387 tricorderTrigger
->start ( 250 );
391 tricorderTrigger
->stop ( );
396 KWiFi_Iface::tricorderBeep ( )
399 device
->get_current_quality ( sig
, noi
, qual
);
401 sine_wave ( 150.0 + qual
* 20 );
405 tricorderTrigger
->start ( 2000 );
407 else if ( qual
< 10 )
409 tricorderTrigger
->start ( 1000 );
413 tricorderTrigger
->start ( 500 );
417 #include "kwifi_ifaceimpl.moc"