Commit b3e422a1 authored by Aard's avatar Aard

Add proper recursive search and default sections for single variable lookup

Recursive search used to return the first match found, now return the last match
found. Additionally, fall back to an autodetected default section if required.
parent 4f7e4a76
......@@ -371,6 +371,11 @@ QStringList SsuDeviceInfo::repos(bool rnd, int filter){
}
QVariant SsuDeviceInfo::variable(QString section, const QString &key){
/// @todo compat-setting as ssudeviceinfo guaranteed to prepend sections with var-;
/// SsuVariables does not have this guarantee. Remove from here as well.
if (!section.startsWith("var-"))
section = "var-" + section;
return SsuVariables::variable(boardMappings, section, key);
}
......
......@@ -136,6 +136,7 @@ SsuSettings *SsuVariables::settings(){
return m_settings;
}
/// @todo add override capability with an override-section in ssu.ini
QVariant SsuVariables::variable(QString section, const QString &key){
if (m_settings != NULL)
return variable(m_settings, section, key);
......@@ -144,23 +145,19 @@ QVariant SsuVariables::variable(QString section, const QString &key){
}
QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QString &key){
if (!section.startsWith("var-"))
section = "var-" + section;
QVariant value;
if (settings->contains(section + "/" + key)){
return settings->value(section + "/" + key);
}
value = readVariable(settings, section, key, 0);
if (settings->contains(section + "/variables")){
QStringList sections = settings->value(section + "/variables").toStringList();
foreach(const QString &section, sections){
QVariant value = variable(settings, section, key);
if (value.type() != QMetaType::UnknownType)
return value;
}
// first check if the value is defined in the main section, and fall back
// to default sections
if (value.type() == QMetaType::UnknownType){
QString dSection = defaultSection(settings, section);
if (!dSection.isEmpty())
value = readVariable(settings, dSection, key, 0, false);
}
return QVariant();
return value;
}
void SsuVariables::variableSection(QString section, QHash<QString, QString> *storageHash){
......@@ -179,7 +176,6 @@ void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash
}
}
// resolve a configuration section, recursively following all 'variables' sections.
// variables which exist in more than one section will get overwritten when discovered
// again
......@@ -239,3 +235,41 @@ void SsuVariables::readSection(SsuSettings *settings, QString section,
}
settings->endGroup();
}
QVariant SsuVariables::readVariable(SsuSettings *settings, QString section, const QString &key,
int recursionDepth, bool logOverride){
QVariant value;
if (recursionDepth >= SSU_MAX_RECURSION){
SsuLog::instance()->print(LOG_WARNING,
QString("Maximum recursion depth for resolving %1 from %2::%3")
.arg(key)
.arg(settings->fileName())
.arg(section));
return value;
}
// variables directly in the section take precedence
if (settings->contains(section + "/" + key)){
return settings->value(section + "/" + key);
}
/// @todo add logging for overrides
if (settings->contains(section + "/variables")){
// child should log unless the parent is a default section
bool childLogOverride = true;
if (section.startsWith("default-") || section.startsWith("var-default-"))
childLogOverride = false;
QStringList sections = settings->value(section + "/variables").toStringList();
foreach(const QString &section, sections){
if (section.startsWith("var-"))
value = readVariable(settings, section, key, recursionDepth + 1, childLogOverride);
else
value = readVariable(settings, "var-" + section, key,
recursionDepth + 1, childLogOverride);
}
}
return value;
}
......@@ -46,7 +46,7 @@ class SsuVariables: public QObject {
* Return a variable from the given variable section. 'var'- is automatically
* prepended to the section name if not specified already. Recursive search
* through several variable sections (specified in the section) is supported,
* returned will be the first occurence of the variable.
* returned will be the last occurence of the variable.
*/
QVariant variable(QString section, const QString &key);
static QVariant variable(SsuSettings *settings, QString section, const QString &key);
......@@ -64,10 +64,15 @@ class SsuVariables: public QObject {
* the default section with the requested section.
*/
void variableSection(QString section, QHash<QString, QString> *storageHash);
static void variableSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash);
static void variableSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash);
private:
static void readSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash, int recursionDepth, bool logOverride=true);
static void readSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash, int recursionDepth,
bool logOverride=true);
static QVariant readVariable(SsuSettings *settings, QString section, const QString &key,
int recursionDepth, bool logOverride=true);
SsuSettings *m_settings;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment