00001 #include <stdio.h>
00002 #include <unistd.h>
00003 #include <errno.h>
00004
00005 #include <opie2/odebug.h>
00006 #include <opie2/oledbox.h>
00007
00008 #include <qpe/qpeapplication.h>
00009 #include <qlistbox.h>
00010 #include <qlayout.h>
00011 #include <qgroupbox.h>
00012 #include <qtimer.h>
00013 #include <qlistbox.h>
00014 #include <qmessagebox.h>
00015 #include <qlabel.h>
00016 #include <qiconview.h>
00017 #include <qtimer.h>
00018 #include <qpe/qpeapplication.h>
00019 #include <qtoolbutton.h>
00020 #include <qevent.h>
00021
00022 #include "networksettings.h"
00023 #include "netnode.h"
00024 #include "editconnection.h"
00025
00026 NetworkSettings::NetworkSettings( QWidget *parent,
00027 const char *name,
00028 WFlags fl ) : NetworkSettingsGUI(parent,name,fl),
00029 NSD() {
00030
00031 UpdateTimer = new QTimer( this );
00032
00033
00034 Add_TB->setPixmap( NSResources->getPixmap( "add" ) );
00035 Delete_TB->setPixmap( NSResources->getPixmap( "remove" ) );
00036 CheckState_TB->setPixmap( NSResources->getPixmap( "check" ) );
00037 GenConfig_TB->setPixmap( NSResources->getPixmap( "configure" ) );
00038
00039 Disable_TB->setPixmap( NSResources->getPixmap( "disabled" ) );
00040 Up_TB->setPixmap( NSResources->getPixmap( "more" ) );
00041 Down_TB->setPixmap( NSResources->getPixmap( "less" ) );
00042
00043 QVBoxLayout* V = new QVBoxLayout( LED_Frm );
00044 QHBoxLayout * H = new QHBoxLayout( 0 );
00045 V->addStretch(1);
00046 V->addLayout( H );
00047 Leds[0] = new Opie::Ui::OLedBox( red, LED_Frm );
00048 H->addWidget( Leds[0], 0, Qt::AlignVCenter );
00049 Leds[1] = new Opie::Ui::OLedBox( red, LED_Frm );
00050 H->addWidget( Leds[1], 0, Qt::AlignVCenter );
00051 Leds[2] = new Opie::Ui::OLedBox( red, LED_Frm );
00052 H->addWidget( Leds[2], 0, Qt::AlignVCenter );
00053 V->addStretch(1);
00054
00055
00056 SLOT_ToProfile();
00057
00058
00059 Profiles_LB->clear();
00060 QPEApplication::setStylusOperation(
00061 Profiles_LB->viewport(), QPEApplication::RightOnHold );
00062
00063 connect( Profiles_LB,
00064 SIGNAL(rightButtonPressed(QListBoxItem*,const QPoint&)),
00065 this, SLOT(SLOT_EditNode(QListBoxItem*)) );
00066
00067 { Name2NetworkSetup_t & M = NSResources->networkSetups();
00068 NetworkSetup * NC;
00069
00070 for( QDictIterator<NetworkSetup> it(M);
00071 it.current();
00072 ++it ) {
00073 NC = it.current();
00074 Profiles_LB->insertItem( NC->devicePixmap(),
00075 NC->name() );
00076 }
00077 }
00078
00079 if( Profiles_LB->count() ) {
00080 Profiles_LB->setSelected( 0, TRUE );
00081 }
00082
00083
00084 if( NSResources->networkSetups().count() == 0 ) {
00085 QTimer::singleShot( 100, this, SLOT(SLOT_AddNode() ) );
00086 }
00087
00088 connect( &(NSResources->system()),
00089 SIGNAL( stdoutLine(const QString &) ),
00090 this, SLOT( SLOT_CmdMessage(const QString &) ) );
00091
00092 connect( &(NSResources->system()),
00093 SIGNAL( stderrLine(const QString &) ),
00094 this, SLOT( SLOT_CmdMessage(const QString &) ) );
00095
00096 connect( &(NSResources->system()),
00097 SIGNAL( processEvent(const QString &) ),
00098 this, SLOT( SLOT_CmdMessage(const QString &) ) );
00099
00100 UpdateTimer->start( 5000 );
00101 connect( UpdateTimer, SIGNAL( timeout() ),
00102 this, SLOT( SLOT_RefreshStates() ) );
00103
00104
00105 connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)),
00106 this, SLOT(SLOT_QCopMessage(const QCString&,const QByteArray&)) );
00107 }
00108
00109 NetworkSettings::~NetworkSettings() {
00110 QString S;
00111
00112 if( NSD.isModified() ) {
00113 S = NSD.saveSettings();
00114 if( ! S.isEmpty() ) {
00115 S.insert( 0, "<p>" );
00116 S.append( "</p>" );
00117
00118 QMessageBox::warning(
00119 0,
00120 tr( "Saving setup" ), S );
00121 }
00122
00123 SLOT_GenerateConfig();
00124 NSD.setModified( 0 );
00125 }
00126
00127 }
00128
00129 void NetworkSettings::SLOT_CmdMessage( const QString & S ) {
00130 Messages_LB->insertItem( S );
00131 Messages_LB->setCurrentItem( Messages_LB->count()-1 );
00132 Messages_LB->ensureCurrentVisible();
00133 }
00134
00135 void NetworkSettings::SLOT_RefreshStates( void ) {
00136 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00137
00138 if( LBI ) {
00139 NetworkSetup * NC;
00140 NSResources->system().probeInterfaces();
00141
00142 NC = NSResources->findNetworkSetup( LBI->text() );
00143 if( NC ) {
00144 State_t OldS = NC->state();
00145 State_t NewS = NC->state(1);
00146 if( OldS != NewS ) {
00147 updateProfileState( LBI );
00148 }
00149 }
00150 }
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 }
00177
00178 void NetworkSettings::SLOT_AddNode( void ) {
00179 SLOT_EditNode( 0 );
00180 }
00181
00182 void NetworkSettings::SLOT_DeleteNode( void ) {
00183 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00184
00185 if ( ! LBI )
00186 return;
00187
00188 if( QMessageBox::warning(
00189 0,
00190 tr( "Removing profile" ),
00191 tr( "Remove selected profile ?" ),
00192 1, 0 ) == 1 ) {
00193 NSResources->removeNetworkSetup( LBI->text() );
00194 delete LBI;
00195 NSD.setModified( 1 );
00196 }
00197 }
00198
00199 void NetworkSettings::SLOT_EditNode( QListBoxItem * LBI ) {
00200 QString OldName = "";
00201
00202 EditNetworkSetup EC( this );
00203
00204 if( LBI ) {
00205 NetworkSetup * NC = NSResources->findNetworkSetup( LBI->text() );
00206 if( ! NC ) {
00207 return;
00208 }
00209 OldName = NC->name();
00210 EC.setNetworkSetup( NC );
00211 }
00212
00213 EC.showMaximized();
00214
00215 UpdateTimer->stop();
00216
00217
00218 while( 1 ) {
00219 if( EC.exec() == QDialog::Accepted ) {
00220
00221 NetworkSetup * NC = EC.networkSetup();
00222 if( NC->isModified() ) {
00223 if( LBI ) {
00224 if( NC->name() != OldName ) {
00225
00226 NetworkSetup * LCN = NSResources->findNetworkSetup(
00227 NC->name() );
00228 if( LCN ) {
00229 QMessageBox::warning(
00230 0,
00231 tr( "In System Config" ),
00232 tr( "Name %1 already exists" ).arg(NC->name())
00233 );
00234 continue;
00235 }
00236
00237 NSResources->removeNetworkSetup( OldName );
00238 NSResources->addNetworkSetup( NC, 0 );
00239 }
00240
00241
00242 Profiles_LB->blockSignals( TRUE );
00243 Profiles_LB->changeItem( NC->devicePixmap(),
00244 NC->name(),
00245 Profiles_LB->index( LBI )
00246 );
00247 Profiles_LB->blockSignals( FALSE );
00248 } else {
00249
00250 int ci = Profiles_LB->count();
00251 NSResources->addNetworkSetup( NC, 0 );
00252 NC->setNumber( NSResources->assignNetworkSetupNumber() );
00253 Profiles_LB->insertItem( NC->devicePixmap(), NC->name() );
00254 Profiles_LB->setSelected( ci, TRUE );
00255 }
00256 SLOT_RefreshStates();
00257 }
00258 } else {
00259
00260 if( LBI ) {
00261 NetworkSetup * NC = NSResources->findNetworkSetup( LBI->text() );
00262 NC->reassign();
00263 }
00264 }
00265 break;
00266 }
00267
00268 UpdateTimer->start( 5000 );
00269 }
00270
00271 void NetworkSettings::SLOT_ShowNode( QListBoxItem * LBI ) {
00272 if( LBI == 0 )
00273 return;
00274
00275 NetworkSetup * NC = NSResources->findNetworkSetup( LBI->text() );
00276
00277 if( NC->description().isEmpty() ) {
00278 Description_LBL->setText( tr( "<<No description>>" ) );
00279 } else {
00280 Description_LBL->setText( NC->description() );
00281 }
00282
00283 Profile_GB->setTitle( LBI->text() + " : " + NC->stateName() );
00284
00285 bool FrmActive = 1;
00286 bool IsEnabled = 1;
00287 int leds = 0;
00288
00289 switch( NC->state() ) {
00290 case Disabled :
00291 IsEnabled = 0;
00292 FrmActive = 0;
00293 break;
00294 case Unknown :
00295 case Unchecked :
00296 case Unavailable :
00297 FrmActive = 0;
00298 break;
00299 case Off :
00300 leds = 1;
00301 break;
00302 case Available :
00303 leds = 2;
00304 break;
00305 case IsUp :
00306 leds = 3;
00307 break;
00308 }
00309
00310 Disable_TB->setOn( ! IsEnabled );
00311 LED_Frm->setEnabled( FrmActive );
00312
00313 for( int i = 0 ; i < leds; i ++ ) {
00314 Leds[i]->setColor( red );
00315 Leds[i]->setOn( true );
00316 }
00317 for( int i = leds ; i < 3; i ++ ) {
00318 Leds[i]->setColor( red );
00319 Leds[i]->setOn( false );
00320 }
00321
00322 Up_TB->setEnabled( leds < 3 && leds != 0 );
00323 Down_TB->setEnabled( leds > 0 );
00324 }
00325
00326 void NetworkSettings::SLOT_CheckState( void ) {
00327 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00328 if ( ! LBI )
00329 return;
00330 updateProfileState( LBI );
00331 }
00332
00333 void NetworkSettings::updateProfileState( QListBoxItem * LBI ) {
00334 if( LBI == Profiles_LB->item( Profiles_LB->currentItem() ) ) {
00335 SLOT_ShowNode( LBI );
00336 }
00337 }
00338
00339 void NetworkSettings::SLOT_GenerateConfig( void ) {
00340 QString S = NSD.generateSettings();
00341 if( ! S.isEmpty() ) {
00342 S.insert( 0, "<p>" );
00343 S.append( "</p>" );
00344 QMessageBox::warning(
00345 0,
00346 tr( "Generate config" ),
00347 S);
00348 }
00349 }
00350
00351 void NetworkSettings::SLOT_Disable( bool T ) {
00352 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00353 QString Msg;
00354
00355 if ( ! LBI )
00356 return;
00357
00358 NetworkSetup * NC = NSResources->findNetworkSetup( LBI->text() );
00359
00360 Log(( "Prepare to %sable\n", (T) ? "en" : "dis" ));
00361 Msg = NC->setState( (T) ? Disable : Enable );
00362 if( ! Msg.isEmpty() ) {
00363 Msg.insert( 0, "<p>" );
00364 Msg.append( "</p>" );
00365 QMessageBox::warning(
00366 0,
00367 tr( "Activating profile" ),
00368 Msg );
00369 return;
00370 }
00371
00372
00373 NC->state( true );
00374 updateProfileState( LBI );
00375 }
00376
00377 void NetworkSettings::SLOT_Up( void ) {
00378
00379
00380 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00381 QString Msg;
00382 int led = -1;
00383
00384 if ( ! LBI )
00385 return;
00386
00387 NetworkSetup * NC =
00388 NSResources->findNetworkSetup( LBI->text() );
00389
00390 switch( NC->state() ) {
00391 case Disabled :
00392 case Unknown :
00393 case Unchecked :
00394 case Unavailable :
00395 case IsUp :
00396 return;
00397 case Off :
00398 led = 1;
00399 Down_TB->setEnabled( true );
00400 Log(( "Activate interface %s\n", NC->name().latin1() ));
00401 Msg = NC->setState( Activate );
00402 break;
00403 case Available :
00404 led = 2;
00405 Log(( "Bring up interface %s\n", NC->name().latin1() ));
00406 Msg = NC->setState( Up );
00407 if( Msg.isEmpty() ) {
00408 Up_TB->setEnabled( false );
00409 }
00410 break;
00411 }
00412
00413 if( ! Msg.isEmpty() ) {
00414 Msg.insert( 0, "<p>" );
00415 Msg.append( "</p>" );
00416 QMessageBox::warning(
00417 0,
00418 tr( "Increase availability" ),
00419 Msg );
00420 return;
00421 }
00422
00423 updateProfileState( LBI );
00424
00425
00426 if( led > 0 ) {
00427 Leds[led]->setColor( blue );
00428 Leds[led]->setOn( true );
00429 }
00430
00431 }
00432
00433 void NetworkSettings::SLOT_Down( void ) {
00434
00435
00436 QListBoxItem * LBI = Profiles_LB->item( Profiles_LB->currentItem() );
00437 int led = -1;
00438 QString Msg;
00439
00440 if ( ! LBI )
00441 return;
00442
00443 NetworkSetup * NC =
00444 NSResources->findNetworkSetup( LBI->text() );
00445
00446 switch( NC->state() ) {
00447 case Disabled :
00448 case Unknown :
00449 case Unchecked :
00450 case Unavailable :
00451 case Off :
00452 break;
00453 case Available :
00454 led = 0;
00455 Log(( "Deactivate interface %s\n", NC->name().latin1() ));
00456 Msg = NC->setState( Deactivate );
00457 Down_TB->setEnabled( false );
00458 break;
00459 case IsUp :
00460 led = 1;
00461 Up_TB->setEnabled( true );
00462 Log(( "Bring down interface %s\n", NC->name().latin1() ));
00463 Msg = NC->setState( Down, 1 );
00464 if( Msg.isEmpty() ) {
00465
00466 unlink ( QString().sprintf( "/tmp/Profile-%d.up", NC->number() ).latin1() );;
00467 }
00468 break;
00469 }
00470
00471 if( ! Msg.isEmpty() ) {
00472 Msg.insert( 0, "<p>" );
00473 Msg.append( "</p>" );
00474 QMessageBox::warning(
00475 0,
00476 tr( "Decrease availability" ),
00477 Msg );
00478 return;
00479 }
00480
00481 updateProfileState( LBI );
00482
00483
00484 if( led >= 0 ) {
00485 Leds[led]->setColor( blue );
00486 }
00487 }
00488
00489 void NetworkSettings::SLOT_ToMessages( void ) {
00490 Profiles_LB->hide();
00491 Profile_GB->hide();
00492 Messages_GB->show();
00493 }
00494
00495 void NetworkSettings::SLOT_ToProfile( void ) {
00496 Profiles_LB->show();
00497 Profile_GB->show();
00498 Messages_GB->hide();
00499 }
00500
00501 void NetworkSettings::SLOT_QCopMessage(const QCString &msg, const QByteArray &data) {
00502 QDataStream stream( data, IO_ReadOnly );
00503
00504 if( msg == "raise" ) {
00505 raise();
00506 return;
00507 }
00508
00509
00510
00511
00512 }