[PATCH] Allow reading of values on invalid KConfigGroups

Sebastian TrĂ¼g trueg at kde.org
Mon Feb 23 21:02:26 GMT 2009


--Boundary-00=_i7woJ3WD2hkFX8e
Content-Type: text/plain;
  charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

The attached patch allows to read values from invalid KConfigGroups instead of 
throughing an assert. This allows to reuse typical loadSettings(const 
KConfigGroup&) methods for loading of defaults. In my case it allowed to 
remove a lot of code duplication from K3b.

Comments?

Cheers,
Sebastian


--Boundary-00=_i7woJ3WD2hkFX8e
Content-Type: text/x-patch;
  charset="UTF-8";
  name="kconfiggroup-allow-read-on-invalid.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="kconfiggroup-allow-read-on-invalid.diff"

Index: kconfiggroup.cpp
===================================================================
--- kconfiggroup.cpp	(revision 930557)
+++ kconfiggroup.cpp	(working copy)
@@ -744,20 +744,20 @@
 
 QString KConfigGroup::readEntry(const char* key, const QString& aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readEntry", "accessing an invalid group");
+    if(isValid()) {
+        bool expand = false;
 
-    bool expand = false;
+        // read value from the entry map
+        QString aValue = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized,
+                                                        &expand);
+        if (aValue.isNull())
+            aValue = aDefault;
 
-    // read value from the entry map
-    QString aValue = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized,
-                                           &expand);
-    if (aValue.isNull())
-        aValue = aDefault;
+        if (expand)
+            return KConfigGroupPrivate::expandString(aValue);
+    }
 
-    if (expand)
-        return KConfigGroupPrivate::expandString(aValue);
-
-    return aValue;
+    return aDefault;
 }
 
 QString KConfigGroup::readEntry(const QString &key, const QString& aDefault) const
@@ -767,13 +767,13 @@
 
 QStringList KConfigGroup::readEntry(const char* key, const QStringList& aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readEntry", "accessing an invalid group");
+    if(isValid()) {
+        const QString data = readEntry(key, QString());
+        if (!data.isNull())
+            return KConfigGroupPrivate::deserializeList(data);
+    }
 
-    const QString data = readEntry(key, QString());
-    if (data.isNull())
-        return aDefault;
-
-    return KConfigGroupPrivate::deserializeList(data);
+    return aDefault;
 }
 
 QStringList KConfigGroup::readEntry( const QString& key, const QStringList& aDefault) const
@@ -783,17 +783,17 @@
 
 QVariant KConfigGroup::readEntry( const char* key, const QVariant &aDefault ) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readEntry", "accessing an invalid group");
+    if(isValid()) {
+        const QByteArray data = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized);
+        if (!data.isNull()) {
+            QVariant value;
+            if (!readEntryGui( data, key, aDefault, value ))
+                return convertToQVariant(key, data, aDefault);
+            return value;
+        }
+    }
 
-    const QByteArray data = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized);
-    if (data.isNull())
-        return aDefault;
-
-    QVariant value;
-    if (!readEntryGui( data, key, aDefault, value ))
-        return convertToQVariant(key, data, aDefault);
-
-    return value;
+    return aDefault;
 }
 
 QVariant KConfigGroup::readEntry( const QString& key, const QVariant& aDefault) const
@@ -803,17 +803,17 @@
 
 QVariantList KConfigGroup::readEntry( const char* key, const QVariantList& aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readEntry", "accessing an invalid group");
+    if(isValid()) {
+        const QString data = readEntry(key, QString());
+        if (!data.isNull()) {
+            QVariantList value;
+            foreach(const QString& v, KConfigGroupPrivate::deserializeList(data))
+                value << v;
+            return value;
+        }
+    }
 
-    const QString data = readEntry(key, QString());
-    if (data.isNull())
-        return aDefault;
-
-    QVariantList value;
-    foreach(const QString& v, KConfigGroupPrivate::deserializeList(data))
-        value << v;
-
-    return value;
+    return aDefault;
 }
 
 QVariantList KConfigGroup::readEntry( const QString& key, const QVariantList& aDefault) const
