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   // the KConfig object is dirty now
01044   // set this before any IO takes place so that if any derivative
01045   // classes do caching, they won't try and flush the cache out
01046   // from under us before we read. A race condition is still
01047   // possible but minimized.
01048   if( bPersistent )
01049     setDirty(true);
01050 
01051   if (!bLocaleInitialized && KGlobal::locale())
01052     setLocale();
01053 
01054   KEntryKey entryKey(mGroup, pKey);
01055   entryKey.bLocal = bNLS;
01056 
01057   KEntry aEntryData;
01058   aEntryData.mValue = value.utf8();  // set new value
01059   aEntryData.bGlobal = bGlobal;
01060   aEntryData.bNLS = bNLS;
01061 
01062   if (bPersistent)
01063     aEntryData.bDirty = true;
01064 
01065   // rewrite the new value
01066   putData(entryKey, aEntryData, true);
01067 }
01068 
01069 void KConfigBase::writePathEntry( const QString& pKey, const QString & path,
01070                                   bool bPersistent, bool bGlobal,
01071                                   bool bNLS)
01072 {
01073    writePathEntry(pKey.utf8().data(), path, bPersistent, bGlobal, bNLS);
01074 }
01075 
01076 
01077 static bool cleanHomeDirPath( QString &path, const QString &homeDir )
01078 {
01079 #ifdef Q_WS_WIN //safer
01080    if (!QDir::convertSeparators(path).startsWith(QDir::convertSeparators(homeDir)))
01081         return false;
01082 #else
01083    if (!path.startsWith(homeDir))
01084         return false;
01085 #endif
01086 
01087    unsigned int len = homeDir.length();
01088    // replace by "$HOME" if possible
01089    if (len && (path.length() == len || path[len] == '/')) {
01090         path.replace(0, len, QString::fromLatin1("$HOME"));
01091         return true;
01092    } else
01093         return false;
01094 }
01095 
01096 static QString translatePath( QString path )
01097 {
01098    if (path.isEmpty())
01099        return path;
01100 
01101    // only "our" $HOME should be interpreted
01102    path.replace('$', "$$");
01103 
01104    bool startsWithFile = path.startsWith("file:", false);
01105 
01106    // return original path, if it refers to another type of URL (e.g. http:/), or
01107    // if the path is already relative to another directory
01108    if (!startsWithFile && path[0] != '/' ||
01109         startsWithFile && path[5] != '/')
01110     return path;
01111 
01112    if (startsWithFile)
01113         path.remove(0,5); // strip leading "file:/" off the string
01114 
01115    // keep only one single '/' at the beginning - needed for cleanHomeDirPath()
01116    while (path[0] == '/' && path[1] == '/')
01117     path.remove(0,1);
01118 
01119    // we can not use KGlobal::dirs()->relativeLocation("home", path) here,
01120    // since it would not recognize paths without a trailing '/'.
01121    // All of the 3 following functions to return the user's home directory
01122    // can return different paths. We have to test all them.
01123    QString homeDir0 = QFile::decodeName(getenv("HOME"));
01124    QString homeDir1 = QDir::homeDirPath();
01125    QString homeDir2 = QDir(homeDir1).canonicalPath();
01126    if (cleanHomeDirPath(path, homeDir0) ||
01127        cleanHomeDirPath(path, homeDir1) ||
01128        cleanHomeDirPath(path, homeDir2) ) {
01129      // kdDebug() << "Path was replaced\n";
01130    }
01131 
01132    if (startsWithFile)
01133       path.prepend( "file://" );
01134 
01135    return path;
01136 }
01137 
01138 void KConfigBase::writePathEntry( const char *pKey, const QString & path,
01139                                   bool bPersistent, bool bGlobal,
01140                                   bool bNLS)
01141 {
01142    writeEntry(pKey, translatePath(path), bPersistent, bGlobal, bNLS);
01143 }
01144 
01145 void KConfigBase::writePathEntry ( const QString& pKey, const QStringList &list,
01146                                char sep , bool bPersistent,
01147                                bool bGlobal, bool bNLS )
01148 {
01149   writePathEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01150 }
01151 
01152 void KConfigBase::writePathEntry ( const char *pKey, const QStringList &list,
01153                                char sep , bool bPersistent,
01154                                bool bGlobal, bool bNLS )
01155 {
01156   if( list.isEmpty() )
01157     {
01158       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01159       return;
01160     }
01161   QStringList new_list;
01162   QStringList::ConstIterator it = list.begin();
01163   for( ; it != list.end(); ++it )
01164     {
01165       QString value = *it;
01166       new_list.append( translatePath(value) );
01167     }
01168   writeEntry( pKey, new_list, sep, bPersistent, bGlobal, bNLS );
01169 }
01170 
01171 void KConfigBase::deleteEntry( const QString& pKey,
01172                                  bool bNLS,
01173                                  bool bGlobal)
01174 {
01175    deleteEntry(pKey.utf8().data(), bNLS, bGlobal);
01176 }
01177 
01178 void KConfigBase::deleteEntry( const char *pKey,
01179                                  bool bNLS,
01180                                  bool bGlobal)
01181 {
01182   // the KConfig object is dirty now
01183   // set this before any IO takes place so that if any derivative
01184   // classes do caching, they won't try and flush the cache out
01185   // from under us before we read. A race condition is still
01186   // possible but minimized.
01187   setDirty(true);
01188 
01189   if (!bLocaleInitialized && KGlobal::locale())
01190     setLocale();
01191 
01192   KEntryKey entryKey(mGroup, pKey);
01193   KEntry aEntryData;
01194 
01195   aEntryData.bGlobal = bGlobal;
01196   aEntryData.bNLS = bNLS;
01197   aEntryData.bDirty = true;
01198   aEntryData.bDeleted = true;
01199 
01200   // rewrite the new value
01201   putData(entryKey, aEntryData, true);
01202 }
01203 
01204 bool KConfigBase::deleteGroup( const QString& group, bool bDeep, bool bGlobal )
01205 {
01206   KEntryMap aEntryMap = internalEntryMap(group);
01207 
01208   if (!bDeep) {
01209     // Check if it empty
01210     return aEntryMap.isEmpty();
01211   }
01212 
01213   bool dirty = false;
01214   bool checkGroup = true;
01215   // we want to remove all entries in the group
01216   KEntryMapIterator aIt;
01217   for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt)
01218   {
01219     if (!aIt.key().mKey.isEmpty() && !aIt.key().bDefault && !(*aIt).bDeleted)
01220     {
01221       (*aIt).bDeleted = true;
01222       (*aIt).bDirty = true;
01223       (*aIt).bGlobal = bGlobal;
01224       (*aIt).mValue = 0;
01225       putData(aIt.key(), *aIt, checkGroup);
01226       checkGroup = false;
01227       dirty = true;
01228     }
01229   }
01230   if (dirty)
01231      setDirty(true);
01232   return true;
01233 }
01234 
01235 void KConfigBase::writeEntry ( const QString& pKey, const QVariant &prop,
01236                                bool bPersistent,
01237                                bool bGlobal, bool bNLS )
01238 {
01239   writeEntry(pKey.utf8().data(), prop, bPersistent, bGlobal, bNLS);
01240 }
01241 
01242 void KConfigBase::writeEntry ( const char *pKey, const QVariant &prop,
01243                                bool bPersistent,
01244                                bool bGlobal, bool bNLS )
01245 {
01246   switch( prop.type() )
01247     {
01248     case QVariant::Invalid:
01249       writeEntry( pKey, "", bPersistent, bGlobal, bNLS );
01250       return;
01251     case QVariant::String:
01252       writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
01253       return;
01254     case QVariant::StringList:
01255       writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
01256       return;
01257     case QVariant::List: {
01258         QValueList<QVariant> list = prop.toList();
01259         QValueList<QVariant>::ConstIterator it = list.begin();
01260         QValueList<QVariant>::ConstIterator end = list.end();
01261         QStringList strList;
01262 
01263         for (; it != end; ++it )
01264             strList.append( (*it).toString() );
01265 
01266         writeEntry( pKey, strList, ',', bPersistent, bGlobal, bNLS );
01267 
01268         return;
01269     }
01270     case QVariant::Font:
01271       writeEntry( pKey, prop.toFont(), bPersistent, bGlobal, bNLS );
01272       return;
01273     case QVariant::Point:
01274       writeEntry( pKey, prop.toPoint(), bPersistent, bGlobal, bNLS );
01275       return;
01276     case QVariant::Rect:
01277       writeEntry( pKey, prop.toRect(), bPersistent, bGlobal, bNLS );
01278       return;
01279     case QVariant::Size:
01280       writeEntry( pKey, prop.toSize(), bPersistent, bGlobal, bNLS );
01281       return;
01282     case QVariant::Color:
01283       writeEntry( pKey, prop.toColor(), bPersistent, bGlobal, bNLS );
01284       return;
01285     case QVariant::Int:
01286       writeEntry( pKey, prop.toInt(), bPersistent, bGlobal, bNLS );
01287       return;
01288     case QVariant::UInt:
01289       writeEntry( pKey, prop.toUInt(), bPersistent, bGlobal, bNLS );
01290       return;
01291     case QVariant::LongLong:
01292       writeEntry( pKey, prop.toLongLong(), bPersistent, bGlobal, bNLS );
01293       return;
01294     case QVariant::ULongLong:
01295       writeEntry( pKey, prop.toULongLong(), bPersistent, bGlobal, bNLS );
01296       return;
01297     case QVariant::Bool:
01298       writeEntry( pKey, prop.toBool(), bPersistent, bGlobal, bNLS );
01299       return;
01300     case QVariant::Double:
01301       writeEntry( pKey, prop.toDouble(), bPersistent, bGlobal, 'g', 6, bNLS );
01302       return;
01303     case QVariant::DateTime:
01304       writeEntry( pKey, prop.toDateTime(), bPersistent, bGlobal, bNLS);
01305       return;
01306     case QVariant::Date:
01307       writeEntry( pKey, QDateTime(prop.toDate()), bPersistent, bGlobal, bNLS);
01308       return;
01309 
01310     case QVariant::Pixmap:
01311     case QVariant::Image:
01312     case QVariant::Brush:
01313     case QVariant::Palette:
01314     case QVariant::ColorGroup:
01315     case QVariant::Map:
01316     case QVariant::IconSet:
01317     case QVariant::CString:
01318     case QVariant::PointArray:
01319     case QVariant::Region:
01320     case QVariant::Bitmap:
01321     case QVariant::Cursor:
01322     case QVariant::SizePolicy:
01323     case QVariant::Time:
01324     case QVariant::ByteArray:
01325     case QVariant::BitArray:
01326     case QVariant::KeySequence:
01327     case QVariant::Pen:
01328         break;
01329     }
01330 
01331   Q_ASSERT( 0 );
01332 }
01333 
01334 void KConfigBase::writeEntry ( const QString& pKey, const QStrList &list,
01335                                char sep , bool bPersistent,
01336                                bool bGlobal, bool bNLS )
01337 {
01338   writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01339 }
01340 
01341 void KConfigBase::writeEntry ( const char *pKey, const QStrList &list,
01342                                char sep , bool bPersistent,
01343                                bool bGlobal, bool bNLS )
01344 {
01345   if( list.isEmpty() )
01346     {
01347       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01348       return;
01349     }
01350   QString str_list;
01351   QStrListIterator it( list );
01352   for( ; it.current(); ++it )
01353     {
01354       uint i;
01355       QString value;
01356       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
01357       // A QStrList may contain values in 8bit locale cpecified
01358       // encoding or in UTF8 encoding.
01359       value = KStringHandler::from8Bit(it.current());
01360       uint strLengh(value.length());
01361       for( i = 0; i < strLengh; i++ )
01362         {
01363           if( value[i] == sep || value[i] == '\\' )
01364             str_list += '\\';
01365           str_list += value[i];
01366         }
01367       str_list += sep;
01368     }
01369   if( str_list.at(str_list.length() - 1) == sep )
01370     str_list.truncate( str_list.length() -1 );
01371   writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS );
01372 }
01373 
01374 void KConfigBase::writeEntry ( const QString& pKey, const QStringList &list,
01375                                char sep , bool bPersistent,
01376                                bool bGlobal, bool bNLS )
01377 {
01378   writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01379 }
01380 
01381 void KConfigBase::writeEntry ( const char *pKey, const QStringList &list,
01382                                char sep , bool bPersistent,
01383                                bool bGlobal, bool bNLS )
01384 {
01385   if( list.isEmpty() )
01386     {
01387       writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01388       return;
01389     }
01390   QString str_list;
01391   str_list.reserve( 4096 );
01392   QStringList::ConstIterator it = list.begin();
01393   for( ; it != list.end(); ++it )
01394     {
01395       QString value = *it;
01396       uint i;
01397       uint strLength(value.length());
01398       for( i = 0; i < strLength; i++ )
01399         {
01400           if( value[i] == sep || value[i] == '\\' )
01401             str_list += '\\';
01402           str_list += value[i];
01403         }
01404       str_list += sep;
01405     }
01406   if( str_list.at(str_list.length() - 1) == sep )
01407     str_list.truncate( str_list.length() -1 );
01408   writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS );
01409 }
01410 
01411 void KConfigBase::writeEntry ( const QString& pKey, const QValueList<int> &list,
01412                                bool bPersistent, bool bGlobal, bool bNLS )
01413 {
01414   writeEntry(pKey.utf8().data(), list, bPersistent, bGlobal, bNLS);
01415 }
01416 
01417 void KConfigBase::writeEntry ( const char *pKey, const QValueList<int> &list,
01418                                bool bPersistent, bool bGlobal, bool bNLS )
01419 {
01420     QStringList strlist;
01421     QValueList<int>::ConstIterator end = list.end();
01422     for (QValueList<int>::ConstIterator it = list.begin(); it != end; it++)
01423         strlist << QString::number(*it);
01424     writeEntry(pKey, strlist, ',', bPersistent, bGlobal, bNLS );
01425 }
01426 
01427 void KConfigBase::writeEntry( const QString& pKey, int nValue,
01428                                  bool bPersistent, bool bGlobal,
01429                                  bool bNLS )
01430 {
01431   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01432 }
01433 
01434 void KConfigBase::writeEntry( const char *pKey, int nValue,
01435                                  bool bPersistent, bool bGlobal,
01436                                  bool bNLS )
01437 {
01438   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01439 }
01440 
01441 
01442 void KConfigBase::writeEntry( const QString& pKey, unsigned int nValue,
01443                                  bool bPersistent, bool bGlobal,
01444                                  bool bNLS )
01445 {
01446   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01447 }
01448 
01449 void KConfigBase::writeEntry( const char *pKey, unsigned int nValue,
01450                                  bool bPersistent, bool bGlobal,
01451                                  bool bNLS )
01452 {
01453   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01454 }
01455 
01456 
01457 void KConfigBase::writeEntry( const QString& pKey, long nValue,
01458                                  bool bPersistent, bool bGlobal,
01459                                  bool bNLS )
01460 {
01461   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01462 }
01463 
01464 void KConfigBase::writeEntry( const char *pKey, long nValue,
01465                                  bool bPersistent, bool bGlobal,
01466                                  bool bNLS )
01467 {
01468   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01469 }
01470 
01471 
01472 void KConfigBase::writeEntry( const QString& pKey, unsigned long nValue,
01473                                  bool bPersistent, bool bGlobal,
01474                                  bool bNLS )
01475 {
01476   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01477 }
01478 
01479 void KConfigBase::writeEntry( const char *pKey, unsigned long nValue,
01480                                  bool bPersistent, bool bGlobal,
01481                                  bool bNLS )
01482 {
01483   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01484 }
01485 
01486 void KConfigBase::writeEntry( const QString& pKey, Q_INT64 nValue,
01487                                  bool bPersistent, bool bGlobal,
01488                                  bool bNLS )
01489 {
01490   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01491 }
01492 
01493 void KConfigBase::writeEntry( const char *pKey, Q_INT64 nValue,
01494                                  bool bPersistent, bool bGlobal,
01495                                  bool bNLS )
01496 {
01497   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01498 }
01499 
01500 
01501 void KConfigBase::writeEntry( const QString& pKey, Q_UINT64 nValue,
01502                                  bool bPersistent, bool bGlobal,
01503                                  bool bNLS )
01504 {
01505   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01506 }
01507 
01508 void KConfigBase::writeEntry( const char *pKey, Q_UINT64 nValue,
01509                                  bool bPersistent, bool bGlobal,
01510                                  bool bNLS )
01511 {
01512   writeEntry( pKey, QString::number(nValue), bPersistent, bGlobal, bNLS );
01513 }
01514 
01515 void KConfigBase::writeEntry( const QString& pKey, double nValue,
01516                                  bool bPersistent, bool bGlobal,
01517                                  char format, int precision,
01518                                  bool bNLS )
01519 {
01520   writeEntry( pKey, QString::number(nValue, format, precision),
01521                      bPersistent, bGlobal, bNLS );
01522 }
01523 
01524 void KConfigBase::writeEntry( const char *pKey, double nValue,
01525                                  bool bPersistent, bool bGlobal,
01526                                  char format, int precision,
01527                                  bool bNLS )
01528 {
01529   writeEntry( pKey, QString::number(nValue, format, precision),
01530                      bPersistent, bGlobal, bNLS );
01531 }
01532 
01533 
01534 void KConfigBase::writeEntry( const QString& pKey, bool bValue,
01535                                  bool bPersistent,
01536                                  bool bGlobal,
01537                                  bool bNLS )
01538 {
01539   writeEntry(pKey.utf8().data(), bValue, bPersistent, bGlobal, bNLS);
01540 }
01541 
01542 void KConfigBase::writeEntry( const char *pKey, bool bValue,
01543                                  bool bPersistent,
01544                                  bool bGlobal,
01545                                  bool bNLS )
01546 {
01547   QString aValue;
01548 
01549   if( bValue )
01550     aValue = "true";
01551   else
01552     aValue = "false";
01553 
01554   writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
01555 }
01556 
01557 
01558 void KConfigBase::writeEntry( const QString& pKey, const QFont& rFont,
01559                                  bool bPersistent, bool bGlobal,
01560                                  bool bNLS )
01561 {
01562   writeEntry(pKey.utf8().data(), rFont, bPersistent, bGlobal, bNLS);
01563 }
01564 
01565 void KConfigBase::writeEntry( const char *pKey, const QFont& rFont,
01566                                  bool bPersistent, bool bGlobal,
01567                                  bool bNLS )
01568 {
01569   writeEntry( pKey, rFont.toString(), bPersistent, bGlobal, bNLS );
01570 }
01571 
01572 
01573 void KConfigBase::writeEntry( const QString& pKey, const QRect& rRect,
01574                               bool bPersistent, bool bGlobal,
01575                               bool bNLS )
01576 {
01577   writeEntry(pKey.utf8().data(), rRect, bPersistent, bGlobal, bNLS);
01578 }
01579 
01580 void KConfigBase::writeEntry( const char *pKey, const QRect& rRect,
01581                               bool bPersistent, bool bGlobal,
01582                               bool bNLS )
01583 {
01584   QStrList list;
01585   QCString tempstr;
01586   list.insert( 0, tempstr.setNum( rRect.left() ) );
01587   list.insert( 1, tempstr.setNum( rRect.top() ) );
01588   list.insert( 2, tempstr.setNum( rRect.width() ) );
01589   list.insert( 3, tempstr.setNum( rRect.height() ) );
01590 
01591   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01592 }
01593 
01594 
01595 void KConfigBase::writeEntry( const QString& pKey, const QPoint& rPoint,
01596                               bool bPersistent, bool bGlobal,
01597                               bool bNLS )
01598 {
01599   writeEntry(pKey.utf8().data(), rPoint, bPersistent, bGlobal, bNLS);
01600 }
01601 
01602 void KConfigBase::writeEntry( const char *pKey, const QPoint& rPoint,
01603                               bool bPersistent, bool bGlobal,
01604                               bool bNLS )
01605 {
01606   QStrList list;
01607   QCString tempstr;
01608   list.insert( 0, tempstr.setNum( rPoint.x() ) );
01609   list.insert( 1, tempstr.setNum( rPoint.y() ) );
01610 
01611   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01612 }
01613 
01614 
01615 void KConfigBase::writeEntry( const QString& pKey, const QSize& rSize,
01616                               bool bPersistent, bool bGlobal,
01617                               bool bNLS )
01618 {
01619   writeEntry(pKey.utf8().data(), rSize, bPersistent, bGlobal, bNLS);
01620 }
01621 
01622 void KConfigBase::writeEntry( const char *pKey, const QSize& rSize,
01623                               bool bPersistent, bool bGlobal,
01624                               bool bNLS )
01625 {
01626   QStrList list;
01627   QCString tempstr;
01628   list.insert( 0, tempstr.setNum( rSize.width() ) );
01629   list.insert( 1, tempstr.setNum( rSize.height() ) );
01630 
01631   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01632 }
01633 
01634 void KConfigBase::writeEntry( const QString& pKey, const QColor& rColor,
01635                               bool bPersistent,
01636                               bool bGlobal,
01637                               bool bNLS  )
01638 {
01639   writeEntry( pKey.utf8().data(), rColor, bPersistent, bGlobal, bNLS);
01640 }
01641 
01642 void KConfigBase::writeEntry( const char *pKey, const QColor& rColor,
01643                               bool bPersistent,
01644                               bool bGlobal,
01645                               bool bNLS  )
01646 {
01647   QString aValue;
01648   if (rColor.isValid())
01649       aValue.sprintf( "%d,%d,%d", rColor.red(), rColor.green(), rColor.blue() );
01650   else
01651       aValue = "invalid";
01652 
01653   writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
01654 }
01655 
01656 void KConfigBase::writeEntry( const QString& pKey, const QDateTime& rDateTime,
01657                               bool bPersistent, bool bGlobal,
01658                               bool bNLS )
01659 {
01660   writeEntry(pKey.utf8().data(), rDateTime, bPersistent, bGlobal, bNLS);
01661 }
01662 
01663 void KConfigBase::writeEntry( const char *pKey, const QDateTime& rDateTime,
01664                               bool bPersistent, bool bGlobal,
01665                               bool bNLS )
01666 {
01667   QStrList list;
01668   QCString tempstr;
01669 
01670   QTime time = rDateTime.time();
01671   QDate date = rDateTime.date();
01672 
01673   list.insert( 0, tempstr.setNum( date.year() ) );
01674   list.insert( 1, tempstr.setNum( date.month() ) );
01675   list.insert( 2, tempstr.setNum( date.day() ) );
01676 
01677   list.insert( 3, tempstr.setNum( time.hour() ) );
01678   list.insert( 4, tempstr.setNum( time.minute() ) );
01679   list.insert( 5, tempstr.setNum( time.second() ) );
01680 
01681   writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
01682 }
01683 
01684 void KConfigBase::parseConfigFiles()
01685 {
01686   if (!bLocaleInitialized && KGlobal::_locale) {
01687     setLocale();
01688   }
01689   if (backEnd)
01690   {
01691      backEnd->parseConfigFiles();
01692      bReadOnly = (backEnd->getConfigState() == ReadOnly);
01693   }
01694 }
01695 
01696 void KConfigBase::sync()
01697 {
01698   if (isReadOnly())
01699     return;
01700 
01701   if (backEnd)
01702      backEnd->sync();
01703   if (bDirty)
01704     rollback();
01705 }
01706 
01707 KConfigBase::ConfigState KConfigBase::getConfigState() const {
01708     if (backEnd)
01709        return backEnd->getConfigState();
01710     return ReadOnly;
01711 }
01712 
01713 void KConfigBase::rollback( bool /*bDeep = true*/ )
01714 {
01715   bDirty = false;
01716 }
01717 
01718 
01719 void KConfigBase::setReadDefaults(bool b)
01720 {
01721   if (!d)
01722   {
01723      if (!b) return;
01724      d = new KConfigBasePrivate();
01725   }
01726 
01727   d->readDefaults = b;
01728 }
01729 
01730 bool KConfigBase::readDefaults() const
01731 {
01732   return (d && d->readDefaults);
01733 }
01734 
01735 void KConfigBase::revertToDefault(const QString &key)
01736 {
01737   setDirty(true);
01738 
01739   KEntryKey aEntryKey(mGroup, key.utf8());
01740   aEntryKey.bDefault = true;
01741 
01742   if (!locale().isNull()) {
01743     // try the localized key first
01744     aEntryKey.bLocal = true;
01745     KEntry entry = lookupData(aEntryKey);
01746     if (entry.mValue.isNull())
01747         entry.bDeleted = true;
01748 
01749     entry.bDirty = true;
01750     putData(aEntryKey, entry, true); // Revert
01751     aEntryKey.bLocal = false;
01752   }
01753 
01754   // try the non-localized version
01755   KEntry entry = lookupData(aEntryKey);
01756   if (entry.mValue.isNull())
01757      entry.bDeleted = true;
01758   entry.bDirty = true;
01759   putData(aEntryKey, entry, true); // Revert
01760 }
01761 
01762 bool KConfigBase::hasDefault(const QString &key) const
01763 {
01764   KEntryKey aEntryKey(mGroup, key.utf8());
01765   aEntryKey.bDefault = true;
01766 
01767   if (!locale().isNull()) {
01768     // try the localized key first
01769     aEntryKey.bLocal = true;
01770     KEntry entry = lookupData(aEntryKey);
01771     if (!entry.mValue.isNull())
01772         return true;
01773 
01774     aEntryKey.bLocal = false;
01775   }
01776 
01777   // try the non-localized version
01778   KEntry entry = lookupData(aEntryKey);
01779   if (!entry.mValue.isNull())
01780      return true;
01781 
01782   return false;
01783 }
01784 
01785 
01786 
01787 KConfigGroup::KConfigGroup(KConfigBase *master, const QString &group)
01788 {
01789   mMaster = master;
01790   backEnd = mMaster->backEnd; // Needed for getConfigState()
01791   bLocaleInitialized = true;
01792   bReadOnly = mMaster->bReadOnly;
01793   bExpand = false;
01794   bDirty = false; // Not used
01795   mGroup = group.utf8();
01796   aLocaleString = mMaster->aLocaleString;
01797   setReadDefaults(mMaster->readDefaults());
01798 }
01799 
01800 KConfigGroup::KConfigGroup(KConfigBase *master, const QCString &group)
01801 {
01802   mMaster = master;
01803   backEnd = mMaster->backEnd; // Needed for getConfigState()
01804   bLocaleInitialized = true;
01805   bReadOnly = mMaster->bReadOnly;
01806   bExpand = false;
01807   bDirty = false; // Not used
01808   mGroup = group;
01809   aLocaleString = mMaster->aLocaleString;
01810   setReadDefaults(mMaster->readDefaults());
01811 }
01812 
01813 KConfigGroup::KConfigGroup(KConfigBase *master, const char * group)
01814 {
01815   mMaster = master;
01816   backEnd = mMaster->backEnd; // Needed for getConfigState()
01817   bLocaleInitialized = true;
01818   bReadOnly = mMaster->bReadOnly;
01819   bExpand = false;
01820   bDirty = false; // Not used
01821   mGroup = group;
01822   aLocaleString = mMaster->aLocaleString;
01823   setReadDefaults(mMaster->readDefaults());
01824 }
01825 
01826 void KConfigGroup::deleteGroup(bool bGlobal)
01827 {
01828   mMaster->deleteGroup(KConfigBase::group(), true, bGlobal);
01829 }
01830 
01831 bool KConfigGroup::groupIsImmutable() const
01832 {
01833     return mMaster->groupIsImmutable(KConfigBase::group());
01834 }
01835 
01836 void KConfigGroup::setDirty(bool _bDirty)
01837 {
01838   mMaster->setDirty(_bDirty);
01839 }
01840 
01841 void KConfigGroup::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
01842 {
01843   mMaster->putData(_key, _data, _checkGroup);
01844 }
01845 
01846 KEntry KConfigGroup::lookupData(const KEntryKey &_key) const
01847 {
01848   return mMaster->lookupData(_key);
01849 }
01850 
01851 void KConfigGroup::sync()
01852 {
01853   mMaster->sync();
01854 }
01855 
01856 void KConfigBase::virtual_hook( int, void* )
01857 { /*BASE::virtual_hook( id, data );*/ }
01858 
01859 void KConfigGroup::virtual_hook( int id, void* data )
01860 { KConfigBase::virtual_hook( id, data ); }
01861 
01862 bool KConfigBase::checkConfigFilesWritable(bool warnUser)
01863 {
01864   if (backEnd)
01865     return backEnd->checkConfigFilesWritable(warnUser);
01866   else
01867     return false;
01868 }
01869 
01870 #include "kconfigbase.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys