00001 /*---------------------------------------------------------------------\ 00002 | | 00003 | __ __ ____ _____ ____ | 00004 | \ \ / /_ _/ ___|_ _|___ \ | 00005 | \ V / _` \___ \ | | __) | | 00006 | | | (_| |___) || | / __/ | 00007 | |_|\__,_|____/ |_| |_____| | 00008 | | 00009 | contributed Qt classes | 00010 | (C) SuSE Linux AG | 00011 \----------------------------------------------------------------------/ 00012 00013 File: QY2Settings.h 00014 00015 Author: Stefan Hundhammer <sh@suse.de> 00016 00017 This is a pure Qt class - it can be used independently of YaST2. 00018 00019 /-*/ 00020 00021 // -*- c++ -*- 00022 00023 00024 #ifndef QY2Settings_h 00025 #define QY2Settings_h 00026 00027 #include <qstring.h> 00028 #include <qmap.h> 00029 #include <qdict.h> 00030 #include <qstringlist.h> 00031 #include <qtextstream.h> 00032 00055 class QY2Settings 00056 { 00057 public: 00058 00059 enum AccessMode 00060 { 00061 ReadOnly, 00062 ReadWrite, 00063 WriteOnly 00064 }; 00065 00071 QY2Settings( const QString & fileName, AccessMode accessMode=ReadOnly ); 00072 00079 ~QY2Settings(); 00080 00085 bool readError() const { return _readError; } 00086 00092 bool readOk() const { return ! _readError; } 00093 00108 bool selectSection( const QString & section ); 00109 00114 void selectDefaultSection(); 00115 00120 QString currentSection() const { return _currentSectionName; } 00121 00128 QStringList sections( bool includeUnnamed = false ) const; 00129 00134 QString get( const QString & key, 00135 const QString & fallback = "") const; 00136 00140 QString operator[] ( const QString & key ); 00141 00145 bool hasKey( const QString & key ); 00146 00151 QStringList keys() const; 00152 00153 // 00154 // Write access 00155 // 00156 // All write access functions will fail if the current access mode is not 00157 // one of ReadWrite / WriteOnly. 00158 // 00159 00164 void set( const QString & key, const QString & value ); 00165 00172 void addSection( const QString & section ); 00173 00177 void clearSection(); 00178 00185 bool save(); 00186 00192 bool pendingChanges() const { return _dirty; } 00193 00197 QString fileName() const { return _fileName; } 00198 00202 AccessMode accessMode() const { return _accessMode; } 00203 00204 00205 protected: 00206 00207 class Section: public QMap<QString, QString> 00208 { 00209 public: 00210 Section( const QString & name ) 00211 : QMap<QString, QString>() 00212 , _name( name ) 00213 {} 00214 QString name() const { return _name; } 00215 00216 protected: 00217 00218 QString _name; 00219 }; 00220 00221 00222 00226 void initSections(); 00227 00232 bool load(); 00233 00237 void saveSection( QTextStream & stream, Section * section ); 00238 00239 00240 // Data members 00241 00242 00243 QString _fileName; 00244 AccessMode _accessMode; 00245 bool _readError; 00246 QString _currentSectionName; 00247 Section * _currentSection; 00248 bool _dirty; 00249 00250 Section * _defaultSection; 00251 QDict<Section> _sections; 00252 00253 typedef QDictIterator<Section> SectionIterator; 00254 }; 00255 00256 #endif // QY2Settings_h