summaryrefslogtreecommitdiffabout
path: root/microkde
authorulf69 <ulf69>2004-08-06 20:27:28 (UTC)
committer ulf69 <ulf69>2004-08-06 20:27:28 (UTC)
commit40ac88770c32ae78e194096e758ef3818d2fb434 (patch) (unidiff)
tree45bd250d5d2329dbbf39f0879cfd4ac8037b88e9 /microkde
parentd4a1a97eb727df28484d00499894bbbdd092f13c (diff)
downloadkdepimpi-40ac88770c32ae78e194096e758ef3818d2fb434.zip
kdepimpi-40ac88770c32ae78e194096e758ef3818d2fb434.tar.gz
kdepimpi-40ac88770c32ae78e194096e758ef3818d2fb434.tar.bz2
moved files from libkdepim to microkde/kdecore
Diffstat (limited to 'microkde') (more/less context) (ignore whitespace changes)
-rw-r--r--microkde/kdecore/kprefs.cpp463
-rw-r--r--microkde/kdecore/kprefs.h301
2 files changed, 764 insertions, 0 deletions
diff --git a/microkde/kdecore/kprefs.cpp b/microkde/kdecore/kprefs.cpp
new file mode 100644
index 0000000..f5e5e5a
--- a/dev/null
+++ b/microkde/kdecore/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
32class 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
46class 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
61class 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
77class 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
93class 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
110class 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
126class 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
142KPrefsItemBool::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
150void KPrefsItemBool::setDefault()
151{
152 *mReference = mDefault;
153}
154
155void KPrefsItemBool::writeConfig(KConfig *config)
156{
157 config->setGroup(mGroup);
158 config->writeEntry(mName,*mReference);
159}
160
161
162void KPrefsItemBool::readConfig(KConfig *config)
163{
164 config->setGroup(mGroup);
165 *mReference = config->readBoolEntry(mName,mDefault);
166}
167
168
169KPrefsItemInt::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
177void KPrefsItemInt::setDefault()
178{
179 *mReference = mDefault;
180}
181
182void KPrefsItemInt::writeConfig(KConfig *config)
183{
184 config->setGroup(mGroup);
185 config->writeEntry(mName,*mReference);
186}
187
188void KPrefsItemInt::readConfig(KConfig *config)
189{
190 config->setGroup(mGroup);
191 *mReference = config->readNumEntry(mName,mDefault);
192}
193
194
195KPrefsItemColor::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
203void KPrefsItemColor::setDefault()
204{
205 *mReference = mDefault;
206}
207
208void KPrefsItemColor::writeConfig(KConfig *config)
209{
210 config->setGroup(mGroup);
211 config->writeEntry(mName,*mReference);
212}
213
214void KPrefsItemColor::readConfig(KConfig *config)
215{
216 config->setGroup(mGroup);
217 *mReference = config->readColorEntry(mName,&mDefault);
218
219}
220
221
222KPrefsItemFont::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
230void KPrefsItemFont::setDefault()
231{
232 *mReference = mDefault;
233}
234
235void KPrefsItemFont::writeConfig(KConfig *config)
236{
237 config->setGroup(mGroup);
238 config->writeEntry(mName,*mReference);
239}
240
241void KPrefsItemFont::readConfig(KConfig *config)
242{
243 config->setGroup(mGroup);
244 *mReference = config->readFontEntry(mName,&mDefault);
245}
246
247
248QString 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
260KPrefsItemString::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
270void KPrefsItemString::setDefault()
271{
272 *mReference = mDefault;
273}
274
275void 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
284void 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
298KPrefsItemStringList::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
306void KPrefsItemStringList::setDefault()
307{
308 *mReference = mDefault;
309}
310
311void KPrefsItemStringList::writeConfig(KConfig *config)
312{
313 config->setGroup(mGroup);
314 config->writeEntry(mName,*mReference);
315}
316
317void KPrefsItemStringList::readConfig(KConfig *config)
318{
319 config->setGroup(mGroup);
320 *mReference = config->readListEntry(mName);
321}
322
323
324KPrefsItemIntList::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
332void KPrefsItemIntList::setDefault()
333{
334 *mReference = mDefault;
335}
336
337void KPrefsItemIntList::writeConfig(KConfig *config)
338{
339 config->setGroup(mGroup);
340 config->writeEntry(mName,*mReference);
341}
342
343void KPrefsItemIntList::readConfig(KConfig *config)
344{
345 config->setGroup(mGroup);
346 *mReference = config->readIntListEntry(mName);
347}
348
349
350QString *KPrefs::mCurrentGroup = 0;
351
352KPrefs::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
367KPrefs::~KPrefs()
368{
369 if (mConfig != KGlobal::config()) {
370 delete mConfig;
371 }
372}
373
374void KPrefs::setCurrentGroup(const QString &group)
375{
376 if (mCurrentGroup) delete mCurrentGroup;
377 mCurrentGroup = new QString(group);
378}
379
380KConfig *KPrefs::config() const
381{
382 return mConfig;
383}
384
385void KPrefs::setDefaults()
386{
387 KPrefsItem *item;
388 for(item = mItems.first();item;item = mItems.next()) {
389 item->setDefault();
390 }
391
392 usrSetDefaults();
393}
394
395void 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
405void 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
418void KPrefs::addItem(KPrefsItem *item)
419{
420 mItems.append(item);
421}
422
423void KPrefs::addItemBool(const QString &key,bool *reference,bool defaultValue)
424{
425 addItem(new KPrefsItemBool(*mCurrentGroup,key,reference,defaultValue));
426}
427
428void KPrefs::addItemInt(const QString &key,int *reference,int defaultValue)
429{
430 addItem(new KPrefsItemInt(*mCurrentGroup,key,reference,defaultValue));
431}
432
433void KPrefs::addItemColor(const QString &key,QColor *reference,const QColor &defaultValue)
434{
435 addItem(new KPrefsItemColor(*mCurrentGroup,key,reference,defaultValue));
436}
437
438void KPrefs::addItemFont(const QString &key,QFont *reference,const QFont &defaultValue)
439{
440 addItem(new KPrefsItemFont(*mCurrentGroup,key,reference,defaultValue));
441}
442
443void KPrefs::addItemString(const QString &key,QString *reference,const QString &defaultValue)
444{
445 addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,false));
446}
447
448void KPrefs::addItemPassword(const QString &key,QString *reference,const QString &defaultValue)
449{
450 addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,true));
451}
452
453void KPrefs::addItemStringList(const QString &key,QStringList *reference,
454 const QStringList &defaultValue)
455{
456 addItem(new KPrefsItemStringList(*mCurrentGroup,key,reference,defaultValue));
457}
458
459void KPrefs::addItemIntList(const QString &key,QValueList<int> *reference,
460 const QValueList<int> &defaultValue)
461{
462 addItem(new KPrefsItemIntList(*mCurrentGroup,key,reference,defaultValue));
463}
diff --git a/microkde/kdecore/kprefs.h b/microkde/kdecore/kprefs.h
new file mode 100644
index 0000000..7014bb8
--- a/dev/null
+++ b/microkde/kdecore/kprefs.h
@@ -0,0 +1,301 @@
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 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#ifndef _KPREFS_H
21#define _KPREFS_H
22// $Id$
23
24#include <qptrlist.h>
25#include <qcolor.h>
26#include <qfont.h>
27#include <qstringlist.h>
28
29class KConfig;
30
31/**
32 @short Class for storing a preferences setting
33 @author Cornelius Schumacher
34 @see KPref
35
36 This class represents one preferences setting as used by @ref KPrefs.
37 Subclasses of KPrefsItem implement storage functions for a certain type of
38 setting. Normally you don't have to use this class directly. Use the special
39 addItem() functions of KPrefs instead. If you subclass this class you will
40 have to register instances with the function KPrefs::addItem().
41*/
42class KPrefsItem {
43 public:
44 /**
45 Constructor.
46
47 @param group Config file group.
48 @param name Config file key.
49 */
50 KPrefsItem(const QString &group,const QString &name) :
51 mGroup(group),mName(name) {}
52 /**
53 Destructor.
54 */
55 virtual ~KPrefsItem() {}
56
57 /**
58 This function is called by @ref KPrefs to set this setting to its default
59 value.
60 */
61 virtual void setDefault() = 0;
62 /**
63 This function is called by @ref KPrefs to read the value for this setting
64 from a config file.
65 value.
66 */
67 virtual void readConfig(KConfig *) = 0;
68 /**
69 This function is called by @ref KPrefs to write the value of this setting
70 to a config file.
71 */
72 virtual void writeConfig(KConfig *) = 0;
73
74 protected:
75 QString mGroup;
76 QString mName;
77};
78
79/**
80 @short Class for handling preferences settings for an application.
81 @author Cornelius Schumacher
82 @see KPrefsItem
83
84 This class provides an interface to preferences settings. Preferences items
85 can be registered by the addItem() function corresponding to the data type of
86 the seetting. KPrefs then handles reading and writing of config files and
87 setting of default values.
88
89 Normally you will subclass KPrefs, add data members for the preferences
90 settings and register the members in the constructor of the subclass.
91
92 Example:
93 <pre>
94 class MyPrefs : public KPrefs {
95 public:
96 MyPrefs()
97 {
98 setCurrentGroup("MyGroup");
99 addItemBool("MySetting1",&mMyBool,false);
100 addItemColor("MySetting2",&mMyColor,QColor(1,2,3));
101
102 setCurrentGroup("MyOtherGroup");
103 addItemFont("MySetting3",&mMyFont,QFont("helvetica",12));
104 }
105
106 bool mMyBool;
107 QColor mMyColor;
108 QFont mMyFont;
109 }
110 </pre>
111
112 It might be convenient in many cases to make this subclass of KPrefs a
113 singleton for global access from all over the application without passing
114 references to the KPrefs object around.
115
116 You can set all values to default values by calling @ref setDefaults(), write
117 the data to the configuration file by calling @ref writeConfig() and read the
118 data from the configuration file by calling @ref readConfig().
119
120 If you have items, which are not covered by the existing addItem() functions
121 you can add customized code for reading, writing and default setting by
122 implementing the functions @ref usrSetDefaults(), @ref usrReadConfig() and
123 @ref usrWriteConfig().
124
125 Internally preferences settings are stored in instances of subclasses of
126 @ref KPrefsItem. You can also add KPrefsItem subclasses for your own types
127 and call the generic @ref addItem() to register them.
128*/
129
130class KPrefs {
131 public:
132 /**
133 Constructor.
134
135 @param configname name of config file. If no name is given, the default
136 config file as returned by kapp()->config() is used.
137 */
138 KPrefs(const QString &configname=QString::null);
139 /**
140 Destructor
141 */
142 virtual ~KPrefs();
143
144 /**
145 Set preferences to default values. All registered items are set to their
146 default values.
147 */
148 void setDefaults();
149
150 /**
151 Read preferences from config file. All registered items are set to the
152 values read from disk.
153 */
154 void readConfig();
155
156 /**
157 Write preferences to config file. The values of all registered items are
158 written to disk.
159 */
160 void writeConfig();
161
162 /**
163 Set the config file group for subsequent addItem() calls. It is valid
164 until setCurrentGroup() is called with a new argument. Call this before
165 you add any items. The default value is "No Group".
166 */
167 static void setCurrentGroup(const QString &group);
168
169 /**
170 Register a custom @ref KPrefsItem.
171 */
172 void addItem(KPrefsItem *);
173
174 /**
175 Register an item of type bool.
176
177 @param key Key used in config file.
178 @param reference Pointer to the variable, which is set by readConfig()
179 and setDefaults() calls and read by writeConfig() calls.
180 @param defaultValue Default value, which is used by setDefaults() and
181 when the config file does not yet contain the key of
182 this item.
183 */
184 void addItemBool(const QString &key,bool *reference,
185 bool defaultValue=false);
186 /**
187 Register an item of type int.
188
189 @param key Key used in config file.
190 @param reference Pointer to the variable, which is set by readConfig()
191 and setDefaults() calls and read by writeConfig() calls.
192 @param defaultValue Default value, which is used by setDefaults() and
193 when the config file does not yet contain the key of
194 this item.
195 */
196 void addItemInt(const QString &key,int *reference,
197 int defaultValue=0);
198 /**
199 Register an item of type QColor.
200
201 @param key Key used in config file.
202 @param reference Pointer to the variable, which is set by readConfig()
203 and setDefaults() calls and read by writeConfig() calls.
204 @param defaultValue Default value, which is used by setDefaults() and
205 when the config file does not yet contain the key of
206 this item.
207 */
208 void addItemColor(const QString &key,QColor *reference,
209 const QColor &defaultValue=QColor(128,128,128));
210 /**
211 Register an item of type QFont.
212
213 @param key Key used in config file.
214 @param reference Pointer to the variable, which is set by readConfig()
215 and setDefaults() calls and read by writeConfig() calls.
216 @param defaultValue Default value, which is used by setDefaults() and
217 when the config file does not yet contain the key of
218 this item.
219 */
220 void addItemFont(const QString &key,QFont *reference,
221 const QFont &defaultValue=QFont("helvetica",12));
222 /**
223 Register an item of type QString.
224
225 @param key Key used in config file.
226 @param reference Pointer to the variable, which is set by readConfig()
227 and setDefaults() calls and read by writeConfig() calls.
228 @param defaultValue Default value, which is used by setDefaults() and
229 when the config file does not yet contain the key of
230 this item.
231 */
232 void addItemString(const QString &key,QString *reference,
233 const QString &defaultValue="");
234 /**
235 Register a password item of type QString. The string value is written
236 encrypted to the config file. Note that the current encryption scheme
237 is very weak.
238
239 @param key Key used in config file.
240 @param reference Pointer to the variable, which is set by readConfig()
241 and setDefaults() calls and read by writeConfig() calls.
242 @param defaultValue Default value, which is used by setDefaults() and
243 when the config file does not yet contain the key of
244 this item.
245 */
246 void addItemPassword(const QString &key,QString *reference,
247 const QString &defaultValue="");
248 /**
249 Register an item of type QStringList.
250
251 @param key Key used in config file.
252 @param reference Pointer to the variable, which is set by readConfig()
253 and setDefaults() calls and read by writeConfig() calls.
254 @param defaultValue Default value, which is used by setDefaults() and
255 when the config file does not yet contain the key of
256 this item.
257 */
258 void addItemStringList(const QString &key,QStringList *reference,
259 const QStringList &defaultValue=QStringList());
260
261 /**
262 Register an item of type QValueList<int>.
263
264 @param key Key used in config file.
265 @param reference Pointer to the variable, which is set by readConfig()
266 and setDefaults() calls and read by writeConfig() calls.
267 @param defaultValue Default value, which is used by setDefaults() and
268 when the config file does not yet contain the key of
269 this item.
270 */
271 void addItemIntList(const QString &key,QValueList<int> *reference,
272 const QValueList<int> &defaultValue=QValueList<int>());
273
274 protected:
275 /**
276 Implemented by subclasses that use special defaults.
277 */
278 virtual void usrSetDefaults() {};
279 /**
280 Implemented by subclasses that read special config values.
281 */
282 virtual void usrReadConfig() {};
283 /**
284 Implemented by subclasses that write special config values.
285 */
286 virtual void usrWriteConfig() {};
287
288 /**
289 Return the @ref KConfig object used for reading and writing the settings.
290 */
291 KConfig *config() const;
292
293 private:
294 static QString *mCurrentGroup;
295
296 KConfig *mConfig; // pointer to KConfig object
297
298 QPtrList<KPrefsItem> mItems;
299};
300
301#endif