summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/resources.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/resources.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.cpp256
1 files changed, 165 insertions, 91 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/resources.cpp b/noncore/settings/networksettings2/networksettings2/resources.cpp
index c95ac7f..71e84cd 100644
--- a/noncore/settings/networksettings2/networksettings2/resources.cpp
+++ b/noncore/settings/networksettings2/networksettings2/resources.cpp
@@ -14,24 +14,26 @@
14 14
15#define PLUGINDIR "plugins/networksettings2" 15#define PLUGINDIR "plugins/networksettings2"
16#define ICONDIR "/pics/networksettings2/" 16#define ICONDIR "/pics/networksettings2/"
17 17
18// single resources instance 18// single resources instance
19TheNSResources * _NSResources = 0; 19TheNSResources * _NSResources = 0;
20 20
21TheNSResources::TheNSResources( void ) : NodeTypeNameMap(), 21TheNSResources::TheNSResources( void ) : NodeTypeNameMap(),
22 ConnectionsMap() { 22 ConnectionsMap() {
23 23
24 _NSResources = this; 24 _NSResources = this;
25 25
26 detectCurrentUser();
27
26 // load available netnodes 28 // load available netnodes
27 findAvailableNetNodes(QPEApplication::qpeDir() + PLUGINDIR ); 29 findAvailableNetNodes(QPEApplication::qpeDir() + PLUGINDIR );
28 30
29 // compile provides and needs lists 31 // compile provides and needs lists
30 { const char ** NeedsRun; 32 { const char ** NeedsRun;
31 QDictIterator<NetNode_t> OuterIt( AllNodeTypes ); 33 QDictIterator<NetNode_t> OuterIt( AllNodeTypes );
32 bool Done; 34 bool Done;
33 35
34 for ( ; OuterIt.current(); ++OuterIt ) { 36 for ( ; OuterIt.current(); ++OuterIt ) {
35 // find needs list 37 // find needs list
36 ANetNode::NetNodeList * NNLP = new ANetNode::NetNodeList; 38 ANetNode::NetNodeList * NNLP = new ANetNode::NetNodeList;
37 ANetNode::NetNodeList & NNL = *(NNLP); 39 ANetNode::NetNodeList & NNL = *(NNLP);
@@ -71,38 +73,49 @@ TheNSResources::TheNSResources( void ) : NodeTypeNameMap(),
71 tr( "<p>Devices that can handle single bytes</p>" ) ); 73 tr( "<p>Devices that can handle single bytes</p>" ) );
72 NodeTypeDescriptionMap.insert( "connection", 74 NodeTypeDescriptionMap.insert( "connection",
73 tr( "<p>Nodes that provide working IP connections</p>" ) ); 75 tr( "<p>Nodes that provide working IP connections</p>" ) );
74 NodeTypeDescriptionMap.insert( "fullsetup", 76 NodeTypeDescriptionMap.insert( "fullsetup",
75 tr( "<p>Fully configured connection profile</p>" ) ); 77 tr( "<p>Fully configured connection profile</p>" ) );
76 78
77 // define system files 79 // define system files
78 addSystemFile( new SystemFile( "interfaces", "./interfaces" ) ); 80 addSystemFile( new SystemFile( "interfaces", "./interfaces" ) );
79 81
80 // get access to the system 82 // get access to the system
81 TheSystem = new System(); 83 TheSystem = new System();
82 84
83 detectCurrentUser();
84} 85}
85 86
86TheNSResources::~TheNSResources( void ) { 87TheNSResources::~TheNSResources( void ) {
87 delete TheSystem; 88 delete TheSystem;
88} 89}
89 90
91void TheNSResources::busy( bool B ) {
92/*
93 if( B ) {
94 ShowWait->show();
95 qApp->process
96 } else {
97 ShowWait->hide();
98 }
99*/
100}
101
90/** 102/**
91 * Load all modules that are found in the path 103 * Load all modules that are found in the path
92 * @param path a directory that is scaned for any plugins that can be loaded 104 * @param path a directory that is scaned for any plugins that can be loaded
93 * and attempts to load them 105 * and attempts to load them
94 */ 106 */
95void TheNSResources::findAvailableNetNodes(const QString &path){ 107void TheNSResources::findAvailableNetNodes(const QString &path){
96 108
109 Log(("Locate plugins in %s\n", path.latin1() ));
97 QDir d(path); 110 QDir d(path);
98 if(!d.exists()) 111 if(!d.exists())
99 return; 112 return;
100 113
101 QString lang = ::getenv("LANG"); 114 QString lang = ::getenv("LANG");
102 115
103 // Don't want sym links 116 // Don't want sym links
104 d.setFilter( QDir::Files | QDir::NoSymLinks ); 117 d.setFilter( QDir::Files | QDir::NoSymLinks );
105 const QFileInfoList *list = d.entryInfoList(); 118 const QFileInfoList *list = d.entryInfoList();
106 QFileInfoListIterator it( *list ); 119 QFileInfoListIterator it( *list );
107 QFileInfo *fi; 120 QFileInfo *fi;
108 121
@@ -118,24 +131,45 @@ void TheNSResources::findAvailableNetNodes(const QString &path){
118 ".qm"; 131 ".qm";
119 132
120 if( trans->load( fn ) ) 133 if( trans->load( fn ) )
121 qApp->installTranslator( trans ); 134 qApp->installTranslator( trans );
122 else 135 else
123 delete trans; 136 delete trans;
124 } 137 }
125 } 138 }
126 ++it; 139 ++it;
127 } 140 }
128} 141}
129 142
143// used to find unique connection number
144int TheNSResources::assignConnectionNumber( void ) {
145 bool found = 1;
146 for( int trial = 0; ; trial ++ ) {
147 found = 1;
148 for( QDictIterator<NodeCollection> it(ConnectionsMap);
149 it.current();
150 ++it ) {
151 if( it.current()->number() == trial ) {
152 found = 0;
153 break;
154 }
155 }
156
157 if( found ) {
158 Log(("Assign profile number %d\n", trial ));
159 return trial;
160 }
161 }
162}
163
130/** 164/**
131 * Attempt to load a function and resolve a function. 165 * Attempt to load a function and resolve a function.
132 * @param pluginFileName - the name of the file in which to attempt to load 166 * @param pluginFileName - the name of the file in which to attempt to load
133 * @param resolveString - function pointer to resolve 167 * @param resolveString - function pointer to resolve
134 * @return true of loading is successful 168 * @return true of loading is successful
135 */ 169 */
136bool TheNSResources::loadNetNode( 170bool TheNSResources::loadNetNode(
137 const QString &pluginFileName, const QString &resolveString){ 171 const QString &pluginFileName, const QString &resolveString){
138 172
139 QLibrary *lib = new QLibrary(pluginFileName); 173 QLibrary *lib = new QLibrary(pluginFileName);
140 void * res = lib->resolve(resolveString); 174 void * res = lib->resolve(resolveString);
141 if( ! res ){ 175 if( ! res ){
@@ -158,36 +192,37 @@ bool TheNSResources::loadNetNode(
158 for( QListIterator<ANetNode> it(PNN); 192 for( QListIterator<ANetNode> it(PNN);
159 it.current(); 193 it.current();
160 ++it ) { 194 ++it ) {
161 NetNode_t * NN; 195 NetNode_t * NN;
162 196
163 NNP = it.current(); 197 NNP = it.current();
164 NN = new NetNode_t; 198 NN = new NetNode_t;
165 NN->NetNode = NNP; 199 NN->NetNode = NNP;
166 NN->TheLibrary = lib; 200 NN->TheLibrary = lib;
167 NN->NodeCountInLib = PNN.count(); 201 NN->NodeCountInLib = PNN.count();
168 202
169 // store mapping 203 // store mapping
170 printf( "Store %s\n", NN->NetNode->name() );
171 AllNodeTypes.insert( NN->NetNode->name(), NN ); 204 AllNodeTypes.insert( NN->NetNode->name(), NN );
172 } 205 }
173 206
174 return 1; 207 return 1;
175} 208}
176 209
177QPixmap TheNSResources::getPixmap( const QString & QS ) { 210QPixmap TheNSResources::getPixmap( const QString & QS ) {
211 QPixmap P;
178 QString S("networksettings2/"); 212 QString S("networksettings2/");
179 S += QS; 213 S += QS;
180 fprintf( stderr, "%s\n", S.latin1() ); 214 Log(("%s\n", S.latin1() ));
181 return Resource::loadPixmap( S ); 215 P = Resource::loadPixmap( S );
216 return ( P.isNull() ) ? QPixmap() : P;
182} 217}
183 218
184QString TheNSResources::tr( const char * s ) { 219QString TheNSResources::tr( const char * s ) {
185 return qApp->translate( "resource", s ); 220 return qApp->translate( "resource", s );
186} 221}
187 222
188const QString & TheNSResources::netNode2Name( const char * s ) { 223const QString & TheNSResources::netNode2Name( const char * s ) {
189 return NodeTypeNameMap[s]; 224 return NodeTypeNameMap[s];
190} 225}
191 226
192const QString & TheNSResources::netNode2Description( const char * s ) { 227const QString & TheNSResources::netNode2Description( const char * s ) {
193 return NodeTypeDescriptionMap[s]; 228 return NodeTypeDescriptionMap[s];
@@ -216,155 +251,194 @@ void TheNSResources::removeConnection( const QString & N ) {
216 // delete netnodes in this connection 251 // delete netnodes in this connection
217 ANetNodeInstance * NNI; 252 ANetNodeInstance * NNI;
218 for( NNI = NC->first(); NNI != 0; NNI = NC->next() ) { 253 for( NNI = NC->first(); NNI != 0; NNI = NC->next() ) {
219 removeNodeInstance( NNI->name() ); 254 removeNodeInstance( NNI->name() );
220 } 255 }
221 ConnectionsMap.remove( N ); 256 ConnectionsMap.remove( N );
222} 257}
223 258
224NodeCollection * TheNSResources::findConnection( const QString & S ) { 259NodeCollection * TheNSResources::findConnection( const QString & S ) {
225 return ConnectionsMap[ S ]; 260 return ConnectionsMap[ S ];
226} 261}
227 262
263/*
228void TheNSResources::renumberConnections( void ) { 264void TheNSResources::renumberConnections( void ) {
229 Name2Connection_t & M = NSResources->connections(); 265 Name2Connection_t & M = NSResources->connections();
230 NodeCollection * NC; 266 NodeCollection * NC;
231 267
232 // for all connections 268 // for all connections
233 NodeCollection::resetMaxNr(); 269 NodeCollection::resetMaxNr();
234 for( QDictIterator<NodeCollection> it(M); 270 for( QDictIterator<NodeCollection> it(M);
235 it.current(); 271 it.current();
236 ++it ) { 272 ++it ) {
237 NC = it.current(); 273 NC = it.current();
238 NC->setNumber( NC->maxConnectionNumber()+1 ); 274 NC->setNumber( NC->maxConnectionNumber()+1 );
239 NC->setModified( 1 ); 275 NC->setModified( 1 );
240 } 276 }
241} 277}
278*/
242 279
243typedef struct EnvVars { 280typedef struct EnvVars {
244 char * Name; 281 char * Name;
245 int Len; 282 int Len;
246} EnvVar_t; 283} EnvVar_t;
247 284
248#define AnEV(x) x, sizeof(x)-1 285#define AnEV(x) x, sizeof(x)-1
249 286
250static EnvVar_t EV[] = { 287static EnvVar_t EV[] = {
251 // AnEV( "HOME=" ), -> SPECIAL 288 // AnEV( "HOME=" ), -> SPECIAL
252 // AnEV( "LOGNAME=" ), -> SPECIAL 289 // AnEV( "LOGNAME=" ), -> SPECIAL
253 AnEV( "USER=" ), 290 AnEV( "USER=" ),
254 AnEV( "LD_LIBRARY_PATH=" ), 291 AnEV( "LD_LIBRARY_PATH=" ),
255 AnEV( "PATH=" ), 292 AnEV( "PATH=" ),
256 AnEV( "QTDIR=" ), 293 AnEV( "QTDIR=" ),
257 AnEV( "OPIEDIR=" ), 294 AnEV( "OPIEDIR=" ),
258 AnEV( "SHELL=" ), 295 AnEV( "SHELL=" ),
259 { NULL, 0 } 296 { NULL, 0 }
260}; 297};
261 298
262void TheNSResources::detectCurrentUser( void ) { 299void TheNSResources::detectCurrentUser( void ) {
263 // find current running qpe 300 // find current running qpe
264 QString QPEEnvFile = ""; 301 QString QPEEnvFile = "";
265 302
266 // open proc dir and find all dirs in it 303 if( getenv( "OPIEDIR" ) == 0 ) {
267 { QRegExp R("[0-9]+"); 304 // nothing known
268 QDir ProcDir( "/proc" ); 305 { // open proc dir and find all dirs in it
269 QString QPELoc = QPEApplication::qpeDir() + "bin/qpe"; 306 QRegExp R("[0-9]+");
270 QFileInfo FI; 307 QDir ProcDir( "/proc" );
271 QStringList EL = ProcDir.entryList( QDir::Dirs ); 308 QFileInfo FI;
272 309 QStringList EL = ProcDir.entryList( QDir::Dirs );
273 // print it out 310
274 for ( QStringList::Iterator it = EL.begin(); 311 // print it out
275 it != EL.end(); 312 for ( QStringList::Iterator it = EL.begin();
276 ++it ) { 313 it != EL.end();
277 if( R.match( (*it) ) >= 0 ) { 314 ++it ) {
278 QString S = ProcDir.path()+"/"+ (*it); 315 if( R.match( (*it) ) >= 0 ) {
279 S.append( "/exe" ); 316 QString S = ProcDir.path()+"/"+ (*it);
280 FI.setFile( S ); 317 S.append( "/exe" );
281 // get the linke 318 FI.setFile( S );
282 S = FI.readLink(); 319 // get the link
283 if( S == QPELoc ) { 320 S = FI.readLink();
284 // found running qpe 321 if( S.right( 8 ) == "/bin/qpe" ) {
285 QPEEnvFile.sprintf( ProcDir.path()+ "/" + (*it) + "/environ" ); 322 // found running qpe
286 break; 323 QPEEnvFile.sprintf( ProcDir.path()+ "/" + (*it) + "/environ" );
324 break;
325 }
287 } 326 }
288 } 327 }
289 } 328 }
290 }
291 329
292 if( QPEEnvFile.isEmpty() ) { 330 if( QPEEnvFile.isEmpty() ) {
293 // could not find qpe 331 // could not find qpe
294 fprintf( stderr, "Could not find qpe\n" ); 332 Log(("Could not find qpe\n" ));
295 return;
296 }
297
298 // FI now contains path ProcDir to the cmd dir
299 { char * Buf = 0;
300 char TB[1024];
301 long BufSize = 0;
302 int fd;
303 int rd;
304
305 fd = open( QPEEnvFile.latin1(), O_RDONLY );
306 if( fd < 0 ) {
307 fprintf( stderr, "Could not open %s : %d\n",
308 QPEEnvFile.latin1(), errno );
309 return; 333 return;
310 } 334 }
311 335
312 while( (rd = read( fd, TB, sizeof(TB) ) ) > 0 ) { 336 // FI now contains path ProcDir to the cmd dir
313 Buf = (char *)realloc( Buf, BufSize+rd ); 337 { char * Buf = 0;
314 memcpy( Buf+BufSize, TB, rd ); 338 char TB[1024];
315 BufSize += rd; 339 long BufSize = 0;
316 } 340 int fd;
341 int rd;
342
343 fd = open( QPEEnvFile.latin1(), O_RDONLY );
344 if( fd < 0 ) {
345 Log(("Could not open %s : %d\n",
346 QPEEnvFile.latin1(), errno ));
347 return;
348 }
317 349
318 char * Data = Buf; 350 while( (rd = read( fd, TB, sizeof(TB) ) ) > 0 ) {
319 char * DataEnd = Data+BufSize-1; 351 Buf = (char *)realloc( Buf, BufSize+rd );
320 352 memcpy( Buf+BufSize, TB, rd );
321 // get env items out of list 353 BufSize += rd;
322 while( Data < DataEnd ) { 354 }
323 if( strncmp( Data, "LOGNAME=", 8 ) == 0 ) { 355
324 CurrentUser.UserName = Data+8; 356 char * Data = Buf;
325 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); 357 char * DataEnd = Data+BufSize-1;
326 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = 358
327 strdup( Data ); 359 // get env items out of list
328 } else if( strncmp( Data, "HOME=", 5 ) == 0 ) { 360 while( Data < DataEnd ) {
329 CurrentUser.HomeDir = Data+5; 361 if( strncmp( Data, "LOGNAME=", 8 ) == 0 ) {
330 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); 362 CurrentUser.UserName = Data+8;
331 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = 363 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
332 strdup( Data ); 364 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
333 } else { 365 strdup( Data );
334 EnvVar_t * Run = EV; 366 } else if( strncmp( Data, "HOME=", 5 ) == 0 ) {
335 while( Run->Name ) { 367 CurrentUser.HomeDir = Data+5;
336 if( strncmp( Data, Run->Name, Run->Len ) == 0 ) { 368 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
337 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); 369 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
338 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = 370 strdup( Data );
339 strdup( Data ); 371 } else {
340 break; 372 EnvVar_t * Run = EV;
373 while( Run->Name ) {
374 if( strncmp( Data, Run->Name, Run->Len ) == 0 ) {
375 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
376 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
377 strdup( Data );
378 // put OPIEDIR in env
379 if( strcmp( Run->Name, "OPIEDIR=" ) == 0 ) {
380 putenv( CurrentUser.EnvList[CurrentUser.EnvList.size()-1] );
381
382 }
383 break;
384 }
385 Run ++;
341 } 386 }
342 Run ++;
343 } 387 }
344 }
345 388
346 Data += strlen( Data )+1; 389 Data += strlen( Data )+1;
347 } 390 }
348 391
349 free( Buf ); 392 free( Buf );
350 393
351 if( ! CurrentUser.UserName.isEmpty() ) { 394 if( ! CurrentUser.UserName.isEmpty() ) {
352 // find user info 395 // find user info
353 struct passwd pwd; 396 struct passwd pwd;
354 struct passwd * pwdres; 397 struct passwd * pwdres;
355 398
356 if( getpwnam_r( CurrentUser.UserName.latin1(), 399 if( getpwnam_r( CurrentUser.UserName.latin1(),
357 &pwd, TB, sizeof(TB), &pwdres ) || 400 &pwd, TB, sizeof(TB), &pwdres ) ||
358 pwdres == 0 ) { 401 pwdres == 0 ) {
359 fprintf( stderr, "Could not determine user %s : %d\n", 402 Log(("Could not determine user %s : %d\n",
360 CurrentUser.UserName.latin1(), errno ); 403 CurrentUser.UserName.latin1(), errno ));
361 return; 404 return;
405 }
406 CurrentUser.Uid = pwd.pw_uid;
407 CurrentUser.Gid = pwd.pw_gid;
408 } else{
409 CurrentUser.Uid =
410 CurrentUser.Gid = -1;
362 } 411 }
363 CurrentUser.Uid = pwd.pw_uid;
364 CurrentUser.Gid = pwd.pw_gid;
365 } else{
366 CurrentUser.Uid =
367 CurrentUser.Gid = -1;
368 } 412 }
413
414 } else {
415 CurrentUser.UserName = getenv( "LOGNAME" );
416 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
417 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
418 strdup( CurrentUser.UserName );
419
420 CurrentUser.HomeDir = getenv( "HOME" );
421 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
422 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] =
423 strdup( CurrentUser.HomeDir );
424
425 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
426 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("USER");
427 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
428 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("LD_LIBRARY_PATH");
429
430 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
431 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("PATH");
432
433 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
434 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("QTDIR");
435
436 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
437 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("OPIEDIR");
438 CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 );
439 CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = getenv("SHELL");
440
441 CurrentUser.Uid = getuid();
442 CurrentUser.Gid = getgid();
369 } 443 }
370} 444}