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

configwindow.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2002-2004 Michael 'Mickey' Lauer.  All rights reserved.
00003 **
00004 ** This file is part of Wellenreiter II.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 **********************************************************************/
00015 
00016 /* LOCAL */
00017 #include "configwindow.h"
00018 #include "mainwindow.h"
00019 #include "protolistview.h"
00020 
00021 /* OPIE */
00022 #include <opie2/onetwork.h>
00023 #ifdef QWS
00024 #include <opie2/oapplication.h>
00025 #include <opie2/oconfig.h>
00026 #include <opie2/odevice.h>
00027 #include <opie2/odebug.h>
00028 using namespace Opie::Core;
00029 using namespace Opie::Net;
00030 #endif
00031 
00032 /* QT */
00033 #include <qapplication.h>
00034 #include <qcheckbox.h>
00035 #include <qcombobox.h>
00036 #include <qfile.h>
00037 #include <qlineedit.h>
00038 #include <qlayout.h>
00039 #include <qmap.h>
00040 #include <qpushbutton.h>
00041 #include <qradiobutton.h>
00042 #include <qspinbox.h>
00043 #include <qtabwidget.h>
00044 #include <qtoolbutton.h>
00045 #include <qtextstream.h>
00046 
00047 /* STD */
00048 #include <assert.h>
00049 
00050 WellenreiterConfigWindow* WellenreiterConfigWindow::_instance = 0;
00051 
00052 WellenreiterConfigWindow::WellenreiterConfigWindow( QWidget * parent, const char * name, WFlags f )
00053            :WellenreiterConfigBase( parent, name, true, f )
00054 {
00055     _devicetype[ "cisco" ] = DEVTYPE_CISCO;
00056     _devicetype[ "wlan-ng" ] = DEVTYPE_WLAN_NG;
00057     _devicetype[ "hostap" ] = DEVTYPE_HOSTAP;
00058     _devicetype[ "orinoco" ] = DEVTYPE_ORINOCO;
00059     _devicetype[ "<manual>" ] = DEVTYPE_MANUAL;
00060     _devicetype[ "<file>" ] = DEVTYPE_FILE;
00061 
00062     // gather possible interface names from ONetwork
00063     ONetwork* net = ONetwork::instance();
00064     ONetwork::InterfaceIterator it = net->iterator();
00065     while ( it.current() )
00066     {
00067         if ( it.current()->isWireless() )
00068             interfaceName->insertItem( it.current()->name() );
00069         ++it;
00070     }
00071 
00072     load();
00073 
00074     #ifdef Q_WS_X11 // We're on X11: adding an Ok-Button for the Dialog here
00075     QPushButton* okButton = new QPushButton( "ok", this );
00076     okButton->show();
00077     WellenreiterConfigBaseLayout->addWidget( okButton, 0, 3 ); //FIXME: rename this in configbase.ui
00078     connect( okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
00079     #endif
00080 
00081     WellenreiterConfigWindow::_instance = this;
00082 
00083     connect( deviceType, SIGNAL( activated(int) ), this, SLOT( changedDeviceType(int) ) );
00084     connect( newNetworkAction, SIGNAL( activated(int) ), this, SLOT( changedNetworkAction(int) ) );
00085     connect( newClientAction, SIGNAL( activated(int) ), this, SLOT( changedClientAction(int) ) );
00086     connect( newStationAction, SIGNAL( activated(int) ), this, SLOT( changedStationAction(int) ) );
00087     connect( getCaptureFileName, SIGNAL( clicked() ), this, SLOT( getCaptureFileNameClicked() ) );
00088 
00089     // make the checkbox 'channelAll' control all other channels
00090     connect( channelAll, SIGNAL( stateChanged(int) ), this, SLOT( channelAllClicked(int) ) );
00091 
00092     connect( autodetect, SIGNAL( clicked() ), this, SLOT( performAutodetection() ) );
00093 
00094     // hide tab4 (parse) until Wellenreiter 1.2
00095     tab->removePage( tab_4 );
00096 };
00097 
00098 
00099 void WellenreiterConfigWindow::accept()
00100 {
00101     save();
00102     QDialog::accept();
00103 }
00104 
00105 
00106 WellenreiterConfigWindow::~WellenreiterConfigWindow()
00107 {
00108 }
00109 
00110 
00111 void WellenreiterConfigWindow::performAutodetection()
00112 {
00113     //TODO: insert modal splash screen here
00114     //      and sleep a second, so that it looks
00115     //      like we're actually doing something fancy... ;-)
00116 
00117     odebug << "WellenreiterConfigWindow::performAutodetection()" << oendl;
00118 
00119     // try to guess device type
00120     QFile m( "/proc/modules" );
00121     if ( m.open( IO_ReadOnly ) )
00122     {
00123         int devicetype(0);
00124         QString line;
00125         QTextStream modules( &m );
00126         while( !modules.atEnd() && !devicetype )
00127         {
00128             modules >> line;
00129             if ( line.contains( "cisco" ) ) devicetype = DEVTYPE_CISCO;
00130             else if ( line.contains( "hostap" ) ) devicetype = DEVTYPE_HOSTAP;
00131             else if ( line.contains( "prism" ) ) devicetype = DEVTYPE_WLAN_NG;
00132             else if ( line.contains( "orinoco" ) ) devicetype = DEVTYPE_ORINOCO;
00133         }
00134         if ( devicetype )
00135         {
00136             deviceType->setCurrentItem( devicetype );
00137             _guess = devicetype;
00138             odebug << "Wellenreiter: guessed device type to be #" << devicetype << "" << oendl;
00139         }
00140     }
00141 }
00142 
00143 
00144 int WellenreiterConfigWindow::driverType() const
00145 {
00146     QString name = deviceType->currentText();
00147     if ( _devicetype.contains( name ) )
00148     {
00149         return _devicetype[name];
00150     }
00151     else
00152     {
00153         return 0;
00154     }
00155 };
00156 
00157 
00158 int WellenreiterConfigWindow::hoppingInterval() const
00159 {
00160     return hopInterval->cleanText().toInt();
00161 }
00162 
00163 
00164 bool WellenreiterConfigWindow::usePrismHeader() const
00165 {
00166     return prismHeader->isChecked();
00167 }
00168 
00169 
00170 bool WellenreiterConfigWindow::isChannelChecked( int channel ) const
00171 {
00172     switch ( channel )
00173     {
00174         case 1: return channel1->isOn();
00175         case 2: return channel2->isOn();
00176         case 3: return channel3->isOn();
00177         case 4: return channel4->isOn();
00178         case 5: return channel5->isOn();
00179         case 6: return channel6->isOn();
00180         case 7: return channel7->isOn();
00181         case 8: return channel8->isOn();
00182         case 9: return channel9->isOn();
00183         case 10: return channel10->isOn();
00184         case 11: return channel11->isOn();
00185         case 12: return channel12->isOn();
00186         case 13: return channel13->isOn();
00187         case 14: return channel14->isOn();
00188         default: return false;
00189     }
00190 
00191 
00192 }
00193 
00194 
00195 void WellenreiterConfigWindow::changedDeviceType(int t)
00196 {
00197     if ( t != DEVTYPE_FILE ) return;
00198     QString name = ( (WellenreiterMainWindow*) qApp->mainWidget() )->getFileName(false);
00199     if ( !name.isEmpty() && QFile::exists( name ) )
00200     {
00201         interfaceName->insertItem( name );
00202         interfaceName->setCurrentItem( interfaceName->count()-1 );
00203     }
00204     else
00205     {
00206         deviceType->setCurrentItem( _guess );
00207     }
00208 
00209 }
00210 
00211 
00212 void WellenreiterConfigWindow::synchronizeActionsAndScripts()
00213 {
00214     if ( newNetworkAction->currentItem() == 4 ) newNetworkScript->show(); else newNetworkScript->hide();
00215     if ( newClientAction->currentItem() == 4 ) newClientScript->show(); else newClientScript->hide();
00216     if ( newStationAction->currentItem() == 4 ) newStationScript->show(); else newStationScript->hide();
00217 
00218     //newNetworkScript->setEnabled( newNetworkAction->currentItem() == 4 );
00219     //newClientScript->setEnabled( newClientAction->currentItem() == 4 );
00220     //newStationScript->setEnabled( newStationAction->currentItem() == 4 );
00221 }
00222 
00223 
00224 void WellenreiterConfigWindow::changedNetworkAction(int )
00225 {
00226     synchronizeActionsAndScripts();
00227 }
00228 
00229 
00230 void WellenreiterConfigWindow::changedClientAction(int )
00231 {
00232     synchronizeActionsAndScripts();
00233 }
00234 
00235 
00236 void WellenreiterConfigWindow::changedStationAction(int )
00237 {
00238     synchronizeActionsAndScripts();
00239 }
00240 
00241 
00242 void WellenreiterConfigWindow::getCaptureFileNameClicked()
00243 {
00244     QString name = ( (WellenreiterMainWindow*) qApp->mainWidget() )->getFileName(true);
00245     odebug << "name = " << name << "" << oendl;
00246     if ( !name.isEmpty() )
00247     {
00248         captureFileName->setText( name );
00249     }
00250 }
00251 
00252 
00253 void WellenreiterConfigWindow::channelAllClicked(int state)
00254 {
00255     bool b = state;
00256     channel1->setChecked( b );
00257     channel2->setChecked( b );
00258     channel3->setChecked( b );
00259     channel4->setChecked( b );
00260     channel5->setChecked( b );
00261     channel6->setChecked( b );
00262     channel7->setChecked( b );
00263     channel8->setChecked( b );
00264     channel9->setChecked( b );
00265     channel10->setChecked( b );
00266     channel11->setChecked( b );
00267     channel12->setChecked( b );
00268     channel13->setChecked( b );
00269     channel14->setChecked( b );
00270 }
00271 
00272 
00273 bool WellenreiterConfigWindow::useGPS() const
00274 {
00275     return enableGPS->isChecked();
00276 }
00277 
00278 
00279 const QString WellenreiterConfigWindow::gpsHost() const
00280 {
00281     return useGPS() ? gpsdHost->currentText() : QString::null;
00282 }
00283 
00284 
00285 int WellenreiterConfigWindow::gpsPort() const
00286 {
00287     return useGPS() ? gpsdPort->value() : -1;
00288 }
00289 
00290 
00291 void WellenreiterConfigWindow::performAction( const QString& type,
00292                                               const QString& essid,
00293                                               const QString& mac,
00294                                               bool wep,
00295                                               int channel,
00296                                               int /* signal */
00297                                               /* , const GpsLocation& loc */ )
00298 {
00299     int action;
00300     QString script;
00301 
00302     if ( type == "network" )
00303     {
00304         action = newNetworkAction->currentItem();
00305         script = newNetworkScript->text();
00306     }
00307     else if ( type == "managed" || type == "adhoc" )
00308     {
00309         action = newClientAction->currentItem();
00310         script = newClientScript->text();
00311     }
00312     else if ( type == "station" )
00313     {
00314         action = newStationAction->currentItem();
00315         script = newStationScript->text();
00316     }
00317     else
00318     {
00319         owarn << "WellenreiterConfigWindow::performAction(): unknown type '" << type << "'" << oendl;
00320         return;
00321     }
00322 
00323     odebug << "for event '" << type << "' I'm going to perform action " << action << " (script='" << script << "')" << oendl;
00324 
00325     switch( action )
00326     {
00327         case 0: /* Ignore */ return;
00328         case 1: /* Play Alarm */ ODevice::inst()->playAlarmSound(); return;
00329         case 2: /* Play Click */ ODevice::inst()->playTouchSound(); return;
00330         case 3: /* Blink LED */ break; //FIXME: Implement this
00331         case 4: /* Run Script */
00332         {
00343             script = script.replace( QRegExp( "$SSID" ), essid );
00344             script = script.replace( QRegExp( "$MAC" ), mac );
00345             script = script.replace( QRegExp( "$WEP" ), wep ? QString( "true" ) : QString( "false" ) );
00346             script = script.replace( QRegExp( "$CHAN" ), QString::number( channel ) );
00347 
00348             odebug << "going to call script '" << script << "'" << oendl;
00349             ::system( script );
00350             odebug << "script returned." << oendl;
00351             return;
00352         }
00353         default: assert( false );
00354     }
00355 }
00356 
00357 
00358 void WellenreiterConfigWindow::load()
00359 {
00360 #ifdef Q_WS_X11
00361     #warning Persistent Configuration not yet implemented for standalone X11 build
00362     performAutodetection();
00363 #else
00364     odebug << "loading configuration settings..." << oendl;
00365 
00366     /* This is dumb monkey typing stuff... We _need_ to do this automatically! */
00367 
00368     OConfig* c = oApp->config();
00369 
00370     c->setGroup( "Interface" );
00371 
00372     QString interface = c->readEntry( "name", "<none>" );
00373     if ( interface != "<none>" )
00374     {
00375 #if QT_VERSION < 0x030000
00376         interfaceName->insertItem( interface, 0 );
00377         interfaceName->setCurrentItem( 0 );
00378 #else
00379         interfaceName->setCurrentText( interface );
00380 #endif
00381 
00382         QString device = c->readEntry( "type", "<select>" );
00383 #if QT_VERSION < 0x030000
00384         for ( int i = 0; i < deviceType->count(); ++i )
00385         {
00386             if ( deviceType->text( i ) == device )
00387             {
00388                 deviceType->setCurrentItem( i );
00389                 break;
00390             }
00391         }
00392 #else
00393         deviceType->setCurrentText( device );
00394 #endif
00395     }
00396     else
00397     {
00398         performAutodetection();
00399     }
00400 
00401     prismHeader->setChecked( c->readBoolEntry( "prism", false ) );
00402     hopChannels->setChecked( c->readBoolEntry( "hop", true ) );
00403     hopInterval->setValue( c->readNumEntry( "interval", 250 ) );
00404     adaptiveHopping->setChecked( c->readBoolEntry( "adaptive", true ) );
00405 
00406     c->setGroup( "Capture" );
00407     writeCaptureFile->setChecked( c->readBoolEntry( "writeCaptureFile", true ) );
00408     captureFileName->setEnabled( writeCaptureFile->isChecked() );
00409     getCaptureFileName->setEnabled( writeCaptureFile->isChecked() );
00410     parsePackets->setEnabled( writeCaptureFile->isChecked() );
00411     captureFileName->setText( c->readEntry( "filename", "/tmp/capture" ) );
00412     hexViewBufferUnlimited->setChecked( c->readBoolEntry( "hexViewBufferUnlimited", true ) );
00413     hexViewBufferLimited->setChecked( !c->readBoolEntry( "hexViewBufferUnlimited", true ) );
00414     hexViewBufferSize->setValue( c->readNumEntry( "hexViewBufferSize", 2000 ) );
00415 
00416     c->setGroup( "UI" );
00417     lookupVendor->setChecked( c->readBoolEntry( "lookupVendor", true ) );
00418     openTree->setChecked( c->readBoolEntry( "openTree", true ) );
00419     disablePM->setChecked( c->readBoolEntry( "disablePM", true ) );
00420     newNetworkAction->setCurrentItem( c->readNumEntry( "newNetworkAction", 1 ) ); // Default: Play Alarm
00421     newNetworkScript->setText( c->readEntry( "newNetworkScript", "" ) );
00422     newClientAction->setCurrentItem( c->readNumEntry( "newClientAction", 2 ) ); // Default: Play Click
00423     newClientScript->setText( c->readEntry( "newClientScript", "" ) );
00424     newStationAction->setCurrentItem( c->readNumEntry( "newStationAction", 2 ) ); // Default: Play Click
00425     newStationScript->setText( c->readEntry( "newStationScript", "" ) );
00426     synchronizeActionsAndScripts(); // needed for showing/hiding the script QLineEdit on demand
00427 
00428     c->setGroup( "GPS" );
00429     enableGPS->setChecked( c->readBoolEntry( "use", false ) );
00430 #if QT_VERSION < 0x030000
00431     gpsdHost->insertItem( c->readEntry( "host", "localhost" ), 0 );
00432     gpsdHost->setCurrentItem( 0 );
00433 #else
00434     gpsdHost->setCurrentText( c->readEntry( "host", "localhost" ) );
00435 #endif
00436     gpsdPort->setValue( c->readNumEntry( "port", 2947 ) );
00437     startGPS->setChecked( c->readBoolEntry( "start", false ) );
00438     commandGPS->setText( c->readEntry( "command", "gpsd -p /dev/ttyS3 -s 57600" ) );
00439 
00440 #endif
00441 }
00442 
00443 
00444 void WellenreiterConfigWindow::save()
00445 {
00446 #ifdef Q_WS_X11
00447     #warning Persistent Configuration not yet implemented for standalone X11 build
00448 #else
00449     odebug << "saving configuration settings..." << oendl;
00450 
00451     /* This is dumb monkey typing stuff... We _need_ to do this automatically! */
00452 
00453     OConfig* c = oApp->config();
00454 
00455     c->setGroup( "Interface" );
00456     c->writeEntry( "name", interfaceName->currentText() );
00457     c->writeEntry( "type", deviceType->currentText() );
00458     c->writeEntry( "prism", prismHeader->isChecked() );
00459     c->writeEntry( "hop", hopChannels->isChecked() );
00460     c->writeEntry( "interval", hopInterval->value() );
00461     c->writeEntry( "adaptive", adaptiveHopping->isChecked() );
00462 
00463     c->setGroup( "Capture" );
00464     c->writeEntry( "writeCaptureFile", writeCaptureFile->isChecked() );
00465     c->writeEntry( "filename", captureFileName->text() );
00466     c->writeEntry( "hexViewBufferUnlimited", hexViewBufferUnlimited->isChecked() );
00467     c->writeEntry( "hexViewBufferSize", hexViewBufferSize->value() );
00468 
00469     c->setGroup( "UI" );
00470     c->writeEntry( "lookupVendor", lookupVendor->isChecked() );
00471     c->writeEntry( "openTree", openTree->isChecked() );
00472     c->writeEntry( "disablePM", disablePM->isChecked() );
00473     c->writeEntry( "newNetworkAction", newNetworkAction->currentItem() );
00474     c->writeEntry( "newNetworkScript", newNetworkScript->text() );
00475     c->writeEntry( "newClientAction", newClientAction->currentItem() );
00476     c->writeEntry( "newClientScript", newClientScript->text() );
00477     c->writeEntry( "newStationAction", newStationAction->currentItem() );
00478     c->writeEntry( "newStationScript", newStationScript->text() );
00479 
00480     c->setGroup( "GPS" );
00481     c->writeEntry( "use", enableGPS->isChecked() );
00482     c->writeEntry( "host", gpsdHost->currentText() );
00483     c->writeEntry( "port", gpsdPort->value() );
00484     c->writeEntry( "start", startGPS->isChecked() );
00485     c->writeEntry( "command", commandGPS->text() );
00486 
00487     c->write();
00488 
00489 #endif
00490 }
00491 
00492 
00493 int WellenreiterConfigWindow::hexViewBuffer() const
00494 {
00495     return hexViewBufferUnlimited->isChecked() ? -1 : hexViewBufferSize->value();
00496 }

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