author | wimpie <wimpie> | 2004-04-03 21:33:30 (UTC) |
---|---|---|
committer | wimpie <wimpie> | 2004-04-03 21:33:30 (UTC) |
commit | d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843 (patch) (unidiff) | |
tree | 32ccf2da6464c498c4c43c750e1391a06a1ad431 /noncore/settings/networksettings2/nsdata.cpp | |
parent | 41ba1a87a850785caf94d0b50675281fb6561168 (diff) | |
download | opie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.zip opie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.tar.gz opie-d82581fff0a7fa02b5c391a5b0bcfbfd1e5a6843.tar.bz2 |
Factored out non-gui data class
Added support for prompting
Diffstat (limited to 'noncore/settings/networksettings2/nsdata.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/nsdata.cpp | 438 |
1 files changed, 438 insertions, 0 deletions
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 | |||
12 | static QString CfgFile; | ||
13 | |||
14 | NetworkSettingsData::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 | ||
26 | NetworkSettingsData::~NetworkSettingsData( void ) { | ||
27 | delete NSResources; | ||
28 | } | ||
29 | |||
30 | void 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 | |||
119 | QString 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 | |||
172 | QString 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 | |||
347 | QList<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 | |||
376 | void 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 | |||
428 | bool 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 | |||