calamares/src/libcalamares/GlobalStorage.cpp

193 lines
3.8 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
*
[libcalamares] Update SPDX identifiers. Update CppJob.h Update CalamaresConfig.h.in Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.cpp Update CalamaresConfig.h.in Update CppJob.cpp Update CppJob.h Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.h Update ProcessJob.cpp Update ProcessJob.h Update PythonHelper.cpp Update PythonJob.cpp Update PythonJob.h Update PythonHelper.h Update PythonJobApi.cpp Update PythonJobApi.h Update Settings.cpp Update Settings.h Update GeoIPJSON.cpp Update GeoIPJSON.h Update GeoIPTests.cpp Update GeoIPTests.h Update GeoIPXML.cpp Update GeoIPXML.h Update Handler.cpp Update Handler.h Update Interface.cpp Update Interface.h Update test_geoip.cpp Update CountryData_p.cpp Update Label.cpp Update Label.h Update LabelModel.cpp Update LabelModel.h Update CountryData_p.cpp Update CountryData_p.cpp Update Lookup.cpp Update Lookup.h Update Tests.cpp Update Tests.h Update TimeZone.cpp Update TimeZone.h Update TranslatableConfiguration.cpp Update TranslatableConfiguration.h Update ZoneData_p.cxxtr Update cldr-extractor.py Update zone-extractor.py Update Actions.h Update Actions.h Update Descriptor.h Update InstanceKey.cpp Update Module.cpp Update Module.h Update Requirement.cpp Update RequirementsChecker.h Update RequirementsModel.cpp Update RequirementsModel.h Update Tests.cpp Update Manager.cpp Update Manager.h Update Tests.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update KPMTests.cpp Update FileSystem.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update Mount.cpp Update Mount.h Update PartitionIterator.cpp Update PartitionIterator.h Update PartitionIterator.h Update PartitionQuery.cpp Update PartitionQuery.h Update PartitionSize.cpp Update PartitionSize.h Update Sync.cpp Update Sync.h Update Tests.cpp Update Tests.h Update BoostPython.h Update CalamaresUtilsSystem.cpp Update CalamaresUtilsSystem.h Update CommandList.cpp Update CommandList.h Update Dirs.cpp Update Dirs.h Update Entropy.cpp Update Entropy.h Update Entropy.cpp Update Logger.cpp Update Logger.h Update NamedEnum.h Update NamedSuffix.h Update PluginFactory.cpp Update PluginFactory.h Update RAII.h Update RAII.h Update Retranslator.cpp Update Retranslator.h Update String.cpp Update String.h Update TestPaths.cpp Update Tests.cpp Update Tests.h Update UMask.cpp Update UMask.h Update Units.h Update Variant.cpp Update Variant.h Update Yaml.cpp Update Yaml.h Update moc-warnings.h
2020-05-30 16:15:03 +02:00
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
[libcalamares] Update SPDX identifiers. Update CppJob.h Update CalamaresConfig.h.in Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.cpp Update CalamaresConfig.h.in Update CppJob.cpp Update CppJob.h Update DllMacro.h Update GlobalStorage.cpp Update GlobalStorage.h Update Job.cpp Update Job.h Update JobExample.cpp Update JobExample.h Update JobQueue.h Update ProcessJob.cpp Update ProcessJob.h Update PythonHelper.cpp Update PythonJob.cpp Update PythonJob.h Update PythonHelper.h Update PythonJobApi.cpp Update PythonJobApi.h Update Settings.cpp Update Settings.h Update GeoIPJSON.cpp Update GeoIPJSON.h Update GeoIPTests.cpp Update GeoIPTests.h Update GeoIPXML.cpp Update GeoIPXML.h Update Handler.cpp Update Handler.h Update Interface.cpp Update Interface.h Update test_geoip.cpp Update CountryData_p.cpp Update Label.cpp Update Label.h Update LabelModel.cpp Update LabelModel.h Update CountryData_p.cpp Update CountryData_p.cpp Update Lookup.cpp Update Lookup.h Update Tests.cpp Update Tests.h Update TimeZone.cpp Update TimeZone.h Update TranslatableConfiguration.cpp Update TranslatableConfiguration.h Update ZoneData_p.cxxtr Update cldr-extractor.py Update zone-extractor.py Update Actions.h Update Actions.h Update Descriptor.h Update InstanceKey.cpp Update Module.cpp Update Module.h Update Requirement.cpp Update RequirementsChecker.h Update RequirementsModel.cpp Update RequirementsModel.h Update Tests.cpp Update Manager.cpp Update Manager.h Update Tests.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update KPMTests.cpp Update FileSystem.cpp Update FileSystem.cpp Update FileSystem.h Update KPMManager.cpp Update KPMManager.h Update Mount.cpp Update Mount.h Update PartitionIterator.cpp Update PartitionIterator.h Update PartitionIterator.h Update PartitionQuery.cpp Update PartitionQuery.h Update PartitionSize.cpp Update PartitionSize.h Update Sync.cpp Update Sync.h Update Tests.cpp Update Tests.h Update BoostPython.h Update CalamaresUtilsSystem.cpp Update CalamaresUtilsSystem.h Update CommandList.cpp Update CommandList.h Update Dirs.cpp Update Dirs.h Update Entropy.cpp Update Entropy.h Update Entropy.cpp Update Logger.cpp Update Logger.h Update NamedEnum.h Update NamedSuffix.h Update PluginFactory.cpp Update PluginFactory.h Update RAII.h Update RAII.h Update Retranslator.cpp Update Retranslator.h Update String.cpp Update String.h Update TestPaths.cpp Update Tests.cpp Update Tests.h Update UMask.cpp Update UMask.h Update Units.h Update Variant.cpp Update Variant.h Update Yaml.cpp Update Yaml.h Update moc-warnings.h
2020-05-30 16:15:03 +02:00
*
*/
#include "GlobalStorage.h"
#include "utils/Logger.h"
#include "utils/Units.h"
#include "utils/Yaml.h"
#include <QFile>
#include <QJsonDocument>
#include <QMutexLocker>
using namespace CalamaresUtils::Units;
namespace Calamares
{
class GlobalStorage::ReadLock : public QMutexLocker
{
public:
ReadLock( const GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
{
}
};
class GlobalStorage::WriteLock : public QMutexLocker
{
public:
WriteLock( GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
, m_gs( gs )
{
}
~WriteLock() { m_gs->changed(); }
GlobalStorage* m_gs;
};
GlobalStorage::GlobalStorage( QObject* parent )
: QObject( parent )
{
}
bool
GlobalStorage::contains( const QString& key ) const
{
ReadLock l( this );
return m.contains( key );
}
int
GlobalStorage::count() const
{
ReadLock l( this );
return m.count();
}
void
GlobalStorage::insert( const QString& key, const QVariant& value )
{
WriteLock l( this );
m.insert( key, value );
}
QStringList
GlobalStorage::keys() const
{
ReadLock l( this );
return m.keys();
}
int
GlobalStorage::remove( const QString& key )
{
WriteLock l( this );
int nItems = m.remove( key );
return nItems;
}
QVariant
GlobalStorage::value( const QString& key ) const
{
ReadLock l( this );
return m.value( key );
}
void
GlobalStorage::debugDump() const
{
ReadLock l( this );
cDebug() << "GlobalStorage" << Logger::Pointer( this ) << m.count() << "items";
for ( auto it = m.cbegin(); it != m.cend(); ++it )
{
cDebug() << Logger::SubEntry << it.key() << '\t' << it.value();
}
}
bool
GlobalStorage::saveJson( const QString& filename ) const
{
ReadLock l( this );
QFile f( filename );
if ( !f.open( QFile::WriteOnly ) )
{
return false;
}
f.write( QJsonDocument::fromVariant( m ).toJson() );
f.close();
return true;
}
bool
GlobalStorage::loadJson( 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
{
WriteLock l( this );
// Do **not** use method insert() here, because it would
// recursively lock the mutex, leading to deadlock. Also,
// that would emit changed() for each key.
auto map = d.toVariant().toMap();
for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{
m.insert( i.key(), *i );
}
return true;
}
return false;
}
bool
GlobalStorage::saveYaml( const QString& filename ) const
{
ReadLock l( this );
return CalamaresUtils::saveYaml( filename, m );
}
bool
GlobalStorage::loadYaml( const QString& filename )
{
bool ok = false;
auto map = CalamaresUtils::loadYaml( filename, &ok );
if ( ok )
{
WriteLock l( this );
// Do **not** use method insert() here, because it would
// recursively lock the mutex, leading to deadlock. Also,
// that would emit changed() for each key.
for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{
m.insert( i.key(), *i );
}
return true;
}
return false;
}
} // namespace Calamares