From 8b10a9cfc2ba97cd3ee0ff39426113f030a3da4d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 12 Mar 2021 14:49:46 +0100 Subject: [PATCH] [libcalamares] Add isEditable() check This adds support for checking whether a field is editable; Config objects should reject changes if the field is not editable. There is an "unlock" setting to override the check, although this is currently always locked. --- src/libcalamares/modulesystem/Config.cpp | 15 ++++++++++++++ src/libcalamares/modulesystem/Config.h | 12 ++++++++++++ src/libcalamares/modulesystem/Preset.cpp | 25 ++++++++++++++++++++++-- src/libcalamares/modulesystem/Preset.h | 18 +++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/libcalamares/modulesystem/Config.cpp b/src/libcalamares/modulesystem/Config.cpp index 75c54badf..701fbec74 100644 --- a/src/libcalamares/modulesystem/Config.cpp +++ b/src/libcalamares/modulesystem/Config.cpp @@ -60,5 +60,20 @@ Config::loadPresets( const QVariantMap& configurationMap, const QStringList& rec = std::make_unique< Presets >( CalamaresUtils::getSubMap( configurationMap, key, bogus ), recognizedKeys ); } +bool +Config::isEditable( const QString& fieldName ) const +{ + if ( m_unlocked ) + { + return true; + } + if ( d && d->m_presets ) + { + return d->m_presets->isEditable( fieldName ); + } + return true; +} + + } // namespace ModuleSystem } // namespace Calamares diff --git a/src/libcalamares/modulesystem/Config.h b/src/libcalamares/modulesystem/Config.h index 9bc0705c6..d0f5fa51f 100644 --- a/src/libcalamares/modulesystem/Config.h +++ b/src/libcalamares/modulesystem/Config.h @@ -34,6 +34,7 @@ namespace ModuleSystem */ class DLLEXPORT Config : public QObject { + Q_OBJECT public: Config( QObject* parent = nullptr ); ~Config() override; @@ -46,6 +47,16 @@ public: */ virtual void setConfigurationMap( const QVariantMap& ) = 0; +public Q_SLOTS: + /** @brief Checks if a @p fieldName is editable according to presets + * + * If the field is named as a preset, **and** the field is set + * to not-editable, returns @c false. Otherwise, return @c true. + * Calling this with an unknown field (one for which no presets + * are accepted) will print a warning and return @c true. + */ + bool isEditable( const QString& fieldName ) const; + protected: void loadPresets( const QVariantMap& configurationMap ); void loadPresets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ); @@ -53,6 +64,7 @@ protected: private: class Private; std::unique_ptr< Private > d; + bool m_unlocked = false; }; } // namespace ModuleSystem } // namespace Calamares diff --git a/src/libcalamares/modulesystem/Preset.cpp b/src/libcalamares/modulesystem/Preset.cpp index 8240e23e7..9685a6b68 100644 --- a/src/libcalamares/modulesystem/Preset.cpp +++ b/src/libcalamares/modulesystem/Preset.cpp @@ -33,7 +33,11 @@ loadPresets( Calamares::ModuleSystem::Presets& preset, } } -Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap ) +namespace Calamares +{ +namespace ModuleSystem +{ +Presets::Presets( const QVariantMap& configurationMap ) { @@ -41,9 +45,26 @@ Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap ) loadPresets( *this, configurationMap, []( const QString& ) { return true; } ); } -Calamares::ModuleSystem::Presets::Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ) +Presets::Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ) { reserve( recognizedKeys.size() ); loadPresets( *this, configurationMap, [&recognizedKeys]( const QString& s ) { return recognizedKeys.contains( s ); } ); } + +bool +Presets::isEditable( const QString& fieldName ) const +{ + for ( const auto& p : *this ) + { + if ( p.fieldName == fieldName ) + { + return p.editable; + } + } + return true; +} + + +} // namespace ModuleSystem +} // namespace Calamares diff --git a/src/libcalamares/modulesystem/Preset.h b/src/libcalamares/modulesystem/Preset.h index 1744313ae..59c308b92 100644 --- a/src/libcalamares/modulesystem/Preset.h +++ b/src/libcalamares/modulesystem/Preset.h @@ -48,8 +48,26 @@ struct PresetField class Presets : public QVector< PresetField > { public: + /** @brief Reads preset entries from the map + * + * The map's keys are used as field name, and each value entry + * should specify an initial value and whether the entry is editable. + * Fields are editable by default. + */ explicit Presets( const QVariantMap& configurationMap ); + /** @brief Reads preset entries from the @p configurationMap + * + * As above, but only field names that occur in @p recognizedKeys + * are kept; others are discarded. + */ Presets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ); + + /** @brief Is the given @p fieldName editable? + * + * Fields are editable by default, so if there is no explicit setting, + * returns @c true. + */ + bool isEditable( const QString& fieldName ) const; }; } // namespace ModuleSystem } // namespace Calamares