From d9f2f5e988ba8f0692106bab7f3e07a641376d38 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 12 Mar 2021 13:16:36 +0100 Subject: [PATCH] [libcalamares] Start a 'presets' configuration datastructure --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/modulesystem/Preset.cpp | 49 ++++++++++++++++++++ src/libcalamares/modulesystem/Preset.h | 57 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/libcalamares/modulesystem/Preset.cpp create mode 100644 src/libcalamares/modulesystem/Preset.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 700eff37b..bd24726bc 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -51,6 +51,7 @@ set( libSources modulesystem/Descriptor.cpp modulesystem/InstanceKey.cpp modulesystem/Module.cpp + modulesystem/Preset.cpp modulesystem/RequirementsChecker.cpp modulesystem/RequirementsModel.cpp diff --git a/src/libcalamares/modulesystem/Preset.cpp b/src/libcalamares/modulesystem/Preset.cpp new file mode 100644 index 000000000..8240e23e7 --- /dev/null +++ b/src/libcalamares/modulesystem/Preset.cpp @@ -0,0 +1,49 @@ +/* === 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 "Preset.h" + +#include "utils/Logger.h" +#include "utils/Variant.h" + +static void +loadPresets( Calamares::ModuleSystem::Presets& preset, + const QVariantMap& configurationMap, + std::function< bool( const QString& ) > pred ) +{ + cDebug() << "Creating presets" << preset.capacity(); + for ( auto it = configurationMap.cbegin(); it != configurationMap.cend(); ++it ) + { + if ( !it.key().isEmpty() && pred( it.key() ) ) + { + QVariantMap m = it.value().toMap(); + QString value = CalamaresUtils::getString( m, "value" ); + bool editable = CalamaresUtils::getBool( m, "editable", true ); + + preset.append( Calamares::ModuleSystem::PresetField { it.key(), value, editable } ); + + cDebug() << Logger::SubEntry << "Preset for" << it.key() << "applied editable?" << editable; + } + } +} + +Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap ) + + +{ + reserve( configurationMap.count() ); + loadPresets( *this, configurationMap, []( const QString& ) { return true; } ); +} + +Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ) +{ + reserve( recognizedKeys.size() ); + loadPresets( + *this, configurationMap, [&recognizedKeys]( const QString& s ) { return recognizedKeys.contains( s ); } ); +} diff --git a/src/libcalamares/modulesystem/Preset.h b/src/libcalamares/modulesystem/Preset.h new file mode 100644 index 000000000..1744313ae --- /dev/null +++ b/src/libcalamares/modulesystem/Preset.h @@ -0,0 +1,57 @@ +/* === 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_PRESET_H +#define CALAMARES_MODULESYSTEM_PRESET_H + +#include +#include +#include + +namespace Calamares +{ +namespace ModuleSystem +{ +/** @brief The settings for a single field + * + * The settings apply to a single field; **often** this will + * correspond to a single value or property of a Config + * object, but there is no guarantee of a correspondence + * between names here and names in the code. + * + * The value is stored as a string; consumers (e.g. the UI) + * will need to translate the value to whatever is actually + * used (e.g. in the case of an integer field). + * + * By default, presets are still editable. Set that to @c false + * to make the field unchangeable (again, the UI is responsible + * for setting that up). + */ +struct PresetField +{ + QString fieldName; + QString value; + bool editable = true; +}; + +/** @brief All the presets for one UI entity + * + * This is a collection of presets read from a module + * configuration file, one setting per field. + */ +class Presets : public QVector< PresetField > +{ +public: + explicit Presets( const QVariantMap& configurationMap ); + Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ); +}; +} // namespace ModuleSystem +} // namespace Calamares + +#endif