From ac287a0ac5b1f6cdbf2706663082aa3b3f66661f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 23 May 2018 08:53:11 -0400 Subject: [PATCH] [libcalamares] Add a save() method to global storage - This is a quick way to dump GS to JSON, which is useful for the preservefiles module #928 - Also useful for, e.g., #466 --- src/libcalamares/GlobalStorage.cpp | 18 +++++++++++++++++- src/libcalamares/GlobalStorage.h | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index fd72697cf..4f98ea2eb 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2015, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot + * Copyright 2017-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 @@ -22,6 +22,9 @@ #include "utils/Logger.h" +#include +#include + #ifdef WITH_PYTHON #include "PythonHelper.h" @@ -94,6 +97,19 @@ GlobalStorage::debugDump() const } } +bool +GlobalStorage::save(const QString& filename) +{ + QFile f( filename ); + if ( !f.open( QFile::WriteOnly ) ) + return false; + + f.write( QJsonDocument::fromVariant( m ).toJson() ) ; + f.close(); + return true; +} + + } // namespace Calamares #ifdef WITH_PYTHON diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index dc79c55e8..72524ba4f 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -1,7 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2015, Teo Mrnjavac - * Copyright 2017, Adriaan de Groot + * Copyright 2017-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 @@ -58,7 +58,16 @@ public: int remove( const QString& key ); QVariant value( const QString& key ) const; + /// @brief dump keys and values to the debug log void debugDump() const; + /** @brief write as JSON to the given filename + * + * No tidying, sanitization, or censoring is done -- for instance, + * the user module sets a slightly-obscured password in global storage, + * and this JSON file will contain that password in-the-only-slightly- + * obscured form. + */ + bool save( const QString& filename ); signals: void changed();