diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2022-07-14 15:42:43 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2022-08-19 16:16:54 +0000 |
commit | 73de2d51c0cd9e35c86584a18d9bcb3c08672f5f (patch) | |
tree | fb88aee6260fec922f1ffab4ab582ae3c023fcbe | |
parent | 607af6a32c60a4667982b1ad23e7662f693cb7d7 (diff) | |
download | kconfig-73de2d51c0cd9e35c86584a18d9bcb3c08672f5f.tar.gz kconfig-73de2d51c0cd9e35c86584a18d9bcb3c08672f5f.tar.bz2 |
General code clean-up
- use the strings in a QStringList directly
- slightly better readability: less if-else-nesting, return early
GIT_SILENT
-rw-r--r-- | src/core/kconfigdata.cpp | 233 | ||||
-rw-r--r-- | src/core/kconfigini.cpp | 75 | ||||
-rw-r--r-- | src/core/kconfigwatcher.cpp | 11 |
3 files changed, 163 insertions, 156 deletions
diff --git a/src/core/kconfigdata.cpp b/src/core/kconfigdata.cpp index 0d6a7bb9..e13f29f5 100644 --- a/src/core/kconfigdata.cpp +++ b/src/core/kconfigdata.cpp @@ -154,62 +154,64 @@ bool KEntryMap::setEntry(const QByteArray &group, const QByteArray &key, const Q } // TODO check for presence of unlocalized key return true; - } else { - // KEntry e2 = it.value(); - if (options & EntryLocalized) { - // fast exit checks for cases where the existing entry is more specific - const KEntry &e2 = it.value(); - if (e2.bLocalizedCountry && !e.bLocalizedCountry) { - // lang_COUNTRY > lang - return false; - } + } + + // KEntry e2 = it.value(); + if (options & EntryLocalized) { + // fast exit checks for cases where the existing entry is more specific + const KEntry &e2 = it.value(); + if (e2.bLocalizedCountry && !e.bLocalizedCountry) { + // lang_COUNTRY > lang + return false; + } + } + + if (it.value() != e) { + // qDebug() << "changing" << k << "from" << it.value().mValue << "to" << value << e; + it.value() = e; + if (k.bDefault) { + KEntryKey nonDefaultKey(k); + nonDefaultKey.bDefault = false; + insert(nonDefaultKey, e); } - if (it.value() != e) { - // qDebug() << "changing" << k << "from" << it.value().mValue << "to" << value << e; - it.value() = e; + if (!(options & EntryLocalized)) { + KEntryKey theKey(group, key, true, false); + // qDebug() << "non-localized entry, remove localized one:" << theKey; + remove(theKey); if (k.bDefault) { - KEntryKey nonDefaultKey(k); - nonDefaultKey.bDefault = false; - insert(nonDefaultKey, e); - } - if (!(options & EntryLocalized)) { - KEntryKey theKey(group, key, true, false); - // qDebug() << "non-localized entry, remove localized one:" << theKey; + theKey.bDefault = true; remove(theKey); - if (k.bDefault) { - theKey.bDefault = true; - remove(theKey); - } } - return true; - } else { - // qDebug() << k << "was already set to" << e.mValue; - if (!(options & EntryLocalized)) { - // qDebug() << "unchanged non-localized entry, remove localized one."; - KEntryKey theKey(group, key, true, false); - bool ret = false; - Iterator cit = find(theKey); - if (cit != end()) { - erase(cit); - ret = true; - } - if (k.bDefault) { - theKey.bDefault = true; - Iterator cit = find(theKey); - if (cit != end()) { - erase(cit); - return true; - } - } - return ret; + } + return true; + } + + // qDebug() << k << "was already set to" << e.mValue; + if (!(options & EntryLocalized)) { + // qDebug() << "unchanged non-localized entry, remove localized one."; + KEntryKey theKey(group, key, true, false); + bool ret = false; + Iterator cit = find(theKey); + if (cit != end()) { + erase(cit); + ret = true; + } + if (k.bDefault) { + theKey.bDefault = true; + Iterator cit = find(theKey); + if (cit != end()) { + erase(cit); + return true; } - // qDebug() << "localized entry, unchanged, return false"; - // When we are writing a default, we know that the non- - // default is the same as the default, so we can simply - // use the same branch. - return false; } + return ret; } + + // qDebug() << "localized entry, unchanged, return false"; + // When we are writing a default, we know that the non- + // default is the same as the default, so we can simply + // use the same branch. + return false; } QString KEntryMap::getEntry(const QByteArray &group, const QByteArray &key, const QString &defaultValue, KEntryMap::SearchFlags flags, bool *expand) const @@ -248,55 +250,57 @@ bool KEntryMap::hasEntry(const QByteArray &group, const QByteArray &key, KEntryM bool KEntryMap::getEntryOption(const KEntryMapConstIterator &it, KEntryMap::EntryOption option) const { - if (it != constEnd()) { - switch (option) { - case EntryDirty: - return it->bDirty; - case EntryLocalized: - return it.key().bLocal; - case EntryGlobal: - return it->bGlobal; - case EntryImmutable: - return it->bImmutable; - case EntryDeleted: - return it->bDeleted; - case EntryExpansion: - return it->bExpand; - case EntryNotify: - return it->bNotify; - default: - break; // fall through - } + if (it == cend()) { + return false; } - return false; + switch (option) { + case EntryDirty: + return it->bDirty; + case EntryLocalized: + return it.key().bLocal; + case EntryGlobal: + return it->bGlobal; + case EntryImmutable: + return it->bImmutable; + case EntryDeleted: + return it->bDeleted; + case EntryExpansion: + return it->bExpand; + case EntryNotify: + return it->bNotify; + default: + return false; + } } void KEntryMap::setEntryOption(KEntryMapIterator it, KEntryMap::EntryOption option, bool bf) { - if (it != end()) { - switch (option) { - case EntryDirty: - it->bDirty = bf; - break; - case EntryGlobal: - it->bGlobal = bf; - break; - case EntryImmutable: - it->bImmutable = bf; - break; - case EntryDeleted: - it->bDeleted = bf; - break; - case EntryExpansion: - it->bExpand = bf; - break; - case EntryNotify: - it->bNotify = bf; - break; - default: - break; // fall through - } + if (it == end()) { + return; + } + + switch (option) { + case EntryDirty: + it->bDirty = bf; + return; + case EntryGlobal: + it->bGlobal = bf; + return; + case EntryImmutable: + it->bImmutable = bf; + return; + case EntryDeleted: + it->bDeleted = bf; + return; + case EntryExpansion: + it->bExpand = bf; + return; + case EntryNotify: + it->bNotify = bf; + return; + default: + return; // fall through } } @@ -304,29 +308,30 @@ bool KEntryMap::revertEntry(const QByteArray &group, const QByteArray &key, KEnt { Q_ASSERT((flags & KEntryMap::SearchDefaults) == 0); Iterator entry = findEntry(group, key, flags); - if (entry != end()) { - // qDebug() << "reverting" << entry.key() << " = " << entry->mValue; - if (entry->bReverted) { // already done before - return false; - } + if (entry == end()) { + return false; + } - KEntryKey defaultKey(entry.key()); - defaultKey.bDefault = true; - // qDebug() << "looking up default entry with key=" << defaultKey; - const auto defaultEntry = constFind(defaultKey); - if (defaultEntry != constEnd()) { - Q_ASSERT(defaultEntry.key().bDefault); - // qDebug() << "found, update entry"; - *entry = *defaultEntry; // copy default value, for subsequent lookups - } else { - entry->mValue = QByteArray(); - } - entry->bNotify = entry->bNotify || (options & EntryNotify); - entry->bDirty = true; - entry->bReverted = true; // skip it when writing out to disk + // qDebug() << "reverting" << entry.key() << " = " << entry->mValue; + if (entry->bReverted) { // already done before + return false; + } - // qDebug() << "Here's what we have now:" << *this; - return true; + KEntryKey defaultKey(entry.key()); + defaultKey.bDefault = true; + // qDebug() << "looking up default entry with key=" << defaultKey; + const auto defaultEntry = constFind(defaultKey); + if (defaultEntry != constEnd()) { + Q_ASSERT(defaultEntry.key().bDefault); + // qDebug() << "found, update entry"; + *entry = *defaultEntry; // copy default value, for subsequent lookups + } else { + entry->mValue = QByteArray(); } - return false; + entry->bNotify = entry->bNotify || (options & EntryNotify); + entry->bDirty = true; + entry->bReverted = true; // skip it when writing out to disk + + // qDebug() << "Here's what we have now:" << *this; + return true; } diff --git a/src/core/kconfigini.cpp b/src/core/kconfigini.cpp index 290c3354..384bed35 100644 --- a/src/core/kconfigini.cpp +++ b/src/core/kconfigini.cpp @@ -534,27 +534,26 @@ bool KConfigIniBackend::writeConfig(const QByteArray &locale, KEntryMap &entryMa bool KConfigIniBackend::isWritable() const { const QString filePath = this->filePath(); - if (!filePath.isEmpty()) { - QFileInfo file(filePath); - if (!file.exists()) { - // If the file does not exist, check if the deepest - // existing dir is writable. - QFileInfo dir(file.absolutePath()); - while (!dir.exists()) { - QString parent = dir.absolutePath(); // Go up. Can't use cdUp() on non-existing dirs. - if (parent == dir.filePath()) { - // no parent - return false; - } - dir.setFile(parent); - } - return dir.isDir() && dir.isWritable(); - } else { - return file.isWritable(); - } + if (filePath.isEmpty()) { + return false; + } + + QFileInfo file(filePath); + if (file.exists()) { + return file.isWritable(); } - return false; + // If the file does not exist, check if the deepest existing dir is writable + QFileInfo dir(file.absolutePath()); + while (!dir.exists()) { + QString parent = dir.absolutePath(); // Go up. Can't use cdUp() on non-existing dirs. + if (parent == dir.filePath()) { + // no parent + return false; + } + dir.setFile(parent); + } + return dir.isDir() && dir.isWritable(); } QString KConfigIniBackend::nonWritableErrorMessage() const @@ -570,28 +569,28 @@ void KConfigIniBackend::createEnclosing() } // Create the containing dir, maybe it wasn't there - QDir dir; - dir.mkpath(QFileInfo(file).absolutePath()); + QDir().mkpath(QFileInfo(file).absolutePath()); } -void KConfigIniBackend::setFilePath(const QString &file) +void KConfigIniBackend::setFilePath(const QString &path) { - if (file.isEmpty()) { + if (path.isEmpty()) { return; } - Q_ASSERT(QDir::isAbsolutePath(file)); + Q_ASSERT(QDir::isAbsolutePath(path)); - const QFileInfo info(file); + const QFileInfo info(path); if (info.exists()) { setLocalFilePath(info.canonicalFilePath()); + return; + } + + if (QString filePath = info.dir().canonicalPath(); !filePath.isEmpty()) { + filePath += QLatin1Char('/') + info.fileName(); + setLocalFilePath(filePath); } else { - const QString dir = info.dir().canonicalPath(); - if (!dir.isEmpty()) { - setLocalFilePath(dir + QLatin1Char('/') + info.fileName()); - } else { - setLocalFilePath(file); - } + setLocalFilePath(path); } } @@ -612,22 +611,24 @@ bool KConfigIniBackend::lock() { Q_ASSERT(!filePath().isEmpty()); - if (!lockFile) { #ifdef Q_OS_ANDROID + if (!lockFile) { // handle content Uris properly if (filePath().startsWith(QLatin1String("content://"))) { // we can't create file at an arbitrary location, so use internal storage to create one // NOTE: filename can be the same, but because this lock is short lived we may never have a collision - lockFile = new QLockFile(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) - + QLatin1String("/") + QFileInfo(filePath()).fileName() + QLatin1String(".lock")); + lockFile = new QLockFile(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/") + + QFileInfo(filePath()).fileName() + QLatin1String(".lock")); } else { -#endif lockFile = new QLockFile(filePath() + QLatin1String(".lock")); -#ifdef Q_OS_ANDROID } -#endif } +#else + if (!lockFile) { + lockFile = new QLockFile(filePath() + QLatin1String(".lock")); + } +#endif lockFile->lock(); return lockFile->isLocked(); diff --git a/src/core/kconfigwatcher.cpp b/src/core/kconfigwatcher.cpp index ae5d8849..0811db80 100644 --- a/src/core/kconfigwatcher.cpp +++ b/src/core/kconfigwatcher.cpp @@ -56,12 +56,13 @@ KConfigWatcher::KConfigWatcher(const KSharedConfig::Ptr &config) qDBusRegisterMetaType<QByteArrayList>(); qDBusRegisterMetaType<QHash<QString, QByteArrayList>>(); - QStringList watchedPaths; - watchedPaths << QLatin1Char('/') + d->m_config->name(); - const auto cfgSources = d->m_config->additionalConfigSources(); - for (const QString &file : cfgSources) { - watchedPaths << QLatin1Char('/') + file; + + QStringList watchedPaths = d->m_config->additionalConfigSources(); + for (QString &file : watchedPaths) { + file.prepend(QLatin1Char('/')); } + watchedPaths.prepend(QLatin1Char('/') + d->m_config->name()); + if (d->m_config->openFlags() & KConfig::IncludeGlobals) { watchedPaths << QStringLiteral("/kdeglobals"); } |