kconfigskeleton.h
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 <tqcolor.h> 00027 #include <tqdatetime.h> 00028 #include <tqfont.h> 00029 #include <tqpoint.h> 00030 #include <tqptrlist.h> 00031 #include <tqdict.h> 00032 #include <tqrect.h> 00033 #include <tqsize.h> 00034 #include <tqstringlist.h> 00035 #include <tqvariant.h> 00036 #include <kconfig.h> 00037 #include <kglobalsettings.h> 00038 00050 class KDECORE_EXPORT KConfigSkeletonItem 00051 { 00052 public: 00053 typedef TQValueList < KConfigSkeletonItem * >List; 00054 typedef TQDict < KConfigSkeletonItem > Dict; 00055 typedef TQDictIterator < KConfigSkeletonItem > DictIterator; 00056 00063 KConfigSkeletonItem(const TQString & group, const TQString & key) 00064 :mGroup(group),mKey(key), mIsImmutable(true) 00065 { 00066 } 00067 00071 virtual ~KConfigSkeletonItem() 00072 { 00073 } 00074 00078 void setGroup( const TQString &group ) 00079 { 00080 mGroup = group; 00081 } 00082 00086 TQString group() const 00087 { 00088 return mGroup; 00089 } 00090 00094 void setKey( const TQString &key ) 00095 { 00096 mKey = key; 00097 } 00098 00102 TQString key() const 00103 { 00104 return mKey; 00105 } 00106 00110 void setName(const TQString &name) 00111 { 00112 mName = name; 00113 } 00114 00118 TQString name() const 00119 { 00120 return mName; 00121 } 00122 00126 void setLabel( const TQString &l ) 00127 { 00128 mLabel = l; 00129 } 00130 00134 TQString label() const 00135 { 00136 return mLabel; 00137 } 00138 00142 void setWhatsThis( const TQString &w ) 00143 { 00144 mWhatsThis = w; 00145 } 00146 00150 TQString 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 TQVariant &p) = 0; 00177 00181 virtual TQVariant property() const = 0; 00182 00186 virtual TQVariant minValue() const { return TQVariant(); } 00187 00191 virtual TQVariant maxValue() const { return TQVariant(); } 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 TQString mGroup; 00220 TQString mKey; 00221 TQString mName; 00222 00223 private: 00224 bool mIsImmutable; 00225 00226 TQString mLabel; 00227 TQString mWhatsThis; 00228 }; 00229 00230 00231 template < typename T > class KConfigSkeletonGenericItem:public KConfigSkeletonItem 00232 { 00233 public: 00234 KConfigSkeletonGenericItem(const TQString & group, const TQString & 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 < TQString > 00373 { 00374 public: 00375 enum Type { Normal, Password, Path }; 00376 00377 ItemString(const TQString & group, const TQString & key, 00378 TQString & reference, 00379 const TQString & defaultValue = TQString::fromLatin1(""), // NOT TQString::null !! 00380 Type type = Normal); 00381 00382 void writeConfig(KConfig * config); 00383 void readConfig(KConfig * config); 00384 void setProperty(const TQVariant & p); 00385 TQVariant property() const; 00386 00387 private: 00388 Type mType; 00389 }; 00390 00394 class KDECORE_EXPORT ItemPassword:public ItemString 00395 { 00396 public: 00397 ItemPassword(const TQString & group, const TQString & key, 00398 TQString & reference, 00399 const TQString & defaultValue = TQString::fromLatin1("")); // NOT TQString::null !! 00400 }; 00401 00405 class KDECORE_EXPORT ItemPath:public ItemString 00406 { 00407 public: 00408 ItemPath(const TQString & group, const TQString & key, 00409 TQString & reference, 00410 const TQString & defaultValue = TQString::null); 00411 }; 00412 00413 00417 class KDECORE_EXPORT ItemProperty:public KConfigSkeletonGenericItem < TQVariant > 00418 { 00419 public: 00420 ItemProperty(const TQString & group, const TQString & key, 00421 TQVariant & reference, TQVariant defaultValue = 0); 00422 00423 void readConfig(KConfig * config); 00424 void setProperty(const TQVariant & p); 00425 TQVariant property() const; 00426 }; 00427 00428 00432 class KDECORE_EXPORT ItemBool:public KConfigSkeletonGenericItem < bool > 00433 { 00434 public: 00435 ItemBool(const TQString & group, const TQString & key, bool & reference, 00436 bool defaultValue = true); 00437 00438 void readConfig(KConfig * config); 00439 void setProperty(const TQVariant & p); 00440 TQVariant property() const; 00441 }; 00442 00443 00447 class KDECORE_EXPORT ItemInt:public KConfigSkeletonGenericItem < int > 00448 { 00449 public: 00450 ItemInt(const TQString & group, const TQString & key, int &reference, 00451 int defaultValue = 0); 00452 00453 void readConfig(KConfig * config); 00454 void setProperty(const TQVariant & p); 00455 TQVariant property() const; 00456 TQVariant minValue() const; 00457 TQVariant 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 < TQ_INT64 > 00473 { 00474 public: 00475 ItemInt64(const TQString & group, const TQString & key, TQ_INT64 &reference, 00476 TQ_INT64 defaultValue = 0); 00477 00478 void readConfig(KConfig * config); 00479 void setProperty(const TQVariant & p); 00480 TQVariant property() const; 00481 00482 TQVariant minValue() const; 00483 TQVariant maxValue() const; 00484 00485 void setMinValue(TQ_INT64); 00486 void setMaxValue(TQ_INT64); 00487 00488 private: 00489 bool mHasMin : 1; 00490 bool mHasMax : 1; 00491 TQ_INT64 mMin; 00492 TQ_INT64 mMax; 00493 }; 00494 00498 class KDECORE_EXPORT ItemEnum:public ItemInt 00499 { 00500 public: 00501 struct Choice 00502 { 00503 TQString name; 00504 TQString label; 00505 TQString whatsThis; 00506 }; 00507 00508 ItemEnum(const TQString & group, const TQString & key, int &reference, 00509 const TQValueList<Choice> &choices, int defaultValue = 0); 00510 00511 TQValueList<Choice> choices() const; 00512 00513 void readConfig(KConfig * config); 00514 void writeConfig(KConfig * config); 00515 00516 private: 00517 TQValueList<Choice> mChoices; 00518 }; 00519 00520 00524 class KDECORE_EXPORT ItemUInt:public KConfigSkeletonGenericItem < unsigned int > 00525 { 00526 public: 00527 ItemUInt(const TQString & group, const TQString & key, 00528 unsigned int &reference, unsigned int defaultValue = 0); 00529 00530 void readConfig(KConfig * config); 00531 void setProperty(const TQVariant & p); 00532 TQVariant property() const; 00533 TQVariant minValue() const; 00534 TQVariant 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 TQString & group, const TQString & key, long &reference, 00554 long defaultValue = 0); 00555 00556 void readConfig(KConfig * config); 00557 void setProperty(const TQVariant & p); 00558 TQVariant property() const; 00559 TQVariant minValue() const; 00560 TQVariant 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 TQString & group, const TQString & key, 00580 unsigned long &reference, unsigned long defaultValue = 0); 00581 00582 void readConfig(KConfig * config); 00583 void setProperty(const TQVariant & p); 00584 TQVariant property() const; 00585 TQVariant minValue() const; 00586 TQVariant 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 < TQ_UINT64 > 00602 { 00603 public: 00604 ItemUInt64(const TQString & group, const TQString & key, TQ_UINT64 &reference, 00605 TQ_UINT64 defaultValue = 0); 00606 00607 void readConfig(KConfig * config); 00608 void setProperty(const TQVariant & p); 00609 TQVariant property() const; 00610 00611 TQVariant minValue() const; 00612 TQVariant maxValue() const; 00613 00614 void setMinValue(TQ_UINT64); 00615 void setMaxValue(TQ_UINT64); 00616 00617 private: 00618 bool mHasMin : 1; 00619 bool mHasMax : 1; 00620 TQ_UINT64 mMin; 00621 TQ_UINT64 mMax; 00622 }; 00623 00627 class KDECORE_EXPORT ItemDouble:public KConfigSkeletonGenericItem < double > 00628 { 00629 public: 00630 ItemDouble(const TQString & group, const TQString & key, 00631 double &reference, double defaultValue = 0); 00632 00633 void readConfig(KConfig * config); 00634 void setProperty(const TQVariant & p); 00635 TQVariant property() const; 00636 TQVariant minValue() const; 00637 TQVariant 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 < TQColor > 00654 { 00655 public: 00656 ItemColor(const TQString & group, const TQString & key, 00657 TQColor & reference, 00658 const TQColor & defaultValue = TQColor(128, 128, 128)); 00659 00660 void readConfig(KConfig * config); 00661 void setProperty(const TQVariant & p); 00662 TQVariant property() const; 00663 }; 00664 00665 00669 class KDECORE_EXPORT ItemFont:public KConfigSkeletonGenericItem < TQFont > 00670 { 00671 public: 00672 ItemFont(const TQString & group, const TQString & key, TQFont & reference, 00673 const TQFont & defaultValue = KGlobalSettings::generalFont()); 00674 00675 void readConfig(KConfig * config); 00676 void setProperty(const TQVariant & p); 00677 TQVariant property() const; 00678 }; 00679 00680 00684 class KDECORE_EXPORT ItemRect:public KConfigSkeletonGenericItem < TQRect > 00685 { 00686 public: 00687 ItemRect(const TQString & group, const TQString & key, TQRect & reference, 00688 const TQRect & defaultValue = TQRect()); 00689 00690 void readConfig(KConfig * config); 00691 void setProperty(const TQVariant & p); 00692 TQVariant property() const; 00693 }; 00694 00695 00699 class KDECORE_EXPORT ItemPoint:public KConfigSkeletonGenericItem < TQPoint > 00700 { 00701 public: 00702 ItemPoint(const TQString & group, const TQString & key, TQPoint & reference, 00703 const TQPoint & defaultValue = TQPoint()); 00704 00705 void readConfig(KConfig * config); 00706 void setProperty(const TQVariant & p); 00707 TQVariant property() const; 00708 }; 00709 00710 00714 class KDECORE_EXPORT ItemSize:public KConfigSkeletonGenericItem < TQSize > 00715 { 00716 public: 00717 ItemSize(const TQString & group, const TQString & key, TQSize & reference, 00718 const TQSize & defaultValue = TQSize()); 00719 00720 void readConfig(KConfig * config); 00721 void setProperty(const TQVariant & p); 00722 TQVariant property() const; 00723 }; 00724 00725 00729 class KDECORE_EXPORT ItemDateTime:public KConfigSkeletonGenericItem < TQDateTime > 00730 { 00731 public: 00732 ItemDateTime(const TQString & group, const TQString & key, 00733 TQDateTime & reference, 00734 const TQDateTime & defaultValue = TQDateTime()); 00735 00736 void readConfig(KConfig * config); 00737 void setProperty(const TQVariant & p); 00738 TQVariant property() const; 00739 }; 00740 00741 00745 class KDECORE_EXPORT ItemStringList:public KConfigSkeletonGenericItem < TQStringList > 00746 { 00747 public: 00748 ItemStringList(const TQString & group, const TQString & key, 00749 TQStringList & reference, 00750 const TQStringList & defaultValue = TQStringList()); 00751 00752 void readConfig(KConfig * config); 00753 void setProperty(const TQVariant & p); 00754 TQVariant property() const; 00755 }; 00756 00757 00761 class KDECORE_EXPORT ItemPathList:public ItemStringList 00762 { 00763 public: 00764 ItemPathList(const TQString & group, const TQString & key, 00765 TQStringList & reference, 00766 const TQStringList & defaultValue = TQStringList()); 00767 00768 void readConfig(KConfig * config); 00769 void writeConfig(KConfig * config); 00770 }; 00771 00772 00776 class KDECORE_EXPORT ItemIntList:public KConfigSkeletonGenericItem < TQValueList < int > > 00777 { 00778 public: 00779 ItemIntList(const TQString & group, const TQString & key, 00780 TQValueList < int >&reference, 00781 const TQValueList < int >&defaultValue = TQValueList < int >()); 00782 00783 void readConfig(KConfig * config); 00784 void setProperty(const TQVariant & p); 00785 TQVariant property() const; 00786 }; 00787 00788 00789 public: 00796 KConfigSkeleton(const TQString & configname = TQString::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 TQString & group); 00833 00837 TQString currentGroup() // ### KDE 4.0: make const 00838 { 00839 return mCurrentGroup; 00840 } 00841 00848 void addItem(KConfigSkeletonItem *, const TQString & name = TQString::null ); 00849 00861 ItemString *addItemString(const TQString & name, TQString & reference, 00862 const TQString & defaultValue = TQString::fromLatin1(""), // NOT TQString::null !! 00863 const TQString & key = TQString::null); 00864 00878 ItemPassword *addItemPassword(const TQString & name, TQString & reference, 00879 const TQString & defaultValue = TQString::fromLatin1(""), 00880 const TQString & key = TQString::null); 00881 00895 ItemPath *addItemPath(const TQString & name, TQString & reference, 00896 const TQString & defaultValue = TQString::fromLatin1(""), 00897 const TQString & key = TQString::null); 00898 00912 ItemProperty *addItemProperty(const TQString & name, TQVariant & reference, 00913 const TQVariant & defaultValue = TQVariant(), 00914 const TQString & key = TQString::null); 00926 ItemBool *addItemBool(const TQString & name, bool & reference, 00927 bool defaultValue = false, 00928 const TQString & key = TQString::null); 00929 00941 ItemInt *addItemInt(const TQString & name, int &reference, int defaultValue = 0, 00942 const TQString & key = TQString::null); 00943 00955 ItemUInt *addItemUInt(const TQString & name, unsigned int &reference, 00956 unsigned int defaultValue = 0, 00957 const TQString & key = TQString::null); 00958 00970 ItemLong *addItemLong(const TQString & name, long &reference, 00971 long defaultValue = 0, 00972 const TQString & key = TQString::null); 00973 00985 ItemULong *addItemULong(const TQString & name, unsigned long &reference, 00986 unsigned long defaultValue = 0, 00987 const TQString & key = TQString::null); 00988 01000 ItemInt64 *addItemInt64(const TQString & name, TQ_INT64 &reference, 01001 TQ_INT64 defaultValue = 0, 01002 const TQString & key = TQString::null); 01003 01015 ItemUInt64 *addItemUInt64(const TQString & name, TQ_UINT64 &reference, 01016 TQ_UINT64 defaultValue = 0, 01017 const TQString & key = TQString::null); 01018 01030 ItemDouble *addItemDouble(const TQString & name, double &reference, 01031 double defaultValue = 0.0, 01032 const TQString & key = TQString::null); 01033 01045 ItemColor *addItemColor(const TQString & name, TQColor & reference, 01046 const TQColor & defaultValue = TQColor(128, 128, 128), 01047 const TQString & key = TQString::null); 01048 01060 ItemFont *addItemFont(const TQString & name, TQFont & reference, 01061 const TQFont & defaultValue = 01062 KGlobalSettings::generalFont(), 01063 const TQString & key = TQString::null); 01064 01076 ItemRect *addItemRect(const TQString & name, TQRect & reference, 01077 const TQRect & defaultValue = TQRect(), 01078 const TQString & key = TQString::null); 01079 01091 ItemPoint *addItemPoint(const TQString & name, TQPoint & reference, 01092 const TQPoint & defaultValue = TQPoint(), 01093 const TQString & key = TQString::null); 01094 01106 ItemSize *addItemSize(const TQString & name, TQSize & reference, 01107 const TQSize & defaultValue = TQSize(), 01108 const TQString & key = TQString::null); 01109 01121 ItemDateTime *addItemDateTime(const TQString & name, TQDateTime & reference, 01122 const TQDateTime & defaultValue = TQDateTime(), 01123 const TQString & key = TQString::null); 01124 01136 ItemStringList *addItemStringList(const TQString & name, TQStringList & reference, 01137 const TQStringList & defaultValue = TQStringList(), 01138 const TQString & key = TQString::null); 01139 01151 ItemIntList *addItemIntList(const TQString & name, TQValueList < int >&reference, 01152 const TQValueList < int >&defaultValue = 01153 TQValueList < int >(), 01154 const TQString & key = TQString::null); 01155 01159 KConfig *config() const; 01160 01164 KConfigSkeletonItem::List items() const 01165 { 01166 return mItems; 01167 } 01168 01172 bool isImmutable(const TQString & name); 01173 01177 KConfigSkeletonItem * findItem(const TQString & 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 TQString 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