-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,297 +1,294 @@ | |||
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 | */ |