diff --git a/src/libcalamaresui/modulesystem/CppJobModule.cpp b/src/libcalamaresui/modulesystem/CppJobModule.cpp index c6571dbf6..2eddeda86 100644 --- a/src/libcalamaresui/modulesystem/CppJobModule.cpp +++ b/src/libcalamaresui/modulesystem/CppJobModule.cpp @@ -86,7 +86,6 @@ CppJobModule::jobs() const void CppJobModule::initFrom( const QVariantMap& moduleDescriptor ) { - Module::initFrom( moduleDescriptor ); QDir directory( location() ); QString load; if ( !moduleDescriptor.value( "load" ).toString().isEmpty() ) diff --git a/src/libcalamaresui/modulesystem/Module.cpp b/src/libcalamaresui/modulesystem/Module.cpp index 799da941e..2d2ea3def 100644 --- a/src/libcalamaresui/modulesystem/Module.cpp +++ b/src/libcalamaresui/modulesystem/Module.cpp @@ -27,6 +27,7 @@ #include "utils/Dirs.h" #include "utils/Logger.h" +#include "utils/NamedEnum.h" #include "utils/Yaml.h" #ifdef WITH_PYTHON @@ -48,8 +49,23 @@ static const char EMERGENCY[] = "emergency"; namespace Calamares { +Module::Module() + : m_loaded( false ) +{ +} + Module::~Module() {} +void +Module::initFrom( const QVariantMap& moduleDescriptor, const QString& id ) +{ + m_key = ModuleSystem::InstanceKey( moduleDescriptor.value( "name" ).toString(), id ); + if ( moduleDescriptor.contains( EMERGENCY ) ) + { + m_maybe_emergency = moduleDescriptor[ EMERGENCY ].toBool(); + } +} + Module* Module::fromDescriptor( const QVariantMap& moduleDescriptor, const QString& instanceId, @@ -131,7 +147,12 @@ Module::fromDescriptor( const QVariantMap& moduleDescriptor, return nullptr; } - m->m_instanceId = instanceId; + m->initFrom( moduleDescriptor, instanceId ); + if ( !m->m_key.isValid() ) + { + cError() << "Module" << instanceId << "invalid ID"; + return nullptr; + } m->initFrom( moduleDescriptor ); try @@ -190,7 +211,7 @@ moduleConfigurationCandidates( bool assumeBuildDir, const QString& moduleName, c void Module::loadConfigurationFile( const QString& configFileName ) //throws YAML::Exception { QStringList configCandidates - = moduleConfigurationCandidates( Settings::instance()->debugMode(), m_name, configFileName ); + = moduleConfigurationCandidates( Settings::instance()->debugMode(), name(), configFileName ); for ( const QString& path : configCandidates ) { QFile configFile( path ); @@ -219,67 +240,59 @@ void Module::loadConfigurationFile( const QString& configFileName ) //throws YA return; } } - cDebug() << "No config file for" << m_name << "found anywhere at" << Logger::DebugList( configCandidates ); + cDebug() << "No config file for" << name() << "found anywhere at" << Logger::DebugList( configCandidates ); } -QString -Module::name() const +static const NamedEnumTable< Module::Type >& +typeNames() { - return m_name; + using Type = Module::Type; + // *INDENT-OFF* + // clang-format off + static const NamedEnumTable< Type > table{ + { QStringLiteral( "job" ), Type::Job }, + { QStringLiteral( "view" ), Type::View }, + { QStringLiteral( "viewmodule" ), Type::View }, + { QStringLiteral( "jobmodule" ), Type::Job } + }; + // *INDENT-ON* + // clang-format on + return table; } - -QString -Module::instanceId() const -{ - return m_instanceId; -} - - -QString -Module::instanceKey() const -{ - return QString( "%1@%2" ).arg( m_name ).arg( m_instanceId ); -} - - -QString -Module::location() const -{ - return m_directory; -} - - QString Module::typeString() const { - switch ( type() ) - { - case Type::Job: - return "Job Module"; - case Type::View: - return "View Module"; - } - return QString(); + bool ok = false; + QString v = typeNames().find( type(), ok ); + return ok ? v : QString(); } +static const NamedEnumTable< Module::Interface >& +interfaceNames() +{ + using Interface = Module::Interface; + // *INDENT-OFF* + // clang-format off + static const NamedEnumTable< Interface > table { + { QStringLiteral("process"), Interface::Process }, + { QStringLiteral("qtplugin"), Interface::QtPlugin }, + { QStringLiteral("python"), Interface::Python }, + { QStringLiteral("pythonqt"), Interface::PythonQt } + }; + // *INDENT-ON* + // clang-format on + return table; +} + QString Module::interfaceString() const { - switch ( interface() ) - { - case Interface::Process: - return "External process"; - case Interface::Python: - return "Python (Boost.Python)"; - case Interface::PythonQt: - return "Python (experimental)"; - case Interface::QtPlugin: - return "Qt Plugin"; - } - return QString(); + bool ok = false; + QString v = interfaceNames().find( interface(), ok ); + return ok ? v : QString(); } @@ -290,22 +303,6 @@ Module::configurationMap() } -Module::Module() - : m_loaded( false ) -{ -} - - -void -Module::initFrom( const QVariantMap& moduleDescriptor ) -{ - m_name = moduleDescriptor.value( "name" ).toString(); - if ( moduleDescriptor.contains( EMERGENCY ) ) - { - m_maybe_emergency = moduleDescriptor[ EMERGENCY ].toBool(); - } -} - RequirementsList Module::checkRequirements() { diff --git a/src/libcalamaresui/modulesystem/Module.h b/src/libcalamaresui/modulesystem/Module.h index d6e6407d1..ec316bece 100644 --- a/src/libcalamaresui/modulesystem/Module.h +++ b/src/libcalamaresui/modulesystem/Module.h @@ -25,6 +25,7 @@ #include "UiDllMacro.h" #include "modulesystem/Descriptor.h" +#include "modulesystem/InstanceKey.h" #include #include @@ -85,13 +86,13 @@ public: * @brief name returns the name of this module. * @return a string with this module's name. */ - virtual QString name() const final; + QString name() const { return m_key.module(); } /** * @brief instanceId returns the instance id of this module. * @return a string with this module's instance id. */ - virtual QString instanceId() const final; + QString instanceId() const { return m_key.id(); } /** * @brief instanceKey returns the instance key of this module. @@ -100,49 +101,13 @@ public: * For instance, "partition\@partition" (default configuration) or * "locale\@someconfig" (custom configuration) */ - virtual QString instanceKey() const final; + QString instanceKey() const { return m_key.toString(); } /** * @brief location returns the full path of this module's directory. * @return the path. */ - virtual QString location() const final; - - /** - * @brief type returns the Type of this module object. - * @return the type enum value. - */ - virtual Type type() const = 0; - - /** - * @brief typeString returns a user-visible string for the module's type. - * @return the type string. - */ - virtual QString typeString() const; - - /** - * @brief interface the Interface used by this module. - * @return the interface enum value. - */ - virtual Interface interface() const = 0; - - /** - * @brief interface returns a user-visible string for the module's interface. - * @return the interface string. - */ - virtual QString interfaceString() const; - - /** - * @brief isLoaded reports on the loaded status of a module. - * @return true if the module's loading phase has finished, otherwise false. - */ - bool isLoaded() const { return m_loaded; } - - /** - * @brief loadSelf initialized the module. - * Subclasses must reimplement this depending on the module type and interface. - */ - virtual void loadSelf() = 0; + QString location() const { return m_directory; } /** * @brief Is this an emergency module? @@ -156,10 +121,10 @@ public: bool isEmergency() const { return m_emergency; } /** - * @brief jobs returns any jobs exposed by this module. - * @return a list of jobs (can be empty). + * @brief isLoaded reports on the loaded status of a module. + * @return true if the module's loading phase has finished, otherwise false. */ - virtual JobList jobs() const = 0; + bool isLoaded() const { return m_loaded; } /** * @brief configurationMap returns the contents of the configuration file for @@ -168,6 +133,42 @@ public: */ QVariantMap configurationMap(); + /** + * @brief typeString returns a user-visible string for the module's type. + * @return the type string. + */ + QString typeString() const; + + /** + * @brief interface returns a user-visible string for the module's interface. + * @return the interface string. + */ + QString interfaceString() const; + + /** + * @brief loadSelf initialized the module. + * Subclasses must reimplement this depending on the module type and interface. + */ + virtual void loadSelf() = 0; + + /** + * @brief jobs returns any jobs exposed by this module. + * @return a list of jobs (can be empty). + */ + virtual JobList jobs() const = 0; + + /** + * @brief type returns the Type of this module object. + * @return the type enum value. + */ + virtual Type type() const = 0; + + /** + * @brief interface the Interface used by this module. + * @return the interface enum value. + */ + virtual Interface interface() const = 0; + /** * @brief Check the requirements of this module. */ @@ -175,7 +176,12 @@ public: protected: explicit Module(); - virtual void initFrom( const QVariantMap& moduleDescriptor ); + + /// @brief For subclasses to read their part of the descriptor + virtual void initFrom( const QVariantMap& moduleDescriptor ) = 0; + /// @brief Generic part of descriptor reading (and instance id) + void initFrom( const QVariantMap& moduleDescriptor, const QString& id ); + QVariantMap m_configurationMap; bool m_loaded = false; @@ -185,9 +191,8 @@ protected: private: void loadConfigurationFile( const QString& configFileName ); //throws YAML::Exception - QString m_name; QString m_directory; - QString m_instanceId; + ModuleSystem::InstanceKey m_key; }; } // namespace Calamares diff --git a/src/libcalamaresui/modulesystem/ProcessJobModule.cpp b/src/libcalamaresui/modulesystem/ProcessJobModule.cpp index fc4b5f254..405d9efd8 100644 --- a/src/libcalamaresui/modulesystem/ProcessJobModule.cpp +++ b/src/libcalamaresui/modulesystem/ProcessJobModule.cpp @@ -63,7 +63,6 @@ ProcessJobModule::jobs() const void ProcessJobModule::initFrom( const QVariantMap& moduleDescriptor ) { - Module::initFrom( moduleDescriptor ); QDir directory( location() ); m_workingPath = directory.absolutePath(); diff --git a/src/libcalamaresui/modulesystem/PythonJobModule.cpp b/src/libcalamaresui/modulesystem/PythonJobModule.cpp index 46ec1dff1..c9e90a707 100644 --- a/src/libcalamaresui/modulesystem/PythonJobModule.cpp +++ b/src/libcalamaresui/modulesystem/PythonJobModule.cpp @@ -64,7 +64,6 @@ PythonJobModule::jobs() const void PythonJobModule::initFrom( const QVariantMap& moduleDescriptor ) { - Module::initFrom( moduleDescriptor ); QDir directory( location() ); m_workingPath = directory.absolutePath(); diff --git a/src/libcalamaresui/modulesystem/PythonQtViewModule.cpp b/src/libcalamaresui/modulesystem/PythonQtViewModule.cpp index 08e4c5c08..b59126eaa 100644 --- a/src/libcalamaresui/modulesystem/PythonQtViewModule.cpp +++ b/src/libcalamaresui/modulesystem/PythonQtViewModule.cpp @@ -174,7 +174,6 @@ PythonQtViewModule::jobs() const void PythonQtViewModule::initFrom( const QVariantMap& moduleDescriptor ) { - Module::initFrom( moduleDescriptor ); QDir directory( location() ); m_workingPath = directory.absolutePath(); diff --git a/src/libcalamaresui/modulesystem/ViewModule.cpp b/src/libcalamaresui/modulesystem/ViewModule.cpp index 7506e5348..02c771ee2 100644 --- a/src/libcalamaresui/modulesystem/ViewModule.cpp +++ b/src/libcalamaresui/modulesystem/ViewModule.cpp @@ -91,7 +91,6 @@ ViewModule::jobs() const void ViewModule::initFrom( const QVariantMap& moduleDescriptor ) { - Module::initFrom( moduleDescriptor ); QDir directory( location() ); QString load; if ( !moduleDescriptor.value( "load" ).toString().isEmpty() )