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 backEnd = aBackEnd;
00060
00061
00062 reparseConfiguration();
00063
00064
00065
00066
00067
00068
00069
00070
00071
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;
00089
00090
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;
00112
00113 if (aIt.key().mKey.isEmpty())
00114 break;
00115
00116 if (!aIt.key().bDefault && !(*aIt).bDeleted)
00117 {
00118 if (group != "$Version")
00119 retList.append(QString::fromUtf8(group));
00120 break;
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;
00140 for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
00141 {
00142
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
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
00174
00175
00176 return tmpEntryMap;
00177 }
00178
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
00193
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
00204 KEntry &entry = aEntryMap[_key];
00205 if (entry.bImmutable)
00206 return;
00207
00208 entry = _data;
00209
00210 entry.bGlobal |= bForceGlobal;
00211
00212 if (_key.bDefault)
00213 {
00214
00215
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"
This file is part of the documentation for kdelibs Version 3.1.4.