PDA

View Full Version : QSettings , read only avaiable?



patrik08
18th November 2007, 10:12
I try to find the path from GPL Ghostscript ..
I find the path ... and is installed 8.60.
Now how i can make QSettings read only...
to not make 39 new Regedit empty path?





/* find gpl GhostScript path or exe */
QString PS_utils::getGSDefaultExeName()
{
QString gsName;
#if defined Q_WS_WIN
QFileInfo Pinfo;
// Try to locate GhostScript thanks to the qsetting
gsName = "gswin32c.exe";
for(int i=0;i<99;i++) {
/* loop all avaiable version on 8+ */
QSettings gs_sett("HKEY_LOCAL_MACHINE\\Software\\GPL Ghostscript\\8."+QString("%1").arg(100 - i),QSettings::NativeFormat);
if (gs_sett.allKeys().size() > 2) {
if (gs_sett.value("GS_DLL").toString().size() > 4) {
Pinfo.setFile(gs_sett.value("GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
}
}
for(int e=0;e<99;e++) {
/* loop all avaiable version on 7+ */
QSettings gs_sett7("HKEY_LOCAL_MACHINE\\Software\\GPL Ghostscript\\7."+QString("%1").arg(100 - e),QSettings::NativeFormat);
if (gs_sett7.allKeys().size() > 2) {
if (gs_sett7.value("GS_DLL").toString().size() > 4) {
Pinfo.setFile(gs_sett7.value("GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
}
}
/* win not having GPL Ghostscript ! */
gsName = "gs";
#else
gsName = "gs";
#endif
return gsName;
}

jpn
18th November 2007, 10:42
You might want to construct only one QSettings object instead of constructing hundreds of them in those loops. ;) Start iterating from "HKEY_LOCAL_MACHINE\\Software" and go deeper in the hierarchy only if childGroups() contains "GPL Ghostscript" etc.

patrik08
18th November 2007, 12:45
You might want to construct only one QSettings object instead of constructing hundreds of them in those loops. ;) Start iterating from "HKEY_LOCAL_MACHINE\\Software" and go deeper in the hierarchy only if childGroups() contains "GPL Ghostscript" etc.


This work on GPL Ghostscript ..
if (AFPL Ghostscript) i suppose can find...

Tanks for idea...





/* find gpl GhostScript path or exe */
QString PS_utils::getGSDefaultExeName()
{
QString gsName;
QString gVersion;
#if defined Q_WS_WIN
QFileInfo Pinfo;
// Try to locate GhostScript thanks to the qsetting
gsName = "gswin32c.exe";
QSettings softs("HKEY_LOCAL_MACHINE\\Software",QSettings::NativeFormat);
QStringList allsoftware = softs.childGroups();
for (int i = 0; i < allsoftware.size(); ++i) {
const QString RealName = allsoftware.at(i); /* realpath */
if (RealName.contains("Ghostscript")) {
qDebug() << "### soft " << RealName;
for(int e=1;e<99;e++) {
/* check version 8 ++ 99 down */
gVersion = QString("8.%1").arg(100 - e);
if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6 ) {
Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
/* check version 7 ++ 99 down */
gVersion = QString("7.%1").arg(100 - e);
if (softs.value(RealName+"/"+gVersion+"/GS_DLL").toString().size() > 6 ) {
Pinfo.setFile(softs.value(RealName+"/"+gVersion+"/GS_DLL").toString());
return gsName.prepend(Pinfo.absolutePath()+"/");
}
}
}
}

/* win not having GPL Ghostscript ! */
gsName = "rundll32.exe url.dll,FileProtocolHandler http://pages.cs.wisc.edu/~ghost/";
#else
gsName = "gs";
#endif
return gsName;
}

jpn
18th November 2007, 13:55
Those loops still bother me so here's an improvement idea:


QStringList allsoftware = softs.childGroups();
// pick matching strings
QStringList gs = allsoftware.filter(QRegExp("Ghostscript \\d+\\.\\d+"));
// sort by version
qSort(gs.begin(), gs.end(), gsVersionGreaterThan);
// now the first item of gs is "Ghostscript M.N" with latest version (unless gs is empty, of course)

The compare function would look more or less something like this:


bool gsVersionGreaterThan(const QString& s1, const QString& s2)
{
QRegExp regExp1("Ghostscript (\\d+)\\.(\\d+)");
QRegExp regExp2("Ghostscript (\\d+)\\.(\\d+)");
int pos1 = regExp1.indexIn(s1);
int pos2 = regExp2.indexIn(s2);
if (pos1 > -1 && pos2 > -1)
{
int maj1 = regExp1.cap(1).toInt();
int maj2 = regExp2.cap(1).toInt();
int min1 = regExp1.cap(2).toInt();
int min2 = regExp2.cap(2).toInt();
return maj1 > maj2 || (maj1 == maj2 && min1 > min2);
}
return false;
}

patrik08
18th November 2007, 14:23
Those loops still bother me so here's an improvement idea:


QStringList allsoftware = softs.childGroups();
// pick matching strings
QStringList gs = allsoftware.filter(QRegExp("Ghostscript \\d+\\.\\d+"));
// sort by version
qSort(gs.begin(), gs.end(), gsVersionGreaterThan);
// now the first item of gs is "Ghostscript M.N" with latest version (unless gs is empty, of course)


You forget the separator //\\

version xx is other childGroups().

avaiable is:
GPL Ghostscript/8.60/
GPL Ghostscript/8.53/
GPL Ghostscript/7.07/
AFPL Ghostscript/8.54/
AFPL Ghostscript/8.50/

GPL Ghostscript / * version 10 time
AFPL Ghostscript / * version 12 time

QRegExp("Ghostscript") find to AFPL Ghostscript and return a small list..




QStringList gsonly = allsoftware.filter(QRegExp("Ghostscript"));
qDebug() << "### gsonly " << gsonly;
/* return ### gsonly ("GPL Ghostscript") */




QStringList gsonly = allsoftware.filter(QRegExp("Ghostscript \\d+\\.\\d+"));
qDebug() << "### gsonly " << gsonly;
/* return ### gsonly ("") not found */

jpn
18th November 2007, 15:21
Well, like I said, it was an improvement idea. I didn't even mean to give a 100% ready solution.

But alright, this changes the situation a bit. It might be easiest to check whether such application exists, go into the child group and pick version numbers:


QStringList allsoftware = settings.childGroups();
if (allsoftware.contains("GPL Ghostscript"))
{
settings.beginGroup("GPL Ghostscript");
QStringList versions = settings.childGroups();
qSort(versions.begin(), versions.end(), versionGreaterThan);
settings.endGroup();
...
}

This also simplifies the compare function a bit.