From d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843 Mon Sep 17 00:00:00 2001 From: wimpie Date: Sat, 03 Apr 2004 21:33:30 +0000 Subject: Factored out non-gui data class Added support for prompting --- (limited to 'noncore') diff --git a/noncore/settings/networksettings2/main.cpp b/noncore/settings/networksettings2/main.cpp index bcef631..1e1270e 100644 --- a/noncore/settings/networksettings2/main.cpp +++ b/noncore/settings/networksettings2/main.cpp @@ -1,3 +1,5 @@ +#include "nsdata.h" +#include "activateprofile.h" #include "networksettings.h" #include @@ -9,9 +11,14 @@ OPIE_EXPORT_APP( OApplicationFactory ) #else +// just standard GUI #define ACT_GUI 0 +// used by interfaces to request for allow of up/down #define ACT_REQUEST 1 +// regenerate config files #define ACT_REGEN 2 +// used by interfaces to request user prompt +#define ACT_PROMPT 3 int main( int argc, char * argv[] ) { int rv = 0; @@ -32,6 +39,9 @@ int main( int argc, char * argv[] ) { Action = ACT_REGEN; GuiType = QApplication::Tty; rmv = 1; + } else if( strcmp( argv[i], "--prompt" ) == 0 ) { + Action = ACT_PROMPT; + rmv = 1; } if( rmv ) { memmove( argv+i, argv+i+rmv, @@ -62,28 +72,41 @@ int main( int argc, char * argv[] ) { #endif // init qt with app widget - if( GuiType != QApplication::Tty ) { - QWidget * W = 0; - W = new NetworkSettings(0); - TheApp->setMainWidget( W ); - W->show(); + + switch( Action ) { + case ACT_REQUEST : + { NetworkSettingsData NS; + NS.canStart( argv[1] ); + } + break; + case ACT_REGEN : + { NetworkSettingsData NS; + // regen returns 0 if OK + rv = (NS.regenerate()) ? 1 : 0; + } + break; + case ACT_PROMPT : + { ActivateProfile AP(argv[1]); + if( AP.exec() == QDialog::Accepted ) { + printf( "%s-c%d-allowed", AP.selectedProfile() ); + } else { + printf( "%s-cNN-disallowed" ); + } + } + break; + case ACT_GUI : + { QWidget * W = new NetworkSettings(0); + TheApp->setMainWidget( W ); + W->show(); #ifdef _WS_QWS_ - W->showMaximized(); + W->showMaximized(); #else - W->resize( W->sizeHint() ); + W->resize( W->sizeHint() ); #endif - rv = TheApp->exec(); - delete W; - } else { - switch( Action ) { - case ACT_REQUEST : - NetworkSettings::canStart( argv[1] ); - break; - case ACT_REGEN : - // regen returns 0 if OK - rv = (NetworkSettings::regenerate()) ? 1 : 0; - break; - } + rv = TheApp->exec(); + delete W; + } + break; } return rv; diff --git a/noncore/settings/networksettings2/networksettings.cpp b/noncore/settings/networksettings2/networksettings.cpp index ffe130c..f72fa8e 100644 --- a/noncore/settings/networksettings2/networksettings.cpp +++ b/noncore/settings/networksettings2/networksettings.cpp @@ -8,10 +8,6 @@ #include #include #include -#include -#include -#include -#include #include #include #include @@ -21,359 +17,6 @@ #include "netnode.h" #include "editconnection.h" -static QString CfgFile; - -NetworkSettingsData::NetworkSettingsData( void ) { - // init global resources structure - new TheNSResources(); - - CfgFile.sprintf( "%s/NETCONFIG", getenv("HOME") ); - - // load settings - Force = 0; - loadSettings(); -} - -// saving is done by caller -NetworkSettingsData::~NetworkSettingsData( void ) { - delete NSResources; -} - -void NetworkSettingsData::loadSettings( void ) { - QString S; - ANetNodeInstance* NNI; - QString Attr, Value; - long idx; - - QFile F( CfgFile ); - QTextStream TS( &F ); - - do { - - if( ! F.open(IO_ReadOnly) ) - break; - - /* load the file -> - - FORMAT : - - [NETNODETYPE] - Entries ... - - [connection] - Name=Name - Node=Name - - */ - while( ! TS.atEnd() ) { - S = TS.readLine(); - - if ( S.isEmpty() || S[0] != '[' ) - continue; - - S = S.mid( 1, S.length()-2 ); - - if( ! NSResources ) { - continue; - } - - if( S == "connection" ) { - // load connections -> collections of nodes - NodeCollection * NC = new NodeCollection( TS ); - if ( NC->count() == 0 ) { - if( QMessageBox::warning( - 0, - qApp->translate( "NetworkSettings2", "Invalid connection" ), - qApp->translate( "NetworkSettings2", - "

