Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ static QString GetLangTerritory()
if(!lang_territory_qsettings.isEmpty())
lang_territory = lang_territory_qsettings;
// 3) -lang command line argument
lang_territory = QString::fromStdString(gArgs.GetArg("-lang", lang_territory.toStdString()));
try {
lang_territory = QString::fromStdString(gArgs.GetArg("-lang", lang_territory.toStdString()));
} catch (const std::exception&) {
// Keep the language selected above if "lang" in settings.json has an
// unexpected type. OptionsModel::Init reports it as an error.
}
return lang_territory;
}

Expand Down
64 changes: 46 additions & 18 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,20 @@ void OptionsModel::addOverriddenOption(const std::string &option)
// Writes all missing QSettings with their default values
bool OptionsModel::Init(bilingual_str& error)
{
// Handles exceptions thrown by univalue that can happen if settings in
// settings.json don't have the expected types.
const auto setting_error = [&](const std::string& setting, const std::exception& e) {
error.original = strprintf("Could not read setting \"%s\", %s.", setting, e.what());
error.translated = tr("Could not read setting \"%1\", %2.").arg(QString::fromStdString(setting), e.what()).toStdString();
return false;
};

// Initialize display settings from stored settings.
language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), ""));
try {
language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), ""));
} catch (const std::exception& e) {
return setting_error("lang", e);
}

checkAndMigrate();

Expand Down Expand Up @@ -256,17 +268,25 @@ bool OptionsModel::Init(bilingual_str& error)
addOverriddenOption("-font-family");
}
if (GUIUtil::fontsLoaded()) {
const QString font_name = QString::fromStdString(
SettingToString(node().getPersistentSetting("font-family"), ""));
GUIUtil::setActiveFont(font_name);
try {
const QString font_name = QString::fromStdString(
SettingToString(node().getPersistentSetting("font-family"), ""));
GUIUtil::setActiveFont(font_name);
} catch (const std::exception& e) {
return setting_error("font-family", e);
}
}

// Font Scale
if (node().isSettingIgnored("font-scale")) {
addOverriddenOption("-font-scale");
}
if (GUIUtil::fontsLoaded()) {
GUIUtil::setFontScale(SettingToInt(node().getPersistentSetting("font-scale"), GUIUtil::defaultFontScale()));
try {
GUIUtil::setFontScale(SettingToInt(node().getPersistentSetting("font-scale"), GUIUtil::defaultFontScale()));
} catch (const std::exception& e) {
return setting_error("font-scale", e);
}
}

// Font Weight (Normal)
Expand All @@ -276,11 +296,17 @@ bool OptionsModel::Init(bilingual_str& error)

const bool override_family{isOptionOverridden("-font-family")};
if (GUIUtil::fontsLoaded()) {
// If font was overridden by CLI but weight wasn't, use the font's default weight
const int default_arg = GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Normal);
const int arg = (!override_family || isOptionOverridden("-font-weight-normal"))
? SettingToInt(node().getPersistentSetting("font-weight-normal"), default_arg)
: default_arg;
// Read the stored value even when it is not used below, so that a
// malformed one is reported here rather than thrown later via gArgs.
int setting_arg;
try {
setting_arg = SettingToInt(node().getPersistentSetting("font-weight-normal"), default_arg);
} catch (const std::exception& e) {
return setting_error("font-weight-normal", e);
}
// If font was overridden by CLI but weight wasn't, use the font's default weight
const int arg = (!override_family || isOptionOverridden("-font-weight-normal")) ? setting_arg : default_arg;
if (!GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, arg)) {
node().forceSetting("font-weight-normal", default_arg);
GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, default_arg);
Expand All @@ -292,11 +318,17 @@ bool OptionsModel::Init(bilingual_str& error)
addOverriddenOption("-font-weight-bold");
}
if (GUIUtil::fontsLoaded()) {
// If font was overridden by CLI but weight wasn't, use the font's default weight
const int default_arg = GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Bold);
const int arg = (!override_family || isOptionOverridden("-font-weight-bold"))
? SettingToInt(node().getPersistentSetting("font-weight-bold"), default_arg)
: default_arg;
// Read the stored value even when it is not used below, so that a
// malformed one is reported here rather than thrown later via gArgs.
int setting_arg;
try {
setting_arg = SettingToInt(node().getPersistentSetting("font-weight-bold"), default_arg);
} catch (const std::exception& e) {
return setting_error("font-weight-bold", e);
}
// If font was overridden by CLI but weight wasn't, use the font's default weight
const int arg = (!override_family || isOptionOverridden("-font-weight-bold")) ? setting_arg : default_arg;
if (!GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, arg)) {
node().forceSetting("font-weight-bold", default_arg);
GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, default_arg);
Expand Down Expand Up @@ -361,11 +393,7 @@ bool OptionsModel::Init(bilingual_str& error)
try {
getOption(option);
} catch (const std::exception& e) {
// This handles exceptions thrown by univalue that can happen if
// settings in settings.json don't have the expected types.
error.original = strprintf("Could not read setting \"%s\", %s.", setting, e.what());
error.translated = tr("Could not read setting \"%1\", %2.").arg(QString::fromStdString(setting), e.what()).toStdString();
return false;
return setting_error(setting, e);
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/qt/test/optiontests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,61 @@ void OptionTests::integerGetArgBug()
gArgs.WriteSettingsFile();
}

