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

lancardrun.cpp

Go to the documentation of this file.
00001 #include <qfile.h>
00002 #include <qtextstream.h>
00003 #include <qstringlist.h>
00004 #include <resources.h>
00005 #include "lancardrun.h"
00006 
00007 State_t LanCardRun::detectState( void ) {
00008 
00009     // unavailable : no card found
00010     // available : card found and assigned to us or free
00011     // up : card found and assigned to us and up
00012 
00013     NetworkSetup * NC = networkSetup();
00014     QString S = QString( "/tmp/profile-%1.up" ).
00015                 arg( NC->number());
00016     System & Sys = NSResources->system();
00017     InterfaceInfo * Run;
00018 
00019     QFile F( S );
00020 
00021     if( F.open( IO_ReadOnly ) ) {
00022       // could open file -> read interface and assign
00023       QString X;
00024       QTextStream TS(&F);
00025       X = TS.readLine();
00026       // find interface
00027       if( handlesInterface( X ) ) {
00028         for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
00029              It.current();
00030              ++It ) {
00031           Run = It.current();
00032           if( X == Run->Name ) {
00033             NC->assignInterface( Run );
00034             return (Run->IsUp) ? IsUp : Available;
00035           }
00036         }
00037       }
00038     } 
00039 
00040     if( ( Run = NC->assignedInterface() ) ) {
00041       // we already have an interface assigned -> still present ?
00042       if( ! Run->IsUp ) {
00043         // usb is still free -> keep assignment
00044         return Available;
00045       } // else interface is up but NOT us -> some other profile
00046     }
00047 
00048     // nothing (valid) assigned to us
00049     NC->assignInterface( 0 );
00050 
00051     // find possible interface
00052     for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
00053          It.current();
00054          ++It ) {
00055       Run = It.current();
00056       if( handlesInterface( *Run ) &&
00057           ( Run->CardType == ARPHRD_ETHER
00058 #ifdef ARPHRD_IEEE1394
00059             || Run->CardType == ARPHRD_IEEE1394
00060 #endif
00061           ) &&
00062           ! Run->IsUp
00063         ) {
00064         // proper type, and Not UP -> free
00065         return Off;
00066       }
00067     }
00068     // no free found
00069 
00070     return Unavailable;
00071 }
00072 
00073 QString LanCardRun::setMyState( NetworkSetup * NC, Action_t A, bool ) {
00074 
00075     if( A == Activate ) {
00076       InterfaceInfo * N = getInterface();
00077 
00078       if( ! N ) {
00079         // no interface available
00080         NC->setCurrentState( Unavailable );
00081         return tr("No interface found");
00082       }
00083 
00084       // because we were OFF the interface
00085       // we get back is NOT assigned
00086       NC->assignInterface( N );
00087       NC->setCurrentState( Available );
00088       return QString();
00089     }
00090 
00091     if( A == Deactivate ) {
00092       NC->assignInterface( 0 );
00093       NC->setCurrentState( Off );
00094     }
00095 
00096     return QString();
00097 }
00098 
00099 // get interface that is free or assigned to us
00100 InterfaceInfo * LanCardRun::getInterface( void ) {
00101 
00102     System & S = NSResources->system();
00103     InterfaceInfo * best = 0, * Run;
00104 
00105     for( QDictIterator<InterfaceInfo> It(S.interfaces());
00106          It.current();
00107          ++It ) {
00108       Run = It.current();
00109       if( handlesInterface( *Run ) &&
00110           ( Run->CardType == ARPHRD_ETHER
00111 #ifdef ARPHRD_IEEE1394
00112             || Run->CardType == ARPHRD_IEEE1394
00113 #endif
00114           )
00115         ) {
00116         // this is a LAN card
00117         if( Run->assignedToNetworkSetup() == netNode()->networkSetup() ) {
00118           // assigned to us
00119           return Run;
00120         } else if( Run->assignedToNetworkSetup() == 0 ) {
00121           // free
00122           best = Run;
00123         }
00124       }
00125     }
00126     return best; // can be 0
00127 }
00128 
00129 bool LanCardRun::handlesInterface( const QString & S ) {
00130     InterfaceInfo * II;
00131     II = NSResources->system().interface( S );
00132     if( ( II = NSResources->system().interface( S ) ) ) {
00133       return handlesInterface( *II );
00134     }
00135     return Pat.match( S ) >= 0;
00136 }
00137 
00138 bool LanCardRun::handlesInterface( const InterfaceInfo & II ) {
00139     if( Pat.match( II.Name ) < 0 )
00140       return 0;
00141 
00142     if( Data->AnyLanCard ) {
00143       return 1;
00144     }
00145 
00146     // must also match hardware address
00147     return ( Data->HWAddresses.findIndex( II.MACAddress ) >= 0 );
00148 }

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