-rw-r--r-- | library/config.cpp | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/library/config.cpp b/library/config.cpp index 9634571..e07eecb 100644 --- a/library/config.cpp +++ b/library/config.cpp | |||
@@ -1,489 +1,486 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 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 | 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 | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 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. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include <qdir.h> | 21 | #include <qdir.h> |
22 | #include <qfile.h> | 22 | #include <qfile.h> |
23 | #include <qfileinfo.h> | 23 | #include <qfileinfo.h> |
24 | #include <qmessagebox.h> | 24 | #include <qmessagebox.h> |
25 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 25 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
26 | #include <qtextcodec.h> | 26 | #include <qtextcodec.h> |
27 | #endif | 27 | #endif |
28 | #include <qtextstream.h> | 28 | #include <qtextstream.h> |
29 | 29 | ||
30 | #include <sys/stat.h> | 30 | #include <sys/stat.h> |
31 | #include <sys/types.h> | 31 | #include <sys/types.h> |
32 | #include <fcntl.h> | 32 | #include <fcntl.h> |
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <unistd.h> | 34 | #include <unistd.h> |
35 | 35 | ||
36 | #define QTOPIA_INTERNAL_LANGLIST | ||
36 | #include "config.h" | 37 | #include "config.h" |
38 | #include "global.h" | ||
37 | 39 | ||
38 | 40 | ||
39 | /*! | 41 | /*! |
40 | \internal | 42 | \internal |
41 | */ | 43 | */ |
42 | QString Config::configFilename(const QString& name, Domain d) | 44 | QString Config::configFilename(const QString& name, Domain d) |
43 | { | 45 | { |
44 | switch (d) { | 46 | switch (d) { |
45 | case File: | 47 | case File: |
46 | return name; | 48 | return name; |
47 | case User: { | 49 | case User: { |
48 | QDir dir = (QString(getenv("HOME")) + "/Settings"); | 50 | QDir dir = (QString(getenv("HOME")) + "/Settings"); |
49 | if ( !dir.exists() ) | 51 | if ( !dir.exists() ) |
50 | mkdir(dir.path().local8Bit(),0700); | 52 | mkdir(dir.path().local8Bit(),0700); |
51 | return dir.path() + "/" + name + ".conf"; | 53 | return dir.path() + "/" + name + ".conf"; |
52 | } | 54 | } |
53 | } | 55 | } |
54 | return name; | 56 | return name; |
55 | } | 57 | } |
56 | 58 | ||
57 | /*! | 59 | /*! |
58 | \class Config config.h | 60 | \class Config config.h |
59 | \brief The Config class provides for saving application cofniguration state. | 61 | \brief The Config class provides for saving application cofniguration state. |
60 | 62 | ||
61 | You should keep a Config in existence only while you do not want others | 63 | You should keep a Config in existence only while you do not want others |
62 | to be able to change the state. There is no locking currently, but there | 64 | to be able to change the state. There is no locking currently, but there |
63 | may be in the future. | 65 | may be in the future. |
64 | */ | 66 | */ |
65 | 67 | ||
66 | /*! | 68 | /*! |
67 | \enum Config::ConfigGroup | 69 | \enum Config::ConfigGroup |
68 | \internal | 70 | \internal |
69 | */ | 71 | */ |
70 | 72 | ||
71 | /*! | 73 | /*! |
72 | \enum Config::Domain | 74 | \enum Config::Domain |
73 | 75 | ||
74 | \value File | 76 | \value File |
75 | \value User | 77 | \value User |
76 | 78 | ||
77 | See Config for details. | 79 | See Config for details. |
78 | */ | 80 | */ |
79 | 81 | ||
80 | /*! | 82 | /*! |
81 | Constructs a config that will load or create a configuration with the | 83 | Constructs a config that will load or create a configuration with the |
82 | given \a name in the given \a domain. | 84 | given \a name in the given \a domain. |
83 | 85 | ||
84 | You must call setGroup() before doing much else with the Config. | 86 | You must call setGroup() before doing much else with the Config. |
85 | 87 | ||
86 | In the default Domain, \e User, | 88 | In the default Domain, \e User, |
87 | the configuration is user-specific. \a name should not contain "/" in | 89 | the configuration is user-specific. \a name should not contain "/" in |
88 | this case, and in general should be the name of the C++ class that is | 90 | this case, and in general should be the name of the C++ class that is |
89 | primarily responsible for maintaining the configuration. | 91 | primarily responsible for maintaining the configuration. |
90 | 92 | ||
91 | In the File Domain, \a name is an absolute filename. | 93 | In the File Domain, \a name is an absolute filename. |
92 | */ | 94 | */ |
93 | Config::Config( const QString &name, Domain domain ) | 95 | Config::Config( const QString &name, Domain domain ) |
94 | : filename( configFilename(name,domain) ) | 96 | : filename( configFilename(name,domain) ) |
95 | { | 97 | { |
96 | git = groups.end(); | 98 | git = groups.end(); |
97 | read(); | 99 | read(); |
98 | 100 | QStringList l = Global::languageList(); | |
99 | lang = getenv("LANG"); | 101 | lang = l[0]; |
100 | int i = lang.find("."); | 102 | glang = l[1]; |
101 | if ( i > 0 ) | ||
102 | lang = lang.left( i ); | ||
103 | i = lang.find( "_" ); | ||
104 | if ( i > 0 ) | ||
105 | glang = lang.left(i); | ||
106 | } | 103 | } |
107 | 104 | ||
108 | /*! | 105 | /*! |
109 | Writes any changes to disk and destroys the in-memory object. | 106 | Writes any changes to disk and destroys the in-memory object. |
110 | */ | 107 | */ |
111 | Config::~Config() | 108 | Config::~Config() |
112 | { | 109 | { |
113 | if ( changed ) | 110 | if ( changed ) |
114 | write(); | 111 | write(); |
115 | } | 112 | } |
116 | 113 | ||
117 | /*! | 114 | /*! |
118 | Returns whether the current group has an entry called \a key. | 115 | Returns whether the current group has an entry called \a key. |
119 | */ | 116 | */ |
120 | bool Config::hasKey( const QString &key ) const | 117 | bool Config::hasKey( const QString &key ) const |
121 | { | 118 | { |
122 | if ( groups.end() == git ) | 119 | if ( groups.end() == git ) |
123 | return FALSE; | 120 | return FALSE; |
124 | ConfigGroup::ConstIterator it = ( *git ).find( key ); | 121 | ConfigGroup::ConstIterator it = ( *git ).find( key ); |
125 | return it != ( *git ).end(); | 122 | return it != ( *git ).end(); |
126 | } | 123 | } |
127 | 124 | ||
128 | /*! | 125 | /*! |
129 | Sets the current group for subsequent reading and writing of | 126 | Sets the current group for subsequent reading and writing of |
130 | entries to \a gname. Grouping allows the application to partition the namespace. | 127 | entries to \a gname. Grouping allows the application to partition the namespace. |
131 | 128 | ||
132 | This function must be called prior to any reading or writing | 129 | This function must be called prior to any reading or writing |
133 | of entries. | 130 | of entries. |
134 | 131 | ||
135 | The \a gname must not be empty. | 132 | The \a gname must not be empty. |
136 | */ | 133 | */ |
137 | void Config::setGroup( const QString &gname ) | 134 | void Config::setGroup( const QString &gname ) |
138 | { | 135 | { |
139 | QMap< QString, ConfigGroup>::Iterator it = groups.find( gname ); | 136 | QMap< QString, ConfigGroup>::Iterator it = groups.find( gname ); |
140 | if ( it == groups.end() ) { | 137 | if ( it == groups.end() ) { |
141 | git = groups.insert( gname, ConfigGroup() ); | 138 | git = groups.insert( gname, ConfigGroup() ); |
142 | changed = TRUE; | 139 | changed = TRUE; |
143 | return; | 140 | return; |
144 | } | 141 | } |
145 | git = it; | 142 | git = it; |
146 | } | 143 | } |
147 | 144 | ||
148 | /*! | 145 | /*! |
149 | Writes a (\a key, \a value) entry to the current group. | 146 | Writes a (\a key, \a value) entry to the current group. |
150 | 147 | ||
151 | \sa readEntry() | 148 | \sa readEntry() |
152 | */ | 149 | */ |
153 | void Config::writeEntry( const QString &key, const char* value ) | 150 | void Config::writeEntry( const QString &key, const char* value ) |
154 | { | 151 | { |
155 | writeEntry(key,QString(value)); | 152 | writeEntry(key,QString(value)); |
156 | } | 153 | } |
157 | 154 | ||
158 | /*! | 155 | /*! |
159 | Writes a (\a key, \a value) entry to the current group. | 156 | Writes a (\a key, \a value) entry to the current group. |
160 | 157 | ||
161 | \sa readEntry() | 158 | \sa readEntry() |
162 | */ | 159 | */ |
163 | void Config::writeEntry( const QString &key, const QString &value ) | 160 | void Config::writeEntry( const QString &key, const QString &value ) |
164 | { | 161 | { |
165 | if ( git == groups.end() ) { | 162 | if ( git == groups.end() ) { |
166 | qWarning( "no group set" ); | 163 | qWarning( "no group set" ); |
167 | return; | 164 | return; |
168 | } | 165 | } |
169 | if ( (*git)[key] != value ) { | 166 | if ( (*git)[key] != value ) { |
170 | ( *git ).insert( key, value ); | 167 | ( *git ).insert( key, value ); |
171 | changed = TRUE; | 168 | changed = TRUE; |
172 | } | 169 | } |
173 | } | 170 | } |
174 | 171 | ||
175 | /* | 172 | /* |
176 | Note that the degree of protection offered by the encryption here is | 173 | Note that the degree of protection offered by the encryption here is |
177 | only sufficient to avoid the most casual observation of the configuration | 174 | only sufficient to avoid the most casual observation of the configuration |
178 | files. People with access to the files can write down the contents and | 175 | files. People with access to the files can write down the contents and |
179 | decrypt it using this source code. | 176 | decrypt it using this source code. |
180 | 177 | ||
181 | Conceivably, and at some burden to the user, this encryption could | 178 | Conceivably, and at some burden to the user, this encryption could |
182 | be improved. | 179 | be improved. |
183 | */ | 180 | */ |
184 | static QString encipher(const QString& plain) | 181 | static QString encipher(const QString& plain) |
185 | { | 182 | { |
186 | // mainly, we make it long | 183 | // mainly, we make it long |
187 | QString cipher; | 184 | QString cipher; |
188 | int mix=28730492; | 185 | int mix=28730492; |
189 | for (int i=0; i<(int)plain.length(); i++) { | 186 | for (int i=0; i<(int)plain.length(); i++) { |
190 | int u = plain[i].unicode(); | 187 | int u = plain[i].unicode(); |
191 | int c = u ^ mix; | 188 | int c = u ^ mix; |
192 | QString x = QString::number(c,36); | 189 | QString x = QString::number(c,36); |
193 | cipher.append(QChar('a'+x.length())); | 190 | cipher.append(QChar('a'+x.length())); |
194 | cipher.append(x); | 191 | cipher.append(x); |
195 | mix *= u; | 192 | mix *= u; |
196 | } | 193 | } |
197 | return cipher; | 194 | return cipher; |
198 | } | 195 | } |
199 | 196 | ||
200 | static QString decipher(const QString& cipher) | 197 | static QString decipher(const QString& cipher) |
201 | { | 198 | { |
202 | QString plain; | 199 | QString plain; |
203 | int mix=28730492; | 200 | int mix=28730492; |
204 | for (int i=0; i<(int)cipher.length();) { | 201 | for (int i=0; i<(int)cipher.length();) { |
205 | int l = cipher[i].unicode()-'a'; | 202 | int l = cipher[i].unicode()-'a'; |
206 | QString x = cipher.mid(i+1,l); i+=l+1; | 203 | QString x = cipher.mid(i+1,l); i+=l+1; |
207 | int u = x.toInt(0,36) ^ mix; | 204 | int u = x.toInt(0,36) ^ mix; |
208 | plain.append(QChar(u)); | 205 | plain.append(QChar(u)); |
209 | mix *= u; | 206 | mix *= u; |
210 | } | 207 | } |
211 | return plain; | 208 | return plain; |
212 | } | 209 | } |
213 | 210 | ||
214 | /*! | 211 | /*! |
215 | Writes an encrypted (\a key, \a value) entry to the current group. | 212 | Writes an encrypted (\a key, \a value) entry to the current group. |
216 | 213 | ||
217 | Note that the degree of protection offered by the encryption is | 214 | Note that the degree of protection offered by the encryption is |
218 | only sufficient to avoid the most casual observation of the configuration | 215 | only sufficient to avoid the most casual observation of the configuration |
219 | files. | 216 | files. |
220 | 217 | ||
221 | \sa readEntry() | 218 | \sa readEntry() |
222 | */ | 219 | */ |
223 | void Config::writeEntryCrypt( const QString &key, const QString &value ) | 220 | void Config::writeEntryCrypt( const QString &key, const QString &value ) |
224 | { | 221 | { |
225 | if ( git == groups.end() ) { | 222 | if ( git == groups.end() ) { |
226 | qWarning( "no group set" ); | 223 | qWarning( "no group set" ); |
227 | return; | 224 | return; |
228 | } | 225 | } |
229 | QString evalue = encipher(value); | 226 | QString evalue = encipher(value); |
230 | if ( (*git)[key] != evalue ) { | 227 | if ( (*git)[key] != evalue ) { |
231 | ( *git ).insert( key, evalue ); | 228 | ( *git ).insert( key, evalue ); |
232 | changed = TRUE; | 229 | changed = TRUE; |
233 | } | 230 | } |
234 | } | 231 | } |
235 | 232 | ||
236 | /*! | 233 | /*! |
237 | Writes a (\a key, \a num) entry to the current group. | 234 | Writes a (\a key, \a num) entry to the current group. |
238 | 235 | ||
239 | \sa readNumEntry() | 236 | \sa readNumEntry() |
240 | */ | 237 | */ |
241 | void Config::writeEntry( const QString &key, int num ) | 238 | void Config::writeEntry( const QString &key, int num ) |
242 | { | 239 | { |
243 | QString s; | 240 | QString s; |
244 | s.setNum( num ); | 241 | s.setNum( num ); |
245 | writeEntry( key, s ); | 242 | writeEntry( key, s ); |
246 | } | 243 | } |
247 | 244 | ||
248 | #ifdef Q_HAS_BOOL_TYPE | 245 | #ifdef Q_HAS_BOOL_TYPE |
249 | /*! | 246 | /*! |
250 | Writes a (\a key, \a b) entry to the current group. This is equivalent | 247 | Writes a (\a key, \a b) entry to the current group. This is equivalent |
251 | to writing a 0 or 1 as an integer entry. | 248 | to writing a 0 or 1 as an integer entry. |
252 | 249 | ||
253 | \sa readBoolEntry() | 250 | \sa readBoolEntry() |
254 | */ | 251 | */ |
255 | void Config::writeEntry( const QString &key, bool b ) | 252 | void Config::writeEntry( const QString &key, bool b ) |
256 | { | 253 | { |
257 | QString s; | 254 | QString s; |
258 | s.setNum( ( int )b ); | 255 | s.setNum( ( int )b ); |
259 | writeEntry( key, s ); | 256 | writeEntry( key, s ); |
260 | } | 257 | } |
261 | #endif | 258 | #endif |
262 | 259 | ||
263 | /*! | 260 | /*! |
264 | Writes a (\a key, \a lst) entry to the current group. The list | 261 | Writes a (\a key, \a lst) entry to the current group. The list |
265 | is separated by \a sep, so the strings must not contain that character. | 262 | is separated by \a sep, so the strings must not contain that character. |
266 | 263 | ||
267 | \sa readListEntry() | 264 | \sa readListEntry() |
268 | */ | 265 | */ |
269 | void Config::writeEntry( const QString &key, const QStringList &lst, const QChar &sep ) | 266 | void Config::writeEntry( const QString &key, const QStringList &lst, const QChar &sep ) |
270 | { | 267 | { |
271 | QString s; | 268 | QString s; |
272 | QStringList::ConstIterator it = lst.begin(); | 269 | QStringList::ConstIterator it = lst.begin(); |
273 | for ( ; it != lst.end(); ++it ) | 270 | for ( ; it != lst.end(); ++it ) |
274 | s += *it + sep; | 271 | s += *it + sep; |
275 | writeEntry( key, s ); | 272 | writeEntry( key, s ); |
276 | } | 273 | } |
277 | 274 | ||
278 | /*! | 275 | /*! |
279 | Removes the \a key entry from the current group. Does nothing if | 276 | Removes the \a key entry from the current group. Does nothing if |
280 | there is no such entry. | 277 | there is no such entry. |
281 | */ | 278 | */ |
282 | 279 | ||
283 | void Config::removeEntry( const QString &key ) | 280 | void Config::removeEntry( const QString &key ) |
284 | { | 281 | { |
285 | if ( git == groups.end() ) { | 282 | if ( git == groups.end() ) { |
286 | qWarning( "no group set" ); | 283 | qWarning( "no group set" ); |
287 | return; | 284 | return; |
288 | } | 285 | } |
289 | ( *git ).remove( key ); | 286 | ( *git ).remove( key ); |
290 | changed = TRUE; | 287 | changed = TRUE; |
291 | } | 288 | } |
292 | 289 | ||
293 | /*! | 290 | /*! |
294 | \fn bool Config::operator == ( const Config & other ) const | 291 | \fn bool Config::operator == ( const Config & other ) const |
295 | 292 | ||
296 | Tests for equality with \a other. Config objects are equal if they refer to the same filename. | 293 | Tests for equality with \a other. Config objects are equal if they refer to the same filename. |
297 | */ | 294 | */ |
298 | 295 | ||
299 | /*! | 296 | /*! |
300 | \fn bool Config::operator != ( const Config & other ) const | 297 | \fn bool Config::operator != ( const Config & other ) const |
301 | 298 | ||
302 | Tests for inequality with \a other. Config objects are equal if they refer to the same filename. | 299 | Tests for inequality with \a other. Config objects are equal if they refer to the same filename. |
303 | */ | 300 | */ |
304 | 301 | ||
305 | /*! | 302 | /*! |
306 | \fn QString Config::readEntry( const QString &key, const QString &deflt ) const | 303 | \fn QString Config::readEntry( const QString &key, const QString &deflt ) const |
307 | 304 | ||
308 | Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry. | 305 | Reads a string entry stored with \a key, defaulting to \a deflt if there is no entry. |
309 | */ | 306 | */ |
310 | 307 | ||
311 | /*! | 308 | /*! |
312 | \internal | 309 | \internal |
313 | For compatibility, non-const version. | 310 | For compatibility, non-const version. |
314 | */ | 311 | */ |
315 | QString Config::readEntry( const QString &key, const QString &deflt ) | 312 | QString Config::readEntry( const QString &key, const QString &deflt ) |
316 | { | 313 | { |
317 | QString res = readEntryDirect( key+"["+lang+"]" ); | 314 | QString res = readEntryDirect( key+"["+lang+"]" ); |
318 | if ( !res.isNull() ) | 315 | if ( !res.isNull() ) |
319 | return res; | 316 | return res; |
320 | if ( !glang.isEmpty() ) { | 317 | if ( !glang.isEmpty() ) { |
321 | res = readEntryDirect( key+"["+glang+"]" ); | 318 | res = readEntryDirect( key+"["+glang+"]" ); |
322 | if ( !res.isNull() ) | 319 | if ( !res.isNull() ) |
323 | return res; | 320 | return res; |
324 | } | 321 | } |
325 | return readEntryDirect( key, deflt ); | 322 | return readEntryDirect( key, deflt ); |
326 | } | 323 | } |
327 | 324 | ||
328 | /*! | 325 | /*! |
329 | \fn QString Config::readEntryCrypt( const QString &key, const QString &deflt ) const | 326 | \fn QString Config::readEntryCrypt( const QString &key, const QString &deflt ) const |
330 | 327 | ||
331 | Reads an encrypted string entry stored with \a key, defaulting to \a deflt if there is no entry. | 328 | Reads an encrypted string entry stored with \a key, defaulting to \a deflt if there is no entry. |
332 | */ | 329 | */ |
333 | 330 | ||
334 | /*! | 331 | /*! |
335 | \internal | 332 | \internal |
336 | For compatibility, non-const version. | 333 | For compatibility, non-const version. |
337 | */ | 334 | */ |
338 | QString Config::readEntryCrypt( const QString &key, const QString &deflt ) | 335 | QString Config::readEntryCrypt( const QString &key, const QString &deflt ) |
339 | { | 336 | { |
340 | QString res = readEntryDirect( key+"["+lang+"]" ); | 337 | QString res = readEntryDirect( key+"["+lang+"]" ); |
341 | if ( res.isNull() && glang.isEmpty() ) | 338 | if ( res.isNull() && glang.isEmpty() ) |
342 | res = readEntryDirect( key+"["+glang+"]" ); | 339 | res = readEntryDirect( key+"["+glang+"]" ); |
343 | if ( res.isNull() ) | 340 | if ( res.isNull() ) |
344 | res = readEntryDirect( key, QString::null ); | 341 | res = readEntryDirect( key, QString::null ); |
345 | if ( res.isNull() ) | 342 | if ( res.isNull() ) |
346 | return deflt; | 343 | return deflt; |
347 | return decipher(res); | 344 | return decipher(res); |
348 | } | 345 | } |
349 | 346 | ||
350 | /*! | 347 | /*! |
351 | \fn QString Config::readEntryDirect( const QString &key, const QString &deflt ) const | 348 | \fn QString Config::readEntryDirect( const QString &key, const QString &deflt ) const |
352 | \internal | 349 | \internal |
353 | */ | 350 | */ |
354 | 351 | ||
355 | /*! | 352 | /*! |
356 | \internal | 353 | \internal |
357 | For compatibility, non-const version. | 354 | For compatibility, non-const version. |
358 | */ | 355 | */ |
359 | QString Config::readEntryDirect( const QString &key, const QString &deflt ) | 356 | QString Config::readEntryDirect( const QString &key, const QString &deflt ) |
360 | { | 357 | { |
361 | if ( git == groups.end() ) { | 358 | if ( git == groups.end() ) { |
362 | //qWarning( "no group set" ); | 359 | //qWarning( "no group set" ); |
363 | return deflt; | 360 | return deflt; |
364 | } | 361 | } |
365 | ConfigGroup::ConstIterator it = ( *git ).find( key ); | 362 | ConfigGroup::ConstIterator it = ( *git ).find( key ); |
366 | if ( it != ( *git ).end() ) | 363 | if ( it != ( *git ).end() ) |
367 | return *it; | 364 | return *it; |
368 | else | 365 | else |
369 | return deflt; | 366 | return deflt; |
370 | } | 367 | } |
371 | 368 | ||
372 | /*! | 369 | /*! |
373 | \fn int Config::readNumEntry( const QString &key, int deflt ) const | 370 | \fn int Config::readNumEntry( const QString &key, int deflt ) const |
374 | Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry. | 371 | Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry. |
375 | */ | 372 | */ |
376 | 373 | ||
377 | /*! | 374 | /*! |
378 | \internal | 375 | \internal |
379 | For compatibility, non-const version. | 376 | For compatibility, non-const version. |
380 | */ | 377 | */ |
381 | int Config::readNumEntry( const QString &key, int deflt ) | 378 | int Config::readNumEntry( const QString &key, int deflt ) |
382 | { | 379 | { |
383 | QString s = readEntry( key ); | 380 | QString s = readEntry( key ); |
384 | if ( s.isEmpty() ) | 381 | if ( s.isEmpty() ) |
385 | return deflt; | 382 | return deflt; |
386 | else | 383 | else |
387 | return s.toInt(); | 384 | return s.toInt(); |
388 | } | 385 | } |
389 | 386 | ||
390 | /*! | 387 | /*! |
391 | \fn bool Config::readBoolEntry( const QString &key, bool deflt ) const | 388 | \fn bool Config::readBoolEntry( const QString &key, bool deflt ) const |
392 | Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry. | 389 | Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry. |
393 | */ | 390 | */ |
394 | 391 | ||
395 | /*! | 392 | /*! |
396 | \internal | 393 | \internal |
397 | For compatibility, non-const version. | 394 | For compatibility, non-const version. |
398 | */ | 395 | */ |
399 | bool Config::readBoolEntry( const QString &key, bool deflt ) | 396 | bool Config::readBoolEntry( const QString &key, bool deflt ) |
400 | { | 397 | { |
401 | QString s = readEntry( key ); | 398 | QString s = readEntry( key ); |
402 | if ( s.isEmpty() ) | 399 | if ( s.isEmpty() ) |
403 | return deflt; | 400 | return deflt; |
404 | else | 401 | else |
405 | return (bool)s.toInt(); | 402 | return (bool)s.toInt(); |
406 | } | 403 | } |
407 | 404 | ||
408 | /*! | 405 | /*! |
409 | \fn QStringList Config::readListEntry( const QString &key, const QChar &sep ) const | 406 | \fn QStringList Config::readListEntry( const QString &key, const QChar &sep ) const |
410 | Reads a string list entry stored with \a key, and with \a sep as the separator. | 407 | Reads a string list entry stored with \a key, and with \a sep as the separator. |
411 | */ | 408 | */ |
412 | 409 | ||
413 | /*! | 410 | /*! |
414 | \internal | 411 | \internal |
415 | For compatibility, non-const version. | 412 | For compatibility, non-const version. |
416 | */ | 413 | */ |
417 | QStringList Config::readListEntry( const QString &key, const QChar &sep ) | 414 | QStringList Config::readListEntry( const QString &key, const QChar &sep ) |
418 | { | 415 | { |
419 | QString s = readEntry( key ); | 416 | QString s = readEntry( key ); |
420 | if ( s.isEmpty() ) | 417 | if ( s.isEmpty() ) |
421 | return QStringList(); | 418 | return QStringList(); |
422 | else | 419 | else |
423 | return QStringList::split( sep, s ); | 420 | return QStringList::split( sep, s ); |
424 | } | 421 | } |
425 | 422 | ||
426 | /*! | 423 | /*! |
427 | Removes all entries from the current group. | 424 | Removes all entries from the current group. |
428 | */ | 425 | */ |
429 | void Config::clearGroup() | 426 | void Config::clearGroup() |
430 | { | 427 | { |
431 | if ( git == groups.end() ) { | 428 | if ( git == groups.end() ) { |
432 | qWarning( "no group set" ); | 429 | qWarning( "no group set" ); |
433 | return; | 430 | return; |
434 | } | 431 | } |
435 | if ( !(*git).isEmpty() ) { | 432 | if ( !(*git).isEmpty() ) { |
436 | ( *git ).clear(); | 433 | ( *git ).clear(); |
437 | changed = TRUE; | 434 | changed = TRUE; |
438 | } | 435 | } |
439 | } | 436 | } |
440 | 437 | ||
441 | /*! | 438 | /*! |
442 | \internal | 439 | \internal |
443 | */ | 440 | */ |
444 | void Config::write( const QString &fn ) | 441 | void Config::write( const QString &fn ) |
445 | { | 442 | { |
446 | QString strNewFile; | 443 | QString strNewFile; |
447 | if ( !fn.isEmpty() ) | 444 | if ( !fn.isEmpty() ) |
448 | filename = fn; | 445 | filename = fn; |
449 | strNewFile = filename + ".new"; | 446 | strNewFile = filename + ".new"; |
450 | 447 | ||
451 | QFile f( strNewFile ); | 448 | QFile f( strNewFile ); |
452 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { | 449 | if ( !f.open( IO_WriteOnly|IO_Raw ) ) { |
453 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); | 450 | qWarning( "could not open for writing `%s'", strNewFile.latin1() ); |
454 | git = groups.end(); | 451 | git = groups.end(); |
455 | return; | 452 | return; |
456 | } | 453 | } |
457 | 454 | ||
458 | QString str; | 455 | QString str; |
459 | QCString cstr; | 456 | QCString cstr; |
460 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); | 457 | QMap< QString, ConfigGroup >::Iterator g_it = groups.begin(); |
461 | 458 | ||
462 | for ( ; g_it != groups.end(); ++g_it ) { | 459 | for ( ; g_it != groups.end(); ++g_it ) { |
463 | str += "[" + g_it.key() + "]\n"; | 460 | str += "[" + g_it.key() + "]\n"; |
464 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); | 461 | ConfigGroup::Iterator e_it = ( *g_it ).begin(); |
465 | for ( ; e_it != ( *g_it ).end(); ++e_it ) | 462 | for ( ; e_it != ( *g_it ).end(); ++e_it ) |
466 | str += e_it.key() + " = " + *e_it + "\n"; | 463 | str += e_it.key() + " = " + *e_it + "\n"; |
467 | } | 464 | } |
468 | cstr = str.utf8(); | 465 | cstr = str.utf8(); |
469 | 466 | ||
470 | int total_length; | 467 | int total_length; |
471 | total_length = f.writeBlock( cstr.data(), cstr.length() ); | 468 | total_length = f.writeBlock( cstr.data(), cstr.length() ); |
472 | if ( total_length != int(cstr.length()) ) { | 469 | if ( total_length != int(cstr.length()) ) { |
473 | QMessageBox::critical( 0, QObject::tr("Out of Space"), | 470 | QMessageBox::critical( 0, QObject::tr("Out of Space"), |
474 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); | 471 | QObject::tr("There was a problem creating\nConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); |
475 | f.close(); | 472 | f.close(); |
476 | QFile::remove( strNewFile ); | 473 | QFile::remove( strNewFile ); |
477 | return; | 474 | return; |
478 | } | 475 | } |
479 | 476 | ||
480 | f.close(); | 477 | f.close(); |
481 | // now rename the file... | 478 | // now rename the file... |
482 | if ( rename( strNewFile, filename ) < 0 ) { | 479 | if ( rename( strNewFile, filename ) < 0 ) { |
483 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), | 480 | qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), |
484 | filename.latin1() ); | 481 | filename.latin1() ); |
485 | QFile::remove( strNewFile ); | 482 | QFile::remove( strNewFile ); |
486 | } | 483 | } |
487 | } | 484 | } |
488 | 485 | ||
489 | /*! | 486 | /*! |