• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kconfig.cpp

Go to the documentation of this file.
00001 /*
00002    This file is part of the KDE libraries
00003    Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
00004    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00005    Copyright (c) 1997-1999 Matthias Kalle Dalheimer <kalle@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020    Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "kconfig.h"
00024 #include "kconfig_p.h"
00025 
00026 #include <cstdlib>
00027 #include <fcntl.h>
00028 #include <unistd.h>
00029 
00030 #include "kconfigbackend.h"
00031 #include "kconfiggroup.h"
00032 #include <kstringhandler.h>
00033 #include <klocale.h>
00034 #include <kstandarddirs.h>
00035 #include <kurl.h>
00036 #include <kcomponentdata.h>
00037 #include <ktoolinvocation.h>
00038 #include <kaboutdata.h>
00039 #include <kdebug.h>
00040 
00041 #include <qbytearray.h>
00042 #include <qfile.h>
00043 #include <qdir.h>
00044 #include <qdatetime.h>
00045 #include <qrect.h>
00046 #include <qsize.h>
00047 #include <qcolor.h>
00048 #include <QtCore/QProcess>
00049 #include <QtCore/QPointer>
00050 #include <QtCore/QSet>
00051 #include <QtCore/QStack>
00052 
00053 bool KConfigPrivate::mappingsRegistered=false;
00054 
00055 KConfigPrivate::KConfigPrivate(const KComponentData &componentData_, KConfig::OpenFlags flags,
00056            const char* resource)
00057     : openFlags(flags), resourceType(resource), mBackend(0),
00058       bDynamicBackend(true),  bDirty(false), bReadDefaults(false),
00059       bFileImmutable(false), bForceGlobal(false), componentData(componentData_),
00060       configState(KConfigBase::NoAccess)
00061 {
00062     sGlobalFileName = componentData.dirs()->saveLocation("config") +
00063                           QString::fromLatin1("kdeglobals");
00064     if (wantGlobals()) {
00065         const KStandardDirs *const dirs = componentData.dirs();
00066         foreach(const QString& dir, dirs->findAllResources("config", QLatin1String("kdeglobals")) +
00067                                     dirs->findAllResources("config", QLatin1String("system.kdeglobals")))
00068             globalFiles.push_front(dir);
00069     }
00070     const QString etc_kderc =
00071 #ifdef Q_WS_WIN
00072         QFile::decodeName( qgetenv("WINDIR") + "/kde4rc" );
00073 #else
00074         QLatin1String("/etc/kde4rc");
00075 #endif
00076     KEntryMap tmp;
00077     // first entry is always /etc/kderc or empty if cannot read
00078     if (KStandardDirs::checkAccess(etc_kderc, R_OK)) {
00079         if (!globalFiles.contains(etc_kderc))
00080             globalFiles.push_front(etc_kderc);
00081 
00082         if (!mappingsRegistered) {
00083             KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, etc_kderc, QLatin1String("INI"));
00084             backend->parseConfig( "en_US", tmp, KConfigBackend::ParseDefaults);
00085         }
00086     } else {
00087         globalFiles.push_front(QString());
00088         mappingsRegistered = true;
00089     }
00090 
00091     if (!mappingsRegistered) {
00092         const QString kde4rc(QDir::home().filePath(".kde4rc"));
00093         if (KStandardDirs::checkAccess(kde4rc, R_OK)) {
00094             KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, kde4rc, QLatin1String("INI"));
00095             backend->parseConfig( "en_US", tmp, KConfigBackend::ParseOptions());
00096         }
00097         KConfigBackend::registerMappings(tmp);
00098         mappingsRegistered = true;
00099     }
00100     setLocale(KGlobal::hasLocale() ? KGlobal::locale()->language() : KLocale::defaultLanguage());
00101 }
00102 
00103 
00104 bool KConfigPrivate::lockLocal()
00105 {
00106     if (mBackend) {
00107         if (fileName == QLatin1String("kdeglobals")) { // we don't want to lock "kdeglobals" twice
00108             if (wantGlobals()) // "kdeglobals" will be locked with the global lock
00109                 return true; // so pretend we locked it here
00110         }
00111         return mBackend->lock(componentData);
00112     }
00113     // anonymous object - pretend we locked it
00114     return true;
00115 }
00116 
00117 void KConfigPrivate::copyGroup(const QByteArray& source, const QByteArray& destination,
00118                                 KConfigGroup *otherGroup, KConfigBase::WriteConfigFlags flags) const
00119 {
00120     KEntryMap& otherMap = otherGroup->config()->d_ptr->entryMap;
00121     const int len = source.length();
00122     const bool sameName = (destination == source);
00123 
00124     // we keep this bool outside the foreach loop so that if
00125     // the group is empty, we don't end up marking the other config
00126     // as dirty erroneously
00127     bool dirtied = false;
00128 
00129     for (KEntryMap::ConstIterator entryMapIt( entryMap.constBegin() ); entryMapIt != entryMap.constEnd(); ++entryMapIt) {
00130         const QByteArray& group = entryMapIt.key().mGroup;
00131 
00132         if (!group.startsWith(source)) // nothing to do
00133             continue;
00134 
00135         // don't copy groups that start with the same prefix, but are not sub-groups
00136         if (group.length() > len && group[len] != '\x1d')
00137             continue;
00138 
00139         KEntryKey newKey = entryMapIt.key();
00140 
00141         if (flags & KConfigBase::Localized) {
00142             newKey.bLocal = true;
00143         }
00144 
00145         if (!sameName)
00146             newKey.mGroup.replace(0, len, destination);
00147 
00148         KEntry entry = entryMap[ entryMapIt.key() ];
00149         dirtied = entry.bDirty = flags & KConfigBase::Persistent;
00150 
00151         if (flags & KConfigBase::Global) {
00152             entry.bGlobal = true;
00153         }
00154 
00155         otherMap[newKey] = entry;
00156     }
00157 
00158     if (dirtied) {
00159         otherGroup->config()->d_ptr->bDirty = true;
00160     }
00161 }
00162 
00163 KConfig::KConfig( const QString& file, OpenFlags mode,
00164                   const char* resourceType)
00165   : d_ptr(new KConfigPrivate(KGlobal::mainComponent(), mode, resourceType))
00166 {
00167     d_ptr->changeFileName(file, resourceType); // set the local file name
00168 
00169     // read initial information off disk
00170     reparseConfiguration();
00171 }
00172 
00173 KConfig::KConfig( const KComponentData& componentData, const QString& file, OpenFlags mode,
00174                   const char* resourceType)
00175     : d_ptr(new KConfigPrivate(componentData, mode, resourceType))
00176 {
00177     d_ptr->changeFileName(file, resourceType); // set the local file name
00178 
00179     // read initial information off disk
00180     reparseConfiguration();
00181 }
00182 
00183 KConfig::KConfig(const QString& file, const QString& backend, const char* resourceType)
00184     : d_ptr(new KConfigPrivate(KGlobal::mainComponent(), SimpleConfig, resourceType))
00185 {
00186     d_ptr->mBackend = KConfigBackend::create(d_ptr->componentData, file, backend);
00187     d_ptr->bDynamicBackend = false;
00188     d_ptr->changeFileName(file, ""); // set the local file name
00189 
00190     // read initial information off disk
00191     reparseConfiguration();
00192 }
00193 
00194 KConfig::KConfig(KConfigPrivate &d)
00195     : d_ptr(&d)
00196 {
00197 }
00198 
00199 KConfig::~KConfig()
00200 {
00201     Q_D(KConfig);
00202     if (d->bDirty && d->mBackend.isUnique())
00203         sync();
00204     delete d;
00205 }
00206 
00207 const KComponentData& KConfig::componentData() const
00208 {
00209     Q_D(const KConfig);
00210     return d->componentData;
00211 }
00212 
00213 QStringList KConfig::groupList() const
00214 {
00215     Q_D(const KConfig);
00216     QStringList groups;
00217 
00218     for (KEntryMap::ConstIterator entryMapIt( d->entryMap.constBegin() ); entryMapIt != d->entryMap.constEnd(); ++entryMapIt) {
00219         const QByteArray group = entryMapIt.key().mGroup;
00220         if (entryMapIt.key().mKey.isNull() && !group.isEmpty() &&
00221             group != "<default>" && group != "$Version" && !group.contains('\x1d'))
00222             groups << QString::fromUtf8(group);
00223     }
00224 
00225     return groups;
00226 }
00227 
00228 QStringList KConfigPrivate::groupList(const QByteArray& group) const
00229 {
00230     QByteArray theGroup = group + '\x1d';
00231     QSet<QString> groups;
00232 
00233     for (KEntryMap::ConstIterator entryMapIt( entryMap.constBegin() ); entryMapIt != entryMap.constEnd(); ++entryMapIt)
00234         if (entryMapIt.key().mKey.isNull() && entryMapIt.key().mGroup.startsWith(theGroup))
00235         {
00236             QString groupname = QString::fromUtf8(entryMapIt.key().mGroup.mid(theGroup.length()));
00237             groups << groupname.left(groupname.indexOf('\x1d'));
00238         }
00239 
00240     return groups.toList();
00241 }
00242 
00243 QStringList KConfig::keyList(const QString& aGroup) const
00244 {
00245     Q_D(const KConfig);
00246     QStringList keys;
00247     const QByteArray theGroup(aGroup.isEmpty() ? "<default>" : aGroup.toUtf8());
00248 
00249     const KEntryMapConstIterator theEnd = d->entryMap.constEnd();
00250     KEntryMapConstIterator it = d->entryMap.findEntry(theGroup);
00251     if (it != theEnd) {
00252         ++it; // advance past the special group entry marker
00253 
00254         QSet<QString> tmp;
00255         for (; it != theEnd && it.key().mGroup == theGroup; ++it) {
00256             const KEntryKey& key = it.key();
00257             if (key.mGroup == theGroup && !key.mKey.isNull() && !it->bDeleted)
00258                 tmp << QString::fromUtf8(key.mKey);
00259         }
00260         keys = tmp.toList();
00261     }
00262 
00263     return keys;
00264 }
00265 
00266 QMap<QString,QString> KConfig::entryMap(const QString& aGroup) const
00267 {
00268     Q_D(const KConfig);
00269     QMap<QString, QString> theMap;
00270     const QByteArray theGroup(aGroup.isEmpty() ? "<default>" : aGroup.toUtf8());
00271 
00272     const KEntryMapConstIterator theEnd = d->entryMap.constEnd();
00273     KEntryMapConstIterator it = d->entryMap.findEntry(theGroup, 0, 0);
00274     if (it != theEnd) {
00275         ++it; // advance past the special group entry marker
00276 
00277         for (; it != theEnd && it.key().mGroup == theGroup; ++it) {
00278             // leave the default values and deleted entries out
00279             if (!it->bDeleted && !it.key().bDefault) {
00280                 const QString key = QString::fromUtf8(it.key().mKey.constData());
00281                 // the localized entry should come first, so don't overwrite it
00282                 // with the non-localized entry
00283                 if (!theMap.contains(key))
00284                     theMap.insert(key,QString::fromUtf8(it->mValue.constData()));
00285             }
00286         }
00287     }
00288 
00289     return theMap;
00290 }
00291 
00292 void KConfig::sync()
00293 {
00294     Q_D(KConfig);
00295 
00296     Q_ASSERT(!isImmutable() && !name().isEmpty()); // can't write to an immutable or anonymous file.
00297 
00298     if (d->bDirty && d->mBackend) {
00299         const QByteArray utf8Locale(locale().toUtf8());
00300 
00301         // Create the containing dir, maybe it wasn't there
00302         d->mBackend->createEnclosing();
00303 
00304         // lock the local file
00305         if (d->configState == ReadWrite && !d->lockLocal()) {
00306             qWarning() << "couldn't lock local file";
00307             return;
00308         }
00309 
00310         // Rewrite global/local config only if there is a dirty entry in it.
00311         bool writeGlobals = false;
00312         bool writeLocals = false;
00313         foreach (const KEntry& e, d->entryMap) {
00314             if (e.bDirty) {
00315                 if (e.bGlobal) {
00316                     writeGlobals = true;
00317                 } else {
00318                     writeLocals = true;
00319                 }
00320 
00321                 if (writeGlobals && writeLocals) {
00322                     break;
00323                 }
00324             }
00325         }
00326 
00327         d->bDirty = false; // will revert to true if a config write fails
00328 
00329         if (d->wantGlobals() && writeGlobals) {
00330             KSharedPtr<KConfigBackend> tmp = KConfigBackend::create(componentData(), d->sGlobalFileName);
00331             if (d->configState == ReadWrite && !tmp->lock(componentData())) {
00332                 qWarning() << "couldn't lock global file";
00333                 return;
00334             }
00335             if (!tmp->writeConfig(utf8Locale, d->entryMap, KConfigBackend::WriteGlobal, d->componentData)) {
00336                 d->bDirty = true;
00337             }
00338             if (tmp->isLocked()) {
00339                 tmp->unlock();
00340             }
00341         }
00342 
00343         if (writeLocals) {
00344             if (!d->mBackend->writeConfig(utf8Locale, d->entryMap, KConfigBackend::WriteOptions(), d->componentData)) {
00345                 d->bDirty = true;
00346             }
00347         }
00348         if (d->mBackend->isLocked()) {
00349             d->mBackend->unlock();
00350         }
00351     }
00352 }
00353 
00354 void KConfig::markAsClean()
00355 {
00356     Q_D(KConfig);
00357     d->bDirty = false;
00358 
00359     // clear any dirty flags that entries might have set
00360     const KEntryMapIterator theEnd = d->entryMap.end();
00361     for (KEntryMapIterator it = d->entryMap.begin(); it != theEnd; ++it)
00362         it->bDirty = false;
00363 }
00364 
00365 void KConfig::checkUpdate(const QString &id, const QString &updateFile)
00366 {
00367     const KConfigGroup cg(this, "$Version");
00368     const QString cfg_id = updateFile+':'+id;
00369     QStringList ids = cg.readEntry("update_info", QStringList());
00370     if (!ids.contains(cfg_id)) {
00371         KToolInvocation::kdeinitExecWait("kconf_update", QStringList() << "--check" << updateFile);
00372         reparseConfiguration();
00373     }
00374 }
00375 
00376 KConfig* KConfig::copyTo(const QString &file, KConfig *config) const
00377 {
00378     Q_D(const KConfig);
00379     if (!config)
00380         config = new KConfig(componentData(), QString(), SimpleConfig);
00381     config->d_func()->changeFileName(file, d->resourceType);
00382     config->d_func()->entryMap = d->entryMap;
00383     config->d_func()->bFileImmutable = false;
00384 
00385     const KEntryMapIterator theEnd = config->d_func()->entryMap.end();
00386     for (KEntryMapIterator it = config->d_func()->entryMap.begin(); it != theEnd; ++it)
00387         it->bDirty = true;
00388     config->d_ptr->bDirty = true;
00389 
00390     return config;
00391 }
00392 
00393 QString KConfig::name() const
00394 {
00395     Q_D(const KConfig);
00396     return d->fileName;
00397 }
00398 
00399 void KConfigPrivate::changeFileName(const QString& name, const char* type)
00400 {
00401     fileName = name;
00402 
00403     QString file;
00404     if (name.isEmpty()) {
00405         if (wantDefaults()) { // accessing default app-specific config "appnamerc"
00406             const QString appName = componentData.aboutData()->appName();
00407             if (!appName.isEmpty()) {
00408                 fileName = appName + QLatin1String("rc");
00409                 if (type && *type)
00410                     resourceType = type; // only change it if it's not empty
00411                 file = KStandardDirs::locateLocal(resourceType, fileName, false, componentData);
00412             }
00413         } else if (wantGlobals()) { // accessing "kdeglobals"
00414             resourceType = "config";
00415             fileName = QLatin1String("kdeglobals");
00416             file = sGlobalFileName;
00417         }
00418     } else if (QDir::isAbsolutePath(fileName))
00419         file = fileName;
00420     else {
00421         if (type && *type)
00422             resourceType = type; // only change it if it's not empty
00423         file = KStandardDirs::locateLocal(resourceType, fileName, false, componentData);
00424 
00425         if (fileName == QLatin1String("kdeglobals"))
00426             openFlags |= KConfig::IncludeGlobals;
00427     }
00428 
00429     bForceGlobal = (fileName == QLatin1String("kdeglobals"));
00430 
00431     if (file.isEmpty()) {
00432         openFlags = KConfig::SimpleConfig;
00433         return;
00434     }
00435 
00436     if (bDynamicBackend || !mBackend) // allow dynamic changing of backend
00437         mBackend = KConfigBackend::create(componentData, file);
00438     else
00439         mBackend->setFilePath(file);
00440 
00441     configState = mBackend->accessMode();
00442 }
00443 
00444 void KConfig::reparseConfiguration()
00445 {
00446     Q_D(KConfig);
00447     // Don't lose pending changes
00448     if (!d->isReadOnly() && d->bDirty)
00449         sync();
00450 
00451     d->entryMap.clear();
00452 
00453     d->bFileImmutable = false;
00454 
00455     // Parse all desired files from the least to the most specific.
00456     if (d->wantGlobals())
00457         d->parseGlobalFiles();
00458 
00459     d->parseConfigFiles();
00460 }
00461 
00462 void KConfigPrivate::parseGlobalFiles()
00463 {
00464 //    qDebug() << "parsing global files" << globalFiles;
00465 
00466     // TODO: can we cache the values in etc_kderc / other global files
00467     //       on a per-application basis?
00468     const QByteArray utf8Locale = locale.toUtf8();
00469     foreach(const QString& file, globalFiles) {
00470         KConfigBackend::ParseOptions parseOpts = KConfigBackend::ParseGlobal|KConfigBackend::ParseExpansions;
00471         if (file != sGlobalFileName)
00472             parseOpts |= KConfigBackend::ParseDefaults;
00473 
00474         KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, file);
00475         if ( backend->parseConfig( utf8Locale, entryMap, parseOpts) == KConfigBackend::ParseImmutable)
00476             break;
00477     }
00478 }
00479 
00480 void KConfigPrivate::parseConfigFiles()
00481 {
00482     if (fileName == QLatin1String("kdeglobals") && wantGlobals())
00483         return; // already parsed in parseGlobalFiles()
00484 
00485     // can only read the file if there is a backend and a file name
00486     if (mBackend && !fileName.isEmpty()) {
00487 
00488         bFileImmutable = false;
00489         QList<QString> files;
00490 
00491         if (wantDefaults())
00492             foreach (const QString& f, componentData.dirs()->findAllResources(resourceType, fileName))
00493                 files.prepend(f);
00494         else
00495             files << mBackend->filePath();
00496 
00497         if (!isSimple())
00498             files = extraFiles.toList() + files;
00499 
00500 //        qDebug() << "parsing local files" << files;
00501 
00502         const QByteArray utf8Locale = locale.toUtf8();
00503         foreach(const QString& file, files) {
00504             if (file == mBackend->filePath()) {
00505                 switch (mBackend->parseConfig(utf8Locale, entryMap, KConfigBackend::ParseExpansions)) {
00506                 case KConfigBackend::ParseOk:
00507                     break;
00508                 case KConfigBackend::ParseImmutable:
00509                     bFileImmutable = true;
00510                     break;
00511                 case KConfigBackend::ParseOpenError:
00512                     configState = KConfigBase::NoAccess;
00513                     break;
00514                 }
00515             } else {
00516                 KSharedPtr<KConfigBackend> backend = KConfigBackend::create(componentData, file);
00517                 bFileImmutable = (backend->parseConfig(utf8Locale, entryMap,
00518                                         KConfigBackend::ParseDefaults|KConfigBackend::ParseExpansions)
00519                                   == KConfigBackend::ParseImmutable);
00520             }
00521 
00522             if (bFileImmutable)
00523                 break;
00524         }
00525         if (componentData.dirs()->isRestrictedResource(resourceType, fileName))
00526             bFileImmutable = true;
00527     }
00528 }
00529 
00530 KConfig::AccessMode KConfig::accessMode() const
00531 {
00532     Q_D(const KConfig);
00533     return d->configState;
00534 }
00535 
00536 void KConfig::addConfigSources(const QStringList& files)
00537 {
00538     Q_D(KConfig);
00539     foreach(const QString& file, files) {
00540         d->extraFiles.push(file);
00541     }
00542 
00543     if (!files.isEmpty()) {
00544         reparseConfiguration();
00545     }
00546 }
00547 
00548 QString KConfig::locale() const
00549 {
00550     Q_D(const KConfig);
00551     return d->locale;
00552 }
00553 
00554 bool KConfigPrivate::setLocale(const QString& aLocale)
00555 {
00556     if (aLocale != locale) {
00557         locale = aLocale;
00558         return true;
00559     }
00560     return false;
00561 }
00562 
00563 bool KConfig::setLocale(const QString& locale)
00564 {
00565     Q_D(KConfig);
00566     if (d->setLocale(locale)) {
00567         reparseConfiguration();
00568         return true;
00569     }
00570     return false;
00571 }
00572 
00573 void KConfig::setReadDefaults(bool b)
00574 {
00575     Q_D(KConfig);
00576     d->bReadDefaults = b;
00577 }
00578 
00579 bool KConfig::readDefaults() const
00580 {
00581     Q_D(const KConfig);
00582     return d->bReadDefaults;
00583 }
00584 
00585 bool KConfig::isImmutable() const
00586 {
00587     Q_D(const KConfig);
00588     return d->bFileImmutable;
00589 }
00590 
00591 bool KConfig::isGroupImmutableImpl(const QByteArray& aGroup) const
00592 {
00593     Q_D(const KConfig);
00594     return isImmutable() || d->entryMap.getEntryOption(aGroup, 0, 0, KEntryMap::EntryImmutable);
00595 }
00596 
00597 void KConfig::setForceGlobal(bool b)
00598 {
00599     Q_D(KConfig);
00600     d->bForceGlobal = b;
00601 }
00602 
00603 bool KConfig::forceGlobal() const
00604 {
00605     Q_D(const KConfig);
00606     return d->bForceGlobal;
00607 }
00608 
00609 KConfigGroup KConfig::groupImpl(const QByteArray &group)
00610 {
00611     return KConfigGroup(this, group.constData());
00612 }
00613 
00614 const KConfigGroup KConfig::groupImpl(const QByteArray &group) const
00615 {
00616     return KConfigGroup(this, group.constData());
00617 }
00618 
00619 KEntryMap::EntryOptions convertToOptions(KConfig::WriteConfigFlags flags)
00620 {
00621     KEntryMap::EntryOptions options=0;
00622 
00623     if (flags&KConfig::Persistent)
00624         options |= KEntryMap::EntryDirty;
00625     if (flags&KConfig::Global)
00626         options |= KEntryMap::EntryGlobal;
00627     if (flags&KConfig::Localized)
00628         options |= KEntryMap::EntryLocalized;
00629     return options;
00630 }
00631 
00632 void KConfig::deleteGroupImpl(const QByteArray &aGroup, WriteConfigFlags flags)
00633 {
00634     Q_D(KConfig);
00635     KEntryMap::EntryOptions options = convertToOptions(flags)|KEntryMap::EntryDeleted;
00636 
00637     QByteArray theGroup = aGroup + '\x1d';
00638     QSet<QByteArray> groups;
00639     groups << aGroup;
00640 
00641     for (KEntryMap::ConstIterator entryMapIt( d->entryMap.constBegin() ); entryMapIt != d->entryMap.constEnd(); ++entryMapIt) {
00642         if (entryMapIt.key().mKey.isNull() && entryMapIt.key().mGroup.startsWith(theGroup)) {
00643             groups << entryMapIt.key().mGroup;
00644         }
00645     }
00646 
00647     foreach (const QByteArray& group, groups) {
00648         const QStringList keys = keyList(QString::fromUtf8(group));
00649         foreach (const QString& _key, keys) {
00650             const QByteArray &key = _key.toUtf8();
00651             if (d->canWriteEntry(group, key.constData())) {
00652                 d->entryMap.setEntry(group, key, QByteArray(), options);
00653                 d->bDirty = true;
00654             }
00655         }
00656     }
00657 }
00658 
00659 bool KConfig::isConfigWritable(bool warnUser)
00660 {
00661     Q_D(KConfig);
00662     bool allWritable = (d->mBackend.isNull()? false: d->mBackend->isWritable());
00663 
00664     if (warnUser && !allWritable) {
00665         QString errorMsg;
00666         if (!d->mBackend.isNull()) // TODO how can be it be null? Set errorMsg appropriately
00667             errorMsg = d->mBackend->nonWritableErrorMessage();
00668 
00669         // Note: We don't ask the user if we should not ask this question again because we can't save the answer.
00670         errorMsg += i18n("Please contact your system administrator.");
00671         QString cmdToExec = KStandardDirs::findExe(QString("kdialog"));
00672         if (!cmdToExec.isEmpty() && componentData().isValid())
00673         {
00674             QProcess::execute(cmdToExec,QStringList() << "--title" << componentData().componentName()
00675                     << "--msgbox" << errorMsg);
00676         }
00677     }
00678 
00679     d->configState = allWritable ?  ReadWrite : ReadOnly; // update the read/write status
00680 
00681     return allWritable;
00682 }
00683 
00684 bool KConfig::hasGroupImpl(const QByteArray& aGroup) const
00685 {
00686     Q_D(const KConfig);
00687     return d->entryMap.hasEntry(aGroup);
00688 }
00689 
00690 bool KConfigPrivate::canWriteEntry(const QByteArray& group, const char* key, bool isDefault) const
00691 {
00692     if (bFileImmutable ||
00693         entryMap.getEntryOption(group, key, KEntryMap::SearchLocalized, KEntryMap::EntryImmutable))
00694         return isDefault;
00695     return true;
00696 }
00697 
00698 void KConfigPrivate::putData( const QByteArray& group, const char* key,
00699                       const QByteArray& value, KConfigBase::WriteConfigFlags flags, bool expand)
00700 {
00701     KEntryMap::EntryOptions options = convertToOptions(flags);
00702     
00703     if (bForceGlobal)
00704         options |= KEntryMap::EntryGlobal;
00705     if (expand)
00706         options |= KEntryMap::EntryExpansion;
00707 
00708     if (value.isNull()) // deleting entry
00709         options |= KEntryMap::EntryDeleted;
00710 
00711     bool dirtied = entryMap.setEntry(group, key, value, options);
00712     if( flags &  KConfigBase::Persistent && dirtied )
00713         bDirty = true;
00714 }
00715 
00716 QByteArray KConfigPrivate::lookupData(const QByteArray& group, const char* key,
00717                                       KEntryMap::SearchFlags flags) const
00718 {
00719     if (bReadDefaults)
00720         flags |= KEntryMap::SearchDefaults;
00721     const KEntryMapConstIterator it = entryMap.findEntry(group, key, flags);
00722     if (it == entryMap.constEnd())
00723         return QByteArray();
00724     return it->mValue;
00725 }
00726 
00727 QString KConfigPrivate::lookupData(const QByteArray& group, const char* key,
00728                                    KEntryMap::SearchFlags flags, bool *expand) const
00729 {
00730     if (bReadDefaults)
00731         flags |= KEntryMap::SearchDefaults;
00732     return entryMap.getEntry(group, key, QString(), flags, expand);
00733 }
00734 
00735 void KConfig::virtual_hook(int /*id*/, void* /*data*/)
00736 {
00737     /* nothing */
00738 }
00739 

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal