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.cpp432
1 files changed, 13 insertions, 419 deletions
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 405
769 // for all connections 406 if( msg == "raise" ) {
770 for( QDictIterator<NodeCollection> it(M); 407 raise();
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; 408 return;
797 } 409 } /* if ( msg == "someMessage(int,int,int)" ) {
798 } 410 int a,b,c;
799 } 411 stream >> a >> b >> c;
800 } 412 ...
801 // if we come here no alternatives are possible 413 } */
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} 414}