tdeconfigbase.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 <tdeapplication.h> 00031 #include <tdeglobalsettings.h> 00032 #include <tdeglobal.h> 00033 #include <tdelocale.h> 00034 #include <kcharsets.h> 00035 00036 #include "tdeconfigbase.h" 00037 #include "tdeconfigbackend.h" 00038 #include "kdebug.h" 00039 #include "kstandarddirs.h" 00040 #include "kstringhandler.h" 00041 00042 class TDEConfigBase::TDEConfigBasePrivate 00043 { 00044 public: 00045 TDEConfigBasePrivate() : readDefaults(false) { }; 00046 00047 public: 00048 bool readDefaults; 00049 }; 00050 00051 TDEConfigBase::TDEConfigBase() 00052 : backEnd(0L), bDirty(false), bLocaleInitialized(false), 00053 bReadOnly(false), bExpand(false), d(0) 00054 { 00055 setGroup(TQString::null); 00056 } 00057 00058 TDEConfigBase::~TDEConfigBase() 00059 { 00060 delete d; 00061 } 00062 00063 void TDEConfigBase::setLocale() 00064 { 00065 bLocaleInitialized = true; 00066 00067 if (TDEGlobal::locale()) 00068 aLocaleString = TDEGlobal::locale()->language().utf8(); 00069 else 00070 aLocaleString = TDELocale::defaultLanguage().utf8(); 00071 if (backEnd) 00072 backEnd->setLocaleString(aLocaleString); 00073 } 00074 00075 TQString TDEConfigBase::locale() const 00076 { 00077 return TQString::fromUtf8(aLocaleString); 00078 } 00079 00080 void TDEConfigBase::setGroup( const TQString& group ) 00081 { 00082 if ( group.isEmpty() ) 00083 mGroup = "<default>"; 00084 else 00085 mGroup = group.utf8(); 00086 } 00087 00088 void TDEConfigBase::setGroup( const char *pGroup ) 00089 { 00090 setGroup(TQCString(pGroup)); 00091 } 00092 00093 void TDEConfigBase::setGroup( const TQCString &group ) 00094 { 00095 if ( group.isEmpty() ) 00096 mGroup = "<default>"; 00097 else 00098 mGroup = group; 00099 } 00100 00101 TQString TDEConfigBase::group() const { 00102 return TQString::fromUtf8(mGroup); 00103 } 00104 00105 void TDEConfigBase::setDesktopGroup() 00106 { 00107 mGroup = "Desktop Entry"; 00108 } 00109 00110 bool TDEConfigBase::hasKey(const TQString &key) const 00111 { 00112 return hasKey(key.utf8().data()); 00113 } 00114 00115 bool TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::hasGroup(const TQString &group) const 00154 { 00155 return internalHasGroup( group.utf8()); 00156 } 00157 00158 bool TDEConfigBase::hasGroup(const char *_pGroup) const 00159 { 00160 return internalHasGroup( TQCString(_pGroup)); 00161 } 00162 00163 bool TDEConfigBase::hasGroup(const TQCString &_pGroup) const 00164 { 00165 return internalHasGroup( _pGroup); 00166 } 00167 00168 bool TDEConfigBase::isImmutable() const 00169 { 00170 return (getConfigState() != ReadWrite); 00171 } 00172 00173 bool TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::readEntryUntranslated( const TQString& pKey, 00206 const TQString& aDefault ) const 00207 { 00208 return TDEConfigBase::readEntryUntranslated(pKey.utf8().data(), aDefault); 00209 } 00210 00211 00212 TQString TDEConfigBase::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 TDEConfigBase::readEntry( const TQString& pKey, 00223 const TQString& aDefault ) const 00224 { 00225 return TDEConfigBase::readEntry(pKey.utf8().data(), aDefault); 00226 } 00227 00228 TQString TDEConfigBase::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 TDEConfig, which will create a infinite 00234 // loop, and nobody likes those. 00235 if (!bLocaleInitialized && TDEGlobal::_locale) { 00236 // get around const'ness. 00237 TDEConfigBase *that = const_cast<TDEConfigBase *>(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 = TDEGlobal::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 = TDEGlobalSettings::desktopPath(); 00311 } 00312 else if (aVarName == "XDG_DOCUMENTS_DIR") { 00313 result = TDEGlobalSettings::documentPath(); 00314 } 00315 else if (aVarName == "XDG_DOWNLOAD_DIR") { 00316 result = TDEGlobalSettings::downloadPath(); 00317 } 00318 else if (aVarName == "XDG_MUSIC_DIR") { 00319 result = TDEGlobalSettings::musicPath(); 00320 } 00321 else if (aVarName == "XDG_PICTURES_DIR") { 00322 result = TDEGlobalSettings::picturesPath(); 00323 } 00324 else if (aVarName == "XDG_PUBLICSHARE_DIR") { 00325 result = TDEGlobalSettings::publicSharePath(); 00326 } 00327 else if (aVarName == "XDG_TEMPLATES_DIR") { 00328 result = TDEGlobalSettings::templatesPath(); 00329 } 00330 else if (aVarName == "XDG_VIDEOS_DIR") { 00331 result = TDEGlobalSettings::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 TDEConfigBase::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 TDEConfigBase::readPropertyEntry( const TQString& pKey, 00367 TQVariant::Type type ) const 00368 { 00369 return readPropertyEntry(pKey.utf8().data(), type); 00370 } 00371 00372 TQVariant TDEConfigBase::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 TDEConfigBase::readPropertyEntry( const TQString& pKey, 00382 const TQVariant &aDefault ) const 00383 { 00384 return readPropertyEntry(pKey.utf8().data(), aDefault); 00385 } 00386 00387 TQVariant TDEConfigBase::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 TDEConfigBase::readListEntry( const TQString& pKey, 00491 TQStrList &list, char sep ) const 00492 { 00493 return readListEntry(pKey.utf8().data(), list, sep); 00494 } 00495 00496 int TDEConfigBase::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 TDEConfigBase::readListEntry( const TQString& pKey, char sep ) const 00536 { 00537 return readListEntry(pKey.utf8().data(), sep); 00538 } 00539 00540 TQStringList TDEConfigBase::readListEntry( const char *pKey, char sep ) const 00541 { 00542 static const TQString& emptyString = TDEGlobal::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 TDEConfigBase::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> TDEConfigBase::readIntListEntry( const TQString& pKey ) const 00591 { 00592 return readIntListEntry(pKey.utf8().data()); 00593 } 00594 00595 TQValueList<int> TDEConfigBase::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 TDEConfigBase::readPathEntry( const TQString& pKey, const TQString& pDefault ) const 00609 { 00610 return readPathEntry(pKey.utf8().data(), pDefault); 00611 } 00612 00613 TQString TDEConfigBase::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 TDEConfigBase::readPathListEntry( const TQString& pKey, char sep ) const 00623 { 00624 return readPathListEntry(pKey.utf8().data(), sep); 00625 } 00626 00627 TQStringList TDEConfigBase::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 TDEConfigBase::readNumEntry( const TQString& pKey, int nDefault) const 00637 { 00638 return readNumEntry(pKey.utf8().data(), nDefault); 00639 } 00640 00641 int TDEConfigBase::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 TDEConfigBase::readUnsignedNumEntry( const TQString& pKey, unsigned int nDefault) const 00658 { 00659 return readUnsignedNumEntry(pKey.utf8().data(), nDefault); 00660 } 00661 00662 unsigned int TDEConfigBase::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 TDEConfigBase::readLongNumEntry( const TQString& pKey, long nDefault) const 00677 { 00678 return readLongNumEntry(pKey.utf8().data(), nDefault); 00679 } 00680 00681 long TDEConfigBase::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 TDEConfigBase::readUnsignedLongNumEntry( const TQString& pKey, unsigned long nDefault) const 00696 { 00697 return readUnsignedLongNumEntry(pKey.utf8().data(), nDefault); 00698 } 00699 00700 unsigned long TDEConfigBase::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 TDEConfigBase::readNum64Entry( const TQString& pKey, TQ_INT64 nDefault) const 00714 { 00715 return readNum64Entry(pKey.utf8().data(), nDefault); 00716 } 00717 00718 TQ_INT64 TDEConfigBase::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 TDEConfigBase::readUnsignedNum64Entry( const TQString& pKey, TQ_UINT64 nDefault) const 00734 { 00735 return readUnsignedNum64Entry(pKey.utf8().data(), nDefault); 00736 } 00737 00738 TQ_UINT64 TDEConfigBase::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 TDEConfigBase::readDoubleNumEntry( const TQString& pKey, double nDefault) const 00753 { 00754 return readDoubleNumEntry(pKey.utf8().data(), nDefault); 00755 } 00756 00757 double TDEConfigBase::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 TDEConfigBase::readBoolEntry( const TQString& pKey, bool bDefault ) const 00772 { 00773 return readBoolEntry(pKey.utf8().data(), bDefault); 00774 } 00775 00776 bool TDEConfigBase::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 TDEConfigBase::readFontEntry( const TQString& pKey, const TQFont* pDefault ) const 00799 { 00800 return readFontEntry(pKey.utf8().data(), pDefault); 00801 } 00802 00803 TQFont TDEConfigBase::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 TDEConfigBase::readRectEntry( const TQString& pKey, const TQRect* pDefault ) const 00896 { 00897 return readRectEntry(pKey.utf8().data(), pDefault); 00898 } 00899 00900 TQRect TDEConfigBase::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 TDEConfigBase::readPointEntry( const TQString& pKey, 00920 const TQPoint* pDefault ) const 00921 { 00922 return readPointEntry(pKey.utf8().data(), pDefault); 00923 } 00924 00925 TQPoint TDEConfigBase::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 TDEConfigBase::readSizeEntry( const TQString& pKey, 00945 const TQSize* pDefault ) const 00946 { 00947 return readSizeEntry(pKey.utf8().data(), pDefault); 00948 } 00949 00950 TQSize TDEConfigBase::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 TDEConfigBase::readColorEntry( const TQString& pKey, 00971 const TQColor* pDefault ) const 00972 { 00973 return readColorEntry(pKey.utf8().data(), pDefault); 00974 } 00975 00976 TQColor TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::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 TDEConfigBase::writeEntry( const char *pKey, const TQString& value, 01084 bool bPersistent, 01085 bool bGlobal, 01086 bool bNLS, 01087 bool bExpand ) 01088 { 01089 // the TDEConfig 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 && TDEGlobal::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 TDEConfigBase::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 TDEGlobal::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 TDEConfigBase::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 TDEConfigBase::writePathEntry( const char *pKey, const TQString & path, 01195 bool bPersistent, bool bGlobal, 01196 bool bNLS, bool expand) 01197 { 01198 writeEntry(pKey, translatePath(path), bPersistent, bGlobal, bNLS, expand); 01199 } 01200 01201 void TDEConfigBase::writePathEntry ( const TQString& pKey, const TQStringList &list, 01202 char sep , bool bPersistent, 01203 bool bGlobal, bool bNLS ) 01204 { 01205 writePathEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS); 01206 } 01207 01208 void TDEConfigBase::writePathEntry ( const char *pKey, const TQStringList &list, 01209 char sep , bool bPersistent, 01210 bool bGlobal, bool bNLS ) 01211 { 01212 if( list.isEmpty() ) 01213 { 01214 writeEntry( pKey, TQString::fromLatin1(""), bPersistent ); 01215 return; 01216 } 01217 TQStringList new_list; 01218 TQStringList::ConstIterator it = list.begin(); 01219 for( ; it != list.end(); ++it ) 01220 { 01221 TQString value = *it; 01222 new_list.append( translatePath(value) ); 01223 } 01224 writeEntry( pKey, new_list, sep, bPersistent, bGlobal, bNLS, true ); 01225 } 01226 01227 void TDEConfigBase::deleteEntry( const TQString& pKey, 01228 bool bNLS, 01229 bool bGlobal) 01230 { 01231 deleteEntry(pKey.utf8().data(), bNLS, bGlobal); 01232 } 01233 01234 void TDEConfigBase::deleteEntry( const char *pKey, 01235 bool bNLS, 01236 bool bGlobal) 01237 { 01238 // the TDEConfig object is dirty now 01239 // set this before any IO takes place so that if any derivative 01240 // classes do caching, they won't try and flush the cache out 01241 // from under us before we read. A race condition is still 01242 // possible but minimized. 01243 setDirty(true); 01244 01245 if (!bLocaleInitialized && TDEGlobal::locale()) 01246 setLocale(); 01247 01248 KEntryKey entryKey(mGroup, pKey); 01249 KEntry aEntryData; 01250 01251 aEntryData.bGlobal = bGlobal; 01252 aEntryData.bNLS = bNLS; 01253 aEntryData.bDirty = true; 01254 aEntryData.bDeleted = true; 01255 01256 // rewrite the new value 01257 putData(entryKey, aEntryData, true); 01258 } 01259 01260 bool TDEConfigBase::deleteGroup( const TQString& group, bool bDeep, bool bGlobal ) 01261 { 01262 KEntryMap aEntryMap = internalEntryMap(group); 01263 01264 if (!bDeep) { 01265 // Check if it empty 01266 return aEntryMap.isEmpty(); 01267 } 01268 01269 bool dirty = false; 01270 bool checkGroup = true; 01271 // we want to remove all entries in the group 01272 KEntryMapIterator aIt; 01273 for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt) 01274 { 01275 if (!aIt.key().mKey.isEmpty() && !aIt.key().bDefault && !(*aIt).bDeleted) 01276 { 01277 (*aIt).bDeleted = true; 01278 (*aIt).bDirty = true; 01279 (*aIt).bGlobal = bGlobal; 01280 (*aIt).mValue = 0; 01281 putData(aIt.key(), *aIt, checkGroup); 01282 checkGroup = false; 01283 dirty = true; 01284 } 01285 } 01286 if (dirty) 01287 setDirty(true); 01288 return true; 01289 } 01290 01291 void TDEConfigBase::writeEntry ( const TQString& pKey, const TQVariant &prop, 01292 bool bPersistent, 01293 bool bGlobal, bool bNLS ) 01294 { 01295 writeEntry(pKey.utf8().data(), prop, bPersistent, bGlobal, bNLS); 01296 } 01297 01298 void TDEConfigBase::writeEntry ( const char *pKey, const TQVariant &prop, 01299 bool bPersistent, 01300 bool bGlobal, bool bNLS ) 01301 { 01302 switch( prop.type() ) 01303 { 01304 case TQVariant::Invalid: 01305 writeEntry( pKey, "", bPersistent, bGlobal, bNLS ); 01306 return; 01307 case TQVariant::String: 01308 writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS ); 01309 return; 01310 case TQVariant::StringList: 01311 writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS ); 01312 return; 01313 case TQVariant::List: { 01314 TQValueList<TQVariant> list = prop.toList(); 01315 TQValueList<TQVariant>::ConstIterator it = list.begin(); 01316 TQValueList<TQVariant>::ConstIterator end = list.end(); 01317 TQStringList strList; 01318 01319 for (; it != end; ++it ) 01320 strList.append( (*it).toString() ); 01321 01322 writeEntry( pKey, strList, ',', bPersistent, bGlobal, bNLS ); 01323 01324 return; 01325 } 01326 case TQVariant::Font: 01327 writeEntry( pKey, prop.toFont(), bPersistent, bGlobal, bNLS ); 01328 return; 01329 case TQVariant::Point: 01330 writeEntry( pKey, prop.toPoint(), bPersistent, bGlobal, bNLS ); 01331 return; 01332 case TQVariant::Rect: 01333 writeEntry( pKey, prop.toRect(), bPersistent, bGlobal, bNLS ); 01334 return; 01335 case TQVariant::Size: 01336 writeEntry( pKey, prop.toSize(), bPersistent, bGlobal, bNLS ); 01337 return; 01338 case TQVariant::Color: 01339 writeEntry( pKey, prop.toColor(), bPersistent, bGlobal, bNLS ); 01340 return; 01341 case TQVariant::Int: 01342 writeEntry( pKey, prop.toInt(), bPersistent, bGlobal, bNLS ); 01343 return; 01344 case TQVariant::UInt: 01345 writeEntry( pKey, prop.toUInt(), bPersistent, bGlobal, bNLS ); 01346 return; 01347 case TQVariant::LongLong: 01348 writeEntry( pKey, prop.toLongLong(), bPersistent, bGlobal, bNLS ); 01349 return; 01350 case TQVariant::ULongLong: 01351 writeEntry( pKey, prop.toULongLong(), bPersistent, bGlobal, bNLS ); 01352 return; 01353 case TQVariant::Bool: 01354 writeEntry( pKey, prop.toBool(), bPersistent, bGlobal, bNLS ); 01355 return; 01356 case TQVariant::Double: 01357 writeEntry( pKey, prop.toDouble(), bPersistent, bGlobal, 'g', 6, bNLS ); 01358 return; 01359 case TQVariant::DateTime: 01360 writeEntry( pKey, prop.toDateTime(), bPersistent, bGlobal, bNLS); 01361 return; 01362 case TQVariant::Date: 01363 writeEntry( pKey, TQDateTime(prop.toDate()), bPersistent, bGlobal, bNLS); 01364 return; 01365 01366 case TQVariant::Pixmap: 01367 case TQVariant::Image: 01368 case TQVariant::Brush: 01369 case TQVariant::Palette: 01370 case TQVariant::ColorGroup: 01371 case TQVariant::Map: 01372 case TQVariant::IconSet: 01373 case TQVariant::CString: 01374 case TQVariant::PointArray: 01375 case TQVariant::Region: 01376 case TQVariant::Bitmap: 01377 case TQVariant::Cursor: 01378 case TQVariant::SizePolicy: 01379 case TQVariant::Time: 01380 #ifdef USE_QT3 01381 case TQVariant::ByteArray: 01382 #endif // USE_QT3 01383 case TQVariant::BitArray: 01384 case TQVariant::KeySequence: 01385 case TQVariant::Pen: 01386 #ifdef USE_QT4 01387 case TQVariant::Char: 01388 case TQVariant::Url: 01389 case TQVariant::Locale: 01390 case TQVariant::RectF: 01391 case TQVariant::SizeF: 01392 case TQVariant::Line: 01393 case TQVariant::LineF: 01394 case TQVariant::PointF: 01395 case TQVariant::RegExp: 01396 case TQVariant::Hash: 01397 case TQVariant::TextLength: 01398 case QVariant::TextFormat: 01399 case TQVariant::Matrix: 01400 case TQVariant::Transform: 01401 case TQVariant::Matrix4x4: 01402 case TQVariant::Vector2D: 01403 case TQVariant::Vector3D: 01404 case TQVariant::Vector4D: 01405 case TQVariant::Quaternion: 01406 case TQVariant::UserType: 01407 #endif // USE_QT4 01408 break; 01409 } 01410 01411 Q_ASSERT( 0 ); 01412 } 01413 01414 void TDEConfigBase::writeEntry ( const TQString& pKey, const TQStrList &list, 01415 char sep , bool bPersistent, 01416 bool bGlobal, bool bNLS ) 01417 { 01418 writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS); 01419 } 01420 01421 void TDEConfigBase::writeEntry ( const char *pKey, const TQStrList &list, 01422 char sep , bool bPersistent, 01423 bool bGlobal, bool bNLS ) 01424 { 01425 if( list.isEmpty() ) 01426 { 01427 writeEntry( pKey, TQString::fromLatin1(""), bPersistent ); 01428 return; 01429 } 01430 TQString str_list; 01431 TQStrListIterator it( list ); 01432 for( ; it.current(); ++it ) 01433 { 01434 uint i; 01435 TQString value; 01436 // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!! 01437 // A TQStrList may contain values in 8bit locale cpecified 01438 // encoding or in UTF8 encoding. 01439 value = KStringHandler::from8Bit(it.current()); 01440 uint strLengh(value.length()); 01441 for( i = 0; i < strLengh; i++ ) 01442 { 01443 if( value[i] == sep || value[i] == '\\' ) 01444 str_list += '\\'; 01445 str_list += value[i]; 01446 } 01447 str_list += sep; 01448 } 01449 if( str_list.at(str_list.length() - 1) == (QChar)sep ) 01450 str_list.truncate( str_list.length() -1 ); 01451 writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS ); 01452 } 01453 01454 void TDEConfigBase::writeEntry ( const TQString& pKey, const TQStringList &list, 01455 char sep , bool bPersistent, 01456 bool bGlobal, bool bNLS ) 01457 { 01458 writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS); 01459 } 01460 01461 void TDEConfigBase::writeEntry ( const char *pKey, const TQStringList &list, 01462 char sep , bool bPersistent, 01463 bool bGlobal, bool bNLS ) 01464 { 01465 writeEntry(pKey, list, sep, bPersistent, bGlobal, bNLS, false); 01466 } 01467 01468 void TDEConfigBase::writeEntry ( const char *pKey, const TQStringList &list, 01469 char sep, bool bPersistent, 01470 bool bGlobal, bool bNLS, bool bExpand ) 01471 { 01472 if( list.isEmpty() ) 01473 { 01474 writeEntry( pKey, TQString::fromLatin1(""), bPersistent ); 01475 return; 01476 } 01477 TQString str_list; 01478 str_list.reserve( 4096 ); 01479 TQStringList::ConstIterator it = list.begin(); 01480 for( ; it != list.end(); ++it ) 01481 { 01482 TQString value = *it; 01483 uint i; 01484 uint strLength(value.length()); 01485 for( i = 0; i < strLength; i++ ) 01486 { 01487 if( value[i] == sep || value[i] == '\\' ) 01488 str_list += '\\'; 01489 str_list += value[i]; 01490 } 01491 str_list += sep; 01492 } 01493 if( str_list.at(str_list.length() - 1) == (QChar)sep ) 01494 str_list.truncate( str_list.length() -1 ); 01495 writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS, bExpand ); 01496 } 01497 01498 void TDEConfigBase::writeEntry ( const TQString& pKey, const TQValueList<int> &list, 01499 bool bPersistent, bool bGlobal, bool bNLS ) 01500 { 01501 writeEntry(pKey.utf8().data(), list, bPersistent, bGlobal, bNLS); 01502 } 01503 01504 void TDEConfigBase::writeEntry ( const char *pKey, const TQValueList<int> &list, 01505 bool bPersistent, bool bGlobal, bool bNLS ) 01506 { 01507 TQStringList strlist; 01508 TQValueList<int>::ConstIterator end = list.end(); 01509 for (TQValueList<int>::ConstIterator it = list.begin(); it != end; it++) 01510 strlist << TQString::number(*it); 01511 writeEntry(pKey, strlist, ',', bPersistent, bGlobal, bNLS ); 01512 } 01513 01514 void TDEConfigBase::writeEntry( const TQString& pKey, int nValue, 01515 bool bPersistent, bool bGlobal, 01516 bool bNLS ) 01517 { 01518 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01519 } 01520 01521 void TDEConfigBase::writeEntry( const char *pKey, int nValue, 01522 bool bPersistent, bool bGlobal, 01523 bool bNLS ) 01524 { 01525 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01526 } 01527 01528 01529 void TDEConfigBase::writeEntry( const TQString& 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 void TDEConfigBase::writeEntry( const char *pKey, unsigned int nValue, 01537 bool bPersistent, bool bGlobal, 01538 bool bNLS ) 01539 { 01540 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01541 } 01542 01543 01544 void TDEConfigBase::writeEntry( const TQString& pKey, long nValue, 01545 bool bPersistent, bool bGlobal, 01546 bool bNLS ) 01547 { 01548 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01549 } 01550 01551 void TDEConfigBase::writeEntry( const char *pKey, long nValue, 01552 bool bPersistent, bool bGlobal, 01553 bool bNLS ) 01554 { 01555 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01556 } 01557 01558 01559 void TDEConfigBase::writeEntry( const TQString& 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 TDEConfigBase::writeEntry( const char *pKey, unsigned long nValue, 01567 bool bPersistent, bool bGlobal, 01568 bool bNLS ) 01569 { 01570 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01571 } 01572 01573 void TDEConfigBase::writeEntry( const TQString& 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 void TDEConfigBase::writeEntry( const char *pKey, TQ_INT64 nValue, 01581 bool bPersistent, bool bGlobal, 01582 bool bNLS ) 01583 { 01584 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01585 } 01586 01587 01588 void TDEConfigBase::writeEntry( const TQString& 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 TDEConfigBase::writeEntry( const char *pKey, TQ_UINT64 nValue, 01596 bool bPersistent, bool bGlobal, 01597 bool bNLS ) 01598 { 01599 writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS ); 01600 } 01601 01602 void TDEConfigBase::writeEntry( const TQString& pKey, double nValue, 01603 bool bPersistent, bool bGlobal, 01604 char format, int precision, 01605 bool bNLS ) 01606 { 01607 writeEntry( pKey, TQString::number(nValue, format, precision), 01608 bPersistent, bGlobal, bNLS ); 01609 } 01610 01611 void TDEConfigBase::writeEntry( const char *pKey, double nValue, 01612 bool bPersistent, bool bGlobal, 01613 char format, int precision, 01614 bool bNLS ) 01615 { 01616 writeEntry( pKey, TQString::number(nValue, format, precision), 01617 bPersistent, bGlobal, bNLS ); 01618 } 01619 01620 01621 void TDEConfigBase::writeEntry( const TQString& pKey, bool bValue, 01622 bool bPersistent, 01623 bool bGlobal, 01624 bool bNLS ) 01625 { 01626 writeEntry(pKey.utf8().data(), bValue, bPersistent, bGlobal, bNLS); 01627 } 01628 01629 void TDEConfigBase::writeEntry( const char *pKey, bool bValue, 01630 bool bPersistent, 01631 bool bGlobal, 01632 bool bNLS ) 01633 { 01634 TQString aValue; 01635 01636 if( bValue ) 01637 aValue = "true"; 01638 else 01639 aValue = "false"; 01640 01641 writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS ); 01642 } 01643 01644 01645 void TDEConfigBase::writeEntry( const TQString& pKey, const TQFont& rFont, 01646 bool bPersistent, bool bGlobal, 01647 bool bNLS ) 01648 { 01649 writeEntry(pKey.utf8().data(), rFont, bPersistent, bGlobal, bNLS); 01650 } 01651 01652 void TDEConfigBase::writeEntry( const char *pKey, const TQFont& rFont, 01653 bool bPersistent, bool bGlobal, 01654 bool bNLS ) 01655 { 01656 writeEntry( pKey, TQString(rFont.toString()), bPersistent, bGlobal, bNLS ); 01657 } 01658 01659 01660 void TDEConfigBase::writeEntry( const TQString& pKey, const TQRect& rRect, 01661 bool bPersistent, bool bGlobal, 01662 bool bNLS ) 01663 { 01664 writeEntry(pKey.utf8().data(), rRect, bPersistent, bGlobal, bNLS); 01665 } 01666 01667 void TDEConfigBase::writeEntry( const char *pKey, const TQRect& rRect, 01668 bool bPersistent, bool bGlobal, 01669 bool bNLS ) 01670 { 01671 TQStrList list; 01672 TQCString tempstr; 01673 list.insert( 0, tempstr.setNum( rRect.left() ) ); 01674 list.insert( 1, tempstr.setNum( rRect.top() ) ); 01675 list.insert( 2, tempstr.setNum( rRect.width() ) ); 01676 list.insert( 3, tempstr.setNum( rRect.height() ) ); 01677 01678 writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS ); 01679 } 01680 01681 01682 void TDEConfigBase::writeEntry( const TQString& pKey, const TQPoint& rPoint, 01683 bool bPersistent, bool bGlobal, 01684 bool bNLS ) 01685 { 01686 writeEntry(pKey.utf8().data(), rPoint, bPersistent, bGlobal, bNLS); 01687 } 01688 01689 void TDEConfigBase::writeEntry( const char *pKey, const TQPoint& rPoint, 01690 bool bPersistent, bool bGlobal, 01691 bool bNLS ) 01692 { 01693 TQStrList list; 01694 TQCString tempstr; 01695 list.insert( 0, tempstr.setNum( rPoint.x() ) ); 01696 list.insert( 1, tempstr.setNum( rPoint.y() ) ); 01697 01698 writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS ); 01699 } 01700 01701 01702 void TDEConfigBase::writeEntry( const TQString& pKey, const TQSize& rSize, 01703 bool bPersistent, bool bGlobal, 01704 bool bNLS ) 01705 { 01706 writeEntry(pKey.utf8().data(), rSize, bPersistent, bGlobal, bNLS); 01707 } 01708 01709 void TDEConfigBase::writeEntry( const char *pKey, const TQSize& rSize, 01710 bool bPersistent, bool bGlobal, 01711 bool bNLS ) 01712 { 01713 TQStrList list; 01714 TQCString tempstr; 01715 list.insert( 0, tempstr.setNum( rSize.width() ) ); 01716 list.insert( 1, tempstr.setNum( rSize.height() ) ); 01717 01718 writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS ); 01719 } 01720 01721 void TDEConfigBase::writeEntry( const TQString& pKey, const TQColor& rColor, 01722 bool bPersistent, 01723 bool bGlobal, 01724 bool bNLS ) 01725 { 01726 writeEntry( pKey.utf8().data(), rColor, bPersistent, bGlobal, bNLS); 01727 } 01728 01729 void TDEConfigBase::writeEntry( const char *pKey, const TQColor& rColor, 01730 bool bPersistent, 01731 bool bGlobal, 01732 bool bNLS ) 01733 { 01734 TQString aValue; 01735 if (rColor.isValid()) 01736 aValue.sprintf( "%d,%d,%d", rColor.red(), rColor.green(), rColor.blue() ); 01737 else 01738 aValue = "invalid"; 01739 01740 writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS ); 01741 } 01742 01743 void TDEConfigBase::writeEntry( const TQString& pKey, const TQDateTime& rDateTime, 01744 bool bPersistent, bool bGlobal, 01745 bool bNLS ) 01746 { 01747 writeEntry(pKey.utf8().data(), rDateTime, bPersistent, bGlobal, bNLS); 01748 } 01749 01750 void TDEConfigBase::writeEntry( const char *pKey, const TQDateTime& rDateTime, 01751 bool bPersistent, bool bGlobal, 01752 bool bNLS ) 01753 { 01754 TQStrList list; 01755 TQCString tempstr; 01756 01757 TQTime time = TQT_TQTIME_OBJECT(rDateTime.time()); 01758 TQDate date = TQT_TQDATE_OBJECT(rDateTime.date()); 01759 01760 list.insert( 0, tempstr.setNum( date.year() ) ); 01761 list.insert( 1, tempstr.setNum( date.month() ) ); 01762 list.insert( 2, tempstr.setNum( date.day() ) ); 01763 01764 list.insert( 3, tempstr.setNum( time.hour() ) ); 01765 list.insert( 4, tempstr.setNum( time.minute() ) ); 01766 list.insert( 5, tempstr.setNum( time.second() ) ); 01767 01768 writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS ); 01769 } 01770 01771 void TDEConfigBase::parseConfigFiles() 01772 { 01773 if (!bLocaleInitialized && TDEGlobal::_locale) { 01774 setLocale(); 01775 } 01776 if (backEnd) 01777 { 01778 backEnd->parseConfigFiles(); 01779 bReadOnly = (backEnd->getConfigState() == ReadOnly); 01780 } 01781 } 01782 01783 void TDEConfigBase::sync() 01784 { 01785 if (isReadOnly()) 01786 return; 01787 01788 if (backEnd) 01789 backEnd->sync(); 01790 if (bDirty) 01791 rollback(); 01792 } 01793 01794 TDEConfigBase::ConfigState TDEConfigBase::getConfigState() const { 01795 if (backEnd) 01796 return backEnd->getConfigState(); 01797 return ReadOnly; 01798 } 01799 01800 void TDEConfigBase::rollback( bool /*bDeep = true*/ ) 01801 { 01802 bDirty = false; 01803 } 01804 01805 01806 void TDEConfigBase::setReadDefaults(bool b) 01807 { 01808 if (!d) 01809 { 01810 if (!b) return; 01811 d = new TDEConfigBasePrivate(); 01812 } 01813 01814 d->readDefaults = b; 01815 } 01816 01817 bool TDEConfigBase::readDefaults() const 01818 { 01819 return (d && d->readDefaults); 01820 } 01821 01822 void TDEConfigBase::revertToDefault(const TQString &key) 01823 { 01824 setDirty(true); 01825 01826 KEntryKey aEntryKey(mGroup, key.utf8()); 01827 aEntryKey.bDefault = true; 01828 01829 if (!locale().isNull()) { 01830 // try the localized key first 01831 aEntryKey.bLocal = true; 01832 KEntry entry = lookupData(aEntryKey); 01833 if (entry.mValue.isNull()) 01834 entry.bDeleted = true; 01835 01836 entry.bDirty = true; 01837 putData(aEntryKey, entry, true); // Revert 01838 aEntryKey.bLocal = false; 01839 } 01840 01841 // try the non-localized version 01842 KEntry entry = lookupData(aEntryKey); 01843 if (entry.mValue.isNull()) 01844 entry.bDeleted = true; 01845 entry.bDirty = true; 01846 putData(aEntryKey, entry, true); // Revert 01847 } 01848 01849 bool TDEConfigBase::hasDefault(const TQString &key) const 01850 { 01851 KEntryKey aEntryKey(mGroup, key.utf8()); 01852 aEntryKey.bDefault = true; 01853 01854 if (!locale().isNull()) { 01855 // try the localized key first 01856 aEntryKey.bLocal = true; 01857 KEntry entry = lookupData(aEntryKey); 01858 if (!entry.mValue.isNull()) 01859 return true; 01860 01861 aEntryKey.bLocal = false; 01862 } 01863 01864 // try the non-localized version 01865 KEntry entry = lookupData(aEntryKey); 01866 if (!entry.mValue.isNull()) 01867 return true; 01868 01869 return false; 01870 } 01871 01872 01873 01874 TDEConfigGroup::TDEConfigGroup(TDEConfigBase *master, const TQString &group) 01875 { 01876 mMaster = master; 01877 backEnd = mMaster->backEnd; // Needed for getConfigState() 01878 bLocaleInitialized = true; 01879 bReadOnly = mMaster->bReadOnly; 01880 bExpand = false; 01881 bDirty = false; // Not used 01882 mGroup = group.utf8(); 01883 aLocaleString = mMaster->aLocaleString; 01884 setReadDefaults(mMaster->readDefaults()); 01885 } 01886 01887 TDEConfigGroup::TDEConfigGroup(TDEConfigBase *master, const TQCString &group) 01888 { 01889 mMaster = master; 01890 backEnd = mMaster->backEnd; // Needed for getConfigState() 01891 bLocaleInitialized = true; 01892 bReadOnly = mMaster->bReadOnly; 01893 bExpand = false; 01894 bDirty = false; // Not used 01895 mGroup = group; 01896 aLocaleString = mMaster->aLocaleString; 01897 setReadDefaults(mMaster->readDefaults()); 01898 } 01899 01900 TDEConfigGroup::TDEConfigGroup(TDEConfigBase *master, const char * group) 01901 { 01902 mMaster = master; 01903 backEnd = mMaster->backEnd; // Needed for getConfigState() 01904 bLocaleInitialized = true; 01905 bReadOnly = mMaster->bReadOnly; 01906 bExpand = false; 01907 bDirty = false; // Not used 01908 mGroup = group; 01909 aLocaleString = mMaster->aLocaleString; 01910 setReadDefaults(mMaster->readDefaults()); 01911 } 01912 01913 void TDEConfigGroup::deleteGroup(bool bGlobal) 01914 { 01915 mMaster->deleteGroup(TDEConfigBase::group(), true, bGlobal); 01916 } 01917 01918 bool TDEConfigGroup::groupIsImmutable() const 01919 { 01920 return mMaster->groupIsImmutable(TDEConfigBase::group()); 01921 } 01922 01923 void TDEConfigGroup::setDirty(bool _bDirty) 01924 { 01925 mMaster->setDirty(_bDirty); 01926 } 01927 01928 void TDEConfigGroup::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup) 01929 { 01930 mMaster->putData(_key, _data, _checkGroup); 01931 } 01932 01933 KEntry TDEConfigGroup::lookupData(const KEntryKey &_key) const 01934 { 01935 return mMaster->lookupData(_key); 01936 } 01937 01938 void TDEConfigGroup::sync() 01939 { 01940 mMaster->sync(); 01941 } 01942 01943 void TDEConfigBase::virtual_hook( int, void* ) 01944 { /*BASE::virtual_hook( id, data );*/ } 01945 01946 void TDEConfigGroup::virtual_hook( int id, void* data ) 01947 { TDEConfigBase::virtual_hook( id, data ); } 01948 01949 bool TDEConfigBase::checkConfigFilesWritable(bool warnUser) 01950 { 01951 if (backEnd) 01952 return backEnd->checkConfigFilesWritable(warnUser); 01953 else 01954 return false; 01955 } 01956 01957 #include "tdeconfigbase.moc"