summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings.cpp820
1 files changed, 820 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/networksettings.cpp b/noncore/settings/networksettings2/networksettings.cpp
new file mode 100644
index 0000000..ffe130c
--- a/dev/null
+++ b/noncore/settings/networksettings2/networksettings.cpp
@@ -0,0 +1,820 @@
1#include <stdio.h>
2
3#include <qpe/qpeapplication.h>
4#include <qiconset.h>
5#include <qgroupbox.h>
6#include <qtimer.h>
7#include <qlistbox.h>
8#include <qmessagebox.h>
9#include <qlabel.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>
16#include <qpe/qpeapplication.h>
17#include <qtoolbutton.h>
18
19#include <asdevice.h>
20#include "networksettings.h"
21#include "netnode.h"
22#include "editconnection.h"
23
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,
378 const char *name,
379 WFlags fl ) : NetworkSettingsGUI(parent,name,fl),
380 NSD() {
381
382 UpdateTimer = new QTimer( this );
383 // set pixmaps
384 Add_TB->setPixmap( NSResources->getPixmap( "add" ) );
385 Delete_TB->setPixmap( NSResources->getPixmap( "remove" ) );
386 CheckState_TB->setPixmap( NSResources->getPixmap( "check" ) );
387 Enable_TB->setPixmap( NSResources->getPixmap( "disabled" ) );
388 GenConfig_TB->setPixmap( NSResources->getPixmap( "configure" ) );
389
390 Connect_TB->setPixmap( NSResources->getPixmap( "connected" ) );
391
392 On_TB->setPixmap( NSResources->getPixmap( "off" ) );
393
394 // populate main Listbox
395 Profiles_IV->clear();
396 { Name2Connection_t & M = NSResources->connections();
397 NodeCollection * NC;
398 QIconViewItem * IVI;
399
400 // for all connections
401 for( QDictIterator<NodeCollection> it(M);
402 it.current();
403 ++it ) {
404 NC = it.current();
405 IVI = new QIconViewItem( Profiles_IV,
406 NC->name(),
407 NC->devicePixmap() );
408 }
409 }
410
411 if( Profiles_IV->count() ) {
412 Profiles_IV->setSelected( Profiles_IV->firstItem(), TRUE );
413 }
414
415 // if no profiles -> auto popup editing
416 if( NSResources->connections().count() == 0 ) {
417 QTimer::singleShot( 100, this, SLOT(SLOT_AddNode() ) );
418 }
419
420 UpdateTimer->start( 5000 );
421 connect( UpdateTimer, SIGNAL( timeout() ),
422 this, SLOT( SLOT_RefreshStates() ) );
423}
424
425NetworkSettings::~NetworkSettings() {
426 QString S;
427
428 S = NSD.generateSettings();
429 if( ! S.isEmpty() ) {
430 QMessageBox::warning(
431 0,
432 tr( "Generating system configuration" ),
433 S
434 );
435 }
436
437 S = NSD.saveSettings();
438 if( ! S.isEmpty() ) {
439 // problem saving
440 QMessageBox::warning(
441 0,
442 tr( "Saving setup" ), S );
443 }
444}
445
446void NetworkSettings::SLOT_RefreshStates( void ) {
447 QIconViewItem * IVI = Profiles_IV->currentItem(); // remeber
448
449 if( IVI ) {
450 NodeCollection * NC;
451 NSResources->system().probeInterfaces();
452 // update current selection only
453 NC = NSResources->findConnection( IVI->text() );
454 if( NC ) {
455 State_t OldS = NC->state();
456 State_t NewS = NC->state(1);
457 if( OldS != NewS ) {
458 updateProfileState( IVI );
459 }
460 }
461 }
462
463
464 /* -> LATER !!
465 bool is;
466 NodeCollection * NC;
467
468 for( unsigned int i = 0; i < Profiles_LB->count() ; i ++ ) {
469 NC = NSResources->findConnection( Profiles_LB->text(i) );
470 if( NC ) {
471 State_t OldS = NC->state();
472 State_t NewS = NC->state(1);
473 if( OldS != NewS ) {
474 is = Profiles_LB->isSelected(i);
475 Profiles_LB->changeItem( NC->statePixmap(NewS),
476 NC->name(),
477 i );
478 if( is ) {
479 Profiles_LB->setSelected( i, TRUE );
480 }
481 }
482 }
483 }
484 if( ci >= 0 )
485 Profiles_LB->setCurrentItem( ci );
486 */
487}
488
489void NetworkSettings::SLOT_AddNode( void ) {
490 SLOT_EditNode( 0 );
491}
492
493void NetworkSettings::SLOT_DeleteNode( void ) {
494 QIconViewItem * IVI = Profiles_IV->currentItem();
495
496 if ( ! IVI )
497 return;
498
499 if( QMessageBox::warning(
500 0,
501 tr( "Removing profile" ),
502 tr( "Remove selected profile ?" ),
503 1, 0 ) == 1 ) {
504 NSResources->removeConnection( IVI->text() );
505 delete IVI;
506 setModified( 1 );
507 NSD.forceGeneration(1);
508 }
509}
510
511void NetworkSettings::SLOT_EditNode( QIconViewItem * IVI ) {
512 QString OldName = "";
513 EditConnection EC( this );
514
515 if( IVI ) {
516 NodeCollection * NC = NSResources->findConnection( IVI->text() );
517 if( ! NC ) {
518 return;
519 }
520 OldName = NC->name();
521 EC.setConnection( NC );
522 }
523
524 EC.showMaximized();
525 // disable refresh timer
526 UpdateTimer->stop();
527 if( EC.exec() == QDialog::Accepted ) {
528 // toplevel item -> store
529 NodeCollection * NC = EC.connection();
530 if( NC->isModified() ) {
531 setModified( 1 );
532 if( IVI ) {
533 // new name -> remove item
534 NSResources->removeConnection( OldName );
535 // must add it here since change will trigger event
536 NSResources->addConnection( NC );
537 IVI->setText( NC->name() );
538 IVI->setPixmap( NC->devicePixmap() );
539 } else {
540 // new item
541 NSResources->addConnection( NC );
542 NC->setNumber( NC->maxConnectionNumber()+1 );
543 IVI = new QIconViewItem( Profiles_IV,
544 NC->name(),
545 NC->devicePixmap()
546 );
547 Profiles_IV->setSelected( IVI, TRUE );
548 }
549 updateProfileState( IVI );
550 }
551 } else {
552 // cancelled : reset connection
553 if( IVI ) {
554 NodeCollection * NC = NSResources->findConnection( IVI->text() );
555 NC->reassign();
556 }
557 }
558 // reenable
559 UpdateTimer->start( 5000 );
560}
561
562void NetworkSettings::SLOT_ShowNode( QIconViewItem * IVI ) {
563 if( IVI == 0 )
564 return;
565
566 NodeCollection * NC = NSResources->findConnection( IVI->text() );
567
568 // is button possible
569 bool EnabledPossible, OnPossible, ConnectPossible;
570 // is button On or Off
571 bool DisabledOn, OnOn, ConnectOn;
572
573 EnabledPossible = OnPossible = ConnectPossible = 1;
574 DisabledOn = 1;
575 OnOn = ConnectOn = 0;
576
577 switch( NC->state() ) {
578 case Unknown :
579 // cannot occur here
580 break;
581 case Unchecked :
582 case Unavailable :
583 // cannot do anything but recheck
584 EnabledPossible = OnPossible = ConnectPossible = 0;
585 break;
586 case Disabled :
587 OnPossible = ConnectPossible = 0;
588 break;
589 case Off :
590 DisabledOn = 0;
591 break;
592 case Available :
593 OnOn = 1;
594 DisabledOn = 0;
595 break;
596 case IsUp :
597 OnOn = ConnectOn = 1;
598 DisabledOn = 0;
599 break;
600 }
601
602 // set button state
603 Enable_TB->setEnabled( EnabledPossible );
604 On_TB->setEnabled( OnPossible );
605 Connect_TB->setEnabled( ConnectPossible );
606
607 Enable_TB->setOn( DisabledOn );
608 On_TB->setOn( OnOn );
609 Connect_TB->setOn( ConnectOn );
610
611 if( NC->description().isEmpty() ) {
612 Description_LBL->setText( tr( "No description" ) );
613 } else {
614 Description_LBL->setText( NC->description() );
615 }
616
617 CurProfile_GB->setTitle( IVI->text() );
618 State_LBL->setText( NC->stateName() );
619}
620
621void NetworkSettings::SLOT_CheckState( void ) {
622 QIconViewItem * IVI = Profiles_IV->currentItem();
623 if ( ! IVI )
624 return;
625 updateProfileState( IVI );
626}
627
628void NetworkSettings::updateProfileState( QIconViewItem * IVI ) {
629 if( IVI == Profiles_IV->currentItem() ) {
630 SLOT_ShowNode( IVI );
631 }
632}
633
634void NetworkSettings::SLOT_GenerateConfig( void ) {
635 QString S = NSD.generateSettings( TRUE );
636
637 if( ! S.isEmpty() ) {
638 QMessageBox::warning(
639 0,
640 tr( "Generating system configuration" ),
641 S
642 );
643 }
644}
645
646void NetworkSettings::SLOT_Enable( void ) {
647 QIconViewItem * IVI = Profiles_IV->currentItem();
648 QString Msg;
649 if ( ! IVI )
650 return;
651
652 NodeCollection * NC =
653 NSResources->findConnection( IVI->text() );
654
655 bool rv;
656 switch( NC->state() ) {
657 case Disabled :
658 Msg = tr( "Cannot enable profile" );
659 rv = NC->setState( Enable );
660 break;
661 default :
662 Msg = tr( "Cannot disable profile" );
663 rv = NC->setState( Disable );
664 break;
665 }
666
667 if( ! rv ) {
668 QMessageBox::warning(
669 0,
670 tr( "Activating profile" ),
671 Msg );
672 return;
673 }
674 updateProfileState( IVI );
675}
676
677void NetworkSettings::SLOT_On( void ) {
678 QIconViewItem * IVI = Profiles_IV->currentItem();
679
680 if ( ! IVI )
681 return;
682
683 NodeCollection * NC =
684 NSResources->findConnection( IVI->text() );
685
686 bool rv;
687 switch( NC->state() ) {
688 case Off :
689 // activate interface
690 rv = NC->setState( Activate );
691 break;
692 case Available :
693 // deactivate
694 rv = NC->setState( Deactivate );
695 break;
696 case IsUp :
697 // bring down and deactivate
698 rv = ( NC->setState( Down ) &&
699 NC->setState( Deactivate ) );
700 break;
701 default :
702 // others no change
703 return;
704 }
705
706 if( ! rv ) {
707 QMessageBox::warning(
708 0,
709 tr( "Activating profile" ),
710 tr( "Cannot enable profile" ) );
711 return;
712 }
713 updateProfileState( IVI );
714}
715
716void NetworkSettings::SLOT_Connect( void ) {
717 QIconViewItem * IVI = Profiles_IV->currentItem();
718
719 if ( ! IVI )
720 return;
721
722 NodeCollection * NC =
723 NSResources->findConnection( IVI->text() );
724
725 bool rv;
726 switch( NC->state() ) {
727 case IsUp :
728 // down interface
729 rv = NC->setState( Down );
730 break;
731 case Available :
732 // up interface
733 rv = NC->setState( Up );
734 break;
735 case Off :
736 // activate and bring up
737 rv = ( NC->setState( Activate ) &&
738 NC->setState( Up ) );
739 break;
740 default :
741 // others no change
742 return;
743 }
744
745 if( ! rv ) {
746 QMessageBox::warning(
747 0,
748 tr( "Activating profile" ),
749 tr( "Cannot enable profile" ) );
750 return;
751 }
752
753 // we do not update the GUI but wait for the REAL upping of the device
754}
755
756/*
757 Called by the system to see if interface can be brought UP
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
814 S = NSD.generateSettings( TRUE );
815 if( ! S.isEmpty() ) {
816 fprintf( stdout, "%s\n", S.latin1() );
817 return 1;
818 }
819 return 0;
820}