From e04f87fe9510d3f7576ec9563f9cbfbd3a86d28b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Mar 2020 22:44:02 +0200 Subject: [PATCH] [libcalamaresui] Refactor moduleFromDescriptor - this function lives in Module -- and is the only thing typing Module to the ViewSteps and JobTypes. Split it out into its own funciton. Nothing else in Module needs to befriend the ViewSteps, so we move the friend declaration around a bit as well. - while here, apply coding style. This is prep-work for moving module to libcalamares. --- src/calamares/testmain.cpp | 2 +- src/libcalamaresui/CMakeLists.txt | 1 + .../modulesystem/CppJobModule.h | 8 +- src/libcalamaresui/modulesystem/Module.cpp | 108 +----------- src/libcalamaresui/modulesystem/Module.h | 23 ++- .../modulesystem/ModuleFactory.cpp | 154 ++++++++++++++++++ .../modulesystem/ModuleFactory.h | 47 ++++++ .../modulesystem/ModuleManager.cpp | 9 +- .../modulesystem/ProcessJobModule.h | 6 +- .../modulesystem/PythonJobModule.h | 6 +- .../modulesystem/PythonQtViewModule.h | 8 +- src/libcalamaresui/modulesystem/ViewModule.h | 8 +- 12 files changed, 249 insertions(+), 131 deletions(-) create mode 100644 src/libcalamaresui/modulesystem/ModuleFactory.cpp create mode 100644 src/libcalamaresui/modulesystem/ModuleFactory.h diff --git a/src/calamares/testmain.cpp b/src/calamares/testmain.cpp index 0845218eb..8dcf41faa 100644 --- a/src/calamares/testmain.cpp +++ b/src/calamares/testmain.cpp @@ -183,7 +183,7 @@ load_module( const ModuleConfig& moduleConfig ) cDebug() << "Module" << moduleName << "job-configuration:" << configFile; - Calamares::Module* module = Calamares::Module::fromDescriptor( descriptor, name, configFile, moduleDirectory ); + Calamares::Module* module = Calamares::moduleFromDescriptor( descriptor, name, configFile, moduleDirectory ); return module; } diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index ed50d777b..da4e4b42b 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -7,6 +7,7 @@ include_directories( ${CMAKE_SOURCE_DIR}/src/libcalamares ${CMAKE_BINARY_DIR}/sr set( calamaresui_SOURCES modulesystem/CppJobModule.cpp modulesystem/Module.cpp + modulesystem/ModuleFactory.cpp modulesystem/ModuleManager.cpp modulesystem/ProcessJobModule.cpp modulesystem/RequirementsChecker.cpp diff --git a/src/libcalamaresui/modulesystem/CppJobModule.h b/src/libcalamaresui/modulesystem/CppJobModule.h index d97443a8a..8f7cfb03a 100644 --- a/src/libcalamaresui/modulesystem/CppJobModule.h +++ b/src/libcalamaresui/modulesystem/CppJobModule.h @@ -21,8 +21,8 @@ #ifndef CALAMARES_CPPJOBMODULE_H #define CALAMARES_CPPJOBMODULE_H -#include "Module.h" #include "DllMacro.h" +#include "Module.h" class QPluginLoader; @@ -42,12 +42,16 @@ protected: void initFrom( const QVariantMap& moduleDescriptor ) override; private: - friend class Module; //so only the superclass can instantiate explicit CppJobModule(); virtual ~CppJobModule() override; QPluginLoader* m_loader; job_ptr m_job; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/Module.cpp b/src/libcalamaresui/modulesystem/Module.cpp index 35b1508f1..08a46e546 100644 --- a/src/libcalamaresui/modulesystem/Module.cpp +++ b/src/libcalamaresui/modulesystem/Module.cpp @@ -66,111 +66,6 @@ Module::initFrom( const Calamares::ModuleSystem::Descriptor& moduleDescriptor, c } } -Module* -Module::fromDescriptor( const Calamares::ModuleSystem::Descriptor& moduleDescriptor, - const QString& instanceId, - const QString& configFileName, - const QString& moduleDirectory ) -{ - std::unique_ptr< Module > m; - - QString typeString = moduleDescriptor.value( "type" ).toString(); - QString intfString = moduleDescriptor.value( "interface" ).toString(); - - if ( typeString.isEmpty() || intfString.isEmpty() ) - { - cError() << "Bad module descriptor format" << instanceId; - return nullptr; - } - if ( ( typeString == "view" ) || ( typeString == "viewmodule" ) ) - { - if ( intfString == "qtplugin" ) - { - m.reset( new ViewModule() ); - } - else if ( intfString == "pythonqt" ) - { -#ifdef WITH_PYTHONQT - m.reset( new PythonQtViewModule() ); -#else - cError() << "PythonQt view modules are not supported in this version of Calamares."; -#endif - } - else - { - cError() << "Bad interface" << intfString << "for module type" << typeString; - } - } - else if ( typeString == "job" ) - { - if ( intfString == "qtplugin" ) - { - m.reset( new CppJobModule() ); - } - else if ( intfString == "process" ) - { - m.reset( new ProcessJobModule() ); - } - else if ( intfString == "python" ) - { -#ifdef WITH_PYTHON - m.reset( new PythonJobModule() ); -#else - cError() << "Python modules are not supported in this version of Calamares."; -#endif - } - else - { - cError() << "Bad interface" << intfString << "for module type" << typeString; - } - } - else - { - cError() << "Bad module type" << typeString; - } - - if ( !m ) - { - cError() << "Bad module type (" << typeString << ") or interface string (" << intfString << ") for module " - << instanceId; - return nullptr; - } - - QDir moduleDir( moduleDirectory ); - if ( moduleDir.exists() && moduleDir.isReadable() ) - { - m->m_directory = moduleDir.absolutePath(); - } - else - { - cError() << "Bad module directory" << moduleDirectory << "for" << instanceId; - return nullptr; - } - - m->initFrom( moduleDescriptor, instanceId ); - if ( !m->m_key.isValid() ) - { - cError() << "Module" << instanceId << "invalid ID"; - return nullptr; - } - - m->initFrom( moduleDescriptor ); - if ( !configFileName.isEmpty() ) - { - try - { - m->loadConfigurationFile( configFileName ); - } - catch ( YAML::Exception& e ) - { - cError() << "YAML parser error " << e.what(); - return nullptr; - } - } - return m.release(); -} - - static QStringList moduleConfigurationCandidates( bool assumeBuildDir, const QString& moduleName, const QString& configFileName ) { @@ -211,7 +106,8 @@ moduleConfigurationCandidates( bool assumeBuildDir, const QString& moduleName, c return paths; } -void Module::loadConfigurationFile( const QString& configFileName ) //throws YAML::Exception +void +Module::loadConfigurationFile( const QString& configFileName ) //throws YAML::Exception { QStringList configCandidates = moduleConfigurationCandidates( Settings::instance()->debugMode(), name(), configFileName ); diff --git a/src/libcalamaresui/modulesystem/Module.h b/src/libcalamaresui/modulesystem/Module.h index 5455916e4..67b76601a 100644 --- a/src/libcalamaresui/modulesystem/Module.h +++ b/src/libcalamaresui/modulesystem/Module.h @@ -33,6 +33,12 @@ namespace Calamares { +class Module; +Module* moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); + /** * @brief The Module class is a common supertype for Calamares modules. @@ -68,18 +74,6 @@ public: PythonQt // Views only, available as enum even if PythonQt isn't used }; - /** - * @brief fromDescriptor creates a new Module object of the correct type. - * @param moduleDescriptor a module descriptor, already parsed into a variant map. - * @param instanceId the instance id of the new module instance. - * @param configFileName the name of the configuration file to read. - * @param moduleDirectory the path to the directory with this module's files. - * @return a pointer to an object of a subtype of Module. - */ - static Module* fromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, - const QString& instanceId, - const QString& configFileName, - const QString& moduleDirectory ); virtual ~Module(); /** @@ -193,6 +187,11 @@ private: QString m_directory; ModuleSystem::InstanceKey m_key; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/ModuleFactory.cpp b/src/libcalamaresui/modulesystem/ModuleFactory.cpp new file mode 100644 index 000000000..f3b46eab7 --- /dev/null +++ b/src/libcalamaresui/modulesystem/ModuleFactory.cpp @@ -0,0 +1,154 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, Teo Mrnjavac + * Copyright 2017-2018, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "ModuleFactory.h" + +#include "CalamaresConfig.h" +#include "CppJobModule.h" +#include "ProcessJobModule.h" +#include "ViewModule.h" + +#include "utils/Dirs.h" +#include "utils/Logger.h" +#include "utils/NamedEnum.h" +#include "utils/Yaml.h" + +#ifdef WITH_PYTHON +#include "PythonJobModule.h" +#endif + +#ifdef WITH_PYTHONQT +#include "PythonQtViewModule.h" +#endif + +#include +#include +#include +#include + + +namespace Calamares +{ + +Module* +moduleFromDescriptor( const Calamares::ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ) +{ + std::unique_ptr< Module > m; + + QString typeString = moduleDescriptor.value( "type" ).toString(); + QString intfString = moduleDescriptor.value( "interface" ).toString(); + + if ( typeString.isEmpty() || intfString.isEmpty() ) + { + cError() << "Bad module descriptor format" << instanceId; + return nullptr; + } + if ( ( typeString == "view" ) || ( typeString == "viewmodule" ) ) + { + if ( intfString == "qtplugin" ) + { + m.reset( new ViewModule() ); + } + else if ( intfString == "pythonqt" ) + { +#ifdef WITH_PYTHONQT + m.reset( new PythonQtViewModule() ); +#else + cError() << "PythonQt view modules are not supported in this version of Calamares."; +#endif + } + else + { + cError() << "Bad interface" << intfString << "for module type" << typeString; + } + } + else if ( typeString == "job" ) + { + if ( intfString == "qtplugin" ) + { + m.reset( new CppJobModule() ); + } + else if ( intfString == "process" ) + { + m.reset( new ProcessJobModule() ); + } + else if ( intfString == "python" ) + { +#ifdef WITH_PYTHON + m.reset( new PythonJobModule() ); +#else + cError() << "Python modules are not supported in this version of Calamares."; +#endif + } + else + { + cError() << "Bad interface" << intfString << "for module type" << typeString; + } + } + else + { + cError() << "Bad module type" << typeString; + } + + if ( !m ) + { + cError() << "Bad module type (" << typeString << ") or interface string (" << intfString << ") for module " + << instanceId; + return nullptr; + } + + QDir moduleDir( moduleDirectory ); + if ( moduleDir.exists() && moduleDir.isReadable() ) + { + m->m_directory = moduleDir.absolutePath(); + } + else + { + cError() << "Bad module directory" << moduleDirectory << "for" << instanceId; + return nullptr; + } + + m->initFrom( moduleDescriptor, instanceId ); + if ( !m->m_key.isValid() ) + { + cError() << "Module" << instanceId << "invalid ID"; + return nullptr; + } + + m->initFrom( moduleDescriptor ); + if ( !configFileName.isEmpty() ) + { + try + { + m->loadConfigurationFile( configFileName ); + } + catch ( YAML::Exception& e ) + { + cError() << "YAML parser error " << e.what(); + return nullptr; + } + } + return m.release(); +} + + +} // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/ModuleFactory.h b/src/libcalamaresui/modulesystem/ModuleFactory.h new file mode 100644 index 000000000..8184967d2 --- /dev/null +++ b/src/libcalamaresui/modulesystem/ModuleFactory.h @@ -0,0 +1,47 @@ +/* === This file is part of Calamares - === + * + * Copyright 2014-2015, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef CALAMARES_MODULEFACTORY_H +#define CALAMARES_MODULEFACTORY_H + +#include "DllMacro.h" + +#include "modulesystem/Descriptor.h" +#include "modulesystem/Module.h" + +#include + +namespace Calamares +{ + +/** + * @brief fromDescriptor creates a new Module object of the correct type. + * @param moduleDescriptor a module descriptor, already parsed into a variant map. + * @param instanceId the instance id of the new module instance. + * @param configFileName the name of the configuration file to read. + * @param moduleDirectory the path to the directory with this module's files. + * @return a pointer to an object of a subtype of Module. + */ +UIDLLEXPORT Module* moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); +} // namespace Calamares + +#endif // CALAMARES_MODULEFACTORY_H diff --git a/src/libcalamaresui/modulesystem/ModuleManager.cpp b/src/libcalamaresui/modulesystem/ModuleManager.cpp index f88d5999d..cd79afad4 100644 --- a/src/libcalamaresui/modulesystem/ModuleManager.cpp +++ b/src/libcalamaresui/modulesystem/ModuleManager.cpp @@ -285,10 +285,11 @@ ModuleManager::loadModules() } else { - thisModule = Module::fromDescriptor( descriptor, - instanceKey.id(), - configFileName, - m_moduleDirectoriesByModuleName.value( instanceKey.module() ) ); + thisModule + = Calamares::moduleFromDescriptor( descriptor, + instanceKey.id(), + configFileName, + m_moduleDirectoriesByModuleName.value( instanceKey.module() ) ); if ( !thisModule ) { cError() << "Module" << instanceKey.toString() << "cannot be created from descriptor" diff --git a/src/libcalamaresui/modulesystem/ProcessJobModule.h b/src/libcalamaresui/modulesystem/ProcessJobModule.h index da2badbd0..5be5de837 100644 --- a/src/libcalamaresui/modulesystem/ProcessJobModule.h +++ b/src/libcalamaresui/modulesystem/ProcessJobModule.h @@ -42,7 +42,6 @@ protected: void initFrom( const QVariantMap& moduleDescriptor ) override; private: - friend class Module; explicit ProcessJobModule(); virtual ~ProcessJobModule() override; @@ -51,6 +50,11 @@ private: std::chrono::seconds m_secondsTimeout; bool m_runInChroot; job_ptr m_job; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/PythonJobModule.h b/src/libcalamaresui/modulesystem/PythonJobModule.h index 295ab5942..8f65daa48 100644 --- a/src/libcalamaresui/modulesystem/PythonJobModule.h +++ b/src/libcalamaresui/modulesystem/PythonJobModule.h @@ -39,13 +39,17 @@ protected: void initFrom( const QVariantMap& moduleDescriptor ) override; private: - friend class Module; explicit PythonJobModule(); virtual ~PythonJobModule() override; QString m_scriptFileName; QString m_workingPath; job_ptr m_job; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/PythonQtViewModule.h b/src/libcalamaresui/modulesystem/PythonQtViewModule.h index 33b8d041b..64cc0f8a9 100644 --- a/src/libcalamaresui/modulesystem/PythonQtViewModule.h +++ b/src/libcalamaresui/modulesystem/PythonQtViewModule.h @@ -19,8 +19,8 @@ #ifndef CALAMARES_PYTHONQTVIEWMODULE_H #define CALAMARES_PYTHONQTVIEWMODULE_H -#include "Module.h" #include "DllMacro.h" +#include "Module.h" namespace Calamares { @@ -40,7 +40,6 @@ protected: void initFrom( const QVariantMap& moduleDescriptor ) override; private: - friend class Module; //so only the superclass can instantiate explicit PythonQtViewModule(); virtual ~PythonQtViewModule(); @@ -48,6 +47,11 @@ private: QString m_scriptFileName; QString m_workingPath; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/ViewModule.h b/src/libcalamaresui/modulesystem/ViewModule.h index c1ee8ff69..958b99c1c 100644 --- a/src/libcalamaresui/modulesystem/ViewModule.h +++ b/src/libcalamaresui/modulesystem/ViewModule.h @@ -20,8 +20,8 @@ #ifndef CALAMARES_VIEWMODULE_H #define CALAMARES_VIEWMODULE_H -#include "Module.h" #include "DllMacro.h" +#include "Module.h" class QPluginLoader; @@ -45,12 +45,16 @@ protected: void initFrom( const QVariantMap& moduleDescriptor ) override; private: - friend class Module; //so only the superclass can instantiate explicit ViewModule(); virtual ~ViewModule() override; QPluginLoader* m_loader; ViewStep* m_viewStep = nullptr; + + friend Module* Calamares::moduleFromDescriptor( const ModuleSystem::Descriptor& moduleDescriptor, + const QString& instanceId, + const QString& configFileName, + const QString& moduleDirectory ); }; } // namespace Calamares