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.79 2004/01/07 00:35:30 mueller 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 
00059   // set the object's back end pointer to this new backend
00060   backEnd = aBackEnd;
00061 
00062   // read initial information off disk
00063   reparseConfiguration();
00064 
00065   // we let KStandardDirs add custom user config files. It will do
00066   // this only once. So only the first call ever to this constructor
00067   // will anything else than return here We have to reparse here as
00068   // configuration files may appear after customized directories have
00069   // been added. and the info they contain needs to be inserted into the
00070   // config object.
00071   // Since this makes only sense for config directories, addCustomized
00072   // returns true only if new config directories appeared.
00073   if (KGlobal::dirs()->addCustomized(this))
00074       reparseConfiguration();
00075 }
00076 
00077 KConfig::KConfig(KConfigBackEnd *aBackEnd, bool bReadOnly)
00078     : bGroupImmutable(false), bFileImmutable(false),
00079     bForceGlobal(false)
00080 {
00081   setReadOnly(bReadOnly);
00082   backEnd = aBackEnd;
00083   reparseConfiguration();
00084 }
00085 
00086 KConfig::~KConfig()
00087 {
00088   sync();
00089 
00090   delete backEnd;
00091 }
00092 
00093 void KConfig::rollback(bool bDeep)
00094 {
00095   KConfigBase::rollback(bDeep);
00096 
00097   if (!bDeep)
00098     return; // object's bDeep flag is set in KConfigBase method
00099 
00100   // clear any dirty flags that entries might have set
00101   for (KEntryMapIterator aIt = aEntryMap.begin();
00102        aIt != aEntryMap.end(); ++aIt)
00103     (*aIt).bDirty = false;
00104 }
00105 
00106 QStringList KConfig::groupList() const
00107 {
00108   QStringList retList;
00109 
00110   KEntryMapConstIterator aIt = aEntryMap.begin();
00111   KEntryMapConstIterator aEnd = aEntryMap.end();
00112   for (; aIt != aEnd; ++aIt)
00113   {
00114     while(aIt.key().mKey.isEmpty())
00115     {
00116       QCString group = aIt.key().mGroup;
00117       ++aIt;
00118       while (true)
00119       {
00120          if (aIt == aEnd)
00121             return retList; // done
00122 
00123          if (aIt.key().mKey.isEmpty())
00124             break; // Group is empty, next group
00125 
00126          if (!aIt.key().bDefault && !(*aIt).bDeleted)
00127          {
00128             if (group != "$Version") // Special case!
00129                retList.append(QString::fromUtf8(group));
00130             break; // Grou is non-empty, added, next gropup
00131          }
00132          ++aIt;
00133       }
00134     }
00135   }
00136 
00137   return retList;
00138 }
00139 
00140 QMap<QString, QString> KConfig::entryMap(const QString &pGroup) const
00141 {
00142   QCString pGroup_utf = pGroup.utf8();
00143   KEntryKey groupKey( pGroup_utf, 0 );
00144   QMap<QString, QString> tmpMap;
00145 
00146   KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
00147   if (aIt == aEntryMap.end())
00148      return tmpMap;
00149   ++aIt; // advance past special group entry marker
00150   for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00151   {
00152     // Leave the default values out && leave deleted entries out
00153     if (!aIt.key().bDefault && !(*aIt).bDeleted)
00154       tmpMap.insert(QString::fromUtf8(aIt.key().mKey), QString::fromUtf8((*aIt).mValue.data(), (*aIt).mValue.length()));
00155   }
00156 
00157   return tmpMap;
00158 }
00159 
00160 void KConfig::reparseConfiguration()
00161 {
00162   // Don't lose pending changes
00163   if (!isReadOnly() && backEnd && bDirty)
00164     backEnd->sync();
00165 
00166   aEntryMap.clear();
00167 
00168   // add the "default group" marker to the map
00169   KEntryKey groupKey("<default>", 0);
00170   aEntryMap.insert(groupKey, KEntry());
00171 
00172   bFileImmutable = false;
00173   parseConfigFiles();
00174   bFileImmutable = bReadOnly;
00175 }
00176 
00177 KEntryMap KConfig::internalEntryMap(const QString &pGroup) const
00178 {
00179   QCString pGroup_utf = pGroup.utf8();
00180   KEntry aEntry;
00181   KEntryMapConstIterator aIt;
00182   KEntryKey aKey(pGroup_utf, 0);
00183   KEntryMap tmpEntryMap;
00184 
00185   aIt = aEntryMap.find(aKey);
00186   if (aIt == aEntryMap.end()) {
00187     // the special group key is not in the map,
00188     // so it must be an invalid group.  Return
00189     // an empty map.
00190     return tmpEntryMap;
00191   }
00192   // we now have a pointer to the nodes we want to copy.
00193   for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00194   {
00195     tmpEntryMap.insert(aIt.key(), *aIt);
00196   }
00197 
00198   return tmpEntryMap;
00199 }
00200 
00201 void KConfig::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
00202 {
00203   if (bFileImmutable)
00204     return;
00205 
00206   // check to see if the special group key is present,
00207   // and if not, put it in.
00208   if (_checkGroup)
00209   {
00210     KEntryKey groupKey( _key.mGroup, 0);
00211     KEntry &entry = aEntryMap[groupKey];
00212     bGroupImmutable = entry.bImmutable;
00213   }
00214   if (bGroupImmutable)
00215     return;
00216 
00217   // now either add or replace the data
00218   KEntry &entry = aEntryMap[_key];
00219   if (entry.bImmutable)
00220     return;
00221 
00222   entry = _data;
00223 
00224   entry.bGlobal |= bForceGlobal; // force to kdeglobals
00225 
00226   if (_key.bDefault)
00227   {
00228      // We have added the data as default value,
00229      // add it as normal value as well.
00230      KEntryKey key(_key);
00231      key.bDefault = false;
00232      aEntryMap[key] = _data;
00233   }
00234 }
00235 
00236 KEntry KConfig::lookupData(const KEntryKey &_key) const
00237 {
00238   KEntryMapConstIterator aIt = aEntryMap.find(_key);
00239   if (aIt != aEntryMap.end())
00240   {
00241     const KEntry &entry = *aIt;
00242     if (entry.bDeleted)
00243        return KEntry();
00244     else
00245        return entry;
00246   }
00247   else {
00248     return KEntry();
00249   }
00250 }
00251 
00252 bool KConfig::internalHasGroup(const QCString &group) const
00253 {
00254   KEntryKey groupKey( group, 0);
00255 
00256   KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
00257   KEntryMapConstIterator aEnd = aEntryMap.end();
00258 
00259   if (aIt == aEnd)
00260      return false;
00261   ++aIt;
00262   for(; (aIt != aEnd); ++aIt)
00263   {
00264      if (aIt.key().mKey.isEmpty())
00265         break;
00266 
00267      if (!aIt.key().bDefault && !(*aIt).bDeleted)
00268         return true;
00269   }
00270   return false;
00271 }
00272 
00273 void KConfig::setFileWriteMode(int mode)
00274 {
00275   backEnd->setFileWriteMode(mode);
00276 }
00277 
00278 void KConfig::checkUpdate(const QString &id, const QString &updateFile)
00279 {
00280   QString oldGroup = group();
00281   setGroup("$Version");
00282   QString cfg_id = updateFile+":"+id;
00283   QStringList ids = readListEntry("update_info");
00284   if (!ids.contains(cfg_id))
00285   {
00286      QStringList args;
00287      args << "--check" << updateFile;
00288      KApplication::kdeinitExecWait("kconf_update", args);
00289      reparseConfiguration();
00290   }
00291   setGroup(oldGroup);
00292 }
00293 
00294 KConfig* KConfig::copyTo(const QString &file, KConfig *config) const
00295 {
00296   if (!config)
00297      config = new KConfig(QString::null, false, false);
00298   config->backEnd->changeFileName(file, "config", false);
00299   config->setReadOnly(false);
00300   config->bFileImmutable = false;
00301   config->backEnd->mConfigState = ReadWrite;
00302 
00303   QStringList groups = groupList();
00304   for(QStringList::ConstIterator it = groups.begin();
00305       it != groups.end(); ++it)
00306   {
00307      QMap<QString, QString> map = entryMap(*it);
00308      config->setGroup(*it);
00309      for (QMap<QString,QString>::Iterator it2  = map.begin();
00310           it2 != map.end(); ++it2)
00311      {
00312         config->writeEntry(it2.key(), it2.data());
00313      }
00314 
00315   }
00316   return config;
00317 }
00318 
00319 void KConfig::virtual_hook( int id, void* data )
00320 { KConfigBase::virtual_hook( id, data ); }
00321 
00322 QValueList<KSharedConfig*> *KSharedConfig::s_list = 0;
00323 
00324 KSharedConfig::Ptr KSharedConfig::openConfig(const QString& fileName, bool immutable, bool useKDEGlobals )
00325 {
00326   if (s_list)
00327   {
00328      for(QValueList<KSharedConfig*>::ConstIterator it = s_list->begin();
00329          it != s_list->end(); ++it)
00330      {
00331         if ((*it)->backEnd->fileName() == fileName)
00332            return (*it);
00333      }
00334   }
00335   return new KSharedConfig(fileName, immutable, useKDEGlobals);
00336 }
00337 
00338 KSharedConfig::KSharedConfig( const QString& fileName, bool readonly, bool usekdeglobals)
00339  : KConfig(fileName, readonly, usekdeglobals)
00340 {
00341   if (!s_list)
00342      s_list = new QValueList<KSharedConfig*>;
00343      
00344   s_list->append(this);
00345 }
00346 
00347 KSharedConfig::~KSharedConfig()
00348 {
00349   s_list->remove(this);
00350 }
00351 
00352 #include "kconfig.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:43:29 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003