-rw-r--r-- | microkde/kconfig.cpp | 467 |
1 files changed, 467 insertions, 0 deletions
diff --git a/microkde/kconfig.cpp b/microkde/kconfig.cpp new file mode 100644 index 0000000..3f23ed2 --- a/dev/null +++ b/microkde/kconfig.cpp | |||
@@ -0,0 +1,467 @@ | |||
1 | #include <qfile.h> | ||
2 | #include <qtextstream.h> | ||
3 | #include <qwidget.h> | ||
4 | |||
5 | #include "kdebug.h" | ||
6 | |||
7 | #include "kurl.h" | ||
8 | #include "kstandarddirs.h" | ||
9 | #include "kconfig.h" | ||
10 | |||
11 | QString KConfig::mGroup = ""; | ||
12 | //QString KConfig::mGroup = "General"; | ||
13 | |||
14 | KConfig::KConfig( const QString &fileName ) | ||
15 | : mFileName( fileName ), mDirty( false ) | ||
16 | { | ||
17 | kdDebug() << "KConfig::KConfig(): '" << fileName << "'" << endl; | ||
18 | |||
19 | load(); | ||
20 | |||
21 | } | ||
22 | |||
23 | |||
24 | KConfig::~KConfig() | ||
25 | { | ||
26 | sync(); | ||
27 | } | ||
28 | |||
29 | void KConfig::setGroup( const QString &group ) | ||
30 | { | ||
31 | kdDebug() << "KConfig::setGroup(): '" << group << "'" << endl; | ||
32 | |||
33 | mGroup = group; | ||
34 | |||
35 | if ( mGroup.right( 1 ) != "/" ) mGroup += "/"; | ||
36 | } | ||
37 | |||
38 | //US | ||
39 | QString KConfig::group() const { | ||
40 | return mGroup; | ||
41 | } | ||
42 | |||
43 | //US added method | ||
44 | QValueList<int> KConfig::readIntListEntry( const QString & key) | ||
45 | { | ||
46 | // qDebug("KConfig::readIntListEntry key=%s:", key.latin1()); | ||
47 | |||
48 | QValueList<int> result; | ||
49 | |||
50 | QMap<QString,QString>::ConstIterator mit = mStringMap.find( mGroup + key ); | ||
51 | |||
52 | if ( mit == mStringMap.end() ) { | ||
53 | return result; | ||
54 | } | ||
55 | |||
56 | QStringList valuesAsStrings = QStringList::split(":", *mit ); | ||
57 | bool ok = false; | ||
58 | bool ok2 = true; | ||
59 | int val; | ||
60 | |||
61 | for ( QStringList::Iterator sit = valuesAsStrings.begin(); sit != valuesAsStrings.end(); ++sit ) { | ||
62 | val = (*sit).toInt(&ok); | ||
63 | result << val; | ||
64 | if (ok == false) { | ||
65 | qDebug("KConfig::readIntListEntry str=%s , int=%n:", (*sit).latin1(), &val); | ||
66 | ok2 = false; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | if (ok2 == false) | ||
71 | { | ||
72 | kdDebug() << "KConfig::readIntListEntry: error while reading one of the intvalues." << endl; | ||
73 | qDebug("KConfig::readIntListEntry: error while reading one of the intvalues."); | ||
74 | } | ||
75 | |||
76 | return result; | ||
77 | } | ||
78 | |||
79 | int KConfig::readNumEntry( const QString & key, int def ) | ||
80 | { | ||
81 | QString res = readEntry(key, QString::number(def ) ); | ||
82 | bool ok = false; | ||
83 | int result = res.toInt(&ok); | ||
84 | if ( ok ) | ||
85 | return result; | ||
86 | return def; | ||
87 | } | ||
88 | |||
89 | QString KConfig::readEntry( const QString &key, const QString &def ) | ||
90 | { | ||
91 | QMap<QString,QString>::ConstIterator it = mStringMap.find( mGroup + key ); | ||
92 | |||
93 | if ( it == mStringMap.end() ) { | ||
94 | return def; | ||
95 | } | ||
96 | |||
97 | return *it; | ||
98 | } | ||
99 | |||
100 | QStringList KConfig::readListEntry( const QString &key ) | ||
101 | { | ||
102 | QMap<QString,QString>::ConstIterator it = mStringMap.find( mGroup + key ); | ||
103 | |||
104 | if ( it == mStringMap.end() ) { | ||
105 | return QStringList(); | ||
106 | } | ||
107 | return QStringList::split(":", *it ); | ||
108 | |||
109 | } | ||
110 | |||
111 | bool KConfig::readBoolEntry( const QString &key, bool def ) | ||
112 | { | ||
113 | QMap<QString,bool>::ConstIterator it = mBoolMap.find( mGroup + key ); | ||
114 | |||
115 | if ( it == mBoolMap.end() ) { | ||
116 | return def; | ||
117 | } | ||
118 | |||
119 | return *it; | ||
120 | } | ||
121 | |||
122 | QColor KConfig::readColorEntry( const QString & e, QColor *def ) | ||
123 | { | ||
124 | |||
125 | QStringList l; | ||
126 | l = readListEntry( e ); | ||
127 | if (l.count() != 3 ) { | ||
128 | if ( def ) | ||
129 | return *def; | ||
130 | else | ||
131 | return QColor(); | ||
132 | } | ||
133 | QColor c ( l[0].toInt(), l[1].toInt(), l[2].toInt() ); | ||
134 | return c; | ||
135 | } | ||
136 | |||
137 | QFont KConfig::readFontEntry( const QString & e, QFont *def ) | ||
138 | { | ||
139 | QStringList font = readListEntry( e ); | ||
140 | if ( font.isEmpty() ) | ||
141 | return *def; | ||
142 | QFont f; | ||
143 | f.setFamily( font[0]); | ||
144 | f.setBold ( font[1] == "bold"); | ||
145 | f.setPointSize ( font[2].toInt()); | ||
146 | f.setItalic( font[1] == "italic" ); | ||
147 | return f; | ||
148 | } | ||
149 | |||
150 | QDateTime KConfig::readDateTimeEntry( const QString &key, const QDateTime *def ) | ||
151 | { | ||
152 | QMap<QString,QDateTime>::ConstIterator it = mDateTimeMap.find( mGroup + key ); | ||
153 | |||
154 | if ( it == mDateTimeMap.end() ) { | ||
155 | if ( def ) return *def; | ||
156 | else return QDateTime(); | ||
157 | } | ||
158 | |||
159 | return *it; | ||
160 | } | ||
161 | |||
162 | //US added method | ||
163 | void KConfig::writeEntry( const QString &key, const QValueList<int> &value) | ||
164 | { | ||
165 | QStringList valuesAsStrings; | ||
166 | |||
167 | QValueList<int>::ConstIterator it; | ||
168 | |||
169 | for( it = value.begin(); it != value.end(); ++it ) | ||
170 | { | ||
171 | valuesAsStrings << QString::number(*it); | ||
172 | } | ||
173 | |||
174 | mStringMap.insert( mGroup + key, valuesAsStrings.join(":") ); | ||
175 | mDirty = true; | ||
176 | } | ||
177 | |||
178 | void KConfig::writeEntry( const QString & key , int num ) | ||
179 | { | ||
180 | writeEntry( key, QString::number ( num ) ); | ||
181 | } | ||
182 | |||
183 | void KConfig::writeEntry( const QString &key, const QString &value ) | ||
184 | { | ||
185 | mStringMap.insert( mGroup + key, value ); | ||
186 | |||
187 | mDirty = true; | ||
188 | } | ||
189 | |||
190 | void KConfig::writeEntry( const QString &key, const QStringList &value ) | ||
191 | { | ||
192 | mStringMap.insert( mGroup + key, value.join(":") ); | ||
193 | |||
194 | mDirty = true; | ||
195 | } | ||
196 | |||
197 | void KConfig::writeEntry( const QString &key, bool value) | ||
198 | { | ||
199 | mBoolMap.insert( mGroup + key, value ); | ||
200 | |||
201 | mDirty = true; | ||
202 | } | ||
203 | |||
204 | void KConfig::writeEntry( const QString & e, const QColor & c ) | ||
205 | { | ||
206 | QStringList l; | ||
207 | l.append( QString::number ( c.red() ) ); | ||
208 | l.append( QString::number ( c.green() ) ); | ||
209 | l.append( QString::number ( c.blue() ) ); | ||
210 | writeEntry( e, l ); | ||
211 | } | ||
212 | |||
213 | void KConfig::writeEntry( const QString & e , const QFont & f ) | ||
214 | { | ||
215 | QStringList font; | ||
216 | font.append( f.family()); | ||
217 | font.append( (!f.bold ()?"nonbold":"bold") ); | ||
218 | font.append( QString::number ( f.pointSize () ) ); | ||
219 | font.append( !f.italic ()?"nonitalic":"italic" ); | ||
220 | writeEntry( e, font ); | ||
221 | } | ||
222 | |||
223 | void KConfig::writeEntry( const QString &key, const QDateTime &dt ) | ||
224 | { | ||
225 | mDateTimeMap.insert( mGroup + key, dt ); | ||
226 | } | ||
227 | |||
228 | void KConfig::load() | ||
229 | { | ||
230 | kdDebug() << "KConfig::load(): " << mFileName << endl; | ||
231 | |||
232 | QFile f( mFileName ); | ||
233 | if ( !f.open( IO_ReadOnly ) ) { | ||
234 | qDebug("KConfig: could not open file %s ",mFileName.latin1() ); | ||
235 | return; | ||
236 | } | ||
237 | |||
238 | mBoolMap.clear(); | ||
239 | mStringMap.clear(); | ||
240 | |||
241 | QTextStream t( &f ); | ||
242 | |||
243 | QString line = t.readLine(); | ||
244 | |||
245 | while ( !line.isNull() ) { | ||
246 | QStringList tokens = QStringList::split( ",", line ); | ||
247 | if ( tokens[0] == "bool" ) { | ||
248 | bool value = false; | ||
249 | if ( tokens[2] == "1" ) value = true; | ||
250 | mBoolMap.insert( tokens[1], value ); | ||
251 | } else if ( tokens[0] == "QString" ) { | ||
252 | QString value = tokens[2]; | ||
253 | mStringMap.insert( tokens[1], value ); | ||
254 | } else if ( tokens[0] == "QDateTime" ) { | ||
255 | #if 0 | ||
256 | int year = tokens[2].toInt(); | ||
257 | QDateTime dt( QDate( year, | ||
258 | tokens[3].toInt(), | ||
259 | tokens[4].toInt() ), | ||
260 | QTime( tokens[5].toInt(), tokens[6].toInt(), | ||
261 | tokens[7].toInt() ) ); | ||
262 | mDateTimeMap.insert( tokens[1], dt ); | ||
263 | #endif | ||
264 | } | ||
265 | |||
266 | line = t.readLine(); | ||
267 | } | ||
268 | } | ||
269 | |||
270 | void KConfig::sync() | ||
271 | { | ||
272 | |||
273 | if ( !mDirty ) return; | ||
274 | //qDebug("KConfig::sync() %s ",mFileName.latin1() ); | ||
275 | //kdDebug() << "KConfig::sync(): " << mFileName << endl; | ||
276 | |||
277 | //US I took the following code from a newer version of KDE | ||
278 | // Create the containing dir if needed | ||
279 | KURL path; | ||
280 | path.setPath(mFileName); | ||
281 | QString dir=path.directory(); | ||
282 | KStandardDirs::makeDir(dir); | ||
283 | |||
284 | QFile f( mFileName ); | ||
285 | if ( !f.open( IO_WriteOnly ) ) { | ||
286 | |||
287 | qDebug("KConfig::sync() Can't open file %s ",mFileName.latin1() ); | ||
288 | |||
289 | return; | ||
290 | } | ||
291 | |||
292 | QTextStream t( &f ); | ||
293 | |||
294 | QMap<QString,bool>::ConstIterator itBool; | ||
295 | for( itBool = mBoolMap.begin(); itBool != mBoolMap.end(); ++itBool ) { | ||
296 | t << "bool," << itBool.key() << "," << ( *itBool ? "1" : "0" ) << endl; | ||
297 | } | ||
298 | |||
299 | QMap<QString,QString>::ConstIterator itString; | ||
300 | for( itString = mStringMap.begin(); itString != mStringMap.end(); ++itString ) { | ||
301 | t << "QString," << itString.key() << "," << (*itString ) << endl; | ||
302 | } | ||
303 | |||
304 | QMap<QString,QDateTime>::ConstIterator itDateTime; | ||
305 | for( itDateTime = mDateTimeMap.begin(); itDateTime != mDateTimeMap.end(); ++itDateTime ) { | ||
306 | QDateTime dt = *itDateTime; | ||
307 | t << "QDateTime," << itDateTime.key() << "," | ||
308 | << dt.date().year() << "," | ||
309 | << dt.date().month() << "," | ||
310 | << dt.date().day() << "," | ||
311 | << dt.time().hour() << "," | ||
312 | << dt.time().minute() << "," | ||
313 | << dt.time().second() << endl; | ||
314 | } | ||
315 | |||
316 | f.close(); | ||
317 | |||
318 | mDirty = false; | ||
319 | } | ||
320 | |||
321 | |||
322 | //US I took the following deleteGroup method from a newer version from KDE. | ||
323 | /** | ||
324 | * Deletes a configuration entry group | ||
325 | * | ||
326 | * If the group is not empty and bDeep is false, nothing gets | ||
327 | * deleted and false is returned. | ||
328 | * If this group is the current group and it is deleted, the | ||
329 | * current group is undefined and should be set with setGroup() | ||
330 | * before the next operation on the configuration object. | ||
331 | * | ||
332 | * @param group The name of the group | ||
333 | * returns true if we deleted at least one entry. | ||
334 | */ | ||
335 | bool KConfig::deleteGroup( const QString& group) | ||
336 | { | ||
337 | bool dirty = false; | ||
338 | int pos; | ||
339 | |||
340 | QMap<QString,bool>::Iterator itBool; | ||
341 | for( itBool = mBoolMap.begin(); itBool != mBoolMap.end(); ++itBool ) | ||
342 | { | ||
343 | pos = itBool.key().find( group ); | ||
344 | if (pos == 0) { | ||
345 | mBoolMap.remove(itBool); | ||
346 | dirty = true; | ||
347 | } | ||
348 | } | ||
349 | |||
350 | QMap<QString,QString>::Iterator itString; | ||
351 | for( itString = mStringMap.begin(); itString != mStringMap.end(); ++itString ) | ||
352 | { | ||
353 | pos = itString.key().find( group ); | ||
354 | if (pos == 0) { | ||
355 | mStringMap.remove(itString); | ||
356 | dirty = true; | ||
357 | } | ||
358 | } | ||
359 | |||
360 | QMap<QString,QDateTime>::Iterator itDateTime; | ||
361 | for( itDateTime = mDateTimeMap.begin(); itDateTime != mDateTimeMap.end(); ++itDateTime ) | ||
362 | { | ||
363 | pos = itDateTime.key().find( group ); | ||
364 | if (pos == 0) { | ||
365 | mDateTimeMap.remove(itDateTime); | ||
366 | dirty = true; | ||
367 | } | ||
368 | } | ||
369 | |||
370 | if (dirty) | ||
371 | mDirty = true; | ||
372 | |||
373 | return dirty; | ||
374 | |||
375 | } | ||
376 | |||
377 | //US I took the following hasGroup method from a newer version from KDE. | ||
378 | /** | ||
379 | * Returns true if the specified group is known about. | ||
380 | * | ||
381 | * @param group The group to search for. | ||
382 | * @return Whether the group exists. | ||
383 | */ | ||
384 | bool KConfig::hasGroup(const QString &group) const | ||
385 | { | ||
386 | QMap<QString,bool>::ConstIterator itBool; | ||
387 | for( itBool = mBoolMap.begin(); itBool != mBoolMap.end(); ++itBool ) | ||
388 | { | ||
389 | if (itBool.key().find( group ) == 0) { | ||
390 | return true; | ||
391 | } | ||
392 | } | ||
393 | |||
394 | QMap<QString,QString>::ConstIterator itString; | ||
395 | for( itString = mStringMap.begin(); itString != mStringMap.end(); ++itString ) | ||
396 | { | ||
397 | if (itString.key().find( group ) == 0) { | ||
398 | return true; | ||
399 | } | ||
400 | } | ||
401 | |||
402 | QMap<QString,QDateTime>::ConstIterator itDateTime; | ||
403 | for( itDateTime = mDateTimeMap.begin(); itDateTime != mDateTimeMap.end(); ++itDateTime ) | ||
404 | { | ||
405 | if (itDateTime.key().find( group ) == 0) { | ||
406 | return true; | ||
407 | } | ||
408 | } | ||
409 | |||
410 | return false; | ||
411 | } | ||
412 | |||
413 | void KConfig::deleteEntry( const QString &key) | ||
414 | { | ||
415 | bool dirty = false; | ||
416 | |||
417 | QMap<QString,bool>::Iterator itBool = mBoolMap.find( mGroup + key ); | ||
418 | if ( itBool != mBoolMap.end() ) { | ||
419 | mBoolMap.remove(itBool); | ||
420 | dirty = true; | ||
421 | } | ||
422 | |||
423 | |||
424 | QMap<QString,QString>::Iterator itString = mStringMap.find( mGroup + key ); | ||
425 | if ( itString != mStringMap.end() ) { | ||
426 | mStringMap.remove(itString); | ||
427 | dirty = true; | ||
428 | } | ||
429 | |||
430 | |||
431 | QMap<QString,QDateTime>::Iterator itDateTime = mDateTimeMap.find( mGroup + key ); | ||
432 | if ( itDateTime != mDateTimeMap.end() ) { | ||
433 | mDateTimeMap.remove(itDateTime); | ||
434 | dirty = true; | ||
435 | } | ||
436 | |||
437 | if (dirty) | ||
438 | mDirty = true; | ||
439 | |||
440 | } | ||
441 | |||
442 | //US | ||
443 | QString KConfig::getFileName() | ||
444 | { | ||
445 | return mFileName; | ||
446 | } | ||
447 | |||
448 | bool KConfig::hasKey( const QString &key) | ||
449 | { | ||
450 | QMap<QString,bool>::Iterator itBool = mBoolMap.find( mGroup + key ); | ||
451 | if ( itBool != mBoolMap.end() ) { | ||
452 | return true; | ||
453 | } | ||
454 | |||
455 | QMap<QString,QString>::Iterator itString = mStringMap.find( mGroup + key ); | ||
456 | if ( itString != mStringMap.end() ) { | ||
457 | return true; | ||
458 | } | ||
459 | |||
460 | QMap<QString,QDateTime>::Iterator itDateTime = mDateTimeMap.find( mGroup + key ); | ||
461 | if ( itDateTime != mDateTimeMap.end() ) { | ||
462 | return true; | ||
463 | } | ||
464 | |||
465 | return false; | ||
466 | } | ||
467 | |||