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.
This commit is contained in:
Jonas Bähr
2025-01-05 13:53:37 +01:00
committed by Yorik van Havre
parent ddf4421db1
commit 9ffa56765c
3 changed files with 46 additions and 24 deletions

View File

@@ -35,6 +35,7 @@
# endif
# include <boost/program_options.hpp>
# include <boost/date_time/posix_time/posix_time.hpp>
# include <boost/scope_exit.hpp>
# include <chrono>
# include <random>
#endif
@@ -2420,11 +2421,6 @@ void processProgramOptions(const variables_map& vm, std::map<std::string,std::st
throw Base::ProgramInformation(str.str());
}
if (vm.count("console")) {
mConfig["Console"] = "1";
mConfig["RunMode"] = "Cmd";
}
if (vm.count("module-path")) {
vector<string> Mods = vm["module-path"].as< vector<string> >();
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";

View File

@@ -100,6 +100,7 @@
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/scope_exit.hpp>
#endif //_PreComp_

View File

@@ -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("<pre>%1</pre>").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("<pre>%1</pre>").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("<pre>") + msg + QLatin1String("</pre>");
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("<pre>") + msg + QLatin1String("</pre>");
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);
}