-rw-r--r-- | libkdepim/kprefs.cpp | 463 |
1 files changed, 463 insertions, 0 deletions
diff --git a/libkdepim/kprefs.cpp b/libkdepim/kprefs.cpp new file mode 100644 index 0000000..f5e5e5a --- a/dev/null +++ b/libkdepim/kprefs.cpp | |||
@@ -0,0 +1,463 @@ | |||
1 | /* | ||
2 | This file is part of KOrganizer. | ||
3 | Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | // $Id$ | ||
22 | |||
23 | #include <qcolor.h> | ||
24 | |||
25 | #include <kconfig.h> | ||
26 | #include <kstandarddirs.h> | ||
27 | #include <kglobal.h> | ||
28 | #include <kdebug.h> | ||
29 | |||
30 | #include "kprefs.h" | ||
31 | |||
32 | class KPrefsItemBool : public KPrefsItem { | ||
33 | public: | ||
34 | KPrefsItemBool(const QString &group,const QString &name,bool *,bool defaultValue=true); | ||
35 | virtual ~KPrefsItemBool() {} | ||
36 | |||
37 | void setDefault(); | ||
38 | void readConfig(KConfig *); | ||
39 | void writeConfig(KConfig *); | ||
40 | |||
41 | private: | ||
42 | bool *mReference; | ||
43 | bool mDefault; | ||
44 | }; | ||
45 | |||
46 | class KPrefsItemInt : public KPrefsItem { | ||
47 | public: | ||
48 | KPrefsItemInt(const QString &group,const QString &name,int *,int defaultValue=0); | ||
49 | virtual ~KPrefsItemInt() {} | ||
50 | |||
51 | void setDefault(); | ||
52 | void readConfig(KConfig *); | ||
53 | void writeConfig(KConfig *); | ||
54 | |||
55 | private: | ||
56 | int *mReference; | ||
57 | int mDefault; | ||
58 | }; | ||
59 | |||
60 | |||
61 | class KPrefsItemColor : public KPrefsItem { | ||
62 | public: | ||
63 | KPrefsItemColor(const QString &group,const QString &name,QColor *, | ||
64 | const QColor &defaultValue=QColor(128,128,128)); | ||
65 | virtual ~KPrefsItemColor() {} | ||
66 | |||
67 | void setDefault(); | ||
68 | void readConfig(KConfig *); | ||
69 | void writeConfig(KConfig *); | ||
70 | |||
71 | private: | ||
72 | QColor *mReference; | ||
73 | QColor mDefault; | ||
74 | }; | ||
75 | |||
76 | |||
77 | class KPrefsItemFont : public KPrefsItem { | ||
78 | public: | ||
79 | KPrefsItemFont(const QString &group,const QString &name,QFont *, | ||
80 | const QFont &defaultValue=QFont("helvetica",12)); | ||
81 | virtual ~KPrefsItemFont() {} | ||
82 | |||
83 | void setDefault(); | ||
84 | void readConfig(KConfig *); | ||
85 | void writeConfig(KConfig *); | ||
86 | |||
87 | private: | ||
88 | QFont *mReference; | ||
89 | QFont mDefault; | ||
90 | }; | ||
91 | |||
92 | |||
93 | class KPrefsItemString : public KPrefsItem { | ||
94 | public: | ||
95 | KPrefsItemString(const QString &group,const QString &name,QString *, | ||
96 | const QString &defaultValue="", bool isPassword=false); | ||
97 | virtual ~KPrefsItemString() {} | ||
98 | |||
99 | void setDefault(); | ||
100 | void readConfig(KConfig *); | ||
101 | void writeConfig(KConfig *); | ||
102 | |||
103 | private: | ||
104 | QString *mReference; | ||
105 | QString mDefault; | ||
106 | bool mPassword; | ||
107 | }; | ||
108 | |||
109 | |||
110 | class KPrefsItemStringList : public KPrefsItem { | ||
111 | public: | ||
112 | KPrefsItemStringList(const QString &group,const QString &name,QStringList *, | ||
113 | const QStringList &defaultValue=QStringList()); | ||
114 | virtual ~KPrefsItemStringList() {} | ||
115 | |||
116 | void setDefault(); | ||
117 | void readConfig(KConfig *); | ||
118 | void writeConfig(KConfig *); | ||
119 | |||
120 | private: | ||
121 | QStringList *mReference; | ||
122 | QStringList mDefault; | ||
123 | }; | ||
124 | |||
125 | |||
126 | class KPrefsItemIntList : public KPrefsItem { | ||
127 | public: | ||
128 | KPrefsItemIntList(const QString &group,const QString &name,QValueList<int> *, | ||
129 | const QValueList<int> &defaultValue=QValueList<int>()); | ||
130 | virtual ~KPrefsItemIntList() {} | ||
131 | |||
132 | void setDefault(); | ||
133 | void readConfig(KConfig *); | ||
134 | void writeConfig(KConfig *); | ||
135 | |||
136 | private: | ||
137 | QValueList<int> *mReference; | ||
138 | QValueList<int> mDefault; | ||
139 | }; | ||
140 | |||
141 | |||
142 | KPrefsItemBool::KPrefsItemBool(const QString &group,const QString &name, | ||
143 | bool *reference,bool defaultValue) : | ||
144 | KPrefsItem(group,name) | ||
145 | { | ||
146 | mReference = reference; | ||
147 | mDefault = defaultValue; | ||
148 | } | ||
149 | |||
150 | void KPrefsItemBool::setDefault() | ||
151 | { | ||
152 | *mReference = mDefault; | ||
153 | } | ||
154 | |||
155 | void KPrefsItemBool::writeConfig(KConfig *config) | ||
156 | { | ||
157 | config->setGroup(mGroup); | ||
158 | config->writeEntry(mName,*mReference); | ||
159 | } | ||
160 | |||
161 | |||
162 | void KPrefsItemBool::readConfig(KConfig *config) | ||
163 | { | ||
164 | config->setGroup(mGroup); | ||
165 | *mReference = config->readBoolEntry(mName,mDefault); | ||
166 | } | ||
167 | |||
168 | |||
169 | KPrefsItemInt::KPrefsItemInt(const QString &group,const QString &name, | ||
170 | int *reference,int defaultValue) : | ||
171 | KPrefsItem(group,name) | ||
172 | { | ||
173 | mReference = reference; | ||
174 | mDefault = defaultValue; | ||
175 | } | ||
176 | |||
177 | void KPrefsItemInt::setDefault() | ||
178 | { | ||
179 | *mReference = mDefault; | ||
180 | } | ||
181 | |||
182 | void KPrefsItemInt::writeConfig(KConfig *config) | ||
183 | { | ||
184 | config->setGroup(mGroup); | ||
185 | config->writeEntry(mName,*mReference); | ||
186 | } | ||
187 | |||
188 | void KPrefsItemInt::readConfig(KConfig *config) | ||
189 | { | ||
190 | config->setGroup(mGroup); | ||
191 | *mReference = config->readNumEntry(mName,mDefault); | ||
192 | } | ||
193 | |||
194 | |||
195 | KPrefsItemColor::KPrefsItemColor(const QString &group,const QString &name, | ||
196 | QColor *reference,const QColor &defaultValue) : | ||
197 | KPrefsItem(group,name) | ||
198 | { | ||
199 | mReference = reference; | ||
200 | mDefault = defaultValue; | ||
201 | } | ||
202 | |||
203 | void KPrefsItemColor::setDefault() | ||
204 | { | ||
205 | *mReference = mDefault; | ||
206 | } | ||
207 | |||
208 | void KPrefsItemColor::writeConfig(KConfig *config) | ||
209 | { | ||
210 | config->setGroup(mGroup); | ||
211 | config->writeEntry(mName,*mReference); | ||
212 | } | ||
213 | |||
214 | void KPrefsItemColor::readConfig(KConfig *config) | ||
215 | { | ||
216 | config->setGroup(mGroup); | ||
217 | *mReference = config->readColorEntry(mName,&mDefault); | ||
218 | |||
219 | } | ||
220 | |||
221 | |||
222 | KPrefsItemFont::KPrefsItemFont(const QString &group,const QString &name, | ||
223 | QFont *reference,const QFont &defaultValue) : | ||
224 | KPrefsItem(group,name) | ||
225 | { | ||
226 | mReference = reference; | ||
227 | mDefault = defaultValue; | ||
228 | } | ||
229 | |||
230 | void KPrefsItemFont::setDefault() | ||
231 | { | ||
232 | *mReference = mDefault; | ||
233 | } | ||
234 | |||
235 | void KPrefsItemFont::writeConfig(KConfig *config) | ||
236 | { | ||
237 | config->setGroup(mGroup); | ||
238 | config->writeEntry(mName,*mReference); | ||
239 | } | ||
240 | |||
241 | void KPrefsItemFont::readConfig(KConfig *config) | ||
242 | { | ||
243 | config->setGroup(mGroup); | ||
244 | *mReference = config->readFontEntry(mName,&mDefault); | ||
245 | } | ||
246 | |||
247 | |||
248 | QString endecryptStr( const QString &aStr ) | ||
249 | { | ||
250 | QString result; | ||
251 | uint i; | ||
252 | for ( i = 0; i < aStr.length(); i++) | ||
253 | result += (aStr.at(i).unicode() < 0x20) ? | ||
254 | aStr.at(i) : | ||
255 | QChar(0x1001F - aStr.at(i).unicode()); | ||
256 | return result; | ||
257 | } | ||
258 | |||
259 | |||
260 | KPrefsItemString::KPrefsItemString(const QString &group,const QString &name, | ||
261 | QString *reference,const QString &defaultValue, | ||
262 | bool isPassword) : | ||
263 | KPrefsItem(group,name) | ||
264 | { | ||
265 | mReference = reference; | ||
266 | mDefault = defaultValue; | ||
267 | mPassword = isPassword; | ||
268 | } | ||
269 | |||
270 | void KPrefsItemString::setDefault() | ||
271 | { | ||
272 | *mReference = mDefault; | ||
273 | } | ||
274 | |||
275 | void KPrefsItemString::writeConfig(KConfig *config) | ||
276 | { | ||
277 | config->setGroup(mGroup); | ||
278 | if ( mPassword ) | ||
279 | config->writeEntry(mName, endecryptStr( *mReference ) ); | ||
280 | else | ||
281 | config->writeEntry(mName,*mReference); | ||
282 | } | ||
283 | |||
284 | void KPrefsItemString::readConfig(KConfig *config) | ||
285 | { | ||
286 | config->setGroup(mGroup); | ||
287 | |||
288 | QString value; | ||
289 | if ( mPassword ) { | ||
290 | value = config->readEntry( mName, endecryptStr( mDefault ) ); | ||
291 | *mReference = endecryptStr( value ); | ||
292 | } else { | ||
293 | *mReference = config->readEntry( mName, mDefault ); | ||
294 | } | ||
295 | } | ||
296 | |||
297 | |||
298 | KPrefsItemStringList::KPrefsItemStringList(const QString &group,const QString &name, | ||
299 | QStringList *reference,const QStringList &defaultValue) : | ||
300 | KPrefsItem(group,name) | ||
301 | { | ||
302 | mReference = reference; | ||
303 | mDefault = defaultValue; | ||
304 | } | ||
305 | |||
306 | void KPrefsItemStringList::setDefault() | ||
307 | { | ||
308 | *mReference = mDefault; | ||
309 | } | ||
310 | |||
311 | void KPrefsItemStringList::writeConfig(KConfig *config) | ||
312 | { | ||
313 | config->setGroup(mGroup); | ||
314 | config->writeEntry(mName,*mReference); | ||
315 | } | ||
316 | |||
317 | void KPrefsItemStringList::readConfig(KConfig *config) | ||
318 | { | ||
319 | config->setGroup(mGroup); | ||
320 | *mReference = config->readListEntry(mName); | ||
321 | } | ||
322 | |||
323 | |||
324 | KPrefsItemIntList::KPrefsItemIntList(const QString &group,const QString &name, | ||
325 | QValueList<int> *reference,const QValueList<int> &defaultValue) : | ||
326 | KPrefsItem(group,name) | ||
327 | { | ||
328 | mReference = reference; | ||
329 | mDefault = defaultValue; | ||
330 | } | ||
331 | |||
332 | void KPrefsItemIntList::setDefault() | ||
333 | { | ||
334 | *mReference = mDefault; | ||
335 | } | ||
336 | |||
337 | void KPrefsItemIntList::writeConfig(KConfig *config) | ||
338 | { | ||
339 | config->setGroup(mGroup); | ||
340 | config->writeEntry(mName,*mReference); | ||
341 | } | ||
342 | |||
343 | void KPrefsItemIntList::readConfig(KConfig *config) | ||
344 | { | ||
345 | config->setGroup(mGroup); | ||
346 | *mReference = config->readIntListEntry(mName); | ||
347 | } | ||
348 | |||
349 | |||
350 | QString *KPrefs::mCurrentGroup = 0; | ||
351 | |||
352 | KPrefs::KPrefs(const QString &configname) | ||
353 | { | ||
354 | if (!configname.isEmpty()) { | ||
355 | //qDebug("KPrefs::KPrefs %s",configname.latin1() ); | ||
356 | mConfig = new KConfig(locateLocal("config",configname)); | ||
357 | } else { | ||
358 | mConfig = KGlobal::config(); | ||
359 | } | ||
360 | |||
361 | mItems.setAutoDelete(true); | ||
362 | |||
363 | // Set default group | ||
364 | if (mCurrentGroup == 0) mCurrentGroup = new QString("No Group"); | ||
365 | } | ||
366 | |||
367 | KPrefs::~KPrefs() | ||
368 | { | ||
369 | if (mConfig != KGlobal::config()) { | ||
370 | delete mConfig; | ||
371 | } | ||
372 | } | ||
373 | |||
374 | void KPrefs::setCurrentGroup(const QString &group) | ||
375 | { | ||
376 | if (mCurrentGroup) delete mCurrentGroup; | ||
377 | mCurrentGroup = new QString(group); | ||
378 | } | ||
379 | |||
380 | KConfig *KPrefs::config() const | ||
381 | { | ||
382 | return mConfig; | ||
383 | } | ||
384 | |||
385 | void KPrefs::setDefaults() | ||
386 | { | ||
387 | KPrefsItem *item; | ||
388 | for(item = mItems.first();item;item = mItems.next()) { | ||
389 | item->setDefault(); | ||
390 | } | ||
391 | |||
392 | usrSetDefaults(); | ||
393 | } | ||
394 | |||
395 | void KPrefs::readConfig() | ||
396 | { | ||
397 | KPrefsItem *item; | ||
398 | for(item = mItems.first();item;item = mItems.next()) { | ||
399 | item->readConfig(mConfig); | ||
400 | } | ||
401 | |||
402 | usrReadConfig(); | ||
403 | } | ||
404 | |||
405 | void KPrefs::writeConfig() | ||
406 | { | ||
407 | KPrefsItem *item; | ||
408 | for(item = mItems.first();item;item = mItems.next()) { | ||
409 | item->writeConfig(mConfig); | ||
410 | } | ||
411 | |||
412 | usrWriteConfig(); | ||
413 | |||
414 | mConfig->sync(); | ||
415 | } | ||
416 | |||
417 | |||
418 | void KPrefs::addItem(KPrefsItem *item) | ||
419 | { | ||
420 | mItems.append(item); | ||
421 | } | ||
422 | |||
423 | void KPrefs::addItemBool(const QString &key,bool *reference,bool defaultValue) | ||
424 | { | ||
425 | addItem(new KPrefsItemBool(*mCurrentGroup,key,reference,defaultValue)); | ||
426 | } | ||
427 | |||
428 | void KPrefs::addItemInt(const QString &key,int *reference,int defaultValue) | ||
429 | { | ||
430 | addItem(new KPrefsItemInt(*mCurrentGroup,key,reference,defaultValue)); | ||
431 | } | ||
432 | |||
433 | void KPrefs::addItemColor(const QString &key,QColor *reference,const QColor &defaultValue) | ||
434 | { | ||
435 | addItem(new KPrefsItemColor(*mCurrentGroup,key,reference,defaultValue)); | ||
436 | } | ||
437 | |||
438 | void KPrefs::addItemFont(const QString &key,QFont *reference,const QFont &defaultValue) | ||
439 | { | ||
440 | addItem(new KPrefsItemFont(*mCurrentGroup,key,reference,defaultValue)); | ||
441 | } | ||
442 | |||
443 | void KPrefs::addItemString(const QString &key,QString *reference,const QString &defaultValue) | ||
444 | { | ||
445 | addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,false)); | ||
446 | } | ||
447 | |||
448 | void KPrefs::addItemPassword(const QString &key,QString *reference,const QString &defaultValue) | ||
449 | { | ||
450 | addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,true)); | ||
451 | } | ||
452 | |||
453 | void KPrefs::addItemStringList(const QString &key,QStringList *reference, | ||
454 | const QStringList &defaultValue) | ||
455 | { | ||
456 | addItem(new KPrefsItemStringList(*mCurrentGroup,key,reference,defaultValue)); | ||
457 | } | ||
458 | |||
459 | void KPrefs::addItemIntList(const QString &key,QValueList<int> *reference, | ||
460 | const QValueList<int> &defaultValue) | ||
461 | { | ||
462 | addItem(new KPrefsItemIntList(*mCurrentGroup,key,reference,defaultValue)); | ||
463 | } | ||