Skip to content

Commit ecf2c24

Browse files
committed
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.
1 parent e977eb4 commit ecf2c24

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/App/Application.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# endif
3636
# include <boost/program_options.hpp>
3737
# include <boost/date_time/posix_time/posix_time.hpp>
38+
# include <boost/scope_exit.hpp>
3839
# include <chrono>
3940
# include <random>
4041
#endif
@@ -2415,11 +2416,6 @@ void processProgramOptions(const variables_map& vm, std::map<std::string,std::st
24152416
throw Base::ProgramInformation(str.str());
24162417
}
24172418

2418-
if (vm.count("console")) {
2419-
mConfig["Console"] = "1";
2420-
mConfig["RunMode"] = "Cmd";
2421-
}
2422-
24232419
if (vm.count("module-path")) {
24242420
vector<string> Mods = vm["module-path"].as< vector<string> >();
24252421
string temp;
@@ -2585,7 +2581,17 @@ void Application::initConfig(int argc, char ** argv)
25852581
}
25862582

25872583
variables_map vm;
2588-
parseProgramOptions(argc, argv, mConfig["ExeName"], vm);
2584+
{
2585+
BOOST_SCOPE_EXIT_ALL(&) {
2586+
// console-mode needs to be set (if possible) also in case parseProgramOptions
2587+
// throws, as it's needed when reporting such exceptions
2588+
if (vm.count("console")) {
2589+
mConfig["Console"] = "1";
2590+
mConfig["RunMode"] = "Cmd";
2591+
}
2592+
};
2593+
parseProgramOptions(argc, argv, mConfig["ExeName"], vm);
2594+
}
25892595

25902596
if (vm.count("keep-deprecated-paths")) {
25912597
mConfig["KeepDeprecatedPaths"] = "1";

src/Main/MainGui.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ class Redirection
9595
FILE* file;
9696
};
9797

98+
static void DisplayInfo(const QString& msg, bool preformatted = true)
99+
{
100+
if (App::Application::Config()["Console"] == "1") {
101+
std::cout << msg.toStdString();
102+
return;
103+
}
104+
105+
QString appName = QString::fromStdString(App::Application::Config()["ExeName"]);
106+
QMessageBox msgBox;
107+
msgBox.setIcon(QMessageBox::Information);
108+
msgBox.setWindowTitle(appName);
109+
msgBox.setDetailedText(msg);
110+
msgBox.setText(preformatted ? QLatin1String("<pre>%1</pre>").arg(msg) : msg);
111+
msgBox.exec();
112+
}
113+
114+
static void DisplayCritical(const QString& msg, bool preformatted = true)
115+
{
116+
if (App::Application::Config()["Console"] == "1") {
117+
std::cerr << msg.toStdString();
118+
return;
119+
}
120+
121+
QString appName = QString::fromStdString(App::Application::Config()["ExeName"]);
122+
QString title = QObject::tr("Initialization of %1 failed").arg(appName);
123+
QString text = preformatted ? QLatin1String("<pre>%1</pre>").arg(msg) : msg;
124+
QMessageBox::critical(nullptr, title, text);
125+
}
126+
98127
int main(int argc, char** argv)
99128
{
100129
#if defined(FC_OS_LINUX) || defined(FC_OS_BSD)
@@ -214,24 +243,14 @@ int main(int argc, char** argv)
214243
}
215244
catch (const Base::UnknownProgramOption& e) {
216245
QApplication app(argc, argv);
217-
QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str());
218246
QString msg = QString::fromLatin1(e.what());
219-
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
220-
QMessageBox::critical(nullptr, appName, s);
247+
DisplayCritical(msg);
221248
exit(1);
222249
}
223250
catch (const Base::ProgramInformation& e) {
224251
QApplication app(argc, argv);
225-
QString appName = QString::fromLatin1(App::Application::Config()["ExeName"].c_str());
226252
QString msg = QString::fromUtf8(e.what());
227-
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
228-
229-
QMessageBox msgBox;
230-
msgBox.setIcon(QMessageBox::Information);
231-
msgBox.setWindowTitle(appName);
232-
msgBox.setDetailedText(msg);
233-
msgBox.setText(s);
234-
msgBox.exec();
253+
DisplayInfo(msg);
235254
exit(0);
236255
}
237256
catch (const Base::Exception& e) {
@@ -258,9 +277,7 @@ int main(int argc, char** argv)
258277
"\nPlease contact the application's support team for more information.\n\n");
259278
}
260279

261-
QMessageBox::critical(nullptr,
262-
QObject::tr("Initialization of %1 failed").arg(appName),
263-
msg);
280+
DisplayCritical(msg, false);
264281
exit(100);
265282
}
266283
catch (...) {
@@ -271,9 +288,7 @@ int main(int argc, char** argv)
271288
QObject::tr("Unknown runtime error occurred while initializing %1.\n\n"
272289
"Please contact the application's support team for more information.\n\n")
273290
.arg(appName);
274-
QMessageBox::critical(nullptr,
275-
QObject::tr("Initialization of %1 failed").arg(appName),
276-
msg);
291+
DisplayCritical(msg, false);
277292
exit(101);
278293
}
279294

0 commit comments

Comments
 (0)