1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
This file is part of the KDE libraries
Copyright (c) 1999 Preston Brown <pbrown@kde.org>
Copyright (C) 1997-1999 Matthias Kalle Dalheimer (kalle@kde.org)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// $Id$
#include <config.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <qfileinfo.h>
#include <kapplication.h>
#include "kconfigbackend.h"
#include "kconfig.h"
#include "kglobal.h"
#include "kstandarddirs.h"
#include "kstaticdeleter.h"
#include <qtimer.h>
KConfig::KConfig( const QString& fileName,
bool bReadOnly, bool bUseKderc, const char *resType )
: KConfigBase(), bGroupImmutable(false), bFileImmutable(false),
bForceGlobal(false)
{
// set the object's read-only status.
setReadOnly(bReadOnly);
// for right now we will hardcode that we are using the INI
// back end driver. In the future this should be converted over to
// a object factory of some sorts.
KConfigINIBackEnd *aBackEnd = new KConfigINIBackEnd(this,
fileName,
resType,
bUseKderc);
// set the object's back end pointer to this new backend
backEnd = aBackEnd;
// read initial information off disk
reparseConfiguration();
// we let KStandardDirs add custom user config files. It will do
// this only once. So only the first call ever to this constructor
// will anything else than return here We have to reparse here as
// configuration files may appear after customized directories have
// been added. and the info they contain needs to be inserted into the
// config object.
// Since this makes only sense for config directories, addCustomized
// returns true only if new config directories appeared.
if (KGlobal::dirs()->addCustomized(this))
reparseConfiguration();
}
KConfig::KConfig(KConfigBackEnd *aBackEnd, bool bReadOnly)
: bGroupImmutable(false), bFileImmutable(false),
bForceGlobal(false)
{
setReadOnly(bReadOnly);
backEnd = aBackEnd;
reparseConfiguration();
}
KConfig::~KConfig()
{
sync();
delete backEnd;
}
void KConfig::rollback(bool bDeep)
{
KConfigBase::rollback(bDeep);
if (!bDeep)
return; // object's bDeep flag is set in KConfigBase method
// clear any dirty flags that entries might have set
for (KEntryMapIterator aIt = aEntryMap.begin();
aIt != aEntryMap.end(); ++aIt)
(*aIt).bDirty = false;
}
QStringList KConfig::groupList() const
{
QStringList retList;
KEntryMapConstIterator aIt = aEntryMap.begin();
KEntryMapConstIterator aEnd = aEntryMap.end();
for (; aIt != aEnd; ++aIt)
{
while(aIt.key().mKey.isEmpty())
{
QCString group = aIt.key().mGroup;
++aIt;
while (true)
{
if (aIt == aEnd)
return retList; // done
if (aIt.key().mKey.isEmpty())
break; // Group is empty, next group
if (!aIt.key().bDefault && !(*aIt).bDeleted)
{
if (group != "$Version") // Special case!
retList.append(QString::fromUtf8(group));
break; // Group is non-empty, added, next group
}
++aIt;
}
}
}
return retList;
}
QMap<QString, QString> KConfig::entryMap(const QString &pGroup) const
{
QCString pGroup_utf = pGroup.utf8();
KEntryKey groupKey( pGroup_utf, 0 );
QMap<QString, QString> tmpMap;
KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
if (aIt == aEntryMap.end())
return tmpMap;
++aIt; // advance past special group entry marker
for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
{
// Leave the default values out && leave deleted entries out
if (!aIt.key().bDefault && !(*aIt).bDeleted)
tmpMap.insert(QString::fromUtf8(aIt.key().mKey), QString::fromUtf8((*aIt).mValue.data(), (*aIt).mValue.length()));
}
return tmpMap;
}
void KConfig::reparseConfiguration()
{
// Don't lose pending changes
if (!isReadOnly() && backEnd && bDirty)
backEnd->sync();
aEntryMap.clear();
// add the "default group" marker to the map
KEntryKey groupKey("<default>", 0);
aEntryMap.insert(groupKey, KEntry());
bFileImmutable = false;
parseConfigFiles();
bFileImmutable = bReadOnly;
}
KEntryMap KConfig::internalEntryMap(const QString &pGroup) const
{
QCString pGroup_utf = pGroup.utf8();
KEntry aEntry;
KEntryMapConstIterator aIt;
KEntryKey aKey(pGroup_utf, 0);
KEntryMap tmpEntryMap;
aIt = aEntryMap.find(aKey);
if (aIt == aEntryMap.end()) {
// the special group key is not in the map,
// so it must be an invalid group. Return
// an empty map.
return tmpEntryMap;
}
// we now have a pointer to the nodes we want to copy.
for (; aIt.key().mGroup == pGroup_utf && aIt != aEntryMap.end(); ++aIt)
{
tmpEntryMap.insert(aIt.key(), *aIt);
}
return tmpEntryMap;
}
void KConfig::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
{
if (bFileImmutable && !_key.bDefault)
return;
// check to see if the special group key is present,
// and if not, put it in.
if (_checkGroup)
{
KEntryKey groupKey( _key.mGroup, 0);
KEntry &entry = aEntryMap[groupKey];
bGroupImmutable = entry.bImmutable;
}
if (bGroupImmutable && !_key.bDefault)
return;
// now either add or replace the data
KEntry &entry = aEntryMap[_key];
bool immutable = entry.bImmutable;
if (immutable && !_key.bDefault)
return;
entry = _data;
entry.bImmutable |= immutable;
entry.bGlobal |= bForceGlobal; // force to kdeglobals
if (_key.bDefault)
{
// We have added the data as default value,
// add it as normal value as well.
KEntryKey key(_key);
key.bDefault = false;
aEntryMap[key] = _data;
}
}
KEntry KConfig::lookupData(const KEntryKey &_key) const
{
KEntryMapConstIterator aIt = aEntryMap.find(_key);
if (aIt != aEntryMap.end())
{
const KEntry &entry = *aIt;
if (entry.bDeleted)
return KEntry();
else
return entry;
}
else {
return KEntry();
}
}
bool KConfig::internalHasGroup(const QCString &group) const
{
KEntryKey groupKey( group, 0);
KEntryMapConstIterator aIt = aEntryMap.find(groupKey);
KEntryMapConstIterator aEnd = aEntryMap.end();
if (aIt == aEnd)
return false;
++aIt;
for(; (aIt != aEnd); ++aIt)
{
if (aIt.key().mKey.isEmpty())
break;
if (!aIt.key().bDefault && !(*aIt).bDeleted)
return true;
}
return false;
}
void KConfig::setFileWriteMode(int mode)
{
backEnd->setFileWriteMode(mode);
}
KLockFile::Ptr KConfig::lockFile(bool bGlobal)
{
KConfigINIBackEnd *aBackEnd = dynamic_cast<KConfigINIBackEnd*>(backEnd);
if (!aBackEnd) return 0;
return aBackEnd->lockFile(bGlobal);
}
void KConfig::checkUpdate(const QString &id, const QString &updateFile)
{
QString oldGroup = group();
setGroup("$Version");
QString cfg_id = updateFile+":"+id;
QStringList ids = readListEntry("update_info");
if (!ids.contains(cfg_id))
{
QStringList args;
args << "--check" << updateFile;
KApplication::kdeinitExecWait("kconf_update", args);
reparseConfiguration();
}
setGroup(oldGroup);
}
KConfig* KConfig::copyTo(const QString &file, KConfig *config) const
{
if (!config)
config = new KConfig(QString::null, false, false);
config->backEnd->changeFileName(file, "config", false);
config->setReadOnly(false);
config->bFileImmutable = false;
config->backEnd->mConfigState = ReadWrite;
QStringList groups = groupList();
for(QStringList::ConstIterator it = groups.begin();
it != groups.end(); ++it)
{
QMap<QString, QString> map = entryMap(*it);
config->setGroup(*it);
for (QMap<QString,QString>::Iterator it2 = map.begin();
it2 != map.end(); ++it2)
{
config->writeEntry(it2.key(), it2.data());
}
}
return config;
}
void KConfig::virtual_hook( int id, void* data )
{ KConfigBase::virtual_hook( id, data ); }
static KStaticDeleter< QValueList<KSharedConfig*> > sd;
QValueList<KSharedConfig*> *KSharedConfig::s_list = 0;
KSharedConfig::Ptr KSharedConfig::openConfig(const QString& fileName, bool readOnly, bool useKDEGlobals )
{
if (s_list)
{
for(QValueList<KSharedConfig*>::ConstIterator it = s_list->begin();
it != s_list->end(); ++it)
{
if ((*it)->backEnd->fileName() == fileName &&
(*it)->bReadOnly == readOnly &&
(*it)->backEnd->useKDEGlobals == useKDEGlobals )
return (*it);
}
}
return new KSharedConfig(fileName, readOnly, useKDEGlobals);
}
KSharedConfig::KSharedConfig( const QString& fileName, bool readonly, bool usekdeglobals)
: KConfig(fileName, readonly, usekdeglobals)
{
if (!s_list)
{
sd.setObject(s_list, new QValueList<KSharedConfig*>);
}
s_list->append(this);
}
KSharedConfig::~KSharedConfig()
{
if ( s_list )
s_list->remove(this);
}
#include "kconfig.moc"
|