[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.
main
Adriaan de Groot 4 years ago
parent 448e478b6d
commit 8b10a9cfc2

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save