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