1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
9 | contributed Qt classes |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
17 This is a pure Qt class - it can be used independently of YaST2.
23 #include "QY2Settings.h"
35 QY2Settings::QY2Settings( const QString & fileName, AccessMode accessMode )
36 : _fileName( fileName )
37 , _accessMode( accessMode )
39 _sections.setAutoDelete( true );
41 if ( _accessMode == ReadOnly || _accessMode == ReadWrite )
47 void QY2Settings::initSections()
49 _defaultSection = new Section( "" );
50 Q_CHECK_PTR( _defaultSection );
52 _currentSection = _defaultSection;
53 _sections.insert( "", _defaultSection );
58 QY2Settings::~QY2Settings()
60 if ( _dirty && _accessMode != ReadOnly )
65 bool QY2Settings::selectSection( const QString & sectionName )
67 _currentSection = _sections[ sectionName ];
69 if ( _currentSection )
75 _currentSection = _defaultSection;
81 void QY2Settings::selectDefaultSection()
83 _currentSection = _defaultSection;
87 QStringList QY2Settings::sections( bool includeUnnamed ) const
89 QStringList sectionList;
90 SectionIterator it( _sections );
94 QString sectionName = (*it)->name();
96 if ( includeUnnamed || ! sectionName.isEmpty() )
97 sectionList.append( sectionName );
106 QString QY2Settings::get( const QString & key, const QString & fallback ) const
108 // Can't use operator[] here since we have a pointer
110 Section::const_iterator it = _currentSection->find( key );
112 if ( it == _currentSection->constEnd() )
119 QString QY2Settings::operator[] ( const QString & key )
125 bool QY2Settings::hasKey( const QString & key )
127 return _currentSection->contains( key );
131 QStringList QY2Settings::keys() const
135 for ( Section::const_iterator it = _currentSection->constBegin();
136 it != _currentSection->constEnd();
139 keyList.append( it.key() );
146 bool QY2Settings::load()
151 if ( _accessMode == WriteOnly )
155 QFile file( _fileName );
157 if ( ! file.open( QIODevice::ReadOnly ) )
159 cerr << "Can't load settings from " << (const char *) _fileName
160 << ": " << (const char *) file.errorString()
168 Q3TextStream str( &file );
169 str.setEncoding( Q3TextStream::UnicodeUTF8 );
173 while ( ! file.atEnd() )
175 line = str.readLine().stripWhiteSpace();
181 if ( line.isEmpty() ) continue;
184 // Skip comment lines
186 if ( line.startsWith( "#" ) ) continue;
187 if ( line.startsWith( ";" ) ) continue;
188 if ( line.startsWith( "//" ) ) continue;
191 if ( line.startsWith( "[" ) )
195 line.replace( QRegExp( "^\\[\\s*" ), "" );
196 line.replace( QRegExp( "\\s*\\].*$" ), "" );
199 else if ( line.contains( "=" ) )
203 QString key = line.section( "=", 0, 0 ).stripWhiteSpace();
204 QString value = line.section( "=", 1, 1 ).stripWhiteSpace();
206 value.replace( QRegExp( "^\"" ), "" ); // strip leading "
207 value.replace( QRegExp( "\"$" ), "" ); // strip trailing "
208 value.replace( "\\\"", "\"" ); // un-escape "
210 // qDebug( "Read %s=%s", (const char *) key, (const char *) value );
215 qWarning( "%s:%d: Syntax error: %s",
216 (const char *) _fileName, lineCount, (const char *) line );
226 bool QY2Settings::save()
228 if ( _accessMode == ReadOnly )
231 QFile file( _fileName );
233 if ( ! file.open( QIODevice::WriteOnly ) )
235 cerr << "Can't save settings to " << (const char *) _fileName
236 << ": " << (const char *) file.errorString()
242 Q3TextStream str( &file );
243 str.setEncoding( Q3TextStream::UnicodeUTF8 );
245 // The default section must be saved first since it doesn't have a section
246 // name that could be used for a headline
247 saveSection( str, _defaultSection );
249 SectionIterator sectIt( _sections );
253 if ( *sectIt != _defaultSection )
254 saveSection( str, *sectIt );
264 void QY2Settings::saveSection( Q3TextStream & str, Section * sect )
268 if ( ! sect->name().isEmpty() )
269 str << "[" << sect->name() << "]" << endl;
271 // value=key pairs for this section
273 for ( Section::iterator it = sect->begin();
277 QString value = it.data();
278 value.replace( "\"", "\\\"" ); // Escape embedded " with \"
280 str << it.key() << "= \"" << value << "\"" << endl;
287 void QY2Settings::set( const QString & key, const QString & value )
289 _currentSection->insert( key, value );
294 void QY2Settings::addSection( const QString & sectionName )
296 _currentSection = _sections[ sectionName ]; // already there?
298 if ( _currentSection ) // -> use it
301 _currentSection = new Section( sectionName );
302 Q_CHECK_PTR( _currentSection );
304 _sections.insert( sectionName, _currentSection );
309 void QY2Settings::clearSection()
311 _currentSection->clear();