Connection %1 contains unrecognized nodes and cannot be loaded

" ).arg(NC->name()), - qApp->translate( "NetworkSettings2", - "Remove node"), - qApp->translate( "NetworkSettings2", - "Exit program") ) == 1 ) { - exit( 0 ); - } - delete NC; - } else - NSResources->addConnection( NC ); - } else { - // load nodes - NNI = NSResources->createNodeInstance( S ); - if( ! NNI ) { - printf( "SKIPPING %s\n", S.latin1() ); - } - - do { - S = TS.readLine(); - if( S.isEmpty() ) { - // empty line - break; - } - // node found ? - if( NNI ) { - idx = S.find( '=' ); - if( idx > 0 ) { - Attr = S.left( idx ); - Value = S.mid( idx+1, S.length() ); - } else { - Value=""; - Attr = S; - } - - Value.stripWhiteSpace(); - Attr.stripWhiteSpace(); - Attr.lower(); - // dequote Attr - Value = deQuote(Value); - - // set the attribute - NNI->setAttribute( Attr, Value ); - } - - } while( 1 ); - if( NNI ) { - // loading from file -> exists - NNI->setNew( FALSE ); - NSResources->addNodeInstance( NNI ); - } - } - } - - } while( 0 ); - -} - -QString NetworkSettingsData::saveSettings( void ) { - QString ErrS = ""; - - if( ! isModified() ) - return ErrS; - - QString S; - QFile F( CfgFile + ".bup" ); - - printf( "Saving settings to %s\n", CfgFile.latin1() ); - if( ! F.open( IO_WriteOnly | IO_Truncate ) ) { - ErrS = qApp->translate( "NetworkSettings", - "

Could not save setup to %1 !

" ). - arg(CfgFile); - // problem - return ErrS; - } - - QTextStream TS( &F ); - { Name2Connection_t & M = NSResources->connections(); - ANetNodeInstance * NNI; - - // for all connections - for( QDictIterator it(M); - it.current(); - ++it ) { - // all nodes in those connections - for( QListIterator nit(*(it.current())); - nit.current(); - ++nit ) { - // header - NNI = nit.current(); - TS << '[' <netNode()->nodeName() << ']' << endl; - NNI->saveAttributes( TS ); - TS << endl; - } - - TS << "[connection]" << endl; - it.current()->save(TS); - } - } - - QDir D("."); - D.rename( CfgFile + ".bup", CfgFile ); - - // - // proper files AND system files regenerated - // - - setModified( 0 ); - return ErrS; -} - -QString NetworkSettingsData::generateSettings( bool ForceReq ) { - bool ForceIt; - QString S = ""; - - // include own force flag - ForceIt = (Force) ? 1 : ForceReq; - - if( ! ForceIt && ! isModified() ) - return S; - - // regenerate system files - printf( "Generating settings from %s\n", CfgFile.latin1() ); - - { Name2SystemFile_t & SFM = NSResources->systemFiles(); - Name2Connection_t & M = NSResources->connections(); - NodeCollection * NC; - ANetNodeInstance * NNI; - SystemFile * SF; - bool needToRegenerate = ForceIt; - - // - // check if we need to generate at least one of the system files - // - if( ! ForceIt ) { - for( QDictIterator sfit(SFM); - sfit.current(); - ++sfit ) { - SF = sfit.current(); - - // check if there are nodes that are modified and require - // data for this system file - - // for all connections - for( QDictIterator ncit(M); - ncit.current(); - ++ncit ) { - NC = ncit.current(); - - if( NC->isModified() ) { - // does this connection 'touch' this system file ? - for( QListIterator cncit(*NC); - cncit.current(); - ++cncit ) { - NNI = cncit.current(); - if( NNI->netNode()->hasDataFor( SF->name() ) && - NNI->isModified() ) { - needToRegenerate = 1; - break; - } - } - } - if( needToRegenerate ) - break; - } - if( needToRegenerate ) - break; - } - } - - // we cannot renumber with a FORCE request since - // we probably are NOT going to save the config - // e.g. when using --regen option - if( ! ForceReq && needToRegenerate ) { - NSResources->renumberConnections(); - setModified(1); - } - - // - // generate files proper to each netnodeinstance - // - { Name2Instance_t & NNIs = NSResources->netNodeInstances(); - ANetNodeInstance * NNI; - - for( QDictIterator NNIIt(NNIs); - NNIIt.current(); - ++NNIIt - ){ - // for all nodes find those that are modified - NNI = NNIIt.current(); - - if( ForceIt || NNI->isModified() ) { - if( ! NNI->netNode()->generateProperFilesFor( NNI ) ) { - // problem generating - S = qApp->translate( "NetworkSettings", - "

