Skip to content

Commit ebfd619

Browse files
committed
gui: Prompt to reset settings when settings.json cannot be read
Fixes bitcoin/bitcoin#21340
1 parent df2b5da commit ebfd619

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

src/qt/bitcoin.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,53 @@ static void initTranslations(QTranslator &qtTranslatorBase, QTranslator &qtTrans
144144
QApplication::installTranslator(&translator);
145145
}
146146

147+
static std::string JoinErrors(const std::vector<std::string>& errors)
148+
{
149+
return Join(errors, "\n", [](const std::string& error) { return "- " + error; });
150+
}
151+
152+
static bool InitSettings()
153+
{
154+
if (!gArgs.GetSettingsPath()) {
155+
return true; // Do nothing if settings file disabled.
156+
}
157+
158+
std::vector<std::string> errors;
159+
if (!gArgs.ReadSettingsFile(&errors)) {
160+
bilingual_str error = _("Settings file could not be read");
161+
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, JoinErrors(errors))));
162+
163+
QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Reset | QMessageBox::Abort);
164+
messagebox.setInformativeText(QObject::tr("Do you want to reset settings to default values, or to abort without making changes?"));
165+
messagebox.setDetailedText(QString::fromStdString(JoinErrors(errors)));
166+
messagebox.setTextFormat(Qt::PlainText);
167+
messagebox.setDefaultButton(QMessageBox::Reset);
168+
switch (messagebox.exec()) {
169+
case QMessageBox::Reset:
170+
break;
171+
case QMessageBox::Abort:
172+
return false;
173+
default:
174+
assert(false);
175+
}
176+
}
177+
178+
errors.clear();
179+
if (!gArgs.WriteSettingsFile(&errors)) {
180+
bilingual_str error = _("Settings file could not be written");
181+
InitError(Untranslated(strprintf("%s:\n%s\n", error.original, JoinErrors(errors))));
182+
183+
QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Ok);
184+
messagebox.setInformativeText(QObject::tr("A fatal error occured. Check that settings file is writable, or try running with -nosettings."));
185+
messagebox.setDetailedText(QString::fromStdString(JoinErrors(errors)));
186+
messagebox.setTextFormat(Qt::PlainText);
187+
messagebox.setDefaultButton(QMessageBox::Ok);
188+
messagebox.exec();
189+
return false;
190+
}
191+
return true;
192+
}
193+
147194
/* qDebug() message handler --> debug.log */
148195
void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &msg)
149196
{
@@ -569,9 +616,8 @@ int GuiMain(int argc, char* argv[])
569616
// Parse URIs on command line -- this can affect Params()
570617
PaymentServer::ipcParseCommandLine(argc, argv);
571618
#endif
572-
if (!gArgs.InitSettings(error)) {
573-
InitError(Untranslated(error));
574-
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error initializing settings: %1").arg(QString::fromStdString(error)));
619+
620+
if (!InitSettings()) {
575621
return EXIT_FAILURE;
576622
}
577623

0 commit comments

Comments
 (0)