[PATCH]KConfigGroup::groupList() kind of broken

Andreas Pakulat apaku at gmx.de
Mon Nov 19 21:12:25 GMT 2007


it seems that KConfigGroup::groupList() doesn't work really well with
subgroups. I'd expect the function to return a list of group names,
which I could use in the KConfigGroup constructor, but it returns the
full name of the group, including any parent groups. Makes the whole
thing kind of useless.

To clarify, for 
| [Foo]
| [Foo/Bar]

and the code

| KConfigGroup grp(kconfig, "Foo");
| kDebug() << grp.groupList();

I get "Foo/Bar" instead of just "Bar" as I'd expect it.

I also noticed during writing the testcase that groupList would return
"Bar/Baz" if Bar has a subgroup called "Baz", I can't imagine how that
is useful, unless groupList() should be recursive and also return all
other subgroups.

The attached patch fixes that by removing the parent groups full name
from the returned string and removing anything after the first slash
from the resulting string. Of course this assumes that a groupname
cannot contain a forward slash. If thats not true than either a parser
for the groupname is needed or some change of how KConfigGroup keeps
track of its subgroups.


Cold hands, no gloves.
-------------- next part --------------
Index: ../../../../kdelibs/kdecore/tests/kconfigtest.cpp
--- ../../../../kdelibs/kdecore/tests/kconfigtest.cpp	(Revision 738799)
+++ ../../../../kdelibs/kdecore/tests/kconfigtest.cpp	(Arbeitskopie)
@@ -65,6 +65,7 @@
 #define HOMEPATH QDir::homePath()+"/foo"
 #define HOMEPATHESCAPE QDir::homePath()+"/foo/$HOME"
+#define SUBGROUPLIST (QStringList() << "SubGroup" << "SubGroup1" << "SubGroup2")
 void KConfigTest::initTestCase()
@@ -702,6 +703,7 @@
     KConfig sc( "kconfigtest" );
     KConfigGroup cg( &sc, "ParentGroup" );
     QVERIFY(cg.readEntry( "parentgrpstring", "") == QString("somevalue") );
+    QVERIFY(cg.groupList() == SUBGROUPLIST );
     KConfigGroup subcg1( &cg, "SubGroup1");
     QVERIFY(subcg1.readEntry( "somestring", "") == QString("somevalue") );
     KConfigGroup subcg2( &cg, "SubGroup2");
Index: ../../../../kdelibs/kdecore/config/kconfig.cpp
--- ../../../../kdelibs/kdecore/config/kconfig.cpp	(Revision 738799)
+++ ../../../../kdelibs/kdecore/config/kconfig.cpp	(Arbeitskopie)
@@ -171,7 +171,10 @@
     foreach (const KEntryKey& key, entryMap.keys())
         if (key.mKey.isNull() && key.mGroup.startsWith(group) && key.mGroup != group)
-            groups << QString::fromUtf8(key.mGroup);
+	{
+            QString groupname = QString::fromUtf8(key.mGroup.mid(group.length()+1));
+            groups << groupname.mid(0, groupname.indexOf("/"));
+	}
     return groups;

More information about the kde-core-devel mailing list