Cannot generate files proper to %1

" ). - arg(NNI->netNode()->nodeName()) ; - return S; - } - } - } - } - - // - // generate system files - // - for( QDictIterator sfit(SFM); - sfit.current(); - ++sfit ) { - SF = sfit.current(); - - // - // regenerate current file - // - printf( "Generating %s\n", SF->name().latin1() ); - SF->open(); - - do { // so we can break; - - if( SF->preSection() ) { - S = qApp->translate( "NetworkSettings", - "

Error in preSection for file %1

" ). - arg( SF->name() ); - return S; - } - - for( QDictIterator ncit(M); - ncit.current(); - ++ncit ) { - NC = ncit.current(); - - // get the netnode that serves as the device for this - // connection - AsDevice * Dev = NC->device(); - - // generate 'entry' for every possible device this profile handles - - for( QListIterator cncit(*NC); - cncit.current(); - ++cncit ) { - NNI = cncit.current(); - for( int i = 0; i < Dev->count(); i ++ ) { - if( NNI->netNode()->hasDataFor( SF->name() ) ) { - if( SF->preNodeSection( NNI, i ) ) { - S = qApp->translate( "NetworkSettings", - "

Error in preNodeSection for file %1 and node %2

" ). - arg( SF->name() ). - arg( NNI->netNode()->nodeName() ); - return S; - } - - if( NNI->netNode()->generateDataForCommonFile(*SF,i,NNI) ) { - S = qApp->translate( "NetworkSettings", - "

Error in node part for file %1 and node %2

" ). - arg( SF->name() ). - arg( NNI->netNode()->nodeName() ); - return S; - } - - if( SF->postNodeSection( NNI, i ) ) { - S = qApp->translate( "NetworkSettings", - "

Error in postNodeSection for file %1 and node %2

" ). - arg( SF->name() ). - arg( NNI->netNode()->nodeName() ); - return S; - } - } - } - } - *SF << endl; - } - - if( SF->postSection() ) { - S = qApp->translate( "NetworkSettings", - "

Error in postSection for file %1

