diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index 8baabac9a..2814b02a0 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -27,6 +27,7 @@ set( utilsSources utils/Logger.cpp utils/PluginFactory.cpp utils/Retranslator.cpp + utils/Variant.cpp utils/YamlUtils.cpp ) set( kdsagSources diff --git a/src/libcalamares/utils/CalamaresUtils.cpp b/src/libcalamares/utils/CalamaresUtils.cpp index 0ff6810c1..6d8862269 100644 --- a/src/libcalamares/utils/CalamaresUtils.cpp +++ b/src/libcalamares/utils/CalamaresUtils.cpp @@ -388,77 +388,4 @@ 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; -} - -QString -getString(const QVariantMap& map, const QString& key) -{ - if ( map.contains( key ) ) - { - auto v = map.value( key ); - if ( v.type() == QVariant::String ) - return v.toString(); - } - return QString(); -} - -int -getInteger( const QVariantMap& map, const QString& key, int d ) -{ - int result = d; - if ( map.contains( key ) ) - { - auto v = map.value( key ); - if ( v.type() == QVariant::Int ) - result = v.toInt(); - } - - return result; -} - -double -getDouble( const QVariantMap& map, const QString& key, double d ) -{ - double result = d; - if ( map.contains( key ) ) - { - auto v = map.value( key ); - if ( v.type() == QVariant::Int ) - result = v.toInt(); - else if ( v.type() == QVariant::Double ) - result = v.toDouble(); - } - - 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 baf7a12dc..2eb202b2c 100644 --- a/src/libcalamares/utils/CalamaresUtils.h +++ b/src/libcalamares/utils/CalamaresUtils.h @@ -108,37 +108,6 @@ 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 ); - - /** - * Get a string value from a mapping; returns empty QString if no value. - */ - DLLEXPORT QString getString( const QVariantMap& map, const QString& key ); - - /** - * Get an integer value from a mapping; returns @p d if no value. - */ - DLLEXPORT int getInteger( const QVariantMap& map, const QString& key, int d ); - - /** - * Get a double value from a mapping (integers are converted); returns @p d if no value. - */ - DLLEXPORT double getDouble( const QVariantMap& map, const QString& key, double 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 diff --git a/src/libcalamares/utils/CommandList.cpp b/src/libcalamares/utils/CommandList.cpp index 6a9d68bef..9916c71fe 100644 --- a/src/libcalamares/utils/CommandList.cpp +++ b/src/libcalamares/utils/CommandList.cpp @@ -21,9 +21,10 @@ #include "GlobalStorage.h" #include "JobQueue.h" -#include "utils/CalamaresUtils.h" +// #include "utils/CalamaresUtils.h" #include "utils/CalamaresUtilsSystem.h" #include "utils/Logger.h" +#include "utils/Variant.h" #include #include diff --git a/src/libcalamares/utils/Variant.cpp b/src/libcalamares/utils/Variant.cpp new file mode 100644 index 000000000..f11853916 --- /dev/null +++ b/src/libcalamares/utils/Variant.cpp @@ -0,0 +1,107 @@ +/* === This file is part of Calamares - === + * + * Copyright 2013-2016, Teo Mrnjavac + * Copyright 2018, Adriaan de Groot + * + * Originally from Tomahawk, portions: + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi + * Copyright 2010-2012, Jeff Mitchell + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Variant.h" + +#include "Logger.h" + +#include +#include + +namespace CalamaresUtils +{ +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; +} + +QString +getString(const QVariantMap& map, const QString& key) +{ + if ( map.contains( key ) ) + { + auto v = map.value( key ); + if ( v.type() == QVariant::String ) + return v.toString(); + } + return QString(); +} + +int +getInteger( const QVariantMap& map, const QString& key, int d ) +{ + int result = d; + if ( map.contains( key ) ) + { + auto v = map.value( key ); + if ( v.type() == QVariant::Int ) + result = v.toInt(); + } + + return result; +} + +double +getDouble( const QVariantMap& map, const QString& key, double d ) +{ + double result = d; + if ( map.contains( key ) ) + { + auto v = map.value( key ); + if ( v.type() == QVariant::Int ) + result = v.toInt(); + else if ( v.type() == QVariant::Double ) + result = v.toDouble(); + } + + 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/Variant.h b/src/libcalamares/utils/Variant.h new file mode 100644 index 000000000..e60eccb4e --- /dev/null +++ b/src/libcalamares/utils/Variant.h @@ -0,0 +1,62 @@ +/* === This file is part of Calamares - === + * + * Copyright 2013-2016, Teo Mrnjavac + * Copyright 2018, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef UTILS_VARIANT_H +#define UTILS_VARIANT_H + +#include "DllMacro.h" + +#include +#include + +namespace CalamaresUtils +{ + /** + * 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 ); + + /** + * Get a string value from a mapping; returns empty QString if no value. + */ + DLLEXPORT QString getString( const QVariantMap& map, const QString& key ); + + /** + * Get an integer value from a mapping; returns @p d if no value. + */ + DLLEXPORT int getInteger( const QVariantMap& map, const QString& key, int d ); + + /** + * Get a double value from a mapping (integers are converted); returns @p d if no value. + */ + DLLEXPORT double getDouble( const QVariantMap& map, const QString& key, double 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 ); +} // namespace + +#endif