This forum has been archived. All content is frozen. Please use KDE Discuss instead.

KConfigGroup is putting subgroups out in wrong order

Tags: None
(comma "," separated)
RyanMcCoskrie
Registered Member
Posts
39
Karma
0
OS
I'm currently trying to (among other things) writing a "Modular* Development Environment" for KDE4

I currently have a very odd problem though.
This is the file listing source in my MDE:

[files][main.cpp]
lang=C++
name=Main File
url=/home/ryan/mde/kde4/main.cpp

[files][mfile.h]
lang=C++
name=MFile Description
url=/home/ryan/mde/kde4/mfile.h

[files][mfile.cpp]
lang=C++
name=MFile implementation
url=/home/ryan/mde/kde4/mfile.cpp

[files][mproj.h]
lang=C++
name=MProj Description
url=/home/ryan/mde/kde4/mproj.h

[files][mproj.cpp]
lang=C++
name=MProj implementation
url=/home/ryan/mde/kde4/mproj.cpp

[files][mwin.h]
lang=C++
name=MWindow Description
url=/home/ryan/mde/kde4/mwin.h

[files][mwin.cpp]
lang=C++
name=MWindow Implementation
url=/home/ryan/mde/kde4/mwin.cpp


Problem is though is that this code (trimed a tad, all under the GPL) for
reading through the descriptins of the files is getting it in the wrong order.
int i;
KConfigGroup *desc; //description of the file
for(i = 0; i url( desc->readEntry("url", QString()) );
_files[i]->name( desc->readEntry("name", QString()) );
_files[i]->lang( desc->readEntry("lang", QString()) );

delete desc;
}


Could someone please explain what is happening?

IF you have any critiques that would be good also but remember that this
is _very_ pre-alpha code.

*Think IDE-like use once your project is configured..


RyanMcCoskrie, proud to be a member of KDE forums since 2008-Dec.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Assuming it is just the read order it is having problems with, then I would recommend adding a new Entry that controls the order ( tied to an internal index )

If it is reading the URL from mwin.cpp then the name from file.h then it is probably related to the use of arrays. ( It would be best to use QList or QMap which handle things like this very well )


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
RyanMcCoskrie
Registered Member
Posts
39
Karma
0
OS

RE:Cannot get all KConfigGroups

Sun Apr 19, 2009 1:06 am
Okay I have reworked the code, procrastinated, worked on other projects, come back, gave everyone some air and looked into what is going on.
There is something deeply wrong at work here but I can't put my finger on the underlying issue.
Essentially only the first part of this config file can be read.
[fileList]
list=main.cpp, mfile.h, mfile.cpp, mproj.h, mproj.cpp, mwin.h, mwin.cpp

[main.cpp]
lang=C++
name=Main File
url=/home/ryan/mde/kde4/main.cpp

[mfile.h]
lang=C++
name=MFile Description
url=/home/ryan/mde/kde4/mfile.h

[mfile.cpp]
lang=C++
name=MFile implementation
url=/home/ryan/mde/kde4/mfile.cpp

(And so on)


I put together a script that reads through it to try to isolate the problem. Once again I'm leaving out non-essentials.
config = KConfig("/home/ryan/mde/kde4/.filerc")
fListconfig = KConfigGroup(config, "fileList")

fList = fListconfig.readEntry("list", [])

for i in fList:
print "Config name: ", i
desc = KConfigGroup(config, i)
print "File URL:", desc.readEntry("url", i)



and here is the output
Config name: main.cpp
File URL: /home/ryan/mde/kde4/main.cpp
Config name: mfile.h
File URL: mfile.h
Config name: mfile.cpp
File URL: mfile.cpp


Can somebody see why the first file description is working?


RyanMcCoskrie, proud to be a member of KDE forums since 2008-Dec.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Unfortunately I do not know why it is failing....
Try this C++ code though:
Code: Select all
KConfig projectFile("/path/to/project/file");
KConfigGroup projectData = projectFile.group("fileList");
QStringList fileList = projectData.readEntry("list").split(',');

foreach( QString file, fileList )
{
    file = file.trim();
    kDebug() << "Config Name: " + file;
    KConfigGroup fileData = projectFile.group(file);
    kDebug() << "File Name: " + file.readEntry("url");
}

Last edited by bcooksley on Sun Apr 19, 2009 8:14 am, edited 1 time in total.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS
Question: why does the order matter in which you load the file metadata?

Remarks:
- when reading a list, use the respective readEntry method, way better than reading a string and splitting yourself

- when doing foreach on a QStringList which you don't want to alter, use "const QString&" as the type for the variable, otherwise you'll needlessly create a copy of the list (it detaches the shared private)

- think about using KUrl as the type for the URL

(roughly based on the original code)
Code: Select all
const QStringList groups = fileData.groupList();
foreach(const QString & groupName, groups) {
  MFile* file = new MFile();
  const KConfigGroup fileConfig = fileData.group(groupName);

  file->url( fileConfig.readEntry("url", KUrl());
  file->name( fileConfig.readEntry("name", QString()) );
  file->lang( fileConfig.readEntry("lang", QString()) );
 
 _files.append(file);
 }


Readability-wise MFile::setUrl() (and friends) might be better, i.e. more consistent with Qt's and KDE's use of lowercase methods for getters

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]