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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:24 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001