summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/netnode.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/netnode.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings2/netnode.cpp222
1 files changed, 198 insertions, 24 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/netnode.cpp b/noncore/settings/networksettings2/networksettings2/netnode.cpp
index 110786a..17653bd 100644
--- a/noncore/settings/networksettings2/networksettings2/netnode.cpp
+++ b/noncore/settings/networksettings2/networksettings2/netnode.cpp
@@ -1,23 +1,36 @@
1#include <time.h> 1#include <time.h>
2#include <qpe/qpeapplication.h> 2#include <qpe/qpeapplication.h>
3#include <qpe/resource.h> 3#include <qpe/resource.h>
4 4
5
6#include <qpainter.h> 5#include <qpainter.h>
7#include <qbitmap.h> 6#include <qbitmap.h>
8#include <qtextstream.h> 7#include <qtextstream.h>
9#include <qpixmap.h> 8#include <qpixmap.h>
10 9
11#include "resources.h" 10#include "resources.h"
12#include "netnode.h" 11#include "netnode.h"
13 12
14#include "asdevice.h" 13static char * ActionName[] = {
15#include "asline.h" 14 "Disable",
16#include "asconnection.h" 15 "Enable",
17#include "asfullsetup.h" 16 "Activate",
17 "Deactivate",
18 "Up",
19 "Down"
20};
21
22static char * StateName[] = {
23 "Unchecked",
24 "Unknown",
25 "Unavailable",
26 "Disabled",
27 "Off",
28 "Available",
29 "IsUp"
30};
18 31
19QString & deQuote( QString & X ) { 32QString & deQuote( QString & X ) {
20 if( X[0] == '"' ) { 33 if( X[0] == '"' ) {
21 // remove end and trailing "" and \x -> x 34 // remove end and trailing "" and \x -> x
22 QChar R; 35 QChar R;
23 long idx; 36 long idx;
@@ -62,12 +75,27 @@ void ANetNode::saveAttributes( QTextStream & TS ) {
62} 75}
63 76
64void ANetNode::setAttribute( QString & Attr, QString & Value ){ 77void ANetNode::setAttribute( QString & Attr, QString & Value ){
65 setSpecificAttribute( Attr, Value ); 78 setSpecificAttribute( Attr, Value );
66} 79}
67 80
81bool ANetNode::isToplevel( void ) {
82 const char ** P = provides();
83 while( *P ) {
84 if( strcmp( *P, "fullsetup") == 0 )
85 return 1;
86 P ++;
87 }
88 return 0;
89}
90
91bool ANetNode::openFile( SystemFile & SF,
92 ANetNodeInstance * NNI ) {
93 return (NNI ) ? NNI->openFile( SF ) : 0 ;
94}
95
68// 96//
69// 97//
70// ANETNODEINSTANCE 98// ANETNODEINSTANCE
71// 99//
72// 100//
73 101
@@ -81,21 +109,21 @@ void ANetNodeInstance::initialize( void ) {
81 N.sprintf( "-%ld", InstanceCounter++ ); 109 N.sprintf( "-%ld", InstanceCounter++ );
82 N.prepend( NodeType->name() ); 110 N.prepend( NodeType->name() );
83 setName( N.latin1() ); 111 setName( N.latin1() );
84} 112}
85 113
86void ANetNodeInstance::setAttribute( QString & Attr, QString & Value ){ 114void ANetNodeInstance::setAttribute( QString & Attr, QString & Value ){
87 if( Attr == "name" ) { 115 if( Attr == "__name" ) {
88 setName( Value.latin1() ); 116 setName( Value.latin1() );
89 } else { 117 } else {
90 setSpecificAttribute( Attr, Value ); 118 setSpecificAttribute( Attr, Value );
91 } 119 }
92} 120}
93 121
94void ANetNodeInstance::saveAttributes( QTextStream & TS ) { 122void ANetNodeInstance::saveAttributes( QTextStream & TS ) {
95 TS << "name=" << name() << endl; 123 TS << "__name=" << name() << endl;
96 saveSpecificAttribute( TS ); 124 saveSpecificAttribute( TS );
97} 125}
98 126
99ANetNodeInstance * ANetNodeInstance::nextNode( void ) { 127ANetNodeInstance * ANetNodeInstance::nextNode( void ) {
100 return connection()->findNext( this ); 128 return connection()->findNext( this );
101} 129}
@@ -109,23 +137,25 @@ ANetNodeInstance * ANetNodeInstance::nextNode( void ) {
109NodeCollection::NodeCollection( void ) : QList<ANetNodeInstance>() { 137NodeCollection::NodeCollection( void ) : QList<ANetNodeInstance>() {
110 IsModified = 0; 138 IsModified = 0;
111 Index = -1; 139 Index = -1;
112 Name=""; 140 Name="";
113 IsNew = 1; 141 IsNew = 1;
114 CurrentState = Unchecked; 142 CurrentState = Unchecked;
143 AssignedInterface = 0;
115} 144}
116 145
117NodeCollection::NodeCollection( QTextStream & TS ) : 146NodeCollection::NodeCollection( QTextStream & TS ) :
118 QList<ANetNodeInstance>() { 147 QList<ANetNodeInstance>() {
119 long idx; 148 long idx;
120 bool InError = 0; 149 bool InError = 0;
121 QString S, A, N; 150 QString S, A, N;
122 IsModified = 0; 151 IsModified = 0;
123 Index = -1; 152 Index = -1;
124 Name=""; 153 Name="";
125 IsNew = 0; 154 IsNew = 0;
155 AssignedInterface = 0;
126 CurrentState = Unchecked; 156 CurrentState = Unchecked;
127 157
128 do { 158 do {
129 S = TS.readLine(); 159 S = TS.readLine();
130 if( S.isEmpty() ) { 160 if( S.isEmpty() ) {
131 if( InError ) { 161 if( InError ) {
@@ -144,33 +174,35 @@ NodeCollection::NodeCollection( QTextStream & TS ) :
144 N.stripWhiteSpace(); 174 N.stripWhiteSpace();
145 N = deQuote( N ); 175 N = deQuote( N );
146 176
147 if( A == "name" ) { 177 if( A == "name" ) {
148 Name = N; 178 Name = N;
149 } else if( A == "number" ) { 179 } else if( A == "number" ) {
150 Log(( "Profile number %s\n", N.latin1() ));
151 setNumber( N.toLong() ); 180 setNumber( N.toLong() );
152 } else if( A == "node" ) { 181 } else if( A == "node" ) {
153 ANetNodeInstance * NNI = NSResources->findNodeInstance( N ); 182 ANetNodeInstance * NNI = NSResources->findNodeInstance( N );
154 if( NNI && ! InError ) { 183 if( NNI && ! InError ) {
155 append( NSResources->findNodeInstance( N ) ); 184 append( NSResources->findNodeInstance( N ) );
156 } else { 185 } else {
157 // could not find a node type -> collection invalid 186 // could not find a node type -> collection invalid
158 InError = 1; 187 InError = 1;
159 } 188 }
160 } 189 }
161 } while( 1 ); 190 } while( 1 );
191
192 Log(( "Profile number %s : %d nodes\n",
193 N.latin1(), count() ));
162} 194}
163 195
164 196
165NodeCollection::~NodeCollection( void ) { 197NodeCollection::~NodeCollection( void ) {
166} 198}
167 199
168const QString & NodeCollection::description( void ) { 200const QString & NodeCollection::description( void ) {
169 ANetNodeInstance * NNI = getToplevel(); 201 ANetNodeInstance * NNI = getToplevel();
170 return (NNI) ? NNI->runtime()->asFullSetup()->description() : Name; 202 return (NNI) ? NNI->runtime()->description() : Name;
171} 203}
172 204
173void NodeCollection::append( ANetNodeInstance * NNI ) { 205void NodeCollection::append( ANetNodeInstance * NNI ) {
174 NNI->setConnection( this ); 206 NNI->setConnection( this );
175 QList<ANetNodeInstance>::append( NNI ); 207 QList<ANetNodeInstance>::append( NNI );
176} 208}
@@ -251,13 +283,14 @@ static char * State2PixmapTbl[] = {
251}; 283};
252 284
253QPixmap NodeCollection::devicePixmap( void ) { 285QPixmap NodeCollection::devicePixmap( void ) {
254 QPixmap pm = NSResources->getPixmap( 286 QPixmap pm = NSResources->getPixmap(
255 getToplevel()->nextNode()->pixmapName()+"-large"); 287 getToplevel()->nextNode()->pixmapName()+"-large");
256 288
257 QPixmap Mini = NSResources->getPixmap( device()->netNode()->pixmapName() ); 289 QPixmap Mini = NSResources->getPixmap(
290 device()->netNode()->pixmapName() );
258 291
259 if( pm.isNull() || Mini.isNull() ) 292 if( pm.isNull() || Mini.isNull() )
260 return Resource::loadPixmap("Unknown"); 293 return Resource::loadPixmap("Unknown");
261 294
262 QPainter painter( &pm ); 295 QPainter painter( &pm );
263 painter.drawPixmap( pm.width()-Mini.width(), 296 painter.drawPixmap( pm.width()-Mini.width(),
@@ -277,17 +310,17 @@ QString NodeCollection::stateName( State_t S) {
277 return qApp->translate( "networksettings2", "Unknown"); 310 return qApp->translate( "networksettings2", "Unknown");
278 case Unavailable : 311 case Unavailable :
279 return qApp->translate( "networksettings2", "Unavailable"); 312 return qApp->translate( "networksettings2", "Unavailable");
280 case Disabled : 313 case Disabled :
281 return qApp->translate( "networksettings2", "Disabled"); 314 return qApp->translate( "networksettings2", "Disabled");
282 case Off : 315 case Off :
283 return qApp->translate( "networksettings2", "Off"); 316 return qApp->translate( "networksettings2", "Inactive");
284 case Available : 317 case Available :
285 return qApp->translate( "networksettings2", "Available"); 318 return qApp->translate( "networksettings2", "Available");
286 case IsUp : 319 case IsUp :
287 return qApp->translate( "networksettings2", "IsUp"); 320 return qApp->translate( "networksettings2", "Up");
288 case Unchecked : /* FT */ 321 case Unchecked : /* FT */
289 default : 322 default :
290 break; 323 break;
291 } 324 }
292 return QString(""); 325 return QString("");
293} 326}
@@ -297,41 +330,182 @@ void NodeCollection::reassign( void ) {
297 it.current(); 330 it.current();
298 ++it ) { 331 ++it ) {
299 it.current()->setConnection( this ); 332 it.current()->setConnection( this );
300 } 333 }
301} 334}
302 335
303bool NodeCollection::triggersVPN() { 336const QStringList & NodeCollection::triggers() {
304 return getToplevel()->runtime()->asFullSetup()->triggersVPN(); 337 return getToplevel()->runtime()->triggers();
305} 338}
306 339
307bool NodeCollection::hasDataForFile( const QString & S ) { 340bool NodeCollection::hasDataForFile( SystemFile & S ) {
308 return ( firstWithDataForFile( S ) != 0 ); 341 return ( firstWithDataForFile( S ) != 0 );
309} 342}
310 343
311ANetNodeInstance * NodeCollection::firstWithDataForFile( const QString & S ) { 344ANetNodeInstance * NodeCollection::firstWithDataForFile( SystemFile & S ) {
312 for( QListIterator<ANetNodeInstance> it(*this); 345 for( QListIterator<ANetNodeInstance> it(*this);
313 it.current(); 346 it.current();
314 ++it ) { 347 ++it ) {
315 if( it.current()->hasDataForFile( S ) ) { 348 if( it.current()->hasDataForFile( S ) ) {
316 Log(( "Node %s has data for %s\n",
317 it.current()->nodeClass()->name(),
318 S.latin1() ));
319 return it.current(); 349 return it.current();
320 } 350 }
321 } 351 }
322 return 0; 352 return 0;
323} 353}
324 354
355State_t NodeCollection::state( bool Update ) {
356 State_t NodeState;
357
358 if( CurrentState == Unchecked || Update ) {
359 // collect states of all nodes until with get the 'higest'
360 // state possible
361
362 Log(( "Connection %s state %s\n",
363 Name.latin1(), StateName[CurrentState] ));
364
365 CurrentState = Unknown;
366 for( QListIterator<ANetNodeInstance> it(*this);
367 it.current();
368 ++it ) {
369 Log(( "-> Detect %s\n", it.current()->name() ));
370 NodeState = it.current()->runtime()->detectState();
371 Log(( " state %s\n", StateName[NodeState] ));
372
373 if( NodeState == Disabled ||
374 NodeState == IsUp ) {
375 // max
376 CurrentState = NodeState;
377 break;
378 }
379
380 if( NodeState > CurrentState ) {
381 // higher
382 CurrentState = NodeState;
383 }
384 }
385 }
386
387 return CurrentState;
388}
389
390QString NodeCollection::setState( Action_t A, bool Force ) {
391
392 QString msg;
393 Action_t Actions[10];
394 int NoOfActions = 0;
395
396 // get current state
397 state( Force );
398
399 switch( A ) {
400 case Disable :
401 if( CurrentState < Disabled ) {
402 // disabled
403 CurrentState = Disabled;
404 return QString();
405 }
406
407 if( CurrentState == IsUp ) {
408 Actions[NoOfActions++] = Down;
409 Actions[NoOfActions++] = Deactivate;
410 } else if( CurrentState == Available ) {
411 Actions[NoOfActions++] = Deactivate;
412 }
413 Actions[NoOfActions++] = Disable;
414 break;
415 case Enable :
416 // always possible -> detected state is new state
417 Actions[NoOfActions++] = Enable;
418 break;
419 case Activate :
420 if( ! Force ) {
421 if( CurrentState >= Available ) {
422 // already available
423 return QString();
424 }
425
426 if( CurrentState != Off ) {
427 return qApp->translate( "System",
428 "State should be off" );
429 }
430 }
431
432 Actions[NoOfActions++] = Activate;
433 break;
434 case Deactivate :
435 if( ! Force ) {
436 if( CurrentState < Off ) {
437 // already inactive
438 return QString();
439 }
440 }
441
442 if( CurrentState == IsUp ) {
443 Actions[NoOfActions++] = Down;
444 }
445 Actions[NoOfActions++] = Deactivate;
446 break;
447 case Up :
448 if( ! Force ) {
449 if( CurrentState == IsUp ) {
450 return QString();
451 }
452 if( CurrentState < Off ) {
453 return qApp->translate( "System",
454 "State should at least be off" );
455 }
456 }
457 if( CurrentState == Off ) {
458 Actions[NoOfActions++] = Activate;
459 }
460 Actions[NoOfActions++] = Up;
461 break;
462 case Down :
463 if( ! Force ) {
464 if( CurrentState < Available ) {
465 // OK
466 return QString();
467 }
468 }
469 Actions[NoOfActions++] = Down;
470 break;
471 }
472
473 // send actions to all nodes
474 Log(( "Action %s requires %d steps\n",
475 ActionName[A], NoOfActions ));
476
477 for( int i = 0 ; i < NoOfActions; i ++ ) {
478 // setState recurses through the tree depth first
479 msg = getToplevel()->runtime()->setState( this, Actions[i], Force );
480 if( ! msg.isEmpty() ) {
481 return msg;
482 }
483 }
484 return QString();
485}
486
325// 487//
326// 488//
327// RUNTIMEINFO 489// RuntimeInfo
328// 490//
329// 491//
330 492
331InterfaceInfo * RuntimeInfo::assignedInterface( void ) { 493QString RuntimeInfo::setState( NodeCollection * NC,
332 return netNode()->nextNode()->runtime()->assignedInterface(); 494 Action_t A,
495 bool Force ) {
496 QString M;
497 RuntimeInfo * Deeper = nextNode();
498
499 if( Deeper ) {
500 // first go deeper
501 M = Deeper->setState( NC, A, Force );
502 if( ! M.isEmpty() )
503 return M;
333} 504}
334 505
335AsDevice * RuntimeInfo::device( void ) { 506 // set my own state
336 return netNode()->nextNode()->runtime()->device(); 507 Log (( "-> Act upon %s\n", netNode()->name() ));
508 M = setMyState( NC, A, Force );
509 Log (( " result %s\n", M.latin1() ));
510 return M;
337} 511}