• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdecore
 

kdecore

  • kdecore
kconfigbase.cpp
1 // -*- c-basic-offset: 2 -*-
2 /*
3  This file is part of the KDE libraries
4  Copyright (c) 1999 Preston Brown <pbrown@kde.org>
5  Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <tqfile.h>
27 #include <tqdir.h>
28 #include <tqtextstream.h>
29 
30 #include <kapplication.h>
31 #include <kglobal.h>
32 #include <klocale.h>
33 #include <kcharsets.h>
34 
35 #include "kconfigbase.h"
36 #include "kconfigbackend.h"
37 #include "kdebug.h"
38 #include "kstandarddirs.h"
39 #include "kstringhandler.h"
40 
41 class KConfigBase::KConfigBasePrivate
42 {
43 public:
44  KConfigBasePrivate() : readDefaults(false) { };
45 
46 public:
47  bool readDefaults;
48 };
49 
50 KConfigBase::KConfigBase()
51  : backEnd(0L), bDirty(false), bLocaleInitialized(false),
52  bReadOnly(false), bExpand(false), d(0)
53 {
54  setGroup(TQString::null);
55 }
56 
57 KConfigBase::~KConfigBase()
58 {
59  delete d;
60 }
61 
62 void KConfigBase::setLocale()
63 {
64  bLocaleInitialized = true;
65 
66  if (KGlobal::locale())
67  aLocaleString = KGlobal::locale()->language().utf8();
68  else
69  aLocaleString = KLocale::defaultLanguage().utf8();
70  if (backEnd)
71  backEnd->setLocaleString(aLocaleString);
72 }
73 
74 TQString KConfigBase::locale() const
75 {
76  return TQString::fromUtf8(aLocaleString);
77 }
78 
79 void KConfigBase::setGroup( const TQString& group )
80 {
81  if ( group.isEmpty() )
82  mGroup = "<default>";
83  else
84  mGroup = group.utf8();
85 }
86 
87 void KConfigBase::setGroup( const char *pGroup )
88 {
89  setGroup(TQCString(pGroup));
90 }
91 
92 void KConfigBase::setGroup( const TQCString &group )
93 {
94  if ( group.isEmpty() )
95  mGroup = "<default>";
96  else
97  mGroup = group;
98 }
99 
100 TQString KConfigBase::group() const {
101  return TQString::fromUtf8(mGroup);
102 }
103 
104 void KConfigBase::setDesktopGroup()
105 {
106  mGroup = "Desktop Entry";
107 }
108 
109 bool KConfigBase::hasKey(const TQString &key) const
110 {
111  return hasKey(key.utf8().data());
112 }
113 
114 bool KConfigBase::hasKey(const char *pKey) const
115 {
116  KEntryKey aEntryKey(mGroup, 0);
117  aEntryKey.c_key = pKey;
118  aEntryKey.bDefault = readDefaults();
119 
120  if (!locale().isNull()) {
121  // try the localized key first
122  aEntryKey.bLocal = true;
123  KEntry entry = lookupData(aEntryKey);
124  if (!entry.mValue.isNull())
125  return true;
126  aEntryKey.bLocal = false;
127  }
128 
129  // try the non-localized version
130  KEntry entry = lookupData(aEntryKey);
131  return !entry.mValue.isNull();
132 }
133 
134 bool KConfigBase::hasTranslatedKey(const char* pKey) const
135 {
136  KEntryKey aEntryKey(mGroup, 0);
137  aEntryKey.c_key = pKey;
138  aEntryKey.bDefault = readDefaults();
139 
140  if (!locale().isNull()) {
141  // try the localized key first
142  aEntryKey.bLocal = true;
143  KEntry entry = lookupData(aEntryKey);
144  if (!entry.mValue.isNull())
145  return true;
146  aEntryKey.bLocal = false;
147  }
148 
149  return false;
150 }
151 
152 bool KConfigBase::hasGroup(const TQString &group) const
153 {
154  return internalHasGroup( group.utf8());
155 }
156 
157 bool KConfigBase::hasGroup(const char *_pGroup) const
158 {
159  return internalHasGroup( TQCString(_pGroup));
160 }
161 
162 bool KConfigBase::hasGroup(const TQCString &_pGroup) const
163 {
164  return internalHasGroup( _pGroup);
165 }
166 
167 bool KConfigBase::isImmutable() const
168 {
169  return (getConfigState() != ReadWrite);
170 }
171 
172 bool KConfigBase::groupIsImmutable(const TQString &group) const
173 {
174  if (getConfigState() != ReadWrite)
175  return true;
176 
177  KEntryKey groupKey(group.utf8(), 0);
178  KEntry entry = lookupData(groupKey);
179  return entry.bImmutable;
180 }
181 
182 bool KConfigBase::entryIsImmutable(const TQString &key) const
183 {
184  if (getConfigState() != ReadWrite)
185  return true;
186 
187  KEntryKey entryKey(mGroup, 0);
188  KEntry aEntryData = lookupData(entryKey); // Group
189  if (aEntryData.bImmutable)
190  return true;
191 
192  TQCString utf8_key = key.utf8();
193  entryKey.c_key = utf8_key.data();
194  aEntryData = lookupData(entryKey); // Normal entry
195  if (aEntryData.bImmutable)
196  return true;
197 
198  entryKey.bLocal = true;
199  aEntryData = lookupData(entryKey); // Localized entry
200  return aEntryData.bImmutable;
201 }
202 
203 
204 TQString KConfigBase::readEntryUntranslated( const TQString& pKey,
205  const TQString& aDefault ) const
206 {
207  return KConfigBase::readEntryUntranslated(pKey.utf8().data(), aDefault);
208 }
209 
210 
211 TQString KConfigBase::readEntryUntranslated( const char *pKey,
212  const TQString& aDefault ) const
213 {
214  TQCString result = readEntryUtf8(pKey);
215  if (result.isNull())
216  return aDefault;
217  return TQString::fromUtf8(result);
218 }
219 
220 
221 TQString KConfigBase::readEntry( const TQString& pKey,
222  const TQString& aDefault ) const
223 {
224  return KConfigBase::readEntry(pKey.utf8().data(), aDefault);
225 }
226 
227 TQString KConfigBase::readEntry( const char *pKey,
228  const TQString& aDefault ) const
229 {
230  // we need to access _locale instead of the method locale()
231  // because calling locale() will create a locale object if it
232  // doesn't exist, which requires KConfig, which will create a infinite
233  // loop, and nobody likes those.
234  if (!bLocaleInitialized && KGlobal::_locale) {
235  // get around const'ness.
236  KConfigBase *that = const_cast<KConfigBase *>(this);
237  that->setLocale();
238  }
239 
240  TQString aValue;
241 
242  bool expand = false;
243  // construct a localized version of the key
244  // try the localized key first
245  KEntry aEntryData;
246  KEntryKey entryKey(mGroup, 0);
247  entryKey.c_key = pKey;
248  entryKey.bDefault = readDefaults();
249  entryKey.bLocal = true;
250  aEntryData = lookupData(entryKey);
251  if (!aEntryData.mValue.isNull()) {
252  // for GNOME .desktop
253  aValue = KStringHandler::from8Bit( aEntryData.mValue.data() );
254  expand = aEntryData.bExpand;
255  } else {
256  entryKey.bLocal = false;
257  aEntryData = lookupData(entryKey);
258  if (!aEntryData.mValue.isNull()) {
259  aValue = TQString::fromUtf8(aEntryData.mValue.data());
260  if (aValue.isNull())
261  {
262  static const TQString &emptyString = KGlobal::staticQString("");
263  aValue = emptyString;
264  }
265  expand = aEntryData.bExpand;
266  } else {
267  aValue = aDefault;
268  }
269  }
270 
271  // only do dollar expansion if so desired
272  if( expand || bExpand )
273  {
274  // check for environment variables and make necessary translations
275  int nDollarPos = aValue.find( '$' );
276 
277  while( nDollarPos != -1 && nDollarPos+1 < static_cast<int>(aValue.length())) {
278  // there is at least one $
279  if( (aValue)[nDollarPos+1] == '(' ) {
280  uint nEndPos = nDollarPos+1;
281  // the next character is no $
282  while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!=')') )
283  nEndPos++;
284  nEndPos++;
285  TQString cmd = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
286 
287  TQString result;
288  FILE *fs = popen(TQFile::encodeName(cmd).data(), "r");
289  if (fs)
290  {
291  {
292  TQTextStream ts(fs, IO_ReadOnly);
293  result = ts.read().stripWhiteSpace();
294  }
295  pclose(fs);
296  }
297  aValue.replace( nDollarPos, nEndPos-nDollarPos, result );
298  } else if( (aValue)[nDollarPos+1] != '$' ) {
299  uint nEndPos = nDollarPos+1;
300  // the next character is no $
301  TQString aVarName;
302  if (aValue[nEndPos]=='{')
303  {
304  while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!='}') )
305  nEndPos++;
306  nEndPos++;
307  aVarName = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
308  }
309  else
310  {
311  while ( nEndPos <= aValue.length() && (aValue[nEndPos].isNumber()
312  || aValue[nEndPos].isLetter() || aValue[nEndPos]=='_' ) )
313  nEndPos++;
314  aVarName = aValue.mid( nDollarPos+1, nEndPos-nDollarPos-1 );
315  }
316  const char* pEnv = 0;
317  if (!aVarName.isEmpty())
318  pEnv = getenv( aVarName.ascii() );
319  if( pEnv ) {
320  // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
321  // A environment variables may contain values in 8bit
322  // locale cpecified encoding or in UTF8 encoding.
323  aValue.replace( nDollarPos, nEndPos-nDollarPos, KStringHandler::from8Bit( pEnv ) );
324  } else
325  aValue.remove( nDollarPos, nEndPos-nDollarPos );
326  } else {
327  // remove one of the dollar signs
328  aValue.remove( nDollarPos, 1 );
329  nDollarPos++;
330  }
331  nDollarPos = aValue.find( '$', nDollarPos );
332  }
333  }
334 
335  return aValue;
336 }
337 
338 TQCString KConfigBase::readEntryUtf8( const char *pKey) const
339 {
340  // We don't try the localized key
341  KEntryKey entryKey(mGroup, 0);
342  entryKey.bDefault = readDefaults();
343  entryKey.c_key = pKey;
344  KEntry aEntryData = lookupData(entryKey);
345  if (aEntryData.bExpand)
346  {
347  // We need to do fancy, take the slow route.
348  return readEntry(pKey, TQString::null).utf8();
349  }
350  return aEntryData.mValue;
351 }
352 
353 TQVariant KConfigBase::readPropertyEntry( const TQString& pKey,
354  TQVariant::Type type ) const
355 {
356  return readPropertyEntry(pKey.utf8().data(), type);
357 }
358 
359 TQVariant KConfigBase::readPropertyEntry( const char *pKey,
360  TQVariant::Type type ) const
361 {
362  TQVariant va;
363  if ( !hasKey( pKey ) ) return va;
364  (void)va.cast(type);
365  return readPropertyEntry(pKey, va);
366 }
367 
368 TQVariant KConfigBase::readPropertyEntry( const TQString& pKey,
369  const TQVariant &aDefault ) const
370 {
371  return readPropertyEntry(pKey.utf8().data(), aDefault);
372 }
373 
374 TQVariant KConfigBase::readPropertyEntry( const char *pKey,
375  const TQVariant &aDefault ) const
376 {
377  if ( !hasKey( pKey ) ) return aDefault;
378 
379  TQVariant tmp = aDefault;
380 
381  switch( aDefault.type() )
382  {
383  case TQVariant::Invalid:
384  return TQVariant();
385  case TQVariant::String:
386  return TQVariant( readEntry( pKey, aDefault.toString() ) );
387  case TQVariant::StringList:
388  return TQVariant( readListEntry( pKey ) );
389  case TQVariant::List: {
390  TQStringList strList = readListEntry( pKey );
391  TQStringList::ConstIterator it = strList.begin();
392  TQStringList::ConstIterator end = strList.end();
393  TQValueList<TQVariant> list;
394 
395  for (; it != end; ++it ) {
396  tmp = *it;
397  list.append( tmp );
398  }
399  return TQVariant( list );
400  }
401  case TQVariant::Font:
402  return TQVariant( readFontEntry( pKey, &tmp.asFont() ) );
403  case TQVariant::Point:
404  return TQVariant( readPointEntry( pKey, &tmp.asPoint() ) );
405  case TQVariant::Rect:
406  return TQVariant( readRectEntry( pKey, &tmp.asRect() ) );
407  case TQVariant::Size:
408  return TQVariant( readSizeEntry( pKey, &tmp.asSize() ) );
409  case TQVariant::Color:
410  return TQVariant( readColorEntry( pKey, &tmp.asColor() ) );
411  case TQVariant::Int:
412  return TQVariant( readNumEntry( pKey, aDefault.toInt() ) );
413  case TQVariant::UInt:
414  return TQVariant( readUnsignedNumEntry( pKey, aDefault.toUInt() ) );
415  case TQVariant::LongLong:
416  return TQVariant( readNum64Entry( pKey, aDefault.toLongLong() ) );
417  case TQVariant::ULongLong:
418  return TQVariant( readUnsignedNum64Entry( pKey, aDefault.toULongLong() ) );
419  case TQVariant::Bool:
420  return TQVariant( readBoolEntry( pKey, aDefault.toBool() ), 0 );
421  case TQVariant::Double:
422  return TQVariant( readDoubleNumEntry( pKey, aDefault.toDouble() ) );
423  case TQVariant::DateTime:
424  return TQVariant( readDateTimeEntry( pKey, &tmp.asDateTime() ) );
425  case TQVariant::Date:
426  return TQVariant(TQT_TQDATE_OBJECT(readDateTimeEntry( pKey, &tmp.asDateTime() ).date()));
427 
428  case TQVariant::Pixmap:
429  case TQVariant::Image:
430  case TQVariant::Brush:
431  case TQVariant::Palette:
432  case TQVariant::ColorGroup:
433  case TQVariant::Map:
434  case TQVariant::IconSet:
435  case TQVariant::CString:
436  case TQVariant::PointArray:
437  case TQVariant::Region:
438  case TQVariant::Bitmap:
439  case TQVariant::Cursor:
440  case TQVariant::SizePolicy:
441  case TQVariant::Time:
442 #ifdef USE_QT3
443  case TQVariant::ByteArray:
444 #endif // USE_QT3
445  case TQVariant::BitArray:
446  case TQVariant::KeySequence:
447  case TQVariant::Pen:
448 #ifdef USE_QT4
449  case TQVariant::Char:
450  case TQVariant::Url:
451  case TQVariant::Locale:
452  case TQVariant::RectF:
453  case TQVariant::SizeF:
454  case TQVariant::Line:
455  case TQVariant::LineF:
456  case TQVariant::PointF:
457  case TQVariant::RegExp:
458  case TQVariant::Hash:
459  case TQVariant::TextLength:
460  case QVariant::TextFormat:
461  case TQVariant::Matrix:
462  case TQVariant::Transform:
463  case TQVariant::Matrix4x4:
464  case TQVariant::Vector2D:
465  case TQVariant::Vector3D:
466  case TQVariant::Vector4D:
467  case TQVariant::Quaternion:
468  case TQVariant::UserType:
469 #endif // USE_QT4
470  break;
471  }
472 
473  Q_ASSERT( 0 );
474  return TQVariant();
475 }
476 
477 int KConfigBase::readListEntry( const TQString& pKey,
478  TQStrList &list, char sep ) const
479 {
480  return readListEntry(pKey.utf8().data(), list, sep);
481 }
482 
483 int KConfigBase::readListEntry( const char *pKey,
484  TQStrList &list, char sep ) const
485 {
486  if( !hasKey( pKey ) )
487  return 0;
488 
489  TQCString str_list = readEntryUtf8( pKey );
490  if (str_list.isEmpty())
491  return 0;
492 
493  list.clear();
494  TQCString value = "";
495  int len = str_list.length();
496 
497  for (int i = 0; i < len; i++) {
498  if (str_list[i] != sep && str_list[i] != '\\') {
499  value += str_list[i];
500  continue;
501  }
502  if (str_list[i] == '\\') {
503  i++;
504  if ( i < len )
505  value += str_list[i];
506  continue;
507  }
508  // if we fell through to here, we are at a separator. Append
509  // contents of value to the list
510  // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
511  // A TQStrList may contain values in 8bit locale cpecified
512  // encoding
513  list.append( value );
514  value.truncate(0);
515  }
516 
517  if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
518  list.append( value );
519  return list.count();
520 }
521 
522 TQStringList KConfigBase::readListEntry( const TQString& pKey, char sep ) const
523 {
524  return readListEntry(pKey.utf8().data(), sep);
525 }
526 
527 TQStringList KConfigBase::readListEntry( const char *pKey, char sep ) const
528 {
529  static const TQString& emptyString = KGlobal::staticQString("");
530 
531  TQStringList list;
532  if( !hasKey( pKey ) )
533  return list;
534  TQString str_list = readEntry( pKey );
535  if( str_list.isEmpty() )
536  return list;
537  TQString value(emptyString);
538  int len = str_list.length();
539  // obviously too big, but faster than letting each += resize the string.
540  value.reserve( len );
541  for( int i = 0; i < len; i++ )
542  {
543  if( str_list[i] != sep && str_list[i] != '\\' )
544  {
545  value += str_list[i];
546  continue;
547  }
548  if( str_list[i] == '\\' )
549  {
550  i++;
551  if ( i < len )
552  value += str_list[i];
553  continue;
554  }
555  TQString finalvalue( value );
556  finalvalue.squeeze();
557  list.append( finalvalue );
558  value.truncate( 0 );
559  }
560  if ( str_list[len-1] != sep || ( len > 1 && str_list[len-2] == '\\' ) )
561  {
562  value.squeeze();
563  list.append( value );
564  }
565  return list;
566 }
567 
568 TQStringList KConfigBase::readListEntry( const char* pKey, const TQStringList& aDefault,
569  char sep ) const
570 {
571  if ( !hasKey( pKey ) )
572  return aDefault;
573  else
574  return readListEntry( pKey, sep );
575 }
576 
577 TQValueList<int> KConfigBase::readIntListEntry( const TQString& pKey ) const
578 {
579  return readIntListEntry(pKey.utf8().data());
580 }
581 
582 TQValueList<int> KConfigBase::readIntListEntry( const char *pKey ) const
583 {
584  TQStringList strlist = readListEntry(pKey);
585  TQValueList<int> list;
586  TQStringList::ConstIterator end(strlist.end());
587  for (TQStringList::ConstIterator it = strlist.begin(); it != end; ++it)
588  // I do not check if the toInt failed because I consider the number of items
589  // more important than their value
590  list << (*it).toInt();
591 
592  return list;
593 }
594 
595 TQString KConfigBase::readPathEntry( const TQString& pKey, const TQString& pDefault ) const
596 {
597  return readPathEntry(pKey.utf8().data(), pDefault);
598 }
599 
600 TQString KConfigBase::readPathEntry( const char *pKey, const TQString& pDefault ) const
601 {
602  const bool bExpandSave = bExpand;
603  bExpand = true;
604  TQString aValue = readEntry( pKey, pDefault );
605  bExpand = bExpandSave;
606  return aValue;
607 }
608 
609 TQStringList KConfigBase::readPathListEntry( const TQString& pKey, char sep ) const
610 {
611  return readPathListEntry(pKey.utf8().data(), sep);
612 }
613 
614 TQStringList KConfigBase::readPathListEntry( const char *pKey, char sep ) const
615 {
616  const bool bExpandSave = bExpand;
617  bExpand = true;
618  TQStringList aValue = readListEntry( pKey, sep );
619  bExpand = bExpandSave;
620  return aValue;
621 }
622 
623 int KConfigBase::readNumEntry( const TQString& pKey, int nDefault) const
624 {
625  return readNumEntry(pKey.utf8().data(), nDefault);
626 }
627 
628 int KConfigBase::readNumEntry( const char *pKey, int nDefault) const
629 {
630  TQCString aValue = readEntryUtf8( pKey );
631  if( aValue.isNull() )
632  return nDefault;
633  else if( aValue == "true" || aValue == "on" || aValue == "yes" )
634  return 1;
635  else
636  {
637  bool ok;
638  int rc = aValue.toInt( &ok );
639  return( ok ? rc : nDefault );
640  }
641 }
642 
643 
644 unsigned int KConfigBase::readUnsignedNumEntry( const TQString& pKey, unsigned int nDefault) const
645 {
646  return readUnsignedNumEntry(pKey.utf8().data(), nDefault);
647 }
648 
649 unsigned int KConfigBase::readUnsignedNumEntry( const char *pKey, unsigned int nDefault) const
650 {
651  TQCString aValue = readEntryUtf8( pKey );
652  if( aValue.isNull() )
653  return nDefault;
654  else
655  {
656  bool ok;
657  unsigned int rc = aValue.toUInt( &ok );
658  return( ok ? rc : nDefault );
659  }
660 }
661 
662 
663 long KConfigBase::readLongNumEntry( const TQString& pKey, long nDefault) const
664 {
665  return readLongNumEntry(pKey.utf8().data(), nDefault);
666 }
667 
668 long KConfigBase::readLongNumEntry( const char *pKey, long nDefault) const
669 {
670  TQCString aValue = readEntryUtf8( pKey );
671  if( aValue.isNull() )
672  return nDefault;
673  else
674  {
675  bool ok;
676  long rc = aValue.toLong( &ok );
677  return( ok ? rc : nDefault );
678  }
679 }
680 
681 
682 unsigned long KConfigBase::readUnsignedLongNumEntry( const TQString& pKey, unsigned long nDefault) const
683 {
684  return readUnsignedLongNumEntry(pKey.utf8().data(), nDefault);
685 }
686 
687 unsigned long KConfigBase::readUnsignedLongNumEntry( const char *pKey, unsigned long nDefault) const
688 {
689  TQCString aValue = readEntryUtf8( pKey );
690  if( aValue.isNull() )
691  return nDefault;
692  else
693  {
694  bool ok;
695  unsigned long rc = aValue.toULong( &ok );
696  return( ok ? rc : nDefault );
697  }
698 }
699 
700 TQ_INT64 KConfigBase::readNum64Entry( const TQString& pKey, TQ_INT64 nDefault) const
701 {
702  return readNum64Entry(pKey.utf8().data(), nDefault);
703 }
704 
705 TQ_INT64 KConfigBase::readNum64Entry( const char *pKey, TQ_INT64 nDefault) const
706 {
707  // Note that TQCString::toLongLong() is missing, we muse use a TQString instead.
708  TQString aValue = readEntry( pKey );
709  if( aValue.isNull() )
710  return nDefault;
711  else
712  {
713  bool ok;
714  TQ_INT64 rc = aValue.toLongLong( &ok );
715  return( ok ? rc : nDefault );
716  }
717 }
718 
719 
720 TQ_UINT64 KConfigBase::readUnsignedNum64Entry( const TQString& pKey, TQ_UINT64 nDefault) const
721 {
722  return readUnsignedNum64Entry(pKey.utf8().data(), nDefault);
723 }
724 
725 TQ_UINT64 KConfigBase::readUnsignedNum64Entry( const char *pKey, TQ_UINT64 nDefault) const
726 {
727  // Note that TQCString::toULongLong() is missing, we muse use a TQString instead.
728  TQString aValue = readEntry( pKey );
729  if( aValue.isNull() )
730  return nDefault;
731  else
732  {
733  bool ok;
734  TQ_UINT64 rc = aValue.toULongLong( &ok );
735  return( ok ? rc : nDefault );
736  }
737 }
738 
739 double KConfigBase::readDoubleNumEntry( const TQString& pKey, double nDefault) const
740 {
741  return readDoubleNumEntry(pKey.utf8().data(), nDefault);
742 }
743 
744 double KConfigBase::readDoubleNumEntry( const char *pKey, double nDefault) const
745 {
746  TQCString aValue = readEntryUtf8( pKey );
747  if( aValue.isNull() )
748  return nDefault;
749  else
750  {
751  bool ok;
752  double rc = aValue.toDouble( &ok );
753  return( ok ? rc : nDefault );
754  }
755 }
756 
757 
758 bool KConfigBase::readBoolEntry( const TQString& pKey, bool bDefault ) const
759 {
760  return readBoolEntry(pKey.utf8().data(), bDefault);
761 }
762 
763 bool KConfigBase::readBoolEntry( const char *pKey, bool bDefault ) const
764 {
765  TQCString aValue = readEntryUtf8( pKey );
766 
767  if( aValue.isNull() )
768  return bDefault;
769  else
770  {
771  if( aValue == "true" || aValue == "on" || aValue == "yes" || aValue == "1" )
772  return true;
773  else
774  {
775  bool bOK;
776  int val = aValue.toInt( &bOK );
777  if( bOK && val != 0 )
778  return true;
779  else
780  return false;
781  }
782  }
783 }
784 
785 TQFont KConfigBase::readFontEntry( const TQString& pKey, const TQFont* pDefault ) const
786 {
787  return readFontEntry(pKey.utf8().data(), pDefault);
788 }
789 
790 TQFont KConfigBase::readFontEntry( const char *pKey, const TQFont* pDefault ) const
791 {
792  TQFont aRetFont;
793 
794  TQString aValue = readEntry( pKey );
795  if( !aValue.isNull() ) {
796  if ( aValue.contains( ',' ) > 5 ) {
797  // KDE3 and upwards entry
798  if ( !aRetFont.fromString( aValue ) && pDefault )
799  aRetFont = *pDefault;
800  }
801  else {
802  // backward compatibility with older font formats
803  // ### remove KDE 3.1 ?
804  // find first part (font family)
805  int nIndex = aValue.find( ',' );
806  if( nIndex == -1 ){
807  if( pDefault )
808  aRetFont = *pDefault;
809  return aRetFont;
810  }
811  aRetFont.setFamily( aValue.left( nIndex ) );
812 
813  // find second part (point size)
814  int nOldIndex = nIndex;
815  nIndex = aValue.find( ',', nOldIndex+1 );
816  if( nIndex == -1 ){
817  if( pDefault )
818  aRetFont = *pDefault;
819  return aRetFont;
820  }
821 
822  aRetFont.setPointSize( aValue.mid( nOldIndex+1,
823  nIndex-nOldIndex-1 ).toInt() );
824 
825  // find third part (style hint)
826  nOldIndex = nIndex;
827  nIndex = aValue.find( ',', nOldIndex+1 );
828 
829  if( nIndex == -1 ){
830  if( pDefault )
831  aRetFont = *pDefault;
832  return aRetFont;
833  }
834 
835  aRetFont.setStyleHint( (TQFont::StyleHint)aValue.mid( nOldIndex+1, nIndex-nOldIndex-1 ).toUInt() );
836 
837  // find fourth part (char set)
838  nOldIndex = nIndex;
839  nIndex = aValue.find( ',', nOldIndex+1 );
840 
841  if( nIndex == -1 ){
842  if( pDefault )
843  aRetFont = *pDefault;
844  return aRetFont;
845  }
846 
847  TQString chStr=aValue.mid( nOldIndex+1,
848  nIndex-nOldIndex-1 );
849  // find fifth part (weight)
850  nOldIndex = nIndex;
851  nIndex = aValue.find( ',', nOldIndex+1 );
852 
853  if( nIndex == -1 ){
854  if( pDefault )
855  aRetFont = *pDefault;
856  return aRetFont;
857  }
858 
859  aRetFont.setWeight( aValue.mid( nOldIndex+1,
860  nIndex-nOldIndex-1 ).toUInt() );
861 
862  // find sixth part (font bits)
863  uint nFontBits = aValue.right( aValue.length()-nIndex-1 ).toUInt();
864 
865  aRetFont.setItalic( nFontBits & 0x01 );
866  aRetFont.setUnderline( nFontBits & 0x02 );
867  aRetFont.setStrikeOut( nFontBits & 0x04 );
868  aRetFont.setFixedPitch( nFontBits & 0x08 );
869  aRetFont.setRawMode( nFontBits & 0x20 );
870  }
871  }
872  else
873  {
874  if( pDefault )
875  aRetFont = *pDefault;
876  }
877 
878  return aRetFont;
879 }
880 
881 
882 TQRect KConfigBase::readRectEntry( const TQString& pKey, const TQRect* pDefault ) const
883 {
884  return readRectEntry(pKey.utf8().data(), pDefault);
885 }
886 
887 TQRect KConfigBase::readRectEntry( const char *pKey, const TQRect* pDefault ) const
888 {
889  TQCString aValue = readEntryUtf8(pKey);
890 
891  if (!aValue.isEmpty())
892  {
893  int left, top, width, height;
894 
895  if (sscanf(aValue.data(), "%d,%d,%d,%d", &left, &top, &width, &height) == 4)
896  {
897  return TQRect(left, top, width, height);
898  }
899  }
900  if (pDefault)
901  return *pDefault;
902  return TQRect();
903 }
904 
905 
906 TQPoint KConfigBase::readPointEntry( const TQString& pKey,
907  const TQPoint* pDefault ) const
908 {
909  return readPointEntry(pKey.utf8().data(), pDefault);
910 }
911 
912 TQPoint KConfigBase::readPointEntry( const char *pKey,
913  const TQPoint* pDefault ) const
914 {
915  TQCString aValue = readEntryUtf8(pKey);
916 
917  if (!aValue.isEmpty())
918  {
919  int x,y;
920 
921  if (sscanf(aValue.data(), "%d,%d", &x, &y) == 2)
922  {
923  return TQPoint(x,y);
924  }
925  }
926  if (pDefault)
927  return *pDefault;
928  return TQPoint();
929 }
930 
931 TQSize KConfigBase::readSizeEntry( const TQString& pKey,
932  const TQSize* pDefault ) const
933 {
934  return readSizeEntry(pKey.utf8().data(), pDefault);
935 }
936 
937 TQSize KConfigBase::readSizeEntry( const char *pKey,
938  const TQSize* pDefault ) const
939 {
940  TQCString aValue = readEntryUtf8(pKey);
941 
942  if (!aValue.isEmpty())
943  {
944  int width,height;
945 
946  if (sscanf(aValue.data(), "%d,%d", &width, &height) == 2)
947  {
948  return TQSize(width, height);
949  }
950  }
951  if (pDefault)
952  return *pDefault;
953  return TQSize();
954 }
955 
956 
957 TQColor KConfigBase::readColorEntry( const TQString& pKey,
958  const TQColor* pDefault ) const
959 {
960  return readColorEntry(pKey.utf8().data(), pDefault);
961 }
962 
963 TQColor KConfigBase::readColorEntry( const char *pKey,
964  const TQColor* pDefault ) const
965 {
966  TQColor aRetColor;
967  int nRed = 0, nGreen = 0, nBlue = 0;
968 
969  TQString aValue = readEntry( pKey );
970  if( !aValue.isEmpty() )
971  {
972  if ( aValue.at(0) == (QChar)'#' )
973  {
974  aRetColor.setNamedColor(aValue);
975  }
976  else
977  {
978 
979  bool bOK;
980 
981  // find first part (red)
982  int nIndex = aValue.find( ',' );
983 
984  if( nIndex == -1 ){
985  // return a sensible default -- Bernd
986  if( pDefault )
987  aRetColor = *pDefault;
988  return aRetColor;
989  }
990 
991  nRed = aValue.left( nIndex ).toInt( &bOK );
992 
993  // find second part (green)
994  int nOldIndex = nIndex;
995  nIndex = aValue.find( ',', nOldIndex+1 );
996 
997  if( nIndex == -1 ){
998  // return a sensible default -- Bernd
999  if( pDefault )
1000  aRetColor = *pDefault;
1001  return aRetColor;
1002  }
1003  nGreen = aValue.mid( nOldIndex+1,
1004  nIndex-nOldIndex-1 ).toInt( &bOK );
1005 
1006  // find third part (blue)
1007  nBlue = aValue.right( aValue.length()-nIndex-1 ).toInt( &bOK );
1008 
1009  aRetColor.setRgb( nRed, nGreen, nBlue );
1010  }
1011  }
1012  else {
1013 
1014  if( pDefault )
1015  aRetColor = *pDefault;
1016  }
1017 
1018  return aRetColor;
1019 }
1020 
1021 
1022 TQDateTime KConfigBase::readDateTimeEntry( const TQString& pKey,
1023  const TQDateTime* pDefault ) const
1024 {
1025  return readDateTimeEntry(pKey.utf8().data(), pDefault);
1026 }
1027 
1028 // ### currentDateTime() as fallback ? (Harri)
1029 TQDateTime KConfigBase::readDateTimeEntry( const char *pKey,
1030  const TQDateTime* pDefault ) const
1031 {
1032  if( !hasKey( pKey ) )
1033  {
1034  if( pDefault )
1035  return *pDefault;
1036  else
1037  return TQDateTime::currentDateTime();
1038  }
1039 
1040  TQStrList list;
1041  int count = readListEntry( pKey, list, ',' );
1042  if( count == 6 ) {
1043  TQDate date( atoi( list.at( 0 ) ), atoi( list.at( 1 ) ),
1044  atoi( list.at( 2 ) ) );
1045  TQTime time( atoi( list.at( 3 ) ), atoi( list.at( 4 ) ),
1046  atoi( list.at( 5 ) ) );
1047 
1048  return TQDateTime( date, time );
1049  }
1050 
1051  return TQDateTime::currentDateTime();
1052 }
1053 
1054 void KConfigBase::writeEntry( const TQString& pKey, const TQString& value,
1055  bool bPersistent,
1056  bool bGlobal,
1057  bool bNLS )
1058 {
1059  writeEntry(pKey.utf8().data(), value, bPersistent, bGlobal, bNLS);
1060 }
1061 
1062 void KConfigBase::writeEntry( const char *pKey, const TQString& value,
1063  bool bPersistent,
1064  bool bGlobal,
1065  bool bNLS )
1066 {
1067  writeEntry(pKey, value, bPersistent, bGlobal, bNLS, false);
1068 }
1069 
1070 void KConfigBase::writeEntry( const char *pKey, const TQString& value,
1071  bool bPersistent,
1072  bool bGlobal,
1073  bool bNLS,
1074  bool bExpand )
1075 {
1076  // the KConfig object is dirty now
1077  // set this before any IO takes place so that if any derivative
1078  // classes do caching, they won't try and flush the cache out
1079  // from under us before we read. A race condition is still
1080  // possible but minimized.
1081  if( bPersistent )
1082  setDirty(true);
1083 
1084  if (!bLocaleInitialized && KGlobal::locale())
1085  setLocale();
1086 
1087  KEntryKey entryKey(mGroup, pKey);
1088  entryKey.bLocal = bNLS;
1089 
1090  KEntry aEntryData;
1091  aEntryData.mValue = value.utf8(); // set new value
1092  aEntryData.bGlobal = bGlobal;
1093  aEntryData.bNLS = bNLS;
1094  aEntryData.bExpand = bExpand;
1095 
1096  if (bPersistent)
1097  aEntryData.bDirty = true;
1098 
1099  // rewrite the new value
1100  putData(entryKey, aEntryData, true);
1101 }
1102 
1103 void KConfigBase::writePathEntry( const TQString& pKey, const TQString & path,
1104  bool bPersistent, bool bGlobal,
1105  bool bNLS)
1106 {
1107  writePathEntry(pKey.utf8().data(), path, bPersistent, bGlobal, bNLS);
1108 }
1109 
1110 
1111 static bool cleanHomeDirPath( TQString &path, const TQString &homeDir )
1112 {
1113 #ifdef Q_WS_WIN //safer
1114  if (!TQDir::convertSeparators(path).startsWith(TQDir::convertSeparators(homeDir)))
1115  return false;
1116 #else
1117  if (!path.startsWith(homeDir))
1118  return false;
1119 #endif
1120 
1121  unsigned int len = homeDir.length();
1122  // replace by "$HOME" if possible
1123  if (len && (path.length() == len || path[len] == '/')) {
1124  path.replace(0, len, TQString::fromLatin1("$HOME"));
1125  return true;
1126  } else
1127  return false;
1128 }
1129 
1130 static TQString translatePath( TQString path )
1131 {
1132  if (path.isEmpty())
1133  return path;
1134 
1135  // only "our" $HOME should be interpreted
1136  path.replace('$', "$$");
1137 
1138  bool startsWithFile = path.startsWith("file:", false);
1139 
1140  // return original path, if it refers to another type of URL (e.g. http:/), or
1141  // if the path is already relative to another directory
1142  if (((!startsWithFile) && (path[0] != '/')) || (startsWithFile && (path[5] != '/'))) {
1143  return path;
1144  }
1145 
1146  if (startsWithFile) {
1147  path.remove(0,5); // strip leading "file:/" off the string
1148  }
1149 
1150  // keep only one single '/' at the beginning - needed for cleanHomeDirPath()
1151  while (path[0] == '/' && path[1] == '/') {
1152  path.remove(0,1);
1153  }
1154 
1155  // we can not use KGlobal::dirs()->relativeLocation("home", path) here,
1156  // since it would not recognize paths without a trailing '/'.
1157  // All of the 3 following functions to return the user's home directory
1158  // can return different paths. We have to test all them.
1159  TQString homeDir0 = TQFile::decodeName(getenv("HOME"));
1160  TQString homeDir1 = TQDir::homeDirPath();
1161  TQString homeDir2 = TQDir(homeDir1).canonicalPath();
1162  if (cleanHomeDirPath(path, homeDir0) ||
1163  cleanHomeDirPath(path, homeDir1) ||
1164  cleanHomeDirPath(path, homeDir2) ) {
1165  // kdDebug() << "Path was replaced\n";
1166  }
1167 
1168  if (startsWithFile)
1169  path.prepend( "file://" );
1170 
1171  return path;
1172 }
1173 
1174 void KConfigBase::writePathEntry( const char *pKey, const TQString & path,
1175  bool bPersistent, bool bGlobal,
1176  bool bNLS)
1177 {
1178  writeEntry(pKey, translatePath(path), bPersistent, bGlobal, bNLS, true);
1179 }
1180 
1181 void KConfigBase::writePathEntry ( const TQString& pKey, const TQStringList &list,
1182  char sep , bool bPersistent,
1183  bool bGlobal, bool bNLS )
1184 {
1185  writePathEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
1186 }
1187 
1188 void KConfigBase::writePathEntry ( const char *pKey, const TQStringList &list,
1189  char sep , bool bPersistent,
1190  bool bGlobal, bool bNLS )
1191 {
1192  if( list.isEmpty() )
1193  {
1194  writeEntry( pKey, TQString::fromLatin1(""), bPersistent );
1195  return;
1196  }
1197  TQStringList new_list;
1198  TQStringList::ConstIterator it = list.begin();
1199  for( ; it != list.end(); ++it )
1200  {
1201  TQString value = *it;
1202  new_list.append( translatePath(value) );
1203  }
1204  writeEntry( pKey, new_list, sep, bPersistent, bGlobal, bNLS, true );
1205 }
1206 
1207 void KConfigBase::deleteEntry( const TQString& pKey,
1208  bool bNLS,
1209  bool bGlobal)
1210 {
1211  deleteEntry(pKey.utf8().data(), bNLS, bGlobal);
1212 }
1213 
1214 void KConfigBase::deleteEntry( const char *pKey,
1215  bool bNLS,
1216  bool bGlobal)
1217 {
1218  // the KConfig object is dirty now
1219  // set this before any IO takes place so that if any derivative
1220  // classes do caching, they won't try and flush the cache out
1221  // from under us before we read. A race condition is still
1222  // possible but minimized.
1223  setDirty(true);
1224 
1225  if (!bLocaleInitialized && KGlobal::locale())
1226  setLocale();
1227 
1228  KEntryKey entryKey(mGroup, pKey);
1229  KEntry aEntryData;
1230 
1231  aEntryData.bGlobal = bGlobal;
1232  aEntryData.bNLS = bNLS;
1233  aEntryData.bDirty = true;
1234  aEntryData.bDeleted = true;
1235 
1236  // rewrite the new value
1237  putData(entryKey, aEntryData, true);
1238 }
1239 
1240 bool KConfigBase::deleteGroup( const TQString& group, bool bDeep, bool bGlobal )
1241 {
1242  KEntryMap aEntryMap = internalEntryMap(group);
1243 
1244  if (!bDeep) {
1245  // Check if it empty
1246  return aEntryMap.isEmpty();
1247  }
1248 
1249  bool dirty = false;
1250  bool checkGroup = true;
1251  // we want to remove all entries in the group
1252  KEntryMapIterator aIt;
1253  for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt)
1254  {
1255  if (!aIt.key().mKey.isEmpty() && !aIt.key().bDefault && !(*aIt).bDeleted)
1256  {
1257  (*aIt).bDeleted = true;
1258  (*aIt).bDirty = true;
1259  (*aIt).bGlobal = bGlobal;
1260  (*aIt).mValue = 0;
1261  putData(aIt.key(), *aIt, checkGroup);
1262  checkGroup = false;
1263  dirty = true;
1264  }
1265  }
1266  if (dirty)
1267  setDirty(true);
1268  return true;
1269 }
1270 
1271 void KConfigBase::writeEntry ( const TQString& pKey, const TQVariant &prop,
1272  bool bPersistent,
1273  bool bGlobal, bool bNLS )
1274 {
1275  writeEntry(pKey.utf8().data(), prop, bPersistent, bGlobal, bNLS);
1276 }
1277 
1278 void KConfigBase::writeEntry ( const char *pKey, const TQVariant &prop,
1279  bool bPersistent,
1280  bool bGlobal, bool bNLS )
1281 {
1282  switch( prop.type() )
1283  {
1284  case TQVariant::Invalid:
1285  writeEntry( pKey, "", bPersistent, bGlobal, bNLS );
1286  return;
1287  case TQVariant::String:
1288  writeEntry( pKey, prop.toString(), bPersistent, bGlobal, bNLS );
1289  return;
1290  case TQVariant::StringList:
1291  writeEntry( pKey, prop.toStringList(), ',', bPersistent, bGlobal, bNLS );
1292  return;
1293  case TQVariant::List: {
1294  TQValueList<TQVariant> list = prop.toList();
1295  TQValueList<TQVariant>::ConstIterator it = list.begin();
1296  TQValueList<TQVariant>::ConstIterator end = list.end();
1297  TQStringList strList;
1298 
1299  for (; it != end; ++it )
1300  strList.append( (*it).toString() );
1301 
1302  writeEntry( pKey, strList, ',', bPersistent, bGlobal, bNLS );
1303 
1304  return;
1305  }
1306  case TQVariant::Font:
1307  writeEntry( pKey, prop.toFont(), bPersistent, bGlobal, bNLS );
1308  return;
1309  case TQVariant::Point:
1310  writeEntry( pKey, prop.toPoint(), bPersistent, bGlobal, bNLS );
1311  return;
1312  case TQVariant::Rect:
1313  writeEntry( pKey, prop.toRect(), bPersistent, bGlobal, bNLS );
1314  return;
1315  case TQVariant::Size:
1316  writeEntry( pKey, prop.toSize(), bPersistent, bGlobal, bNLS );
1317  return;
1318  case TQVariant::Color:
1319  writeEntry( pKey, prop.toColor(), bPersistent, bGlobal, bNLS );
1320  return;
1321  case TQVariant::Int:
1322  writeEntry( pKey, prop.toInt(), bPersistent, bGlobal, bNLS );
1323  return;
1324  case TQVariant::UInt:
1325  writeEntry( pKey, prop.toUInt(), bPersistent, bGlobal, bNLS );
1326  return;
1327  case TQVariant::LongLong:
1328  writeEntry( pKey, prop.toLongLong(), bPersistent, bGlobal, bNLS );
1329  return;
1330  case TQVariant::ULongLong:
1331  writeEntry( pKey, prop.toULongLong(), bPersistent, bGlobal, bNLS );
1332  return;
1333  case TQVariant::Bool:
1334  writeEntry( pKey, prop.toBool(), bPersistent, bGlobal, bNLS );
1335  return;
1336  case TQVariant::Double:
1337  writeEntry( pKey, prop.toDouble(), bPersistent, bGlobal, 'g', 6, bNLS );
1338  return;
1339  case TQVariant::DateTime:
1340  writeEntry( pKey, prop.toDateTime(), bPersistent, bGlobal, bNLS);
1341  return;
1342  case TQVariant::Date:
1343  writeEntry( pKey, TQDateTime(prop.toDate()), bPersistent, bGlobal, bNLS);
1344  return;
1345 
1346  case TQVariant::Pixmap:
1347  case TQVariant::Image:
1348  case TQVariant::Brush:
1349  case TQVariant::Palette:
1350  case TQVariant::ColorGroup:
1351  case TQVariant::Map:
1352  case TQVariant::IconSet:
1353  case TQVariant::CString:
1354  case TQVariant::PointArray:
1355  case TQVariant::Region:
1356  case TQVariant::Bitmap:
1357  case TQVariant::Cursor:
1358  case TQVariant::SizePolicy:
1359  case TQVariant::Time:
1360 #ifdef USE_QT3
1361  case TQVariant::ByteArray:
1362 #endif // USE_QT3
1363  case TQVariant::BitArray:
1364  case TQVariant::KeySequence:
1365  case TQVariant::Pen:
1366 #ifdef USE_QT4
1367  case TQVariant::Char:
1368  case TQVariant::Url:
1369  case TQVariant::Locale:
1370  case TQVariant::RectF:
1371  case TQVariant::SizeF:
1372  case TQVariant::Line:
1373  case TQVariant::LineF:
1374  case TQVariant::PointF:
1375  case TQVariant::RegExp:
1376  case TQVariant::Hash:
1377  case TQVariant::TextLength:
1378  case QVariant::TextFormat:
1379  case TQVariant::Matrix:
1380  case TQVariant::Transform:
1381  case TQVariant::Matrix4x4:
1382  case TQVariant::Vector2D:
1383  case TQVariant::Vector3D:
1384  case TQVariant::Vector4D:
1385  case TQVariant::Quaternion:
1386  case TQVariant::UserType:
1387 #endif // USE_QT4
1388  break;
1389  }
1390 
1391  Q_ASSERT( 0 );
1392 }
1393 
1394 void KConfigBase::writeEntry ( const TQString& pKey, const TQStrList &list,
1395  char sep , bool bPersistent,
1396  bool bGlobal, bool bNLS )
1397 {
1398  writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
1399 }
1400 
1401 void KConfigBase::writeEntry ( const char *pKey, const TQStrList &list,
1402  char sep , bool bPersistent,
1403  bool bGlobal, bool bNLS )
1404 {
1405  if( list.isEmpty() )
1406  {
1407  writeEntry( pKey, TQString::fromLatin1(""), bPersistent );
1408  return;
1409  }
1410  TQString str_list;
1411  TQStrListIterator it( list );
1412  for( ; it.current(); ++it )
1413  {
1414  uint i;
1415  TQString value;
1416  // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
1417  // A TQStrList may contain values in 8bit locale cpecified
1418  // encoding or in UTF8 encoding.
1419  value = KStringHandler::from8Bit(it.current());
1420  uint strLengh(value.length());
1421  for( i = 0; i < strLengh; i++ )
1422  {
1423  if( value[i] == sep || value[i] == '\\' )
1424  str_list += '\\';
1425  str_list += value[i];
1426  }
1427  str_list += sep;
1428  }
1429  if( str_list.at(str_list.length() - 1) == (QChar)sep )
1430  str_list.truncate( str_list.length() -1 );
1431  writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS );
1432 }
1433 
1434 void KConfigBase::writeEntry ( const TQString& pKey, const TQStringList &list,
1435  char sep , bool bPersistent,
1436  bool bGlobal, bool bNLS )
1437 {
1438  writeEntry(pKey.utf8().data(), list, sep, bPersistent, bGlobal, bNLS);
1439 }
1440 
1441 void KConfigBase::writeEntry ( const char *pKey, const TQStringList &list,
1442  char sep , bool bPersistent,
1443  bool bGlobal, bool bNLS )
1444 {
1445  writeEntry(pKey, list, sep, bPersistent, bGlobal, bNLS, false);
1446 }
1447 
1448 void KConfigBase::writeEntry ( const char *pKey, const TQStringList &list,
1449  char sep, bool bPersistent,
1450  bool bGlobal, bool bNLS, bool bExpand )
1451 {
1452  if( list.isEmpty() )
1453  {
1454  writeEntry( pKey, TQString::fromLatin1(""), bPersistent );
1455  return;
1456  }
1457  TQString str_list;
1458  str_list.reserve( 4096 );
1459  TQStringList::ConstIterator it = list.begin();
1460  for( ; it != list.end(); ++it )
1461  {
1462  TQString value = *it;
1463  uint i;
1464  uint strLength(value.length());
1465  for( i = 0; i < strLength; i++ )
1466  {
1467  if( value[i] == sep || value[i] == '\\' )
1468  str_list += '\\';
1469  str_list += value[i];
1470  }
1471  str_list += sep;
1472  }
1473  if( str_list.at(str_list.length() - 1) == (QChar)sep )
1474  str_list.truncate( str_list.length() -1 );
1475  writeEntry( pKey, str_list, bPersistent, bGlobal, bNLS, bExpand );
1476 }
1477 
1478 void KConfigBase::writeEntry ( const TQString& pKey, const TQValueList<int> &list,
1479  bool bPersistent, bool bGlobal, bool bNLS )
1480 {
1481  writeEntry(pKey.utf8().data(), list, bPersistent, bGlobal, bNLS);
1482 }
1483 
1484 void KConfigBase::writeEntry ( const char *pKey, const TQValueList<int> &list,
1485  bool bPersistent, bool bGlobal, bool bNLS )
1486 {
1487  TQStringList strlist;
1488  TQValueList<int>::ConstIterator end = list.end();
1489  for (TQValueList<int>::ConstIterator it = list.begin(); it != end; it++)
1490  strlist << TQString::number(*it);
1491  writeEntry(pKey, strlist, ',', bPersistent, bGlobal, bNLS );
1492 }
1493 
1494 void KConfigBase::writeEntry( const TQString& pKey, int nValue,
1495  bool bPersistent, bool bGlobal,
1496  bool bNLS )
1497 {
1498  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1499 }
1500 
1501 void KConfigBase::writeEntry( const char *pKey, int nValue,
1502  bool bPersistent, bool bGlobal,
1503  bool bNLS )
1504 {
1505  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1506 }
1507 
1508 
1509 void KConfigBase::writeEntry( const TQString& pKey, unsigned int nValue,
1510  bool bPersistent, bool bGlobal,
1511  bool bNLS )
1512 {
1513  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1514 }
1515 
1516 void KConfigBase::writeEntry( const char *pKey, unsigned int nValue,
1517  bool bPersistent, bool bGlobal,
1518  bool bNLS )
1519 {
1520  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1521 }
1522 
1523 
1524 void KConfigBase::writeEntry( const TQString& pKey, long nValue,
1525  bool bPersistent, bool bGlobal,
1526  bool bNLS )
1527 {
1528  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1529 }
1530 
1531 void KConfigBase::writeEntry( const char *pKey, long nValue,
1532  bool bPersistent, bool bGlobal,
1533  bool bNLS )
1534 {
1535  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1536 }
1537 
1538 
1539 void KConfigBase::writeEntry( const TQString& pKey, unsigned long nValue,
1540  bool bPersistent, bool bGlobal,
1541  bool bNLS )
1542 {
1543  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1544 }
1545 
1546 void KConfigBase::writeEntry( const char *pKey, unsigned long nValue,
1547  bool bPersistent, bool bGlobal,
1548  bool bNLS )
1549 {
1550  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1551 }
1552 
1553 void KConfigBase::writeEntry( const TQString& pKey, TQ_INT64 nValue,
1554  bool bPersistent, bool bGlobal,
1555  bool bNLS )
1556 {
1557  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1558 }
1559 
1560 void KConfigBase::writeEntry( const char *pKey, TQ_INT64 nValue,
1561  bool bPersistent, bool bGlobal,
1562  bool bNLS )
1563 {
1564  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1565 }
1566 
1567 
1568 void KConfigBase::writeEntry( const TQString& pKey, TQ_UINT64 nValue,
1569  bool bPersistent, bool bGlobal,
1570  bool bNLS )
1571 {
1572  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1573 }
1574 
1575 void KConfigBase::writeEntry( const char *pKey, TQ_UINT64 nValue,
1576  bool bPersistent, bool bGlobal,
1577  bool bNLS )
1578 {
1579  writeEntry( pKey, TQString::number(nValue), bPersistent, bGlobal, bNLS );
1580 }
1581 
1582 void KConfigBase::writeEntry( const TQString& pKey, double nValue,
1583  bool bPersistent, bool bGlobal,
1584  char format, int precision,
1585  bool bNLS )
1586 {
1587  writeEntry( pKey, TQString::number(nValue, format, precision),
1588  bPersistent, bGlobal, bNLS );
1589 }
1590 
1591 void KConfigBase::writeEntry( const char *pKey, double nValue,
1592  bool bPersistent, bool bGlobal,
1593  char format, int precision,
1594  bool bNLS )
1595 {
1596  writeEntry( pKey, TQString::number(nValue, format, precision),
1597  bPersistent, bGlobal, bNLS );
1598 }
1599 
1600 
1601 void KConfigBase::writeEntry( const TQString& pKey, bool bValue,
1602  bool bPersistent,
1603  bool bGlobal,
1604  bool bNLS )
1605 {
1606  writeEntry(pKey.utf8().data(), bValue, bPersistent, bGlobal, bNLS);
1607 }
1608 
1609 void KConfigBase::writeEntry( const char *pKey, bool bValue,
1610  bool bPersistent,
1611  bool bGlobal,
1612  bool bNLS )
1613 {
1614  TQString aValue;
1615 
1616  if( bValue )
1617  aValue = "true";
1618  else
1619  aValue = "false";
1620 
1621  writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
1622 }
1623 
1624 
1625 void KConfigBase::writeEntry( const TQString& pKey, const TQFont& rFont,
1626  bool bPersistent, bool bGlobal,
1627  bool bNLS )
1628 {
1629  writeEntry(pKey.utf8().data(), rFont, bPersistent, bGlobal, bNLS);
1630 }
1631 
1632 void KConfigBase::writeEntry( const char *pKey, const TQFont& rFont,
1633  bool bPersistent, bool bGlobal,
1634  bool bNLS )
1635 {
1636  writeEntry( pKey, TQString(rFont.toString()), bPersistent, bGlobal, bNLS );
1637 }
1638 
1639 
1640 void KConfigBase::writeEntry( const TQString& pKey, const TQRect& rRect,
1641  bool bPersistent, bool bGlobal,
1642  bool bNLS )
1643 {
1644  writeEntry(pKey.utf8().data(), rRect, bPersistent, bGlobal, bNLS);
1645 }
1646 
1647 void KConfigBase::writeEntry( const char *pKey, const TQRect& rRect,
1648  bool bPersistent, bool bGlobal,
1649  bool bNLS )
1650 {
1651  TQStrList list;
1652  TQCString tempstr;
1653  list.insert( 0, tempstr.setNum( rRect.left() ) );
1654  list.insert( 1, tempstr.setNum( rRect.top() ) );
1655  list.insert( 2, tempstr.setNum( rRect.width() ) );
1656  list.insert( 3, tempstr.setNum( rRect.height() ) );
1657 
1658  writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
1659 }
1660 
1661 
1662 void KConfigBase::writeEntry( const TQString& pKey, const TQPoint& rPoint,
1663  bool bPersistent, bool bGlobal,
1664  bool bNLS )
1665 {
1666  writeEntry(pKey.utf8().data(), rPoint, bPersistent, bGlobal, bNLS);
1667 }
1668 
1669 void KConfigBase::writeEntry( const char *pKey, const TQPoint& rPoint,
1670  bool bPersistent, bool bGlobal,
1671  bool bNLS )
1672 {
1673  TQStrList list;
1674  TQCString tempstr;
1675  list.insert( 0, tempstr.setNum( rPoint.x() ) );
1676  list.insert( 1, tempstr.setNum( rPoint.y() ) );
1677 
1678  writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
1679 }
1680 
1681 
1682 void KConfigBase::writeEntry( const TQString& pKey, const TQSize& rSize,
1683  bool bPersistent, bool bGlobal,
1684  bool bNLS )
1685 {
1686  writeEntry(pKey.utf8().data(), rSize, bPersistent, bGlobal, bNLS);
1687 }
1688 
1689 void KConfigBase::writeEntry( const char *pKey, const TQSize& rSize,
1690  bool bPersistent, bool bGlobal,
1691  bool bNLS )
1692 {
1693  TQStrList list;
1694  TQCString tempstr;
1695  list.insert( 0, tempstr.setNum( rSize.width() ) );
1696  list.insert( 1, tempstr.setNum( rSize.height() ) );
1697 
1698  writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
1699 }
1700 
1701 void KConfigBase::writeEntry( const TQString& pKey, const TQColor& rColor,
1702  bool bPersistent,
1703  bool bGlobal,
1704  bool bNLS )
1705 {
1706  writeEntry( pKey.utf8().data(), rColor, bPersistent, bGlobal, bNLS);
1707 }
1708 
1709 void KConfigBase::writeEntry( const char *pKey, const TQColor& rColor,
1710  bool bPersistent,
1711  bool bGlobal,
1712  bool bNLS )
1713 {
1714  TQString aValue;
1715  if (rColor.isValid())
1716  aValue.sprintf( "%d,%d,%d", rColor.red(), rColor.green(), rColor.blue() );
1717  else
1718  aValue = "invalid";
1719 
1720  writeEntry( pKey, aValue, bPersistent, bGlobal, bNLS );
1721 }
1722 
1723 void KConfigBase::writeEntry( const TQString& pKey, const TQDateTime& rDateTime,
1724  bool bPersistent, bool bGlobal,
1725  bool bNLS )
1726 {
1727  writeEntry(pKey.utf8().data(), rDateTime, bPersistent, bGlobal, bNLS);
1728 }
1729 
1730 void KConfigBase::writeEntry( const char *pKey, const TQDateTime& rDateTime,
1731  bool bPersistent, bool bGlobal,
1732  bool bNLS )
1733 {
1734  TQStrList list;
1735  TQCString tempstr;
1736 
1737  TQTime time = TQT_TQTIME_OBJECT(rDateTime.time());
1738  TQDate date = TQT_TQDATE_OBJECT(rDateTime.date());
1739 
1740  list.insert( 0, tempstr.setNum( date.year() ) );
1741  list.insert( 1, tempstr.setNum( date.month() ) );
1742  list.insert( 2, tempstr.setNum( date.day() ) );
1743 
1744  list.insert( 3, tempstr.setNum( time.hour() ) );
1745  list.insert( 4, tempstr.setNum( time.minute() ) );
1746  list.insert( 5, tempstr.setNum( time.second() ) );
1747 
1748  writeEntry( pKey, list, ',', bPersistent, bGlobal, bNLS );
1749 }
1750 
1751 void KConfigBase::parseConfigFiles()
1752 {
1753  if (!bLocaleInitialized && KGlobal::_locale) {
1754  setLocale();
1755  }
1756  if (backEnd)
1757  {
1758  backEnd->parseConfigFiles();
1759  bReadOnly = (backEnd->getConfigState() == ReadOnly);
1760  }
1761 }
1762 
1763 void KConfigBase::sync()
1764 {
1765  if (isReadOnly())
1766  return;
1767 
1768  if (backEnd)
1769  backEnd->sync();
1770  if (bDirty)
1771  rollback();
1772 }
1773 
1774 KConfigBase::ConfigState KConfigBase::getConfigState() const {
1775  if (backEnd)
1776  return backEnd->getConfigState();
1777  return ReadOnly;
1778 }
1779 
1780 void KConfigBase::rollback( bool /*bDeep = true*/ )
1781 {
1782  bDirty = false;
1783 }
1784 
1785 
1786 void KConfigBase::setReadDefaults(bool b)
1787 {
1788  if (!d)
1789  {
1790  if (!b) return;
1791  d = new KConfigBasePrivate();
1792  }
1793 
1794  d->readDefaults = b;
1795 }
1796 
1797 bool KConfigBase::readDefaults() const
1798 {
1799  return (d && d->readDefaults);
1800 }
1801 
1802 void KConfigBase::revertToDefault(const TQString &key)
1803 {
1804  setDirty(true);
1805 
1806  KEntryKey aEntryKey(mGroup, key.utf8());
1807  aEntryKey.bDefault = true;
1808 
1809  if (!locale().isNull()) {
1810  // try the localized key first
1811  aEntryKey.bLocal = true;
1812  KEntry entry = lookupData(aEntryKey);
1813  if (entry.mValue.isNull())
1814  entry.bDeleted = true;
1815 
1816  entry.bDirty = true;
1817  putData(aEntryKey, entry, true); // Revert
1818  aEntryKey.bLocal = false;
1819  }
1820 
1821  // try the non-localized version
1822  KEntry entry = lookupData(aEntryKey);
1823  if (entry.mValue.isNull())
1824  entry.bDeleted = true;
1825  entry.bDirty = true;
1826  putData(aEntryKey, entry, true); // Revert
1827 }
1828 
1829 bool KConfigBase::hasDefault(const TQString &key) const
1830 {
1831  KEntryKey aEntryKey(mGroup, key.utf8());
1832  aEntryKey.bDefault = true;
1833 
1834  if (!locale().isNull()) {
1835  // try the localized key first
1836  aEntryKey.bLocal = true;
1837  KEntry entry = lookupData(aEntryKey);
1838  if (!entry.mValue.isNull())
1839  return true;
1840 
1841  aEntryKey.bLocal = false;
1842  }
1843 
1844  // try the non-localized version
1845  KEntry entry = lookupData(aEntryKey);
1846  if (!entry.mValue.isNull())
1847  return true;
1848 
1849  return false;
1850 }
1851 
1852 
1853 
1854 KConfigGroup::KConfigGroup(KConfigBase *master, const TQString &group)
1855 {
1856  mMaster = master;
1857  backEnd = mMaster->backEnd; // Needed for getConfigState()
1858  bLocaleInitialized = true;
1859  bReadOnly = mMaster->bReadOnly;
1860  bExpand = false;
1861  bDirty = false; // Not used
1862  mGroup = group.utf8();
1863  aLocaleString = mMaster->aLocaleString;
1864  setReadDefaults(mMaster->readDefaults());
1865 }
1866 
1867 KConfigGroup::KConfigGroup(KConfigBase *master, const TQCString &group)
1868 {
1869  mMaster = master;
1870  backEnd = mMaster->backEnd; // Needed for getConfigState()
1871  bLocaleInitialized = true;
1872  bReadOnly = mMaster->bReadOnly;
1873  bExpand = false;
1874  bDirty = false; // Not used
1875  mGroup = group;
1876  aLocaleString = mMaster->aLocaleString;
1877  setReadDefaults(mMaster->readDefaults());
1878 }
1879 
1880 KConfigGroup::KConfigGroup(KConfigBase *master, const char * group)
1881 {
1882  mMaster = master;
1883  backEnd = mMaster->backEnd; // Needed for getConfigState()
1884  bLocaleInitialized = true;
1885  bReadOnly = mMaster->bReadOnly;
1886  bExpand = false;
1887  bDirty = false; // Not used
1888  mGroup = group;
1889  aLocaleString = mMaster->aLocaleString;
1890  setReadDefaults(mMaster->readDefaults());
1891 }
1892 
1893 void KConfigGroup::deleteGroup(bool bGlobal)
1894 {
1895  mMaster->deleteGroup(KConfigBase::group(), true, bGlobal);
1896 }
1897 
1898 bool KConfigGroup::groupIsImmutable() const
1899 {
1900  return mMaster->groupIsImmutable(KConfigBase::group());
1901 }
1902 
1903 void KConfigGroup::setDirty(bool _bDirty)
1904 {
1905  mMaster->setDirty(_bDirty);
1906 }
1907 
1908 void KConfigGroup::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
1909 {
1910  mMaster->putData(_key, _data, _checkGroup);
1911 }
1912 
1913 KEntry KConfigGroup::lookupData(const KEntryKey &_key) const
1914 {
1915  return mMaster->lookupData(_key);
1916 }
1917 
1918 void KConfigGroup::sync()
1919 {
1920  mMaster->sync();
1921 }
1922 
1923 void KConfigBase::virtual_hook( int, void* )
1924 { /*BASE::virtual_hook( id, data );*/ }
1925 
1926 void KConfigGroup::virtual_hook( int id, void* data )
1927 { KConfigBase::virtual_hook( id, data ); }
1928 
1929 bool KConfigBase::checkConfigFilesWritable(bool warnUser)
1930 {
1931  if (backEnd)
1932  return backEnd->checkConfigFilesWritable(warnUser);
1933  else
1934  return false;
1935 }
1936 
1937 #include "kconfigbase.moc"

kdecore

Skip menu "kdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |