00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026 #include <qfile.h>
00027 #include <qdir.h>
00028 #include <qtextstream.h>
00029
00030 #include <kapplication.h>
00031 #include <kglobal.h>
00032 #include <klocale.h>
00033 #include <kcharsets.h>
00034
00035 #include "kconfigbase.h"
00036 #include "kconfigbackend.h"
00037 #include "kdebug.h"
00038 #include "kstandarddirs.h"
00039 #include "kstringhandler.h"
00040
00041 class KConfigBase::KConfigBasePrivate
00042 {
00043 public:
00044 KConfigBasePrivate() : readDefaults(false) { };
00045
00046 public:
00047 bool readDefaults;
00048 };
00049
00050 KConfigBase::KConfigBase()
00051 : backEnd(0L), bDirty(false), bLocaleInitialized(false),
00052 bReadOnly(false), bExpand(false), d(0)
00053 {
00054 setGroup(QString::null);
00055 }
00056
00057 KConfigBase::~KConfigBase()
00058 {
00059 delete d;
00060 }
00061
00062 void KConfigBase::setLocale()
00063 {
00064 bLocaleInitialized = true;
00065
00066 if (KGlobal::locale())
00067 aLocaleString = KGlobal::locale()->language().utf8();
00068 else
00069 aLocaleString = KLocale::defaultLanguage().utf8();
00070 if (backEnd)
00071 backEnd->setLocaleString(aLocaleString);
00072 }
00073
00074 QString KConfigBase::locale() const
00075 {
00076 return QString::fromUtf8(aLocaleString);
00077 }
00078
00079 void KConfigBase::setGroup( const QString& group )
00080 {
00081 if ( group.isEmpty() )
00082 mGroup = "<default>";
00083 else
00084 mGroup = group.utf8();
00085 }
00086
00087 void KConfigBase::setGroup( const char *pGroup )
00088 {
00089 setGroup(QCString(pGroup));
00090 }
00091
00092 void KConfigBase::setGroup( const QCString &group )
00093 {
00094 if ( group.isEmpty() )
00095 mGroup = "<default>";
00096 else
00097 mGroup = group;
00098 }
00099
00100 QString KConfigBase::group() const {
00101 return QString::fromUtf8(mGroup);
00102 }
00103
00104 void KConfigBase::setDesktopGroup()
00105 {
00106 mGroup = "Desktop Entry";
00107 }
00108
00109 bool KConfigBase::hasKey(const QString &key) const
00110 {
00111 return hasKey(key.utf8().data());
00112 }
00113
00114 bool KConfigBase::hasKey(const char *pKey) const
00115 {
00116 KEntryKey aEntryKey(mGroup, 0);
00117 aEntryKey.c_key = pKey;
00118 aEntryKey.bDefault = readDefaults();
00119
00120 if (!locale().isNull()) {
00121
00122 aEntryKey.bLocal = true;
00123 KEntry entry = lookupData(aEntryKey);
00124 if (!entry.mValue.isNull())
00125 return true;
00126 aEntryKey.bLocal = false;
00127 }
00128
00129
00130 KEntry entry = lookupData(aEntryKey);
00131 return !entry.mValue.isNull();
00132 }
00133
00134 bool KConfigBase::hasGroup(const QString &group) const
00135 {
00136 return internalHasGroup( group.utf8());
00137 }
00138
00139 bool KConfigBase::hasGroup(const char *_pGroup) const
00140 {
00141 return internalHasGroup( QCString(_pGroup));
00142 }
00143
00144 bool KConfigBase::hasGroup(const QCString &_pGroup) const
00145 {
00146 return internalHasGroup( _pGroup);
00147 }
00148
00149 bool KConfigBase::isImmutable() const
00150 {
00151 return (getConfigState() != ReadWrite);
00152 }
00153
00154 bool KConfigBase::groupIsImmutable(const QString &group) const
00155 {
00156 if (getConfigState() != ReadWrite)
00157 return true;
00158
00159 KEntryKey groupKey(group.utf8(), 0);
00160 KEntry entry = lookupData(groupKey);
00161 return entry.bImmutable;
00162 }
00163
00164 bool KConfigBase::entryIsImmutable(const QString &key) const
00165 {
00166 if (getConfigState() != ReadWrite)
00167 return true;
00168
00169 KEntryKey entryKey(mGroup, 0);
00170 KEntry aEntryData = lookupData(entryKey);
00171 if (aEntryData.bImmutable)
00172 return true;
00173
00174 QCString utf8_key = key.utf8();
00175 entryKey.c_key = utf8_key.data();
00176 aEntryData = lookupData(entryKey);
00177 if (aEntryData.bImmutable)
00178 return true;
00179
00180 entryKey.bLocal = true;
00181 aEntryData = lookupData(entryKey);
00182 return aEntryData.bImmutable;
00183 }
00184
00185
00186 QString KConfigBase::readEntryUntranslated( const QString& pKey,
00187 const QString& aDefault ) const
00188 {
00189 return KConfigBase::readEntryUntranslated(pKey.utf8().data(), aDefault);
00190 }
00191
00192
00193 QString KConfigBase::readEntryUntranslated( const char *pKey,
00194 const QString& aDefault ) const
00195 {
00196 QCString result = readEntryUtf8(pKey);
00197 if (result.isNull())
00198 return aDefault;
00199 return QString::fromUtf8(result);
00200 }
00201
00202
00203 QString KConfigBase::readEntry( const QString& pKey,
00204 const QString& aDefault ) const
00205 {
00206 return KConfigBase::readEntry(pKey.utf8().data(), aDefault);
00207 }
00208
00209 QString KConfigBase::readEntry( const char *pKey,
00210 const QString& aDefault ) const
00211 {
00212
00213
00214
00215
00216 if (!bLocaleInitialized && KGlobal::_locale) {
00217
00218 KConfigBase *that = const_cast<KConfigBase *>(this);
00219 that->setLocale();
00220 }
00221
00222 QString aValue;
00223
00224 bool expand = false;
00225
00226
00227 KEntry aEntryData;
00228 KEntryKey entryKey(mGroup, 0);
00229 entryKey.c_key = pKey;
00230 entryKey.bDefault = readDefaults();
00231 entryKey.bLocal = true;
00232 aEntryData = lookupData(entryKey);
00233 if (!aEntryData.mValue.isNull()) {
00234
00235 aValue = KStringHandler::from8Bit( aEntryData.mValue.data() );
00236 expand = aEntryData.bExpand;
00237 } else {
00238 entryKey.bLocal = false;
00239 aEntryData = lookupData(entryKey);
00240 if (!aEntryData.mValue.isNull()) {
00241 aValue = QString::fromUtf8(aEntryData.mValue.data());
00242 if (aValue.isNull())
00243 {
00244 static const QString &emptyString = KGlobal::staticQString("");
00245 aValue = emptyString;
00246 }
00247 expand = aEntryData.bExpand;
00248 } else {
00249 aValue = aDefault;
00250 }
00251 }
00252
00253
00254 if( expand || bExpand )
00255 {
00256
00257 int nDollarPos = aValue.find( '$' );
00258
00259 while( nDollarPos != -1 && nDollarPos+1 < static_cast<int>(aValue.length())) {
00260
00261 if( (aValue)[nDollarPos+1] == '(' ) {
00262 uint nEndPos = nDollarPos+1;
00263
00264 while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!=')') )
00265 nEndPos++;
00266 nEndPos++;
00267 QString cmd = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
00268
00269 QString result;
00270 FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
00271 if (fs)
00272 {
00273 QTextStream ts(fs, IO_ReadOnly);
00274 result = ts.read().stripWhiteSpace();
00275 pclose(fs);
00276 }
00277 aValue.replace( nDollarPos, nEndPos-nDollarPos, result );
00278 } else if( (aValue)[nDollarPos+1] != '$' ) {
00279 uint nEndPos = nDollarPos+1;
00280
00281 QString aVarName;
00282 if (aValue[nEndPos]=='{')
00283 {
00284 while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!='}') )
00285 nEndPos++;
00286 nEndPos++;
00287 aVarName = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
00288 }
00289 else
00290 {
00291 while ( nEndPos <= aValue.length() && (aValue[nEndPos].isNumber()
00292 || aValue[nEndPos].isLetter() || aValue[nEndPos]=='_' ) )
00293 nEndPos++;
00294 aVarName = aValue.mid( nDollarPos+1, nEndPos-nDollarPos-1 );
00295 }
00296 const char* pEnv = 0;
00297 if (!aVarName.isEmpty())
00298 pEnv = getenv( aVarName.ascii() );
00299 if( pEnv ) {
00300
00301
00302
00303 aValue.replace( nDollarPos, nEndPos-nDollarPos, KStringHandler::from8Bit( pEnv ) );
00304 } else
00305 aValue.remove( nDollarPos, nEndPos-nDollarPos );
00306 } else {
00307
00308 aValue.remove( nDollarPos, 1 );
00309 nDollarPos++;
00310 }
00311 nDollarPos = aValue.find( '$', nDollarPos );
00312 }
00313 }
00314
00315 return aValue;
00316 }
00317
00318 QCString KConfigBase::readEntryUtf8( const char *pKey) const
00319 {
00320
00321 KEntryKey entryKey(mGroup, 0);
00322 entryKey.bDefault = readDefaults();
00323 entryKey.c_key = pKey;
00324 KEntry aEntryData = lookupData(entryKey);
00325 if (aEntryData.bExpand)
00326 {
00327
00328 return readEntry(pKey, QString::null).utf8();
00329 }
00330 return aEntryData.mValue;
00331 }
00332
00333 QVariant KConfigBase::readPropertyEntry( const QString& pKey,
00334 QVariant::Type type ) const
00335 {
00336 return readPropertyEntry(pKey.utf8().data(), type);
00337 }
00338
00339 QVariant KConfigBase::readPropertyEntry( const char *pKey,
00340 QVariant::Type type ) const
00341 {
00342 QVariant va;
00343 if ( !hasKey( pKey ) ) return va;
00344 (void)va.cast(type);
00345 return readPropertyEntry(pKey, va);
00346 }
00347
00348 QVariant KConfigBase::readPropertyEntry( const QString& pKey,
00349 const QVariant &aDefault ) const
00350 {
00351 return readPropertyEntry(pKey.utf8().data(), aDefault);
00352 }
00353
00354 QVariant KConfigBase::readPropertyEntry( const char *pKey,
00355 const QVariant &aDefault ) const
00356 {
00357 if ( !hasKey( pKey ) ) return aDefault;
00358
00359 QVariant tmp = aDefault;
00360
00361 switch( aDefault.type() )
00362 {
00363 case QVariant::Invalid:
00364 return QVariant();
00365 case QVariant::String:
00366 return QVariant( readEntry( pKey, aDefault.toString() ) );
00367 case QVariant::StringList:
00368 return QVariant( readListEntry( pKey ) );
00369 case QVariant::List: {
00370 QStringList strList = readListEntry( pKey );
00371 QStringList::ConstIterator it = strList.begin();
00372 QStringList::ConstIterator end = strList.end();
00373 QValueList<QVariant> list;
00374
00375 for (; it != end; ++it ) {
00376 tmp = *it;
00377 list.append( tmp );
00378 }
00379 return QVariant( list );
00380 }
00381 case QVariant::Font:
00382 return QVariant( readFontEntry( pKey, &tmp.asFont() ) );
00383 case QVariant::Point:
00384 return QVariant( readPointEntry( pKey, &tmp.asPoint() ) );
00385 case QVariant::Rect:
00386 return QVariant( readRectEntry( pKey, &tmp.asRect() ) );
00387 case QVariant::Size:
00388 return QVariant( readSizeEntry( pKey, &tmp.asSize() ) );
00389 case QVariant::Color:
00390 return QVariant( readColorEntry( pKey, &tmp.asColor() ) );
00391 case QVariant::Int:
00392 return QVariant( readNumEntry( pKey, aDefault.toInt() ) );
00393 case QVariant::UInt:
00394 return QVariant( readUnsignedNumEntry( pKey, aDefault.toUInt() ) );
00395 case QVariant::LongLong:
00396 return QVariant( readNum64Entry( pKey, aDefault.toLongLong() ) );
00397 case QVariant::ULongLong:
00398 return QVariant( readUnsignedNum64Entry( pKey, aDefault.toULongLong() ) );
00399 case QVariant::Bool:
00400 return QVariant( readBoolEntry( pKey, aDefault.toBool() ), 0 );
00401 case QVariant::Double:
00402 return QVariant( readDoubleNumEntry( pKey, aDefault.toDouble() ) );
00403 case QVariant::DateTime:
00404 return QVariant( readDateTimeEntry( pKey, &tmp.asDateTime() ) );
00405 case QVariant::Date:
00406 return QVariant(readDateTimeEntry( pKey, &tmp.asDateTime() ).date());
00407
00408 case QVariant::Pixmap:
00409 case QVariant::Image:
00410 case QVariant::Brush:
00411 case QVariant::Palette:
00412 case QVariant::ColorGroup:
00413 case QVariant::Map:
00414 case QVariant::IconSet:
00415 case QVariant::CString:
00416 case QVariant::PointArray:
00417 case QVariant::Region:
00418 case QVariant::Bitmap:
00419 case QVariant::Cursor:
00420 case QVariant::SizePolicy:
00421 case QVariant::Time:
00422 case QVariant::ByteArray:
00423 case QVariant::BitArray:
00424 case QVariant::KeySequence:
00425 case QVariant::Pen:
00426 break;
00427 }
00428
00429 Q_ASSERT( 0 );
00430 return QVariant();
00431 }
00432
00433 int KConfigBase::readListEntry( const QString& pKey,
00434 QStrList &list, char sep ) const
00435 {
00436 return readListEntry(pKey.utf8().data(), list, sep);
00437 }
00438
00439 int KConfigBase::readListEntry( const char *pKey,
00440 QStrList &list, char sep ) const
00441 {
00442 if( !hasKey( pKey ) )
00443 return 0;
00444
00445 QCString str_list = readEntryUtf8( pKey );
00446 if (str_list.isEmpty())
00447 return 0;
00448
00449 list.clear();
00450 QCString value = "";
00451 int len = str_list.length();
00452
00453 for (int i = 0; i < len; i++) {
00454 if (str_list[i] != sep && str_list[i] != '\\') {
00455 value += str_list[i];
00456 continue;
00457 }
00458 if (str_list[i] == '\\') {
00459 i++;
00460 if ( i < len )
00461 value += str_list[i];
00462 continue;
00463 }
00464
00465
00466
00467
00468
00469 list.append( value );
00470 value.truncate(0);
00471 }
00472
00473 if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
00474 list.append( value );
00475 return list.count();
00476 }
00477
00478 QStringList KConfigBase::readListEntry( const QString& pKey, char sep ) const
00479 {
00480 return readListEntry(pKey.utf8().data(), sep);
00481 }
00482
00483 QStringList KConfigBase::readListEntry( const char *pKey, char sep ) const
00484 {
00485 static const QString& emptyString = KGlobal::staticQString("");
00486
00487 QStringList list;
00488 if( !hasKey( pKey ) )
00489 return list;
00490 QString str_list = readEntry( pKey );
00491 if( str_list.isEmpty() )
00492 return list;
00493 QString value(emptyString);
00494 int len = str_list.length();
00495
00496 value.reserve( len );
00497 for( int i = 0; i < len; i++ )
00498 {
00499 if( str_list[i] != sep && str_list[i] != '\\' )
00500 {
00501 value += str_list[i];
00502 continue;
00503 }
00504 if( str_list[i] == '\\' )
00505 {
00506 i++;
00507 if ( i < len )
00508 value += str_list[i];
00509 continue;
00510 }
00511 QString finalvalue( value );
00512 finalvalue.squeeze();
00513 list.append( finalvalue );
00514 value.truncate( 0 );
00515 }
00516 if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
00517 {
00518 value.squeeze();
00519 list.append( value );
00520 }
00521 return list;
00522 }
00523
00524 QValueList<int> KConfigBase::readIntListEntry( const QString& pKey ) const
00525 {
00526 return readIntListEntry(pKey.utf8().data());
00527 }
00528
00529 QValueList<int> KConfigBase::readIntListEntry( const char *pKey ) const
00530 {
00531 QStringList strlist = readListEntry(pKey);
00532 QValueList<int> list;
00533 for (QStringList::ConstIterator it = strlist.begin(); it != strlist.end(); it++)
00534
00535
00536 list << (*it).toInt();
00537
00538 return list;
00539 }
00540
00541 QString KConfigBase::readPathEntry( const QString& pKey, const QString& pDefault ) const
00542 {
00543 return readPathEntry(pKey.utf8().data(), pDefault);
00544 }
00545
00546 QString KConfigBase::readPathEntry( const char *pKey, const QString& pDefault ) const
00547 {
00548 const bool bExpandSave = bExpand;
00549 bExpand = true;
00550 QString aValue = readEntry( pKey, pDefault );
00551 bExpand = bExpandSave;
00552 return aValue;
00553 }
00554
00555 QStringList KConfigBase::readPathListEntry( const QString& pKey, char sep ) const
00556 {
00557 return readPathListEntry(pKey.utf8().data(), sep);
00558 }
00559
00560 QStringList KConfigBase::readPathListEntry( const char *pKey, char sep ) const
00561 {
00562 const bool bExpandSave = bExpand;
00563 bExpand = true;
00564 QStringList aValue = readListEntry( pKey, sep );
00565 bExpand = bExpandSave;
00566 return aValue;
00567 }
00568
00569 int KConfigBase::readNumEntry( const QString& pKey, int nDefault) const
00570 {
00571 return readNumEntry(pKey.utf8().data(), nDefault);
00572 }
00573
00574 int KConfigBase::readNumEntry( const char *pKey, int nDefault) const
00575 {
00576 QCString aValue = readEntryUtf8( pKey );
00577 if( aValue.isNull() )
00578 return nDefault;
00579 else if( aValue == "true" || aValue == "on" || aValue == "yes" )
00580 return 1;
00581 else
00582 {
00583 bool ok;
00584 int rc = aValue.toInt( &ok );
00585 return( ok ? rc : nDefault );
00586 }
00587 }
00588
00589
00590 unsigned int KConfigBase::readUnsignedNumEntry( const QString& pKey, unsigned int nDefault) const
00591 {
00592 return readUnsignedNumEntry(pKey.utf8().data(), nDefault);
00593 }
00594
00595 unsigned int KConfigBase::readUnsignedNumEntry( const char *pKey, unsigned int nDefault) const
00596 {
00597 QCString aValue = readEntryUtf8( pKey );
00598 if( aValue.isNull() )
00599 return nDefault;
00600 else
00601 {
00602 bool ok;
00603 unsigned int rc = aValue.toUInt( &ok );
00604 return( ok ? rc : nDefault );
00605 }
00606 }
00607
00608
00609 long KConfigBase::readLongNumEntry( const QString& pKey, long nDefault) const
00610 {
00611 return readLongNumEntry(pKey.utf8().data(), nDefault);
00612 }
00613
00614 long KConfigBase::readLongNumEntry( const char *pKey, long nDefault) const
00615 {
00616 QCString aValue = readEntryUtf8( pKey );
00617 if( aValue.isNull() )
00618 return nDefault;
00619 else
00620 {
00621 bool ok;
00622 long rc = aValue.toLong( &ok );
00623 return( ok ? rc : nDefault );
00624 }
00625 }
00626
00627
00628 unsigned long KConfigBase::readUnsignedLongNumEntry( const QString& pKey, unsigned long nDefault) const
00629 {
00630 return readUnsignedLongNumEntry(pKey.utf8().data(), nDefault);
00631 }
00632
00633 unsigned long KConfigBase::readUnsignedLongNumEntry( const char *pKey, unsigned long nDefault) const
00634 {
00635 QCString aValue = readEntryUtf8( pKey );
00636 if( aValue.isNull() )
00637 return nDefault;
00638 else
00639 {
00640 bool ok;
00641 unsigned long rc = aValue.toULong( &ok );
00642 return( ok ? rc : nDefault );
00643 }
00644 }
00645
00646 Q_INT64 KConfigBase::readNum64Entry( const QString& pKey, Q_INT64 nDefault) const
00647 {
00648 return readNum64Entry(pKey.utf8().data(), nDefault);
00649 }
00650
00651 Q_INT64 KConfigBase::readNum64Entry( const char *pKey, Q_INT64 nDefault) const
00652 {
00653
00654 QString aValue = readEntry( pKey );
00655 if( aValue.isNull() )
00656 return nDefault;
00657 else
00658 {
00659 bool ok;
00660 Q_INT64 rc = aValue.toLongLong( &ok );
00661 return( ok ? rc : nDefault );
00662 }
00663 }
00664
00665
00666 Q_UINT64 KConfigBase::readUnsignedNum64Entry( const QString& pKey, Q_UINT64 nDefault) const
00667 {
00668 return readUnsignedNum64Entry(pKey.utf8().data(), nDefault);
00669 }
00670
00671 Q_UINT64 KConfigBase::readUnsignedNum64Entry( const char *pKey, Q_UINT64 nDefault) const
00672 {
00673
00674 QString aValue = readEntry( pKey );
00675 if( aValue.isNull() )
00676 return nDefault;
00677 else
00678 {
00679 bool ok;
00680 Q_UINT64 rc = aValue.toULongLong( &ok );
00681 return( ok ? rc : nDefault );
00682 }
00683 }
00684
00685 double KConfigBase::readDoubleNumEntry( const QString& pKey, double nDefault) const
00686 {
00687 return readDoubleNumEntry(pKey.utf8().data(), nDefault);
00688 }
00689
00690 double KConfigBase::readDoubleNumEntry( const char *pKey, double nDefault) const
00691 {
00692 QCString aValue = readEntryUtf8( pKey );
00693 if( aValue.isNull() )
00694 return nDefault;
00695 else
00696 {
00697 bool ok;
00698 double rc = aValue.toDouble( &ok );
00699 return( ok ? rc : nDefault );
00700 }
00701 }
00702
00703
00704 bool KConfigBase::readBoolEntry( const QString& pKey, const bool bDefault ) const
00705 {
00706 return readBoolEntry(pKey.utf8().data(), bDefault);
00707 }
00708
00709 bool KConfigBase::readBoolEntry( const char *pKey, const bool bDefault ) const
00710 {
00711 QCString aValue = readEntryUtf8( pKey );
00712
00713 if( aValue.isNull() )
00714 return bDefault;
00715 else
00716 {
00717 if( aValue == "true" || aValue == "on" || aValue == "yes" || aValue == "1" )
00718 return true;
00719 else
00720 {
00721 bool bOK;
00722 int val = aValue.toInt( &bOK );
00723 if( bOK && val != 0 )
00724 return true;
00725 else
00726 return false;
00727 }
00728 }
00729 }
00730
00731 QFont KConfigBase::readFontEntry( const QString& pKey, const QFont* pDefault ) const
00732 {
00733 return readFontEntry(pKey.utf8().data(), pDefault);
00734 }
00735
00736 QFont KConfigBase::readFontEntry( const char *pKey, const QFont* pDefault ) const
00737 {
00738 QFont aRetFont;
00739
00740 QString aValue = readEntry( pKey );
00741 if( !aValue.isNull() ) {
00742 if ( aValue.contains( ',' ) > 5 ) {
00743
00744 if ( !aRetFont.fromString( aValue ) && pDefault )
00745 aRetFont = *pDefault;
00746 }
00747 else {
00748
00749
00750
00751 int nIndex = aValue.find( ',' );
00752 if( nIndex == -1 ){
00753 if( pDefault )
00754 aRetFont = *pDefault;
00755 return aRetFont;
00756 }
00757 aRetFont.setFamily( aValue.left( nIndex ) );
00758
00759
00760 int nOldIndex = nIndex;
00761 nIndex = aValue.find( ',', nOldIndex+1 );
00762 if( nIndex == -1 ){
00763 if( pDefault )
00764 aRetFont = *pDefault;
00765 return aRetFont;
00766 }
00767
00768 aRetFont.setPointSize( aValue.mid( nOldIndex+1,
00769 nIndex-nOldIndex-1 ).toInt() );
00770
00771
00772 nOldIndex = nIndex;
00773 nIndex = aValue.find( ',', nOldIndex+1 );
00774
00775 if( nIndex == -1 ){
00776 if( pDefault )
00777 aRetFont = *pDefault;
00778 return aRetFont;
00779 }
00780
00781 aRetFont.setStyleHint( (QFont::StyleHint)aValue.mid( nOldIndex+1, nIndex-nOldIndex-1 ).toUInt() );
00782
00783
00784 nOldIndex = nIndex;
00785 nIndex = aValue.find( ',', nOldIndex+1 );
00786
00787 if( nIndex == -1 ){
00788 if( pDefault )
00789 aRetFont = *pDefault;
00790 return aRetFont;
00791 }
00792
00793 QString chStr=aValue.mid( nOldIndex+1,
00794 nIndex-nOldIndex-1 );
00795
00796 nOldIndex = nIndex;
00797 nIndex = aValue.find( ',', nOldIndex+1 );
00798
00799 if( nIndex == -1 ){
00800 if( pDefault )
00801 aRetFont = *pDefault;
00802 return aRetFont;
00803 }
00804
00805 aRetFont.setWeight( aValue.mid( nOldIndex+1,
00806 nIndex-nOldIndex-1 ).toUInt() );
00807
00808
00809 uint nFontBits = aValue.right( aValue.length()-nIndex-1 ).toUInt();
00810
00811 aRetFont.setItalic( nFontBits & 0x01 );
00812 aRetFont.setUnderline( nFontBits & 0x02 );
00813 aRetFont.setStrikeOut( nFontBits & 0x04 );
00814 aRetFont.setFixedPitch( nFontBits & 0x08 );
00815 aRetFont.setRawMode( nFontBits & 0x20 );
00816 }
00817 }
00818 else
00819 {
00820 if( pDefault )
00821 aRetFont = *pDefault;
00822 }
00823
00824 return aRetFont;
00825 }
00826
00827
00828 QRect KConfigBase::readRectEntry( const QString& pKey, const QRect* pDefault ) const
00829 {
00830 return readRectEntry(pKey.utf8().data(), pDefault);
00831 }
00832
00833 QRect KConfigBase::readRectEntry( const char *pKey, const QRect* pDefault ) const
00834 {
00835 QCString aValue = readEntryUtf8(pKey);
00836
00837 if (!aValue.isEmpty())
00838 {
00839 int left, top, width, height;
00840
00841 if (sscanf(aValue.data(), "%d,%d,%d,%d", &left, &top, &width, &height) == 4)
00842 {
00843 return QRect(left, top, width, height);
00844 }
00845 }
00846 if (pDefault)
00847 return *pDefault;
00848 return QRect();
00849 }
00850
00851
00852 QPoint KConfigBase::readPointEntry( const QString& pKey,
00853 const QPoint* pDefault ) const
00854 {
00855 return readPointEntry(pKey.utf8().data(), pDefault);
00856 }
00857
00858 QPoint KConfigBase::readPointEntry( const char *pKey,
00859 const QPoint* pDefault ) const
00860 {
00861 QCString aValue = readEntryUtf8(pKey);
00862
00863 if (!aValue.isEmpty())
00864 {
00865 int x,y;
00866
00867 if (sscanf(aValue.data(), "%d,%d", &x, &y) == 2)
00868 {
00869 return QPoint(x,y);
00870 }
00871 }
00872 if (pDefault)
00873 return *pDefault;
00874 return QPoint();
00875 }
00876
00877 QSize KConfigBase::readSizeEntry( const QString& pKey,
00878 const QSize* pDefault ) const
00879 {
00880 return readSizeEntry(pKey.utf8().data(), pDefault);
00881 }
00882
00883 QSize KConfigBase::readSizeEntry( const char *pKey,
00884 const QSize* pDefault ) const
00885 {
00886 QCString aValue = readEntryUtf8(pKey);
00887
00888 if (!aValue.isEmpty())
00889 {
00890 int width,height;
00891
00892 if (sscanf(aValue.data(), "%d,%d", &width, &height) == 2)
00893 {
00894 return QSize(width, height);
00895 }
00896 }
00897 if (pDefault)
00898 return *pDefault;
00899 return QSize();
00900 }
00901
00902
00903 QColor KConfigBase::readColorEntry( const QString& pKey,
00904 const QColor* pDefault ) const
00905 {
00906 return readColorEntry(pKey.utf8().data(), pDefault);
00907 }
00908
00909 QColor KConfigBase::readColorEntry( const char *pKey,
00910 const QColor* pDefault ) const
00911 {
00912 QColor aRetColor;
00913 int nRed = 0, nGreen = 0, nBlue = 0;
00914
00915 QString aValue = readEntry( pKey );
00916 if( !aValue.isEmpty() )
00917 {
00918 if ( aValue.at(0) == '#' )
00919 {
00920 aRetColor.setNamedColor(aValue);
00921 }
00922 else
00923 {
00924
00925 bool bOK;
00926
00927
00928 int nIndex = aValue.find( ',' );
00929
00930 if( nIndex == -1 ){
00931
00932 if( pDefault )
00933 aRetColor = *pDefault;
00934 return aRetColor;
00935 }
00936
00937 nRed = aValue.left( nIndex ).toInt( &bOK );
00938
00939
00940 int nOldIndex = nIndex;
00941 nIndex = aValue.find( ',', nOldIndex+1 );
00942
00943 if( nIndex == -1 ){
00944
00945 if( pDefault )
00946 aRetColor = *pDefault;
00947 return aRetColor;
00948 }
00949 nGreen = aValue.mid( nOldIndex+1,
00950 nIndex-nOldIndex-1 ).toInt( &bOK );
00951
00952
00953 nBlue = aValue.right( aValue.length()-nIndex-1 ).toInt( &bOK );
00954
00955 aRetColor.setRgb( nRed, nGreen, nBlue );
00956 }
00957 }
00958 else {
00959
00960 if( pDefault )
00961 aRetColor = *pDefault;
00962 }
00963
00964 return aRetColor;
00965 }
00966
00967
00968 QDateTime KConfigBase::readDateTimeEntry( const QString& pKey,
00969 const QDateTime* pDefault ) const
00970 {
00971 return readDateTimeEntry(pKey.utf8().data(), pDefault);
00972 }
00973
00974
00975 QDateTime KConfigBase::readDateTimeEntry( const char *pKey,
00976 const QDateTime* pDefault ) const
00977 {
00978 if( !hasKey( pKey ) )
00979 {
00980 if( pDefault )
00981 return *pDefault;
00982 else
00983 return QDateTime::currentDateTime();
00984 }
00985
00986 QStrList list;
00987 int count = readListEntry( pKey, list, ',' );
00988 if( count == 6 ) {
00989 QDate date( atoi( list.at( 0 ) ), atoi( list.at( 1 ) ),
00990 atoi( list.at( 2 ) ) );
00991 QTime time( atoi( list.at( 3 ) ), atoi( list.at( 4 ) ),
00992 atoi( list.at( 5 ) ) );
00993
00994 return QDateTime( date, time );
00995 }
00996
00997 return QDateTime::currentDateTime();
00998 }
00999
01000 void KConfigBase::writeEntry( const QString& pKey, const QString& value,
01001 bool bPersistent,
01002 bool bGlobal,
01003 bool bNLS )
01004 {
01005 writeEntry(pKey.utf8().data(), value, bPersistent, bGlobal, bNLS);
01006 }
01007
01008 void KConfigBase::writeEntry( const char *pKey, const QString& value,
01009 bool bPersistent,
01010 bool bGlobal,
01011 bool bNLS )
01012 {
01013
01014
01015
01016
01017
01018 if( bPersistent )
01019 setDirty(true);
01020
01021 if (!bLocaleInitialized && KGlobal::locale())
01022 setLocale();
01023
01024 KEntryKey entryKey(mGroup, pKey);
01025 entryKey.bLocal = bNLS;
01026
01027 KEntry aEntryData;
01028 aEntryData.mValue = value.utf8();
01029 aEntryData.bGlobal = bGlobal;
01030 aEntryData.bNLS = bNLS;
01031
01032 if (bPersistent)
01033 aEntryData.bDirty = true;
01034
01035
01036 putData(entryKey, aEntryData, true);
01037 }
01038
01039 void KConfigBase::writePathEntry( const QString& pKey, const QString & path,
01040 bool bPersistent, bool bGlobal,
01041 bool bNLS)
01042 {
01043 writePathEntry(pKey.utf8().data(), path, bPersistent, bGlobal, bNLS);
01044 }
01045
01046
01047 static bool cleanHomeDirPath( QString &path, const QString &homeDir )
01048 {
01049 if (!path.startsWith(homeDir))
01050 return false;
01051
01052 unsigned int len = homeDir.length();
01053
01054 if (path.length() == len || path[len] == '/') {
01055 path = path.replace(0, len, QString::fromLatin1("$HOME"));
01056 return true;
01057 } else
01058 return false;
01059 }
01060
01061 static QString translatePath( QString path )
01062 {
01063 if (path.isEmpty())
01064 return path;
01065
01066 bool startsWithFile = path.startsWith("file:", false);
01067
01068
01069
01070 if (!startsWithFile && path[0] != '/' ||
01071 startsWithFile && path[5] != '/')
01072 return path;
01073
01074 if (startsWithFile)
01075 path.remove(0,5);
01076
01077
01078
01079
01080
01081 QString homeDir0 = QFile::decodeName(getenv("HOME"));
01082 QString homeDir1 = QDir::homeDirPath();
01083 QString homeDir2 = QDir(homeDir1).canonicalPath();
01084 if (cleanHomeDirPath(path, homeDir0) ||
01085 cleanHomeDirPath(path, homeDir1) ||
01086 cleanHomeDirPath(path, homeDir2) ) {
01087
01088 }
01089
01090 if (startsWithFile)
01091 path.prepend( "file:" );
01092
01093 return path;
01094 }
01095
01096 void KConfigBase::writePathEntry( const char *pKey, const QString & path,
01097 bool bPersistent, bool bGlobal,
01098 bool bNLS)
01099 {
01100 writeEntry(pKey, translatePath(path), bPersistent, bGlobal, bNLS);
01101 }
01102
01103 void KConfigBase::writePathEntry ( const QString& pKey, const QStringList &list,
01104 char sep , bool bPersistent,
01105 bool bGlobal, bool bNLS )
01106 {
01107 writePathEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
01108 }
01109
01110 void KConfigBase::writePathEntry ( const char *pKey, const QStringList &list,
01111 char sep , bool bPersistent,
01112 bool bGlobal, bool bNLS )
01113 {
01114 if( list.isEmpty() )
01115 {
01116 writeEntry( pKey, QString::fromLatin1(""), bPersistent );
01117 return;
01118 }
01119 QStringList new_list;
01120 QStringList::ConstIterator it = list.begin();
01121 for( ; it != list.end(); ++it )
01122 {
01123 QString value = *it;
01124 new_list.append( translatePath(value) );
01125 }
01126 writeEntry( pKey, new_list, sep, bPersistent, bGlobal, bNLS );
01127 }
01128
01129 void KConfigBase::deleteEntry( const QString& pKey,
01130 bool bNLS,
01131 bool bGlobal)
01132 {
01133 deleteEntry(pKey.utf8().data(), bNLS, bGlobal);
01134 }
01135
01136 void KConfigBase::deleteEntry( const char *pKey,
01137 bool bNLS,
01138 bool bGlobal)
01139 {
01140
01141
01142
01143
01144
01145 setDirty(true);
01146
01147 if (!bLocaleInitialized && KGlobal::locale())
01148 setLocale();
01149
01150 KEntryKey entryKey(mGroup, pKey);
01151 KEntry aEntryData;
01152
01153 aEntryData.bGlobal = bGlobal;
01154 aEntryData.bNLS = bNLS;
01155 aEntryData.bDirty = true;
01156 aEntryData.bDeleted = true;
01157
01158
01159 putData(entryKey, aEntryData, true);
01160 }
01161
01162 bool KConfigBase::deleteGroup( const QString& group, bool bDeep, bool bGlobal )
01163 {
01164 KEntryMap aEntryMap = internalEntryMap(group);
01165
01166 if (!bDeep) {
01167
01168 return aEntryMap.isEmpty();
01169 }
01170
01171 bool dirty = false;
01172 bool checkGroup = true;
01173
01174 KEntryMapIterator aIt;
01175 for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt)
01176 {
01177 if (!aIt.key().mKey.isEmpty() && !aIt.key().bDefault && !(*aIt).bDeleted)
01178 {
01179 (*aIt).bDeleted = true;
01180 (*aIt).bDirty = true;
01181 (*aIt).bGlobal = bGlobal;
01182 (*aIt).mValue = 0;
01183 putData(aIt.key(), *aIt, checkGroup);
01184 checkGroup = false;
01185 dirty = true;
01186 }
01187 }
01188 if (dirty)
01189 setDirty(true);
01190 return true;
01191 }
01192
01193 void KConfigBase::writeEntry ( const QString& pKey, const QVariant &prop,
01194 bool bPersistent,
01195 bool bGlobal, bool bNLS )
01196 {
01197 writeEntry(pKey.utf8().data(), prop, bPersistent, bGlobal, bNLS);
01198 }
01199
01200 void KConfigBase::writeEntry ( const char *pKey, const QVariant &prop,
01201 bool bPersistent,
01202 bool bGlobal, bool bNLS )
01203 {
01204 switch( prop.type() )
01205 {
01206 case QVariant::Invalid:
01207 writeEntry( pKey, "", bPersistent, bGlobal, bNLS );
01208 return;
01209 case QVariant::String:
01210 writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
01211 return;
01212 case QVariant::StringList:
01213 writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
01214 return;
01215 case QVariant::List: {
01216 QValueList<QVariant> list = prop.toList();
01217 QValueList<QVariant>::ConstIterator it = list.begin();
01218 QValueList<QVariant>::ConstIterator end = list.end();
01219 QStringList strList;
01220
01221