diff --git a/src/libcalamaresui/modulesystem/ModuleManager.cpp b/src/libcalamaresui/modulesystem/ModuleManager.cpp index a65f64108..487cb47c4 100644 --- a/src/libcalamaresui/modulesystem/ModuleManager.cpp +++ b/src/libcalamaresui/modulesystem/ModuleManager.cpp @@ -137,7 +137,12 @@ ModuleManager::doInit() QStringList ModuleManager::loadedInstanceKeys() { - return m_loadedModulesByInstanceKey.keys(); + QStringList l; + for ( const auto& m : m_loadedModulesByInstanceKey.keys() ) + { + l << QString( m ); + } + return l; } @@ -150,7 +155,7 @@ ModuleManager::moduleDescriptor( const QString& name ) Module* ModuleManager::moduleInstance( const QString& instanceKey ) { - return m_loadedModulesByInstanceKey.value( instanceKey ); + return m_loadedModulesByInstanceKey.value( ModuleInstanceKey::fromString( instanceKey ) ); } @@ -160,12 +165,12 @@ ModuleManager::moduleInstance( const QString& instanceKey ) * @return -1 on failure, otherwise index of the instance that matches. */ static int -findCustomInstance( const Settings::InstanceDescriptionList& customInstances, const QString& module, const QString& id ) +findCustomInstance( const Settings::InstanceDescriptionList& customInstances, const ModuleInstanceKey& m ) { for ( int i = 0; i < customInstances.count(); ++i ) { const auto& thisInstance = customInstances[ i ]; - if ( thisInstance.value( "module" ) == module && thisInstance.value( "id" ) == id ) + if ( thisInstance.value( "module" ) == m.module() && thisInstance.value( "id" ) == m.id() ) { return i; } @@ -189,32 +194,28 @@ ModuleManager::loadModules() foreach ( const QString& moduleEntry, modulePhase.second ) { - QStringList moduleEntrySplit = moduleEntry.split( '@' ); - QString moduleName; - QString instanceId; - QString configFileName; - if ( moduleEntrySplit.length() < 1 || moduleEntrySplit.length() > 2 ) + auto instanceKey = ModuleInstanceKey::fromString( moduleEntry ); + if ( !instanceKey.isValid() ) { cError() << "Wrong module entry format for module" << moduleEntry; failedModules.append( moduleEntry ); continue; } - moduleName = moduleEntrySplit.first(); - instanceId = moduleEntrySplit.last(); - configFileName = QString( "%1.conf" ).arg( moduleName ); - if ( !m_availableDescriptorsByModuleName.contains( moduleName ) - || m_availableDescriptorsByModuleName.value( moduleName ).isEmpty() ) + + if ( !m_availableDescriptorsByModuleName.contains( instanceKey.module() ) + || m_availableDescriptorsByModuleName.value( instanceKey.module() ).isEmpty() ) { - cError() << "Module" << moduleName << "not found in module search paths." + cError() << "Module" << QString( instanceKey ) << "not found in module search paths." << Logger::DebugList( m_paths ); - failedModules.append( moduleName ); + failedModules.append( QString( instanceKey ) ); continue; } - if ( moduleName != instanceId ) //means this is a custom instance + QString configFileName; + if ( instanceKey.isCustom() ) { - int found = findCustomInstance( customInstances, moduleName, instanceId ); + int found = findCustomInstance( customInstances, instanceKey ); if ( found > -1 ) { @@ -227,6 +228,10 @@ ModuleManager::loadModules() continue; } } + else + { + configFileName = QString( "%1.conf" ).arg( instanceKey.module() ); + } // So now we can assume that the module entry is at least valid, // that we have a descriptor on hand (and therefore that the @@ -235,38 +240,35 @@ ModuleManager::loadModules() // We still don't know whether the config file for the entry // exists and is valid, but that's the only thing that could fail // from this point on. -- Teo 8/2015 - - QString instanceKey = QString( "%1@%2" ).arg( moduleName ).arg( instanceId ); - Module* thisModule = m_loadedModulesByInstanceKey.value( instanceKey, nullptr ); if ( thisModule && !thisModule->isLoaded() ) { - cError() << "Module" << instanceKey << "exists but not loaded."; - failedModules.append( instanceKey ); + cError() << "Module" << QString( instanceKey ) << "exists but not loaded."; + failedModules.append( QString( instanceKey ) ); continue; } if ( thisModule && thisModule->isLoaded() ) { - cDebug() << "Module" << instanceKey << "already loaded."; + cDebug() << "Module" << QString( instanceKey ) << "already loaded."; } else { - thisModule = Module::fromDescriptor( m_availableDescriptorsByModuleName.value( moduleName ), - instanceId, + thisModule = Module::fromDescriptor( m_availableDescriptorsByModuleName.value( instanceKey.module() ), + instanceKey.id(), configFileName, - m_moduleDirectoriesByModuleName.value( moduleName ) ); + m_moduleDirectoriesByModuleName.value( instanceKey.module() ) ); if ( !thisModule ) { - cError() << "Module" << instanceKey << "cannot be created from descriptor" << configFileName; - failedModules.append( instanceKey ); + cError() << "Module" << QString( instanceKey ) << "cannot be created from descriptor" << configFileName; + failedModules.append( QString( instanceKey ) ); continue; } if ( !checkDependencies( *thisModule ) ) { // Error message is already printed - failedModules.append( instanceKey ); + failedModules.append( QString( instanceKey ) ); continue; } @@ -275,8 +277,8 @@ ModuleManager::loadModules() m_loadedModulesByInstanceKey.insert( instanceKey, thisModule ); if ( !thisModule->isLoaded() ) { - cError() << "Module" << instanceKey << "loading FAILED."; - failedModules.append( instanceKey ); + cError() << "Module" << QString( instanceKey ) << "loading FAILED."; + failedModules.append( QString( instanceKey ) ); continue; } } @@ -293,7 +295,7 @@ ModuleManager::loadModules() ViewManager::instance()->addViewStep( evs ); } - evs->appendJobModuleInstanceKey( instanceKey ); + evs->appendJobModuleInstanceKey( QString( instanceKey ) ); } } } diff --git a/src/libcalamaresui/modulesystem/ModuleManager.h b/src/libcalamaresui/modulesystem/ModuleManager.h index 3a7535de4..ed314c7e0 100644 --- a/src/libcalamaresui/modulesystem/ModuleManager.h +++ b/src/libcalamaresui/modulesystem/ModuleManager.h @@ -45,25 +45,54 @@ struct RequirementEntry; // from Requirement.h * This is supported by the *instances* configuration entry * in `settings.conf`. */ -class ModuleInstanceKey : protected QPair< QString, QString > +class ModuleInstanceKey : public QPair< QString, QString > { public: /// @brief Create an instance key from explicit module and id. ModuleInstanceKey( const QString& module, const QString& id ) : QPair( module, id ) { + if ( second.isEmpty() ) + { + second = first; + } } /// @brief Create "usual" instances keys `module@module` - ModuleInstanceKey( const QString& module ) + explicit ModuleInstanceKey( const QString& module ) : QPair( module, module ) { } + /// @brief Create unusual, invalid instance key + ModuleInstanceKey() + : QPair( QString(), QString() ) + { + } + + /// @brief A valid module has both name and id + bool isValid() const { return !first.isEmpty() && !second.isEmpty(); } + + /// @brief A custom module has a non-default id + bool isCustom() const { return first != second; } + QString module() const { return first; } QString id() const { return second; } explicit operator QString() const { return module() + '@' + id(); } + + /// @brief Create instance key from stringified version + static ModuleInstanceKey fromString( const QString& s ) + { + QStringList moduleEntrySplit = s.split( '@' ); + if ( moduleEntrySplit.length() < 1 || moduleEntrySplit.length() > 2 ) + { + return ModuleInstanceKey(); + } + // For length 1, first == last + return ModuleInstanceKey( moduleEntrySplit.first(), moduleEntrySplit.last() ); + } + }; @@ -158,7 +187,7 @@ private: QMap< QString, QVariantMap > m_availableDescriptorsByModuleName; QMap< QString, QString > m_moduleDirectoriesByModuleName; - QMap< QString, Module* > m_loadedModulesByInstanceKey; + QMap< ModuleInstanceKey, Module* > m_loadedModulesByInstanceKey; const QStringList m_paths; static ModuleManager* s_instance;