kdecore Library API Documentation

kconfig.cpp

00001 /*
00002   This file is part of the KDE libraries
00003   Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00004   Copyright (C) 1997-1999 Matthias Kalle Dalheimer (kalle@kde.org)
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019   Boston, MA 02111-1307, USA.
00020 */
00021 
00022 // $Id: kconfig.cpp,v 1.69 2002/08/06 22:46:58 waba Exp $
00023 
00024 #include <config.h>
00025 
00026 #ifdef HAVE_SYS_STAT_H
00027 #include <sys/stat.h>
00028 #endif
00029 
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032 
00033 #include <qfileinfo.h>
00034 
00035 #include <kapplication.h>
00036 #include "kconfigbackend.h"
00037 
00038 #include "kconfig.h"
00039 #include "kglobal.h"
00040 #include "kstandarddirs.h"
00041 #include <qtimer.h>
00042 
00043 KConfig::KConfig( const QString& fileName,
00044                  bool bReadOnly, bool bUseKderc, const char *resType )
00045   : KConfigBase(), bGroupImmutable(false), bFileImmutable(false),
00046     bForceGlobal(false)
00047 {
00048   // set the object's read-only status.
00049   setReadOnly(bReadOnly);
00050 
00051   // for right now we will hardcode that we are using the INI
00052   // back end driver.  In the future this should be converted over to
00053   // a object factory of some sorts.
00054   KConfigINIBackEnd *aBackEnd = new KConfigINIBackEnd(this,
00055                                                       fileName,
00056                                                       resType,
00057                                                       bUseKderc);
00058   // set the object's back end pointer to this new backend
00059   backEnd = aBackEnd;
00060 
00061   // read initial information off disk
00062   reparseConfiguration();
00063 
00064   // we let KStandardDirs add custom user config files. It will do
00065   // this only once. So only the first call ever to this constructor
00066   // will anything else than return here We have to reparse here as
00067   // configuration files may appear after customized directories have
00068   // been added. and the info they contain needs to be inserted into the
00069   // config object.
00070   // Since this makes only sense for config directories, addCustomized
00071   // returns true only if new config directories appeared.
00072   if (KGlobal::dirs()->addCustomized(this))
00073       reparseConfiguration();
00074 }
00075 
00076 KConfig::~KConfig()
00077 {
00078   sync();
00079 
00080   delete backEnd;
00081 }
00082 
00083 void KConfig::rollback(bool bDeep)
00084 {
00085   KConfigBase::rollback(bDeep);
00086 
00087   if (!bDeep)
00088     return; // object's bDeep flag is set in KConfigBase method
00089 
00090   // clear any dirty flags that entries might have set
00091   for (KEntryMapIterator aIt = aEntryMap.begin();
00092        aIt != aEntryMap.end(); ++aIt)
00093     (*aIt).bDirty = false;
00094 }
00095 
00096 QStringList KConfig::groupList() const
00097 {
00098   QStringList retList;
00099 
00100   KEntryMapConstIterator aIt = aEntryMap.begin();
00101   KEntryMapConstIterator aEnd = aEntryMap.end();
00102   for (; aIt != aEnd; ++aIt)
00103   {
00104     while(aIt.key().mKey.isEmpty())
00105     {
00106       QCString group = aIt.key().mGroup;
00107       ++aIt;
00108       while (true)
00109       {
00110          if (aIt == aEnd)
00111             return retList; // done
00112 
00113          if (aIt.key().mKey.isEmpty())
00114             break; // Group is empty, next group
00115 
00116          if (!aIt.key().bDefault && !(*aIt).bDeleted)
00117          {
00118             if (group != "$Version") // Special case!
00119                retList.append(QString::fromUtf8(group));
00120             break; // Grou is non-empty, added, next gropup
00121          }
00122          ++aIt;
00123       }
00124     }
00125   }
00126 
00127   return retList;
00128 }
00129 
00130 QMap<QString, QString> KConfig::entryMap(const QString &pGroup) const
00131 {
00132   QCString pGroup_utf = pGroup.utf8();
00133   KEntryKey groupKey( pGroup_utf, 0 );
00134   QMap<QString, QString> tmpMap;
00135 
00136   KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
00137   if (aIt == aEntryMap.end())
00138      return tmpMap;
00139   ++aIt; // advance past special group entry marker
00140   for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00141   {
00142     // Leave the default values out && leave deleted entries out
00143     if (!aIt.key().bDefault && !(*aIt).bDeleted)
00144       tmpMap.insert(QString::fromUtf8(aIt.key().mKey), QString::fromUtf8((*aIt).mValue.data(), (*aIt).mValue.length()));
00145   }
00146 
00147   return tmpMap;
00148 }
00149 
00150 void KConfig::reparseConfiguration()
00151 {
00152   aEntryMap.clear();
00153 
00154   // add the "default group" marker to the map
00155   KEntryKey groupKey("<default>", 0);
00156   aEntryMap.insert(groupKey, KEntry());
00157 
00158   bFileImmutable = false;
00159   parseConfigFiles();
00160   bFileImmutable = bReadOnly;
00161 }
00162 
00163 KEntryMap KConfig::internalEntryMap(const QString &pGroup) const
00164 {
00165   QCString pGroup_utf = pGroup.utf8();
00166   KEntry aEntry;
00167   KEntryMapConstIterator aIt;
00168   KEntryKey aKey(pGroup_utf, 0);
00169   KEntryMap tmpEntryMap;
00170 
00171   aIt = aEntryMap.find(aKey);
00172   if (aIt == aEntryMap.end()) {
00173     // the special group key is not in the map,
00174     // so it must be an invalid group.  Return
00175     // an empty map.
00176     return tmpEntryMap;
00177   }
00178   // we now have a pointer to the nodes we want to copy.
00179   for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00180   {
00181     tmpEntryMap.insert(aIt.key(), *aIt);
00182   }
00183 
00184   return tmpEntryMap;
00185 }
00186 
00187 void KConfig::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
00188 {
00189   if (bFileImmutable)
00190     return;
00191 
00192   // check to see if the special group key is present,
00193   // and if not, put it in.
00194   if (_checkGroup)
00195   {
00196     KEntryKey groupKey( _key.mGroup, 0);
00197     KEntry &entry = aEntryMap[groupKey];
00198     bGroupImmutable = entry.bImmutable;
00199   }
00200   if (bGroupImmutable)
00201     return;
00202 
00203   // now either add or replace the data
00204   KEntry &entry = aEntryMap[_key];
00205   if (entry.bImmutable)
00206     return;
00207 
00208   entry = _data;
00209 
00210   entry.bGlobal |= bForceGlobal; // force to kdeglobals
00211 
00212   if (_key.bDefault)
00213   {
00214      // We have added the data as default value,
00215      // add it as normal value as well.
00216      KEntryKey key(_key);
00217      key.bDefault = false;
00218      aEntryMap[key] = _data;
00219   }
00220 }
00221 
00222 KEntry KConfig::lookupData(const KEntryKey &_key) const
00223 {
00224   KEntryMapConstIterator aIt = aEntryMap.find(_key);
00225   if (aIt != aEntryMap.end())
00226   {
00227     const KEntry &entry = *aIt;
00228     if (entry.bDeleted)
00229        return KEntry();
00230     else
00231        return entry;
00232   }
00233   else {
00234     return KEntry();
00235   }
00236 }
00237 
00238 bool KConfig::internalHasGroup(const QCString &group) const
00239 {
00240   KEntryKey groupKey( group, 0);
00241 
00242   KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
00243   KEntryMapConstIterator aEnd = aEntryMap.end();
00244 
00245   if (aIt == aEnd)
00246      return false;
00247   ++aIt;
00248   for(; (aIt != aEnd); ++aIt)
00249   {
00250      if (aIt.key().mKey.isEmpty())
00251         break;
00252 
00253      if (!aIt.key().bDefault && !(*aIt).bDeleted)
00254         return true;
00255   }
00256   return false;
00257 }
00258 
00259 void KConfig::setFileWriteMode(int mode)
00260 {
00261   backEnd->setFileWriteMode(mode);
00262 }
00263 
00264 void KConfig::checkUpdate(const QString &id, const QString &updateFile)
00265 {
00266   QString oldGroup = group();
00267   setGroup("$Version");
00268   QString cfg_id = updateFile+":"+id;
00269   QStringList ids = readListEntry("update_info");
00270   if (!ids.contains(cfg_id))
00271   {
00272      QStringList args;
00273      args << "--check" << updateFile;
00274      KApplication::kdeinitExecWait("kconf_update", args);
00275      reparseConfiguration();
00276   }
00277   setGroup(oldGroup);
00278 }
00279 
00280 void KConfig::virtual_hook( int id, void* data )
00281 { KConfigBase::virtual_hook( id, data ); }
00282 
00283 
00284 
00285 #include "kconfig.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:22 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001