summaryrefslogtreecommitdiff
path: root/library/config.cpp
Unidiff
Diffstat (limited to 'library/config.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/config.cpp164
1 files changed, 163 insertions, 1 deletions
diff --git a/library/config.cpp b/library/config.cpp
index 664ca34..61ff089 100644
--- a/library/config.cpp
+++ b/library/config.cpp
@@ -1,130 +1,130 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000,2004 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include <qdir.h> 21#include <qdir.h>
22#include <qmessagebox.h> 22#include <qmessagebox.h>
23#if QT_VERSION <= 230 && defined(QT_NO_CODECS) 23#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
24#include <qtextcodec.h> 24#include <qtextcodec.h>
25#endif 25#endif
26#include <qtextstream.h> 26#include <qtextstream.h>
27 27
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <sys/types.h> 29#include <sys/types.h>
30#include <fcntl.h> 30#include <fcntl.h>
31#include <stdlib.h> 31#include <stdlib.h>
32#include <unistd.h> 32#include <unistd.h>
33 33
34#define QTOPIA_INTERNAL_LANGLIST 34#define QTOPIA_INTERNAL_LANGLIST
35#include "config.h" 35#include "config.h"
36#include "global.h" 36#include "global.h"
37 37
38 38
39/*! 39/*!
40 \internal 40 \internal
41*/ 41*/
42QString Config::configFilename(const QString& name, Domain d) 42QString Config::configFilename(const QString& name, Domain d)
43{ 43{
44 switch (d) { 44 switch (d) {
45 case File: 45 case File:
46 return name; 46 return name;
47 case User: { 47 case User: {
48 QDir dir = (QString(getenv("HOME")) + "/Settings"); 48 QDir dir = (QString(getenv("HOME")) + "/Settings");
49 if ( !dir.exists() ) 49 if ( !dir.exists() )
50 mkdir(dir.path().local8Bit(),0700); 50 mkdir(dir.path().local8Bit(),0700);
51 return dir.path() + "/" + name + ".conf"; 51 return dir.path() + "/" + name + ".conf";
52 } 52 }
53 } 53 }
54 return name; 54 return name;
55} 55}
56 56
57/*! 57/*!
58 \class Config config.h 58 \class Config config.h
59 \brief The Config class provides for saving application cofniguration state. 59 \brief The Config class provides for saving application cofniguration state.
60 60
61 You should keep a Config in existence only while you do not want others 61 You should keep a Config in existence only while you do not want others
62 to be able to change the state. There is no locking currently, but there 62 to be able to change the state. There is no locking currently, but there
63 may be in the future. 63 may be in the future.
64*/ 64*/
65 65
66/*! 66/*!
67 \enum Config::ConfigGroup 67 \enum Config::ConfigGroup
68 \internal 68 \internal
69*/ 69*/
70 70
71/*! 71/*!
72 \enum Config::Domain 72 \enum Config::Domain
73 73
74 \value File 74 \value File
75 \value User 75 \value User
76 76
77 See Config for details. 77 See Config for details.
78*/ 78*/
79 79
80/*! 80/*!
81 Constructs a config that will load or create a configuration with the 81 Constructs a config that will load or create a configuration with the
82 given \a name in the given \a domain. 82 given \a name in the given \a domain.
83 83
84 You must call setGroup() before doing much else with the Config. 84 You must call setGroup() before doing much else with the Config.
85 85
86 In the default Domain, \e User, 86 In the default Domain, \e User,
87 the configuration is user-specific. \a name should not contain "/" in 87 the configuration is user-specific. \a name should not contain "/" in
88 this case, and in general should be the name of the C++ class that is 88 this case, and in general should be the name of the C++ class that is
89 primarily responsible for maintaining the configuration. 89 primarily responsible for maintaining the configuration.
90 90
91 In the File Domain, \a name is an absolute filename. 91 In the File Domain, \a name is an absolute filename.
92*/ 92*/
93Config::Config( const QString &name, Domain domain ) 93Config::Config( const QString &name, Domain domain )
94 : filename( configFilename(name,domain) ) 94 : filename( configFilename(name,domain) )
95{ 95{
96 git = groups.end(); 96 git = groups.end();
97 read(); 97 read();
98 QStringList l = Global::languageList(); 98 QStringList l = Global::languageList();
99 lang = l[0]; 99 lang = l[0];
100 glang = l[1]; 100 glang = l[1];
101} 101}
102 102
103 103
104// Sharp ROM compatibility 104// Sharp ROM compatibility
105Config::Config ( const QString &name, bool what ) 105Config::Config ( const QString &name, bool what )
106 : filename( configFilename(name,what ? User : File) ) 106 : filename( configFilename(name,what ? User : File) )
107{ 107{
108 git = groups.end(); 108 git = groups.end();
109 read(); 109 read();
110 QStringList l = Global::languageList(); 110 QStringList l = Global::languageList();
111 lang = l[0]; 111 lang = l[0];
112 glang = l[1]; 112 glang = l[1];
113} 113}
114 114
115/*! 115/*!
116 Writes any changes to disk and destroys the in-memory object. 116 Writes any changes to disk and destroys the in-memory object.
117*/ 117*/
118Config::~Config() 118Config::~Config()
119{ 119{
120 if ( changed ) 120 if ( changed )
121 write(); 121 write();
122} 122}
123 123
124/*! 124/*!
125 Returns whether the current group has an entry called \a key. 125 Returns whether the current group has an entry called \a key.
126*/ 126*/
127bool Config::hasKey( const QString &key ) const 127bool Config::hasKey( const QString &key ) const
128{ 128{
129 if ( groups.end() == git ) 129 if ( groups.end() == git )
130 return FALSE; 130 return FALSE;
@@ -453,128 +453,290 @@ void Config::write( const QString &fn )
453 QString strNewFile; 453 QString strNewFile;
454 if ( !fn.isEmpty() ) 454 if ( !fn.isEmpty() )
455 filename = fn; 455 filename = fn;
456 strNewFile = filename + ".new"; 456 strNewFile = filename + ".new";
457 457
458 QFile f( strNewFile ); 458 QFile f( strNewFile );
459 if ( !f.open( IO_WriteOnly|IO_Raw ) ) { 459 if ( !f.open( IO_WriteOnly|IO_Raw ) ) {
460 qWarning( "could not open for writing `%s'", strNewFile.latin1() ); 460 qWarning( "could not open for writing `%s'", strNewFile.latin1() );
461 git = groups.end(); 461 git = groups.end();
462 return; 462 return;
463 } 463 }
464 464
465 QString str; 465 QString str;
466 QCString cstr; 466 QCString cstr;
467 QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); 467 QMap< QString, ConfigGroup >::Iterator g_it = groups.begin();
468 468
469 for ( ; g_it != groups.end(); ++g_it ) { 469 for ( ; g_it != groups.end(); ++g_it ) {
470 str += "[" + g_it.key() + "]\n"; 470 str += "[" + g_it.key() + "]\n";
471 ConfigGroup::Iterator e_it = ( *g_it ).begin(); 471 ConfigGroup::Iterator e_it = ( *g_it ).begin();
472 for ( ; e_it != ( *g_it ).end(); ++e_it ) 472 for ( ; e_it != ( *g_it ).end(); ++e_it )
473 str += e_it.key() + " = " + *e_it + "\n"; 473 str += e_it.key() + " = " + *e_it + "\n";
474 } 474 }
475 cstr = str.utf8(); 475 cstr = str.utf8();
476 476
477 int total_length; 477 int total_length;
478 total_length = f.writeBlock( cstr.data(), cstr.length() ); 478 total_length = f.writeBlock( cstr.data(), cstr.length() );
479 if ( total_length != int(cstr.length()) ) { 479 if ( total_length != int(cstr.length()) ) {
480 QMessageBox::critical( 0, QObject::tr("Out of Space"), 480 QMessageBox::critical( 0, QObject::tr("Out of Space"),
481 QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); 481 QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") );
482 f.close(); 482 f.close();
483 QFile::remove( strNewFile ); 483 QFile::remove( strNewFile );
484 return; 484 return;
485 } 485 }
486 486
487 f.close(); 487 f.close();
488 // now rename the file... 488 // now rename the file...
489 if ( rename( strNewFile, filename ) < 0 ) { 489 if ( rename( strNewFile, filename ) < 0 ) {
490 qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), 490 qWarning( "problem renaming the file %s to %s", strNewFile.latin1(),
491 filename.latin1() ); 491 filename.latin1() );
492 QFile::remove( strNewFile ); 492 QFile::remove( strNewFile );
493 } 493 }
494} 494}
495 495
496/*! 496/*!
497 Returns whether the Config is in a valid state. 497 Returns whether the Config is in a valid state.
498*/ 498*/
499bool Config::isValid() const 499bool Config::isValid() const
500{ 500{
501 return groups.end() != git; 501 return groups.end() != git;
502} 502}
503 503
504/*! 504/*!
505 \internal 505 \internal
506*/ 506*/
507void Config::read() 507void Config::read()
508{ 508{
509 changed = FALSE; 509 changed = FALSE;
510 510
511 if ( !QFileInfo( filename ).exists() ) { 511 if ( !QFileInfo( filename ).exists() ) {
512 git = groups.end(); 512 git = groups.end();
513 return; 513 return;
514 } 514 }
515 515
516 QFile f( filename ); 516 QFile f( filename );
517 if ( !f.open( IO_ReadOnly ) ) { 517 if ( !f.open( IO_ReadOnly ) ) {
518 git = groups.end(); 518 git = groups.end();
519 return; 519 return;
520 } 520 }
521 521
522 522
523 // hack to avoid problems if big files are passed to test 523 // hack to avoid problems if big files are passed to test
524 // if they are valid configs ( like passing a mp3 ... ) 524 // if they are valid configs ( like passing a mp3 ... )
525 // I just hope that there are no conf files > 100000 byte 525 // I just hope that there are no conf files > 100000 byte
526 // not the best solution, find something else later 526 // not the best solution, find something else later
527 if ( f.getch()!='[' ||f.size() > 100000 ) { 527 if ( f.getch()!='[' ||f.size() > 100000 ) {
528 git = groups.end(); 528 git = groups.end();
529 return; 529 return;
530 } 530 }
531 f.ungetch('['); 531 f.ungetch('[');
532 532
533 533
534 QTextStream s( &f ); 534 QTextStream s( &f );
535#if QT_VERSION <= 230 && defined(QT_NO_CODECS) 535#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
536 // 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
537 s.setCodec( QTextCodec::codecForMib( 106 ) ); 537 s.setCodec( QTextCodec::codecForMib( 106 ) );
538#else 538#else
539 s.setEncoding( QTextStream::UnicodeUTF8 ); 539 s.setEncoding( QTextStream::UnicodeUTF8 );
540#endif 540#endif
541 541
542 QStringList list = QStringList::split('\n', s.read() ); 542 QStringList list = QStringList::split('\n', s.read() );
543 f.close(); 543 f.close();
544 544
545 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { 545 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
546 if ( !parse( *it ) ) { 546 if ( !parse( *it ) ) {
547 git = groups.end(); 547 git = groups.end();
548 return; 548 return;
549 } 549 }
550 } 550 }
551} 551}
552 552
553/*! 553/*!
554 \internal 554 \internal
555*/ 555*/
556bool Config::parse( const QString &l ) 556bool Config::parse( const QString &l )
557{ 557{
558 QString line = l.stripWhiteSpace(); 558 QString line = l.stripWhiteSpace();
559 559
560 if ( line [0] == QChar ( '#' )) 560 if ( line [0] == QChar ( '#' ))
561 return true; // ignore comments 561 return true; // ignore comments
562 562
563 if ( line[ 0 ] == QChar( '[' ) ) { 563 if ( line[ 0 ] == QChar( '[' ) ) {
564 QString gname = line; 564 QString gname = line;
565 gname = gname.remove( 0, 1 ); 565 gname = gname.remove( 0, 1 );
566 if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) 566 if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) )
567 gname = gname.remove( gname.length() - 1, 1 ); 567 gname = gname.remove( gname.length() - 1, 1 );
568 git = groups.insert( gname, ConfigGroup() ); 568 git = groups.insert( gname, ConfigGroup() );
569 } else if ( !line.isEmpty() ) { 569 } else if ( !line.isEmpty() ) {
570 if ( git == groups.end() ) 570 if ( git == groups.end() )
571 return FALSE; 571 return FALSE;
572 int eq = line.find( '=' ); 572 int eq = line.find( '=' );
573 if ( eq == -1 ) 573 if ( eq == -1 )
574 return FALSE; 574 return FALSE;
575 QString key = line.left(eq).stripWhiteSpace(); 575 QString key = line.left(eq).stripWhiteSpace();
576 QString value = line.mid(eq+1).stripWhiteSpace(); 576 QString value = line.mid(eq+1).stripWhiteSpace();
577 ( *git ).insert( key, value ); 577 ( *git ).insert( key, value );
578 } 578 }
579 return TRUE; 579 return TRUE;
580} 580}
581
582
583
584bool Config::hasGroup( const QString& name )const {
585 return ( groups. find ( name ) != groups. end ( ));
586};
587
588QStringList Config::groupList()const {
589 QStringList sl;
590 for ( ConfigGroupMap::ConstIterator it = groups. begin ( ); it != groups. end ( ); ++it )
591 sl << it.key();
592
593 return sl;
594};
595
596/////////////
597// Qtopia 2.1 Functions
598//
599////////////
600
601QStringList Config::allGroups()const {
602 return groupList();
603}
604
605/*!
606 Returns the time stamp for the config identified by \a name. The
607 time stamp represents the time the config was last committed to storage.
608 Returns 0 if there is no time stamp available for the config.
609
610 A \a domain can optionally be specified and defaults to User.
611 See \l{Config()} for details.
612
613 First availability: Qtopia 2.0
614*/
615long Config::timeStamp(const QString& name, Domain domain)
616{
617#ifdef Q_WS_WIN
618 // Too slow (many conversions too and from time_t and QDataTime)
619 QDateTime epoch;
620 epoch.setTime_t(0);
621 return epoch.secsTo(QFileInfo(Config::configFilename(name,domain)).lastModified());
622#else
623 QString fn = Config::configFilename(name,domain);
624 struct stat b;
625 if (lstat( QFile::encodeName(fn).data(), &b ) == 0)
626 return b.st_mtime;
627 else
628 return 0;
629#endif
630}
631
632
633/*!
634 Removes the current group (and all its entries).
635
636 The current group becomes unset.
637
638 First availability: Qtopia 2.0
639*/
640void Config::removeGroup()
641{
642 if ( git == groups.end() ) {
643 qWarning( "no group set" );
644 return;
645 }
646
647 groups.remove(git.key());
648 git = groups.end();
649 changed = TRUE;
650}
651
652/*!
653 Removes the current group (and all its entries).
654
655 The current group becomes unset.
656
657 First availability: Qtopia 2.0
658*/
659void Config::removeGroup(const QString& g)
660{
661 groups.remove(g);
662 git = groups.end();
663}
664
665
666
667/*!
668 Writes a (\a key, \a lst) entry to the current group.
669
670 The list is
671 separated by the two characters "^e", and "^" withing the strings
672 is replaced by "^^", such that the strings may contain any character,
673 including "^".
674
675 Null strings are also allowed, and are recorded as "^0" in the string.
676
677 First availability: Qtopia 2.0
678
679 \sa readListEntry()
680*/
681void Config::writeEntry( const QString &key, const QStringList &lst )
682{
683 QString s;
684 for (QStringList::ConstIterator it=lst.begin(); it!=lst.end(); ++it) {
685 QString el = *it;
686 if ( el.isNull() ) {
687 el = "^0";
688 } else {
689 el.replace(QRegExp("\\^"), "^^");
690 }
691 s+=el;
692 s+="^e"; // end of element
693 }
694 writeEntry(key, s);
695}
696
697/*!
698 Returns the string list entry stored using \a key and with
699 the escaped seperator convention described in writeListEntry().
700
701 First availability: Qtopia 2.0
702*/
703QStringList Config::readListEntry( const QString &key ) const
704{
705 QString value = readEntry( key, QString::null );
706 QStringList l;
707 QString s;
708 bool esc=FALSE;
709 for (int i=0; i<(int)value.length(); i++) {
710 if ( esc ) {
711 if ( value[i] == 'e' ) { // end-of-string
712 l.append(s);
713 s="";
714 } else if ( value[i] == '0' ) { // null string
715 s=QString::null;
716 } else {
717 s.append(value[i]);
718 }
719 esc = FALSE;
720 } else if ( value[i] == '^' ) {
721 esc = TRUE;
722 } else {
723 s.append(value[i]);
724 if ( i == (int)value.length()-1 )
725 l.append(s);
726 }
727 }
728 return l;
729}
730
731QString Config::readEntry( const QString &key, const QString &deflt ) const
732{ return ((Config*)this)->readEntry(key,deflt); }
733QString Config::readEntryCrypt( const QString &key, const QString &deflt ) const
734{ return ((Config*)this)->readEntryCrypt(key,deflt); }
735QString Config::readEntryDirect( const QString &key, const QString &deflt ) const
736{ return ((Config*)this)->readEntryDirect(key,deflt); }
737int Config::readNumEntry( const QString &key, int deflt ) const
738{ return ((Config*)this)->readNumEntry(key,deflt); }
739bool Config::readBoolEntry( const QString &key, bool deflt ) const
740{ return ((Config*)this)->readBoolEntry(key,deflt); }
741QStringList Config::readListEntry( const QString &key, const QChar &sep ) const
742{ return ((Config*)this)->readListEntry(key,sep); }