From c19ce26e5ff78dea9db76b19679a7814e355ae22 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 2 Jul 2018 06:11:57 -0400 Subject: [PATCH] [libcalamares] Expand GlobalStorage with load() - Counterpart to save(), for JSON-style dumps --- src/libcalamares/GlobalStorage.cpp | 27 +++++++++++++++++++++++++++ src/libcalamares/GlobalStorage.h | 8 ++++++++ 2 files changed, 35 insertions(+) diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index 73236a311..99be39246 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -22,6 +22,7 @@ #include "utils/Logger.h" #include "utils/Yaml.h" +#include "utils/Units.h" #include #include @@ -37,6 +38,8 @@ namespace bp = boost::python; #endif +using CalamaresUtils::operator""_MiB; + namespace Calamares { GlobalStorage::GlobalStorage() @@ -110,6 +113,30 @@ GlobalStorage::save(const QString& filename) return true; } +bool +GlobalStorage::load( const QString& filename ) +{ + QFile f( filename ); + if ( !f.open( QFile::ReadOnly ) ) + return false; + + QJsonParseError e; + QJsonDocument d = QJsonDocument::fromJson( f.read( 1_MiB ), &e ); + if ( d.isNull() ) + cWarning() << filename << e.errorString(); + else if ( !d.isObject() ) + cWarning() << filename << "Not suitable JSON."; + else + { + auto map = d.toVariant().toMap(); + for( auto i = map.constBegin() ; i != map.constEnd() ; ++i ) + { + insert( i.key(), *i ); + } + return true; + } + return false; +} bool GlobalStorage::saveYaml( const QString& filename ) diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index 2c31a8f47..89ebb5050 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -70,6 +70,14 @@ public: */ bool save( const QString& filename ); + /** @brief Adds the keys from the given JSON file + * + * No tidying, sanitization, or censoring is done. + * The JSON file is read and each key is added as a value to + * the global storage. + */ + bool load( const QString& filename ); + /** @brief write as YAML to the given filename * * See also save(), above.