[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.
This commit is contained in:
Adriaan de Groot 2021-03-12 14:49:46 +01:00
parent 448e478b6d
commit 8b10a9cfc2
4 changed files with 68 additions and 2 deletions

View File

@ -60,5 +60,20 @@ Config::loadPresets( const QVariantMap& configurationMap, const QStringList& rec
= std::make_unique< Presets >( CalamaresUtils::getSubMap( configurationMap, key, bogus ), recognizedKeys ); = 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 ModuleSystem
} // namespace Calamares } // namespace Calamares

View File

@ -34,6 +34,7 @@ namespace ModuleSystem
*/ */
class DLLEXPORT Config : public QObject class DLLEXPORT Config : public QObject
{ {
Q_OBJECT
public: public:
Config( QObject* parent = nullptr ); Config( QObject* parent = nullptr );
~Config() override; ~Config() override;
@ -46,6 +47,16 @@ public:
*/ */
virtual void setConfigurationMap( const QVariantMap& ) = 0; 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: protected:
void loadPresets( const QVariantMap& configurationMap ); void loadPresets( const QVariantMap& configurationMap );
void loadPresets( const QVariantMap& configurationMap, const QStringList& recognizedKeys ); void loadPresets( const QVariantMap& configurationMap, const QStringList& recognizedKeys );
@ -53,6 +64,7 @@ protected:
private: private:
class Private; class Private;
std::unique_ptr< Private > d; std::unique_ptr< Private > d;
bool m_unlocked = false;
}; };
} // namespace ModuleSystem } // namespace ModuleSystem
} // namespace Calamares } // namespace Calamares

View File

@ -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; } ); 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() ); reserve( recognizedKeys.size() );
loadPresets( loadPresets(
*this, configurationMap, [&recognizedKeys]( const QString& s ) { return recognizedKeys.contains( s ); } ); *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

View File

@ -48,8 +48,26 @@ struct PresetField
class Presets : public QVector< PresetField > class Presets : public QVector< PresetField >
{ {
public: 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 ); 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 ); 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 ModuleSystem
} // namespace Calamares } // namespace Calamares