From 0be5e04c2e8655f2965f26f4892fa4f07516e4aa Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 12 Mar 2021 13:49:37 +0100 Subject: [PATCH] [libcalamares] Add a base class for Config-objects This is an optional (until 3.3) base class, which can handle Presets consistently for configurations. --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/modulesystem/Config.cpp | 64 ++++++++++++++++++++++++ src/libcalamares/modulesystem/Config.h | 60 ++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/libcalamares/modulesystem/Config.cpp create mode 100644 src/libcalamares/modulesystem/Config.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index bd24726bc..826f0bc41 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -48,6 +48,7 @@ set( libSources locale/TranslatableString.cpp # Modules + modulesystem/Config.cpp modulesystem/Descriptor.cpp modulesystem/InstanceKey.cpp modulesystem/Module.cpp diff --git a/src/libcalamares/modulesystem/Config.cpp b/src/libcalamares/modulesystem/Config.cpp new file mode 100644 index 000000000..75c54badf --- /dev/null +++ b/src/libcalamares/modulesystem/Config.cpp @@ -0,0 +1,64 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#include "Config.h" + +#include "Preset.h" +#include "utils/Variant.h" + +namespace Calamares +{ +namespace ModuleSystem +{ + + +class Config::Private +{ +public: + std::unique_ptr< Presets > m_presets; +}; + + +Config::Config( QObject* parent ) + : QObject( parent ) + , d( std::make_unique< Private >() ) +{ +} + +Config::~Config() {} + +void +Config::loadPresets( const QVariantMap& configurationMap ) +{ + const QString key( "presets" ); + if ( !configurationMap.contains( key ) ) + { + d->m_presets.reset(); + return; + } + bool bogus = true; + d->m_presets = std::make_unique< Presets >( CalamaresUtils::getSubMap( configurationMap, key, bogus ) ); +} + +void +Config::loadPresets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ) +{ + const QString key( "presets" ); + if ( !configurationMap.contains( key ) ) + { + d->m_presets.reset(); + return; + } + bool bogus = true; + d->m_presets + = std::make_unique< Presets >( CalamaresUtils::getSubMap( configurationMap, key, bogus ), recognizedKeys ); +} + +} // namespace ModuleSystem +} // namespace Calamares diff --git a/src/libcalamares/modulesystem/Config.h b/src/libcalamares/modulesystem/Config.h new file mode 100644 index 000000000..9bc0705c6 --- /dev/null +++ b/src/libcalamares/modulesystem/Config.h @@ -0,0 +1,60 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2021 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ + +#ifndef CALAMARES_MODULESYSTEM_CONFIG_H +#define CALAMARES_MODULESYSTEM_CONFIG_H + +#include "DllMacro.h" + +#include +#include +#include + +#include + +namespace Calamares +{ +namespace ModuleSystem +{ +/** @brief Base class for Config-objects + * + * This centralizes the things every Config-object should + * do and provides one source of preset-data. A Config-object + * for a module can **optionally** inherit from this class + * to get presets-support. + * + * TODO:3.3 This is not optional + * TODO:3.3 Put consistent i18n for Configurations in here too + */ +class DLLEXPORT Config : public QObject +{ +public: + Config( QObject* parent = nullptr ); + ~Config() override; + + /** @brief Set the configuration from the config file + * + * Subclasses must implement this to load configuration data; + * that subclass **should** also call loadPresets() with the + * same map, to pick up the "presets" key consistently. + */ + virtual void setConfigurationMap( const QVariantMap& ) = 0; + +protected: + void loadPresets( const QVariantMap& configurationMap ); + void loadPresets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ); + +private: + class Private; + std::unique_ptr< Private > d; +}; +} // namespace ModuleSystem +} // namespace Calamares + +#endif