• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.25 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • config
kconfig.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE libraries
3  Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4  Copyright (c) 1999 Preston Brown <pbrown@kde.org>
5  Copyright (c) 1997-1999 Matthias Kalle Dalheimer <kalle@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include "kconfig.h"
24 #include "kconfig_p.h"
25 
26 #include <cstdlib>
27 #include <fcntl.h>
28 #include <unistd.h>
29 
30 #include "kconfigbackend.h"
31 #include "kconfiggroup.h"
32 #include <kde_file.h>
33 #include <kstringhandler.h>
34 #include <klocale.h>
35 #include <kstandarddirs.h>
36 #include <kurl.h>
37 #include <kcomponentdata.h>
38 #include <ktoolinvocation.h>
39 #include <kaboutdata.h>
40 #include <kdebug.h>
41 
42 #include <qbytearray.h>
43 #include <qfile.h>
44 #include <qdir.h>
45 #include <qdatetime.h>
46 #include <qrect.h>
47 #include <qsize.h>
48 #include <qcolor.h>
49 #include <QtCore/QProcess>
50 #include <QtCore/QPointer>
51 #include <QtCore/QSet>
52 #include <QtCore/QStack>
53 
54 bool KConfigPrivate::mappingsRegistered=false;
55 
56 KConfigPrivate::KConfigPrivate(const KComponentData &componentData_, KConfig::OpenFlags flags,
57  const char* resource)
58  : openFlags(flags), resourceType(resource), mBackend(0),
59  bDynamicBackend(true), bDirty(false), bReadDefaults(false),
60  bFileImmutable(false), bForceGlobal(false), bSuppressGlobal(false),
61  componentData(componentData_), configState(KConfigBase::NoAccess)
62 {
63  sGlobalFileName = componentData.dirs()->saveLocation("config", QString(), false) + QLatin1String("kdeglobals");
64 
65  static int use_etc_kderc = -1;
66  if (use_etc_kderc < 0)
67  use_etc_kderc = getenv("KDE_SKIP_KDERC") != 0 ? 0 : 1; // for unit tests
68  if (use_etc_kderc) {
69 
70  etc_kderc =
71 #ifdef Q_WS_WIN
72  QFile::decodeName( qgetenv("WINDIR") + "/kde4rc" );
73 #else
74  QLatin1String("/etc/kde4rc");
75 #endif
76  if (!KStandardDirs::checkAccess(etc_kderc, R_OK)) {
77  etc_kderc.clear();
78  }
79  }
80 
81 // if (!mappingsRegistered) {
82 // KEntryMap tmp;
83 // if (!etc_kderc.isEmpty()) {
84 // KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, etc_kderc, QLatin1String("INI"));
85 // backend->parseConfig( "en_US", tmp, KConfigBackend::ParseDefaults);
86 // }
87 // const QString kde4rc(QDir::home().filePath(".kde4rc"));
88 // if (KStandardDirs::checkAccess(kde4rc, R_OK)) {
89 // KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, kde4rc, QLatin1String("INI"));
90 // backend->parseConfig( "en_US", tmp, KConfigBackend::ParseOptions());
91 // }
92 // KConfigBackend::registerMappings(tmp);
93 // mappingsRegistered = true;
94 // }
95 
96  setLocale(KGlobal::hasLocale() ? KGlobal::locale()->language() : KLocale::defaultLanguage());
97 }
98 
99 
100 bool KConfigPrivate::lockLocal()
101 {
102  if (mBackend) {
103  return mBackend->lock(componentData);
104  }
105  // anonymous object - pretend we locked it
106  return true;
107 }
108 
109 void KConfigPrivate::copyGroup(const QByteArray& source, const QByteArray& destination,
110  KConfigGroup *otherGroup, KConfigBase::WriteConfigFlags flags) const
111 {
112  KEntryMap& otherMap = otherGroup->config()->d_ptr->entryMap;
113  const int len = source.length();
114  const bool sameName = (destination == source);
115 
116  // we keep this bool outside the foreach loop so that if
117  // the group is empty, we don't end up marking the other config
118  // as dirty erroneously
119  bool dirtied = false;
120 
121  for (KEntryMap::ConstIterator entryMapIt( entryMap.constBegin() ); entryMapIt != entryMap.constEnd(); ++entryMapIt) {
122  const QByteArray& group = entryMapIt.key().mGroup;
123 
124  if (!group.startsWith(source)) // nothing to do
125  continue;
126 
127  // don't copy groups that start with the same prefix, but are not sub-groups
128  if (group.length() > len && group[len] != '\x1d')
129  continue;
130 
131  KEntryKey newKey = entryMapIt.key();
132 
133  if (flags & KConfigBase::Localized) {
134  newKey.bLocal = true;
135  }
136 
137  if (!sameName)
138  newKey.mGroup.replace(0, len, destination);
139 
140  KEntry entry = entryMap[ entryMapIt.key() ];
141  dirtied = entry.bDirty = flags & KConfigBase::Persistent;
142 
143  if (flags & KConfigBase::Global) {
144  entry.bGlobal = true;
145  }
146 
147  otherMap[newKey] = entry;
148  }
149 
150  if (dirtied) {
151  otherGroup->config()->d_ptr->bDirty = true;
152  }
153 }
154 
155 QString KConfigPrivate::expandString(const QString& value)
156 {
157  QString aValue = value;
158 
159  // check for environment variables and make necessary translations
160  int nDollarPos = aValue.indexOf( QLatin1Char('$') );
161  while( nDollarPos != -1 && nDollarPos+1 < aValue.length()) {
162  // there is at least one $
163  if( aValue[nDollarPos+1] == QLatin1Char('(') ) {
164  int nEndPos = nDollarPos+1;
165  // the next character is not $
166  while ( (nEndPos <= aValue.length()) && (aValue[nEndPos]!=QLatin1Char(')')) )
167  nEndPos++;
168  nEndPos++;
169  QString cmd = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
170 
171  QString result;
172  QByteArray oldpath = qgetenv( "PATH" );
173  QByteArray newpath;
174  if (KGlobal::hasMainComponent()) {
175  newpath = QFile::encodeName(KGlobal::dirs()->resourceDirs("exe").join(QChar::fromLatin1(KPATH_SEPARATOR)));
176  if (!newpath.isEmpty() && !oldpath.isEmpty())
177  newpath += KPATH_SEPARATOR;
178  }
179  newpath += oldpath;
180  setenv( "PATH", newpath, 1/*overwrite*/ );
181 // FIXME: wince does not have pipes
182 #ifndef _WIN32_WCE
183  FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
184  if (fs) {
185  QTextStream ts(fs, QIODevice::ReadOnly);
186  result = ts.readAll().trimmed();
187  pclose(fs);
188  }
189 #endif
190  setenv( "PATH", oldpath, 1/*overwrite*/ );
191  aValue.replace( nDollarPos, nEndPos-nDollarPos, result );
192  nDollarPos += result.length();
193  } else if( aValue[nDollarPos+1] != QLatin1Char('$') ) {
194  int nEndPos = nDollarPos+1;
195  // the next character is not $
196  QString aVarName;
197  if ( aValue[nEndPos] == QLatin1Char('{') ) {
198  while ( (nEndPos <= aValue.length()) && (aValue[nEndPos] != QLatin1Char('}')) )
199  nEndPos++;
200  nEndPos++;
201  aVarName = aValue.mid( nDollarPos+2, nEndPos-nDollarPos-3 );
202  } else {
203  while ( nEndPos <= aValue.length() &&
204  (aValue[nEndPos].isNumber() ||
205  aValue[nEndPos].isLetter() ||
206  aValue[nEndPos] == QLatin1Char('_') ) )
207  nEndPos++;
208  aVarName = aValue.mid( nDollarPos+1, nEndPos-nDollarPos-1 );
209  }
210  QString env;
211  if (!aVarName.isEmpty()) {
212 #ifdef Q_OS_WIN
213  if (aVarName == QLatin1String("HOME"))
214  env = QDir::homePath();
215  else
216 #endif
217  {
218  QByteArray pEnv = qgetenv( aVarName.toLatin1() );
219  if( !pEnv.isEmpty() )
220  // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
221  // An environment variable may contain values in 8bit
222  // locale specified encoding or UTF8 encoding
223  env = KStringHandler::from8Bit( pEnv );
224  }
225  aValue.replace(nDollarPos, nEndPos-nDollarPos, env);
226  nDollarPos += env.length();
227  } else
228  aValue.remove( nDollarPos, nEndPos-nDollarPos );
229  } else {
230  // remove one of the dollar signs
231  aValue.remove( nDollarPos, 1 );
232  nDollarPos++;
233  }
234  nDollarPos = aValue.indexOf( QLatin1Char('$'), nDollarPos );
235  }
236 
237  return aValue;
238 }
239 
240 
241 KConfig::KConfig( const QString& file, OpenFlags mode,
242  const char* resourceType)
243  : d_ptr(new KConfigPrivate(KGlobal::mainComponent(), mode, resourceType))
244 {
245  d_ptr->changeFileName(file, resourceType); // set the local file name
246 
247  // read initial information off disk
248  reparseConfiguration();
249 }
250 
251 KConfig::KConfig( const KComponentData& componentData, const QString& file, OpenFlags mode,
252  const char* resourceType)
253  : d_ptr(new KConfigPrivate(componentData, mode, resourceType))
254 {
255  d_ptr->changeFileName(file, resourceType); // set the local file name
256 
257  // read initial information off disk
258  reparseConfiguration();
259 }
260 
261 KConfig::KConfig(const QString& file, const QString& backend, const char* resourceType)
262  : d_ptr(new KConfigPrivate(KGlobal::mainComponent(), SimpleConfig, resourceType))
263 {
264  d_ptr->mBackend = KConfigBackend::create(d_ptr->componentData, file, backend);
265  d_ptr->bDynamicBackend = false;
266  d_ptr->changeFileName(file, ""); // set the local file name
267 
268  // read initial information off disk
269  reparseConfiguration();
270 }
271 
272 KConfig::KConfig(KConfigPrivate &d)
273  : d_ptr(&d)
274 {
275 }
276 
277 KConfig::~KConfig()
278 {
279  Q_D(KConfig);
280  if (d->bDirty && d->mBackend.isUnique())
281  sync();
282  delete d;
283 }
284 
285 const KComponentData& KConfig::componentData() const
286 {
287  Q_D(const KConfig);
288  return d->componentData;
289 }
290 
291 QStringList KConfig::groupList() const
292 {
293  Q_D(const KConfig);
294  QSet<QString> groups;
295 
296  for (KEntryMap::ConstIterator entryMapIt( d->entryMap.constBegin() ); entryMapIt != d->entryMap.constEnd(); ++entryMapIt) {
297  const KEntryKey& key = entryMapIt.key();
298  const QByteArray group = key.mGroup;
299  if (key.mKey.isNull() && !group.isEmpty() && group != "<default>" && group != "$Version") {
300  const QString groupname = QString::fromUtf8(group);
301  groups << groupname.left(groupname.indexOf(QLatin1Char('\x1d')));
302  }
303  }
304 
305  return groups.toList();
306 }
307 
308 QStringList KConfigPrivate::groupList(const QByteArray& group) const
309 {
310  QByteArray theGroup = group + '\x1d';
311  QSet<QString> groups;
312 
313  for (KEntryMap::ConstIterator entryMapIt( entryMap.constBegin() ); entryMapIt != entryMap.constEnd(); ++entryMapIt) {
314  const KEntryKey& key = entryMapIt.key();
315  if (key.mKey.isNull() && key.mGroup.startsWith(theGroup)) {
316  const QString groupname = QString::fromUtf8(key.mGroup.mid(theGroup.length()));
317  groups << groupname.left(groupname.indexOf(QLatin1Char('\x1d')));
318  }
319  }
320 
321  return groups.toList();
322 }
323 
324 static bool isGroupOrSubGroupMatch(const QByteArray& potentialGroup, const QByteArray& group)
325 {
326  if (!potentialGroup.startsWith(group)) {
327  return false;
328  }
329  return potentialGroup.length() == group.length() || potentialGroup[group.length()] == '\x1d';
330 }
331 
332 // List all sub groups, including subsubgroups
333 QSet<QByteArray> KConfigPrivate::allSubGroups(const QByteArray& parentGroup) const
334 {
335  QSet<QByteArray> groups;
336 
337  for (KEntryMap::const_iterator entryMapIt = entryMap.begin(); entryMapIt != entryMap.end(); ++entryMapIt) {
338  const KEntryKey& key = entryMapIt.key();
339  if (key.mKey.isNull() && isGroupOrSubGroupMatch(key.mGroup, parentGroup)) {
340  groups << key.mGroup;
341  }
342  }
343  return groups;
344 }
345 
346 bool KConfigPrivate::hasNonDeletedEntries(const QByteArray& group) const
347 {
348  for (KEntryMap::const_iterator it = entryMap.begin(); it != entryMap.end(); ++it) {
349  const KEntryKey& key = it.key();
350  // Check for any non-deleted entry
351  if (isGroupOrSubGroupMatch(key.mGroup, group) && !key.mKey.isNull() && !it->bDeleted)
352  return true;
353  }
354  return false;
355 }
356 
357 
358 QStringList KConfigPrivate::keyListImpl(const QByteArray& theGroup) const
359 {
360  QStringList keys;
361 
362  const KEntryMapConstIterator theEnd = entryMap.constEnd();
363  KEntryMapConstIterator it = entryMap.findEntry(theGroup);
364  if (it != theEnd) {
365  ++it; // advance past the special group entry marker
366 
367  QSet<QString> tmp;
368  for (; it != theEnd && it.key().mGroup == theGroup; ++it) {
369  const KEntryKey& key = it.key();
370  if (!key.mKey.isNull() && !it->bDeleted)
371  tmp << QString::fromUtf8(key.mKey);
372  }
373  keys = tmp.toList();
374  }
375 
376  return keys;
377 }
378 
379 QStringList KConfig::keyList(const QString& aGroup) const
380 {
381  Q_D(const KConfig);
382  const QByteArray theGroup(aGroup.isEmpty() ? "<default>" : aGroup.toUtf8());
383  return d->keyListImpl(theGroup);
384 }
385 
386 QMap<QString,QString> KConfig::entryMap(const QString& aGroup) const
387 {
388  Q_D(const KConfig);
389  QMap<QString, QString> theMap;
390  const QByteArray theGroup(aGroup.isEmpty() ? "<default>" : aGroup.toUtf8());
391 
392  const KEntryMapConstIterator theEnd = d->entryMap.constEnd();
393  KEntryMapConstIterator it = d->entryMap.findEntry(theGroup, 0, 0);
394  if (it != theEnd) {
395  ++it; // advance past the special group entry marker
396 
397  for (; it != theEnd && it.key().mGroup == theGroup; ++it) {
398  // leave the default values and deleted entries out
399  if (!it->bDeleted && !it.key().bDefault) {
400  const QString key = QString::fromUtf8(it.key().mKey.constData());
401  // the localized entry should come first, so don't overwrite it
402  // with the non-localized entry
403  if (!theMap.contains(key)) {
404  if (it->bExpand) {
405  theMap.insert(key,KConfigPrivate::expandString(QString::fromUtf8(it->mValue.constData())));
406  } else {
407  theMap.insert(key,QString::fromUtf8(it->mValue.constData()));
408  }
409  }
410  }
411  }
412  }
413 
414  return theMap;
415 }
416 
417 // TODO KDE5: return a bool value
418 void KConfig::sync()
419 {
420  Q_D(KConfig);
421 
422  if (isImmutable() || name().isEmpty()) {
423  // can't write to an immutable or anonymous file.
424  return;
425  }
426 
427  if (d->bDirty && d->mBackend) {
428  const QByteArray utf8Locale(locale().toUtf8());
429 
430  // Create the containing dir, maybe it wasn't there
431  d->mBackend->createEnclosing();
432 
433  // lock the local file
434  if (d->configState == ReadWrite && !d->lockLocal()) {
435  qWarning() << "couldn't lock local file";
436  return;
437  }
438 
439  // Rewrite global/local config only if there is a dirty entry in it.
440  bool writeGlobals = false;
441  bool writeLocals = false;
442  foreach (const KEntry& e, d->entryMap) {
443  if (e.bDirty) {
444  if (e.bGlobal) {
445  writeGlobals = true;
446  } else {
447  writeLocals = true;
448  }
449 
450  if (writeGlobals && writeLocals) {
451  break;
452  }
453  }
454  }
455 
456  d->bDirty = false; // will revert to true if a config write fails
457 
458  if (d->wantGlobals() && writeGlobals) {
459  KSharedPtr<KConfigBackend> tmp = KConfigBackend::create(componentData(), d->sGlobalFileName);
460  if (d->configState == ReadWrite && !tmp->lock(componentData())) {
461  qWarning() << "couldn't lock global file";
462  d->bDirty = true;
463  return;
464  }
465  if (!tmp->writeConfig(utf8Locale, d->entryMap, KConfigBackend::WriteGlobal, d->componentData)) {
466  d->bDirty = true;
467  // TODO KDE5: return false? (to tell the app that writing wasn't possible, e.g.
468  // config file is immutable or disk full)
469  }
470  if (tmp->isLocked()) {
471  tmp->unlock();
472  }
473  }
474 
475  if (writeLocals) {
476  if (!d->mBackend->writeConfig(utf8Locale, d->entryMap, KConfigBackend::WriteOptions(), d->componentData)) {
477  d->bDirty = true;
478  // TODO KDE5: return false? (to tell the app that writing wasn't possible, e.g.
479  // config file is immutable or disk full)
480  }
481  }
482  if (d->mBackend->isLocked()) {
483  d->mBackend->unlock();
484  }
485  }
486 }
487 
488 void KConfig::markAsClean()
489 {
490  Q_D(KConfig);
491  d->bDirty = false;
492 
493  // clear any dirty flags that entries might have set
494  const KEntryMapIterator theEnd = d->entryMap.end();
495  for (KEntryMapIterator it = d->entryMap.begin(); it != theEnd; ++it)
496  it->bDirty = false;
497 }
498 
499 bool KConfig::isDirty() const
500 {
501  Q_D(const KConfig);
502  return d->bDirty;
503 }
504 
505 void KConfig::checkUpdate(const QString &id, const QString &updateFile)
506 {
507  const KConfigGroup cg(this, "$Version");
508  const QString cfg_id = updateFile+QLatin1Char(':')+id;
509  const QStringList ids = cg.readEntry("update_info", QStringList());
510  if (!ids.contains(cfg_id)) {
511  KToolInvocation::kdeinitExecWait(QString::fromLatin1("kconf_update"), QStringList() << QString::fromLatin1("--check") << updateFile);
512  reparseConfiguration();
513  }
514 }
515 
516 KConfig* KConfig::copyTo(const QString &file, KConfig *config) const
517 {
518  Q_D(const KConfig);
519  if (!config)
520  config = new KConfig(componentData(), QString(), SimpleConfig);
521  config->d_func()->changeFileName(file, d->resourceType);
522  config->d_func()->entryMap = d->entryMap;
523  config->d_func()->bFileImmutable = false;
524 
525  const KEntryMapIterator theEnd = config->d_func()->entryMap.end();
526  for (KEntryMapIterator it = config->d_func()->entryMap.begin(); it != theEnd; ++it)
527  it->bDirty = true;
528  config->d_ptr->bDirty = true;
529 
530  return config;
531 }
532 
533 QString KConfig::name() const
534 {
535  Q_D(const KConfig);
536  return d->fileName;
537 }
538 
539 void KConfigPrivate::changeFileName(const QString& name, const char* type)
540 {
541  fileName = name;
542 
543  QString file;
544  if (name.isEmpty()) {
545  if (wantDefaults()) { // accessing default app-specific config "appnamerc"
546  const QString appName = componentData.aboutData()->appName();
547  if (!appName.isEmpty()) {
548  fileName = appName + QLatin1String("rc");
549  if (type && *type)
550  resourceType = type; // only change it if it's not empty
551  file = KStandardDirs::locateLocal(resourceType, fileName, false, componentData);
552  }
553  } else if (wantGlobals()) { // accessing "kdeglobals" - XXX used anywhere?
554  resourceType = "config";
555  fileName = QLatin1String("kdeglobals");
556  file = sGlobalFileName;
557  } // else anonymous config.
558  // KDE5: remove these magic overloads
559  } else if (QDir::isAbsolutePath(fileName)) {
560  fileName = KStandardDirs::realFilePath(fileName);
561  file = fileName;
562  } else {
563  if (type && *type)
564  resourceType = type; // only change it if it's not empty
565  file = KStandardDirs::locateLocal(resourceType, fileName, false, componentData);
566  }
567 
568  if (file.isEmpty()) {
569  openFlags = KConfig::SimpleConfig;
570  return;
571  }
572 
573 #ifndef Q_OS_WIN
574  bSuppressGlobal = (file == sGlobalFileName);
575 #else
576  bSuppressGlobal = (file.compare(sGlobalFileName, Qt::CaseInsensitive) == 0);
577 #endif
578 
579  if (bDynamicBackend || !mBackend) // allow dynamic changing of backend
580  mBackend = KConfigBackend::create(componentData, file);
581  else
582  mBackend->setFilePath(file);
583 
584  configState = mBackend->accessMode();
585 }
586 
587 void KConfig::reparseConfiguration()
588 {
589  Q_D(KConfig);
590  if (d->fileName.isEmpty()) {
591  return;
592  }
593 
594  // Don't lose pending changes
595  if (!d->isReadOnly() && d->bDirty)
596  sync();
597 
598  d->entryMap.clear();
599 
600  d->bFileImmutable = false;
601 
602  // Parse all desired files from the least to the most specific.
603  if (d->wantGlobals())
604  d->parseGlobalFiles();
605 
606  d->parseConfigFiles();
607 }
608 
609 
610 QStringList KConfigPrivate::getGlobalFiles() const
611 {
612  const KStandardDirs *const dirs = componentData.dirs();
613  QStringList globalFiles;
614  foreach (const QString& dir1, dirs->findAllResources("config", QLatin1String("kdeglobals")))
615  globalFiles.push_front(dir1);
616  foreach (const QString& dir2, dirs->findAllResources("config", QLatin1String("system.kdeglobals")))
617  globalFiles.push_front(dir2);
618  if (!etc_kderc.isEmpty())
619  globalFiles.push_front(etc_kderc);
620  return globalFiles;
621 }
622 
623 void KConfigPrivate::parseGlobalFiles()
624 {
625  const QStringList globalFiles = getGlobalFiles();
626 // qDebug() << "parsing global files" << globalFiles;
627 
628  // TODO: can we cache the values in etc_kderc / other global files
629  // on a per-application basis?
630  const QByteArray utf8Locale = locale.toUtf8();
631  foreach(const QString& file, globalFiles) {
632  KConfigBackend::ParseOptions parseOpts = KConfigBackend::ParseGlobal|KConfigBackend::ParseExpansions;
633 #ifndef Q_OS_WIN
634  if (file != sGlobalFileName)
635 #else
636  if (file.compare(sGlobalFileName, Qt::CaseInsensitive) != 0)
637 #endif
638  parseOpts |= KConfigBackend::ParseDefaults;
639 
640  KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, file);
641  if ( backend->parseConfig( utf8Locale, entryMap, parseOpts) == KConfigBackend::ParseImmutable)
642  break;
643  }
644 }
645 
646 void KConfigPrivate::parseConfigFiles()
647 {
648  // can only read the file if there is a backend and a file name
649  if (mBackend && !fileName.isEmpty()) {
650 
651  bFileImmutable = false;
652 
653  QList<QString> files;
654  if (wantDefaults()) {
655  if (bSuppressGlobal) {
656  files = getGlobalFiles();
657  } else {
658  foreach (const QString& f, componentData.dirs()->findAllResources(
659  resourceType, fileName))
660  files.prepend(f);
661  }
662  } else {
663  files << mBackend->filePath();
664  }
665  if (!isSimple())
666  files = extraFiles.toList() + files;
667 
668 // qDebug() << "parsing local files" << files;
669 
670  const QByteArray utf8Locale = locale.toUtf8();
671  foreach(const QString& file, files) {
672 #ifndef Q_OS_WIN
673  if (file == mBackend->filePath()) {
674 #else
675  if (file.compare(mBackend->filePath(), Qt::CaseInsensitive) == 0) {
676 #endif
677  switch (mBackend->parseConfig(utf8Locale, entryMap, KConfigBackend::ParseExpansions)) {
678  case KConfigBackend::ParseOk:
679  break;
680  case KConfigBackend::ParseImmutable:
681  bFileImmutable = true;
682  break;
683  case KConfigBackend::ParseOpenError:
684  configState = KConfigBase::NoAccess;
685  break;
686  }
687  } else {
688  KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, file);
689  bFileImmutable = (backend->parseConfig(utf8Locale, entryMap,
690  KConfigBackend::ParseDefaults|KConfigBackend::ParseExpansions)
691  == KConfigBackend::ParseImmutable);
692  }
693 
694  if (bFileImmutable)
695  break;
696  }
697  if (componentData.dirs()->isRestrictedResource(resourceType, fileName))
698  bFileImmutable = true;
699  }
700 }
701 
702 KConfig::AccessMode KConfig::accessMode() const
703 {
704  Q_D(const KConfig);
705  return d->configState;
706 }
707 
708 void KConfig::addConfigSources(const QStringList& files)
709 {
710  Q_D(KConfig);
711  foreach(const QString& file, files) {
712  d->extraFiles.push(file);
713  }
714 
715  if (!files.isEmpty()) {
716  reparseConfiguration();
717  }
718 }
719 
720 QString KConfig::locale() const
721 {
722  Q_D(const KConfig);
723  return d->locale;
724 }
725 
726 bool KConfigPrivate::setLocale(const QString& aLocale)
727 {
728  if (aLocale != locale) {
729  locale = aLocale;
730  return true;
731  }
732  return false;
733 }
734 
735 bool KConfig::setLocale(const QString& locale)
736 {
737  Q_D(KConfig);
738  if (d->setLocale(locale)) {
739  reparseConfiguration();
740  return true;
741  }
742  return false;
743 }
744 
745 void KConfig::setReadDefaults(bool b)
746 {
747  Q_D(KConfig);
748  d->bReadDefaults = b;
749 }
750 
751 bool KConfig::readDefaults() const
752 {
753  Q_D(const KConfig);
754  return d->bReadDefaults;
755 }
756 
757 bool KConfig::isImmutable() const
758 {
759  Q_D(const KConfig);
760  return d->bFileImmutable;
761 }
762 
763 bool KConfig::isGroupImmutableImpl(const QByteArray& aGroup) const
764 {
765  Q_D(const KConfig);
766  return isImmutable() || d->entryMap.getEntryOption(aGroup, 0, 0, KEntryMap::EntryImmutable);
767 }
768 
769 #ifndef KDE_NO_DEPRECATED
770 void KConfig::setForceGlobal(bool b)
771 {
772  Q_D(KConfig);
773  d->bForceGlobal = b;
774 }
775 #endif
776 
777 #ifndef KDE_NO_DEPRECATED
778 bool KConfig::forceGlobal() const
779 {
780  Q_D(const KConfig);
781  return d->bForceGlobal;
782 }
783 #endif
784 
785 KConfigGroup KConfig::groupImpl(const QByteArray &group)
786 {
787  return KConfigGroup(this, group.constData());
788 }
789 
790 const KConfigGroup KConfig::groupImpl(const QByteArray &group) const
791 {
792  return KConfigGroup(this, group.constData());
793 }
794 
795 KEntryMap::EntryOptions convertToOptions(KConfig::WriteConfigFlags flags)
796 {
797  KEntryMap::EntryOptions options=0;
798 
799  if (flags&KConfig::Persistent)
800  options |= KEntryMap::EntryDirty;
801  if (flags&KConfig::Global)
802  options |= KEntryMap::EntryGlobal;
803  if (flags&KConfig::Localized)
804  options |= KEntryMap::EntryLocalized;
805  return options;
806 }
807 
808 void KConfig::deleteGroupImpl(const QByteArray &aGroup, WriteConfigFlags flags)
809 {
810  Q_D(KConfig);
811  KEntryMap::EntryOptions options = convertToOptions(flags)|KEntryMap::EntryDeleted;
812 
813  const QSet<QByteArray> groups = d->allSubGroups(aGroup);
814  foreach (const QByteArray& group, groups) {
815  const QStringList keys = d->keyListImpl(group);
816  foreach (const QString& _key, keys) {
817  const QByteArray &key = _key.toUtf8();
818  if (d->canWriteEntry(group, key.constData())) {
819  d->entryMap.setEntry(group, key, QByteArray(), options);
820  d->bDirty = true;
821  }
822  }
823  }
824 }
825 
826 bool KConfig::isConfigWritable(bool warnUser)
827 {
828  Q_D(KConfig);
829  bool allWritable = (d->mBackend.isNull()? false: d->mBackend->isWritable());
830 
831  if (warnUser && !allWritable) {
832  QString errorMsg;
833  if (!d->mBackend.isNull()) // TODO how can be it be null? Set errorMsg appropriately
834  errorMsg = d->mBackend->nonWritableErrorMessage();
835 
836  // Note: We don't ask the user if we should not ask this question again because we can't save the answer.
837  errorMsg += i18n("Please contact your system administrator.");
838  QString cmdToExec = KStandardDirs::findExe(QString::fromLatin1("kdialog"));
839  if (!cmdToExec.isEmpty() && componentData().isValid())
840  {
841  QProcess::execute(cmdToExec, QStringList()
842  << QString::fromLatin1("--title") << componentData().componentName()
843  << QString::fromLatin1("--msgbox") << errorMsg);
844  }
845  }
846 
847  d->configState = allWritable ? ReadWrite : ReadOnly; // update the read/write status
848 
849  return allWritable;
850 }
851 
852 bool KConfig::hasGroupImpl(const QByteArray& aGroup) const
853 {
854  Q_D(const KConfig);
855 
856  // No need to look for the actual group entry anymore, or for subgroups:
857  // a group exists if it contains any non-deleted entry.
858 
859  return d->hasNonDeletedEntries(aGroup);
860 }
861 
862 bool KConfigPrivate::canWriteEntry(const QByteArray& group, const char* key, bool isDefault) const
863 {
864  if (bFileImmutable ||
865  entryMap.getEntryOption(group, key, KEntryMap::SearchLocalized, KEntryMap::EntryImmutable))
866  return isDefault;
867  return true;
868 }
869 
870 void KConfigPrivate::putData( const QByteArray& group, const char* key,
871  const QByteArray& value, KConfigBase::WriteConfigFlags flags, bool expand)
872 {
873  KEntryMap::EntryOptions options = convertToOptions(flags);
874 
875  if (bForceGlobal)
876  options |= KEntryMap::EntryGlobal;
877  if (expand)
878  options |= KEntryMap::EntryExpansion;
879 
880  if (value.isNull()) // deleting entry
881  options |= KEntryMap::EntryDeleted;
882 
883  bool dirtied = entryMap.setEntry(group, key, value, options);
884  if (dirtied && (flags & KConfigBase::Persistent))
885  bDirty = true;
886 }
887 
888 void KConfigPrivate::revertEntry(const QByteArray& group, const char* key)
889 {
890  bool dirtied = entryMap.revertEntry(group, key);
891  if (dirtied)
892  bDirty = true;
893 }
894 
895 QByteArray KConfigPrivate::lookupData(const QByteArray& group, const char* key,
896  KEntryMap::SearchFlags flags) const
897 {
898  if (bReadDefaults)
899  flags |= KEntryMap::SearchDefaults;
900  const KEntryMapConstIterator it = entryMap.findEntry(group, key, flags);
901  if (it == entryMap.constEnd())
902  return QByteArray();
903  return it->mValue;
904 }
905 
906 QString KConfigPrivate::lookupData(const QByteArray& group, const char* key,
907  KEntryMap::SearchFlags flags, bool *expand) const
908 {
909  if (bReadDefaults)
910  flags |= KEntryMap::SearchDefaults;
911  return entryMap.getEntry(group, key, QString(), flags, expand);
912 }
913 
914 void KConfig::virtual_hook(int /*id*/, void* /*data*/)
915 {
916  /* nothing */
917 }
918 
KConfigPrivate::canWriteEntry
bool canWriteEntry(const QByteArray &group, const char *key, bool isDefault=false) const
Definition: kconfig.cpp:862
KConfig::forceGlobal
bool forceGlobal() const
Definition: kconfig.cpp:778
KConfig::checkUpdate
void checkUpdate(const QString &id, const QString &updateFile)
Ensures that the configuration file contains a certain update.
Definition: kconfig.cpp:505
KGlobal
Access to the KDE global objects.
Definition: kglobal.h:333
i18n
QString i18n(const char *text)
Returns a localized version of a string.
Definition: klocalizedstring.h:630
KEntryMap::EntryDeleted
Definition: kconfigdata.h:165
KConfigBackend::isLocked
virtual bool isLocked() const =0
KEntryMap::EntryDirty
Definition: kconfigdata.h:162
KSharedPtr< KConfigBackend >
KConfig::sync
void sync()
Definition: kconfig.cpp:418
KComponentData::aboutData
const KAboutData * aboutData() const
Returns the about data of this component.
Definition: kcomponentdata.cpp:215
KConfigBase::Persistent
Save this entry when saving the config object.
Definition: kconfigbase.h:46
KConfigPrivate::changeFileName
void changeFileName(const QString &fileName, const char *resourceType)
Definition: kconfig.cpp:539
kdebug.h
KConfigBackend::unlock
virtual void unlock()=0
Release the lock on the file.
KConfigPrivate::KConfigPrivate
KConfigPrivate(const KComponentData &componentData_, KConfig::OpenFlags flags, const char *resource)
Definition: kconfig.cpp:56
KStringHandler::from8Bit
QString from8Bit(const char *str)
Construct QString from a c string, guessing whether it is UTF8- or Local8Bit-encoded.
Definition: kstringhandler.cpp:289
KStandardDirs::realFilePath
static QString realFilePath(const QString &filename)
Expands all symbolic links and resolves references to &#39;/.
Definition: kstandarddirs.cpp:973
KEntryMap
Definition: kconfigdata.h:152
kurl.h
KEntryMap::EntryImmutable
Definition: kconfigdata.h:164
isGroupOrSubGroupMatch
static bool isGroupOrSubGroupMatch(const QByteArray &potentialGroup, const QByteArray &group)
Definition: kconfig.cpp:324
KComponentData::isValid
bool isValid() const
Returns whether this is a valid object.
Definition: kcomponentdata.cpp:128
KConfig::setReadDefaults
void setReadDefaults(bool b)
defaults
Definition: kconfig.cpp:745
KConfigBackend::ParseOpenError
the configuration is immutable
Definition: kconfigbackend.h:112
KEntry
map/dict/list config node entry.
Definition: kconfigdata.h:35
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
KConfig::name
QString name() const
Returns the filename used to store the configuration.
Definition: kconfig.cpp:533
KConfigBase::Global
Save the entry to the global KDE config file instead of the application specific config file...
Definition: kconfigbase.h:50
KConfig::groupImpl
virtual KConfigGroup groupImpl(const QByteArray &b)
Definition: kconfig.cpp:785
KConfig::accessMode
AccessMode accessMode() const
Definition: kconfig.cpp:702
kconfig.h
KConfig::groupList
QStringList groupList() const
Definition: kconfig.cpp:291
KGlobal::dirs
KStandardDirs * dirs()
Returns the application standard dirs object.
KConfigPrivate::expandString
static QString expandString(const QString &value)
Definition: kconfig.cpp:155
KComponentData::dirs
KStandardDirs * dirs() const
Returns the application standard dirs object.
Definition: kcomponentdata.cpp:193
KStandardDirs::checkAccess
static bool checkAccess(const QString &pathname, int mode)
Check, if a file may be accessed in a given mode.
Definition: kstandarddirs.cpp:2120
KConfig::setForceGlobal
void setForceGlobal(bool force)
global
Definition: kconfig.cpp:770
KConfig::isImmutable
bool isImmutable() const
Definition: kconfig.cpp:757
KConfigPrivate::lookupData
QString lookupData(const QByteArray &group, const char *key, KEntryMap::SearchFlags flags, bool *expand) const
Definition: kconfig.cpp:906
KConfigPrivate::bDynamicBackend
bool bDynamicBackend
Definition: kconfig_p.h:81
KConfig::SimpleConfig
Just a single config file.
Definition: kconfig.h:96
KConfigBase::group
KConfigGroup group(const QByteArray &group)
Returns an object for the named subgroup.
Definition: kconfigbase.cpp:44
QString
ktoolinvocation.h
KConfigPrivate::revertEntry
void revertEntry(const QByteArray &group, const char *key)
Definition: kconfig.cpp:888
klocale.h
KConfigBackend::WriteGlobal
Definition: kconfigbackend.h:103
KEntryMap::EntryExpansion
Definition: kconfigdata.h:166
kconfig_p.h
KConfig::virtual_hook
virtual void virtual_hook(int id, void *data)
Virtual hook, used to add new "virtual" functions while maintaining binary compatibility.
Definition: kconfig.cpp:914
KGlobal::config
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:139
KConfig::deleteGroupImpl
virtual void deleteGroupImpl(const QByteArray &group, WriteConfigFlags flags=Normal)
Definition: kconfig.cpp:808
KConfigBase::ReadOnly
Definition: kconfigbase.h:133
KConfig::componentData
const KComponentData & componentData() const
Returns the component data this configuration is for.
Definition: kconfig.cpp:285
kconfigbackend.h
KConfigBackend::parseConfig
virtual ParseInfo parseConfig(const QByteArray &locale, KEntryMap &pWriteBackMap, ParseOptions options=ParseOptions())=0
Read persistent storage.
KEntryMap::EntryLocalized
Definition: kconfigdata.h:169
KConfigBackend::ParseGlobal
Definition: kconfigbackend.h:94
KEntryKey
key structure holding both the actual key and the group to which it belongs.
Definition: kconfigdata.h:89
KEntryKey::mGroup
QByteArray mGroup
The "group" to which this EntryKey belongs.
Definition: kconfigdata.h:99
KConfig::isDirty
bool isDirty() const
Returns true if sync has any changes to write out.
Definition: kconfig.cpp:499
KConfig::addConfigSources
void addConfigSources(const QStringList &sources)
extra config files
Definition: kconfig.cpp:708
KConfig::setLocale
bool setLocale(const QString &aLocale)
Sets the locale to aLocale.
Definition: kconfig.cpp:735
KStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:171
QStringList
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
Tries to find all resources with the specified type.
Definition: kstandarddirs.cpp:900
KStandardDirs::saveLocation
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
Finds a location to save files into for the given type in the user&#39;s home directory.
Definition: kstandarddirs.cpp:1484
KConfigPrivate
Definition: kconfig_p.h:42
KEntryKey::bLocal
bool bLocal
Entry is localised or not.
Definition: kconfigdata.h:107
KConfigBackend::writeConfig
virtual bool writeConfig(const QByteArray &locale, KEntryMap &entryMap, WriteOptions options, const KComponentData &data)=0
Write the dirty entries to permanent storage.
KConfigBackend::lock
virtual bool lock(const KComponentData &componentData)=0
Lock the file.
KConfigPrivate::mBackend
KSharedPtr< KConfigBackend > mBackend
Definition: kconfig_p.h:72
KConfigPrivate::keyListImpl
QStringList keyListImpl(const QByteArray &theGroup) const
Definition: kconfig.cpp:358
KConfigBase
Definition: kconfigbase.h:38
QSet< QString >
KConfigPrivate::hasNonDeletedEntries
bool hasNonDeletedEntries(const QByteArray &group) const
Definition: kconfig.cpp:346
kstringhandler.h
KGlobal::locale
KLocale * locale()
Returns the global locale object.
Definition: kglobal.cpp:170
KStandardDirs::isRestrictedResource
bool isRestrictedResource(const char *type, const QString &relPath=QString()) const
Checks whether a resource is restricted as part of the KIOSK framework.
Definition: kstandarddirs.cpp:254
KAboutData::appName
QString appName() const
Returns the application&#39;s internal name.
Definition: kaboutdata.cpp:678
KConfigBackend::ParseImmutable
the configuration was opened read/write
Definition: kconfigbackend.h:111
KConfigGroup
A class for one specific group in a KConfig object.
Definition: kconfiggroup.h:53
KConfigBase::ReadWrite
Definition: kconfigbase.h:133
KEntry::bGlobal
bool bGlobal
Entry should be written to the global config file.
Definition: kconfigdata.h:50
KConfig::~KConfig
virtual ~KConfig()
Definition: kconfig.cpp:277
KConfig
The central class of the KDE configuration data system.
Definition: kconfig.h:70
KConfig::hasGroupImpl
virtual bool hasGroupImpl(const QByteArray &group) const
Definition: kconfig.cpp:852
KConfigGroup::config
KConfig * config()
Return the config object that this group belongs to.
Definition: kconfiggroup.cpp:610
KConfig::isGroupImmutableImpl
virtual bool isGroupImmutableImpl(const QByteArray &aGroup) const
Definition: kconfig.cpp:763
KConfigBase::NoAccess
Definition: kconfigbase.h:133
KConfig::markAsClean
void markAsClean()
Definition: kconfig.cpp:488
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfiggroup.h:248
KConfigBackend::ParseOk
Definition: kconfigbackend.h:110
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
This function is much like locate.
Definition: kstandarddirs.cpp:2097
kstandarddirs.h
KConfig::reparseConfiguration
void reparseConfiguration()
Updates the state of this object to match the persistent storage.
Definition: kconfig.cpp:587
KEntryKey::mKey
QByteArray mKey
The actual key of the entry in question.
Definition: kconfigdata.h:103
KToolInvocation::kdeinitExecWait
static int kdeinitExecWait(const QString &name, const QStringList &args=QStringList(), QString *error=0, int *pid=0, const QByteArray &startup_id=QByteArray())
Starts a program via kdeinit and wait for it to finish.
Definition: ktoolinvocation.cpp:240
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
Finds the executable in the system path.
Definition: kstandarddirs.cpp:1334
KConfig::readDefaults
bool readDefaults() const
Definition: kconfig.cpp:751
KConfig::locale
QString locale() const
locales
Definition: kconfig.cpp:720
KGlobal::mainComponent
const KComponentData & mainComponent()
Returns the global component data.
Definition: kglobal.cpp:145
KGlobal::hasMainComponent
bool hasMainComponent()
Definition: kglobal.cpp:151
KConfigBase::AccessMode
AccessMode
Possible return values for accessMode().
Definition: kconfigbase.h:133
KConfig::KConfig
KConfig(const QString &file=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
Creates a KConfig object to manipulate a configuration file for the current application.
Definition: kconfig.cpp:241
convertToOptions
KEntryMap::EntryOptions convertToOptions(KConfig::WriteConfigFlags flags)
Definition: kconfig.cpp:795
KEntryMap::SearchLocalized
Definition: kconfigdata.h:157
kaboutdata.h
KConfigPrivate::resourceType
const char * resourceType
Definition: kconfig_p.h:47
KConfigBackend::ParseDefaults
entries should be marked as global
Definition: kconfigbackend.h:95
kcomponentdata.h
KEntryMap::EntryGlobal
Definition: kconfigdata.h:163
KConfigBackend::create
static KSharedPtr< KConfigBackend > create(const KComponentData &componentData, const QString &fileName=QString(), const QString &system=QString())
Creates a new KConfig backend.
Definition: kconfigbackend.cpp:64
KConfigPrivate::allSubGroups
QSet< QByteArray > allSubGroups(const QByteArray &parentGroup) const
Definition: kconfig.cpp:333
KConfig::entryMap
QMap< QString, QString > entryMap(const QString &aGroup=QString()) const
Returns a map (tree) of entries in a particular group.
Definition: kconfig.cpp:386
KConfigPrivate::putData
void putData(const QByteArray &group, const char *key, const QByteArray &value, KConfigBase::WriteConfigFlags flags, bool expand=false)
Definition: kconfig.cpp:870
KConfigBase::Localized
Add the locale tag to the key when writing it.
Definition: kconfigbase.h:55
KGlobal::hasLocale
bool hasLocale()
Definition: kglobal.cpp:205
KEntryMap::SearchDefaults
Definition: kconfigdata.h:156
KComponentData
Per component data.
Definition: kcomponentdata.h:46
KEntry::bDirty
bool bDirty
Must the entry be written back to disk?
Definition: kconfigdata.h:46
QMap
KLocale::defaultLanguage
static QString defaultLanguage()
Returns the name of the internal language.
Definition: klocale.cpp:615
kconfiggroup.h
KConfigPrivate::groupList
QStringList groupList(const QByteArray &group) const
Definition: kconfig.cpp:308
KConfig::isConfigWritable
bool isConfigWritable(bool warnUser)
Whether the configuration can be written to.
Definition: kconfig.cpp:826
KConfig::d_ptr
KConfigPrivate *const d_ptr
Definition: kconfig.h:403
KConfigPrivate::copyGroup
void copyGroup(const QByteArray &source, const QByteArray &destination, KConfigGroup *otherGroup, KConfigBase::WriteConfigFlags flags) const
Definition: kconfig.cpp:109
QList< QString >
KConfigBackend::ParseExpansions
entries should be marked as default
Definition: kconfigbackend.h:96
KConfig::KConfigGroup
friend class KConfigGroup
Definition: kconfig.h:395
KConfig::copyTo
KConfig * copyTo(const QString &file, KConfig *config=0) const
Copies all entries from this config object to a new config object that will save itself to file...
Definition: kconfig.cpp:516
This file is part of the KDE documentation.
Documentation copyright © 1996-2016 The KDE developers.
Generated on Wed Oct 19 2016 23:23:32 by doxygen 1.8.12 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.25 API Reference

Skip menu "kdelibs-4.14.25 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal