Diffstat (limited to 'noncore/games/kpacman/config.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/kpacman/config.cpp | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/noncore/games/kpacman/config.cpp b/noncore/games/kpacman/config.cpp new file mode 100644 index 0000000..b9e3607 --- a/dev/null +++ b/noncore/games/kpacman/config.cpp | |||
@@ -0,0 +1,394 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qt Palmtop Environment. | ||
5 | ** | ||
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 | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
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. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | ** $Id$ | ||
20 | ** | ||
21 | **********************************************************************/ | ||
22 | |||
23 | #include "config.h" | ||
24 | |||
25 | #include <qfile.h> | ||
26 | #include <qdir.h> | ||
27 | #include <qfileinfo.h> | ||
28 | #include <qtextstream.h> | ||
29 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | ||
30 | #include <qtextcodec.h> | ||
31 | #endif | ||
32 | #include <stdlib.h> | ||
33 | #include <sys/stat.h> | ||
34 | #include <sys/types.h> | ||
35 | #include <fcntl.h> | ||
36 | #include <unistd.h> | ||
37 | |||
38 | /*! | ||
39 | \internal | ||
40 | */ | ||
41 | QString Config::configFilename(const QString& name, Domain d) | ||
42 | { | ||
43 | switch (d) { | ||
44 | case File: | ||
45 | return name; | ||
46 | case User: { | ||
47 | QDir dir = (QString(getenv("HOME")) + "/Settings"); | ||
48 | if ( !dir.exists() ) | ||
49 | mkdir(dir.path().local8Bit(),0700); | ||
50 | return dir.path() + "/" + name + ".conf"; | ||
51 | } | ||
52 | } | ||
53 | return name; | ||
54 | } | ||
55 | |||
56 | /*! | ||
57 | \class Config config.h | ||
58 | \brief The Config class provides for saving application cofniguration state. | ||
59 | |||
60 | You should keep a Config in existence only while you do not want others | ||
61 | to be able to change the state. There is no locking currently, but there | ||
62 | may be in the future. | ||
63 | */ | ||
64 | |||
65 | /*! | ||
66 | \enum Config::ConfigGroup | ||
67 | \internal | ||
68 | */ | ||
69 | |||
70 | /*! | ||
71 | \enum Config::Domain | ||
72 | |||
73 | \value File | ||
74 | \value User | ||
75 | |||
76 | See Config for details. | ||
77 | */ | ||
78 | |||
79 | /*! | ||
80 | Constructs a config that will load or create a configuration with the | ||
81 | given \a name in the given \a domain. | ||
82 | |||
83 | You must call setGroup() before doing much else with the Config. | ||
84 | |||
85 | In the default Domain, \e User, | ||
86 | the configuration is user-specific. \a name should not contain "/" in | ||
87 | this case, and in general should be the name of the C++ class that is | ||
88 | primarily responsible for maintaining the configuration. | ||
89 | |||
90 | In the File Domain, \a name is an absolute filename. | ||
91 | */ | ||
92 | Config::Config( const QString &name, Domain domain ) | ||
93 | : filename( configFilename(name,domain) ) | ||
94 | { | ||
95 | git = groups.end(); | ||
96 | read(); | ||
97 | |||
98 | lang = getenv("LANG"); | ||
99 | int i = lang.find("."); | ||
100 | if ( i > 0 ) | ||
101 | lang = lang.left( i ); | ||
102 | i = lang.find( "_" ); | ||
103 | if ( i > 0 ) | ||
104 | glang = lang.left(i); | ||
105 | } | ||
106 | |||
107 | /*! | ||
108 | Writes any changes to disk and destroys the in-memory object. | ||
109 | */ | ||
110 | Config::~Config() | ||
111 | { | ||
112 | if ( changed ) | ||
113 | write(); | ||
114 | } | ||
115 | |||
116 | /*! | ||
117 | Returns whether the current group has an entry called \a key. | ||
118 | */ | ||
119 | bool Config::hasKey( const QString &key ) const | ||
120 | { | ||
121 | if ( groups.end() == git ) | ||
122 | return FALSE; | ||
123 | ConfigGroup::ConstIterator it = ( *git ).find( key ); | ||
124 | return it != ( *git ).end(); | ||
125 | } | ||
126 | |||
127 | /*! | ||
128 | Sets the current group for subsequent reading and writing of | ||
129 | entries to \a gname. Grouping allows the application to partition the namespace. | ||
130 | |||
131 | This function must be called prior to any reading or writing | ||
132 | of entries. | ||
133 | |||
134 | The \a gname must not be empty. | ||
135 | */ | ||
136 | void Config::setGroup( const QString &gname ) | ||
137 | { | ||
138 | QMap< QString, ConfigGroup>::Iterator it = groups.find( gname ); | ||
139 | if ( it == groups.end() ) { | ||
140 | ConfigGroup *grp = new ConfigGroup; | ||
141 | git = groups.insert( gname, *grp ); | ||
142 | changed = TRUE; | ||
143 | return; | ||
144 | } | ||
145 | git = it; | ||
146 | } | ||
147 | |||
148 | /*! | ||
149 | Writes a (\a key, \a value) entry to the current group. | ||
150 | |||
151 | \sa readEntry() | ||
152 | */ | ||
153 | void Config::writeEntry( const QString &key, const QString &value ) | ||
154 | { | ||
155 | if ( git == groups.end() ) { | ||
156 | qWarning( "no group set" ); | ||
157 | return; | ||
158 | } | ||
159 | if ( (*git)[key] != value ) { | ||
160 | ( *git ).insert( key, value ); | ||
161 | changed = TRUE; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | /*! | ||
166 | Writes a (\a key, \a num) entry to the current group. | ||
167 | |||
168 | \sa readNumEntry() | ||
169 | */ | ||
170 | void Config::writeEntry( const QString &key, int num ) | ||
171 | { | ||
172 | QString s; | ||
173 | s.setNum( num ); | ||
174 | writeEntry( key, s ); | ||
175 | } | ||
176 | |||
177 | #ifdef Q_HAS_BOOL_TYPE | ||
178 | /*! | ||
179 | Writes a (\a key, \a b) entry to the current group. | ||
180 | |||
181 | \sa readBoolEntry() | ||
182 | */ | ||
183 | void Config::writeEntry( const QString &key, bool b ) | ||
184 | { | ||
185 | QString s; | ||
186 | s.setNum( ( int )b ); | ||
187 | writeEntry( key, s ); | ||
188 | } | ||
189 | #endif | ||
190 | |||
191 | /*! | ||
192 | Writes a (\a key, \a lst) entry to the current group. The list | ||
193 | is separated by \a sep, so the strings must not contain that character. | ||
194 | |||
195 | \sa readListEntry() | ||
196 | */ | ||
197 | void Config::writeEntry( const QString &key, const QStringList &lst, const QChar &sep ) | ||
198 | { | ||
199 | QString s; | ||
200 | QStringList::ConstIterator it = lst.begin(); | ||
201 | for ( ; it != lst.end(); ++it ) | ||
202 | s += *it + sep; | ||
203 | writeEntry( key, s ); | ||
204 | } | ||
205 | |||
206 | |||
207 | |||
208 | /*! | ||
209 | Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry. | ||
210 | */ | ||
211 | QString Config::readEntry( const QString &key, const QString &deflt ) | ||
212 | { | ||
213 | QString res = readEntryDirect( key+"["+lang+"]" ); | ||
214 | if ( !res.isNull() ) | ||
215 | return res; | ||
216 | if ( !glang.isEmpty() ) { | ||
217 | res = readEntryDirect( key+"["+glang+"]" ); | ||
218 | if ( !res.isNull() ) | ||
219 | return res; | ||
220 | } | ||
221 | return readEntryDirect( key, deflt ); | ||
222 | } | ||
223 | |||
224 | /*! | ||
225 | \internal | ||
226 | */ | ||
227 | QString Config::readEntryDirect( const QString &key, const QString &deflt ) | ||
228 | { | ||
229 | if ( git == groups.end() ) { | ||
230 | //qWarning( "no group set" ); | ||
231 | return deflt; | ||
232 | } | ||
233 | ConfigGroup::Iterator it = ( *git ).find( key ); | ||
234 | if ( it != ( *git ).end() ) | ||
235 | return *it; | ||
236 | else | ||
237 | return deflt; | ||
238 | } | ||
239 | |||
240 | /*! | ||
241 | Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry. | ||
242 | */ | ||
243 | int Config::readNumEntry( const QString &key, int deflt ) | ||
244 | { | ||
245 | QString s = readEntry( key ); | ||
246 | if ( s.isEmpty() ) | ||
247 | return deflt; | ||
248 | else | ||
249 | return s.toInt(); | ||
250 | } | ||
251 | |||
252 | /*! | ||
253 | Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry. | ||
254 | */ | ||
255 | bool Config::readBoolEntry( const QString &key, bool deflt ) | ||
256 | { | ||
257 | QString s = readEntry( key ); | ||
258 | if ( s.isEmpty() ) | ||
259 | return deflt; | ||
260 | else | ||
261 | return (bool)s.toInt(); | ||
262 | } | ||
263 | |||
264 | /*! | ||
265 | Reads a string list entry stored with \a key, and with \a sep as the separator. | ||
266 | */ | ||
267 | QStringList Config::readListEntry( const QString &key, const QChar &sep ) | ||
268 | { | ||
269 | QString s = readEntry( key ); | ||
270 | if ( s.isEmpty() ) | ||
271 | return QStringList(); | ||
272 | else | ||
273 | return QStringList::split( sep, s ); | ||
274 | } | ||
275 | |||
276 | /*! | ||
277 | Removes all entries from the current group. | ||
278 | */ | ||
279 | void Config::clearGroup() | ||
280 | { | ||
281 | if ( git == groups.end() ) { | ||
282 | qWarning( "no group set" ); | ||
283 | return; | ||
284 | } | ||
285 | if ( !(*git).isEmpty() ) { | ||
286 | ( *git ).clear(); | ||
287 | changed = TRUE; | ||
288 | } | ||
289 | } | ||
290 | |||
291 | /*! | ||
292 | \internal | ||
293 | */ | ||
294 | void Config::write( const QString &fn ) | ||
295 | { | ||
296 | if ( !fn.isEmpty() ) | ||
297 | filename = fn; | ||
298 | |||
299 | QFile f( filename ); | ||
300 | if ( !f.open( IO_WriteOnly ) ) { | ||
301 | qWarning( "could not open for writing `%s'", filename.latin1() ); | ||
302 | git = groups.end(); | ||
303 | return; | ||
304 | } | ||
305 | |||
306 | QTextStream s( &f ); | ||
307 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | ||
308 | // The below should work, but doesn't in Qt 2.3.0 | ||
309 | s.setCodec( QTextCodec::codecForMib( 106 ) ); | ||
310 | #else | ||
311 | s.setEncoding( QTextStream::UnicodeUTF8 ); | ||
312 | #endif | ||
313 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); | ||
314 | for ( ; g_it != groups.end(); ++g_it ) { | ||
315 | s << "[" << g_it.key() << "]" << "\n"; | ||
316 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); | ||
317 | for ( ; e_it != ( *g_it ).end(); ++e_it ) | ||
318 | s << e_it.key() << " = " << *e_it << "\n"; | ||
319 | } | ||
320 | |||
321 | f.close(); | ||
322 | } | ||
323 | |||
324 | /*! | ||
325 | Returns whether the Config is in a valid state. | ||
326 | */ | ||
327 | bool Config::isValid() const | ||
328 | { | ||
329 | return groups.end() != git; | ||
330 | } | ||
331 | |||
332 | /*! | ||
333 | \internal | ||
334 | */ | ||
335 | void Config::read() | ||
336 | { | ||
337 | changed = FALSE; | ||
338 | |||
339 | if ( !QFileInfo( filename ).exists() ) { | ||
340 | git = groups.end(); | ||
341 | return; | ||
342 | } | ||
343 | |||
344 | QFile f( filename ); | ||
345 | if ( !f.open( IO_ReadOnly ) ) { | ||
346 | git = groups.end(); | ||
347 | return; | ||
348 | } | ||
349 | |||
350 | QTextStream s( &f ); | ||
351 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | ||
352 | // The below should work, but doesn't in Qt 2.3.0 | ||
353 | s.setCodec( QTextCodec::codecForMib( 106 ) ); | ||
354 | #else | ||
355 | s.setEncoding( QTextStream::UnicodeUTF8 ); | ||
356 | #endif | ||
357 | |||
358 | QString line; | ||
359 | while ( !s.atEnd() ) { | ||
360 | line = s.readLine(); | ||
361 | if ( !parse( line ) ) { | ||
362 | git = groups.end(); | ||
363 | return; | ||
364 | } | ||
365 | } | ||
366 | |||
367 | f.close(); | ||
368 | } | ||
369 | |||
370 | /*! | ||
371 | \internal | ||
372 | */ | ||
373 | bool Config::parse( const QString &l ) | ||
374 | { | ||
375 | QString line = l.stripWhiteSpace(); | ||
376 | if ( line[ 0 ] == QChar( '[' ) ) { | ||
377 | QString gname = line; | ||
378 | gname = gname.remove( 0, 1 ); | ||
379 | if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) | ||
380 | gname = gname.remove( gname.length() - 1, 1 ); | ||
381 | ConfigGroup *grp = new ConfigGroup; | ||
382 | git = groups.insert( gname, *grp ); | ||
383 | } else if ( !line.isEmpty() ) { | ||
384 | if ( git == groups.end() ) | ||
385 | return FALSE; | ||
386 | int eq = line.find( '=' ); | ||
387 | if ( eq == -1 ) | ||
388 | return FALSE; | ||
389 | QString key = line.left(eq).stripWhiteSpace(); | ||
390 | QString value = line.mid(eq+1).stripWhiteSpace(); | ||
391 | ( *git ).insert( key, value ); | ||
392 | } | ||
393 | return TRUE; | ||
394 | } | ||