|
9 | 9 | #include <qt/bitcoin.h> |
10 | 10 |
|
11 | 11 | #include <chainparams.h> |
| 12 | +#include <common/init.h> |
12 | 13 | #include <init.h> |
13 | 14 | #include <interfaces/handler.h> |
14 | 15 | #include <interfaces/init.h> |
@@ -165,54 +166,36 @@ static void initTranslations(QTranslator &qtTranslatorBase, QTranslator &qtTrans |
165 | 166 | } |
166 | 167 | } |
167 | 168 |
|
168 | | -static bool InitSettings() |
| 169 | +static bool ErrorSettingsRead(const bilingual_str& error, const std::vector<std::string>& details) |
169 | 170 | { |
170 | | - gArgs.EnsureDataDir(); |
171 | | - if (!gArgs.GetSettingsPath()) { |
172 | | - return true; // Do nothing if settings file disabled. |
173 | | - } |
174 | | - |
175 | | - std::vector<std::string> errors; |
176 | | - if (!gArgs.ReadSettingsFile(&errors)) { |
177 | | - std::string error = QT_TRANSLATE_NOOP("bitcoin-core", "Settings file could not be read"); |
178 | | - std::string error_translated = QCoreApplication::translate("bitcoin-core", error.c_str()).toStdString(); |
179 | | - InitError(Untranslated(strprintf("%s:\n%s", error, MakeUnorderedList(errors)))); |
180 | | - |
181 | | - QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error_translated)), QMessageBox::Reset | QMessageBox::Abort); |
182 | | - /*: Explanatory text shown on startup when the settings file cannot be read. |
183 | | - Prompts user to make a choice between resetting or aborting. */ |
184 | | - messagebox.setInformativeText(QObject::tr("Do you want to reset settings to default values, or to abort without making changes?")); |
185 | | - messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(errors))); |
186 | | - messagebox.setTextFormat(Qt::PlainText); |
187 | | - messagebox.setDefaultButton(QMessageBox::Reset); |
188 | | - switch (messagebox.exec()) { |
189 | | - case QMessageBox::Reset: |
190 | | - break; |
191 | | - case QMessageBox::Abort: |
192 | | - return false; |
193 | | - default: |
194 | | - assert(false); |
195 | | - } |
196 | | - } |
197 | | - |
198 | | - errors.clear(); |
199 | | - if (!gArgs.WriteSettingsFile(&errors)) { |
200 | | - std::string error = QT_TRANSLATE_NOOP("bitcoin-core", "Settings file could not be written"); |
201 | | - std::string error_translated = QCoreApplication::translate("bitcoin-core", error.c_str()).toStdString(); |
202 | | - InitError(Untranslated(strprintf("%s:\n%s", error, MakeUnorderedList(errors)))); |
203 | | - |
204 | | - QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error_translated)), QMessageBox::Ok); |
205 | | - /*: Explanatory text shown on startup when the settings file could not be written. |
206 | | - Prompts user to check that we have the ability to write to the file. |
207 | | - Explains that the user has the option of running without a settings file.*/ |
208 | | - messagebox.setInformativeText(QObject::tr("A fatal error occurred. Check that settings file is writable, or try running with -nosettings.")); |
209 | | - messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(errors))); |
210 | | - messagebox.setTextFormat(Qt::PlainText); |
211 | | - messagebox.setDefaultButton(QMessageBox::Ok); |
212 | | - messagebox.exec(); |
| 171 | + QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Reset | QMessageBox::Abort); |
| 172 | + /*: Explanatory text shown on startup when the settings file cannot be read. |
| 173 | + Prompts user to make a choice between resetting or aborting. */ |
| 174 | + messagebox.setInformativeText(QObject::tr("Do you want to reset settings to default values, or to abort without making changes?")); |
| 175 | + messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(details))); |
| 176 | + messagebox.setTextFormat(Qt::PlainText); |
| 177 | + messagebox.setDefaultButton(QMessageBox::Reset); |
| 178 | + switch (messagebox.exec()) { |
| 179 | + case QMessageBox::Reset: |
213 | 180 | return false; |
| 181 | + case QMessageBox::Abort: |
| 182 | + return true; |
| 183 | + default: |
| 184 | + assert(false); |
214 | 185 | } |
215 | | - return true; |
| 186 | +} |
| 187 | + |
| 188 | +static void ErrorSettingsWrite(const bilingual_str& error, const std::vector<std::string>& details) |
| 189 | +{ |
| 190 | + QMessageBox messagebox(QMessageBox::Critical, PACKAGE_NAME, QString::fromStdString(strprintf("%s.", error.translated)), QMessageBox::Ok); |
| 191 | + /*: Explanatory text shown on startup when the settings file could not be written. |
| 192 | + Prompts user to check that we have the ability to write to the file. |
| 193 | + Explains that the user has the option of running without a settings file.*/ |
| 194 | + messagebox.setInformativeText(QObject::tr("A fatal error occurred. Check that settings file is writable, or try running with -nosettings.")); |
| 195 | + messagebox.setDetailedText(QString::fromStdString(MakeUnorderedList(details))); |
| 196 | + messagebox.setTextFormat(Qt::PlainText); |
| 197 | + messagebox.setDefaultButton(QMessageBox::Ok); |
| 198 | + messagebox.exec(); |
216 | 199 | } |
217 | 200 |
|
218 | 201 | /* qDebug() message handler --> debug.log */ |
@@ -587,45 +570,30 @@ int GuiMain(int argc, char* argv[]) |
587 | 570 | // Gracefully exit if the user cancels |
588 | 571 | if (!Intro::showIfNeeded(did_show_intro, prune_MiB)) return EXIT_SUCCESS; |
589 | 572 |
|
590 | | - /// 6a. Determine availability of data directory |
591 | | - if (!CheckDataDirOption()) { |
592 | | - InitError(strprintf(Untranslated("Specified data directory \"%s\" does not exist."), gArgs.GetArg("-datadir", ""))); |
593 | | - QMessageBox::critical(nullptr, PACKAGE_NAME, |
594 | | - QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", "")))); |
595 | | - return EXIT_FAILURE; |
596 | | - } |
597 | | - try { |
598 | | - /// 6b. Parse bitcoin.conf |
599 | | - /// - Do not call gArgs.GetDataDirNet() before this step finishes |
600 | | - if (!gArgs.ReadConfigFiles(error, true)) { |
601 | | - InitError(strprintf(Untranslated("Error reading configuration file: %s"), error)); |
602 | | - QMessageBox::critical(nullptr, PACKAGE_NAME, |
603 | | - QObject::tr("Error: Cannot parse configuration file: %1.").arg(QString::fromStdString(error))); |
604 | | - return EXIT_FAILURE; |
| 573 | + /// 6-7. Parse bitcoin.conf, determine network, switch to network specific |
| 574 | + /// options, and create datadir and settings.json. |
| 575 | + // - Do not call gArgs.GetDataDirNet() before this step finishes |
| 576 | + // - Do not call Params() before this step |
| 577 | + // - QSettings() will use the new application name after this, resulting in network-specific settings |
| 578 | + // - Needs to be done before createOptionsModel |
| 579 | + if (auto error = common::InitConfig(gArgs, ErrorSettingsRead)) { |
| 580 | + InitError(error->message, error->details); |
| 581 | + if (error->status == common::ConfigStatus::FAILED_WRITE) { |
| 582 | + // Show a custom error message to provide more information in the |
| 583 | + // case of a datadir write error. |
| 584 | + ErrorSettingsWrite(error->message, error->details); |
| 585 | + } else if (error->status != common::ConfigStatus::ABORTED) { |
| 586 | + // Show a generic message in other cases, and no additional error |
| 587 | + // message in the case of a read error if the user decided to abort. |
| 588 | + QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: %1").arg(QString::fromStdString(error->message.translated))); |
605 | 589 | } |
606 | | - |
607 | | - /// 7. Determine network (and switch to network specific options) |
608 | | - // - Do not call Params() before this step |
609 | | - // - Do this after parsing the configuration file, as the network can be switched there |
610 | | - // - QSettings() will use the new application name after this, resulting in network-specific settings |
611 | | - // - Needs to be done before createOptionsModel |
612 | | - |
613 | | - // Check for chain settings (Params() calls are only valid after this clause) |
614 | | - SelectParams(gArgs.GetChainName()); |
615 | | - } catch(std::exception &e) { |
616 | | - InitError(Untranslated(strprintf("%s", e.what()))); |
617 | | - QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: %1").arg(e.what())); |
618 | 590 | return EXIT_FAILURE; |
619 | 591 | } |
620 | 592 | #ifdef ENABLE_WALLET |
621 | 593 | // Parse URIs on command line |
622 | 594 | PaymentServer::ipcParseCommandLine(argc, argv); |
623 | 595 | #endif |
624 | 596 |
|
625 | | - if (!InitSettings()) { |
626 | | - return EXIT_FAILURE; |
627 | | - } |
628 | | - |
629 | 597 | QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(Params().NetworkIDString())); |
630 | 598 | assert(!networkStyle.isNull()); |
631 | 599 | // Allow for separate UI settings for testnets |
|
0 commit comments