Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

networkapplet.cpp

Go to the documentation of this file.
00001 /*
00002                      This file is part of the Opie Project
00003 
00004               =.             (C) 2003 Michael 'Mickey' Lauer <mickey@tm.informatik.uni-frankfurt.de>
00005             .=l.
00006      .>+-=
00007 _;:,   .>  :=|.         This program is free software; you can
00008 .> <`_,  > .  <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--  :           the terms of the GNU Library General Public
00010 .="- .-=="i,   .._         License as published by the Free Software
00011 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00012   ._= =}    :          or (at your option) any later version.
00013   .%`+i>    _;_.
00014   .i_,=:_.   -<s.       This program is distributed in the hope that
00015   + . -:.    =       it will be useful,  but WITHOUT ANY WARRANTY;
00016   : ..  .:,   . . .    without even the implied warranty of
00017   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00018  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=    =    ;      Library General Public License for more
00020 ++=  -.   .`   .:       details.
00021 :   = ...= . :.=-
00022 -.  .:....=;==+<;          You should have received a copy of the GNU
00023  -_. . .  )=. =           Library General Public License along with
00024   --    :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 
00029 */
00030 
00031 #include "networkapplet.h"
00032 
00033 /* OPIE */
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 /* QT */
00045 #include <qpainter.h>
00046 #include <qlabel.h>
00047 #include <qlayout.h>
00048 #include <qobjectlist.h>
00049 
00050 /* STD */
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() ); // it might not have worked...
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() ); // setIPV4Address destroys the netmask...
00102     _iface->setIPV4Address( a );
00103     _iface->setIPV4Netmask( mask );           // recover the old netmask
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     //TODO: Insert neat symbol and check for tunnel devices
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     // delete all child widgets from this frame
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     // renew layout
00200     l = new QVBoxLayout( this, 4, 2 );
00201     resize( 0, 0 );
00202 }
00203 
00204 
00205 QSize NetworkAppletControl::sizeHint() const
00206 {
00207     ONetwork::instance()->synchronize(); // rebuild interface database
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 

Generated on Sat Nov 5 16:16:44 2005 for OPIE by  doxygen 1.4.2