summaryrefslogtreecommitdiff
path: root/library/config.cpp
Unidiff
Diffstat (limited to 'library/config.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/config.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/library/config.cpp b/library/config.cpp
index e07eecb..1121cd4 100644
--- a/library/config.cpp
+++ b/library/config.cpp
@@ -281,274 +281,278 @@ void Config::removeEntry( const QString &key )
281{ 281{
282 if ( git == groups.end() ) { 282 if ( git == groups.end() ) {
283 qWarning( "no group set" ); 283 qWarning( "no group set" );
284 return; 284 return;
285 } 285 }
286 ( *git ).remove( key ); 286 ( *git ).remove( key );
287 changed = TRUE; 287 changed = TRUE;
288} 288}
289 289
290/*! 290/*!
291 \fn bool Config::operator == ( const Config & other ) const 291 \fn bool Config::operator == ( const Config & other ) const
292 292
293 Tests for equality with \a other. Config objects are equal if they refer to the same filename. 293 Tests for equality with \a other. Config objects are equal if they refer to the same filename.
294*/ 294*/
295 295
296/*! 296/*!
297 \fn bool Config::operator != ( const Config & other ) const 297 \fn bool Config::operator != ( const Config & other ) const
298 298
299 Tests for inequality with \a other. Config objects are equal if they refer to the same filename. 299 Tests for inequality with \a other. Config objects are equal if they refer to the same filename.
300*/ 300*/
301 301
302/*! 302/*!
303 \fn QString Config::readEntry( const QString &key, const QString &deflt ) const 303 \fn QString Config::readEntry( const QString &key, const QString &deflt ) const
304 304
305 Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry. 305 Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry.
306*/ 306*/
307 307
308/*! 308/*!
309 \internal 309 \internal
310 For compatibility, non-const version. 310 For compatibility, non-const version.
311*/ 311*/
312QString Config::readEntry( const QString &key, const QString &deflt ) 312QString Config::readEntry( const QString &key, const QString &deflt )
313{ 313{
314 QString res = readEntryDirect( key+"["+lang+"]" ); 314 QString res = readEntryDirect( key+"["+lang+"]" );
315 if ( !res.isNull() ) 315 if ( !res.isNull() )
316 return res; 316 return res;
317 if ( !glang.isEmpty() ) { 317 if ( !glang.isEmpty() ) {
318 res = readEntryDirect( key+"["+glang+"]" ); 318 res = readEntryDirect( key+"["+glang+"]" );
319 if ( !res.isNull() ) 319 if ( !res.isNull() )
320 return res; 320 return res;
321 } 321 }
322 return readEntryDirect( key, deflt ); 322 return readEntryDirect( key, deflt );
323} 323}
324 324
325/*! 325/*!
326 \fn QString Config::readEntryCrypt( const QString &key, const QString &deflt ) const 326 \fn QString Config::readEntryCrypt( const QString &key, const QString &deflt ) const
327 327
328 Reads an encrypted string entry stored with \a key, defaulting to \a deflt if there is no entry. 328 Reads an encrypted string entry stored with \a key, defaulting to \a deflt if there is no entry.
329*/ 329*/
330 330
331/*! 331/*!
332 \internal 332 \internal
333 For compatibility, non-const version. 333 For compatibility, non-const version.
334*/ 334*/
335QString Config::readEntryCrypt( const QString &key, const QString &deflt ) 335QString Config::readEntryCrypt( const QString &key, const QString &deflt )
336{ 336{
337 QString res = readEntryDirect( key+"["+lang+"]" ); 337 QString res = readEntryDirect( key+"["+lang+"]" );
338 if ( res.isNull() && glang.isEmpty() ) 338 if ( res.isNull() && glang.isEmpty() )
339 res = readEntryDirect( key+"["+glang+"]" ); 339 res = readEntryDirect( key+"["+glang+"]" );
340 if ( res.isNull() ) 340 if ( res.isNull() )
341 res = readEntryDirect( key, QString::null ); 341 res = readEntryDirect( key, QString::null );
342 if ( res.isNull() ) 342 if ( res.isNull() )
343 return deflt; 343 return deflt;
344 return decipher(res); 344 return decipher(res);
345} 345}
346 346
347/*! 347/*!
348 \fn QString Config::readEntryDirect( const QString &key, const QString &deflt ) const 348 \fn QString Config::readEntryDirect( const QString &key, const QString &deflt ) const
349 \internal 349 \internal
350*/ 350*/
351 351
352/*! 352/*!
353 \internal 353 \internal
354 For compatibility, non-const version. 354 For compatibility, non-const version.
355*/ 355*/
356QString Config::readEntryDirect( const QString &key, const QString &deflt ) 356QString Config::readEntryDirect( const QString &key, const QString &deflt )
357{ 357{
358 if ( git == groups.end() ) { 358 if ( git == groups.end() ) {
359 //qWarning( "no group set" ); 359 //qWarning( "no group set" );
360 return deflt; 360 return deflt;
361 } 361 }
362 ConfigGroup::ConstIterator it = ( *git ).find( key ); 362 ConfigGroup::ConstIterator it = ( *git ).find( key );
363 if ( it != ( *git ).end() ) 363 if ( it != ( *git ).end() )
364 return *it; 364 return *it;
365 else 365 else
366 return deflt; 366 return deflt;
367} 367}
368 368
369/*! 369/*!
370 \fn int Config::readNumEntry( const QString &key, int deflt ) const 370 \fn int Config::readNumEntry( const QString &key, int deflt ) const
371 Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry. 371 Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry.
372*/ 372*/
373 373
374/*! 374/*!
375 \internal 375 \internal
376 For compatibility, non-const version. 376 For compatibility, non-const version.
377*/ 377*/
378int Config::readNumEntry( const QString &key, int deflt ) 378int Config::readNumEntry( const QString &key, int deflt )
379{ 379{
380 QString s = readEntry( key ); 380 QString s = readEntry( key );
381 if ( s.isEmpty() ) 381 if ( s.isEmpty() )
382 return deflt; 382 return deflt;
383 else 383 else
384 return s.toInt(); 384 return s.toInt();
385} 385}
386 386
387/*! 387/*!
388 \fn bool Config::readBoolEntry( const QString &key, bool deflt ) const 388 \fn bool Config::readBoolEntry( const QString &key, bool deflt ) const
389 Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry. 389 Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry.
390*/ 390*/
391 391
392/*! 392/*!
393 \internal 393 \internal
394 For compatibility, non-const version. 394 For compatibility, non-const version.
395*/ 395*/
396bool Config::readBoolEntry( const QString &key, bool deflt ) 396bool Config::readBoolEntry( const QString &key, bool deflt )
397{ 397{
398 QString s = readEntry( key ); 398 QString s = readEntry( key );
399 if ( s.isEmpty() ) 399 if ( s.isEmpty() )
400 return deflt; 400 return deflt;
401 else 401 else
402 return (bool)s.toInt(); 402 return (bool)s.toInt();
403} 403}
404 404
405/*! 405/*!
406 \fn QStringList Config::readListEntry( const QString &key, const QChar &sep ) const 406 \fn QStringList Config::readListEntry( const QString &key, const QChar &sep ) const
407 Reads a string list entry stored with \a key, and with \a sep as the separator. 407 Reads a string list entry stored with \a key, and with \a sep as the separator.
408*/ 408*/
409 409
410/*! 410/*!
411 \internal 411 \internal
412 For compatibility, non-const version. 412 For compatibility, non-const version.
413*/ 413*/
414QStringList Config::readListEntry( const QString &key, const QChar &sep ) 414QStringList Config::readListEntry( const QString &key, const QChar &sep )
415{ 415{
416 QString s = readEntry( key ); 416 QString s = readEntry( key );
417 if ( s.isEmpty() ) 417 if ( s.isEmpty() )
418 return QStringList(); 418 return QStringList();
419 else 419 else
420 return QStringList::split( sep, s ); 420 return QStringList::split( sep, s );
421} 421}
422 422
423/*! 423/*!
424 Removes all entries from the current group. 424 Removes all entries from the current group.
425*/ 425*/
426void Config::clearGroup() 426void Config::clearGroup()
427{ 427{
428 if ( git == groups.end() ) { 428 if ( git == groups.end() ) {
429 qWarning( "no group set" ); 429 qWarning( "no group set" );
430 return; 430 return;
431 } 431 }
432 if ( !(*git).isEmpty() ) { 432 if ( !(*git).isEmpty() ) {
433 ( *git ).clear(); 433 ( *git ).clear();
434 changed = TRUE; 434 changed = TRUE;
435 } 435 }
436} 436}
437 437
438/*! 438/*!
439 \internal 439 \internal
440*/ 440*/
441void Config::write( const QString &fn ) 441void Config::write( const QString &fn )
442{ 442{
443 QString strNewFile; 443 QString strNewFile;
444 if ( !fn.isEmpty() ) 444 if ( !fn.isEmpty() )
445 filename = fn; 445 filename = fn;
446 strNewFile = filename + ".new"; 446 strNewFile = filename + ".new";
447 447
448 QFile f( strNewFile ); 448 QFile f( strNewFile );
449 if ( !f.open( IO_WriteOnly|IO_Raw ) ) { 449 if ( !f.open( IO_WriteOnly|IO_Raw ) ) {
450 qWarning( "could not open for writing `%s'", strNewFile.latin1() ); 450 qWarning( "could not open for writing `%s'", strNewFile.latin1() );
451 git = groups.end(); 451 git = groups.end();
452 return; 452 return;
453 } 453 }
454 454
455 QString str; 455 QString str;
456 QCString cstr; 456 QCString cstr;
457 QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); 457 QMap< QString, ConfigGroup >::Iterator g_it = groups.begin();
458 458
459 for ( ; g_it != groups.end(); ++g_it ) { 459 for ( ; g_it != groups.end(); ++g_it ) {
460 str += "[" + g_it.key() + "]\n"; 460 str += "[" + g_it.key() + "]\n";
461 ConfigGroup::Iterator e_it = ( *g_it ).begin(); 461 ConfigGroup::Iterator e_it = ( *g_it ).begin();
462 for ( ; e_it != ( *g_it ).end(); ++e_it ) 462 for ( ; e_it != ( *g_it ).end(); ++e_it )
463 str += e_it.key() + " = " + *e_it + "\n"; 463 str += e_it.key() + " = " + *e_it + "\n";
464 } 464 }
465 cstr = str.utf8(); 465 cstr = str.utf8();
466 466
467 int total_length; 467 int total_length;
468 total_length = f.writeBlock( cstr.data(), cstr.length() ); 468 total_length = f.writeBlock( cstr.data(), cstr.length() );
469 if ( total_length != int(cstr.length()) ) { 469 if ( total_length != int(cstr.length()) ) {
470 QMessageBox::critical( 0, QObject::tr("Out of Space"), 470 QMessageBox::critical( 0, QObject::tr("Out of Space"),
471 QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); 471 QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") );
472 f.close(); 472 f.close();
473 QFile::remove( strNewFile ); 473 QFile::remove( strNewFile );
474 return; 474 return;
475 } 475 }
476 476
477 f.close(); 477 f.close();
478 // now rename the file... 478 // now rename the file...
479 if ( rename( strNewFile, filename ) < 0 ) { 479 if ( rename( strNewFile, filename ) < 0 ) {
480 qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), 480 qWarning( "problem renaming the file %s to %s", strNewFile.latin1(),
481 filename.latin1() ); 481 filename.latin1() );
482 QFile::remove( strNewFile ); 482 QFile::remove( strNewFile );
483 } 483 }
484} 484}
485 485
486/*! 486/*!
487 Returns whether the Config is in a valid state. 487 Returns whether the Config is in a valid state.
488*/ 488*/
489bool Config::isValid() const 489bool Config::isValid() const
490{ 490{
491 return groups.end() != git; 491 return groups.end() != git;
492} 492}
493 493
494/*! 494/*!
495 \internal 495 \internal
496*/ 496*/
497void Config::read() 497void Config::read()
498{ 498{
499 changed = FALSE; 499 changed = FALSE;
500 500
501 if ( !QFileInfo( filename ).exists() ) { 501 if ( !QFileInfo( filename ).exists() ) {
502 git = groups.end(); 502 git = groups.end();
503 return; 503 return;
504 } 504 }
505 505
506 QFile f( filename ); 506 QFile f( filename );
507 if ( !f.open( IO_ReadOnly ) ) { 507 if ( !f.open( IO_ReadOnly ) ) {
508 git = groups.end(); 508 git = groups.end();
509 return; 509 return;
510 } 510 }
511 511
512 QTextStream s( &f ); 512 QTextStream s( &f );
513#if QT_VERSION <= 230 && defined(QT_NO_CODECS) 513#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
514 // The below should work, but doesn't in Qt 2.3.0 514 // The below should work, but doesn't in Qt 2.3.0
515 s.setCodec( QTextCodec::codecForMib( 106 ) ); 515 s.setCodec( QTextCodec::codecForMib( 106 ) );
516#else 516#else
517 s.setEncoding( QTextStream::UnicodeUTF8 ); 517 s.setEncoding( QTextStream::UnicodeUTF8 );
518#endif 518#endif
519 519
520 QStringList list = QStringList::split('\n', s.read() ); 520 QStringList list = QStringList::split('\n', s.read() );
521 f.close(); 521 f.close();
522 522
523 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { 523 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
524 if ( !parse( *it ) ) { 524 if ( !parse( *it ) ) {
525 git = groups.end(); 525 git = groups.end();
526 return; 526 return;
527 } 527 }
528 } 528 }
529} 529}
530 530
531/*! 531/*!
532 \internal 532 \internal
533*/ 533*/
534bool Config::parse( const QString &l ) 534bool Config::parse( const QString &l )
535{ 535{
536 QString line = l.stripWhiteSpace(); 536 QString line = l.stripWhiteSpace();
537
538 if ( line [0] == QChar ( '#' ))
539 return true; // ignore comments
540
537 if ( line[ 0 ] == QChar( '[' ) ) { 541 if ( line[ 0 ] == QChar( '[' ) ) {
538 QString gname = line; 542 QString gname = line;
539 gname = gname.remove( 0, 1 ); 543 gname = gname.remove( 0, 1 );
540 if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) 544 if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) )
541 gname = gname.remove( gname.length() - 1, 1 ); 545 gname = gname.remove( gname.length() - 1, 1 );
542 git = groups.insert( gname, ConfigGroup() ); 546 git = groups.insert( gname, ConfigGroup() );
543 } else if ( !line.isEmpty() ) { 547 } else if ( !line.isEmpty() ) {
544 if ( git == groups.end() ) 548 if ( git == groups.end() )
545 return FALSE; 549 return FALSE;
546 int eq = line.find( '=' ); 550 int eq = line.find( '=' );
547 if ( eq == -1 ) 551 if ( eq == -1 )
548 return FALSE; 552 return FALSE;
549 QString key = line.left(eq).stripWhiteSpace(); 553 QString key = line.left(eq).stripWhiteSpace();
550 QString value = line.mid(eq+1).stripWhiteSpace(); 554 QString value = line.mid(eq+1).stripWhiteSpace();
551 ( *git ).insert( key, value ); 555 ( *git ).insert( key, value );
552 } 556 }
553 return TRUE; 557 return TRUE;
554} 558}