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