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

networkedit.cpp

Go to the documentation of this file.
00001 #include <qtoolbutton.h>
00002 #include <qcheckbox.h>
00003 #include <qtabwidget.h>
00004 #include <qlineedit.h>
00005 #include <qlistbox.h>
00006 #include <GUIUtils.h>
00007 #include <resources.h>
00008 #include "networkedit.h"
00009 
00010 NetworkEdit::NetworkEdit( QWidget * Parent ) : NetworkGUI( Parent ){
00011 
00012     AddPreDown_TB->setPixmap( NSResources->getPixmap( "add" ) );
00013     AddPreUp_TB->setPixmap( NSResources->getPixmap( "add" ) );
00014     AddPostDown_TB->setPixmap( NSResources->getPixmap( "add" ) );
00015     AddPostUp_TB->setPixmap( NSResources->getPixmap( "add" ) );
00016     
00017     DeletePreDown_TB->setPixmap( NSResources->getPixmap( "remove" ) );
00018     DeletePreUp_TB->setPixmap( NSResources->getPixmap( "remove" ) );
00019     DeletePostDown_TB->setPixmap( NSResources->getPixmap( "remove" ) );
00020     DeletePostUp_TB->setPixmap( NSResources->getPixmap( "remove" ) );
00021 
00022     UpPreDown_TB->setPixmap( NSResources->getPixmap( "up" ) );
00023     UpPreUp_TB->setPixmap( NSResources->getPixmap( "up" ) );
00024     UpPostDown_TB->setPixmap( NSResources->getPixmap( "up" ) );
00025     UpPostUp_TB->setPixmap( NSResources->getPixmap( "up" ) );
00026 
00027     DownPreDown_TB->setPixmap( NSResources->getPixmap( "down" ) );
00028     DownPreUp_TB->setPixmap( NSResources->getPixmap( "down" ) );
00029     DownPostDown_TB->setPixmap( NSResources->getPixmap( "down" ) );
00030     DownPostUp_TB->setPixmap( NSResources->getPixmap( "down" ) );
00031 
00032 }
00033 
00034 QString NetworkEdit::acceptable( void ) {
00035     if( DHCP_CB->isChecked() ) {
00036       if( SendHostname_CB->isChecked() )
00037         if( Hostname_LE->text().isEmpty() ) 
00038           return tr("Hostname needed");
00039       return QString();
00040     }
00041 
00042     if( IPAddress_LE->text().isEmpty() )
00043       return tr("IPAddress needed");
00044     if( Broadcast_LE->text().isEmpty() )
00045       return tr("Broadcast needed");
00046     if( SubnetMask_LE->text().isEmpty() ) 
00047       return tr("Subnet mask needed");
00048 
00049     // valid IP ?
00050     if( ! validIP( IPAddress_LE->text() ) )
00051       return tr("IPAddress not valid");
00052     if( ! validIP( SubnetMask_LE->text() ) )
00053       return tr("Subnet mask not valid");
00054     if( ! validIP( Broadcast_LE->text() ) )
00055       return tr("Broadcast address not valid");
00056     if( Gateway_LE->text().isEmpty() ||
00057         ! validIP( Gateway_LE->text() ) )
00058       return tr("Gateway address not valid");
00059     if( ! DNS1_LE->text().isEmpty() &&
00060         ! validIP( DNS1_LE->text() ) )
00061       return tr("DNS1 address not valid");
00062     if( ! DNS2_LE->text().isEmpty() &&
00063         ! validIP( DNS2_LE->text() ) )
00064       return tr("DNS2 address not valid");
00065     return QString();
00066 }
00067 
00068 bool NetworkEdit::commit( NetworkData & Data ) {
00069     bool SM = 0;
00070     CBM( Data.UseDHCP, DHCP_CB, SM );
00071     TXTM( Data.IPAddress, IPAddress_LE, SM );
00072     CBM( Data.SendHostname, SendHostname_CB, SM );
00073     TXTM( Data.Hostname, Hostname_LE, SM );
00074     TXTM( Data.Gateway, Gateway_LE, SM );
00075     TXTM( Data.Broadcast, Broadcast_LE, SM );
00076     TXTM( Data.NetMask, SubnetMask_LE, SM );
00077     TXTM( Data.DNS1, DNS1_LE, SM );
00078     TXTM( Data.DNS2, DNS2_LE, SM );
00079 
00080     SM |= updateList( Data.PreUp_SL, PreUp_LB );
00081     SM |= updateList( Data.PostUp_SL, PostUp_LB );
00082     SM |= updateList( Data.PreDown_SL, PreDown_LB );
00083     SM |= updateList( Data.PostDown_SL, PostDown_LB );
00084 
00085     return SM;
00086 }
00087 
00088 void NetworkEdit::showData( NetworkData & Data ) {
00089     DHCP_CB->setChecked( Data.UseDHCP );
00090     SendHostname_CB->setChecked( Data.SendHostname );
00091     Hostname_LE->setText( Data.Hostname );
00092     IPAddress_LE->setText( Data.IPAddress );
00093     Gateway_LE->setText( Data.Gateway );
00094     SubnetMask_LE->setText( Data.NetMask );
00095     Broadcast_LE->setText( Data.Broadcast );
00096     DNS1_LE->setText( Data.DNS1 );
00097     DNS2_LE->setText( Data.DNS2 );
00098 
00099     populateList( Data.PreUp_SL, PreUp_LB );
00100     populateList( Data.PostUp_SL, PostUp_LB );
00101     populateList( Data.PreDown_SL, PreDown_LB );
00102     populateList( Data.PostDown_SL, PostDown_LB );
00103 }
00104 
00105 bool NetworkEdit::updateList( QStringList & SL, QListBox * LB ) {
00106     bool Changed;
00107     QStringList NewSL;
00108 
00109     // collect new list
00110     for( unsigned int i = 0; i < LB->count() ; i ++ ) {
00111       NewSL.append( LB->text(i) );
00112     }
00113 
00114     if( NewSL.count() != SL.count() ) {
00115       // less or more items
00116       SL= NewSL;
00117       return 1;
00118     }
00119 
00120     // Same size -> same content ?
00121     Changed = 0;
00122     for ( QStringList::Iterator it = NewSL.begin(); 
00123           it != NewSL.end(); 
00124           ++it ) {
00125       if( SL.findIndex( (*it) ) < 0 ) {
00126         // new or modified item
00127         Changed = 1;
00128         SL = NewSL;
00129         break;
00130       }
00131     }
00132     return Changed;
00133 }
00134 
00135 void NetworkEdit::populateList( QStringList & SL, QListBox * LB ) {
00136     LB->clear();
00137     for ( QStringList::Iterator it = SL.begin(); 
00138           it != SL.end(); 
00139           ++it ) {
00140       LB->insertItem( (*it) );
00141     }
00142 }
00143 
00144 void NetworkEdit::SLOT_NetmaskModified( const QString & ) {
00145     QString IP, SN;
00146     IP = IPAddress_LE->text();
00147     SN = SubnetMask_LE->text();
00148     if( IP.isEmpty() || SN.isEmpty() ) 
00149       return;
00150 
00151     if( ! validIP(IP) || ! validIP( SN ) )
00152       return;
00153 
00154     // if all ones
00155     // broadcast = (IPAddress | ~netmask )
00156     { QString NW;
00157       QStringList ipal = QStringList::split( '.', IP );
00158       QStringList nmal = QStringList::split( '.', SN );
00159 
00160       NW = QString( "%1.%2.%3.%4" ).
00161           arg( ipal[0].toShort() | ( ~ nmal[0].toShort() & 0x00ff) ).
00162           arg( ipal[1].toShort() | ( ~ nmal[1].toShort() & 0x00ff) ).
00163           arg( ipal[2].toShort() | ( ~ nmal[2].toShort() & 0x00ff) ).
00164           arg( ipal[3].toShort() | ( ~ nmal[3].toShort() & 0x00ff) );
00165       Broadcast_LE->setText( NW );
00166     }
00167 }
00168 
00169 QListBox * NetworkEdit::getActiveLB( void ) {
00170     switch( Tab_TAB->currentPageIndex() ) {
00171       case 0 :
00172         return PreUp_LB;
00173       case 1 :
00174         return PostUp_LB;
00175       case 2 :
00176         return PreDown_LB;
00177     }
00178     return PostDown_LB;
00179 }
00180 
00181 void NetworkEdit::SLOT_Add( void ) {
00182     if( Command_LE->text().isEmpty() )
00183       return;
00184     QListBox * LB = getActiveLB();
00185 
00186     LB->insertItem( Command_LE->text() );
00187 }
00188 
00189 void NetworkEdit::SLOT_Remove( void ) {
00190     QListBox * LB = getActiveLB();
00191     int i;
00192 
00193     if( ( i = LB->currentItem() ) >= 0 ) {
00194       LB->removeItem( i );
00195     }
00196 }
00197 
00198 void NetworkEdit::SLOT_Up( void ) {
00199     QListBox * LB = getActiveLB();
00200     int i;
00201 
00202     if( ( i = LB->currentItem() ) > 0 ) {
00203       QListBoxItem * LBI =  LB->item(i);
00204       LB->takeItem( LBI );
00205       LB->insertItem( LBI, --i );
00206       LB->setCurrentItem( i );
00207     }
00208 }
00209 
00210 void NetworkEdit::SLOT_Down( void ) {
00211     QListBox * LB = getActiveLB();
00212     int i;
00213 
00214     if( ( i = LB->currentItem() ) >= 0 && (unsigned)(i+1) != LB->count() ) {
00215       QListBoxItem * LBI =  LB->item(i);
00216       LB->takeItem( LBI );
00217       LB->insertItem( LBI, ++i );
00218       LB->setCurrentItem( i );
00219     }
00220 }
00221 
00222 void NetworkEdit::SLOT_ShowCommand( QListBoxItem * It ) {
00223     Command_LE->setText( It->text() );
00224 }

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