00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "networkapplet.h"
00032
00033
00034 #include <opie2/odebug.h>
00035 #include <opie2/onetwork.h>
00036 #include <opie2/oresource.h>
00037 #include <opie2/otaskbarapplet.h>
00038 #include <qpe/applnk.h>
00039 #include <qpe/qpeapplication.h>
00040 using namespace Opie::Core;
00041 using namespace Opie::Ui;
00042 using namespace Opie::Net;
00043
00044
00045 #include <qpainter.h>
00046 #include <qlabel.h>
00047 #include <qlayout.h>
00048 #include <qobjectlist.h>
00049
00050
00051 #include <assert.h>
00052
00053 IfaceUpDownButton::IfaceUpDownButton( QWidget* parent, const char* name )
00054 :QToolButton( parent, name )
00055 {
00056 _iface = ONetwork::instance()->interface( name );
00057 assert( _iface );
00058 setToggleButton( true );
00059 setUsesBigPixmap( qApp->desktop()->size().width() > 330 );
00060 setOnIconSet( QIconSet( Opie::Core::OResource::loadPixmap( "up", Opie::Core::OResource::SmallIcon ) ) );
00061 setOffIconSet( QIconSet( Opie::Core::OResource::loadPixmap( "down", Opie::Core::OResource::SmallIcon ) ) );
00062 setOn( _iface->isUp() );
00063 connect( this, SIGNAL( clicked() ), this, SLOT( clicked() ) );
00064 }
00065
00066
00067 IfaceUpDownButton::~IfaceUpDownButton()
00068 {
00069 }
00070
00071
00072 void IfaceUpDownButton::clicked()
00073 {
00074 _iface->setUp( isOn() );
00075 setOn( _iface->isUp() );
00076 repaint();
00077 }
00078
00079
00080 IfaceIPAddress::IfaceIPAddress( QWidget* parent, const char* name )
00081 :QLineEdit( parent, name )
00082 {
00083 setFont( QFont( "fixed" ) );
00084 _iface = ONetwork::instance()->interface( name );
00085 setFixedWidth( 105 );
00086 setText( _iface->ipV4Address().toString() );
00087 connect( this, SIGNAL( returnPressed() ), this, SLOT( returnPressed() ) );
00088 }
00089
00090
00091 IfaceIPAddress::~IfaceIPAddress()
00092 {
00093 }
00094
00095
00096 void IfaceIPAddress::returnPressed()
00097 {
00098 QHostAddress a;
00099 a.setAddress( text() );
00100 QHostAddress mask;
00101 mask.setAddress( _iface->ipV4Netmask().toString() );
00102 _iface->setIPV4Address( a );
00103 _iface->setIPV4Netmask( mask );
00104 setText( _iface->ipV4Address().toString() );
00105 repaint();
00106 }
00107
00108
00109 NetworkAppletControl::NetworkAppletControl( OTaskbarApplet* parent, const char* name )
00110 :QFrame( parent, name, WStyle_StaysOnTop | WType_Popup ), l(0)
00111 {
00112 setFrameStyle( QFrame::PopupPanel | QFrame::Raised );
00113 l = new QVBoxLayout( this, 4, 2 );
00114 }
00115
00116
00117 void NetworkAppletControl::build()
00118 {
00119 ONetwork::InterfaceIterator it = ONetwork::instance()->iterator();
00120 while ( it.current() )
00121 {
00122 QHBoxLayout* h = new QHBoxLayout( l );
00123 QLabel* symbol = new QLabel( this );
00124 symbol->setPixmap( Opie::Core::OResource::loadPixmap( guessDevice( it.current() ), Opie::Core::OResource::SmallIcon ) );
00125 h->addWidget( symbol );
00126 symbol->show();
00127
00128 QLabel* name = new QLabel( it.current()->name(), this );
00129 name->setFixedWidth( 35 );
00130 h->addWidget( name );
00131 name->show();
00132
00133 IfaceIPAddress* ip = new IfaceIPAddress( this, it.current()->name() );
00134 h->addWidget( ip );
00135 ip->show();
00136
00137 IfaceUpDownButton* tb = new IfaceUpDownButton( this, it.current()->name() );
00138 tb->show();
00139
00140 h->addWidget( tb );
00141
00142 ++it;
00143 }
00144 }
00145
00146
00147 NetworkAppletControl::~NetworkAppletControl()
00148 {
00149 }
00150
00151
00152 QString NetworkAppletControl::guessDevice( ONetworkInterface* iface )
00153 {
00154 if ( iface->isWireless() )
00155 return "networksettings/wlan";
00156 if ( iface->isLoopback() )
00157 return "networksettings/lo";
00158 if ( QString( iface->name() ).contains( "usb" ) )
00159 return "networksettings/usb";
00160 if ( QString( iface->name() ).contains( "ir" ) )
00161 return "networksettings/irda";
00162
00163
00164
00165 return "networksettings/lan";
00166
00167 }
00168
00169
00170 void NetworkAppletControl::showEvent( QShowEvent* e )
00171 {
00172 odebug << "showEvent" << oendl;
00173 build();
00174 QWidget::showEvent( e );
00175 }
00176
00177
00178 void NetworkAppletControl::hideEvent( QHideEvent* e )
00179 {
00180 odebug << "hideEvent" << oendl;
00181 QWidget::hideEvent( e );
00182
00183 delete l;
00184
00185
00186 QObjectList* list = const_cast<QObjectList*>( children() );
00187 QObjectListIt it(*list);
00188 QObject* obj;
00189 while ( (obj=it.current()) )
00190 {
00191 ++it;
00192 delete obj;
00193 }
00194
00195 list = const_cast<QObjectList*>( children() );
00196 if ( list )
00197 owarn << "D'oh! We still have " << list->count() << " children..." << oendl;
00198
00199
00200 l = new QVBoxLayout( this, 4, 2 );
00201 resize( 0, 0 );
00202 }
00203
00204
00205 QSize NetworkAppletControl::sizeHint() const
00206 {
00207 ONetwork::instance()->synchronize();
00208 odebug << "sizeHint (#ifaces=" << ONetwork::instance()->count() << ")" << oendl;
00209 return QSize( 14+35+105+14 + 8, ONetwork::instance()->count() * 26 );
00210 }
00211
00212
00213 NetworkApplet::NetworkApplet( QWidget *parent, const char *name )
00214 :OTaskbarApplet( parent, name )
00215 {
00216 setFixedHeight( AppLnk::smallIconSize() );
00217 setFixedWidth( AppLnk::smallIconSize() );
00218 _pixmap = Opie::Core::OResource::loadPixmap( "networkapplet/network", Opie::Core::OResource::SmallIcon );
00219 _control = new NetworkAppletControl( this, "control" );
00220 }
00221
00222
00223 NetworkApplet::~NetworkApplet()
00224 {
00225 }
00226
00227
00228 int NetworkApplet::position()
00229 {
00230 return 4;
00231 }
00232
00233
00234 void NetworkApplet::paintEvent( QPaintEvent* )
00235 {
00236 QPainter p(this);
00237 p.drawPixmap(0, 2, _pixmap );
00238 }
00239
00240
00241 void NetworkApplet::mousePressEvent( QMouseEvent* )
00242 {
00243 if ( !_control->isVisible() )
00244 {
00245 popup( _control );
00246 }
00247 else
00248 {
00249 _control->hide();
00250 }
00251 }
00252
00253 EXPORT_OPIE_APPLET_v1( NetworkApplet )
00254
00255