kconfig.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00049 setReadOnly(bReadOnly);
00050
00051
00052
00053
00054 KConfigINIBackEnd *aBackEnd = new KConfigINIBackEnd(this,
00055 fileName,
00056 resType,
00057 bUseKderc);
00058
00059
00060 backEnd = aBackEnd;
00061
00062
00063 reparseConfiguration();
00064
00065
00066
00067
00068
00069
00070
00071
00072
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;
00099
00100
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;
00122
00123 if (aIt.key().mKey.isEmpty())
00124 break;
00125
00126 if (!aIt.key().bDefault && !(*aIt).bDeleted)
00127 {
00128 if (group != "$Version")
00129 retList.append(QString::fromUtf8(group));
00130 break;
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;
00150 for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00151 {
00152
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
00163 if (!isReadOnly() && backEnd && bDirty)
00164 backEnd->sync();
00165
00166 aEntryMap.clear();
00167
00168
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
00188
00189
00190 return tmpEntryMap;
00191 }
00192
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
00207
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
00218 KEntry &entry = aEntryMap[_key];
00219 if (entry.bImmutable)
00220 return;
00221
00222 entry = _data;
00223
00224 entry.bGlobal |= bForceGlobal;
00225
00226 if (_key.bDefault)
00227 {
00228
00229
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"
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