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