From 9ffa56765ced08979f562483a2d49c98212035f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20B=C3=A4hr?= Date: Sun, 5 Jan 2025 13:53:37 +0100 Subject: [PATCH] Main: Avoid Popups in Console Mode When in console mode, some messages from CLI interaction still resulted in popup dialogs, e.g. the response from `--help` or `--version`. This is becasue those information are communicated via exceptions and those exceptions prevented the console mode from being properly set. By using a scope guard the console mode flag is now evaluated in all cases. The code to display those messages got refactored into dedicated methods which now also take care of console mode. --- src/App/Application.cpp | 18 ++++++++++----- src/App/PreCompiled.h | 1 + src/Main/MainGui.cpp | 51 ++++++++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 967bf5bcb2..1120475074 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -35,6 +35,7 @@ # endif # include # include +# include # include # include #endif @@ -2420,11 +2421,6 @@ void processProgramOptions(const variables_map& vm, std::map Mods = vm["module-path"].as< vector >(); string temp; @@ -2599,7 +2595,17 @@ void Application::initConfig(int argc, char ** argv) } variables_map vm; - parseProgramOptions(argc, argv, mConfig["ExeName"], vm); + { + BOOST_SCOPE_EXIT_ALL(&) { + // console-mode needs to be set (if possible) also in case parseProgramOptions + // throws, as it's needed when reporting such exceptions + if (vm.count("console")) { + mConfig["Console"] = "1"; + mConfig["RunMode"] = "Cmd"; + } + }; + parseProgramOptions(argc, argv, mConfig["ExeName"], vm); + } if (vm.count("keep-deprecated-paths")) { mConfig["KeepDeprecatedPaths"] = "1"; diff --git a/src/App/PreCompiled.h b/src/App/PreCompiled.h index 57cafc2a35..66ec6e5a34 100644 --- a/src/App/PreCompiled.h +++ b/src/App/PreCompiled.h @@ -100,6 +100,7 @@ #include #include #include +#include #endif //_PreComp_ diff --git a/src/Main/MainGui.cpp b/src/Main/MainGui.cpp index be25becbef..80e0c5e8a2 100644 --- a/src/Main/MainGui.cpp +++ b/src/Main/MainGui.cpp @@ -95,6 +95,35 @@ private: FILE* file; }; +static void DisplayInfo(const QString& msg, bool preformatted = true) +{ + if (App::Application::Config()["Console"] == "1") { + std::cout << msg.toStdString(); + return; + } + + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); + QMessageBox msgBox; + msgBox.setIcon(QMessageBox::Information); + msgBox.setWindowTitle(appName); + msgBox.setDetailedText(msg); + msgBox.setText(preformatted ? QStringLiteral("
%1
").arg(msg) : msg); + msgBox.exec(); +} + +static void DisplayCritical(const QString& msg, bool preformatted = true) +{ + if (App::Application::Config()["Console"] == "1") { + std::cerr << msg.toStdString(); + return; + } + + QString appName = QString::fromStdString(App::Application::Config()["ExeName"]); + QString title = QObject::tr("Initialization of %1 failed").arg(appName); + QString text = preformatted ? QStringLiteral("
%1
").arg(msg) : msg; + QMessageBox::critical(nullptr, title, text); +} + int main(int argc, char** argv) { #if defined(FC_OS_LINUX) || defined(FC_OS_BSD) @@ -217,24 +246,14 @@ int main(int argc, char** argv) } catch (const Base::UnknownProgramOption& e) { QApplication app(argc, argv); - QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str()); QString msg = QString::fromLatin1(e.what()); - QString s = QLatin1String("
") + msg + QLatin1String("
"); - QMessageBox::critical(nullptr, appName, s); + DisplayCritical(msg); exit(1); } catch (const Base::ProgramInformation& e) { QApplication app(argc, argv); - QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str()); QString msg = QString::fromUtf8(e.what()); - QString s = QLatin1String("
") + msg + QLatin1String("
"); - - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Information); - msgBox.setWindowTitle(appName); - msgBox.setDetailedText(msg); - msgBox.setText(s); - msgBox.exec(); + DisplayInfo(msg); exit(0); } catch (const Base::Exception& e) { @@ -261,9 +280,7 @@ int main(int argc, char** argv) "\nPlease contact the application's support team for more information.\n\n"); } - QMessageBox::critical(nullptr, - QObject::tr("Initialization of %1 failed").arg(appName), - msg); + DisplayCritical(msg, false); exit(100); } catch (...) { @@ -274,9 +291,7 @@ int main(int argc, char** argv) QObject::tr("Unknown runtime error occurred while initializing %1.\n\n" "Please contact the application's support team for more information.\n\n") .arg(appName); - QMessageBox::critical(nullptr, - QObject::tr("Initialization of %1 failed").arg(appName), - msg); + DisplayCritical(msg, false); exit(101); }