Diffstat (limited to 'noncore/applets/wirelessapplet/wireless.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/applets/wirelessapplet/wireless.cpp | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/noncore/applets/wirelessapplet/wireless.cpp b/noncore/applets/wirelessapplet/wireless.cpp new file mode 100644 index 0000000..15ccc58 --- a/dev/null +++ b/noncore/applets/wirelessapplet/wireless.cpp | |||
@@ -0,0 +1,358 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2002 Michael 'Mickey' Lauer | ||
3 | ** <mickey@tm.informatik.uni-frankfurt.de> | ||
4 | ** http://www.Vanille.de | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | **********************************************************************/ | ||
15 | |||
16 | #include "wireless.h" | ||
17 | |||
18 | #include <qapplication.h> | ||
19 | #include <qpe/qpeapplication.h> | ||
20 | |||
21 | #include <qpoint.h> | ||
22 | #include <qradiobutton.h> | ||
23 | #include <qcheckbox.h> | ||
24 | #include <qpainter.h> | ||
25 | #include <qlabel.h> | ||
26 | #include <qslider.h> | ||
27 | #include <qbuttongroup.h> | ||
28 | #include <qlayout.h> | ||
29 | #include <qframe.h> | ||
30 | #include <qpixmap.h> | ||
31 | #include <qstring.h> | ||
32 | #include <qfile.h> | ||
33 | |||
34 | #include "networkinfo.h" | ||
35 | #include "mgraph.h" | ||
36 | |||
37 | #include "connect0.xpm" | ||
38 | #include "connect1.xpm" | ||
39 | #include "connect2.xpm" | ||
40 | #include "connect3.xpm" | ||
41 | #include "connect4.xpm" | ||
42 | #include "connect5.xpm" | ||
43 | #include "nowireless.xpm" | ||
44 | |||
45 | #define STYLE_BARS 0 | ||
46 | #define STYLE_ANTENNA 1 | ||
47 | |||
48 | WirelessControl::WirelessControl( WirelessApplet *applet, QWidget *parent, const char *name ) | ||
49 | : QFrame( parent, name, WStyle_StaysOnTop | WType_Popup ), applet( applet ) | ||
50 | { | ||
51 | |||
52 | setFrameStyle( QFrame::PopupPanel | QFrame::Raised ); | ||
53 | QGridLayout *grid = new QGridLayout( this, 3, 2, 6, 2, "top layout" ); | ||
54 | |||
55 | /* status label */ | ||
56 | |||
57 | statusLabel = new QLabel( this, "statuslabel" ); | ||
58 | QString text( "Wireless Status:<br>" | ||
59 | "*** Unknown ***<br>" | ||
60 | "Card not inserted ?<br>" | ||
61 | "Or Sharp ROM ?<br>" | ||
62 | "CELL: 00:00:00:00:00:00" ); | ||
63 | /* QString text( "Station: Unknown<br>" | ||
64 | "ESSID: Unknown<br>" | ||
65 | "MODE: Unknown<br>" | ||
66 | "FREQ: Unknown<br>" | ||
67 | "CELL: AA:BB:CC:DD:EE:FF" ); */ | ||
68 | statusLabel->setText( text ); | ||
69 | statusLabel->setFixedSize( statusLabel->sizeHint() ); | ||
70 | grid->addWidget( statusLabel, 0, 0 ); | ||
71 | |||
72 | /* visualization group box */ | ||
73 | |||
74 | QButtonGroup* group = new QButtonGroup( 1, Qt::Horizontal, "Visualization", this ); | ||
75 | QRadioButton* r1 = new QRadioButton( "Color Bars", group ); | ||
76 | QRadioButton* r2 = new QRadioButton( "Antenna", group ); | ||
77 | r1->setFocusPolicy( QWidget::NoFocus ); | ||
78 | r2->setFocusPolicy( QWidget::NoFocus ); | ||
79 | group->setFocusPolicy( QWidget::NoFocus ); | ||
80 | group->setButton( STYLE_ANTENNA ); | ||
81 | grid->addWidget( group, 0, 1 ); | ||
82 | |||
83 | /* quality graph */ | ||
84 | |||
85 | mgraph = new MGraph( this ); | ||
86 | mgraph->setFrameStyle( QFrame::Panel | QFrame::Sunken ); | ||
87 | mgraph->setMin( 0 ); | ||
88 | mgraph->setMax( 92 ); | ||
89 | grid->addWidget( mgraph, 1, 0 ); | ||
90 | mgraph->setFocusPolicy( QWidget::NoFocus ); | ||
91 | |||
92 | /* dhcp renew CheckBox */ | ||
93 | |||
94 | //FIXME: under construction | ||
95 | //QCheckBox* dhcpCheckBox = new QCheckBox( "DHCP renew", this ); | ||
96 | //dhcpCheckBox->setFocusPolicy( QWidget::NoFocus ); | ||
97 | //grid->addWidget( dhcpCheckBox, 2, 0, Qt::AlignCenter ); | ||
98 | |||
99 | /* update Frequency Label */ | ||
100 | |||
101 | updateLabel = new QLabel( this ); | ||
102 | updateLabel->setText( "Update every 500 ms" ); | ||
103 | grid->addWidget( updateLabel, 2, 1 ); | ||
104 | |||
105 | /* update Frequency Slider */ | ||
106 | |||
107 | QSlider* updateSlider = new QSlider( QSlider::Horizontal, this ); | ||
108 | updateSlider->setRange( 50, 999 ); | ||
109 | updateSlider->setValue( 500 ); | ||
110 | updateSlider->setTickmarks( QSlider::Both ); | ||
111 | updateSlider->setTickInterval( 100 ); | ||
112 | updateSlider->setSteps( 50, 50 ); | ||
113 | updateSlider->setFocusPolicy( QWidget::NoFocus ); | ||
114 | grid->addWidget( updateSlider, 1, 1 ); | ||
115 | connect( updateSlider, SIGNAL( valueChanged( int ) ), | ||
116 | this, SLOT( updateDelayChange( int ) ) ); | ||
117 | |||
118 | setFixedSize( sizeHint() ); | ||
119 | setFocusPolicy( QWidget::NoFocus ); | ||
120 | |||
121 | connect( group, SIGNAL( clicked( int ) ), | ||
122 | applet, SLOT( styleChange( int ) ) ); | ||
123 | |||
124 | } | ||
125 | |||
126 | void WirelessControl::updateDelayChange( int delay ) | ||
127 | { | ||
128 | QString text; | ||
129 | text.sprintf( "Update every %3d ms", delay ); | ||
130 | updateLabel->setText( text ); | ||
131 | applet->updateDelayChange( delay ); | ||
132 | } | ||
133 | |||
134 | void WirelessControl::show ( bool ) | ||
135 | { | ||
136 | QPoint curPos = applet->mapToGlobal( QPoint ( 0, 0 ) ); | ||
137 | |||
138 | int w = sizeHint().width(); | ||
139 | int x = curPos.x() - ( w / 2 ); | ||
140 | |||
141 | if ( ( x + w ) > QPEApplication::desktop()->width() ) | ||
142 | x = QPEApplication::desktop ( )-> width ( ) - w; | ||
143 | |||
144 | move( x, curPos.y () - sizeHint().height () ); | ||
145 | QFrame::show(); | ||
146 | } | ||
147 | |||
148 | |||
149 | //=========================================================================== | ||
150 | |||
151 | WirelessApplet::WirelessApplet( QWidget *parent, const char *name ) | ||
152 | : QWidget( parent, name ), visualStyle( STYLE_ANTENNA ), interface( 0 ) | ||
153 | { | ||
154 | setFixedHeight( 18 ); | ||
155 | setFixedWidth( 14 ); | ||
156 | status = new WirelessControl( this, 0, "wireless status" ); | ||
157 | |||
158 | network = new MWirelessNetwork(); | ||
159 | |||
160 | timer = startTimer( 500 ); | ||
161 | } | ||
162 | |||
163 | void WirelessApplet::checkInterface() | ||
164 | { | ||
165 | interface = network->getFirstInterface(); | ||
166 | if ( interface ) | ||
167 | { | ||
168 | qDebug( "WIFIAPPLET: using interface '%s'", (const char*) | ||
169 | interface->getName() ); | ||
170 | } | ||
171 | else | ||
172 | { | ||
173 | qDebug( "WIFIAPPLET: D'oh! No Wireless interface present... :(" ); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | void WirelessApplet::updateDelayChange( int delay ) | ||
178 | { | ||
179 | killTimer( timer ); | ||
180 | timer = startTimer( delay ); | ||
181 | } | ||
182 | |||
183 | WirelessApplet::~WirelessApplet() | ||
184 | { | ||
185 | } | ||
186 | |||
187 | void WirelessApplet::styleChange( int style ) | ||
188 | { | ||
189 | visualStyle = style; | ||
190 | repaint(); | ||
191 | } | ||
192 | |||
193 | void WirelessApplet::timerEvent( QTimerEvent* ) | ||
194 | { | ||
195 | MWirelessNetworkInterface* iface = ( MWirelessNetworkInterface* ) interface; | ||
196 | |||
197 | if ( iface ) | ||
198 | { | ||
199 | iface->updateStatistics(); | ||
200 | if ( mustRepaint() ) | ||
201 | { | ||
202 | //qDebug( "WIFIAPPLET: A value has changed -> repainting." ); | ||
203 | repaint(); | ||
204 | } | ||
205 | |||
206 | if ( status->isVisible() ) | ||
207 | updatePopupWindow(); | ||
208 | } else checkInterface(); | ||
209 | } | ||
210 | |||
211 | void WirelessApplet::mousePressEvent( QMouseEvent *) | ||
212 | { | ||
213 | if ( status->isVisible() ) | ||
214 | status->hide(); | ||
215 | else | ||
216 | status->show( true ); | ||
217 | } | ||
218 | |||
219 | bool WirelessApplet::mustRepaint() | ||
220 | { | ||
221 | MWirelessNetworkInterface* iface = ( MWirelessNetworkInterface* ) interface; | ||
222 | |||
223 | // check if there are enough changes to justify a (flickering) repaint | ||
224 | |||
225 | // has the interface changed? | ||
226 | |||
227 | if ( iface != oldiface ) | ||
228 | { | ||
229 | oldiface = iface; | ||
230 | return true; | ||
231 | } | ||
232 | |||
233 | const char** pixmap = getQualityPixmap(); | ||
234 | |||
235 | if ( pixmap && ( pixmap != oldpixmap ) ) | ||
236 | { | ||
237 | oldpixmap = pixmap; | ||
238 | return true; | ||
239 | } | ||
240 | |||
241 | int noiseH = iface->noisePercent() * ( height() - 3 ) / 100; | ||
242 | int signalH = iface->signalPercent() * ( height() - 3 ) / 100; | ||
243 | int qualityH = iface->qualityPercent() * ( height() - 3 ) / 100; | ||
244 | |||
245 | if ( ( noiseH != oldnoiseH ) | ||
246 | || ( signalH != oldsignalH ) | ||
247 | || ( qualityH != oldqualityH ) ) | ||
248 | { | ||
249 | oldnoiseH = noiseH; | ||
250 | oldsignalH = signalH; | ||
251 | oldqualityH = qualityH; | ||
252 | return true; | ||
253 | } | ||
254 | |||
255 | return false; | ||
256 | } | ||
257 | |||
258 | void WirelessApplet::updatePopupWindow() | ||
259 | { | ||
260 | MWirelessNetworkInterface* iface = ( MWirelessNetworkInterface* ) interface; | ||
261 | int qualityH = iface->qualityPercent(); | ||
262 | |||
263 | if ( status->mgraph ) | ||
264 | status->mgraph->addValue( qualityH, false ); | ||
265 | |||
266 | QString freqString; | ||
267 | QString cell = ( iface->mode == "Managed" ) ? "AP: " : "Cell: "; | ||
268 | freqString.sprintf( "%.3f GHz", iface->freq ); | ||
269 | status->statusLabel->setText( "Station: " + iface->nick + "<br>" + | ||
270 | "ESSID: " + iface->essid + "<br>" + | ||
271 | "MODE: " + iface->mode + "<br>" + | ||
272 | "FREQ: " + freqString + "<br>" + | ||
273 | cell + " " + iface->APAddr ); | ||
274 | } | ||
275 | |||
276 | const char** WirelessApplet::getQualityPixmap() | ||
277 | { | ||
278 | MWirelessNetworkInterface* iface = ( MWirelessNetworkInterface* ) interface; | ||
279 | |||
280 | if ( !iface ) return ( const char** ) nowireless_xpm; | ||
281 | int qualityH = iface->qualityPercent(); | ||
282 | if ( qualityH < 0 ) return ( const char** ) nowireless_xpm; | ||
283 | |||
284 | if ( visualStyle == STYLE_ANTENNA ) | ||
285 | { | ||
286 | if ( qualityH < 1 ) return ( const char** ) connect0_xpm; | ||
287 | if ( qualityH < 17 ) return ( const char** ) connect1_xpm; | ||
288 | if ( qualityH < 34 ) return ( const char** ) connect2_xpm; | ||
289 | if ( qualityH < 50 ) return ( const char** ) connect3_xpm; | ||
290 | if ( qualityH < 65 ) return ( const char** ) connect4_xpm; | ||
291 | return ( const char** ) connect5_xpm; | ||
292 | } | ||
293 | |||
294 | return 0; // please draw your bars | ||
295 | } | ||
296 | |||
297 | void WirelessApplet::paintEvent( QPaintEvent* ) | ||
298 | { | ||
299 | MWirelessNetworkInterface* iface = ( MWirelessNetworkInterface* ) interface; | ||
300 | |||
301 | QPainter p(this); | ||
302 | QColor color; | ||
303 | |||
304 | const char** pixmap = getQualityPixmap(); | ||
305 | |||
306 | if ( pixmap ) | ||
307 | p.drawPixmap( 0, 1, pixmap ); | ||
308 | else | ||
309 | { | ||
310 | |||
311 | int noiseH = iface->noisePercent() * ( height() - 3 ) / 100; | ||
312 | int signalH = iface->signalPercent() * ( height() - 3 ) / 100; | ||
313 | int qualityH = iface->qualityPercent() * ( height() - 3 ) / 100; | ||
314 | |||
315 | double intensity; | ||
316 | int pixelHeight; | ||
317 | int pixelWidth = 2; | ||
318 | int Hue; | ||
319 | int barSpace = 3; | ||
320 | int leftoffset = 0; | ||
321 | int bottomoffset = 2; | ||
322 | |||
323 | // draw noise indicator | ||
324 | pixelHeight = noiseH; | ||
325 | Hue = 50; | ||
326 | for ( int i = 0; i < pixelHeight; ++i ) | ||
327 | { | ||
328 | intensity = 50 + ( (double) i / (double) pixelHeight ) * 205; | ||
329 | color.setHsv( Hue, 255, intensity ); | ||
330 | p.setPen ( color ); | ||
331 | p.drawLine( leftoffset, height()-bottomoffset-i, pixelWidth+leftoffset, height()-bottomoffset-i ); | ||
332 | } | ||
333 | |||
334 | // draw signal indicator | ||
335 | pixelHeight = signalH; | ||
336 | Hue = 100; | ||
337 | leftoffset += pixelWidth + barSpace; | ||
338 | for ( int i = 0; i < pixelHeight; ++i ) | ||
339 | { | ||
340 | intensity = 50 + ( (double) i / (double) pixelHeight ) * 205; | ||
341 | color.setHsv( Hue, 255, intensity ); | ||
342 | p.setPen ( color ); | ||
343 | p.drawLine( leftoffset, height()-bottomoffset-i, pixelWidth+leftoffset, height()-bottomoffset-i ); | ||
344 | } | ||
345 | |||
346 | // draw quality indicator | ||
347 | pixelHeight = qualityH; | ||
348 | Hue = 250; | ||
349 | leftoffset += pixelWidth + barSpace; | ||
350 | for ( int i = 0; i < pixelHeight; ++i ) | ||
351 | { | ||
352 | intensity = 50 + ( (double) i / (double) pixelHeight ) * 205; | ||
353 | color.setHsv( Hue, 255, intensity ); | ||
354 | p.setPen ( color ); | ||
355 | p.drawLine( leftoffset, height()-bottomoffset-i, pixelWidth+leftoffset, height()-bottomoffset-i ); | ||
356 | } | ||
357 | } | ||
358 | } | ||