Commit eb8f88a3 authored by Tobias Hunger's avatar Tobias Hunger

SettingsAccessor: Rewrite bestSettings

Change-Id: Ic0e7dfdd7df201f5050e3631011f52f64f7876c5
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent d2ad6aa9
......@@ -589,6 +589,38 @@ QVariantMap SettingsAccessor::prepareSettings(const QVariantMap &data) const
return data;
}
/**
* @brief Check which of two sets of data are a better match to load.
*
* This method is used to compare data extracted from two XML settings files.
* It will never be called with a version too old or too new to be read by
* the current instance of Qt Creator.
*
* @param origData The basis for the compare.
* @param newData The new value to compare against the basis.
* @return True if newData is a better match than origData and false otherwise.
*/
bool SettingsAccessor::isBetterMatch(const QVariantMap &origData, const QVariantMap &newData) const
{
if (origData.isEmpty())
return true;
int origVersion = versionFromMap(origData);
QByteArray origEnv = environmentIdFromMap(origData);
int newVersion = versionFromMap(newData);
QByteArray newEnv = environmentIdFromMap(newData);
if (origEnv != newEnv) {
if (origEnv == creatorId())
return false;
if (newEnv == creatorId())
return true;
}
return newVersion > origVersion;
}
/**
* @brief Upgrade the settings to a target version
* @param data The settings to upgrade
......@@ -912,46 +944,20 @@ QVariantMap SettingsAccessor::readSharedSettings(QWidget *parent) const
SettingsAccessorPrivate::Settings SettingsAccessorPrivate::bestSettings(const SettingsAccessor *accessor,
const QList<Utils::FileName> &pathList)
{
Settings newestNonMatching;
Settings newestMatching;
Settings tmp;
foreach (const Utils::FileName &file, pathList) {
tmp.path = file;
tmp.map = accessor->readFile(tmp.path);
if (tmp.map.isEmpty())
continue;
Settings bestMatch;
foreach (const Utils::FileName &path, pathList) {
QVariantMap tmp = accessor->readFile(path);
const int tmpVersion = SettingsAccessor::versionFromMap(tmp.map);
if (tmpVersion > accessor->currentVersion()) {
qWarning() << "Skipping settings file" << tmp.path.toUserOutput() << "(too new).";
continue;
}
if (tmpVersion < accessor->firstSupportedVersion()) {
qWarning() << "Skipping settings file" << tmp.path.toUserOutput() << "(too old).";
int version = SettingsAccessor::versionFromMap(tmp);
if (version < firstVersion() || version > currentVersion())
continue;
}
const QByteArray tmpEnvironmentId = SettingsAccessor::environmentIdFromMap(tmp.map);
if (tmpEnvironmentId.isEmpty() || tmpEnvironmentId == SettingsAccessor::creatorId()) {
if (tmpVersion > SettingsAccessor::versionFromMap(newestMatching.map))
newestMatching = tmp;
} else {
if (tmpVersion > SettingsAccessor::versionFromMap(newestNonMatching.map))
newestNonMatching = tmp;
if (accessor->isBetterMatch(bestMatch.map, tmp)) {
bestMatch.path = path;
bestMatch.map = tmp;
}
if (SettingsAccessor::versionFromMap(newestMatching.map) == accessor->currentVersion())
break;
}
Settings result;
if (newestMatching.isValid())
result = newestMatching;
else if (newestNonMatching.isValid())
result = newestNonMatching;
return result;
return bestMatch;
}
QVariantMap SettingsAccessor::mergeSettings(const QVariantMap &userMap,
......
......@@ -70,6 +70,8 @@ protected:
QVariantMap upgradeSettings(const QVariantMap &data, int toVersion) const;
virtual QVariantMap prepareSettings(const QVariantMap &data) const;
virtual bool isBetterMatch(const QVariantMap &origData, const QVariantMap &newData) const;
private:
QList<Utils::FileName> findSettingsFiles(const QString &suffix) const;
static QByteArray creatorId();
......
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