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

tdecore

  • tdecore
tdeconfig.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 <tdeapplication.h>
36 #include "tdeconfigbackend.h"
37 
38 #include "tdeconfig.h"
39 #include "tdeglobal.h"
40 #include "kstandarddirs.h"
41 #include "kstaticdeleter.h"
42 #include <tqtimer.h>
43 
44 TDEConfig::TDEConfig( const TQString& fileName,
45  bool bReadOnly, bool bUseKderc, const char *resType )
46  : TDEConfigBase(), 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  TDEConfigINIBackEnd *aBackEnd = new TDEConfigINIBackEnd(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 TDEStandardDirs 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 (TDEGlobal::dirs()->addCustomized(this))
75  reparseConfiguration();
76 }
77 
78 TDEConfig::TDEConfig(TDEConfigBackEnd *aBackEnd, bool bReadOnly)
79  : bGroupImmutable(false), bFileImmutable(false),
80  bForceGlobal(false)
81 {
82  setReadOnly(bReadOnly);
83  backEnd = aBackEnd;
84  reparseConfiguration();
85 }
86 
87 TDEConfig::~TDEConfig()
88 {
89  sync();
90 
91  delete backEnd;
92 }
93 
94 void TDEConfig::rollback(bool bDeep)
95 {
96  TDEConfigBase::rollback(bDeep);
97 
98  if (!bDeep)
99  return; // object's bDeep flag is set in TDEConfigBase 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 TDEConfig::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> TDEConfig::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 TDEConfig::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 TDEConfig::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 TDEConfig::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 TDEConfig::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 TDEConfig::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 TDEConfig::setFileWriteMode(int mode)
276 {
277  backEnd->setFileWriteMode(mode);
278 }
279 
280 TDELockFile::Ptr TDEConfig::lockFile(bool bGlobal)
281 {
282  TDEConfigINIBackEnd *aBackEnd = dynamic_cast<TDEConfigINIBackEnd*>(backEnd);
283  if (!aBackEnd) return 0;
284  return aBackEnd->lockFile(bGlobal);
285 }
286 
287 void TDEConfig::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  TDEApplication::tdeinitExecWait("tdeconf_update", args);
298  reparseConfiguration();
299  }
300  setGroup(oldGroup);
301 }
302 
303 TDEConfig* TDEConfig::copyTo(const TQString &file, TDEConfig *config) const
304 {
305  if (!config)
306  config = new TDEConfig(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 TDEConfig::virtual_hook( int id, void* data )
329 { TDEConfigBase::virtual_hook( id, data ); }
330 
331 static KStaticDeleter< TQValueList<TDESharedConfig*> > sd;
332 TQValueList<TDESharedConfig*> *TDESharedConfig::s_list = 0;
333 
334 TDESharedConfig::Ptr TDESharedConfig::openConfig(const TQString& fileName, bool readOnly, bool useKDEGlobals )
335 {
336  if (s_list)
337  {
338  for(TQValueList<TDESharedConfig*>::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 TDESharedConfig(fileName, readOnly, useKDEGlobals);
348 }
349 
350 TDESharedConfig::TDESharedConfig( const TQString& fileName, bool readonly, bool usekdeglobals)
351  : TDEConfig(fileName, readonly, usekdeglobals)
352 {
353  if (!s_list)
354  {
355  sd.setObject(s_list, new TQValueList<TDESharedConfig*>);
356  }
357 
358  s_list->append(this);
359 }
360 
361 TDESharedConfig::~TDESharedConfig()
362 {
363  if ( s_list )
364  s_list->remove(this);
365 }
366 
367 #include "tdeconfig.moc"
TDEConfig::reparseConfiguration
virtual void reparseConfiguration()
Clears all internal data structures and then reread configuration information from disk...
Definition: tdeconfig.cpp:161
TDEConfig::internalEntryMap
virtual KEntryMap internalEntryMap() const
Returns a map (tree) of the entries in the tree.
Definition: tdeconfig.h:212
TDEConfig
Access KDE Configuration entries.
Definition: tdeconfig.h:43
TDESharedConfig::openConfig
static TDESharedConfig::Ptr openConfig(const TQString &fileName, bool readOnly=false, bool bUseKDEGlobals=true)
Returns a ref-counted pointer to a shared read-write config object.
Definition: tdeconfig.cpp:334
TDEConfig::entryMap
virtual TQMap< TQString, TQString > entryMap(const TQString &pGroup) const
Returns a map (tree) of entries for all entries in a particular group.
Definition: tdeconfig.cpp:141
KEntry::bDeleted
bool bDeleted
Entry has been deleted.
Definition: tdeconfigdata.h:57
KEntry
map/dict/list config node entry.
Definition: tdeconfigdata.h:32
TDEConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: tdeconfigbase.cpp:79
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: tdeconfigdata.h:90
TDEConfig::aEntryMap
KEntryMap aEntryMap
Contains all key,value entries, as well as some "special" keys which indicate the start of a group of...
Definition: tdeconfig.h:243
TDEConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep= ',') const
Reads a list of strings.
Definition: tdeconfigbase.cpp:491
TDEConfig::lookupData
virtual KEntry lookupData(const KEntryKey &_key) const
Looks up an entry in the config object's internal structure.
Definition: tdeconfig.cpp:238
TDEConfig::~TDEConfig
virtual ~TDEConfig()
Destructs the TDEConfig object.
Definition: tdeconfig.cpp:87
TDEConfig::groupList
virtual TQStringList groupList() const
Returns a list of groups that are known.
Definition: tdeconfig.cpp:107
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to TDEGlobal.
Definition: kstaticdeleter.h:85
TDEConfigBackEnd::setFileWriteMode
void setFileWriteMode(int mode)
Set the file mode for newly created files.
Definition: tdeconfigbackend.cpp:315
TDEConfigBase::setReadOnly
virtual void setReadOnly(bool _ro)
Sets the config object's read-only status.
Definition: tdeconfigbase.h:1755
KEntry::bImmutable
bool bImmutable
Entry can not be modified.
Definition: tdeconfigdata.h:53
TDEConfigBase
KDE Configuration Management abstract base class.
Definition: tdeconfigbase.h:70
KEntryKey
key structure holding both the actual key and the the group to which it belongs.
Definition: tdeconfigdata.h:69
TDEGlobal::dirs
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
Definition: tdeglobal.cpp:58
TDEConfigBase::rollback
virtual void rollback(bool bDeep=true)
Mark the config object as "clean," i.e.
Definition: tdeconfigbase.cpp:1803
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
Writes a key/value pair.
Definition: tdeconfigbase.cpp:1068
TDEConfig::copyTo
TDEConfig * copyTo(const TQString &file, TDEConfig *config=0) const
Copies all entries from this config object to a new config object that will save itself to file...
Definition: tdeconfig.cpp:303
TDEConfigBase::parseConfigFiles
virtual void parseConfigFiles()
Parses all configuration files for a configuration object.
Definition: tdeconfigbase.cpp:1774
KEntry::KEntryMap
TQMap< KEntryKey, KEntry > KEntryMap
Definition: tdeconfigdata.h:128
TDEConfig::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: tdeconfig.cpp:202
TDEConfigBackEnd::changeFileName
void changeFileName(const TQString &_fileName, const char *_resType, bool _useKDEGlobals)
Changes the filenames associated with this back end.
Definition: tdeconfigbackend.cpp:243
TDEConfig::lockFile
TDELockFile::Ptr lockFile(bool bGlobal=false)
Returns a lock file object for the configuration file or 0 if the backend does not support locking...
Definition: tdeconfig.cpp:280
TDEConfigBackEnd::sync
virtual void sync(bool bMerge=true)=0
Writes configuration data to file(s).
KEntry::bGlobal
bool bGlobal
Entry should be written to the global config file.
Definition: tdeconfigdata.h:49
TDEConfig::TDEConfig
TDEConfig(const TQString &fileName=TQString::null, bool bReadOnly=false, bool bUseKDEGlobals=true, const char *resType="config")
Constructs a TDEConfig object.
Definition: tdeconfig.cpp:44
TDEConfig::setFileWriteMode
void setFileWriteMode(int mode)
Set the file mode for newly created files.
Definition: tdeconfig.cpp:275
TDEConfigBase::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: tdeconfigbase.cpp:100
TDEConfigBase::backEnd
TDEConfigBackEnd * backEnd
A back end for loading/saving to disk in a particular format.
Definition: tdeconfigbase.h:1999
TDESharedConfig
TDEConfig variant using shared memory.
Definition: tdeconfig.h:273
TDEConfigBackEnd::lockFile
TDELockFile::Ptr lockFile(bool bGlobal=false)
Returns a lock file object for the configuration file.
Definition: tdeconfigbackend.cpp:273
TDEConfig::internalHasGroup
virtual bool internalHasGroup(const TQCString &group) const
Returns true if the specified group is known.
Definition: tdeconfig.cpp:254
KEntryKey::mGroup
TQCString mGroup
The "group" to which this EntryKey belongs.
Definition: tdeconfigdata.h:78
TDEConfigINIBackEnd
Class for KDE INI-style configuration file loading/saving.
Definition: tdeconfigbackend.h:191
TDEApplication::tdeinitExecWait
static int tdeinitExecWait(const TQString &name, const TQStringList &args, TQString *error, int *pid, const TQCString &startup_id)
Starts a program via tdeinit and wait for it to finish.
Definition: tdeapplication.cpp:3229
TDESharedPtr< TDELockFile >
TDEConfig::checkUpdate
void checkUpdate(const TQString &id, const TQString &updateFile)
Checks whether the config file contains the update id as contained in updateFile. ...
Definition: tdeconfig.cpp:287
TDEConfigBase::isReadOnly
bool isReadOnly() const
Returns the read-only status of the config object.
Definition: tdeconfigbase.h:1762
TDEConfigBase::bDirty
bool bDirty
Indicates whether there are any dirty entries in the config object that need to be written back to di...
Definition: tdeconfigbase.h:2025
TDEConfigBackEnd
Abstract base class for KDE configuration file loading/saving.
Definition: tdeconfigbackend.h:48
TDEConfigBase::sync
virtual void sync()
Flushes all changes that currently reside only in memory back to disk / permanent storage...
Definition: tdeconfigbase.cpp:1786
TDEConfig::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: tdeconfig.cpp:94

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.