void OptionTests::invalidFontSetting()
{
if (QApplication::platformName() == "minimal") {
QSKIP("AppTests cannot initialize fonts with the 'minimal' platform plugin.");
}

// Font settings are only read once loadFonts() has run, which AppTests does.
QVERIFY2(GUIUtil::fontsLoaded(),
"GUIUtil::loadFonts() must succeed in AppTests::appTests() before OptionTests run.");

for (const std::string setting : {"font-family", "font-scale", "font-weight-normal", "font-weight-bold"}) {
gArgs.LockSettings([&](util::Settings& settings) {
settings.rw_settings[setting] = UniValue{UniValue::VARR};
});
bilingual_str error;
QVERIFY(!OptionsModel{m_node}.Init(error));
QCOMPARE(QString::fromStdString(error.original),
QString::fromStdString(strprintf("Could not read setting \"%s\", JSON value of type array is not of expected type string.", setting)));
gArgs.LockSettings([&](util::Settings& settings) {
settings.rw_settings.erase(setting);
});
}

// With -font-family on the command line the stored weights are not used, but
// dash-qt still reads them through gArgs later, so they must be reported too.
for (const std::string setting : {"font-weight-normal", "font-weight-bold"}) {
gArgs.LockSettings([&](util::Settings& settings) {
settings.command_line_options["font-family"] = {UniValue{GUIUtil::defaultFontFamily().toStdString()}};
settings.rw_settings[setting] = UniValue{UniValue::VARR};
});
bilingual_str error;
QVERIFY(!OptionsModel{m_node}.Init(error));
QCOMPARE(QString::fromStdString(error.original),
QString::fromStdString(strprintf("Could not read setting \"%s\", JSON value of type array is not of expected type string.", setting)));
gArgs.LockSettings([&](util::Settings& settings) {
settings.command_line_options.erase("font-family");
settings.rw_settings.erase(setting);
});
}
}

void OptionTests::invalidLangSetting()
{
// A "lang" value with an unexpected type in settings.json must be reported
// through the error string like other settings instead of throwing out of
// OptionsModel::Init.
gArgs.LockSettings([&](util::Settings& settings) {
settings.rw_settings["lang"] = UniValue{UniValue::VOBJ};
});
bilingual_str error;
QVERIFY(!OptionsModel{m_node}.Init(error));
QCOMPARE(QString::fromStdString(error.original),
QString{"Could not read setting \"lang\", JSON value of type object is not of expected type string."});
}

void OptionTests::parametersInteraction()
{
// Test that the bug https://github.com/bitcoin-core/gui/issues/567 does not resurface.
Expand Down
2 changes: 2 additions & 0 deletions src/qt/test/optiontests.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ private Q_SLOTS:
void init(); // called before each test function execution.
void migrateSettings();
void integerGetArgBug();
void invalidFontSetting();
void invalidLangSetting();
void parametersInteraction();
void extractFilter();
void effectivePointSize();
Expand Down
Loading