kdecore Library: kconfigbase.cpp Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kconfigbase.cpp

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

This file is part of the documentation for kdecore Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:18:03 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003