" ). - arg( SF->name() ); - return S; - } - } while( 0 ); - SF->close(); - } - } - Force = 0; - return S; -} - -// -// GUI part -// - NetworkSettings::NetworkSettings( QWidget *parent, const char *name, WFlags fl ) : NetworkSettingsGUI(parent,name,fl), @@ -420,6 +63,10 @@ NetworkSettings::NetworkSettings( QWidget *parent, UpdateTimer->start( 5000 ); connect( UpdateTimer, SIGNAL( timeout() ), this, SLOT( SLOT_RefreshStates() ) ); + + /* Add QCopChannel */ + connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)), + this, SLOT(SLOT_QCopMessage(const QCString&,const QByteArray&)) ); } NetworkSettings::~NetworkSettings() { @@ -753,68 +400,15 @@ void NetworkSettings::SLOT_Connect( void ) { // we do not update the GUI but wait for the REAL upping of the device } -/* - Called by the system to see if interface can be brought UP - - if allowed, echo Interface-allowed else Interface-disallowed -*/ - -void NetworkSettings::canStart( const char * Interface ) { - // load situation - NetworkSettingsData NSD; - - { Name2Connection_t & M = NSResources->connections(); - NodeCollection * NC; - - // for all connections - for( QDictIterator it(M); - it.current(); - ++it ) { - NC = it.current(); - // check if this profile handles the requested interface - if( NC->handlesInterface( Interface ) ) { - switch( NC->state() ) { - case Unchecked : - case Unknown : - case Unavailable : - case Disabled : - // this profile does not allow interface to be UP - // -> try others - break; - case Off : - // try to UP the device - if( ! NC->setState( Activate ) ) { - // cannot bring device Online -> try other alters - break; - } - // FT - case Available : - case IsUp : // also called for 'down' - // device is ready -> done - printf( "%s-c%d-allowed\n", - Interface, NC->number() ); - return; - } - } - } - } - // if we come here no alternatives are possible - printf( "%s-cnn-disallowed\n", Interface ); -} - -/* - Called by the system to regenerate config files -*/ - -bool NetworkSettings::regenerate( void ) { - QString S; - // load situation - NetworkSettingsData NSD; +void NetworkSettings::SLOT_QCopMessage(const QCString &msg, const QByteArray &data) { + QDataStream stream( data, IO_ReadOnly ); - S = NSD.generateSettings( TRUE ); - if( ! S.isEmpty() ) { - fprintf( stdout, "%s\n", S.latin1() ); - return 1; - } - return 0; + if( msg == "raise" ) { + raise(); + return; + } /* if ( msg == "someMessage(int,int,int)" ) { + int a,b,c; + stream >> a >> b >> c; + ... + } */ } diff --git a/noncore/settings/networksettings2/networksettings.h b/noncore/settings/networksettings2/networksettings.h index eb475a3..fadf81b 100644 --- a/noncore/settings/networksettings2/networksettings.h +++ b/noncore/settings/networksettings2/networksettings.h @@ -1,3 +1,4 @@ +#include "nsdata.h" #include "networksettingsGUI.h" #include "resources.h" @@ -6,33 +7,6 @@ class ANetNodeInstance; class QTimer; class QIconViewItem; -class NetworkSettingsData { - -public : - - NetworkSettingsData( void ); - ~NetworkSettingsData( void ); - - void loadSettings( void ); - QString saveSettings( void ); - - QString generateSettings( bool Force = FALSE ); - - bool isModified( void ) - { return IsModified; } - void setModified( bool m ) - { IsModified = m; } - - void forceGeneration( bool m ) - { Force = m; } - -private : - - bool IsModified; - bool Force; - -}; - class NetworkSettings : public NetworkSettingsGUI { Q_OBJECT @@ -47,9 +21,6 @@ public : static QString appName( void ) { return QString::fromLatin1("networksettings"); } - static void canStart( const char * Interface ); - static bool regenerate( void ); - bool isModified( void ) { return NSD.isModified(); } void setModified( bool m ) @@ -67,6 +38,7 @@ public slots : void SLOT_Connect( void ); void SLOT_GenerateConfig( void ); void SLOT_RefreshStates( void ); + void SLOT_QCopMessage( const QCString&,const QByteArray& ); private : diff --git a/noncore/settings/networksettings2/networksettings.pro b/noncore/settings/networksettings2/networksettings.pro index fb1f2cf..fe47971 100644 --- a/noncore/settings/networksettings2/networksettings.pro +++ b/noncore/settings/networksettings2/networksettings.pro @@ -2,14 +2,19 @@ CONFIG = qt warn_on debug quick-app #CONFIG = qt warn_on release HEADERS = networksettings.h \ + activateprofile.h \ editconnection.h SOURCES = main.cpp \ networksettings.cpp \ + nsdata.cpp \ + activateprofile.cpp \ editconnection.cpp INCLUDEPATH += $(OPIEDIR)/include networksettings2/ DEPENDPATH += $(OPIEDIR)/include networksettings2/ LIBS += -lqpe -L$(OPIEDIR)/plugins/networksettings2 -lnetworksettings2 -lopie -INTERFACES = networksettingsGUI.ui editconnectionGUI.ui +INTERFACES = networksettingsGUI.ui \ + editconnectionGUI.ui \ + activateprofileGUI.ui TARGET = networksettings2 diff --git a/noncore/settings/networksettings2/networksettings2/netnode.cpp b/noncore/settings/networksettings2/networksettings2/netnode.cpp index a5b572b..d36a1e5 100644 --- a/noncore/settings/networksettings2/networksettings2/netnode.cpp +++ b/noncore/settings/networksettings2/networksettings2/netnode.cpp @@ -276,3 +276,27 @@ InterfaceInfo * RuntimeInfo::assignedInterface( void ) { AsDevice * RuntimeInfo::device( void ) { return netNode()->nextNode()->runtime()->device(); } + +ANetNodeInstance * FakeNetNode::createInstance( void ) { + return new FakeNetNodeInstance( this ); +} + +void FakeNetNodeInstance::setSpecificAttribute( + QString & A, QString & V ) { + ValAttrPairs.insert( A, new QString(V) ); +} + +void FakeNetNodeInstance::saveSpecificAttribute( QTextStream &TS ) { + for( QDictIterator it( ValAttrPairs ); + it.current(); + ++ it ) { + TS << it.currentKey().latin1() + << "=" + << quote( *(it.current())) + << endl ; + ++it; + } +} + +// collects all info that no plugin acceps +FakeNetNode * FakeNode = 0; diff --git a/noncore/settings/networksettings2/networksettings2/netnode.h b/noncore/settings/networksettings2/networksettings2/netnode.h index 0ecd64e..56333c5 100644 --- a/noncore/settings/networksettings2/networksettings2/netnode.h +++ b/noncore/settings/networksettings2/networksettings2/netnode.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -358,4 +359,82 @@ private : }; +// +// special node that is used to remember entries for plugins +// that seem missing. This way we never loose data +// + +class FakeNetNode : public ANetNode { + +public: + + FakeNetNode( ) { }; + virtual ~FakeNetNode(){}; + + const QString pixmapName() + { return QString(""); } + const QString nodeName() + { return QString("Fake node" ); } + const QString nodeDescription() + { return QString("Fake node" ); } + ANetNodeInstance * createInstance( void ); + const char * provides( void ) + { return ""; } + virtual const char ** needs( void ) + { return 0; } + virtual bool generateProperFilesFor( ANetNodeInstance * ) + { return 0; } + virtual bool hasDataFor( const QString & ) + { return 0; } + virtual bool generateDataForCommonFile( + SystemFile & , long , ANetNodeInstance * ) + {return 1; } + +private : + +}; + +class FakeNetNodeInstance : public ANetNodeInstance { + +public: + + FakeNetNodeInstance( ANetNode * NN ) : + ANetNodeInstance( NN ), ValAttrPairs() { } + virtual ~FakeNetNodeInstance( void ) { } + + virtual RuntimeInfo * runtime( void ) + { return 0; } + + // create edit widget under parent + virtual QWidget * edit( QWidget * ) + { return 0; } + // is given data acceptable + virtual QString acceptable( void ) + { return QString(""); } + + // get data from GUI and store in node + virtual void commit( void ) {} + + // get next node + ANetNodeInstance * nextNode() + { return 0; } + // return NetNode this is an instance of + + // intialize am instance of a net node + void initialize( void ){} + + // returns node specific data -> only useful for 'buddy' + virtual void * data( void ) + { return 0; } + +protected : + + virtual void setSpecificAttribute( QString & , QString & ); + virtual void saveSpecificAttribute( QTextStream & ); + + QDict ValAttrPairs; +}; + +extern FakeNetNode * FakeNode; + #endif diff --git a/noncore/settings/networksettings2/networksettings2/resources.cpp b/noncore/settings/networksettings2/networksettings2/resources.cpp index ff6e457..15e1b64 100644 --- a/noncore/settings/networksettings2/networksettings2/resources.cpp +++ b/noncore/settings/networksettings2/networksettings2/resources.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "netnode.h" @@ -132,9 +133,6 @@ bool TheNSResources::loadNetNode( QLibrary *lib = new QLibrary(pluginFileName); void * res = lib->resolve(resolveString); if( ! res ){ -#ifdef DEBUG - qDebug("loadNetNode: Warning: %s is not a plugin", pluginFileName.latin1()); -#endif delete lib; return 0; } @@ -146,9 +144,6 @@ bool TheNSResources::loadNetNode( getNetNodeList( PNN ); if( PNN.isEmpty() ) { -#ifdef DEBUG - qDebug("loadNetNode: Couldn't get node list, but did load library!"); -#endif delete lib; return 0; } diff --git a/noncore/settings/networksettings2/networksettings2/resources.h b/noncore/settings/networksettings2/networksettings2/resources.h index cfa0b7a..4df3ce3 100644 --- a/noncore/settings/networksettings2/networksettings2/resources.h +++ b/noncore/settings/networksettings2/networksettings2/resources.h @@ -52,10 +52,15 @@ public : ANetNodeInstance * createNodeInstance( const QString & S ) { ANetNodeInstance * NNI = 0; NetNode_t * NNT = AllNodeTypes[S]; - if( NNT ) { - NNI = NNT->NetNode->createInstance(); - NNI->initialize(); + ANetNode * NN; + if( ! NNT ) { + NN = FakeNode = + ( FakeNode ) ? FakeNode : new FakeNetNode(); + } else { + NN = NNT->NetNode; } + NNI = NN->createInstance(); + NNI->initialize(); return NNI; } diff --git a/noncore/settings/networksettings2/nsdata.cpp b/noncore/settings/networksettings2/nsdata.cpp new file mode 100644 index 0000000..9b2ad3a --- a/dev/null +++ b/noncore/settings/networksettings2/nsdata.cpp @@ -0,0 +1,438 @@ +#include +#include +#include +#include +#include +#include + +#include "nsdata.h" +#include +#include + +static QString CfgFile; + +NetworkSettingsData::NetworkSettingsData( void ) { + // init global resources structure + new TheNSResources(); + + CfgFile.sprintf( "%s/NETCONFIG", getenv("HOME") ); + + // load settings + Force = 0; + loadSettings(); +} + +// saving is done by caller +NetworkSettingsData::~NetworkSettingsData( void ) { + delete NSResources; +} + +void NetworkSettingsData::loadSettings( void ) { + QString S; + ANetNodeInstance* NNI; + QString Attr, Value; + long idx; + + QFile F( CfgFile ); + QTextStream TS( &F ); + + do { + + if( ! F.open(IO_ReadOnly) ) + break; + + /* load the file -> + + FORMAT : + + [NETNODETYPE] + Entries ... + + [connection] + Name=Name + Node=Name + + */ + while( ! TS.atEnd() ) { + S = TS.readLine(); + + if ( S.isEmpty() || S[0] != '[' ) + continue; + + S = S.mid( 1, S.length()-2 ); + + if( ! NSResources ) { + continue; + } + + if( S == "connection" ) { + // load connections -> collections of nodes + NodeCollection * NC = new NodeCollection( TS ); + NSResources->addConnection( NC ); + } else { + // load nodes + NNI = NSResources->createNodeInstance( S ); + if( ! NNI ) { + printf( "SKIPPING %s\n", S.latin1() ); + } + + do { + S = TS.readLine(); + if( S.isEmpty() ) { + // empty line + break; + } + // node found ? + if( NNI ) { + idx = S.find( '=' ); + if( idx > 0 ) { + Attr = S.left( idx ); + Value = S.mid( idx+1, S.length() ); + } else { + Value=""; + Attr = S; + } + + Value.stripWhiteSpace(); + Attr.stripWhiteSpace(); + Attr.lower(); + // dequote Attr + Value = deQuote(Value); + + // set the attribute + NNI->setAttribute( Attr, Value ); + } + + } while( 1 ); + if( NNI ) { + // loading from file -> exists + NNI->setNew( FALSE ); + NSResources->addNodeInstance( NNI ); + } + } + } + + } while( 0 ); + +} + +QString NetworkSettingsData::saveSettings( void ) { + QString ErrS = ""; + + if( ! isModified() ) + return ErrS; + + QString S; + QFile F( CfgFile + ".bup" ); + + printf( "Saving settings to %s\n", CfgFile.latin1() ); + if( ! F.open( IO_WriteOnly | IO_Truncate ) ) { + ErrS = qApp->translate( "NetworkSettings", + "

Could not save setup to %1 !

" ). + arg(CfgFile); + // problem + return ErrS; + } + + QTextStream TS( &F ); + { Name2Connection_t & M = NSResources->connections(); + ANetNodeInstance * NNI; + + // for all connections + for( QDictIterator it(M); + it.current(); + ++it ) { + // all nodes in those connections + for( QListIterator nit(*(it.current())); + nit.current(); + ++nit ) { + // header + NNI = nit.current(); + TS << '[' <netNode()->nodeName() << ']' << endl; + NNI->saveAttributes( TS ); + TS << endl; + } + + TS << "[connection]" << endl; + it.current()->save(TS); + } + } + + QDir D("."); + D.rename( CfgFile + ".bup", CfgFile ); + + // + // proper files AND system files regenerated + // + + setModified( 0 ); + return ErrS; +} + +QString NetworkSettingsData::generateSettings( bool ForceReq ) { + bool ForceIt; + QString S = ""; + + // include own force flag + ForceIt = (Force) ? 1 : ForceReq; + + if( ! ForceIt && ! isModified() ) + return S; + + // regenerate system files + printf( "Generating settings from %s\n", CfgFile.latin1() ); + + { Name2SystemFile_t & SFM = NSResources->systemFiles(); + Name2Connection_t & M = NSResources->connections(); + NodeCollection * NC; + ANetNodeInstance * NNI; + SystemFile * SF; + bool needToRegenerate = ForceIt; + + // + // check if we need to generate at least one of the system files + // + if( ! ForceIt ) { + for( QDictIterator sfit(SFM); + sfit.current(); + ++sfit ) { + SF = sfit.current(); + + // check if there are nodes that are modified and require + // data for this system file + + // for all connections + for( QDictIterator ncit(M); + ncit.current(); + ++ncit ) { + NC = ncit.current(); + + if( NC->isModified() ) { + // does this connection 'touch' this system file ? + for( QListIterator cncit(*NC); + cncit.current(); + ++cncit ) { + NNI = cncit.current(); + if( NNI->netNode()->hasDataFor( SF->name() ) && + NNI->isModified() ) { + needToRegenerate = 1; + break; + } + } + } + if( needToRegenerate ) + break; + } + if( needToRegenerate ) + break; + } + } + + // we cannot renumber with a FORCE request since + // we probably are NOT going to save the config + // e.g. when using --regen option + if( ! ForceReq && needToRegenerate ) { + NSResources->renumberConnections(); + setModified(1); + } + + // + // generate files proper to each netnodeinstance + // + { Name2Instance_t & NNIs = NSResources->netNodeInstances(); + ANetNodeInstance * NNI; + + for( QDictIterator NNIIt(NNIs); + NNIIt.current(); + ++NNIIt + ){ + // for all nodes find those that are modified + NNI = NNIIt.current(); + + if( ForceIt || NNI->isModified() ) { + if( ! NNI->netNode()->generateProperFilesFor( NNI ) ) { + // problem generating + S = qApp->translate( "NetworkSettings", + "

Cannot generate files proper to %1

" ). + arg(NNI->netNode()->nodeName()) ; + return S; + } + } + } + } + + // + // generate system files + // + for( QDictIterator sfit(SFM); + sfit.current(); + ++sfit ) { + SF = sfit.current(); + + // + // regenerate current file + // + printf( "Generating %s\n", SF->name().latin1() ); + SF->open(); + + do { // so we can break; + + if( SF->preSection() ) { + S = qApp->translate( "NetworkSettings", + "

Error in preSection for file %1

" ). + arg( SF->name() ); + return S; + } + + for( QDictIterator ncit(M); + ncit.current(); + ++ncit ) { + NC = ncit.current(); + + // get the netnode that serves as the device for this + // connection + AsDevice * Dev = NC->device(); + + // generate 'entry' for every possible device this profile handles + + for( QListIterator cncit(*NC); + cncit.current(); + ++cncit ) { + NNI = cncit.current(); + for( int i = 0; i < Dev->count(); i ++ ) { + if( NNI->netNode()->hasDataFor( SF->name() ) ) { + if( SF->preNodeSection( NNI, i ) ) { + S = qApp->translate( "NetworkSettings", + "

Error in preNodeSection for file %1 and node %2

" ). + arg( SF->name() ). + arg( NNI->netNode()->nodeName() ); + return S; + } + + if( NNI->netNode()->generateDataForCommonFile(*SF,i,NNI) ) { + S = qApp->translate( "NetworkSettings", + "

Error in node part for file %1 and node %2

" ). + arg( SF->name() ). + arg( NNI->netNode()->nodeName() ); + return S; + } + + if( SF->postNodeSection( NNI, i ) ) { + S = qApp->translate( "NetworkSettings", + "

Error in postNodeSection for file %1 and node %2

" ). + arg( SF->name() ). + arg( NNI->netNode()->nodeName() ); + return S; + } + } + } + } + *SF << endl; + } + + if( SF->postSection() ) { + S = qApp->translate( "NetworkSettings", + "

Error in postSection for file %1

" ). + arg( SF->name() ); + return S; + } + } while( 0 ); + SF->close(); + } + } + Force = 0; + return S; +} + +QList NetworkSettingsData::collectPossible( const char * Interface ) { + // collect connections that can work on top of this interface + NodeCollection * NC; + QList PossibleConnections; + Name2Connection_t & M = NSResources->connections(); + + // for all connections + for( QDictIterator it(M); + it.current(); + ++it ) { + NC = it.current(); + // check if this profile handles the requested interface + if( NC->handlesInterface( Interface ) && // if different Intf. + NC->state() != Disabled && // if not enabled + NC->state() != IsUp // if already used + ) { + PossibleConnections.append( NC ); + } + } + return PossibleConnections; +} + + +/* + Called by the system to see if interface can be brought UP + + if allowed, echo Interface-allowed else Interface-disallowed +*/ + +void NetworkSettingsData::canStart( const char * Interface ) { + // load situation + NodeCollection * NC = 0; + QList PossibleConnections; + + PossibleConnections = collectPossible( Interface ); + + switch( PossibleConnections.count() ) { + case 0 : // no connections + break; + case 1 : // one connection + NC = PossibleConnections.first(); + break; + default : // need to ask user ? + // are we connected to a server + // system( "su %d networksettings2 --prompt %s\n", + // "", Interface ); + break; + } + + if( NC ) { + switch( NC->state() ) { + case Unchecked : + case Unknown : + case Unavailable : + case Disabled : + // this profile does not allow interface to be UP + // -> try others + break; + case Off : + // try to UP the device + if( ! NC->setState( Activate ) ) { + // cannot bring device Online -> try other alters + break; + } + // FT + case Available : + case IsUp : // also called for 'ifdown' + // device is ready -> done + printf( "%s-c%d-allowed\n", Interface, NC->number() ); + return; + } + } else { + // if we come here no alternatives are possible + printf( "%s-cnn-disallowed\n", Interface ); + } +} + +/* + Called by the system to regenerate config files +*/ + +bool NetworkSettingsData::regenerate( void ) { + QString S; + // load situation + S = generateSettings( TRUE ); + if( ! S.isEmpty() ) { + fprintf( stdout, "%s\n", S.latin1() ); + return 1; + } + return 0; +} + diff --git a/noncore/settings/networksettings2/nsdata.h b/noncore/settings/networksettings2/nsdata.h new file mode 100644 index 0000000..55f8c71 --- a/dev/null +++ b/noncore/settings/networksettings2/nsdata.h @@ -0,0 +1,37 @@ +#ifndef __NSDATA_H +#define __NSDATA_H + +#include "netnode.h" + +class NetworkSettingsData { + +public : + + NetworkSettingsData( void ); + ~NetworkSettingsData( void ); + + void loadSettings( void ); + QString saveSettings( void ); + + QString generateSettings( bool Force = FALSE ); + + bool isModified( void ) + { return IsModified; } + void setModified( bool m ) + { IsModified = m; } + + QList collectPossible( const char * Interface ); + void canStart( const char * Interface ); + bool regenerate( void ); + + void forceGeneration( bool m ) + { Force = m; } + +private : + + bool IsModified; + bool Force; + +}; + +#endif -- cgit v0.9.0.2