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