kdecore Library API Documentation

kconfigbase.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003    This file is part of the KDE libraries
00004    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00005    Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@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., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #include <qfile.h>
00027 #include <qdir.h>
00028 #include <qtextstream.h>
00029 
00030 #include <kapplication.h>
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 #include <kcharsets.h>
00034 
00035 #include "kconfigbase.h"
00036 #include "kconfigbackend.h"
00037 #include "kdebug.h"
00038 #include "kstandarddirs.h"
00039 #include "kstringhandler.h"
00040 
00041 class KConfigBase::KConfigBasePrivate
00042 {
00043 public:
00044      KConfigBasePrivate() : readDefaults(false) { };
00045 
00046 public:
00047      bool readDefaults;
00048 };
00049 
00050 KConfigBase::KConfigBase()
00051   : backEnd(0L), bDirty(false), bLocaleInitialized(false),
00052     bReadOnly(false), bExpand(false), d(0)
00053 {
00054     setGroup(QString::null);
00055 }
00056 
00057 KConfigBase::~KConfigBase()
00058 {
00059     delete d;
00060 }
00061 
00062 void KConfigBase::setLocale()
00063 {
00064   bLocaleInitialized = true;
00065 
00066   if (KGlobal::locale())
00067     aLocaleString = KGlobal::locale()->language().utf8();
00068   else
00069     aLocaleString = KLocale::defaultLanguage().utf8();
00070   if (backEnd)
00071      backEnd->setLocaleString(aLocaleString);
00072 }
00073 
00074 QString KConfigBase::locale() const
00075 {
00076   return QString::fromUtf8(aLocaleString);
00077 }
00078 
00079 void KConfigBase::setGroup( const QString& group )
00080 {
00081   if ( group.isEmpty() )
00082     mGroup = "<default>";
00083   else
00084     mGroup = group.utf8();
00085 }
00086 
00087 void KConfigBase::setGroup( const char *pGroup )
00088 {
00089   setGroup(QCString(pGroup));
00090 }
00091 
00092 void KConfigBase::setGroup( const QCString &group )
00093 {
00094   if ( group.isEmpty() )
00095     mGroup = "<default>";
00096   else
00097     mGroup = group;
00098 }
00099 
00100 QString KConfigBase::group() const {
00101   return QString::fromUtf8(mGroup);
00102 }
00103 
00104 void KConfigBase::setDesktopGroup()
00105 {
00106   mGroup = "Desktop Entry";
00107 }
00108 
00109 bool KConfigBase::hasKey(const QString &key) const
00110 {
00111    return hasKey(key.utf8().data());
00112 }
00113 
00114 bool KConfigBase::hasKey(const char *pKey) const
00115 {
00116   KEntryKey aEntryKey(mGroup, 0);
00117   aEntryKey.c_key = pKey;
00118   aEntryKey.bDefault = readDefaults();
00119 
00120   if (!locale().isNull()) {
00121     // try the localized key first
00122     aEntryKey.bLocal = true;
00123     KEntry entry = lookupData(aEntryKey);
00124     if (!entry.mValue.isNull())
00125        return true;
00126     aEntryKey.bLocal = false;
00127   }
00128 
00129   // try the non-localized version
00130   KEntry entry = lookupData(aEntryKey);
00131   return !entry.mValue.isNull();
00132 }
00133 
00134 bool KConfigBase::hasGroup(const QString &group) const
00135 {
00136   return internalHasGroup( group.utf8());
00137 }
00138 
00139 bool KConfigBase::hasGroup(const char *_pGroup) const
00140 {
00141   return internalHasGroup( QCString(_pGroup));
00142 }
00143 
00144 bool KConfigBase::hasGroup(const QCString &_pGroup) const
00145 {
00146   return internalHasGroup( _pGroup);
00147 }
00148 
00149 bool KConfigBase::isImmutable() const
00150 {
00151   return (getConfigState() != ReadWrite);
00152 }
00153 
00154 bool KConfigBase::groupIsImmutable(const QString &group) const
00155 {
00156   if (getConfigState() != ReadWrite)
00157      return true;
00158 
00159   KEntryKey groupKey(group.utf8(), 0);
00160   KEntry entry = lookupData(groupKey);
00161   return entry.bImmutable;
00162 }
00163 
00164 bool KConfigBase::entryIsImmutable(const QString &key) const
00165 {
00166   if (getConfigState() != ReadWrite)
00167      return true;
00168 
00169   KEntryKey entryKey(mGroup, 0);
00170   KEntry aEntryData = lookupData(entryKey); // Group
00171   if (aEntryData.bImmutable)
00172     return true;
00173 
00174   QCString utf8_key = key.utf8();
00175   entryKey.c_key = utf8_key.data();
00176   aEntryData = lookupData(entryKey); // Normal entry
00177   if (aEntryData.bImmutable)
00178     return true;
00179 
00180   entryKey.bLocal = true;
00181   aEntryData = lookupData(entryKey); // Localized entry
00182   return aEntryData.bImmutable;
00183 }
00184 
00185 
00186 QString KConfigBase::readEntryUntranslated( const QString& pKey,
00187                                 const QString& aDefault ) const
00188 {
00189    return KConfigBase::readEntryUntranslated(pKey.utf8().data(), aDefault);
00190 }
00191 
00192 
00193 QString KConfigBase::readEntryUntranslated( const char *pKey,
00194                                 const QString& aDefault ) const
00195 {
00196    QCString result = readEntryUtf8(pKey);
00197    if (result.isNull())
00198       return aDefault;
00199    return QString::fromUtf8(result);
00200 }
00201 
00202 
00203 QString KConfigBase::readEntry( const QString& pKey,
00204                                 const QString& aDefault ) const
00205 {
00206    return KConfigBase::readEntry(pKey.utf8().data(), aDefault);
00207 }
00208 
00209 QString KConfigBase::readEntry( const char *pKey,
00210                                 const QString& aDefault ) const
00211 {
00212   // we need to access _locale instead of the method locale()
00213   // because calling locale() will create a locale object if it
00214   // doesn't exist, which requires KConfig, which will create a infinite
00215   // loop, and nobody likes those.
00216   if (!bLocaleInitialized && KGlobal::_locale) {
00217     // get around const'ness.
00218     KConfigBase *that = const_cast<KConfigBase *>(this);
00219     that->setLocale();
00220   }
00221 
00222   QString aValue;
00223 
00224   bool expand = false;
00225   // construct a localized version of the key
00226   // try the localized key first
00227   KEntry aEntryData;
00228   KEntryKey entryKey(mGroup, 0);
00229   entryKey.c_key = pKey;
00230   entryKey.bDefault = readDefaults();
00231   entryKey.bLocal = true;
00232   aEntryData = lookupData(entryKey);
00233   if (!aEntryData.mValue.isNull()) {
00234     // for GNOME .desktop
00235     aValue = KStringHandler::from8Bit( aEntryData.mValue.data() );
00236     expand = aEntryData.bExpand;
00237   } else {
00238     entryKey.bLocal = false;
00239     aEntryData = lookupData(entryKey);
00240     if (!aEntryData.mValue.isNull()) {
00241       aValue = QString::fromUtf8(aEntryData.mValue.data());
00242       if (aValue.isNull())
00243       {
00244         static const QString &emptyString = KGlobal::staticQString("");
00245         aValue = emptyString;
00246       }
00247       expand = aEntryData.bExpand;
00248     } else {
00249       aValue = aDefault;
00250     }
00251   }
00252 
00253   // only do dollar expansion if so desired
00254   if( expand || bExpand )
00255     {
00256       // check for environment variables and make necessary translations
00257       int nDollarPos = aValue.find( '$' );
00258 
00259       while( nDollarPos != -1 && nDollarPos+1 < static_cast<int>(aValue.length())) {
00260         // there is at least one $
00261         if( (aValue)[nDollarPos+1] == '(' ) {
00262           uint nEndPos = nDollarPos+1;
00263           // the next character is no $
00264           while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!=')') )
00265               nEndPos++;
00266           nEndPos++;
00267           QString cmd = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
00268 
00269           QString result;
00270           FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
00271           if (fs)
00272           {
00273              QTextStream ts(fs, IO_ReadOnly);
00274              result = ts.read().stripWhiteSpace();
00275              pclose(fs);
00276           }
00277           aValue.replace( nDollarPos, nEndPos-nDollarPos, result );
00278         } else if( (aValue)[nDollarPos+1] != '$' ) {
00279           uint nEndPos = nDollarPos+1;
00280           // the next character is no $
00281           QString aVarName;
00282           if (aValue[nEndPos]=='{')
00283           {
00284             while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!='}') )
00285                 nEndPos++;
00286             nEndPos++;
00287             aVarName = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
00288           }
00289           else
00290           {
00291             while ( nEndPos <= aValue.length() && (aValue[nEndPos].isNumber()
00292                     || aValue[nEndPos].isLetter() || aValue[nEndPos]=='_' )  )
00293                 nEndPos++;
00294             aVarName = aValue.mid( nDollarPos+1, nEndPos-nDollarPos-1 );
00295           }
00296           const char* pEnv = 0;
00297           if (!aVarName.isEmpty())
00298                pEnv = getenv( aVarName.ascii() );
00299           if( pEnv ) {
00300         // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00301         // A environment variables may contain values in 8bit
00302         // locale cpecified encoding or in UTF8 encoding.
00303         aValue.replace( nDollarPos, nEndPos-nDollarPos, KStringHandler::from8Bit( pEnv ) );
00304           } else
00305             aValue.remove( nDollarPos, nEndPos-nDollarPos );
00306         } else {
00307           // remove one of the dollar signs
00308           aValue.remove( nDollarPos, 1 );
00309           nDollarPos++;
00310         }
00311         nDollarPos = aValue.find( '$', nDollarPos );
00312       }
00313     }
00314 
00315   return aValue;
00316 }
00317 
00318 QCString KConfigBase::readEntryUtf8( const char *pKey) const
00319 {
00320   // We don't try the localized key
00321   KEntryKey entryKey(mGroup, 0);
00322   entryKey.bDefault = readDefaults();
00323   entryKey.c_key = pKey;
00324   KEntry aEntryData = lookupData(entryKey);
00325   if (aEntryData.bExpand)
00326   {
00327      // We need to do fancy, take the slow route.
00328      return readEntry(pKey, QString::null).utf8();
00329   }
00330   return aEntryData.mValue;
00331 }
00332 
00333 QVariant KConfigBase::readPropertyEntry( const QString& pKey,
00334                                           QVariant::Type type ) const
00335 {
00336   return readPropertyEntry(pKey.utf8().data(), type);
00337 }
00338 
00339 QVariant KConfigBase::readPropertyEntry( const char *pKey,
00340                                           QVariant::Type type ) const
00341 {
00342   QVariant va;
00343   if ( !hasKey( pKey ) ) return va;
00344   (void)va.cast(type);
00345   return readPropertyEntry(pKey, va);
00346 }
00347 
00348 QVariant KConfigBase::readPropertyEntry( const QString& pKey,
00349                                          const QVariant &aDefault ) const
00350 {
00351   return readPropertyEntry(pKey.utf8().data(), aDefault);
00352 }
00353 
00354 QVariant KConfigBase::readPropertyEntry( const char *pKey,
00355                                          const QVariant &aDefault ) const
00356 {
00357   if ( !hasKey( pKey ) ) return aDefault;
00358 
00359   QVariant tmp = aDefault;
00360 
00361   switch( aDefault.type() )
00362   {
00363       case QVariant::Invalid:
00364           return QVariant();
00365       case QVariant::String:
00366           return QVariant( readEntry( pKey, aDefault.toString() ) );
00367       case QVariant::StringList:
00368           return QVariant( readListEntry( pKey ) );
00369       case QVariant::List: {
00370           QStringList strList = readListEntry( pKey );
00371           QStringList::ConstIterator it = strList.begin();
00372           QStringList::ConstIterator end = strList.end();
00373           QValueList<QVariant> list;
00374 
00375           for (; it != end; ++it ) {
00376               tmp = *it;
00377               list.append( tmp );
00378           }
00379           return QVariant( list );
00380       }
00381       case QVariant::Font:
00382           return QVariant( readFontEntry( pKey, &tmp.asFont() ) );
00383       case QVariant::Point:
00384           return QVariant( readPointEntry( pKey, &tmp.asPoint() ) );
00385       case QVariant::Rect:
00386           return QVariant( readRectEntry( pKey, &tmp.asRect() ) );
00387       case QVariant::Size:
00388           return QVariant( readSizeEntry( pKey, &tmp.asSize() ) );
00389       case QVariant::Color:
00390           return QVariant( readColorEntry( pKey, &tmp.asColor() ) );
00391       case QVariant::Int:
00392           return QVariant( readNumEntry( pKey, aDefault.toInt() ) );
00393       case QVariant::UInt:
00394           return QVariant( readUnsignedNumEntry( pKey, aDefault.toUInt() ) );
00395       case QVariant::LongLong:
00396           return QVariant( readNum64Entry( pKey, aDefault.toLongLong() ) );
00397       case QVariant::ULongLong:
00398           return QVariant( readUnsignedNum64Entry( pKey, aDefault.toULongLong() ) );
00399       case QVariant::Bool:
00400           return QVariant( readBoolEntry( pKey, aDefault.toBool() ), 0 );
00401       case QVariant::Double:
00402           return QVariant( readDoubleNumEntry( pKey, aDefault.toDouble() ) );
00403       case QVariant::DateTime:
00404           return QVariant( readDateTimeEntry( pKey, &tmp.asDateTime() ) );
00405       case QVariant::Date:
00406           return QVariant(readDateTimeEntry( pKey, &tmp.asDateTime() ).date());
00407 
00408       case QVariant::Pixmap:
00409       case QVariant::Image:
00410       case QVariant::Brush:
00411       case QVariant::Palette:
00412       case QVariant::ColorGroup:
00413       case QVariant::Map:
00414       case QVariant::IconSet:
00415       case QVariant::CString:
00416       case QVariant::PointArray:
00417       case QVariant::Region:
00418       case QVariant::Bitmap:
00419       case QVariant::Cursor:
00420       case QVariant::SizePolicy:
00421       case QVariant::Time:
00422       case QVariant::ByteArray:
00423       case QVariant::BitArray:
00424       case QVariant::KeySequence:
00425       case QVariant::Pen:
00426           break;
00427   }
00428 
00429   Q_ASSERT( 0 );
00430   return QVariant();
00431 }
00432 
00433 int KConfigBase::readListEntry( const QString& pKey,
00434                                 QStrList &list, char sep ) const
00435 {
00436   return readListEntry(pKey.utf8().data(), list, sep);
00437 }
00438 
00439 int KConfigBase::readListEntry( const char *pKey,
00440                                 QStrList &list, char sep ) const
00441 {
00442   if( !hasKey( pKey ) )
00443     return 0;
00444 
00445   QCString str_list = readEntryUtf8( pKey );
00446   if (str_list.isEmpty())
00447     return 0;
00448 
00449   list.clear();
00450   QCString value = "";
00451   int len = str_list.length();
00452 
00453   for (int i = 0; i < len; i++) {
00454     if (str_list[i] != sep && str_list[i] != '\\') {
00455       value += str_list[i];
00456       continue;
00457     }
00458     if (str_list[i] == '\\') {
00459       i++;
00460       if ( i < len )
00461         value += str_list[i];
00462       continue;
00463     }
00464     // if we fell through to here, we are at a separator.  Append
00465     // contents of value to the list
00466     // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00467     // A QStrList may contain values in 8bit locale cpecified
00468     // encoding
00469     list.append( value );
00470     value.truncate(0);
00471   }
00472 
00473   if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
00474     list.append( value );
00475   return list.count();
00476 }
00477 
00478 QStringList KConfigBase::readListEntry( const QString& pKey, char sep ) const
00479 {
00480   return readListEntry(pKey.utf8().data(), sep);
00481 }
00482 
00483 QStringList KConfigBase::readListEntry( const char *pKey, char sep ) const
00484 {
00485   static const QString& emptyString = KGlobal::staticQString("");
00486 
00487   QStringList list;
00488   if( !hasKey( pKey ) )
00489     return list;
00490   QString str_list = readEntry( pKey );
00491   if( str_list.isEmpty() )
00492     return list;
00493   QString value(emptyString);
00494   int len = str_list.length();
00495  // obviously too big, but faster than letting each += resize the string.
00496   value.reserve( len );
00497   for( int i = 0; i < len; i++ )
00498     {
00499       if( str_list[i] != sep && str_list[i] != '\\' )
00500         {
00501           value += str_list[i];
00502           continue;
00503         }
00504       if( str_list[i] == '\\' )
00505         {
00506           i++;
00507           if ( i < len )
00508             value += str_list[i];
00509           continue;
00510         }
00511       QString finalvalue( value );
00512       finalvalue.squeeze();
00513       list.append( finalvalue );
00514       value.truncate( 0 );
00515     }
00516   if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
00517   {
00518     value.squeeze();
00519     list.append( value );
00520   }
00521   return list;
00522 }
00523 
00524 QStringList KConfigBase::readListEntry( const char* pKey, const QStringList& aDefault,
00525         char sep ) const
00526 {
00527     if ( !hasKey( pKey ) )
00528         return aDefault;
00529     else
00530         return readListEntry( pKey, sep );
00531 }
00532 
00533 QValueList<int> KConfigBase::readIntListEntry( const QString& pKey ) const
00534 {
00535   return readIntListEntry(pKey.utf8().data());
00536 }
00537 
00538 QValueList<int> KConfigBase::readIntListEntry( const char *pKey ) const
00539 {
00540   QStringList strlist = readListEntry(pKey);
00541   QValueList<int> list;
00542   for (QStringList::ConstIterator it = strlist.begin(); it != strlist.end(); it++)
00543     // I do not check if the toInt failed because I consider the number of items
00544     // more important than their value
00545     list << (*it).toInt();
00546 
00547   return list;
00548 }
00549 
00550 QString KConfigBase::readPathEntry( const QString& pKey, const QString& pDefault ) const
00551 {
00552   return readPathEntry(pKey.utf8().data(), pDefault);
00553 }
00554 
00555 QString KConfigBase::readPathEntry( const char *pKey, const QString& pDefault ) const
00556 {
00557   const bool bExpandSave = bExpand;
00558   bExpand = true;
00559   QString aValue = readEntry( pKey, pDefault );
00560   bExpand = bExpandSave;
00561   return aValue;
00562 }
00563 
00564 QStringList KConfigBase::readPathListEntry( const QString& pKey, char sep ) const
00565 {
00566   return readPathListEntry(pKey.utf8().data(), sep);
00567 }
00568 
00569 QStringList KConfigBase::readPathListEntry( const char *pKey, char sep ) const
00570 {
00571   const bool bExpandSave = bExpand;
00572   bExpand = true;
00573   QStringList aValue = readListEntry( pKey, sep );
00574   bExpand = bExpandSave;
00575   return aValue;
00576 }
00577 
00578 int KConfigBase::readNumEntry( const QString& pKey, int nDefault) const
00579 {
00580   return readNumEntry(pKey.utf8().data(), nDefault);
00581 }
00582 
00583 int KConfigBase::readNumEntry( const char *pKey, int nDefault) const
00584 {
00585   QCString aValue = readEntryUtf8( pKey );
00586   if( aValue.isNull() )
00587     return nDefault;
00588   else if( aValue == "true" || aValue == "on" || aValue == "yes" )
00589     return 1;
00590   else
00591     {
00592       bool ok;
00593       int rc = aValue.toInt( &ok );
00594       return( ok ? rc : nDefault );
00595     }
00596 }
00597 
00598 
00599 unsigned int KConfigBase::readUnsignedNumEntry( const QString& pKey, unsigned int nDefault) const
00600 {
00601   return readUnsignedNumEntry(pKey.utf8().data(), nDefault);
00602 }
00603 
00604 unsigned int KConfigBase::readUnsignedNumEntry( const char *pKey, unsigned int nDefault) const
00605 {
00606   QCString aValue = readEntryUtf8( pKey );
00607   if( aValue.isNull() )
00608     return nDefault;
00609   else
00610     {
00611       bool ok;
00612       unsigned int rc = aValue.toUInt( &ok );
00613       return( ok ? rc : nDefault );
00614     }
00615 }
00616 
00617 
00618 long KConfigBase::readLongNumEntry( const QString& pKey, long nDefault) const
00619 {
00620   return readLongNumEntry(pKey.utf8().data(), nDefault);
00621 }
00622 
00623 long KConfigBase::readLongNumEntry( const char *pKey, long nDefault) const
00624 {
00625   QCString aValue = readEntryUtf8( pKey );
00626   if( aValue.isNull() )
00627     return nDefault;
00628   else
00629     {
00630       bool ok;
00631       long rc = aValue.toLong( &ok );
00632       return( ok ? rc : nDefault );
00633     }
00634 }
00635 
00636 
00637 unsigned long KConfigBase::readUnsignedLongNumEntry( const QString& pKey, unsigned long nDefault) const
00638 {
00639   return readUnsignedLongNumEntry(pKey.utf8().data(), nDefault);
00640 }
00641 
00642 unsigned long KConfigBase::readUnsignedLongNumEntry( const char *pKey, unsigned long nDefault) const
00643 {
00644   QCString aValue = readEntryUtf8( pKey );
00645   if( aValue.isNull() )
00646     return nDefault;
00647   else
00648     {
00649       bool ok;
00650       unsigned long rc = aValue.toULong( &ok );
00651       return( ok ? rc : nDefault );
00652     }
00653 }
00654 
00655 Q_INT64 KConfigBase::readNum64Entry( const QString& pKey, Q_INT64 nDefault) const
00656 {
00657   return readNum64Entry(pKey.utf8().data(), nDefault);
00658 }
00659 
00660 Q_INT64 KConfigBase::readNum64Entry( const char *pKey, Q_INT64 nDefault) const
00661 {
00662   // Note that QCString::toLongLong() is missing, we muse use a QString instead.
00663   QString aValue = readEntry( pKey );
00664   if( aValue.isNull() )
00665     return nDefault;
00666   else
00667     {
00668       bool ok;
00669       Q_INT64 rc = aValue.toLongLong( &ok );
00670       return( ok ? rc : nDefault );
00671     }
00672 }
00673 
00674 
00675 Q_UINT64 KConfigBase::readUnsignedNum64Entry( const QString& pKey, Q_UINT64 nDefault) const
00676 {
00677   return readUnsignedNum64Entry(pKey.utf8().data(), nDefault);
00678 }
00679 
00680 Q_UINT64 KConfigBase::readUnsignedNum64Entry( const char *pKey, Q_UINT64 nDefault) const
00681 {
00682   // Note that QCString::toULongLong() is missing, we muse use a QString instead.
00683   QString aValue = readEntry( pKey );
00684   if( aValue.isNull() )
00685     return nDefault;
00686   else
00687     {
00688       bool ok;
00689       Q_UINT64 rc = aValue.toULongLong( &ok );
00690       return( ok ? rc : nDefault );
00691     }
00692 }
00693 
00694 double KConfigBase::readDoubleNumEntry( const QString& pKey, double nDefault) const
00695 {
00696   return readDoubleNumEntry(pKey.utf8().data(), nDefault);
00697 }
00698 
00699 double KConfigBase::readDoubleNumEntry( const char *pKey, double nDefault) const
00700 {
00701   QCString aValue = readEntryUtf8( pKey );
00702   if( aValue.isNull() )
00703     return nDefault;
00704   else
00705     {
00706       bool ok;
00707       double rc = aValue.toDouble( &ok );
00708       return( ok ? rc : nDefault );
00709     }
00710 }
00711 
00712 
00713 bool KConfigBase::readBoolEntry( const QString& pKey, bool bDefault ) const
00714 {
00715    return readBoolEntry(pKey.utf8().data(), bDefault);
00716 }
00717 
00718 bool KConfigBase::readBoolEntry( const char *pKey, bool bDefault ) const
00719 {
00720   QCString aValue = readEntryUtf8( pKey );
00721 
00722   if( aValue.isNull() )
00723     return bDefault;
00724   else
00725     {
00726       if( aValue == "true" || aValue == "on" || aValue == "yes" || aValue == "1" )
00727         return true;
00728       else
00729         {
00730           bool bOK;
00731           int val = aValue.toInt( &bOK );
00732           if( bOK && val != 0 )
00733             return true;
00734           else
00735             return false;
00736         }
00737     }
00738 }
00739 
00740 QFont KConfigBase::readFontEntry( const QString& pKey, const QFont* pDefault ) const
00741 {
00742   return readFontEntry(pKey.utf8().data(), pDefault);
00743 }
00744 
00745 QFont KConfigBase::readFontEntry( const char *pKey, const QFont* pDefault ) const
00746 {
00747   QFont aRetFont;
00748 
00749   QString aValue = readEntry( pKey );
00750   if( !aValue.isNull() ) {
00751     if ( aValue.contains( ',' ) > 5 ) {
00752       // KDE3 and upwards entry
00753       if ( !aRetFont.fromString( aValue ) && pDefault )
00754         aRetFont = *pDefault;
00755     }
00756     else {
00757       // backward compatibility with older font formats
00758       // ### remove KDE 3.1 ?
00759       // find first part (font family)
00760       int nIndex = aValue.find( ',' );
00761       if( nIndex == -1 ){
00762         if( pDefault )
00763           aRetFont = *pDefault;
00764         return aRetFont;
00765       }
00766       aRetFont.setFamily( aValue.left( nIndex ) );
00767 
00768       // find second part (point size)
00769       int nOldIndex = nIndex;
00770       nIndex = aValue.find( ',', nOldIndex+1 );
00771       if( nIndex == -1 ){
00772         if( pDefault )
00773           aRetFont = *pDefault;
00774         return aRetFont;
00775       }
00776 
00777       aRetFont.setPointSize( aValue.mid( nOldIndex+1,
00778                                          nIndex-nOldIndex-1 ).toInt() );
00779 
00780       // find third part (style hint)
00781       nOldIndex = nIndex;
00782       nIndex = aValue.find( ',', nOldIndex+1 );
00783 
00784       if( nIndex == -1 ){
00785         if( pDefault )
00786           aRetFont = *pDefault;
00787         return aRetFont;
00788       }
00789 
00790       aRetFont.setStyleHint( (QFont::StyleHint)aValue.mid( nOldIndex+1, nIndex-nOldIndex-1 ).toUInt() );
00791 
00792       // find fourth part (char set)
00793       nOldIndex = nIndex;
00794       nIndex = aValue.find( ',', nOldIndex+1 );
00795 
00796       if( nIndex == -1 ){
00797         if( pDefault )
00798           aRetFont = *pDefault;
00799         return aRetFont;
00800       }
00801 
00802       QString chStr=aValue.mid( nOldIndex+1,
00803                                 nIndex-nOldIndex-1 );
00804       // find fifth part (weight)
00805       nOldIndex = nIndex;
00806       nIndex = aValue.find( ',', nOldIndex+1 );
00807 
00808       if( nIndex == -1 ){
00809         if( pDefault )
00810           aRetFont = *pDefault;
00811         return aRetFont;
00812       }
00813 
00814       aRetFont.setWeight( aValue.mid( nOldIndex+1,
00815                                       nIndex-nOldIndex-1 ).toUInt() );
00816 
00817       // find sixth part (font bits)
00818       uint nFontBits = aValue.right( aValue.length()-nIndex-1 ).toUInt();
00819 
00820       aRetFont.setItalic( nFontBits & 0x01 );
00821       aRetFont.setUnderline( nFontBits & 0x02 );
00822       aRetFont.setStrikeOut( nFontBits & 0x04 );
00823       aRetFont.setFixedPitch( nFontBits & 0x08 );
00824       aRetFont.setRawMode( nFontBits & 0x20 );
00825     }
00826   }
00827   else
00828     {
00829       if( pDefault )
00830         aRetFont = *pDefault;
00831     }
00832 
00833   return aRetFont;
00834 }
00835 
00836 
00837 QRect KConfigBase::readRectEntry( const QString& pKey, const QRect* pDefault ) const
00838 {
00839   return readRectEntry(pKey.utf8().data(), pDefault);
00840 }
00841 
00842 QRect KConfigBase::readRectEntry( const char *pKey, const QRect* pDefault ) const
00843 {
00844   QCString aValue = readEntryUtf8(pKey);
00845 
00846   if (!aValue.isEmpty())
00847   {
00848     int left, top, width, height;
00849 
00850     if (sscanf(aValue.data(), "%d,%d,%d,%d", &left, &top, &width, &height) == 4)
00851     {
00852        return QRect(left, top, width, height);
00853     }
00854   }
00855   if (pDefault)
00856     return *pDefault;
00857   return QRect();
00858 }
00859 
00860 
00861 QPoint KConfigBase::readPointEntry( const QString& pKey,
00862                                     const QPoint* pDefault ) const
00863 {
00864   return readPointEntry(pKey.utf8().data(), pDefault);
00865 }
00866 
00867 QPoint KConfigBase::readPointEntry( const char *pKey,
00868                                     const QPoint* pDefault ) const
00869 {
00870   QCString aValue = readEntryUtf8(pKey);
00871 
00872   if (!aValue.isEmpty())
00873   {
00874     int x,y;
00875 
00876     if (sscanf(aValue.data(), "%d,%d", &x, &y) == 2)
00877     {
00878        return QPoint(x,y);
00879     }
00880   }
00881   if (pDefault)
00882     return *pDefault;
00883   return QPoint();
00884 }
00885 
00886 QSize KConfigBase::readSizeEntry( const QString& pKey,
00887                                   const QSize* pDefault ) const
00888 {
00889   return readSizeEntry(pKey.utf8().data(), pDefault);
00890 }
00891 
00892 QSize KConfigBase::readSizeEntry( const char *pKey,
00893                                   const QSize* pDefault ) const
00894 {
00895   QCString aValue = readEntryUtf8(pKey);
00896 
00897   if (!aValue.isEmpty())
00898   {
00899     int width,height;
00900 
00901     if (sscanf(aValue.data(), "%d,%d", &width, &height) == 2)
00902     {
00903        return QSize(width, height);
00904     }
00905   }
00906   if (pDefault)
00907     return *pDefault;
00908   return QSize();
00909 }
00910 
00911 
00912 QColor KConfigBase::readColorEntry( const QString& pKey,
00913                                     const QColor* pDefault ) const
00914 {
00915   return readColorEntry(pKey.utf8().data(), pDefault);
00916 }
00917 
00918 QColor KConfigBase::readColorEntry( const char *pKey,
00919                                     const QColor* pDefault ) const
00920 {
00921   QColor aRetColor;
00922   int nRed = 0, nGreen = 0, nBlue = 0;
00923 
00924   QString aValue = readEntry( pKey );
00925   if( !aValue.isEmpty() )
00926     {
00927       if ( aValue.at(0) == '#' )
00928         {
00929           aRetColor.setNamedColor(aValue);
00930         }
00931       else
00932         {
00933 
00934           bool bOK;
00935 
00936           // find first part (red)
00937           int nIndex = aValue.find( ',' );
00938 
00939           if( nIndex == -1 ){
00940             // return a sensible default -- Bernd
00941             if( pDefault )
00942               aRetColor = *pDefault;
00943             return aRetColor;
00944           }
00945 
00946           nRed = aValue.left( nIndex ).toInt( &bOK );
00947 
00948           // find second part (green)
00949           int nOldIndex = nIndex;
00950           nIndex = aValue.find( ',', nOldIndex+1 );
00951 
00952           if( nIndex == -1 ){
00953             // return a sensible default -- Bernd
00954             if( pDefault )
00955               aRetColor = *pDefault;
00956             return aRetColor;
00957           }
00958           nGreen = aValue.mid( nOldIndex+1,
00959                                nIndex-nOldIndex-1 ).toInt( &bOK );
00960 
00961           // find third part (blue)
00962           nBlue = aValue.right( aValue.length()-nIndex-1 ).toInt( &bOK );
00963 
00964           aRetColor.setRgb( nRed, nGreen, nBlue );
00965         }
00966     }
00967   else {
00968 
00969     if( pDefault )
00970       aRetColor = *pDefault;
00971   }
00972 
00973   return aRetColor;
00974 }
00975 
00976 
00977 QDateTime KConfigBase::readDateTimeEntry( const QString& pKey,
00978                                           const QDateTime* pDefault ) const
00979 {
00980   return readDateTimeEntry(pKey.utf8().data(), pDefault);
00981 }
00982 
00983 // ### currentDateTime() as fallback ? (Harri)
00984 QDateTime KConfigBase::readDateTimeEntry( const char *pKey,
00985                                           const QDateTime* pDefault ) const
00986 {
00987   if( !hasKey( pKey ) )
00988     {
00989       if( pDefault )
00990         return *pDefault;
00991       else
00992         return QDateTime::currentDateTime();
00993     }
00994 
00995   QStrList list;
00996   int count = readListEntry( pKey, list, ',' );
00997   if( count == 6 ) {
00998     QDate date( atoi( list.at( 0 ) ), atoi( list.at( 1 ) ),
00999                 atoi( list.at( 2 ) ) );
01000     QTime time( atoi( list.at( 3 ) ), atoi( list.at( 4 ) ),
01001                 atoi( list.at( 5 ) ) );
01002 
01003     return QDateTime( date, time );
01004   }
01005 
01006   return QDateTime::currentDateTime();
01007 }
01008 
01009 void KConfigBase::writeEntry( const QString& pKey, const QString& value,
01010                                  bool bPersistent,
01011                                  bool bGlobal,
01012                                  bool bNLS )
01013 {
01014    writeEntry(pKey.utf8().data(), value, bPersistent,  bGlobal, bNLS);
01015 }
01016 
01017 void KConfigBase::writeEntry( const char *pKey, const QString& value,
01018                                  bool bPersistent,
01019                                  bool bGlobal,
01020                                  bool bNLS )
01021 {
01022   // the KConfig object is dirty now
01023   // set this before any IO takes place so that if any derivative
01024   // classes do caching, they won't try and flush the cache out
01025   // from under us before we read. A race condition is still
01026   // possible but minimized.
01027   if( bPersistent )
01028     setDirty(true);
01029 
01030   if (!bLocaleInitialized && KGlobal::locale())
01031     setLocale();
01032 
01033   KEntryKey entryKey(mGroup, pKey);
01034   entryKey.bLocal = bNLS;
01035 
01036   KEntry aEntryData;
01037   aEntryData.mValue = value.utf8();  // set new value
01038   aEntryData.bGlobal = bGlobal;
01039   aEntryData.bNLS = bNLS;
01040 
01041   if (bPersistent)
01042     aEntryData.bDirty = true;
01043 
01044   // rewrite the new value
01045   putData(entryKey, aEntryData, true);
01046 }
01047 
01048 void KConfigBase::writePathEntry( const QString& pKey, const QString & path,
01049                                   bool bPersistent, bool bGlobal,
01050                                   bool bNLS)
01051 {
01052    writePathEntry(pKey.utf8().data(), path, bPersistent, bGlobal, bNLS);
01053 }
01054 
01055 
01056 static bool cleanHomeDirPath( QString &path, const QString &homeDir )
01057 {
01058    if (!path.startsWith(homeDir))
01059         return false;
01060 
01061    unsigned int len = homeDir.length();
01062    // replace by "$HOME" if possible
01063    if (path.length() == len || path[len] == '/') {
01064         path.replace(0, len, QString::fromLatin1("$HOME"));
01065         return true;
01066    } else
01067         return false;
01068 }
01069 
01070 static QString translatePath( QString path )
01071 {
01072    if (path.isEmpty())
01073        return path;
01074 
01075    // only "our" $HOME should be interpreted
01076    path.replace('$', "$$");
01077 
01078    bool startsWithFile = path.startsWith("file:", false);
01079 
01080    // return original path, if it refers to another type of URL (e.g. http:/), or
01081    // if the path is already relative to another directory
01082    if (!startsWithFile && path[0] != '/' ||
01083         startsWithFile && path[5] != '/')
01084     return path;
01085 
01086    if (startsWithFile)
01087         path.remove(0,5); // strip leading "file:/" off the string
01088 
01089    // we can not use KGlobal::dirs()->relativeLocation("home", path) here,
01090    // since it would not recognize paths without a trailing '/'.
01091    // All of the 3 following functions to return the user's home directory
01092    // can return different paths. We have to test all them.
01093    QString homeDir0 = QFile::decodeName(getenv("HOME"));
01094    QString homeDir1 = QDir::homeDirPath();
01095    QString homeDir2 = QDir(homeDir1).canonicalPath();
01096    if (cleanHomeDirPath(path, homeDir0) ||
01097        cleanHomeDirPath(path, homeDir1) ||
01098        cleanHomeDirPath(path, homeDir2) ) {
01099      // kdDebug() << "Path was replaced\n";
01100    }
01101 
01102    if (startsWithFile)
01103       path.prepend( "file:" );
01104 
01105    return path;
01106 }
01107 
01108 void KConfigBase::writePathEntry( const char *pKey, const QString & path,
01109                                   bool bPersistent, bool bGlobal,
01110                                   bool bNLS)
01111 {
01112    writeEntry(pKey, translatePath(path), bPersistent, bGlobal, bNLS);
01113 }
01114 
01115 void KConfigBase::writePathEntry ( const QString& pKey, const QStringList &list,
01116                                char sep , bool bPersistent,
01117                                bool bGlobal, bool bNLS )
01118 {
01119   writePathEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01120 }
01121 
01122 void KConfigBase::writePathEntry ( const char *pKey, const QStringList &list,
01123                                char sep , bool bPersistent,
01124                                bool bGlobal, bool bNLS )
01125 {
01126   if( list.isEmpty() )
01127     {
01128       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01129       return;
01130     }
01131   QStringList new_list;
01132   QStringList::ConstIterator it = list.begin();
01133   for( ; it != list.end(); ++it )
01134     {
01135       QString value = *it;
01136       new_list.append( translatePath(value) );
01137     }
01138   writeEntry( pKey, new_list, sep, bPersistent, bGlobal, bNLS );
01139 }
01140 
01141 void KConfigBase::deleteEntry( const QString& pKey,
01142                                  bool bNLS,
01143                                  bool bGlobal)
01144 {
01145    deleteEntry(pKey.utf8().data(), bNLS, bGlobal);
01146 }
01147 
01148 void KConfigBase::deleteEntry( const char *pKey,
01149                                  bool bNLS,
01150                                  bool bGlobal)
01151 {
01152   // the KConfig object is dirty now
01153   // set this before any IO takes place so that if any derivative
01154   // classes do caching, they won't try and flush the cache out
01155   // from under us before we read. A race condition is still
01156   // possible but minimized.
01157   setDirty(true);
01158 
01159   if (!bLocaleInitialized && KGlobal::locale())
01160     setLocale();
01161 
01162   KEntryKey entryKey(mGroup, pKey);
01163   KEntry aEntryData;
01164 
01165   aEntryData.bGlobal = bGlobal;
01166   aEntryData.bNLS = bNLS;
01167   aEntryData.bDirty = true;
01168   aEntryData.bDeleted = true;
01169 
01170   // rewrite the new value
01171   putData(entryKey, aEntryData, true);
01172 }
01173 
01174 bool KConfigBase::deleteGroup( const QString& group, bool bDeep, bool bGlobal )
01175 {
01176   KEntryMap aEntryMap = internalEntryMap(group);
01177 
01178   if (!bDeep) {
01179     // Check if it empty
01180     return aEntryMap.isEmpty();
01181   }
01182 
01183   bool dirty = false;
01184   bool checkGroup = true;
01185   // we want to remove all entries in the group
01186   KEntryMapIterator aIt;
01187   for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt)
01188   {
01189     if (!aIt.key().mKey.isEmpty() && !aIt.key().bDefault && !(*aIt).bDeleted)
01190     {
01191       (*aIt).bDeleted = true;
01192       (*aIt).bDirty = true;
01193       (*aIt).bGlobal = bGlobal;
01194       (*aIt).mValue = 0;
01195       putData(aIt.key(), *aIt, checkGroup);
01196       checkGroup = false;
01197       dirty = true;
01198     }
01199   }
01200   if (dirty)
01201      setDirty(true);
01202   return true;
01203 }
01204 
01205 void KConfigBase::writeEntry ( const QString& pKey, const QVariant &prop,
01206                                bool bPersistent,
01207                                bool bGlobal, bool bNLS )
01208 {
01209   writeEntry(pKey.utf8().data(), prop, bPersistent, bGlobal, bNLS);
01210 }
01211 
01212 void KConfigBase::writeEntry ( const char *pKey, const QVariant &prop,
01213                                bool bPersistent,
01214                                bool bGlobal, bool bNLS )
01215 {
01216   switch( prop.type() )
01217     {
01218     case QVariant::Invalid:
01219       writeEntry( pKey, "", bPersistent, bGlobal, bNLS );
01220       return;
01221     case QVariant::String:
01222       writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
01223       return;
01224     case QVariant::StringList:
01225       writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
01226       return;
01227     case QVariant::List: {
01228         QValueList<QVariant> list = prop.toList();
01229         QValueList<QVariant>::ConstIterator it = list.begin();
01230         QValueList<QVariant>::ConstIterator end = list.end();
01231         QStringList strList;
01232 
01233         for (; it != end; ++it )
01234             strList.append( (*it).toString() );
01235 
01236         writeEntry( pKey, strList, ',', bPersistent, bGlobal, bNLS );
01237 
01238         return;
01239     }
01240     case QVariant::Font:
01241       writeEntry( pKey, prop.toFont(), bPersistent, bGlobal, bNLS );
01242       return;
01243     case QVariant::Point:
01244       writeEntry( pKey, prop.toPoint(), bPersistent, bGlobal, bNLS );
01245       return;
01246     case QVariant::Rect:
01247       writeEntry( pKey, prop.toRect(), bPersistent, bGlobal, bNLS );
01248       return;
01249     case QVariant::Size:
01250       writeEntry( pKey, prop.toSize(), bPersistent, bGlobal, bNLS );
01251       return;
01252     case QVariant::Color:
01253       writeEntry( pKey, prop.toColor(), bPersistent, bGlobal, bNLS );
01254       return;
01255     case QVariant::Int:
01256       writeEntry( pKey, prop.toInt(), bPersistent, bGlobal, bNLS );
01257       return;
01258     case QVariant::UInt:
01259       writeEntry( pKey, prop.toUInt(), bPersistent, bGlobal, bNLS );
01260       return;
01261     case QVariant::LongLong:
01262       writeEntry( pKey, prop.toLongLong(), bPersistent, bGlobal, bNLS );
01263       return;
01264     case QVariant::ULongLong:
01265       writeEntry( pKey, prop.toULongLong(), bPersistent, bGlobal, bNLS );
01266       return;
01267     case QVariant::Bool:
01268       writeEntry( pKey, prop.toBool(), bPersistent, bGlobal, bNLS );
01269       return;
01270     case QVariant::Double:
01271       writeEntry( pKey, prop.toDouble(), bPersistent, bGlobal, 'g', 6, bNLS );
01272       return;
01273     case QVariant::DateTime:
01274       writeEntry( pKey, prop.toDateTime(), bPersistent, bGlobal, bNLS);
01275       return;
01276     case QVariant::Date:
01277       writeEntry( pKey, QDateTime(prop.toDate()), bPersistent, bGlobal, bNLS);
01278       return;
01279 
01280     case QVariant::Pixmap:
01281     case QVariant::Image:
01282     case QVariant::Brush:
01283     case QVariant::Palette:
01284     case QVariant::ColorGroup:
01285     case QVariant::Map:
01286     case QVariant::IconSet:
01287     case QVariant::CString:
01288     case QVariant::PointArray:
01289     case QVariant::Region:
01290     case QVariant::Bitmap:
01291     case QVariant::Cursor:
01292     case QVariant::SizePolicy:
01293     case QVariant::Time:
01294     case QVariant::ByteArray:
01295     case QVariant::BitArray:
01296     case QVariant::KeySequence:
01297     case QVariant::Pen:
01298         break;
01299     }
01300 
01301   Q_ASSERT( 0 );
01302 }
01303 
01304 void KConfigBase::writeEntry ( const QString& pKey, const QStrList &list,
01305                                char sep , bool bPersistent,
01306                                bool bGlobal, bool bNLS )
01307 {
01308   writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01309 }
01310 
01311 void KConfigBase::writeEntry ( const char *pKey, const QStrList &list,
01312                                char sep , bool bPersistent,
01313                                bool bGlobal, bool bNLS )
01314 {
01315   if( list.isEmpty() )
01316     {
01317       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01318       return;
01319     }
01320   QString str_list;
01321   QStrListIterator it( list );
01322   for( ; it.current(); ++it )
01323     {
01324       uint i;
01325       QString value;
01326       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
01327       // A QStrList may contain values in 8bit locale cpecified
01328       // encoding or in UTF8 encoding.
01329       value = KStringHandler::from8Bit(it.current());
01330       for( i = 0; i < value.length(); i++ )
01331         {
01332           if( value[i] == sep || value[i] == '\\' )
01333             str_list += '\\';
01334           str_list += value[i];
01335         }
01336       str_list += sep;
01337     }
01338   if( str_list.at(str_list.length() - 1) == sep )
01339     str_list.truncate( str_list.length() -1 );
01340   writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS );
01341 }
01342 
01343 void KConfigBase::writeEntry ( const QString& pKey, const QStringList &list,
01344                                char sep , bool bPersistent,
01345                                bool bGlobal, bool bNLS )
01346 {
01347   writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01348 }
01349 
01350 void KConfigBase::writeEntry ( const char *pKey, const QStringList &list,
01351                                char sep , bool bPersistent,
01352                                bool bGlobal, bool bNLS )
01353 {
01354   if( list.isEmpty() )
01355     {
01356       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01357       return;
01358     }
01359   QString str_list;
01360   str_list.reserve( 4096 );
01361   QStringList::ConstIterator it = list.begin();
01362   for( ; it != list.end(); ++it )
01363     {
01364       QString value = *it;
01365       uint i;
01366       for( i = 0; i < value.length(); i++ )
01367         {
01368           if( value[i] == sep || value[i] == '\\' )
01369             str_list += '\\';
01370           str_list += value[i];
01371         }
01372       str_list += sep;
01373     }
01374   if( str_list.at(str_list.length() - 1) == sep )
01375     str_list.truncate( str_list.length() -1 );
01376   writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS );
01377 }
01378 
01379 void KConfigBase::writeEntry ( const QString& pKey, const QValueList<int> &list,
01380                                bool bPersistent, bool bGlobal, bool bNLS )
01381 {
01382   writeEntry(pKey.utf8().data(), list, bPersistent, bGlobal, bNLS);
01383 }
01384 
01385 void KConfigBase::writeEntry ( const char *pKey, const QValueList<int> &list,
01386                                bool bPersistent, bool bGlobal, bool bNLS )
01387 {
01388     QStringList strlist;
01389     QValueList<int>::ConstIterator end = list.end();
01390     for (QValueList<int>::ConstIterator it = list.begin(); it != end; it++)
01391         strlist << QString::number(*it);
01392     writeEntry(pKey, strlist, ',', bPersistent, bGlobal, bNLS );
01393 }
01394 
01395 void KConfigBase::writeEntry( const QString& pKey, int nValue,
01396                                  bool bPersistent, bool bGlobal,
01397                                  bool bNLS )
01398 {
01399   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01400 }
01401 
01402 void KConfigBase::writeEntry( const char *pKey, int nValue,
01403                                  bool bPersistent, bool bGlobal,
01404                                  bool bNLS )
01405 {
01406   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01407 }
01408 
01409 
01410 void KConfigBase::writeEntry( const QString& pKey, unsigned int nValue,
01411                                  bool bPersistent, bool bGlobal,
01412                                  bool bNLS )
01413 {
01414   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01415 }
01416 
01417 void KConfigBase::writeEntry( const char *pKey, unsigned int nValue,
01418                                  bool bPersistent, bool bGlobal,
01419                                  bool bNLS )
01420 {
01421   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01422 }
01423 
01424 
01425 void KConfigBase::writeEntry( const QString& pKey, long nValue,
01426                                  bool bPersistent, bool bGlobal,
01427                                  bool bNLS )
01428 {
01429   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01430 }
01431 
01432 void KConfigBase::writeEntry( const char *pKey, long nValue,
01433                                  bool bPersistent, bool bGlobal,
01434                                  bool bNLS )
01435 {
01436   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01437 }
01438 
01439 
01440 void KConfigBase::writeEntry( const QString& pKey, unsigned long nValue,
01441                                  bool bPersistent, bool bGlobal,
01442                                  bool bNLS )
01443 {
01444   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01445 }
01446 
01447 void KConfigBase::writeEntry( const char *pKey, unsigned long nValue,
01448                                  bool bPersistent, bool bGlobal,
01449                                  bool bNLS )
01450 {
01451   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01452 }
01453 
01454 void KConfigBase::writeEntry( const QString& pKey, Q_INT64 nValue,
01455                                  bool bPersistent, bool bGlobal,
01456                                  bool bNLS )
01457 {
01458   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01459 }
01460 
01461 void KConfigBase::writeEntry( const char *pKey, Q_INT64 nValue,
01462                                  bool bPersistent, bool bGlobal,
01463                                  bool bNLS )
01464 {
01465   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01466 }
01467 
01468 
01469 void KConfigBase::writeEntry( const QString& pKey, Q_UINT64 nValue,
01470                                  bool bPersistent, bool bGlobal,
01471                                  bool bNLS )
01472 {
01473   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01474 }
01475 
01476 void KConfigBase::writeEntry( const char *pKey, Q_UINT64 nValue,
01477                                  bool bPersistent, bool bGlobal,
01478                                  bool bNLS )
01479 {
01480   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01481 }
01482 
01483 void KConfigBase::writeEntry( const QString& pKey, double nValue,
01484                                  bool bPersistent, bool bGlobal,
01485                                  char format, int precision,
01486                                  bool bNLS )
01487 {
01488   writeEntry( pKey, QString::number(nValue, format, precision),
01489                      bPersistent, bGlobal, bNLS );
01490 }
01491 
01492 void KConfigBase::writeEntry( const char *pKey, double nValue,
01493                                  bool bPersistent, bool bGlobal,
01494                                  char format, int precision,
01495                                  bool bNLS )
01496 {
01497   writeEntry( pKey, QString::number(nValue, format, precision),
01498                      bPersistent, bGlobal, bNLS );
01499 }
01500 
01501 
01502 void KConfigBase::writeEntry( const QString& pKey, bool bValue,
01503                                  bool bPersistent,
01504                                  bool bGlobal,
01505                                  bool bNLS )
01506 {
01507   writeEntry(pKey.utf8().data(), bValue, bPersistent, bGlobal, bNLS);
01508 }
01509 
01510 void KConfigBase::writeEntry( const char *pKey, bool bValue,
01511                                  bool bPersistent,
01512                                  bool bGlobal,
01513                                  bool bNLS )
01514 {
01515   QString aValue;
01516 
01517   if( bValue )
01518     aValue = "true";
01519   else
01520     aValue = "false";
01521 
01522   writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
01523 }
01524 
01525 
01526 void KConfigBase::writeEntry( const QString& pKey, const QFont& rFont,
01527                                  bool bPersistent, bool bGlobal,
01528                                  bool bNLS )
01529 {
01530   writeEntry(pKey.utf8().data(), rFont, bPersistent, bGlobal, bNLS);
01531 }
01532 
01533 void KConfigBase::writeEntry( const char *pKey, const QFont& rFont,
01534                                  bool bPersistent, bool bGlobal,
01535                                  bool bNLS )
01536 {
01537   writeEntry( pKey, rFont.toString(), bPersistent, bGlobal, bNLS );
01538 }
01539 
01540 
01541 void KConfigBase::writeEntry( const QString& pKey, const QRect& rRect,
01542                               bool bPersistent, bool bGlobal,
01543                               bool bNLS )
01544 {
01545   writeEntry(pKey.utf8().data(), rRect, bPersistent, bGlobal, bNLS);
01546 }
01547 
01548 void KConfigBase::writeEntry( const char *pKey, const QRect& rRect,
01549                               bool bPersistent, bool bGlobal,
01550                               bool bNLS )
01551 {
01552   QStrList list;
01553   QCString tempstr;
01554   list.insert( 0, tempstr.setNum( rRect.left() ) );
01555   list.insert( 1, tempstr.setNum( rRect.top() ) );
01556   list.insert( 2, tempstr.setNum( rRect.width() ) );
01557   list.insert( 3, tempstr.setNum( rRect.height() ) );
01558 
01559   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01560 }
01561 
01562 
01563 void KConfigBase::writeEntry( const QString& pKey, const QPoint& rPoint,
01564                               bool bPersistent, bool bGlobal,
01565                               bool bNLS )
01566 {
01567   writeEntry(pKey.utf8().data(), rPoint, bPersistent, bGlobal, bNLS);
01568 }
01569 
01570 void KConfigBase::writeEntry( const char *pKey, const QPoint& rPoint,
01571                               bool bPersistent, bool bGlobal,
01572                               bool bNLS )
01573 {
01574   QStrList list;
01575   QCString tempstr;
01576   list.insert( 0, tempstr.setNum( rPoint.x() ) );
01577   list.insert( 1, tempstr.setNum( rPoint.y() ) );
01578 
01579   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01580 }
01581 
01582 
01583 void KConfigBase::writeEntry( const QString& pKey, const QSize& rSize,
01584                               bool bPersistent, bool bGlobal,
01585                               bool bNLS )
01586 {
01587   writeEntry(pKey.utf8().data(), rSize, bPersistent, bGlobal, bNLS);
01588 }
01589 
01590 void KConfigBase::writeEntry( const char *pKey, const QSize& rSize,
01591                               bool bPersistent, bool bGlobal,
01592                               bool bNLS )
01593 {
01594   QStrList list;
01595   QCString tempstr;
01596   list.insert( 0, tempstr.setNum( rSize.width() ) );
01597   list.insert( 1, tempstr.setNum( rSize.height() ) );
01598 
01599   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01600 }
01601 
01602 void KConfigBase::writeEntry( const QString& pKey, const QColor& rColor,
01603                               bool bPersistent,
01604                               bool bGlobal,
01605                               bool bNLS  )
01606 {
01607   writeEntry( pKey.utf8().data(), rColor, bPersistent, bGlobal, bNLS);
01608 }
01609 
01610 void KConfigBase::writeEntry( const char *pKey, const QColor& rColor,
01611                               bool bPersistent,
01612                               bool bGlobal,
01613                               bool bNLS  )
01614 {
01615   QString aValue;
01616   if (rColor.isValid())
01617       aValue.sprintf( "%d,%d,%d", rColor.red(), rColor.green(), rColor.blue() );
01618   else
01619       aValue = "invalid";
01620 
01621   writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
01622 }
01623 
01624 void KConfigBase::writeEntry( const QString& pKey, const QDateTime& rDateTime,
01625                               bool bPersistent, bool bGlobal,
01626                               bool bNLS )
01627 {
01628   writeEntry(pKey.utf8().data(), rDateTime, bPersistent, bGlobal, bNLS);
01629 }
01630 
01631 void KConfigBase::writeEntry( const char *pKey, const QDateTime& rDateTime,
01632                               bool bPersistent, bool bGlobal,
01633                               bool bNLS )
01634 {
01635   QStrList list;
01636   QCString tempstr;
01637 
01638   QTime time = rDateTime.time();
01639   QDate date = rDateTime.date();
01640 
01641   list.insert( 0, tempstr.setNum( date.year() ) );
01642   list.insert( 1, tempstr.setNum( date.month() ) );
01643   list.insert( 2, tempstr.setNum( date.day() ) );
01644 
01645   list.insert( 3, tempstr.setNum( time.hour() ) );
01646   list.insert( 4, tempstr.setNum( time.minute() ) );
01647   list.insert( 5, tempstr.setNum( time.second() ) );
01648 
01649   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01650 }
01651 
01652 void KConfigBase::parseConfigFiles()
01653 {
01654   if (!bLocaleInitialized && KGlobal::_locale) {
01655     setLocale();
01656   }
01657   if (backEnd)
01658   {
01659      backEnd->parseConfigFiles();
01660      bReadOnly = (backEnd->getConfigState() == ReadOnly);
01661   }
01662 }
01663 
01664 void KConfigBase::sync()
01665 {
01666   if (isReadOnly())
01667     return;
01668 
01669   if (backEnd)
01670      backEnd->sync();
01671   if (bDirty)
01672     rollback();
01673 }
01674 
01675 KConfigBase::ConfigState KConfigBase::getConfigState() const {
01676     if (backEnd)
01677        return backEnd->getConfigState();
01678     return ReadOnly;
01679 }
01680 
01681 void KConfigBase::rollback( bool /*bDeep = true*/ )
01682 {
01683   bDirty = false;
01684 }
01685 
01686 
01687 void KConfigBase::setReadDefaults(bool b)
01688 {
01689   if (!d)
01690   {
01691      if (!b) return;
01692      d = new KConfigBasePrivate();
01693   }
01694 
01695   d->readDefaults = b;
01696 }
01697 
01698 bool KConfigBase::readDefaults() const
01699 {
01700   return (d && d->readDefaults);
01701 }
01702 
01703 void KConfigBase::revertToDefault(const QString &key)
01704 {
01705   setDirty(true);
01706 
01707   KEntryKey aEntryKey(mGroup, key.utf8());
01708   aEntryKey.bDefault = true;
01709 
01710   if (!locale().isNull()) {
01711     // try the localized key first
01712     aEntryKey.bLocal = true;
01713     KEntry entry = lookupData(aEntryKey);
01714     if (entry.mValue.isNull())
01715         entry.bDeleted = true;
01716 
01717     entry.bDirty = true;
01718     putData(aEntryKey, entry, true); // Revert
01719     aEntryKey.bLocal = false;
01720   }
01721 
01722   // try the non-localized version
01723   KEntry entry = lookupData(aEntryKey);
01724   if (entry.mValue.isNull())
01725      entry.bDeleted = true;
01726   entry.bDirty = true;
01727   putData(aEntryKey, entry, true); // Revert
01728 }
01729 
01730 bool KConfigBase::hasDefault(const QString &key) const
01731 {
01732   KEntryKey aEntryKey(mGroup, key.utf8());
01733   aEntryKey.bDefault = true;
01734 
01735   if (!locale().isNull()) {
01736     // try the localized key first
01737     aEntryKey.bLocal = true;
01738     KEntry entry = lookupData(aEntryKey);
01739     if (!entry.mValue.isNull())
01740         return true;
01741 
01742     aEntryKey.bLocal = false;
01743   }
01744 
01745   // try the non-localized version
01746   KEntry entry = lookupData(aEntryKey);
01747   if (!entry.mValue.isNull())
01748      return true;
01749 
01750   return false;
01751 }
01752 
01753 
01754 
01755 KConfigGroup::KConfigGroup(KConfigBase *master, const QString &group)
01756 {
01757   mMaster = master;
01758   backEnd = mMaster->backEnd; // Needed for getConfigState()
01759   bLocaleInitialized = true;
01760   bReadOnly = mMaster->bReadOnly;
01761   bExpand = false;
01762   bDirty = false; // Not used
01763   mGroup = group.utf8();
01764   aLocaleString = mMaster->aLocaleString;
01765   setReadDefaults(mMaster->readDefaults());
01766 }
01767 
01768 KConfigGroup::KConfigGroup(KConfigBase *master, const QCString &group)
01769 {
01770   mMaster = master;
01771   backEnd = mMaster->backEnd; // Needed for getConfigState()
01772   bLocaleInitialized = true;
01773   bReadOnly = mMaster->bReadOnly;
01774   bExpand = false;
01775   bDirty = false; // Not used
01776   mGroup = group;
01777   aLocaleString = mMaster->aLocaleString;
01778   setReadDefaults(mMaster->readDefaults());
01779 }
01780 
01781 KConfigGroup::KConfigGroup(KConfigBase *master, const char * group)
01782 {
01783   mMaster = master;
01784   backEnd = mMaster->backEnd; // Needed for getConfigState()
01785   bLocaleInitialized = true;
01786   bReadOnly = mMaster->bReadOnly;
01787   bExpand = false;
01788   bDirty = false; // Not used
01789   mGroup = group;
01790   aLocaleString = mMaster->aLocaleString;
01791   setReadDefaults(mMaster->readDefaults());
01792 }
01793 
01794 void KConfigGroup::deleteGroup(bool bGlobal)
01795 {
01796   mMaster->deleteGroup(KConfigBase::group(), true, bGlobal);
01797 }
01798 
01799 void KConfigGroup::setDirty(bool _bDirty)
01800 {
01801   mMaster->setDirty(_bDirty);
01802 }
01803 
01804 void KConfigGroup::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
01805 {
01806   mMaster->putData(_key, _data, _checkGroup);
01807 }
01808 
01809 KEntry KConfigGroup::lookupData(const KEntryKey &_key) const
01810 {
01811   return mMaster->lookupData(_key);
01812 }
01813 
01814 void KConfigGroup::sync()
01815 {
01816   mMaster->sync();
01817 }
01818 
01819 void KConfigBase::virtual_hook( int, void* )
01820 { /*BASE::virtual_hook( id, data );*/ }
01821 
01822 void KConfigGroup::virtual_hook( int id, void* data )
01823 { KConfigBase::virtual_hook( id, data ); }
01824 
01825 bool KConfigBase::checkConfigFilesWritable(bool warnUser)
01826 {
01827   if (backEnd)
01828     return backEnd->checkConfigFilesWritable(warnUser);
01829   else
01830     return false;
01831 }
01832 
01833 #include "kconfigbase.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.90.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 30 10:09:38 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003