Skip to content

Commit f00e40d

Browse files
committed
LP-419 config: merge factory defaults at startup
a simple registry is used to track all configurations ever seen only new configurations (i.e. not in the registry) are added merging only applies to plugin and gadget configurations for now
1 parent b52424b commit f00e40d

2 files changed

Lines changed: 58 additions & 25 deletions

File tree

ground/gcs/src/app/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ int runApplication(int argc, char * *argv)
416416
qDebug() << "main - resetting user settings";
417417
Utils::resetToFactoryDefaults(settings);
418418
}
419+
Utils::mergeFactoryDefaults(settings);
419420

420421
// override settings with command line provided values
421422
// take notice that the overridden values will be saved in the user settings and will continue to be effective

ground/gcs/src/libs/utils/settingsutils.cpp

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,31 @@ const QLatin1String CONFIG_OPTION("-D");
4343
// list of read only QSettings objects containing factory defaults
4444
QList<QSettings const *> factorySettingsList;
4545

46-
// helper class to manage a simple registry
46+
/*
47+
* Helper class to manage a simple registry
48+
* Each entry in the registry is a configuration path
49+
* For example : UAVGadgetConfigurations/DialGadget/Attitude
50+
* Note that entries are base64 encoded so QSettings does not mess with them
51+
*/
4752
class QTCREATOR_UTILS_EXPORT Registry {
4853
public:
49-
Registry(QSettings &settings) : settings(settings)
54+
Registry()
5055
{
51-
settings.beginGroup("registry");
56+
QSettings settings;
57+
58+
settings.beginGroup("Registry");
5259
registry = settings.childKeys();
5360
settings.endGroup();
54-
// qDebug() << "read registry: " << settings.group() << registry;
5561
}
5662

5763
void save() const
5864
{
59-
// qDebug() << "saving registry: " << settings.group() << registry;
60-
settings.beginGroup("registry");
65+
QSettings settings;
66+
67+
settings.beginGroup("Registry");
6168
settings.remove("");
6269
foreach(QString entry, registry) {
63-
settings.setValue(key(entry), 1);
70+
settings.setValue(entry, 1);
6471
}
6572
settings.endGroup();
6673
}
@@ -76,12 +83,11 @@ class QTCREATOR_UTILS_EXPORT Registry {
7683
}
7784

7885
private:
79-
QSettings &settings;
8086
QStringList registry;
8187

8288
QString key(QString &entry) const
8389
{
84-
return entry;
90+
return entry.toUtf8().toBase64();
8591
}
8692
};
8793

@@ -114,41 +120,56 @@ void copySettings(const QSettings &from, QSettings &to)
114120
{
115121
foreach(QString key, from.allKeys()) {
116122
if (!to.contains(key)) {
117-
// qDebug() << "+++" << key << from.value(key);
123+
// qDebug() << "++" << key << from.value(key);
118124
to.setValue(key, from.value(key));
119125
} else if (from.value(key) != to.value(key)) {
120-
// qDebug() << ">>>" << key << from.value(key) << to.value(key);
126+
// qDebug() << ">>" << key << from.value(key) << to.value(key);
121127
to.setValue(key, from.value(key));
122128
}
123129
}
124130
}
125131

126-
void mergeSettings(const QSettings &from, QSettings &to)
132+
void mergeSettings(Registry &registry, const QSettings &from, QSettings &to)
127133
{
128-
const_cast<QSettings &>(from).beginGroup(to.group());
129-
130-
// registry of all groups ever seen
131-
// used to avoid re-adding a group that was deleted by the user
132-
Registry registry(to);
134+
to.beginGroup(from.group());
133135

134136
// iterate over factory defaults groups
137+
// note that merging could be done in smarter way
138+
// currently we do a "all or nothing" merge but we could do something more granular
139+
// this, would allow, new configuration options to be added to existing configurations
135140
foreach(QString group, from.childGroups()) {
136141
const_cast<QSettings &>(from).beginGroup(group);
137142
to.beginGroup(group);
138-
if (!registry.contains(group)) {
143+
QString id = from.group();
144+
if (!registry.contains(id)) {
139145
// registry keeps growing...
140-
registry.add(group);
141-
if (to.allKeys().count() <= 0) {
146+
registry.add(id);
147+
// copy settings only if destination group is totally empty (see comment above)
148+
if (to.allKeys().count() <= 0 && to.childGroups().count() <= 0) {
142149
// found new group, copy it to the destination
150+
qDebug() << "settings - adding new configuration" << id;
143151
copySettings(from, to);
144152
}
145153
}
146154
to.endGroup();
147155
const_cast<QSettings &>(from).endGroup();
148156
}
149157

150-
registry.save();
158+
to.endGroup();
159+
}
151160

161+
void mergeFactorySettings(Registry &registry, const QSettings &from, QSettings &to)
162+
{
163+
const_cast<QSettings &>(from).beginGroup("Plugins");
164+
mergeSettings(registry, from, to);
165+
const_cast<QSettings &>(from).endGroup();
166+
167+
const_cast<QSettings &>(from).beginGroup("UAVGadgetConfigurations");
168+
foreach(QString childGroup, from.childGroups()) {
169+
const_cast<QSettings &>(from).beginGroup(childGroup);
170+
mergeSettings(registry, from, to);
171+
const_cast<QSettings &>(from).endGroup();
172+
}
152173
const_cast<QSettings &>(from).endGroup();
153174
}
154175

@@ -163,7 +184,7 @@ void initSettings(const QString &factoryDefaultsFileName)
163184

164185
if (fileName.isEmpty()) {
165186
// check default file
166-
qDebug() << "Looking for factory defaults configuration files in" << directory.absolutePath();
187+
qDebug() << "settings - looking for factory defaults configuration files in" << directory.absolutePath();
167188
fileName = checkFile(directory.absoluteFilePath(DEFAULT_CONFIG_FILENAME));
168189
}
169190

@@ -182,7 +203,7 @@ void initSettings(const QString &factoryDefaultsFileName)
182203
file = checkFile(file);
183204

184205
QSettings const *settings = new QSettings(file, XmlConfig::XmlFormat);
185-
qDebug() << "Loaded factory defaults" << file;
206+
qDebug() << "settings - loaded factory defaults" << file;
186207

187208
factorySettingsList.append(settings);
188209
}
@@ -198,7 +219,7 @@ void overrideSettings(QSettings &settings, int argc, char * *argv)
198219
if (rx.indexIn(argv[++i]) > -1) {
199220
QString key = rx.cap(1);
200221
QString value = rx.cap(2);
201-
qDebug() << "User setting" << key << "set to value" << value;
222+
qDebug() << "settings - user setting" << key << "set to value" << value;
202223
settings.setValue(key, value);
203224
}
204225
}
@@ -213,10 +234,21 @@ void resetToFactoryDefaults(QSettings &toSettings)
213234
);
214235
}
215236

237+
/**
238+
* Merge factory defaults into user settings.
239+
* Currently only Plugins and UAVGadgetConfigurations are merged
240+
*/
216241
void mergeFactoryDefaults(QSettings &toSettings)
217242
{
243+
// registry of all configuration groups ever seen
244+
// used to avoid re-adding a group that was deleted by the user
245+
Registry registry;
246+
247+
// merge all loaded factory defaults
218248
applyToFactorySettings(
219-
[&](const QSettings &fromSettings) { mergeSettings(fromSettings, toSettings); }
249+
[&](const QSettings &fromSettings) { mergeFactorySettings(registry, fromSettings, toSettings); }
220250
);
251+
252+
registry.save();
221253
}
222254
}

0 commit comments

Comments
 (0)