summaryrefslogtreecommitdiff
path: root/noncore/applets/networkapplet/networkapplet.cpp
Unidiff
Diffstat (limited to 'noncore/applets/networkapplet/networkapplet.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/applets/networkapplet/networkapplet.cpp252
1 files changed, 252 insertions, 0 deletions
diff --git a/noncore/applets/networkapplet/networkapplet.cpp b/noncore/applets/networkapplet/networkapplet.cpp
new file mode 100644
index 0000000..3019456
--- a/dev/null
+++ b/noncore/applets/networkapplet/networkapplet.cpp
@@ -0,0 +1,252 @@
1/*
2                 This file is part of the Opie Project
3
4 =. (C) 2003 Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de>
5 .=l.
6           .>+-=
7 _;:,     .>    :=|. This program is free software; you can
8.> <`_,   >  .   <= redistribute it and/or modify it under
9:`=1 )Y*s>-.--   : the terms of the GNU Library General Public
10.="- .-=="i,     .._ License as published by the Free Software
11 - .   .-<_>     .<> Foundation; either version 2 of the License,
12     ._= =}       : or (at your option) any later version.
13    .%`+i>       _;_.
14    .i_,=:_.      -<s. This program is distributed in the hope that
15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
16    : ..    .:,     . . . without even the implied warranty of
17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.=       =       ; Library General Public License for more
20++=   -.     .`     .: details.
21 :     =  ...= . :.=-
22 -.   .:....=;==+<; You should have received a copy of the GNU
23  -_. . .   )=.  = Library General Public License along with
24    --        :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA.
28
29*/
30
31#include "networkapplet.h"
32#include <opie2/otaskbarapplet.h>
33#include <qpe/qlibrary.h>
34#include <qpe/resource.h>
35#include <qpainter.h>
36#include <opie2/odebug.h>
37#include <opie2/onetwork.h>
38
39#include <qpushbutton.h>
40#include <qlabel.h>
41#include <qlayout.h>
42#include <qlineedit.h>
43#include <qtoolbutton.h>
44#include <qhostaddress.h>
45#include <qobjectlist.h>
46
47#include <assert.h>
48
49IfaceUpDownButton::IfaceUpDownButton( QWidget* parent, const char* name )
50 :QToolButton( parent, name )
51{
52 _iface = ONetwork::instance()->interface( name );
53 assert( _iface );
54 setToggleButton( true );
55 //setAutoRaise( true );
56 setOnIconSet( QIconSet( Resource::loadPixmap( "up" ) ) );
57 setOffIconSet( QIconSet( Resource::loadPixmap( "down" ) ) );
58 setOn( _iface->isUp() );
59 //setFixedWidth( 16 );
60 connect( this, SIGNAL( clicked() ), this, SLOT( clicked() ) );
61}
62
63
64IfaceUpDownButton::~IfaceUpDownButton()
65{
66}
67
68
69void IfaceUpDownButton::clicked()
70{
71 _iface->setUp( isOn() );
72 setOn( _iface->isUp() ); // it might not have worked...
73 repaint();
74}
75
76
77IfaceIPAddress::IfaceIPAddress( QWidget* parent, const char* name )
78 :QLineEdit( parent, name )
79{
80 setFont( QFont( "fixed" ) );
81 _iface = ONetwork::instance()->interface( name );
82 setFixedWidth( 105 );
83 setText( _iface->ipV4Address() );
84 connect( this, SIGNAL( returnPressed() ), this, SLOT( returnPressed() ) );
85}
86
87
88IfaceIPAddress::~IfaceIPAddress()
89{
90}
91
92
93void IfaceIPAddress::returnPressed()
94{
95 QHostAddress a;
96 a.setAddress( text() );
97 QHostAddress mask;
98 mask.setAddress( _iface->ipV4Netmask() ); // setIPV4Address destroys the netmask...
99 _iface->setIPV4Address( a );
100 _iface->setIPV4Netmask( mask ); // recover the old netmask
101 setText( _iface->ipV4Address() );
102 repaint();
103}
104
105
106NetworkAppletControl::NetworkAppletControl( OTaskbarApplet* parent, const char* name )
107 :QFrame( parent, name, WStyle_StaysOnTop | WType_Popup ), l(0)
108{
109 setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
110 l = new QVBoxLayout( this, 4, 2 );
111}
112
113
114void NetworkAppletControl::build()
115{
116 ONetwork::InterfaceIterator it = ONetwork::instance()->iterator();
117 while ( it.current() )
118 {
119 QHBoxLayout* h = new QHBoxLayout( l );
120 QLabel* symbol = new QLabel( this );
121 symbol->setPixmap( Resource::loadPixmap( guessDevice( it.current() ) ) );
122 h->addWidget( symbol );
123 symbol->show();
124
125 QLabel* name = new QLabel( it.current()->name(), this );
126 name->setFixedWidth( 35 );
127 h->addWidget( name );
128 name->show();
129
130 IfaceIPAddress* ip = new IfaceIPAddress( this, it.current()->name() );
131 h->addWidget( ip );
132 ip->show();
133
134 IfaceUpDownButton* tb = new IfaceUpDownButton( this, it.current()->name() );
135 tb->show();
136
137 h->addWidget( tb );
138
139 ++it;
140 }
141}
142
143
144NetworkAppletControl::~NetworkAppletControl()
145{
146}
147
148
149QString NetworkAppletControl::guessDevice( ONetworkInterface* iface )
150{
151 if ( iface->isWireless() )
152 return "networksettings/wlan";
153 if ( iface->isLoopback() )
154 return "networksettings/lo";
155 if ( QString( iface->name() ).contains( "usb" ) )
156 return "networksettings/usb";
157 if ( QString( iface->name() ).contains( "ir" ) )
158 return "networksettings/ir";
159
160 //TODO: Insert neat symbol and check for tunnel devices
161
162 return "networksettings/lan";
163
164}
165
166
167void NetworkAppletControl::showEvent( QShowEvent* e )
168{
169 qDebug( "showEvent" );
170 build();
171 QWidget::showEvent( e );
172}
173
174
175void NetworkAppletControl::hideEvent( QHideEvent* e )
176{
177 qDebug( "hideEvent" );
178 QWidget::hideEvent( e );
179
180 delete l;
181
182 // delete all child widgets from this frame
183 QObjectList* list = const_cast<QObjectList*>( children() );
184 QObjectListIt it(*list);
185 QObject* obj;
186 while ( (obj=it.current()) )
187 {
188 ++it;
189 delete obj;
190 }
191
192 list = const_cast<QObjectList*>( children() );
193 if ( list )
194 qWarning( "D'oh! We still have %d children...", list->count() );
195
196 // renew layout
197 l = new QVBoxLayout( this, 4, 2 );
198 resize( 0, 0 );
199}
200
201
202QSize NetworkAppletControl::sizeHint() const
203{
204 ONetwork::instance()->synchronize(); // rebuild interface database
205 qDebug( "sizeHint (#ifaces=%d)", ONetwork::instance()->count() );
206 return QSize( 14+35+105+14 + 8, ONetwork::instance()->count() * 26 );
207}
208
209
210NetworkApplet::NetworkApplet( QWidget *parent, const char *name )
211 :OTaskbarApplet( parent, name )
212{
213 _control = new NetworkAppletControl( this, "control" );
214}
215
216
217NetworkApplet::~NetworkApplet()
218{
219}
220
221
222int NetworkApplet::position()
223{
224 return 4;
225}
226
227
228void NetworkApplet::paintEvent( QPaintEvent* )
229{
230 QPainter p(this);
231 p.drawPixmap(0, 2, Resource::loadPixmap( "networkapplet/network" ) );
232}
233
234
235void NetworkApplet::mousePressEvent( QMouseEvent* )
236{
237 if ( !_control->isVisible() )
238 {
239 popup( _control );
240 }
241 else
242 {
243 _control->hide();
244 }
245}
246
247
248Q_EXPORT_INTERFACE()
249{
250 Q_CREATE_INSTANCE( OTaskbarAppletWrapper<NetworkApplet> );
251}
252