2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-07-21 17:08:06 +02:00
|
|
|
*
|
2015-03-05 19:35:26 +01:00
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2018-05-23 14:53:11 +02:00
|
|
|
* Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
|
2014-07-21 17:08:06 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GlobalStorage.h"
|
2017-08-15 13:49:43 +02:00
|
|
|
#include "JobQueue.h"
|
2014-07-21 17:08:06 +02:00
|
|
|
|
2017-06-06 11:46:12 +02:00
|
|
|
#include "utils/Logger.h"
|
2019-04-29 12:04:55 +02:00
|
|
|
#include "utils/Yaml.h"
|
2018-07-02 12:11:57 +02:00
|
|
|
#include "utils/Units.h"
|
2017-06-06 11:46:12 +02:00
|
|
|
|
2018-05-23 14:53:11 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
#include "PythonHelper.h"
|
|
|
|
|
2017-08-15 13:49:43 +02:00
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
#undef slots
|
|
|
|
#include <boost/python/list.hpp>
|
|
|
|
#include <boost/python/str.hpp>
|
|
|
|
|
|
|
|
namespace bp = boost::python;
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 12:11:57 +02:00
|
|
|
using CalamaresUtils::operator""_MiB;
|
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
namespace Calamares {
|
|
|
|
|
|
|
|
GlobalStorage::GlobalStorage()
|
2015-03-05 19:35:26 +01:00
|
|
|
: QObject( nullptr )
|
2014-07-21 17:08:06 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
GlobalStorage::contains( const QString& key ) const
|
|
|
|
{
|
|
|
|
return m.contains( key );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
GlobalStorage::count() const
|
|
|
|
{
|
|
|
|
return m.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
GlobalStorage::insert( const QString& key, const QVariant& value )
|
|
|
|
{
|
|
|
|
m.insert( key, value );
|
2015-03-05 19:35:26 +01:00
|
|
|
emit changed();
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QStringList
|
|
|
|
GlobalStorage::keys() const
|
|
|
|
{
|
|
|
|
return m.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
GlobalStorage::remove( const QString& key )
|
|
|
|
{
|
2015-06-13 21:30:25 +02:00
|
|
|
int nItems = m.remove( key );
|
2015-03-05 19:35:26 +01:00
|
|
|
emit changed();
|
2015-06-13 21:30:25 +02:00
|
|
|
return nItems;
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
GlobalStorage::value( const QString& key ) const
|
|
|
|
{
|
|
|
|
return m.value( key );
|
|
|
|
}
|
|
|
|
|
2017-06-06 11:46:12 +02:00
|
|
|
void
|
|
|
|
GlobalStorage::debugDump() const
|
|
|
|
{
|
|
|
|
for ( auto it = m.cbegin(); it != m.cend(); ++it )
|
|
|
|
{
|
|
|
|
cDebug() << it.key() << '\t' << it.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 14:53:11 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-02 12:11:57 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-05-23 14:53:11 +02:00
|
|
|
|
2019-01-28 13:50:30 +01:00
|
|
|
bool
|
|
|
|
GlobalStorage::saveYaml( const QString& filename )
|
|
|
|
{
|
|
|
|
return CalamaresUtils::saveYaml( filename, m );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GlobalStorage::loadYaml( const QString& filename )
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
auto gs = CalamaresUtils::loadYaml( filename, &ok );
|
|
|
|
if ( ok )
|
|
|
|
m = gs;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-05 19:35:26 +01:00
|
|
|
} // namespace Calamares
|
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
|
2015-03-05 19:35:26 +01:00
|
|
|
namespace CalamaresPython
|
|
|
|
{
|
|
|
|
|
2017-08-15 22:50:26 +02:00
|
|
|
Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr;
|
|
|
|
|
2017-08-15 13:49:43 +02:00
|
|
|
// The special handling for nullptr is only for the testing
|
|
|
|
// script for the python bindings, which passes in None;
|
|
|
|
// normal use will have a GlobalStorage from JobQueue::instance()
|
|
|
|
// passed in. Testing use will leak the allocated GlobalStorage
|
|
|
|
// object, but that's OK for testing.
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
|
2017-08-15 22:50:26 +02:00
|
|
|
: m_gs( gs ? gs : s_gs_instance )
|
|
|
|
{
|
|
|
|
if (!m_gs)
|
|
|
|
{
|
|
|
|
s_gs_instance = new Calamares::GlobalStorage;
|
|
|
|
m_gs = s_gs_instance;
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 19:35:26 +01:00
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
bool
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::contains( const std::string& key ) const
|
2014-07-21 17:08:06 +02:00
|
|
|
{
|
2015-03-05 19:35:26 +01:00
|
|
|
return m_gs->contains( QString::fromStdString( key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
GlobalStoragePythonWrapper::count() const
|
|
|
|
{
|
|
|
|
return m_gs->count();
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::insert( const std::string& key,
|
2014-07-21 17:08:06 +02:00
|
|
|
const bp::object& value )
|
|
|
|
{
|
2015-03-05 19:35:26 +01:00
|
|
|
m_gs->insert( QString::fromStdString( key ),
|
|
|
|
CalamaresPython::variantFromPyObject( value ) );
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bp::list
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::keys() const
|
2014-07-21 17:08:06 +02:00
|
|
|
{
|
|
|
|
bp::list pyList;
|
2016-09-01 14:21:05 +02:00
|
|
|
const auto keys = m_gs->keys();
|
|
|
|
for ( const QString& key : keys )
|
2014-07-21 17:08:06 +02:00
|
|
|
pyList.append( key.toStdString() );
|
|
|
|
return pyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::remove( const std::string& key )
|
2014-07-21 17:08:06 +02:00
|
|
|
{
|
2015-03-05 19:35:26 +01:00
|
|
|
return m_gs->remove( QString::fromStdString( key ) );
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bp::object
|
2015-03-05 19:35:26 +01:00
|
|
|
GlobalStoragePythonWrapper::value( const std::string& key ) const
|
2014-07-21 17:08:06 +02:00
|
|
|
{
|
2015-03-05 19:35:26 +01:00
|
|
|
return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
|
2014-07-21 17:08:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-05 19:35:26 +01:00
|
|
|
} // namespace CalamaresPython
|
2014-07-21 17:08:06 +02:00
|
|
|
|
2015-03-05 19:35:26 +01:00
|
|
|
#endif // WITH_PYTHON
|