summaryrefslogtreecommitdiff
path: root/noncore
authorwimpie <wimpie>2004-04-03 21:33:30 (UTC)
committer wimpie <wimpie>2004-04-03 21:33:30 (UTC)
commitd82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843 (patch) (unidiff)
tree32ccf2da6464c498c4c43c750e1391a06a1ad431 /noncore
parent41ba1a87a850785caf94d0b50675281fb6561168 (diff)
downloadopie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.zip
opie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.tar.gz
opie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.tar.bz2
Factored out non-gui data class
Added support for prompting
Diffstat (limited to 'noncore') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/main.cpp61
-rw-r--r--noncore/settings/networksettings2/networksettings.cpp434
-rw-r--r--noncore/settings/networksettings2/networksettings.h32
-rw-r--r--noncore/settings/networksettings2/networksettings.pro7
-rw-r--r--noncore/settings/networksettings2/networksettings2/netnode.cpp24
-rw-r--r--noncore/settings/networksettings2/networksettings2/netnode.h79
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.cpp7
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.h11
-rw-r--r--noncore/settings/networksettings2/nsdata.cpp438
-rw-r--r--noncore/settings/networksettings2/nsdata.h37
10 files changed, 651 insertions, 479 deletions
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 @@
1#include "nsdata.h"
2#include "activateprofile.h"
1#include "networksettings.h" 3#include "networksettings.h"
2#include <qpe/qpeapplication.h> 4#include <qpe/qpeapplication.h>
3 5
@@ -9,9 +11,14 @@ OPIE_EXPORT_APP( OApplicationFactory<NetworkSettings> )
9 11
10#else 12#else
11 13
14// just standard GUI
12#define ACT_GUI 0 15#define ACT_GUI 0
16// used by interfaces to request for allow of up/down
13#define ACT_REQUEST 1 17#define ACT_REQUEST 1
18// regenerate config files
14#define ACT_REGEN 2 19#define ACT_REGEN 2
20// used by interfaces to request user prompt
21#define ACT_PROMPT 3
15 22
16int main( int argc, char * argv[] ) { 23int main( int argc, char * argv[] ) {
17 int rv = 0; 24 int rv = 0;
@@ -32,6 +39,9 @@ int main( int argc, char * argv[] ) {
32 Action = ACT_REGEN; 39 Action = ACT_REGEN;
33 GuiType = QApplication::Tty; 40 GuiType = QApplication::Tty;
34 rmv = 1; 41 rmv = 1;
42 } else if( strcmp( argv[i], "--prompt" ) == 0 ) {
43 Action = ACT_PROMPT;
44 rmv = 1;
35 } 45 }
36 if( rmv ) { 46 if( rmv ) {
37 memmove( argv+i, argv+i+rmv, 47 memmove( argv+i, argv+i+rmv,
@@ -62,28 +72,41 @@ int main( int argc, char * argv[] ) {
62#endif 72#endif
63 73
64 // init qt with app widget 74 // init qt with app widget
65 if( GuiType != QApplication::Tty ) { 75
66 QWidget * W = 0; 76 switch( Action ) {
67 W = new NetworkSettings(0); 77 case ACT_REQUEST :
68 TheApp->setMainWidget( W ); 78 { NetworkSettingsData NS;
69 W->show(); 79 NS.canStart( argv[1] );
80 }
81 break;
82 case ACT_REGEN :
83 { NetworkSettingsData NS;
84 // regen returns 0 if OK
85 rv = (NS.regenerate()) ? 1 : 0;
86 }
87 break;
88 case ACT_PROMPT :
89 { ActivateProfile AP(argv[1]);
90 if( AP.exec() == QDialog::Accepted ) {
91 printf( "%s-c%d-allowed", AP.selectedProfile() );
92 } else {
93 printf( "%s-cNN-disallowed" );
94 }
95 }
96 break;
97 case ACT_GUI :
98 { QWidget * W = new NetworkSettings(0);
99 TheApp->setMainWidget( W );
100 W->show();
70#ifdef _WS_QWS_ 101#ifdef _WS_QWS_
71 W->showMaximized(); 102 W->showMaximized();
72#else 103#else
73 W->resize( W->sizeHint() ); 104 W->resize( W->sizeHint() );
74#endif 105#endif
75 rv = TheApp->exec(); 106 rv = TheApp->exec();
76 delete W; 107 delete W;
77 } else { 108 }
78 switch( Action ) { 109 break;
79 case ACT_REQUEST :
80 NetworkSettings::canStart( argv[1] );
81 break;
82 case ACT_REGEN :
83 // regen returns 0 if OK
84 rv = (NetworkSettings::regenerate()) ? 1 : 0;
85 break;
86 }
87 } 110 }
88 111
89 return rv; 112 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 @@
8#include <qmessagebox.h> 8#include <qmessagebox.h>
9#include <qlabel.h> 9#include <qlabel.h>
10#include <qiconview.h> 10#include <qiconview.h>
11#include <qtextstream.h>
12#include <qdir.h>
13#include <qfile.h>
14#include <qfileinfo.h>
15#include <qtimer.h> 11#include <qtimer.h>
16#include <qpe/qpeapplication.h> 12#include <qpe/qpeapplication.h>
17#include <qtoolbutton.h> 13#include <qtoolbutton.h>
@@ -21,359 +17,6 @@
21#include "netnode.h" 17#include "netnode.h"
22#include "editconnection.h" 18#include "editconnection.h"
23 19
24static QString CfgFile;
25
26NetworkSettingsData::NetworkSettingsData( void ) {
27 // init global resources structure
28 new TheNSResources();
29
30 CfgFile.sprintf( "%s/NETCONFIG", getenv("HOME") );
31
32 // load settings
33 Force = 0;
34 loadSettings();
35}
36
37// saving is done by caller
38NetworkSettingsData::~NetworkSettingsData( void ) {
39 delete NSResources;
40}
41
42void NetworkSettingsData::loadSettings( void ) {
43 QString S;
44 ANetNodeInstance* NNI;
45 QString Attr, Value;
46 long idx;
47
48 QFile F( CfgFile );
49 QTextStream TS( &F );
50
51 do {
52
53 if( ! F.open(IO_ReadOnly) )
54 break;
55
56 /* load the file ->
57
58 FORMAT :
59
60 [NETNODETYPE]
61 Entries ...
62 <EMPTYLINE>
63 [connection]
64 Name=Name
65 Node=Name
66 <EMPTYLINE>
67 */
68 while( ! TS.atEnd() ) {
69 S = TS.readLine();
70
71 if ( S.isEmpty() || S[0] != '[' )
72 continue;
73
74 S = S.mid( 1, S.length()-2 );
75
76 if( ! NSResources ) {
77 continue;
78 }
79
80 if( S == "connection" ) {
81 // load connections -> collections of nodes
82 NodeCollection * NC = new NodeCollection( TS );
83 if ( NC->count() == 0 ) {
84 if( QMessageBox::warning(
85 0,
86 qApp->translate( "NetworkSettings2", "Invalid connection" ),
87 qApp->translate( "NetworkSettings2",
88 "<p>Connection %1 contains unrecognized nodes and cannot be loaded</p>" ).arg(NC->name()),
89 qApp->translate( "NetworkSettings2",
90 "Remove node"),
91 qApp->translate( "NetworkSettings2",
92 "Exit program") ) == 1 ) {
93 exit( 0 );
94 }
95 delete NC;
96 } else
97 NSResources->addConnection( NC );
98 } else {
99 // load nodes
100 NNI = NSResources->createNodeInstance( S );
101 if( ! NNI ) {
102 printf( "SKIPPING %s\n", S.latin1() );
103 }
104
105 do {
106 S = TS.readLine();
107 if( S.isEmpty() ) {
108 // empty line
109 break;
110 }
111 // node found ?
112 if( NNI ) {
113 idx = S.find( '=' );
114 if( idx > 0 ) {
115 Attr = S.left( idx );
116 Value = S.mid( idx+1, S.length() );
117 } else {
118 Value="";
119 Attr = S;
120 }
121
122 Value.stripWhiteSpace();
123 Attr.stripWhiteSpace();
124 Attr.lower();
125 // dequote Attr
126 Value = deQuote(Value);
127
128 // set the attribute
129 NNI->setAttribute( Attr, Value );
130 }
131
132 } while( 1 );
133 if( NNI ) {
134 // loading from file -> exists
135 NNI->setNew( FALSE );
136 NSResources->addNodeInstance( NNI );
137 }
138 }
139 }
140
141 } while( 0 );
142
143}
144
145QString NetworkSettingsData::saveSettings( void ) {
146 QString ErrS = "";
147
148 if( ! isModified() )
149 return ErrS;
150
151 QString S;
152 QFile F( CfgFile + ".bup" );
153
154 printf( "Saving settings to %s\n", CfgFile.latin1() );
155 if( ! F.open( IO_WriteOnly | IO_Truncate ) ) {
156 ErrS = qApp->translate( "NetworkSettings",
157 "<p>Could not save setup to %1 !</p>" ).
158 arg(CfgFile);
159 // problem
160 return ErrS;
161 }
162
163 QTextStream TS( &F );
164 { Name2Connection_t & M = NSResources->connections();
165 ANetNodeInstance * NNI;
166
167 // for all connections
168 for( QDictIterator<NodeCollection> it(M);
169 it.current();
170 ++it ) {
171 // all nodes in those connections
172 for( QListIterator<ANetNodeInstance> nit(*(it.current()));
173 nit.current();
174 ++nit ) {
175 // header
176 NNI = nit.current();
177 TS << '[' <<NNI->netNode()->nodeName() << ']' << endl;
178 NNI->saveAttributes( TS );
179 TS << endl;
180 }
181
182 TS << "[connection]" << endl;
183 it.current()->save(TS);
184 }
185 }
186
187 QDir D(".");
188 D.rename( CfgFile + ".bup", CfgFile );
189
190 //
191 // proper files AND system files regenerated
192 //
193
194 setModified( 0 );
195 return ErrS;
196}
197
198QString NetworkSettingsData::generateSettings( bool ForceReq ) {
199 bool ForceIt;
200 QString S = "";
201
202 // include own force flag
203 ForceIt = (Force) ? 1 : ForceReq;
204
205 if( ! ForceIt && ! isModified() )
206 return S;
207
208 // regenerate system files
209 printf( "Generating settings from %s\n", CfgFile.latin1() );
210
211 { Name2SystemFile_t & SFM = NSResources->systemFiles();
212 Name2Connection_t & M = NSResources->connections();
213 NodeCollection * NC;
214 ANetNodeInstance * NNI;
215 SystemFile * SF;
216 bool needToRegenerate = ForceIt;
217
218 //
219 // check if we need to generate at least one of the system files
220 //
221 if( ! ForceIt ) {
222 for( QDictIterator<SystemFile> sfit(SFM);
223 sfit.current();
224 ++sfit ) {
225 SF = sfit.current();
226
227 // check if there are nodes that are modified and require
228 // data for this system file
229
230 // for all connections
231 for( QDictIterator<NodeCollection> ncit(M);
232 ncit.current();
233 ++ncit ) {
234 NC = ncit.current();
235
236 if( NC->isModified() ) {
237 // does this connection 'touch' this system file ?
238 for( QListIterator<ANetNodeInstance> cncit(*NC);
239 cncit.current();
240 ++cncit ) {
241 NNI = cncit.current();
242 if( NNI->netNode()->hasDataFor( SF->name() ) &&
243 NNI->isModified() ) {
244 needToRegenerate = 1;
245 break;
246 }
247 }
248 }
249 if( needToRegenerate )
250 break;
251 }
252 if( needToRegenerate )
253 break;
254 }
255 }
256
257 // we cannot renumber with a FORCE request since
258 // we probably are NOT going to save the config
259 // e.g. when using --regen option
260 if( ! ForceReq && needToRegenerate ) {
261 NSResources->renumberConnections();
262 setModified(1);
263 }
264
265 //
266 // generate files proper to each netnodeinstance
267 //
268 { Name2Instance_t & NNIs = NSResources->netNodeInstances();
269 ANetNodeInstance * NNI;
270
271 for( QDictIterator<ANetNodeInstance> NNIIt(NNIs);
272 NNIIt.current();
273 ++NNIIt
274 ){
275 // for all nodes find those that are modified
276 NNI = NNIIt.current();
277
278 if( ForceIt || NNI->isModified() ) {
279 if( ! NNI->netNode()->generateProperFilesFor( NNI ) ) {
280 // problem generating
281 S = qApp->translate( "NetworkSettings",
282 "<p>Cannot generate files proper to %1</p>" ).
283 arg(NNI->netNode()->nodeName()) ;
284 return S;
285 }
286 }
287 }
288 }
289
290 //
291 // generate system files
292 //
293 for( QDictIterator<SystemFile> sfit(SFM);
294 sfit.current();
295 ++sfit ) {
296 SF = sfit.current();
297
298 //
299 // regenerate current file
300 //
301 printf( "Generating %s\n", SF->name().latin1() );
302 SF->open();
303
304 do { // so we can break;
305
306 if( SF->preSection() ) {
307 S = qApp->translate( "NetworkSettings",
308 "<p>Error in preSection for file %1</p>" ).
309 arg( SF->name() );
310 return S;
311 }
312
313 for( QDictIterator<NodeCollection> ncit(M);
314 ncit.current();
315 ++ncit ) {
316 NC = ncit.current();
317
318 // get the netnode that serves as the device for this
319 // connection
320 AsDevice * Dev = NC->device();
321
322 // generate 'entry' for every possible device this profile handles
323
324 for( QListIterator<ANetNodeInstance> cncit(*NC);
325 cncit.current();
326 ++cncit ) {
327 NNI = cncit.current();
328 for( int i = 0; i < Dev->count(); i ++ ) {
329 if( NNI->netNode()->hasDataFor( SF->name() ) ) {
330 if( SF->preNodeSection( NNI, i ) ) {
331 S = qApp->translate( "NetworkSettings",
332 "<p>Error in preNodeSection for file %1 and node %2</p>" ).
333 arg( SF->name() ).
334 arg( NNI->netNode()->nodeName() );
335 return S;
336 }
337
338 if( NNI->netNode()->generateDataForCommonFile(*SF,i,NNI) ) {
339 S = qApp->translate( "NetworkSettings",
340 "<p>Error in node part for file %1 and node %2</p>" ).
341 arg( SF->name() ).
342 arg( NNI->netNode()->nodeName() );
343 return S;
344 }
345
346 if( SF->postNodeSection( NNI, i ) ) {
347 S = qApp->translate( "NetworkSettings",
348 "<p>Error in postNodeSection for file %1 and node %2</p>" ).
349 arg( SF->name() ).
350 arg( NNI->netNode()->nodeName() );
351 return S;
352 }
353 }
354 }
355 }
356 *SF << endl;
357 }
358
359 if( SF->postSection() ) {
360 S = qApp->translate( "NetworkSettings",
361 "<p>Error in postSection for file %1</p>" ).
362 arg( SF->name() );
363 return S;
364 }
365 } while( 0 );
366 SF->close();
367 }
368 }
369 Force = 0;
370 return S;
371}
372
373//
374// GUI part
375//
376
377NetworkSettings::NetworkSettings( QWidget *parent, 20NetworkSettings::NetworkSettings( QWidget *parent,
378 const char *name, 21 const char *name,
379 WFlags fl ) : NetworkSettingsGUI(parent,name,fl), 22 WFlags fl ) : NetworkSettingsGUI(parent,name,fl),
@@ -420,6 +63,10 @@ NetworkSettings::NetworkSettings( QWidget *parent,
420 UpdateTimer->start( 5000 ); 63 UpdateTimer->start( 5000 );
421 connect( UpdateTimer, SIGNAL( timeout() ), 64 connect( UpdateTimer, SIGNAL( timeout() ),
422 this, SLOT( SLOT_RefreshStates() ) ); 65 this, SLOT( SLOT_RefreshStates() ) );
66
67 /* Add QCopChannel */
68 connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)),
69 this, SLOT(SLOT_QCopMessage(const QCString&,const QByteArray&)) );
423} 70}
424 71
425NetworkSettings::~NetworkSettings() { 72NetworkSettings::~NetworkSettings() {
@@ -753,68 +400,15 @@ void NetworkSettings::SLOT_Connect( void ) {
753 // we do not update the GUI but wait for the REAL upping of the device 400 // we do not update the GUI but wait for the REAL upping of the device
754} 401}
755 402
756/* 403void NetworkSettings::SLOT_QCopMessage(const QCString &msg, const QByteArray &data) {
757 Called by the system to see if interface can be brought UP 404 QDataStream stream( data, IO_ReadOnly );
758
759 if allowed, echo Interface-allowed else Interface-disallowed
760*/
761
762void NetworkSettings::canStart( const char * Interface ) {
763 // load situation
764 NetworkSettingsData NSD;
765
766 { Name2Connection_t & M = NSResources->connections();
767 NodeCollection * NC;
768
769 // for all connections
770 for( QDictIterator<NodeCollection> it(M);
771 it.current();
772 ++it ) {
773 NC = it.current();
774 // check if this profile handles the requested interface
775 if( NC->handlesInterface( Interface ) ) {
776 switch( NC->state() ) {
777 case Unchecked :
778 case Unknown :
779 case Unavailable :
780 case Disabled :
781 // this profile does not allow interface to be UP
782 // -> try others
783 break;
784 case Off :
785 // try to UP the device
786 if( ! NC->setState( Activate ) ) {
787 // cannot bring device Online -> try other alters
788 break;
789 }
790 // FT
791 case Available :
792 case IsUp : // also called for 'down'
793 // device is ready -> done
794 printf( "%s-c%d-allowed\n",
795 Interface, NC->number() );
796 return;
797 }
798 }
799 }
800 }
801 // if we come here no alternatives are possible
802 printf( "%s-cnn-disallowed\n", Interface );
803}
804
805/*
806 Called by the system to regenerate config files
807*/
808
809bool NetworkSettings::regenerate( void ) {
810 QString S;
811 // load situation
812 NetworkSettingsData NSD;
813 405
814 S = NSD.generateSettings( TRUE ); 406 if( msg == "raise" ) {
815 if( ! S.isEmpty() ) { 407 raise();
816 fprintf( stdout, "%s\n", S.latin1() ); 408 return;
817 return 1; 409 } /* if ( msg == "someMessage(int,int,int)" ) {
818 } 410 int a,b,c;
819 return 0; 411 stream >> a >> b >> c;
412 ...
413 } */
820} 414}
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 @@
1#include "nsdata.h"
1#include "networksettingsGUI.h" 2#include "networksettingsGUI.h"
2#include "resources.h" 3#include "resources.h"
3 4
@@ -6,33 +7,6 @@ class ANetNodeInstance;
6class QTimer; 7class QTimer;
7class QIconViewItem; 8class QIconViewItem;
8 9
9class NetworkSettingsData {
10
11public :
12
13 NetworkSettingsData( void );
14 ~NetworkSettingsData( void );
15
16 void loadSettings( void );
17 QString saveSettings( void );
18
19 QString generateSettings( bool Force = FALSE );
20
21 bool isModified( void )
22 { return IsModified; }
23 void setModified( bool m )
24 { IsModified = m; }
25
26 void forceGeneration( bool m )
27 { Force = m; }
28
29private :
30
31 bool IsModified;
32 bool Force;
33
34};
35
36class NetworkSettings : public NetworkSettingsGUI { 10class NetworkSettings : public NetworkSettingsGUI {
37 11
38 Q_OBJECT 12 Q_OBJECT
@@ -47,9 +21,6 @@ public :
47 static QString appName( void ) 21 static QString appName( void )
48 { return QString::fromLatin1("networksettings"); } 22 { return QString::fromLatin1("networksettings"); }
49 23
50 static void canStart( const char * Interface );
51 static bool regenerate( void );
52
53 bool isModified( void ) 24 bool isModified( void )
54 { return NSD.isModified(); } 25 { return NSD.isModified(); }
55 void setModified( bool m ) 26 void setModified( bool m )
@@ -67,6 +38,7 @@ public slots :
67 void SLOT_Connect( void ); 38 void SLOT_Connect( void );
68 void SLOT_GenerateConfig( void ); 39 void SLOT_GenerateConfig( void );
69 void SLOT_RefreshStates( void ); 40 void SLOT_RefreshStates( void );
41 void SLOT_QCopMessage( const QCString&,const QByteArray& );
70 42
71private : 43private :
72 44
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 @@
2CONFIG = qt warn_on debug quick-app 2CONFIG = qt warn_on debug quick-app
3#CONFIG = qt warn_on release 3#CONFIG = qt warn_on release
4HEADERS = networksettings.h \ 4HEADERS = networksettings.h \
5 activateprofile.h \
5 editconnection.h 6 editconnection.h
6SOURCES = main.cpp \ 7SOURCES = main.cpp \
7 networksettings.cpp \ 8 networksettings.cpp \
9 nsdata.cpp \
10 activateprofile.cpp \
8 editconnection.cpp 11 editconnection.cpp
9INCLUDEPATH += $(OPIEDIR)/include networksettings2/ 12INCLUDEPATH += $(OPIEDIR)/include networksettings2/
10DEPENDPATH += $(OPIEDIR)/include networksettings2/ 13DEPENDPATH += $(OPIEDIR)/include networksettings2/
11LIBS += -lqpe -L$(OPIEDIR)/plugins/networksettings2 -lnetworksettings2 -lopie 14LIBS += -lqpe -L$(OPIEDIR)/plugins/networksettings2 -lnetworksettings2 -lopie
12INTERFACES = networksettingsGUI.ui editconnectionGUI.ui 15INTERFACES = networksettingsGUI.ui \
16 editconnectionGUI.ui \
17 activateprofileGUI.ui
13TARGET = networksettings2 18TARGET = networksettings2
14 19
15 20
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 ) {
276AsDevice * RuntimeInfo::device( void ) { 276AsDevice * RuntimeInfo::device( void ) {
277 return netNode()->nextNode()->runtime()->device(); 277 return netNode()->nextNode()->runtime()->device();
278} 278}
279
280ANetNodeInstance * FakeNetNode::createInstance( void ) {
281 return new FakeNetNodeInstance( this );
282}
283
284void FakeNetNodeInstance::setSpecificAttribute(
285 QString & A, QString & V ) {
286 ValAttrPairs.insert( A, new QString(V) );
287}
288
289void FakeNetNodeInstance::saveSpecificAttribute( QTextStream &TS ) {
290 for( QDictIterator<QString> it( ValAttrPairs );
291 it.current();
292 ++ it ) {
293 TS << it.currentKey().latin1()
294 << "="
295 << quote( *(it.current()))
296 << endl ;
297 ++it;
298 }
299}
300
301// collects all info that no plugin acceps
302FakeNetNode * 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 @@
3 3
4#include <qtextstream.h> 4#include <qtextstream.h>
5#include <qlist.h> 5#include <qlist.h>
6#include <qdict.h>
6#include <qpixmap.h> 7#include <qpixmap.h>
7#include <qobject.h> 8#include <qobject.h>
8#include <time.h> 9#include <time.h>
@@ -358,4 +359,82 @@ private :
358 359
359}; 360};
360 361
362//
363// special node that is used to remember entries for plugins
364// that seem missing. This way we never loose data
365//
366
367class FakeNetNode : public ANetNode {
368
369public:
370
371 FakeNetNode( ) { };
372 virtual ~FakeNetNode(){};
373
374 const QString pixmapName()
375 { return QString(""); }
376 const QString nodeName()
377 { return QString("Fake node" ); }
378 const QString nodeDescription()
379 { return QString("Fake node" ); }
380 ANetNodeInstance * createInstance( void );
381 const char * provides( void )
382 { return ""; }
383 virtual const char ** needs( void )
384 { return 0; }
385 virtual bool generateProperFilesFor( ANetNodeInstance * )
386 { return 0; }
387 virtual bool hasDataFor( const QString & )
388 { return 0; }
389 virtual bool generateDataForCommonFile(
390 SystemFile & , long , ANetNodeInstance * )
391 {return 1; }
392
393private :
394
395};
396
397class FakeNetNodeInstance : public ANetNodeInstance {
398
399public:
400
401 FakeNetNodeInstance( ANetNode * NN ) :
402 ANetNodeInstance( NN ), ValAttrPairs() { }
403 virtual ~FakeNetNodeInstance( void ) { }
404
405 virtual RuntimeInfo * runtime( void )
406 { return 0; }
407
408 // create edit widget under parent
409 virtual QWidget * edit( QWidget * )
410 { return 0; }
411 // is given data acceptable
412 virtual QString acceptable( void )
413 { return QString(""); }
414
415 // get data from GUI and store in node
416 virtual void commit( void ) {}
417
418 // get next node
419 ANetNodeInstance * nextNode()
420 { return 0; }
421 // return NetNode this is an instance of
422
423 // intialize am instance of a net node
424 void initialize( void ){}
425
426 // returns node specific data -> only useful for 'buddy'
427 virtual void * data( void )
428 { return 0; }
429
430protected :
431
432 virtual void setSpecificAttribute( QString & , QString & );
433 virtual void saveSpecificAttribute( QTextStream & );
434
435 QDict<QString> ValAttrPairs;
436};
437
438extern FakeNetNode * FakeNode;
439
361#endif 440#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 @@
2#include <qpe/qlibrary.h> 2#include <qpe/qlibrary.h>
3#include <qpe/qpeapplication.h> 3#include <qpe/qpeapplication.h>
4#include <qdir.h> 4#include <qdir.h>
5#include <opie2/odebug.h>
5#include <qtopia/resource.h> 6#include <qtopia/resource.h>
6 7
7#include "netnode.h" 8#include "netnode.h"
@@ -132,9 +133,6 @@ bool TheNSResources::loadNetNode(
132 QLibrary *lib = new QLibrary(pluginFileName); 133 QLibrary *lib = new QLibrary(pluginFileName);
133 void * res = lib->resolve(resolveString); 134 void * res = lib->resolve(resolveString);
134 if( ! res ){ 135 if( ! res ){
135#ifdef DEBUG
136 qDebug("loadNetNode: Warning: %s is not a plugin", pluginFileName.latin1());
137#endif
138 delete lib; 136 delete lib;
139 return 0; 137 return 0;
140 } 138 }
@@ -146,9 +144,6 @@ bool TheNSResources::loadNetNode(
146 144
147 getNetNodeList( PNN ); 145 getNetNodeList( PNN );
148 if( PNN.isEmpty() ) { 146 if( PNN.isEmpty() ) {
149#ifdef DEBUG
150 qDebug("loadNetNode: Couldn't get node list, but did load library!");
151#endif
152 delete lib; 147 delete lib;
153 return 0; 148 return 0;
154 } 149 }
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 :
52 ANetNodeInstance * createNodeInstance( const QString & S ) 52 ANetNodeInstance * createNodeInstance( const QString & S )
53 { ANetNodeInstance * NNI = 0; 53 { ANetNodeInstance * NNI = 0;
54 NetNode_t * NNT = AllNodeTypes[S]; 54 NetNode_t * NNT = AllNodeTypes[S];
55 if( NNT ) { 55 ANetNode * NN;
56 NNI = NNT->NetNode->createInstance(); 56 if( ! NNT ) {
57 NNI->initialize(); 57 NN = FakeNode =
58 ( FakeNode ) ? FakeNode : new FakeNetNode();
59 } else {
60 NN = NNT->NetNode;
58 } 61 }
62 NNI = NN->createInstance();
63 NNI->initialize();
59 return NNI; 64 return NNI;
60 } 65 }
61 66
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 @@
1#include <stdlib.h>
2#include <qpe/qpeapplication.h>
3#include <qtextstream.h>
4#include <qdir.h>
5#include <qfile.h>
6#include <qfileinfo.h>
7
8#include "nsdata.h"
9#include <asdevice.h>
10#include <resources.h>
11
12static QString CfgFile;
13
14NetworkSettingsData::NetworkSettingsData( void ) {
15 // init global resources structure
16 new TheNSResources();
17
18 CfgFile.sprintf( "%s/NETCONFIG", getenv("HOME") );
19
20 // load settings
21 Force = 0;
22 loadSettings();
23}
24
25// saving is done by caller
26NetworkSettingsData::~NetworkSettingsData( void ) {
27 delete NSResources;
28}
29
30void NetworkSettingsData::loadSettings( void ) {
31 QString S;
32 ANetNodeInstance* NNI;
33 QString Attr, Value;
34 long idx;
35
36 QFile F( CfgFile );
37 QTextStream TS( &F );
38
39 do {
40
41 if( ! F.open(IO_ReadOnly) )
42 break;
43
44 /* load the file ->
45
46 FORMAT :
47
48 [NETNODETYPE]
49 Entries ...
50 <EMPTYLINE>
51 [connection]
52 Name=Name
53 Node=Name
54 <EMPTYLINE>
55 */
56 while( ! TS.atEnd() ) {
57 S = TS.readLine();
58
59 if ( S.isEmpty() || S[0] != '[' )
60 continue;
61
62 S = S.mid( 1, S.length()-2 );
63
64 if( ! NSResources ) {
65 continue;
66 }
67
68 if( S == "connection" ) {
69 // load connections -> collections of nodes
70 NodeCollection * NC = new NodeCollection( TS );
71 NSResources->addConnection( NC );
72 } else {
73 // load nodes
74 NNI = NSResources->createNodeInstance( S );
75 if( ! NNI ) {
76 printf( "SKIPPING %s\n", S.latin1() );
77 }
78
79 do {
80 S = TS.readLine();
81 if( S.isEmpty() ) {
82 // empty line
83 break;
84 }
85 // node found ?
86 if( NNI ) {
87 idx = S.find( '=' );
88 if( idx > 0 ) {
89 Attr = S.left( idx );
90 Value = S.mid( idx+1, S.length() );
91 } else {
92 Value="";
93 Attr = S;
94 }
95
96 Value.stripWhiteSpace();
97 Attr.stripWhiteSpace();
98 Attr.lower();
99 // dequote Attr
100 Value = deQuote(Value);
101
102 // set the attribute
103 NNI->setAttribute( Attr, Value );
104 }
105
106 } while( 1 );
107 if( NNI ) {
108 // loading from file -> exists
109 NNI->setNew( FALSE );
110 NSResources->addNodeInstance( NNI );
111 }
112 }
113 }
114
115 } while( 0 );
116
117}
118
119QString NetworkSettingsData::saveSettings( void ) {
120 QString ErrS = "";
121
122 if( ! isModified() )
123 return ErrS;
124
125 QString S;
126 QFile F( CfgFile + ".bup" );
127
128 printf( "Saving settings to %s\n", CfgFile.latin1() );
129 if( ! F.open( IO_WriteOnly | IO_Truncate ) ) {
130 ErrS = qApp->translate( "NetworkSettings",
131 "<p>Could not save setup to %1 !</p>" ).
132 arg(CfgFile);
133 // problem
134 return ErrS;
135 }
136
137 QTextStream TS( &F );
138 { Name2Connection_t & M = NSResources->connections();
139 ANetNodeInstance * NNI;
140
141 // for all connections
142 for( QDictIterator<NodeCollection> it(M);
143 it.current();
144 ++it ) {
145 // all nodes in those connections
146 for( QListIterator<ANetNodeInstance> nit(*(it.current()));
147 nit.current();
148 ++nit ) {
149 // header
150 NNI = nit.current();
151 TS << '[' <<NNI->netNode()->nodeName() << ']' << endl;
152 NNI->saveAttributes( TS );
153 TS << endl;
154 }
155
156 TS << "[connection]" << endl;
157 it.current()->save(TS);
158 }
159 }
160
161 QDir D(".");
162 D.rename( CfgFile + ".bup", CfgFile );
163
164 //
165 // proper files AND system files regenerated
166 //
167
168 setModified( 0 );
169 return ErrS;
170}
171
172QString NetworkSettingsData::generateSettings( bool ForceReq ) {
173 bool ForceIt;
174 QString S = "";
175
176 // include own force flag
177 ForceIt = (Force) ? 1 : ForceReq;
178
179 if( ! ForceIt && ! isModified() )
180 return S;
181
182 // regenerate system files
183 printf( "Generating settings from %s\n", CfgFile.latin1() );
184
185 { Name2SystemFile_t & SFM = NSResources->systemFiles();
186 Name2Connection_t & M = NSResources->connections();
187 NodeCollection * NC;
188 ANetNodeInstance * NNI;
189 SystemFile * SF;
190 bool needToRegenerate = ForceIt;
191
192 //
193 // check if we need to generate at least one of the system files
194 //
195 if( ! ForceIt ) {
196 for( QDictIterator<SystemFile> sfit(SFM);
197 sfit.current();
198 ++sfit ) {
199 SF = sfit.current();
200
201 // check if there are nodes that are modified and require
202 // data for this system file
203
204 // for all connections
205 for( QDictIterator<NodeCollection> ncit(M);
206 ncit.current();
207 ++ncit ) {
208 NC = ncit.current();
209
210 if( NC->isModified() ) {
211 // does this connection 'touch' this system file ?
212 for( QListIterator<ANetNodeInstance> cncit(*NC);
213 cncit.current();
214 ++cncit ) {
215 NNI = cncit.current();
216 if( NNI->netNode()->hasDataFor( SF->name() ) &&
217 NNI->isModified() ) {
218 needToRegenerate = 1;
219 break;
220 }
221 }
222 }
223 if( needToRegenerate )
224 break;
225 }
226 if( needToRegenerate )
227 break;
228 }
229 }
230
231 // we cannot renumber with a FORCE request since
232 // we probably are NOT going to save the config
233 // e.g. when using --regen option
234 if( ! ForceReq && needToRegenerate ) {
235 NSResources->renumberConnections();
236 setModified(1);
237 }
238
239 //
240 // generate files proper to each netnodeinstance
241 //
242 { Name2Instance_t & NNIs = NSResources->netNodeInstances();
243 ANetNodeInstance * NNI;
244
245 for( QDictIterator<ANetNodeInstance> NNIIt(NNIs);
246 NNIIt.current();
247 ++NNIIt
248 ){
249 // for all nodes find those that are modified
250 NNI = NNIIt.current();
251
252 if( ForceIt || NNI->isModified() ) {
253 if( ! NNI->netNode()->generateProperFilesFor( NNI ) ) {
254 // problem generating
255 S = qApp->translate( "NetworkSettings",
256 "<p>Cannot generate files proper to %1</p>" ).
257 arg(NNI->netNode()->nodeName()) ;
258 return S;
259 }
260 }
261 }
262 }
263
264 //
265 // generate system files
266 //
267 for( QDictIterator<SystemFile> sfit(SFM);
268 sfit.current();
269 ++sfit ) {
270 SF = sfit.current();
271
272 //
273 // regenerate current file
274 //
275 printf( "Generating %s\n", SF->name().latin1() );
276 SF->open();
277
278 do { // so we can break;
279
280 if( SF->preSection() ) {
281 S = qApp->translate( "NetworkSettings",
282 "<p>Error in preSection for file %1</p>" ).
283 arg( SF->name() );
284 return S;
285 }
286
287 for( QDictIterator<NodeCollection> ncit(M);
288 ncit.current();
289 ++ncit ) {
290 NC = ncit.current();
291
292 // get the netnode that serves as the device for this
293 // connection
294 AsDevice * Dev = NC->device();
295
296 // generate 'entry' for every possible device this profile handles
297
298 for( QListIterator<ANetNodeInstance> cncit(*NC);
299 cncit.current();
300 ++cncit ) {
301 NNI = cncit.current();
302 for( int i = 0; i < Dev->count(); i ++ ) {
303 if( NNI->netNode()->hasDataFor( SF->name() ) ) {
304 if( SF->preNodeSection( NNI, i ) ) {
305 S = qApp->translate( "NetworkSettings",
306 "<p>Error in preNodeSection for file %1 and node %2</p>" ).
307 arg( SF->name() ).
308 arg( NNI->netNode()->nodeName() );
309 return S;
310 }
311
312 if( NNI->netNode()->generateDataForCommonFile(*SF,i,NNI) ) {
313 S = qApp->translate( "NetworkSettings",
314 "<p>Error in node part for file %1 and node %2</p>" ).
315 arg( SF->name() ).
316 arg( NNI->netNode()->nodeName() );
317 return S;
318 }
319
320 if( SF->postNodeSection( NNI, i ) ) {
321 S = qApp->translate( "NetworkSettings",
322 "<p>Error in postNodeSection for file %1 and node %2</p>" ).
323 arg( SF->name() ).
324 arg( NNI->netNode()->nodeName() );
325 return S;
326 }
327 }
328 }
329 }
330 *SF << endl;
331 }
332
333 if( SF->postSection() ) {
334 S = qApp->translate( "NetworkSettings",
335 "<p>Error in postSection for file %1</p>" ).
336 arg( SF->name() );
337 return S;
338 }
339 } while( 0 );
340 SF->close();
341 }
342 }
343 Force = 0;
344 return S;
345}
346
347QList<NodeCollection> NetworkSettingsData::collectPossible( const char * Interface ) {
348 // collect connections that can work on top of this interface
349 NodeCollection * NC;
350 QList<NodeCollection> PossibleConnections;
351 Name2Connection_t & M = NSResources->connections();
352
353 // for all connections
354 for( QDictIterator<NodeCollection> it(M);
355 it.current();
356 ++it ) {
357 NC = it.current();
358 // check if this profile handles the requested interface
359 if( NC->handlesInterface( Interface ) && // if different Intf.
360 NC->state() != Disabled && // if not enabled
361 NC->state() != IsUp // if already used
362 ) {
363 PossibleConnections.append( NC );
364 }
365 }
366 return PossibleConnections;
367}
368
369
370/*
371 Called by the system to see if interface can be brought UP
372
373 if allowed, echo Interface-allowed else Interface-disallowed
374*/
375
376void NetworkSettingsData::canStart( const char * Interface ) {
377 // load situation
378 NodeCollection * NC = 0;
379 QList<NodeCollection> PossibleConnections;
380
381 PossibleConnections = collectPossible( Interface );
382
383 switch( PossibleConnections.count() ) {
384 case 0 : // no connections
385 break;
386 case 1 : // one connection
387 NC = PossibleConnections.first();
388 break;
389 default : // need to ask user ?
390 // are we connected to a server
391 // system( "su %d networksettings2 --prompt %s\n",
392 // "", Interface );
393 break;
394 }
395
396 if( NC ) {
397 switch( NC->state() ) {
398 case Unchecked :
399 case Unknown :
400 case Unavailable :
401 case Disabled :
402 // this profile does not allow interface to be UP
403 // -> try others
404 break;
405 case Off :
406 // try to UP the device
407 if( ! NC->setState( Activate ) ) {
408 // cannot bring device Online -> try other alters
409 break;
410 }
411 // FT
412 case Available :
413 case IsUp : // also called for 'ifdown'
414 // device is ready -> done
415 printf( "%s-c%d-allowed\n", Interface, NC->number() );
416 return;
417 }
418 } else {
419 // if we come here no alternatives are possible
420 printf( "%s-cnn-disallowed\n", Interface );
421 }
422}
423
424/*
425 Called by the system to regenerate config files
426*/
427
428bool NetworkSettingsData::regenerate( void ) {
429 QString S;
430 // load situation
431 S = generateSettings( TRUE );
432 if( ! S.isEmpty() ) {
433 fprintf( stdout, "%s\n", S.latin1() );
434 return 1;
435 }
436 return 0;
437}
438
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 @@
1#ifndef __NSDATA_H
2#define __NSDATA_H
3
4#include "netnode.h"
5
6class NetworkSettingsData {
7
8public :
9
10 NetworkSettingsData( void );
11 ~NetworkSettingsData( void );
12
13 void loadSettings( void );
14 QString saveSettings( void );
15
16 QString generateSettings( bool Force = FALSE );
17
18 bool isModified( void )
19 { return IsModified; }
20 void setModified( bool m )
21 { IsModified = m; }
22
23 QList<NodeCollection> collectPossible( const char * Interface );
24 void canStart( const char * Interface );
25 bool regenerate( void );
26
27 void forceGeneration( bool m )
28 { Force = m; }
29
30private :
31
32 bool IsModified;
33 bool Force;
34
35};
36
37#endif