kconfigbase.cpp

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