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 if ( _accessMode == ReadOnly || _accessMode == ReadWrite )
45 void QY2Settings::initSections()
47 _defaultSection = new Section( "" );
48 Q_CHECK_PTR( _defaultSection );
50 _currentSection = _defaultSection;
51 _sections.insert( "", _defaultSection );
56 QY2Settings::~QY2Settings()
58 if ( _dirty && _accessMode != ReadOnly )
63 bool QY2Settings::selectSection( const QString & sectionName )
65 _currentSection = _sections[ sectionName ];
67 if ( _currentSection )
73 _currentSection = _defaultSection;
79 void QY2Settings::selectDefaultSection()
81 _currentSection = _defaultSection;
85 QStringList QY2Settings::sections( bool includeUnnamed ) const
87 QStringList sectionList;
88 SectionIterator it( _sections );
90 while ( it.hasNext() )
92 QString sectionName = (it.value())->name();
94 if ( includeUnnamed || ! sectionName.isEmpty() )
95 sectionList.append( sectionName );
104 QString QY2Settings::get( const QString & key, const QString & fallback ) const
106 // Can't use operator[] here since we have a pointer
108 Section::const_iterator it = _currentSection->find( key );
110 if ( it == _currentSection->constEnd() )
117 QString QY2Settings::operator[] ( const QString & key )
123 bool QY2Settings::hasKey( const QString & key )
125 return _currentSection->contains( key );
129 QStringList QY2Settings::keys() const
133 for ( Section::const_iterator it = _currentSection->constBegin();
134 it != _currentSection->constEnd();
137 keyList.append( it.key() );
144 bool QY2Settings::load()
149 if ( _accessMode == WriteOnly )
153 QFile file( _fileName );
155 if ( ! file.open( QIODevice::ReadOnly ) )
157 cerr << "Can't load settings from " << qPrintable(_fileName)
158 << ": " << qPrintable(file.errorString())
166 QTextStream str( &file );
167 str.setAutoDetectUnicode(true);
171 while ( ! file.atEnd() )
173 line = str.readLine().trimmed();
179 if ( line.isEmpty() ) continue;
182 // Skip comment lines
184 if ( line.startsWith( "#" ) ) continue;
185 if ( line.startsWith( ";" ) ) continue;
186 if ( line.startsWith( "//" ) ) continue;
189 if ( line.startsWith( "[" ) )
193 line.replace( QRegExp( "^\\[\\s*" ), "" );
194 line.replace( QRegExp( "\\s*\\].*$" ), "" );
197 else if ( line.contains( "=" ) )
201 QString key = line.section( "=", 0, 0 ).trimmed();
202 QString value = line.section( "=", 1, 1 ).trimmed();
204 value.replace( QRegExp( "^\"" ), "" ); // strip leading "
205 value.replace( QRegExp( "\"$" ), "" ); // strip trailing "
206 value.replace( "\\\"", "\"" ); // un-escape "
208 // qDebug( "Read %s=%s", (const char *) key, (const char *) value );
213 qWarning( "%s:%d: Syntax error: %s",
214 qPrintable( _fileName), lineCount, qPrintable(line) );
224 bool QY2Settings::save()
226 if ( _accessMode == ReadOnly )
229 QFile file( _fileName );
231 if ( ! file.open( QIODevice::WriteOnly ) )
233 cerr << "Can't save settings to " << qPrintable(_fileName)
234 << ": " << qPrintable(file.errorString())
240 QTextStream str( &file );
241 str.setAutoDetectUnicode(true);
243 // The default section must be saved first since it doesn't have a section
244 // name that could be used for a headline
245 saveSection( str, _defaultSection );
247 SectionIterator sectIt( _sections );
249 while ( sectIt.hasNext() )
251 if ( sectIt.value() != _defaultSection )
252 saveSection( str, sectIt.value() );
262 void QY2Settings::saveSection( QTextStream & str, Section * sect )
266 if ( ! sect->name().isEmpty() )
267 str << "[" << sect->name() << "]" << endl;
269 // value=key pairs for this section
271 for ( Section::iterator it = sect->begin();
275 QString value = it.value();
276 value.replace( "\"", "\\\"" ); // Escape embedded " with \"
278 str << it.key() << "= \"" << value << "\"" << endl;
285 void QY2Settings::set( const QString & key, const QString & value )
287 _currentSection->insert( key, value );
292 void QY2Settings::addSection( const QString & sectionName )
294 _currentSection = _sections[ sectionName ]; // already there?
296 if ( _currentSection ) // -> use it
299 _currentSection = new Section( sectionName );
300 Q_CHECK_PTR( _currentSection );
302 _sections.insert( sectionName, _currentSection );
307 void QY2Settings::clearSection()
309 _currentSection->clear();