diff --git a/src/libcalamares/utils/CalamaresUtils.cpp b/src/libcalamares/utils/CalamaresUtils.cpp index db748ec94..ae3a14497 100644 --- a/src/libcalamares/utils/CalamaresUtils.cpp +++ b/src/libcalamares/utils/CalamaresUtils.cpp @@ -326,5 +326,35 @@ crash() *a = 1; } +bool +getBool( const QVariantMap& map, const QString& key, bool d ) +{ + bool result = d; + if ( map.contains( key ) ) + { + auto v = map.value( key ); + if ( v.type() == QVariant::Bool ) + result = v.toBool(); + } + + return result; +} + +QVariantMap +getSubMap( const QVariantMap& map, const QString& key, bool& success ) +{ + success = false; + + if ( map.contains( key ) ) + { + auto v = map.value( key ); + if ( v.type() == QVariant::Map ) + { + success = true; + return v.toMap(); + } + } + return QVariantMap(); +} } diff --git a/src/libcalamares/utils/CalamaresUtils.h b/src/libcalamares/utils/CalamaresUtils.h index 66650ff57..651c6746a 100644 --- a/src/libcalamares/utils/CalamaresUtils.h +++ b/src/libcalamares/utils/CalamaresUtils.h @@ -97,6 +97,22 @@ namespace CalamaresUtils * @brief crash makes Calamares crash immediately. */ DLLEXPORT void crash(); + + /** + * Get a bool value from a mapping with a given key; returns the default + * if no value is stored in the map. + */ + DLLEXPORT bool getBool( const QVariantMap& map, const QString& key, bool d ); + + /** + * Returns a sub-map (i.e. a nested map) from the given mapping with the + * given key. @p success is set to true if the @p key exists + * in @p map and converts to a map, false otherwise. + * + * Returns an empty map if there is no such key or it is not a map-value. + * (e.g. if @p success is false). + */ + DLLEXPORT QVariantMap getSubMap( const QVariantMap& map, const QString& key, bool& success ); } #endif // CALAMARESUTILS_H