From 9d3ae3e2b39710765d0523a47d7b7bb9c28d0d1c Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Thu, 15 Feb 2024 00:01:31 +0100 Subject: [PATCH] refactor: Restructure parsing and setup loading methods --- src/blackcore/application.cpp | 39 +++++++++++-------- src/blackcore/application.h | 31 ++++++++++----- src/swiftcore/main.cpp | 2 +- src/swiftdata/main.cpp | 2 +- src/swiftguistandard/main.cpp | 4 +- src/swiftlauncher/main.cpp | 2 +- .../context/testcontext/testcontext.cpp | 2 +- .../testconnectivity/testconnectivity.cpp | 2 +- 8 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 4494c03aa..ad551af72 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -349,12 +349,7 @@ namespace BlackCore { m_started = false; // reset - // parse if needed, parsing contains its own error handling - if (!m_parsed) - { - const bool s = this->parseAndStartupCheck(); - if (!s) { return false; } - } + Q_ASSERT_X(m_parsed, Q_FUNC_INFO, "Call this function after parsing"); // parsing itself is done CStatusMessageList msgs; @@ -1339,18 +1334,18 @@ namespace BlackCore return m_parser.value(option).trimmed(); } - bool CApplication::parseAndStartupCheck() + bool CApplication::parseCommandLineArgsAndLoadSetup() + { + if (!this->startupCheck()) return false; + if (!this->parseCommandLineArguments()) return false; + if (!this->loadSetupAndHandleErrors()) return false; + return true; + } + + bool CApplication::parseCommandLineArguments() { if (m_parsed) { return m_parsed; } // already done - // checks - const QStringList verifyErrors = CSwiftDirectories::verifyRuntimeDirectoriesAndFiles(); - if (!verifyErrors.isEmpty() && !m_applicationInfo.isUnitTest()) - { - this->cmdLineErrorMessage("Missing runtime directories/files:", verifyErrors.join(", ")); - return false; - } - // we call parse because we also want to display a GUI error message when applicable const QStringList args(QCoreApplication::instance()->arguments()); if (!m_parser.parse(args)) @@ -1390,9 +1385,19 @@ namespace BlackCore return true; } - bool CApplication::parseAndLoadSetup() + bool CApplication::startupCheck() const + { + const QStringList verifyErrors = CSwiftDirectories::verifyRuntimeDirectoriesAndFiles(); + if (!verifyErrors.isEmpty() && !m_applicationInfo.isUnitTest()) + { + cmdLineErrorMessage("Missing runtime directories/files:", verifyErrors.join(", ")); + return false; + } + return true; + } + + bool CApplication::loadSetupAndHandleErrors() { - if (!this->parseAndStartupCheck()) return false; const CStatusMessageList msgs = loadSetup(); if (msgs.isFailure()) diff --git a/src/blackcore/application.h b/src/blackcore/application.h index 3091e8e42..80df3121a 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -287,16 +287,13 @@ namespace BlackCore //! Delegates to QCommandLineParser::value QString getParserValue(const QCommandLineOption &option) const; - //! Parses and handles the standard options such as help, version, parse error - //! \note in some cases (error, version, help) application is terminated during this step - //! \sa parsingHookIn - //! \return true means to continue, false to stop - bool parseAndStartupCheck(); - - //! Combined function - //! \see parseAndStartupCheck - //! \see loadSetup - bool parseAndLoadSetup(); + //! Combined function that does a startup check, parses the command line arguments and loads the setup + //! \see startupCheck + //! \see parseCommandLineArguments + //! \see loadSetupAndHandleErrors + //! \remark This function cannot automatically called from the constructor because (1) it calls virtual + //! functions to show error messages and (2) the some command line arguments are added after constructing the object + bool parseCommandLineArgsAndLoadSetup(); //! Display warning message virtual bool cmdLineWarningMessage(const QString &text, const QString &informativeText = "") const; @@ -712,6 +709,20 @@ namespace BlackCore //! Write meta information into the application directory so other swift versions can display them void tagApplicationDataDirectory(); + //! Check if all required runtime files are in place + //! \returns true if the check succeeded + bool startupCheck() const; + + //! Loads the setup (bootstrap) and handles/displays warnings and error messages + //! \returns true if loading succedded without errors + bool loadSetupAndHandleErrors(); + + //! Parses and handles the standard options such as help, version, parse error + //! \note in some cases (error, version, help) application is terminated during this step + //! \sa parsingHookIn + //! \return true means to continue, false to stop + bool parseCommandLineArguments(); + CInputManager *m_inputManager = nullptr; //!< Input devices and hotkeys QNetworkConfigurationManager *m_networkConfigManager = nullptr; //!< configuration QNetworkAccessManager *m_accessManager = nullptr; //!< single network access manager diff --git a/src/swiftcore/main.cpp b/src/swiftcore/main.cpp index eaa60597f..6e3d555a2 100644 --- a/src/swiftcore/main.cpp +++ b/src/swiftcore/main.cpp @@ -34,7 +34,7 @@ int main(int argc, char *argv[]) a.addDBusAddressOption(); a.addVatlibOptions(); a.addAudioOptions(); - if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } + if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; } const QString dBusAdress(a.getCmdDBusAddressValue()); a.useContexts(CCoreFacadeConfig::forCoreAllLocalInDBus(dBusAdress)); diff --git a/src/swiftdata/main.cpp b/src/swiftdata/main.cpp index aa7dfa9d4..2324ba8e3 100644 --- a/src/swiftdata/main.cpp +++ b/src/swiftdata/main.cpp @@ -25,8 +25,8 @@ int main(int argc, char *argv[]) CCrashHandler::instance()->init(); CGuiApplication a(CApplicationInfo::swiftMappingTool(), CApplicationInfo::MappingTool, CIcons::swiftDatabase48()); + if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; } a.splashScreen(CIcons::swiftDatabase256()); - if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forMappingTool()); a.useFacadeNoContexts(); if (!a.start()) diff --git a/src/swiftguistandard/main.cpp b/src/swiftguistandard/main.cpp index e204a19db..b30633153 100644 --- a/src/swiftguistandard/main.cpp +++ b/src/swiftguistandard/main.cpp @@ -37,10 +37,10 @@ int main(int argc, char *argv[]) int r = 0; { CSwiftGuiStdApplication a; // application with contexts + if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; } a.splashScreen(CIcons::swift256()); a.setMinimumSizeInCharacters(60, 42); // experimental - if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } - if (!a.hasSetupReader() || !a.start()) + if (!a.start()) { a.gracefulShutdown(); return EXIT_FAILURE; diff --git a/src/swiftlauncher/main.cpp b/src/swiftlauncher/main.cpp index 1263b73b2..f2b15d976 100644 --- a/src/swiftlauncher/main.cpp +++ b/src/swiftlauncher/main.cpp @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) CGuiApplication a(CApplicationInfo::swiftLauncher(), CApplicationInfo::Laucher, CIcons::swiftLauncher1024()); a.addVatlibOptions(); // so it can be passed (hand over) to started applications a.addParserOption({ { "i", "installer" }, QCoreApplication::translate("main", "Installer setup.") }); - if (!a.parseAndLoadSetup()) { return EXIT_FAILURE; } + if (!a.parseCommandLineArgsAndLoadSetup()) { return EXIT_FAILURE; } a.useWebDataServices(BlackCore::CWebReaderFlags::AllSwiftDbReaders, CDatabaseReaderConfigList::forLauncher()); a.useFacadeNoContexts(); if (!a.start()) diff --git a/tests/blackcore/context/testcontext/testcontext.cpp b/tests/blackcore/context/testcontext/testcontext.cpp index d20ef2a44..e5495c3e6 100644 --- a/tests/blackcore/context/testcontext/testcontext.cpp +++ b/tests/blackcore/context/testcontext/testcontext.cpp @@ -100,7 +100,7 @@ int main(int argc, char *argv[]) BLACKTEST_INIT(BlackCoreTest::CTestContext) CApplication a(CApplicationInfo::UnitTest); a.addVatlibOptions(); - const bool setup = a.parseAndLoadSetup(); + const bool setup = a.parseCommandLineArgsAndLoadSetup(); if (!setup) { qWarning() << "No setup loaded"; } int r = EXIT_FAILURE; if (a.start()) diff --git a/tests/blackcore/testconnectivity/testconnectivity.cpp b/tests/blackcore/testconnectivity/testconnectivity.cpp index fdc93c246..e9ded53e6 100644 --- a/tests/blackcore/testconnectivity/testconnectivity.cpp +++ b/tests/blackcore/testconnectivity/testconnectivity.cpp @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) BLACKTEST_INIT(BlackCoreTest::CTestConnectivity) CApplication a(CApplicationInfo::UnitTest); a.addVatlibOptions(); - const bool setup = a.parseAndLoadSetup(); + const bool setup = a.parseCommandLineArgsAndLoadSetup(); if (!setup) { qWarning() << "No setup loaded"; } int r = EXIT_FAILURE; if (a.start())