Improve load data behavior of SettingsStorage class

Previously it only handle the case of failed lookup, now it discard
invalid values when deserializing the database from disk.
Also checks whether the data is convertible to the intended type.
This commit is contained in:
Chocobo1 2021-01-01 19:31:27 +08:00
parent cfb55d9d77
commit b1020c599f
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
2 changed files with 12 additions and 4 deletions

View File

@ -295,7 +295,11 @@ QString TransactionalSettings::deserialize(const QString &name, QVariantHash &da
// or that we don't touch directly in this code (eg disabled by ifdef). This ensures
// that they will be copied over when save our settings to disk.
for (const QString &key : asConst(settings->allKeys()))
data.insert(key, settings->value(key));
{
const QVariant value = settings->value(key);
if (value.isValid())
data[key] = value;
}
return settings->fileName();
}

View File

@ -48,12 +48,16 @@ public:
T get(const T &defaultValue = {}) const
{
if constexpr (std::is_enum_v<T>) {
if constexpr (std::is_enum_v<T>)
{
const auto value = SettingsStorage::instance()->loadValue(m_keyName, {}).toString();
return Utils::String::toEnum(value, defaultValue);
}
else {
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue).template value<T>();
else
{
const QVariant value = SettingsStorage::instance()->loadValue(m_keyName);
// check if retrieved value is convertible to T
return value.template canConvert<T>() ? value.template value<T>() : defaultValue;
}
}