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

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.6
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