• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KDECore

kconfigskeleton.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of KDE.
00003  * 
00004  * Copyright (c) 2001,2002,2003 Cornelius Schumacher <schumacher@kde.org>
00005  * Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Library General Public License
00018  * along with this library; see the file COPYING.LIB.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef _KCONFIGSKELETON_H
00024 #define _KCONFIGSKELETON_H
00025 
00026 #include <qcolor.h>
00027 #include <qdatetime.h>
00028 #include <qfont.h>
00029 #include <qpoint.h>
00030 #include <qptrlist.h>
00031 #include <qdict.h>
00032 #include <qrect.h>
00033 #include <qsize.h>
00034 #include <qstringlist.h>
00035 #include <qvariant.h>
00036 #include <kconfig.h>
00037 #include <kglobalsettings.h>
00038 
00050   class KDECORE_EXPORT KConfigSkeletonItem
00051   {
00052   public:
00053     typedef QValueList < KConfigSkeletonItem * >List;
00054     typedef QDict < KConfigSkeletonItem > Dict;
00055     typedef QDictIterator < KConfigSkeletonItem > DictIterator;
00056 
00063     KConfigSkeletonItem(const QString & group, const QString & key)
00064       :mGroup(group),mKey(key), mIsImmutable(true)
00065     {
00066     }
00067 
00071     virtual ~KConfigSkeletonItem()
00072     {
00073     }
00074 
00078     void setGroup( const QString &group )
00079     {
00080       mGroup = group;
00081     }
00082 
00086     QString group() const
00087     {
00088       return mGroup;
00089     }
00090 
00094     void setKey( const QString &key )
00095     {
00096       mKey = key;
00097     }
00098     
00102     QString key() const
00103     {
00104       return mKey;
00105     }
00106 
00110     void setName(const QString &name)
00111     {
00112       mName = name;
00113     }
00114 
00118     QString name() const
00119     {
00120       return mName;
00121     }
00122 
00126     void setLabel( const QString &l )
00127     {
00128       mLabel = l;
00129     }
00130     
00134     QString label() const
00135     {
00136       return mLabel;
00137     }
00138 
00142     void setWhatsThis( const QString &w )
00143     {
00144       mWhatsThis = w;
00145     }
00146     
00150     QString whatsThis() const
00151     {
00152       return mWhatsThis;
00153     }
00154 
00160     virtual void readConfig(KConfig *) = 0;
00161 
00166     virtual void writeConfig(KConfig *) = 0;
00167 
00171     virtual void readDefault(KConfig *) = 0;
00172 
00176     virtual void setProperty(const QVariant &p) = 0;
00177     
00181     virtual QVariant property() const = 0;
00182 
00186     virtual QVariant minValue() const { return QVariant(); }
00187 
00191     virtual QVariant maxValue() const { return QVariant(); }
00192 
00196     virtual void setDefault() = 0;
00197 
00202     virtual void swapDefault() = 0;
00203 
00207     bool isImmutable() const
00208     {
00209       return mIsImmutable;
00210     }
00211 
00212   protected:
00217     void readImmutability(KConfig *config);
00218 
00219     QString mGroup;
00220     QString mKey;
00221     QString mName;
00222 
00223   private:
00224     bool mIsImmutable;
00225 
00226     QString mLabel;
00227     QString mWhatsThis;
00228   };
00229 
00230 
00231 template < typename T > class KConfigSkeletonGenericItem:public KConfigSkeletonItem
00232   {
00233   public:
00234     KConfigSkeletonGenericItem(const QString & group, const QString & key, T & reference,
00235                 T defaultValue)
00236       : KConfigSkeletonItem(group, key), mReference(reference),
00237         mDefault(defaultValue), mLoadedValue(defaultValue)
00238     {
00239     }
00240 
00244     void setValue(const T & v)
00245     {
00246       mReference = v;
00247     }
00248 
00252     T & value()
00253     {
00254       return mReference;
00255     }
00256 
00260     const T & value() const
00261     {
00262       return mReference;
00263     }
00264 
00268     virtual void setDefaultValue( const T &v )
00269     {
00270       mDefault = v;
00271     }
00272 
00273     virtual void setDefault()
00274     {
00275       mReference = mDefault;
00276     }
00277 
00278     virtual void writeConfig(KConfig * config)
00279     {
00280       if ( mReference != mLoadedValue ) // Is this needed?
00281       {
00282         config->setGroup(mGroup);
00283         if ((mDefault == mReference) && !config->hasDefault( mKey))
00284           config->revertToDefault( mKey );
00285         else
00286           config->writeEntry(mKey, mReference);
00287       }
00288     }
00289 
00290     void readDefault(KConfig * config)
00291     {
00292       config->setReadDefaults(true);
00293       readConfig(config);
00294       config->setReadDefaults(false);
00295       mDefault = mReference;
00296     }
00297 
00298     void swapDefault()
00299     {
00300       T tmp = mReference;
00301       mReference = mDefault;
00302       mDefault = tmp;
00303     }
00304 
00305   protected:
00306     T & mReference;
00307     T mDefault;
00308     T mLoadedValue;
00309   };
00310 
00365 class KDECORE_EXPORT KConfigSkeleton
00366 {
00367 public:
00368 
00372   class KDECORE_EXPORT ItemString:public KConfigSkeletonGenericItem < QString >
00373   {
00374   public:
00375     enum Type { Normal, Password, Path };
00376 
00377     ItemString(const QString & group, const QString & key,
00378                QString & reference,
00379                const QString & defaultValue = QString::fromLatin1(""), // NOT QString::null !!
00380                Type type = Normal);
00381 
00382     void writeConfig(KConfig * config);
00383     void readConfig(KConfig * config);
00384     void setProperty(const QVariant & p);
00385     QVariant property() const;
00386 
00387   private:
00388     Type mType;
00389   };
00390 
00394   class KDECORE_EXPORT ItemPassword:public ItemString
00395   {
00396   public:
00397     ItemPassword(const QString & group, const QString & key,
00398                QString & reference,
00399                const QString & defaultValue = QString::fromLatin1("")); // NOT QString::null !!
00400   };
00401 
00405   class KDECORE_EXPORT ItemPath:public ItemString
00406   {
00407   public:
00408     ItemPath(const QString & group, const QString & key,
00409              QString & reference,
00410              const QString & defaultValue = QString::null);
00411   };
00412 
00413 
00417   class KDECORE_EXPORT ItemProperty:public KConfigSkeletonGenericItem < QVariant >
00418   {
00419   public:
00420     ItemProperty(const QString & group, const QString & key,
00421                  QVariant & reference, QVariant defaultValue = 0);
00422 
00423     void readConfig(KConfig * config);
00424     void setProperty(const QVariant & p);
00425     QVariant property() const;
00426   };
00427 
00428 
00432   class KDECORE_EXPORT ItemBool:public KConfigSkeletonGenericItem < bool >
00433   {
00434   public:
00435     ItemBool(const QString & group, const QString & key, bool & reference,
00436              bool defaultValue = true);
00437 
00438     void readConfig(KConfig * config);
00439     void setProperty(const QVariant & p);
00440     QVariant property() const;
00441   };
00442 
00443 
00447   class KDECORE_EXPORT ItemInt:public KConfigSkeletonGenericItem < int >
00448   {
00449   public:
00450     ItemInt(const QString & group, const QString & key, int &reference,
00451             int defaultValue = 0);
00452 
00453     void readConfig(KConfig * config);
00454     void setProperty(const QVariant & p);
00455     QVariant property() const;
00456     QVariant minValue() const;
00457     QVariant maxValue() const;
00458 
00459     void setMinValue(int);
00460     void setMaxValue(int);
00461     
00462   private:  
00463     bool mHasMin : 1;
00464     bool mHasMax : 1;
00465     int mMin;
00466     int mMax;
00467   };
00468 
00472   class KDECORE_EXPORT ItemInt64:public KConfigSkeletonGenericItem < Q_INT64 >
00473   {
00474   public:
00475     ItemInt64(const QString & group, const QString & key, Q_INT64 &reference,
00476             Q_INT64 defaultValue = 0);
00477 
00478     void readConfig(KConfig * config);
00479     void setProperty(const QVariant & p);
00480     QVariant property() const;
00481 
00482     QVariant minValue() const;
00483     QVariant maxValue() const;
00484 
00485     void setMinValue(Q_INT64);
00486     void setMaxValue(Q_INT64);
00487     
00488   private:  
00489     bool mHasMin : 1;
00490     bool mHasMax : 1;
00491     Q_INT64 mMin;
00492     Q_INT64 mMax;
00493   };
00494 
00498   class KDECORE_EXPORT ItemEnum:public ItemInt
00499   {
00500   public:
00501     struct Choice
00502     {
00503       QString name;
00504       QString label;
00505       QString whatsThis;
00506     };
00507 
00508     ItemEnum(const QString & group, const QString & key, int &reference,
00509              const QValueList<Choice> &choices, int defaultValue = 0);
00510 
00511     QValueList<Choice> choices() const;
00512 
00513     void readConfig(KConfig * config);
00514     void writeConfig(KConfig * config);
00515 
00516   private:
00517       QValueList<Choice> mChoices;
00518   };
00519 
00520 
00524   class KDECORE_EXPORT ItemUInt:public KConfigSkeletonGenericItem < unsigned int >
00525   {
00526   public:
00527     ItemUInt(const QString & group, const QString & key,
00528              unsigned int &reference, unsigned int defaultValue = 0);
00529 
00530     void readConfig(KConfig * config);
00531     void setProperty(const QVariant & p);
00532     QVariant property() const;
00533     QVariant minValue() const;
00534     QVariant maxValue() const;
00535 
00536     void setMinValue(unsigned int);
00537     void setMaxValue(unsigned int);
00538     
00539   private:  
00540     bool mHasMin : 1;
00541     bool mHasMax : 1;
00542     unsigned int mMin;
00543     unsigned int mMax;
00544   };
00545 
00546 
00550   class KDECORE_EXPORT ItemLong:public KConfigSkeletonGenericItem < long >
00551   {
00552   public:
00553     ItemLong(const QString & group, const QString & key, long &reference,
00554              long defaultValue = 0);
00555 
00556     void readConfig(KConfig * config);
00557     void setProperty(const QVariant & p);
00558     QVariant property() const;
00559     QVariant minValue() const;
00560     QVariant maxValue() const;
00561 
00562     void setMinValue(long);
00563     void setMaxValue(long);
00564     
00565   private:  
00566     bool mHasMin : 1;
00567     bool mHasMax : 1;
00568     long mMin;
00569     long mMax;
00570   };
00571 
00572 
00576   class KDECORE_EXPORT ItemULong:public KConfigSkeletonGenericItem < unsigned long >
00577   {
00578   public:
00579     ItemULong(const QString & group, const QString & key,
00580               unsigned long &reference, unsigned long defaultValue = 0);
00581 
00582     void readConfig(KConfig * config);
00583     void setProperty(const QVariant & p);
00584     QVariant property() const;
00585     QVariant minValue() const;
00586     QVariant maxValue() const;
00587 
00588     void setMinValue(unsigned long);
00589     void setMaxValue(unsigned long);
00590     
00591   private:  
00592     bool mHasMin : 1;
00593     bool mHasMax : 1;
00594     unsigned long mMin;
00595     unsigned long mMax;
00596   };
00597 
00601   class KDECORE_EXPORT ItemUInt64:public KConfigSkeletonGenericItem < Q_UINT64 >
00602   {
00603   public:
00604     ItemUInt64(const QString & group, const QString & key, Q_UINT64 &reference,
00605             Q_UINT64 defaultValue = 0);
00606 
00607     void readConfig(KConfig * config);
00608     void setProperty(const QVariant & p);
00609     QVariant property() const;
00610 
00611     QVariant minValue() const;
00612     QVariant maxValue() const;
00613 
00614     void setMinValue(Q_UINT64);
00615     void setMaxValue(Q_UINT64);
00616     
00617   private:  
00618     bool mHasMin : 1;
00619     bool mHasMax : 1;
00620     Q_UINT64 mMin;
00621     Q_UINT64 mMax;
00622   };
00623 
00627   class KDECORE_EXPORT ItemDouble:public KConfigSkeletonGenericItem < double >
00628   {
00629   public:
00630     ItemDouble(const QString & group, const QString & key,
00631                double &reference, double defaultValue = 0);
00632 
00633     void readConfig(KConfig * config);
00634     void setProperty(const QVariant & p);
00635     QVariant property() const;
00636     QVariant minValue() const;
00637     QVariant maxValue() const;
00638 
00639     void setMinValue(double);
00640     void setMaxValue(double);
00641     
00642   private:  
00643     bool mHasMin : 1;
00644     bool mHasMax : 1;
00645     double mMin;
00646     double mMax;
00647   };
00648 
00649 
00653   class KDECORE_EXPORT ItemColor:public KConfigSkeletonGenericItem < QColor >
00654   {
00655   public:
00656     ItemColor(const QString & group, const QString & key,
00657               QColor & reference,
00658               const QColor & defaultValue = QColor(128, 128, 128));
00659 
00660     void readConfig(KConfig * config);
00661     void setProperty(const QVariant & p);
00662     QVariant property() const;
00663   };
00664 
00665 
00669   class KDECORE_EXPORT ItemFont:public KConfigSkeletonGenericItem < QFont >
00670   {
00671   public:
00672     ItemFont(const QString & group, const QString & key, QFont & reference,
00673              const QFont & defaultValue = KGlobalSettings::generalFont());
00674 
00675     void readConfig(KConfig * config);
00676     void setProperty(const QVariant & p);
00677     QVariant property() const;
00678   };
00679 
00680 
00684   class KDECORE_EXPORT ItemRect:public KConfigSkeletonGenericItem < QRect >
00685   {
00686   public:
00687     ItemRect(const QString & group, const QString & key, QRect & reference,
00688              const QRect & defaultValue = QRect());
00689 
00690     void readConfig(KConfig * config);
00691     void setProperty(const QVariant & p);
00692     QVariant property() const;
00693   };
00694 
00695 
00699   class KDECORE_EXPORT ItemPoint:public KConfigSkeletonGenericItem < QPoint >
00700   {
00701   public:
00702     ItemPoint(const QString & group, const QString & key, QPoint & reference,
00703               const QPoint & defaultValue = QPoint());
00704 
00705     void readConfig(KConfig * config);
00706     void setProperty(const QVariant & p);
00707     QVariant property() const;
00708   };
00709 
00710 
00714   class KDECORE_EXPORT ItemSize:public KConfigSkeletonGenericItem < QSize >
00715   {
00716   public:
00717     ItemSize(const QString & group, const QString & key, QSize & reference,
00718              const QSize & defaultValue = QSize());
00719 
00720     void readConfig(KConfig * config);
00721     void setProperty(const QVariant & p);
00722     QVariant property() const;
00723   };
00724 
00725 
00729   class KDECORE_EXPORT ItemDateTime:public KConfigSkeletonGenericItem < QDateTime >
00730   {
00731   public:
00732     ItemDateTime(const QString & group, const QString & key,
00733                  QDateTime & reference,
00734                  const QDateTime & defaultValue = QDateTime());
00735 
00736     void readConfig(KConfig * config);
00737     void setProperty(const QVariant & p);
00738     QVariant property() const;
00739   };
00740 
00741 
00745   class KDECORE_EXPORT ItemStringList:public KConfigSkeletonGenericItem < QStringList >
00746   {
00747   public:
00748     ItemStringList(const QString & group, const QString & key,
00749                    QStringList & reference,
00750                    const QStringList & defaultValue = QStringList());
00751 
00752     void readConfig(KConfig * config);
00753     void setProperty(const QVariant & p);
00754     QVariant property() const;
00755   };
00756 
00757 
00761   class KDECORE_EXPORT ItemPathList:public ItemStringList
00762   {
00763   public:
00764     ItemPathList(const QString & group, const QString & key,
00765                    QStringList & reference,
00766                    const QStringList & defaultValue = QStringList());
00767 
00768     void readConfig(KConfig * config);
00769     void writeConfig(KConfig * config);
00770   };
00771 
00772 
00776   class KDECORE_EXPORT ItemIntList:public KConfigSkeletonGenericItem < QValueList < int > >
00777   {
00778   public:
00779     ItemIntList(const QString & group, const QString & key,
00780                 QValueList < int >&reference,
00781                 const QValueList < int >&defaultValue = QValueList < int >());
00782 
00783     void readConfig(KConfig * config);
00784     void setProperty(const QVariant & p);
00785     QVariant property() const;
00786   };
00787 
00788 
00789 public:
00796   KConfigSkeleton(const QString & configname = QString::null);
00797 
00803   KConfigSkeleton(KSharedConfig::Ptr config);
00804 
00808     virtual ~ KConfigSkeleton();
00809 
00813   void setDefaults();
00814 
00819   void readConfig();
00820 
00825   void writeConfig();
00826 
00832   void setCurrentGroup(const QString & group);
00833 
00837   QString currentGroup() // ### KDE 4.0: make const
00838   {
00839     return mCurrentGroup;
00840   }
00841 
00848   void addItem(KConfigSkeletonItem *, const QString & name = QString::null );
00849 
00861   ItemString *addItemString(const QString & name, QString & reference,
00862                             const QString & defaultValue = QString::fromLatin1(""), // NOT QString::null !!
00863                             const QString & key = QString::null);
00864 
00878   ItemPassword *addItemPassword(const QString & name, QString & reference,
00879                               const QString & defaultValue = QString::fromLatin1(""),
00880                               const QString & key = QString::null);
00881 
00895   ItemPath *addItemPath(const QString & name, QString & reference,
00896                           const QString & defaultValue = QString::fromLatin1(""),
00897                           const QString & key = QString::null);
00898 
00912   ItemProperty *addItemProperty(const QString & name, QVariant & reference,
00913                                 const QVariant & defaultValue = QVariant(),
00914                                 const QString & key = QString::null);
00926   ItemBool *addItemBool(const QString & name, bool & reference,
00927                         bool defaultValue = false,
00928                         const QString & key = QString::null);
00929 
00941   ItemInt *addItemInt(const QString & name, int &reference, int defaultValue = 0,
00942                       const QString & key = QString::null);
00943 
00955   ItemUInt *addItemUInt(const QString & name, unsigned int &reference,
00956                         unsigned int defaultValue = 0,
00957                         const QString & key = QString::null);
00958 
00970   ItemLong *addItemLong(const QString & name, long &reference,
00971                         long defaultValue = 0,
00972                         const QString & key = QString::null);
00973 
00985   ItemULong *addItemULong(const QString & name, unsigned long &reference,
00986                           unsigned long defaultValue = 0,
00987                           const QString & key = QString::null);
00988 
01000   ItemInt64 *addItemInt64(const QString & name, Q_INT64 &reference,
01001                           Q_INT64 defaultValue = 0,
01002                           const QString & key = QString::null);
01003 
01015   ItemUInt64 *addItemUInt64(const QString & name, Q_UINT64 &reference,
01016                             Q_UINT64 defaultValue = 0,
01017                             const QString & key = QString::null);
01018 
01030   ItemDouble *addItemDouble(const QString & name, double &reference,
01031                             double defaultValue = 0.0,
01032                             const QString & key = QString::null);
01033 
01045   ItemColor *addItemColor(const QString & name, QColor & reference,
01046                           const QColor & defaultValue = QColor(128, 128, 128),
01047                           const QString & key = QString::null);
01048 
01060   ItemFont *addItemFont(const QString & name, QFont & reference,
01061                         const QFont & defaultValue =
01062                         KGlobalSettings::generalFont(),
01063                         const QString & key = QString::null);
01064 
01076   ItemRect *addItemRect(const QString & name, QRect & reference,
01077                         const QRect & defaultValue = QRect(),
01078                         const QString & key = QString::null);
01079 
01091   ItemPoint *addItemPoint(const QString & name, QPoint & reference,
01092                           const QPoint & defaultValue = QPoint(),
01093                           const QString & key = QString::null);
01094 
01106   ItemSize *addItemSize(const QString & name, QSize & reference,
01107                         const QSize & defaultValue = QSize(),
01108                         const QString & key = QString::null);
01109 
01121   ItemDateTime *addItemDateTime(const QString & name, QDateTime & reference,
01122                                 const QDateTime & defaultValue = QDateTime(),
01123                                 const QString & key = QString::null);
01124 
01136   ItemStringList *addItemStringList(const QString & name, QStringList & reference,
01137                                     const QStringList & defaultValue = QStringList(),
01138                                     const QString & key = QString::null);
01139 
01151   ItemIntList *addItemIntList(const QString & name, QValueList < int >&reference,
01152                               const QValueList < int >&defaultValue =
01153                               QValueList < int >(),
01154                               const QString & key = QString::null);
01155 
01159   KConfig *config() const;
01160 
01164   KConfigSkeletonItem::List items() const
01165   {
01166     return mItems;
01167   }
01168 
01172   bool isImmutable(const QString & name);
01173 
01177   KConfigSkeletonItem * findItem(const QString & name);
01178 
01185   bool useDefaults(bool b);
01186 
01187 protected:
01193   virtual void usrUseDefaults(bool)
01194   {
01195   }
01196 
01197   virtual void usrSetDefaults()
01198   {
01199   }
01200 
01204   virtual void usrReadConfig()
01205   {
01206   }
01207 
01211   virtual void usrWriteConfig()
01212   {
01213   }
01214 
01215 private:
01216   QString mCurrentGroup;
01217 
01218   KSharedConfig::Ptr mConfig; // pointer to KConfig object
01219 
01220   KConfigSkeletonItem::List mItems;
01221   KConfigSkeletonItem::Dict mItemDict;
01222   
01223   bool mUseDefaults;
01224 
01225   class Private;
01226   Private *d;
01227 
01228 };
01229 
01230 #endif

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal