1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
|
#include <stdio.h>
#include <qpe/qpeapplication.h>
#include <qiconset.h>
#include <qgroupbox.h>
#include <qtimer.h>
#include <qlistbox.h>
#include <qmessagebox.h>
#include <qlabel.h>
#include <qiconview.h>
#include <qtextstream.h>
#include <qdir.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qtimer.h>
#include <qpe/qpeapplication.h>
#include <qtoolbutton.h>
#include <asdevice.h>
#include "networksettings.h"
#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 ...
<EMPTYLINE>
[connection]
Name=Name
Node=Name
<EMPTYLINE>
*/
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",
"<p>Connection %1 contains unrecognized nodes and cannot be loaded</p>" ).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",
"<p>Could not save setup to %1 !</p>" ).
arg(CfgFile);
// problem
return ErrS;
}
QTextStream TS( &F );
{ Name2Connection_t & M = NSResources->connections();
ANetNodeInstance * NNI;
// for all connections
for( QDictIterator<NodeCollection> it(M);
it.current();
++it ) {
// all nodes in those connections
for( QListIterator<ANetNodeInstance> nit(*(it.current()));
nit.current();
++nit ) {
// header
NNI = nit.current();
TS << '[' <<NNI->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<SystemFile> 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<NodeCollection> ncit(M);
ncit.current();
++ncit ) {
NC = ncit.current();
if( NC->isModified() ) {
// does this connection 'touch' this system file ?
for( QListIterator<ANetNodeInstance> 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<ANetNodeInstance> 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",
"<p>Cannot generate files proper to %1</p>" ).
arg(NNI->netNode()->nodeName()) ;
return S;
}
}
}
}
//
// generate system files
//
for( QDictIterator<SystemFile> 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",
"<p>Error in preSection for file %1</p>" ).
arg( SF->name() );
return S;
}
for( QDictIterator<NodeCollection> 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<ANetNodeInstance> 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",
"<p>Error in preNodeSection for file %1 and node %2</p>" ).
arg( SF->name() ).
arg( NNI->netNode()->nodeName() );
return S;
}
if( NNI->netNode()->generateDataForCommonFile(*SF,i,NNI) ) {
S = qApp->translate( "NetworkSettings",
"<p>Error in node part for file %1 and node %2</p>" ).
arg( SF->name() ).
arg( NNI->netNode()->nodeName() );
return S;
}
if( SF->postNodeSection( NNI, i ) ) {
S = qApp->translate( "NetworkSettings",
"<p>Error in postNodeSection for file %1 and node %2</p>" ).
arg( SF->name() ).
arg( NNI->netNode()->nodeName() );
return S;
}
}
}
}
*SF << endl;
}
if( SF->postSection() ) {
S = qApp->translate( "NetworkSettings",
"<p>Error in postSection for file %1</p>" ).
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),
NSD() {
UpdateTimer = new QTimer( this );
// set pixmaps
Add_TB->setPixmap( NSResources->getPixmap( "add" ) );
Delete_TB->setPixmap( NSResources->getPixmap( "remove" ) );
CheckState_TB->setPixmap( NSResources->getPixmap( "check" ) );
Enable_TB->setPixmap( NSResources->getPixmap( "disabled" ) );
GenConfig_TB->setPixmap( NSResources->getPixmap( "configure" ) );
Connect_TB->setPixmap( NSResources->getPixmap( "connected" ) );
On_TB->setPixmap( NSResources->getPixmap( "off" ) );
// populate main Listbox
Profiles_IV->clear();
{ Name2Connection_t & M = NSResources->connections();
NodeCollection * NC;
QIconViewItem * IVI;
// for all connections
for( QDictIterator<NodeCollection> it(M);
it.current();
++it ) {
NC = it.current();
IVI = new QIconViewItem( Profiles_IV,
NC->name(),
NC->devicePixmap() );
}
}
if( Profiles_IV->count() ) {
Profiles_IV->setSelected( Profiles_IV->firstItem(), TRUE );
}
// if no profiles -> auto popup editing
if( NSResources->connections().count() == 0 ) {
QTimer::singleShot( 100, this, SLOT(SLOT_AddNode() ) );
}
UpdateTimer->start( 5000 );
connect( UpdateTimer, SIGNAL( timeout() ),
this, SLOT( SLOT_RefreshStates() ) );
}
NetworkSettings::~NetworkSettings() {
QString S;
S = NSD.generateSettings();
if( ! S.isEmpty() ) {
QMessageBox::warning(
0,
tr( "Generating system configuration" ),
S
);
}
S = NSD.saveSettings();
if( ! S.isEmpty() ) {
// problem saving
QMessageBox::warning(
0,
tr( "Saving setup" ), S );
}
}
void NetworkSettings::SLOT_RefreshStates( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem(); // remeber
if( IVI ) {
NodeCollection * NC;
NSResources->system().probeInterfaces();
// update current selection only
NC = NSResources->findConnection( IVI->text() );
if( NC ) {
State_t OldS = NC->state();
State_t NewS = NC->state(1);
if( OldS != NewS ) {
updateProfileState( IVI );
}
}
}
/* -> LATER !!
bool is;
NodeCollection * NC;
for( unsigned int i = 0; i < Profiles_LB->count() ; i ++ ) {
NC = NSResources->findConnection( Profiles_LB->text(i) );
if( NC ) {
State_t OldS = NC->state();
State_t NewS = NC->state(1);
if( OldS != NewS ) {
is = Profiles_LB->isSelected(i);
Profiles_LB->changeItem( NC->statePixmap(NewS),
NC->name(),
i );
if( is ) {
Profiles_LB->setSelected( i, TRUE );
}
}
}
}
if( ci >= 0 )
Profiles_LB->setCurrentItem( ci );
*/
}
void NetworkSettings::SLOT_AddNode( void ) {
SLOT_EditNode( 0 );
}
void NetworkSettings::SLOT_DeleteNode( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem();
if ( ! IVI )
return;
if( QMessageBox::warning(
0,
tr( "Removing profile" ),
tr( "Remove selected profile ?" ),
1, 0 ) == 1 ) {
NSResources->removeConnection( IVI->text() );
delete IVI;
setModified( 1 );
NSD.forceGeneration(1);
}
}
void NetworkSettings::SLOT_EditNode( QIconViewItem * IVI ) {
QString OldName = "";
EditConnection EC( this );
if( IVI ) {
NodeCollection * NC = NSResources->findConnection( IVI->text() );
if( ! NC ) {
return;
}
OldName = NC->name();
EC.setConnection( NC );
}
EC.showMaximized();
// disable refresh timer
UpdateTimer->stop();
if( EC.exec() == QDialog::Accepted ) {
// toplevel item -> store
NodeCollection * NC = EC.connection();
if( NC->isModified() ) {
setModified( 1 );
if( IVI ) {
// new name -> remove item
NSResources->removeConnection( OldName );
// must add it here since change will trigger event
NSResources->addConnection( NC );
IVI->setText( NC->name() );
IVI->setPixmap( NC->devicePixmap() );
} else {
// new item
NSResources->addConnection( NC );
NC->setNumber( NC->maxConnectionNumber()+1 );
IVI = new QIconViewItem( Profiles_IV,
NC->name(),
NC->devicePixmap()
);
Profiles_IV->setSelected( IVI, TRUE );
}
updateProfileState( IVI );
}
} else {
// cancelled : reset connection
if( IVI ) {
NodeCollection * NC = NSResources->findConnection( IVI->text() );
NC->reassign();
}
}
// reenable
UpdateTimer->start( 5000 );
}
void NetworkSettings::SLOT_ShowNode( QIconViewItem * IVI ) {
if( IVI == 0 )
return;
NodeCollection * NC = NSResources->findConnection( IVI->text() );
// is button possible
bool EnabledPossible, OnPossible, ConnectPossible;
// is button On or Off
bool DisabledOn, OnOn, ConnectOn;
EnabledPossible = OnPossible = ConnectPossible = 1;
DisabledOn = 1;
OnOn = ConnectOn = 0;
switch( NC->state() ) {
case Unknown :
// cannot occur here
break;
case Unchecked :
case Unavailable :
// cannot do anything but recheck
EnabledPossible = OnPossible = ConnectPossible = 0;
break;
case Disabled :
OnPossible = ConnectPossible = 0;
break;
case Off :
DisabledOn = 0;
break;
case Available :
OnOn = 1;
DisabledOn = 0;
break;
case IsUp :
OnOn = ConnectOn = 1;
DisabledOn = 0;
break;
}
// set button state
Enable_TB->setEnabled( EnabledPossible );
On_TB->setEnabled( OnPossible );
Connect_TB->setEnabled( ConnectPossible );
Enable_TB->setOn( DisabledOn );
On_TB->setOn( OnOn );
Connect_TB->setOn( ConnectOn );
if( NC->description().isEmpty() ) {
Description_LBL->setText( tr( "No description" ) );
} else {
Description_LBL->setText( NC->description() );
}
CurProfile_GB->setTitle( IVI->text() );
State_LBL->setText( NC->stateName() );
}
void NetworkSettings::SLOT_CheckState( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem();
if ( ! IVI )
return;
updateProfileState( IVI );
}
void NetworkSettings::updateProfileState( QIconViewItem * IVI ) {
if( IVI == Profiles_IV->currentItem() ) {
SLOT_ShowNode( IVI );
}
}
void NetworkSettings::SLOT_GenerateConfig( void ) {
QString S = NSD.generateSettings( TRUE );
if( ! S.isEmpty() ) {
QMessageBox::warning(
0,
tr( "Generating system configuration" ),
S
);
}
}
void NetworkSettings::SLOT_Enable( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem();
QString Msg;
if ( ! IVI )
return;
NodeCollection * NC =
NSResources->findConnection( IVI->text() );
bool rv;
switch( NC->state() ) {
case Disabled :
Msg = tr( "Cannot enable profile" );
rv = NC->setState( Enable );
break;
default :
Msg = tr( "Cannot disable profile" );
rv = NC->setState( Disable );
break;
}
if( ! rv ) {
QMessageBox::warning(
0,
tr( "Activating profile" ),
Msg );
return;
}
updateProfileState( IVI );
}
void NetworkSettings::SLOT_On( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem();
if ( ! IVI )
return;
NodeCollection * NC =
NSResources->findConnection( IVI->text() );
bool rv;
switch( NC->state() ) {
case Off :
// activate interface
rv = NC->setState( Activate );
break;
case Available :
// deactivate
rv = NC->setState( Deactivate );
break;
case IsUp :
// bring down and deactivate
rv = ( NC->setState( Down ) &&
NC->setState( Deactivate ) );
break;
default :
// others no change
return;
}
if( ! rv ) {
QMessageBox::warning(
0,
tr( "Activating profile" ),
tr( "Cannot enable profile" ) );
return;
}
updateProfileState( IVI );
}
void NetworkSettings::SLOT_Connect( void ) {
QIconViewItem * IVI = Profiles_IV->currentItem();
if ( ! IVI )
return;
NodeCollection * NC =
NSResources->findConnection( IVI->text() );
bool rv;
switch( NC->state() ) {
case IsUp :
// down interface
rv = NC->setState( Down );
break;
case Available :
// up interface
rv = NC->setState( Up );
break;
case Off :
// activate and bring up
rv = ( NC->setState( Activate ) &&
NC->setState( Up ) );
break;
default :
// others no change
return;
}
if( ! rv ) {
QMessageBox::warning(
0,
tr( "Activating profile" ),
tr( "Cannot enable profile" ) );
return;
}
// 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<NodeCollection> 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;
S = NSD.generateSettings( TRUE );
if( ! S.isEmpty() ) {
fprintf( stdout, "%s\n", S.latin1() );
return 1;
}
return 0;
}
|