From 40ac88770c32ae78e194096e758ef3818d2fb434 Mon Sep 17 00:00:00 2001 From: ulf69 Date: Fri, 06 Aug 2004 20:27:28 +0000 Subject: moved files from libkdepim to microkde/kdecore --- 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 @@ +/* + This file is part of KOrganizer. + Copyright (c) 2000,2001 Cornelius Schumacher + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +// $Id$ + +#include + +#include +#include +#include +#include + +#include "kprefs.h" + +class KPrefsItemBool : public KPrefsItem { + public: + KPrefsItemBool(const QString &group,const QString &name,bool *,bool defaultValue=true); + virtual ~KPrefsItemBool() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + bool *mReference; + bool mDefault; +}; + +class KPrefsItemInt : public KPrefsItem { + public: + KPrefsItemInt(const QString &group,const QString &name,int *,int defaultValue=0); + virtual ~KPrefsItemInt() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + int *mReference; + int mDefault; +}; + + +class KPrefsItemColor : public KPrefsItem { + public: + KPrefsItemColor(const QString &group,const QString &name,QColor *, + const QColor &defaultValue=QColor(128,128,128)); + virtual ~KPrefsItemColor() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + QColor *mReference; + QColor mDefault; +}; + + +class KPrefsItemFont : public KPrefsItem { + public: + KPrefsItemFont(const QString &group,const QString &name,QFont *, + const QFont &defaultValue=QFont("helvetica",12)); + virtual ~KPrefsItemFont() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + QFont *mReference; + QFont mDefault; +}; + + +class KPrefsItemString : public KPrefsItem { + public: + KPrefsItemString(const QString &group,const QString &name,QString *, + const QString &defaultValue="", bool isPassword=false); + virtual ~KPrefsItemString() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + QString *mReference; + QString mDefault; + bool mPassword; +}; + + +class KPrefsItemStringList : public KPrefsItem { + public: + KPrefsItemStringList(const QString &group,const QString &name,QStringList *, + const QStringList &defaultValue=QStringList()); + virtual ~KPrefsItemStringList() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + QStringList *mReference; + QStringList mDefault; +}; + + +class KPrefsItemIntList : public KPrefsItem { + public: + KPrefsItemIntList(const QString &group,const QString &name,QValueList *, + const QValueList &defaultValue=QValueList()); + virtual ~KPrefsItemIntList() {} + + void setDefault(); + void readConfig(KConfig *); + void writeConfig(KConfig *); + + private: + QValueList *mReference; + QValueList mDefault; +}; + + +KPrefsItemBool::KPrefsItemBool(const QString &group,const QString &name, + bool *reference,bool defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemBool::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemBool::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + + +void KPrefsItemBool::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readBoolEntry(mName,mDefault); +} + + +KPrefsItemInt::KPrefsItemInt(const QString &group,const QString &name, + int *reference,int defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemInt::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemInt::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + +void KPrefsItemInt::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readNumEntry(mName,mDefault); +} + + +KPrefsItemColor::KPrefsItemColor(const QString &group,const QString &name, + QColor *reference,const QColor &defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemColor::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemColor::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + +void KPrefsItemColor::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readColorEntry(mName,&mDefault); + +} + + +KPrefsItemFont::KPrefsItemFont(const QString &group,const QString &name, + QFont *reference,const QFont &defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemFont::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemFont::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + +void KPrefsItemFont::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readFontEntry(mName,&mDefault); +} + + +QString endecryptStr( const QString &aStr ) +{ + QString result; + uint i; + for ( i = 0; i < aStr.length(); i++) + result += (aStr.at(i).unicode() < 0x20) ? + aStr.at(i) : + QChar(0x1001F - aStr.at(i).unicode()); + return result; +} + + +KPrefsItemString::KPrefsItemString(const QString &group,const QString &name, + QString *reference,const QString &defaultValue, + bool isPassword) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; + mPassword = isPassword; +} + +void KPrefsItemString::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemString::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + if ( mPassword ) + config->writeEntry(mName, endecryptStr( *mReference ) ); + else + config->writeEntry(mName,*mReference); +} + +void KPrefsItemString::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + + QString value; + if ( mPassword ) { + value = config->readEntry( mName, endecryptStr( mDefault ) ); + *mReference = endecryptStr( value ); + } else { + *mReference = config->readEntry( mName, mDefault ); + } +} + + +KPrefsItemStringList::KPrefsItemStringList(const QString &group,const QString &name, + QStringList *reference,const QStringList &defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemStringList::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemStringList::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + +void KPrefsItemStringList::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readListEntry(mName); +} + + +KPrefsItemIntList::KPrefsItemIntList(const QString &group,const QString &name, + QValueList *reference,const QValueList &defaultValue) : + KPrefsItem(group,name) +{ + mReference = reference; + mDefault = defaultValue; +} + +void KPrefsItemIntList::setDefault() +{ + *mReference = mDefault; +} + +void KPrefsItemIntList::writeConfig(KConfig *config) +{ + config->setGroup(mGroup); + config->writeEntry(mName,*mReference); +} + +void KPrefsItemIntList::readConfig(KConfig *config) +{ + config->setGroup(mGroup); + *mReference = config->readIntListEntry(mName); +} + + +QString *KPrefs::mCurrentGroup = 0; + +KPrefs::KPrefs(const QString &configname) +{ + if (!configname.isEmpty()) { + //qDebug("KPrefs::KPrefs %s",configname.latin1() ); + mConfig = new KConfig(locateLocal("config",configname)); + } else { + mConfig = KGlobal::config(); + } + + mItems.setAutoDelete(true); + + // Set default group + if (mCurrentGroup == 0) mCurrentGroup = new QString("No Group"); +} + +KPrefs::~KPrefs() +{ + if (mConfig != KGlobal::config()) { + delete mConfig; + } +} + +void KPrefs::setCurrentGroup(const QString &group) +{ + if (mCurrentGroup) delete mCurrentGroup; + mCurrentGroup = new QString(group); +} + +KConfig *KPrefs::config() const +{ + return mConfig; +} + +void KPrefs::setDefaults() +{ + KPrefsItem *item; + for(item = mItems.first();item;item = mItems.next()) { + item->setDefault(); + } + + usrSetDefaults(); +} + +void KPrefs::readConfig() +{ + KPrefsItem *item; + for(item = mItems.first();item;item = mItems.next()) { + item->readConfig(mConfig); + } + + usrReadConfig(); +} + +void KPrefs::writeConfig() +{ + KPrefsItem *item; + for(item = mItems.first();item;item = mItems.next()) { + item->writeConfig(mConfig); + } + + usrWriteConfig(); + + mConfig->sync(); +} + + +void KPrefs::addItem(KPrefsItem *item) +{ + mItems.append(item); +} + +void KPrefs::addItemBool(const QString &key,bool *reference,bool defaultValue) +{ + addItem(new KPrefsItemBool(*mCurrentGroup,key,reference,defaultValue)); +} + +void KPrefs::addItemInt(const QString &key,int *reference,int defaultValue) +{ + addItem(new KPrefsItemInt(*mCurrentGroup,key,reference,defaultValue)); +} + +void KPrefs::addItemColor(const QString &key,QColor *reference,const QColor &defaultValue) +{ + addItem(new KPrefsItemColor(*mCurrentGroup,key,reference,defaultValue)); +} + +void KPrefs::addItemFont(const QString &key,QFont *reference,const QFont &defaultValue) +{ + addItem(new KPrefsItemFont(*mCurrentGroup,key,reference,defaultValue)); +} + +void KPrefs::addItemString(const QString &key,QString *reference,const QString &defaultValue) +{ + addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,false)); +} + +void KPrefs::addItemPassword(const QString &key,QString *reference,const QString &defaultValue) +{ + addItem(new KPrefsItemString(*mCurrentGroup,key,reference,defaultValue,true)); +} + +void KPrefs::addItemStringList(const QString &key,QStringList *reference, + const QStringList &defaultValue) +{ + addItem(new KPrefsItemStringList(*mCurrentGroup,key,reference,defaultValue)); +} + +void KPrefs::addItemIntList(const QString &key,QValueList *reference, + const QValueList &defaultValue) +{ + addItem(new KPrefsItemIntList(*mCurrentGroup,key,reference,defaultValue)); +} 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 @@ +/* + This file is part of KOrganizer. + Copyright (c) 2001 Cornelius Schumacher + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +#ifndef _KPREFS_H +#define _KPREFS_H +// $Id$ + +#include +#include +#include +#include + +class KConfig; + +/** + @short Class for storing a preferences setting + @author Cornelius Schumacher + @see KPref + + This class represents one preferences setting as used by @ref KPrefs. + Subclasses of KPrefsItem implement storage functions for a certain type of + setting. Normally you don't have to use this class directly. Use the special + addItem() functions of KPrefs instead. If you subclass this class you will + have to register instances with the function KPrefs::addItem(). +*/ +class KPrefsItem { + public: + /** + Constructor. + + @param group Config file group. + @param name Config file key. + */ + KPrefsItem(const QString &group,const QString &name) : + mGroup(group),mName(name) {} + /** + Destructor. + */ + virtual ~KPrefsItem() {} + + /** + This function is called by @ref KPrefs to set this setting to its default + value. + */ + virtual void setDefault() = 0; + /** + This function is called by @ref KPrefs to read the value for this setting + from a config file. + value. + */ + virtual void readConfig(KConfig *) = 0; + /** + This function is called by @ref KPrefs to write the value of this setting + to a config file. + */ + virtual void writeConfig(KConfig *) = 0; + + protected: + QString mGroup; + QString mName; +}; + +/** + @short Class for handling preferences settings for an application. + @author Cornelius Schumacher + @see KPrefsItem + + This class provides an interface to preferences settings. Preferences items + can be registered by the addItem() function corresponding to the data type of + the seetting. KPrefs then handles reading and writing of config files and + setting of default values. + + Normally you will subclass KPrefs, add data members for the preferences + settings and register the members in the constructor of the subclass. + + Example: +
+  class MyPrefs : public KPrefs {
+    public:
+      MyPrefs()
+      {
+        setCurrentGroup("MyGroup");
+        addItemBool("MySetting1",&mMyBool,false);
+        addItemColor("MySetting2",&mMyColor,QColor(1,2,3));
+        
+        setCurrentGroup("MyOtherGroup");
+        addItemFont("MySetting3",&mMyFont,QFont("helvetica",12));
+      }
+      
+      bool mMyBool;
+      QColor mMyColor;
+      QFont mMyFont;
+  }
+  
+ + It might be convenient in many cases to make this subclass of KPrefs a + singleton for global access from all over the application without passing + references to the KPrefs object around. + + You can set all values to default values by calling @ref setDefaults(), write + the data to the configuration file by calling @ref writeConfig() and read the + data from the configuration file by calling @ref readConfig(). + + If you have items, which are not covered by the existing addItem() functions + you can add customized code for reading, writing and default setting by + implementing the functions @ref usrSetDefaults(), @ref usrReadConfig() and + @ref usrWriteConfig(). + + Internally preferences settings are stored in instances of subclasses of + @ref KPrefsItem. You can also add KPrefsItem subclasses for your own types + and call the generic @ref addItem() to register them. +*/ + +class KPrefs { + public: + /** + Constructor. + + @param configname name of config file. If no name is given, the default + config file as returned by kapp()->config() is used. + */ + KPrefs(const QString &configname=QString::null); + /** + Destructor + */ + virtual ~KPrefs(); + + /** + Set preferences to default values. All registered items are set to their + default values. + */ + void setDefaults(); + + /** + Read preferences from config file. All registered items are set to the + values read from disk. + */ + void readConfig(); + + /** + Write preferences to config file. The values of all registered items are + written to disk. + */ + void writeConfig(); + + /** + Set the config file group for subsequent addItem() calls. It is valid + until setCurrentGroup() is called with a new argument. Call this before + you add any items. The default value is "No Group". + */ + static void setCurrentGroup(const QString &group); + + /** + Register a custom @ref KPrefsItem. + */ + void addItem(KPrefsItem *); + + /** + Register an item of type bool. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemBool(const QString &key,bool *reference, + bool defaultValue=false); + /** + Register an item of type int. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemInt(const QString &key,int *reference, + int defaultValue=0); + /** + Register an item of type QColor. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemColor(const QString &key,QColor *reference, + const QColor &defaultValue=QColor(128,128,128)); + /** + Register an item of type QFont. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemFont(const QString &key,QFont *reference, + const QFont &defaultValue=QFont("helvetica",12)); + /** + Register an item of type QString. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemString(const QString &key,QString *reference, + const QString &defaultValue=""); + /** + Register a password item of type QString. The string value is written + encrypted to the config file. Note that the current encryption scheme + is very weak. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemPassword(const QString &key,QString *reference, + const QString &defaultValue=""); + /** + Register an item of type QStringList. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemStringList(const QString &key,QStringList *reference, + const QStringList &defaultValue=QStringList()); + + /** + Register an item of type QValueList. + + @param key Key used in config file. + @param reference Pointer to the variable, which is set by readConfig() + and setDefaults() calls and read by writeConfig() calls. + @param defaultValue Default value, which is used by setDefaults() and + when the config file does not yet contain the key of + this item. + */ + void addItemIntList(const QString &key,QValueList *reference, + const QValueList &defaultValue=QValueList()); + + protected: + /** + Implemented by subclasses that use special defaults. + */ + virtual void usrSetDefaults() {}; + /** + Implemented by subclasses that read special config values. + */ + virtual void usrReadConfig() {}; + /** + Implemented by subclasses that write special config values. + */ + virtual void usrWriteConfig() {}; + + /** + Return the @ref KConfig object used for reading and writing the settings. + */ + KConfig *config() const; + + private: + static QString *mCurrentGroup; + + KConfig *mConfig; // pointer to KConfig object + + QPtrList mItems; +}; + +#endif -- cgit v0.9.0.2