@@ -828,37 +828,38 @@
 
 QStringList KConfigGroup::readXdgListEntry(const char *key, const QStringList& aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readXdgListEntry", "accessing an invalid group");
-
-    const QString data = readEntry(key, QString());
-    if (data.isNull())
-        return aDefault;
-
-    QStringList value;
-    QString val;
-    val.reserve(data.size());
-    // XXX List serialization being a separate layer from low-level parsing is
-    // probably a bug. No affected entries are defined, though.
-    bool quoted = false;
-    for (int p = 0; p < data.length(); p++) {
-        if (quoted) {
-            val += data[p];
-            quoted = false;
-        } else if (data[p] == '\\') {
-            quoted = true;
-        } else if (data[p] == ';') {
-            value.append(val);
-            val.clear();
-            val.reserve(data.size() - p);
-        } else {
-            val += data[p];
+    if(isValid()) {
+        const QString data = readEntry(key, QString());
+        if (!data.isNull()) {
+            QStringList value;
+            QString val;
+            val.reserve(data.size());
+            // XXX List serialization being a separate layer from low-level parsing is
+            // probably a bug. No affected entries are defined, though.
+            bool quoted = false;
+            for (int p = 0; p < data.length(); p++) {
+                if (quoted) {
+                    val += data[p];
+                    quoted = false;
+                } else if (data[p] == '\\') {
+                    quoted = true;
+                } else if (data[p] == ';') {
+                    value.append(val);
+                    val.clear();
+                    val.reserve(data.size() - p);
+                } else {
+                    val += data[p];
+                }
+            }
+            if (!val.isEmpty()) {
+                kWarning() << "List entry" << key << "in" << config()->name() << "is not compliant with XDG standard (missing trailing semicolon).";
+                value.append(val);
+            }
+            return value;
         }
     }
-    if (!val.isEmpty()) {
-        kWarning() << "List entry" << key << "in" << config()->name() << "is not compliant with XDG standard (missing trailing semicolon).";
-        value.append(val);
-    }
-    return value;
+
+    return aDefault;
 }
 
 QString KConfigGroup::readPathEntry(const QString& pKey, const QString & aDefault) const
@@ -868,14 +869,19 @@
 
 QString KConfigGroup::readPathEntry(const char *key, const QString & aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readPathEntry", "accessing an invalid group");
+    QString aValue;
 
-    bool expand = false;
+    if(isValid()) {
+        bool expand = false;
 
-    QString aValue = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized,
-                            &expand);
-    if (aValue.isNull())
+        QString aValue = config()->d_func()->lookupData(d->fullName(), key, KEntryMap::SearchLocalized,
+                                                        &expand);
+        if (aValue.isNull())
+            aValue = aDefault;
+    }
+    else {
         aValue = aDefault;
+    }
 
     return KConfigGroupPrivate::expandString(aValue);
 }
@@ -887,13 +893,13 @@
 
 QStringList KConfigGroup::readPathEntry(const char *key, const QStringList& aDefault) const
 {
-    Q_ASSERT_X(isValid(), "KConfigGroup::readPathEntry", "accessing an invalid group");
+    if(isValid()) {
+        const QString data = readPathEntry(key, QString());
+        if (!data.isNull())
+            return KConfigGroupPrivate::deserializeList(data);
+    }
 
-    const QString data = readPathEntry(key, QString());
-    if (data.isNull())
-        return aDefault;
-
-    return KConfigGroupPrivate::deserializeList(data);
+    return aDefault;
 }
 
 void KConfigGroup::writeEntry( const char* key, const QString& value, WriteConfigFlags flags )

--Boundary-00=_i7woJ3WD2hkFX8e--




More information about the kde-core-devel mailing list