/
ssuvariables.cpp
278 lines (229 loc) · 8.58 KB
1
2
3
4
5
6
7
8
/**
* @file ssuvariables.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QStringList>
9
10
#include <QRegExp>
#include <QStringRef>
11
12
#include "ssuvariables.h"
13
#include "ssulog.h"
14
15
16
#include "../constants.h"
17
18
19
20
SsuVariables::SsuVariables(): QObject() {
}
21
22
QString SsuVariables::defaultSection(SsuSettings *settings, QString section){
QStringList parts = section.split("-");
23
24
25
26
27
if (section.startsWith("var-"))
parts.insert(1, "default");
else
parts.replace(0, "default");
28
29
30
31
32
33
34
QString key = parts.join("-");
if (settings->childGroups().contains(key))
return key;
else
return "";
35
}
36
37
38
39
40
41
QString SsuVariables::resolveString(QString pattern, QHash<QString, QString> *variables, int recursionDepth){
if (recursionDepth >= SSU_MAX_RECURSION){
return "maximum-recursion-level-reached";
}
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
QRegExp regex("%\\\([^%]*\\\)", Qt::CaseSensitive, QRegExp::RegExp2);
regex.setMinimal(true);
int pos = 0;
while ((pos = regex.indexIn(pattern, pos)) != -1){
QString match = regex.cap(0);
if (match.contains(":")){
// variable is special, resolve before replacing
QString variable = resolveVariable(match, variables);
pattern.replace(match, variable);
pos += variable.length();
} else {
// look up variable name in hashmap, and replace it with stored value,
// if found, or ""
QString variableName = match;
variableName.remove(0,2);
variableName.chop(1);
if (variables->contains(variableName)){
pattern.replace(match, variables->value(variableName));
pos += variables->value(variableName).length();
} else
pattern.replace(match, "");
}
}
// check if string still contains variables, and recurse
if (regex.indexIn(pattern, 0) != -1)
70
pattern = resolveString(pattern, variables, recursionDepth + 1);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
return pattern;
}
QString SsuVariables::resolveVariable(QString variable, QHash<QString, QString> *variables){
QString variableValue = "";
if (variable.endsWith(")"))
variable.chop(1);
if (variable.startsWith("%("))
variable.remove(0,2);
// hunt for your colon
int magic = variable.indexOf(":");
// seems you misplaced your colon
if (magic == -1) return variable;
QStringRef variableName(&variable, 0, magic);
QStringRef variableSub(&variable, magic + 2, variable.length() - magic - 2);
// Fill in variable value for later tests, if it exists
if (variables->contains(variableName.toString()))
variableValue = variables->value(variableName.toString());
// find the operator who's after your colon
QChar op;
98
if (variable.length() > magic +1)
99
100
op = variable.at(magic + 1);
101
switch(op.toLatin1()){
102
103
104
105
106
107
108
109
110
111
case '-':
// substitute default value if variable is empty
if (variableValue == "")
return variableSub.toString();
break;
case '+':
// substitute default value if variable is not empty
if (variableValue != "")
return variableSub.toString();
break;
112
113
114
115
116
117
118
119
120
121
122
123
124
case '=': {
// %(%(foo):=bar?foobar|baz)
// if foo == bar then return foobar, else baz
QString sub = variableSub.toString();
QString a = sub.left(sub.indexOf("?"));
QString b = sub.right(sub.length() - sub.indexOf("?") - 1);
if (b.indexOf("|") == -1)
return b;
if (variableName == a)
return b.left(b.indexOf("|"));
else
return b.right(b.length() - b.indexOf("|") - 1);
}
125
126
127
128
129
}
// no proper substitution found -> return default value
return variableValue;
}
130
131
132
133
134
135
136
137
138
void SsuVariables::setSettings(SsuSettings *settings){
m_settings = settings;
}
SsuSettings *SsuVariables::settings(){
return m_settings;
}
139
/// @todo add override capability with an override-section in ssu.ini
140
141
142
143
144
145
146
147
QVariant SsuVariables::variable(QString section, const QString &key){
if (m_settings != NULL)
return variable(m_settings, section, key);
else
return QVariant();
}
QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QString &key){
148
QVariant value;
149
150
value = readVariable(settings, section, key, 0);
151
152
153
154
155
156
157
// 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);
158
159
}
160
return value;
161
162
163
164
165
166
167
168
}
void SsuVariables::variableSection(QString section, QHash<QString, QString> *storageHash){
if (m_settings != NULL)
variableSection(m_settings, section, storageHash);
}
void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash){
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
QString dSection = defaultSection(settings, section);
if (dSection.isEmpty())
readSection(settings, section, storageHash, 0);
else {
readSection(settings, dSection, storageHash, 0);
readSection(settings, section, storageHash, 0, false);
}
}
// resolve a configuration section, recursively following all 'variables' sections.
// variables which exist in more than one section will get overwritten when discovered
// again
// the section itself gets evaluated at the end, again having a chance to overwrite variables
void SsuVariables::readSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash, int recursionDepth,
bool logOverride){
if (recursionDepth >= SSU_MAX_RECURSION){
SsuLog::instance()->print(LOG_WARNING,
QString("Maximum recursion depth for resolving section %1 from %2")
.arg(section)
.arg(settings->fileName()));
return;
}
193
194
if (settings->contains(section + "/variables")){
195
196
197
198
199
// child should log unless the parent is a default section
bool childLogOverride = true;
if (section.startsWith("default-") || section.startsWith("var-default-"))
childLogOverride = false;
200
QStringList sections = settings->value(section + "/variables").toStringList();
201
202
203
204
205
206
207
foreach(const QString §ion, sections){
if (section.startsWith("var-"))
readSection(settings, section, storageHash, recursionDepth + 1, childLogOverride);
else
readSection(settings, "var-" + section, storageHash,
recursionDepth + 1, childLogOverride);
}
208
209
210
211
212
213
}
settings->beginGroup(section);
if (settings->group() != section)
return;
214
215
216
217
QStringList locals;
if (settings->contains("local"))
locals = settings->value("local").toStringList();
218
219
QStringList keys = settings->allKeys();
foreach (const QString &key, keys){
220
221
222
223
224
225
226
// local variable
if (key.startsWith("_"))
continue;
if (locals.contains(key))
continue;
227
228
229
if (key == "variables" || key == "local")
continue;
230
231
232
233
234
235
236
if (storageHash->contains(key) && logOverride){
SsuLog::instance()->print(LOG_DEBUG,
QString("Variable %1 overwritten from %2::%3")
.arg(key)
.arg(settings->fileName())
.arg(section));
}
237
238
239
240
storageHash->insert(key, settings->value(key).toString());
}
settings->endGroup();
}
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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 §ion, 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;
}