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