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

kdecore

  • kdecore
kconfig.cpp
1 /*
2  This file is part of the KDE libraries
3  Copyright (c) 1999 Preston Brown <pbrown@kde.org>
4  Copyright (C) 1997-1999 Matthias Kalle Dalheimer (kalle@kde.org)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 // $Id$
23 
24 #include <config.h>
25 
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 
30 #include <stdlib.h>
31 #include <unistd.h>
32 
33 #include <tqfileinfo.h>
34 
35 #include <kapplication.h>
36 #include "kconfigbackend.h"
37 
38 #include "kconfig.h"
39 #include "kglobal.h"
40 #include "kstandarddirs.h"
41 #include "kstaticdeleter.h"
42 #include <tqtimer.h>
43 
44 KConfig::KConfig( const TQString& fileName,
45  bool bReadOnly, bool bUseKderc, const char *resType )
46  : KConfigBase(), bGroupImmutable(false), bFileImmutable(false),
47  bForceGlobal(false)
48 {
49  // set the object's read-only status.
50  setReadOnly(bReadOnly);
51 
52  // for right now we will hardcode that we are using the INI
53  // back end driver. In the future this should be converted over to
54  // a object factory of some sorts.
55  KConfigINIBackEnd *aBackEnd = new KConfigINIBackEnd(this,
56  fileName,
57  resType,
58  bUseKderc);
59 
60  // set the object's back end pointer to this new backend
61  backEnd = aBackEnd;
62 
63  // read initial information off disk
64  reparseConfiguration();
65 
66  // we let KStandardDirs add custom user config files. It will do
67  // this only once. So only the first call ever to this constructor
68  // will anything else than return here We have to reparse here as
69  // configuration files may appear after customized directories have
70  // been added. and the info they contain needs to be inserted into the
71  // config object.
72  // Since this makes only sense for config directories, addCustomized
73  // returns true only if new config directories appeared.
74  if (KGlobal::dirs()->addCustomized(this))
75  reparseConfiguration();
76 }
77 
78 KConfig::KConfig(KConfigBackEnd *aBackEnd, bool bReadOnly)
79  : bGroupImmutable(false), bFileImmutable(false),
80  bForceGlobal(false)
81 {
82  setReadOnly(bReadOnly);
83  backEnd = aBackEnd;
84  reparseConfiguration();
85 }
86 
87 KConfig::~KConfig()
88 {
89  sync();
90 
91  delete backEnd;
92 }
93 
94 void KConfig::rollback(bool bDeep)
95 {
96  KConfigBase::rollback(bDeep);
97 
98  if (!bDeep)
99  return; // object's bDeep flag is set in KConfigBase method
100 
101  // clear any dirty flags that entries might have set
102  for (KEntryMapIterator aIt = aEntryMap.begin();
103  aIt != aEntryMap.end(); ++aIt)
104  (*aIt).bDirty = false;
105 }
106 
107 TQStringList KConfig::groupList() const
108 {
109  TQStringList retList;
110 
111  KEntryMapConstIterator aIt = aEntryMap.begin();
112  KEntryMapConstIterator aEnd = aEntryMap.end();
113  for (; aIt != aEnd; ++aIt)
114  {
115  while(aIt.key().mKey.isEmpty())
116  {
117  TQCString group = aIt.key().mGroup;
118  ++aIt;
119  while (true)
120  {
121  if (aIt == aEnd)
122  return retList; // done
123 
124  if (aIt.key().mKey.isEmpty())
125  break; // Group is empty, next group
126 
127  if (!aIt.key().bDefault && !(*aIt).bDeleted)
128  {
129  if (group != "$Version") // Special case!
130  retList.append(TQString::fromUtf8(group));
131  break; // Group is non-empty, added, next group
132  }
133  ++aIt;
134  }
135  }
136  }
137 
138  return retList;
139 }
140 
141 TQMap<TQString, TQString> KConfig::entryMap(const TQString &pGroup) const
142 {
143  TQCString pGroup_utf = pGroup.utf8();
144  KEntryKey groupKey( pGroup_utf, 0 );
145  TQMap<TQString, TQString> tmpMap;
146 
147  KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
148  if (aIt == aEntryMap.end())
149  return tmpMap;
150  ++aIt; // advance past special group entry marker
151  for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
152  {
153  // Leave the default values out && leave deleted entries out
154  if (!aIt.key().bDefault && !(*aIt).bDeleted)
155  tmpMap.insert(TQString::fromUtf8(aIt.key().mKey), TQString::fromUtf8((*aIt).mValue.data(), (*aIt).mValue.length()));
156  }
157 
158  return tmpMap;
159 }
160 
161 void KConfig::reparseConfiguration()
162 {
163  // Don't lose pending changes
164  if (!isReadOnly() && backEnd && bDirty)
165  backEnd->sync();
166 
167  aEntryMap.clear();
168 
169  // add the "default group" marker to the map
170  KEntryKey groupKey("<default>", 0);
171  aEntryMap.insert(groupKey, KEntry());
172 
173  bFileImmutable = false;
174  parseConfigFiles();
175  bFileImmutable = bReadOnly;
176 }
177 
178 KEntryMap KConfig::internalEntryMap(const TQString &pGroup) const
179 {
180  TQCString pGroup_utf = pGroup.utf8();
181  KEntry aEntry;
182  KEntryMapConstIterator aIt;
183  KEntryKey aKey(pGroup_utf, 0);
184  KEntryMap tmpEntryMap;
185 
186  aIt = aEntryMap.find(aKey);
187  if (aIt == aEntryMap.end()) {
188  // the special group key is not in the map,
189  // so it must be an invalid group. Return
190  // an empty map.
191  return tmpEntryMap;
192  }
193  // we now have a pointer to the nodes we want to copy.
194  for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
195  {
196  tmpEntryMap.insert(aIt.key(), *aIt);
197  }
198 
199  return tmpEntryMap;
200 }
201 
202 void KConfig::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
203 {
204  if (bFileImmutable && !_key.bDefault)
205  return;
206 
207  // check to see if the special group key is present,
208  // and if not, put it in.
209  if (_checkGroup)
210  {
211  KEntryKey groupKey( _key.mGroup, 0);
212  KEntry &entry = aEntryMap[groupKey];
213  bGroupImmutable = entry.bImmutable;
214  }
215  if (bGroupImmutable && !_key.bDefault)
216  return;
217 
218  // now either add or replace the data
219  KEntry &entry = aEntryMap[_key];
220  bool immutable = entry.bImmutable;
221  if (immutable && !_key.bDefault)
222  return;
223 
224  entry = _data;
225  entry.bImmutable |= immutable;
226  entry.bGlobal |= bForceGlobal; // force to kdeglobals
227 
228  if (_key.bDefault)
229  {
230  // We have added the data as default value,
231  // add it as normal value as well.
232  KEntryKey key(_key);
233  key.bDefault = false;
234  aEntryMap[key] = _data;
235  }
236 }
237 
238 KEntry KConfig::lookupData(const KEntryKey &_key) const
239 {
240  KEntryMapConstIterator aIt = aEntryMap.find(_key);
241  if (aIt != aEntryMap.end())
242  {
243  const KEntry &entry = *aIt;
244  if (entry.bDeleted)
245  return KEntry();
246  else
247  return entry;
248  }
249  else {
250  return KEntry();
251  }
252 }
253 
254 bool KConfig::internalHasGroup(const TQCString &group) const
255 {
256  KEntryKey groupKey( group, 0);
257 
258  KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
259  KEntryMapConstIterator aEnd = aEntryMap.end();
260 
261  if (aIt == aEnd)
262  return false;
263  ++aIt;
264  for(; (aIt != aEnd); ++aIt)
265  {
266  if (aIt.key().mKey.isEmpty())
267  break;
268 
269  if (!aIt.key().bDefault && !(*aIt).bDeleted)
270  return true;
271  }
272  return false;
273 }
274 
275 void KConfig::setFileWriteMode(int mode)
276 {
277  backEnd->setFileWriteMode(mode);
278 }
279 
280 KLockFile::Ptr KConfig::lockFile(bool bGlobal)
281 {
282  KConfigINIBackEnd *aBackEnd = dynamic_cast<KConfigINIBackEnd*>(backEnd);
283  if (!aBackEnd) return 0;
284  return aBackEnd->lockFile(bGlobal);
285 }
286 
287 void KConfig::checkUpdate(const TQString &id, const TQString &updateFile)
288 {
289  TQString oldGroup = group();
290  setGroup("$Version");
291  TQString cfg_id = updateFile+":"+id;
292  TQStringList ids = readListEntry("update_info");
293  if (!ids.contains(cfg_id))
294  {
295  TQStringList args;
296  args << "--check" << updateFile;
297  KApplication::kdeinitExecWait("kconf_update", args);
298  reparseConfiguration();
299  }
300  setGroup(oldGroup);
301 }
302 
303 KConfig* KConfig::copyTo(const TQString &file, KConfig *config) const
304 {
305  if (!config)
306  config = new KConfig(TQString::null, false, false);
307  config->backEnd->changeFileName(file, "config", false);
308  config->setReadOnly(false);
309  config->bFileImmutable = false;
310  config->backEnd->mConfigState = ReadWrite;
311 
312  TQStringList groups = groupList();
313  for(TQStringList::ConstIterator it = groups.begin();
314  it != groups.end(); ++it)
315  {
316  TQMap<TQString, TQString> map = entryMap(*it);
317  config->setGroup(*it);
318  for (TQMap<TQString,TQString>::Iterator it2 = map.begin();
319  it2 != map.end(); ++it2)
320  {
321  config->writeEntry(it2.key(), it2.data());
322  }
323 
324  }
325  return config;
326 }
327 
328 void KConfig::virtual_hook( int id, void* data )
329 { KConfigBase::virtual_hook( id, data ); }
330 
331 static KStaticDeleter< TQValueList<KSharedConfig*> > sd;
332 TQValueList<KSharedConfig*> *KSharedConfig::s_list = 0;
333 
334 KSharedConfig::Ptr KSharedConfig::openConfig(const TQString& fileName, bool readOnly, bool useKDEGlobals )
335 {
336  if (s_list)
337  {
338  for(TQValueList<KSharedConfig*>::ConstIterator it = s_list->begin();
339  it != s_list->end(); ++it)
340  {
341  if ((*it)->backEnd->fileName() == fileName &&
342  (*it)->bReadOnly == readOnly &&
343  (*it)->backEnd->useKDEGlobals == useKDEGlobals )
344  return (*it);
345  }
346  }
347  return new KSharedConfig(fileName, readOnly, useKDEGlobals);
348 }
349 
350 KSharedConfig::KSharedConfig( const TQString& fileName, bool readonly, bool usekdeglobals)
351  : KConfig(fileName, readonly, usekdeglobals)
352 {
353  if (!s_list)
354  {
355  sd.setObject(s_list, new TQValueList<KSharedConfig*>);
356  }
357 
358  s_list->append(this);
359 }
360 
361 KSharedConfig::~KSharedConfig()
362 {
363  if ( s_list )
364  s_list->remove(this);
365 }
366 
367 #include "kconfig.moc"
KConfig::lockFile
KLockFile::Ptr lockFile(bool bGlobal=false)
Returns a lock file object for the configuration file or 0 if the backend does not support locking...
Definition: kconfig.cpp:280
KSharedPtr< KLockFile >
KSharedConfig
KConfig variant using shared memory.
Definition: kconfig.h:273
KEntry::bDeleted
bool bDeleted
Entry has been deleted.
Definition: kconfigdata.h:57
KEntry
map/dict/list config node entry.
Definition: kconfigdata.h:32
KConfigBase::isReadOnly
bool isReadOnly() const
Returns the read-only status of the config object.
Definition: kconfigbase.h:1739
KConfig::rollback
virtual void rollback(bool bDeep=true)
Clears all entries out of the dirtyEntryMap, so the values will not be written to disk on a later cal...
Definition: kconfig.cpp:94
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
KEntryKey::bDefault
bool bDefault
Entry indicates if this is a default value.
Definition: kconfigdata.h:90
KConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
Writes a key/value pair.
Definition: kconfigbase.cpp:1067
KConfig::setFileWriteMode
void setFileWriteMode(int mode)
Set the file mode for newly created files.
Definition: kconfig.cpp:275
KConfigBase::sync
virtual void sync()
Flushes all changes that currently reside only in memory back to disk / permanent storage...
Definition: kconfigbase.cpp:1776
KConfigBackEnd::changeFileName
void changeFileName(const TQString &_fileName, const char *_resType, bool _useKDEGlobals)
Changes the filenames associated with this back end.
Definition: kconfigbackend.cpp:243
KConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: kconfigbase.cpp:80
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to KGlobal.
Definition: kstaticdeleter.h:85
KConfigBase::group
TQString group() const
Returns the name of the group in which we are searching for keys and from which we are retrieving ent...
Definition: kconfigbase.cpp:101
KConfig::internalEntryMap
virtual KEntryMap internalEntryMap() const
Returns a map (tree) of the entries in the tree.
Definition: kconfig.h:212
KEntry::bImmutable
bool bImmutable
Entry can not be modified.
Definition: kconfigdata.h:53
KGlobal::dirs
static KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:54
KEntryKey
key structure holding both the actual key and the the group to which it belongs.
Definition: kconfigdata.h:69
KConfig::putData
virtual void putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup=true)
Inserts a (key, value) pair into the internal storage mechanism of the configuration object...
Definition: kconfig.cpp:202
KConfig::internalHasGroup
virtual bool internalHasGroup(const TQCString &group) const
Returns true if the specified group is known.
Definition: kconfig.cpp:254
KConfigBase::parseConfigFiles
virtual void parseConfigFiles()
Parses all configuration files for a configuration object.
Definition: kconfigbase.cpp:1764
KConfigBase::backEnd
KConfigBackEnd * backEnd
A back end for loading/saving to disk in a particular format.
Definition: kconfigbase.h:1976
KConfigINIBackEnd
Class for KDE INI-style configuration file loading/saving.
Definition: kconfigbackend.h:191
KEntry::KEntryMap
TQMap< KEntryKey, KEntry > KEntryMap
Definition: kconfigdata.h:128
KConfig::entryMap
virtual TQMap< TQString, TQString > entryMap(const TQString &pGroup) const
Returns a map (tree) of entries for all entries in a particular group.
Definition: kconfig.cpp:141
KConfigBase
KDE Configuration Management abstract base class.
Definition: kconfigbase.h:70
KApplication::kdeinitExecWait
static int kdeinitExecWait(const TQString &name, const TQStringList &args, TQString *error, int *pid, const TQCString &startup_id)
Starts a program via kdeinit and wait for it to finish.
Definition: kapplication.cpp:3165
KConfig::KConfig
KConfig(const TQString &fileName=TQString::null, bool bReadOnly=false, bool bUseKDEGlobals=true, const char *resType="config")
Constructs a KConfig object.
Definition: kconfig.cpp:44
KConfigBackEnd::lockFile
KLockFile::Ptr lockFile(bool bGlobal=false)
Returns a lock file object for the configuration file.
Definition: kconfigbackend.cpp:273
KConfig::lookupData
virtual KEntry lookupData(const KEntryKey &_key) const
Looks up an entry in the config object's internal structure.
Definition: kconfig.cpp:238
KEntry::bGlobal
bool bGlobal
Entry should be written to the global config file.
Definition: kconfigdata.h:49
KConfig::~KConfig
virtual ~KConfig()
Destructs the KConfig object.
Definition: kconfig.cpp:87
KConfigBackEnd
Abstract base class for KDE configuration file loading/saving.
Definition: kconfigbackend.h:48
KConfig
Access KDE Configuration entries.
Definition: kconfig.h:43
KConfig::reparseConfiguration
virtual void reparseConfiguration()
Clears all internal data structures and then reread configuration information from disk...
Definition: kconfig.cpp:161
KConfigBackEnd::sync
virtual void sync(bool bMerge=true)=0
Writes configuration data to file(s).
KConfigBase::setReadOnly
virtual void setReadOnly(bool _ro)
Sets the config object's read-only status.
Definition: kconfigbase.h:1732
KEntryKey::mGroup
TQCString mGroup
The "group" to which this EntryKey belongs.
Definition: kconfigdata.h:78
KConfigBase::rollback
virtual void rollback(bool bDeep=true)
Mark the config object as "clean," i.e.
Definition: kconfigbase.cpp:1793
KConfigBase::bDirty
bool bDirty
Indicates whether there are any dirty entries in the config object that need to be written back to di...
Definition: kconfigbase.h:2002
KConfig::groupList
virtual TQStringList groupList() const
Returns a list of groups that are known.
Definition: kconfig.cpp:107
KConfig::checkUpdate
void checkUpdate(const TQString &id, const TQString &updateFile)
Checks whether the config file contains the update id as contained in updateFile. ...
Definition: kconfig.cpp:287
KConfig::aEntryMap
KEntryMap aEntryMap
Contains all key,value entries, as well as some "special" keys which indicate the start of a group of...
Definition: kconfig.h:243
KSharedConfig::openConfig
static KSharedConfig::Ptr openConfig(const TQString &fileName, bool readOnly=false, bool bUseKDEGlobals=true)
Returns a ref-counted pointer to a shared read-write config object.
Definition: kconfig.cpp:334
KConfig::copyTo
KConfig * copyTo(const TQString &file, KConfig *config=0) const
Copies all entries from this config object to a new config object that will save itself to file...
Definition: kconfig.cpp:303
KConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep= ',') const
Reads a list of strings.
Definition: kconfigbase.cpp:490
KConfigBackEnd::setFileWriteMode
void setFileWriteMode(int mode)
Set the file mode for newly created files.
Definition: kconfigbackend.cpp:315

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.6
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |