source: calamares/trunk/fuentes/src/libcalamares/GlobalStorage.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.8 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "GlobalStorage.h"
21#include "JobQueue.h"
22
23#include "utils/Logger.h"
24
25#include <QFile>
26#include <QJsonDocument>
27
28#ifdef WITH_PYTHON
29#include "PythonHelper.h"
30
31
32#undef slots
33#include <boost/python/list.hpp>
34#include <boost/python/str.hpp>
35
36namespace bp = boost::python;
37#endif
38
39namespace Calamares {
40
41GlobalStorage::GlobalStorage()
42    : QObject( nullptr )
43{
44}
45
46
47bool
48GlobalStorage::contains( const QString& key ) const
49{
50    return m.contains( key );
51}
52
53
54int
55GlobalStorage::count() const
56{
57    return m.count();
58}
59
60
61void
62GlobalStorage::insert( const QString& key, const QVariant& value )
63{
64    m.insert( key, value );
65    emit changed();
66}
67
68
69QStringList
70GlobalStorage::keys() const
71{
72    return m.keys();
73}
74
75
76int
77GlobalStorage::remove( const QString& key )
78{
79    int nItems = m.remove( key );
80    emit changed();
81    return nItems;
82}
83
84
85QVariant
86GlobalStorage::value( const QString& key ) const
87{
88    return m.value( key );
89}
90
91void
92GlobalStorage::debugDump() const
93{
94    for ( auto it = m.cbegin(); it != m.cend(); ++it )
95    {
96        cDebug() << it.key() << '\t' << it.value();
97    }
98}
99
100bool
101GlobalStorage::save(const QString& filename)
102{
103    QFile f( filename );
104    if ( !f.open( QFile::WriteOnly ) )
105        return false;
106
107    f.write( QJsonDocument::fromVariant( m ).toJson() ) ;
108    f.close();
109    return true;
110}
111
112
113} // namespace Calamares
114
115#ifdef WITH_PYTHON
116
117namespace CalamaresPython
118{
119
120Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr;
121
122// The special handling for nullptr is only for the testing
123// script for the python bindings, which passes in None;
124// normal use will have a GlobalStorage from JobQueue::instance()
125// passed in. Testing use will leak the allocated GlobalStorage
126// object, but that's OK for testing.
127GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
128    : m_gs( gs ? gs : s_gs_instance )
129{
130    if (!m_gs)
131    {
132        s_gs_instance = new Calamares::GlobalStorage;
133        m_gs = s_gs_instance;
134    }
135}
136
137bool
138GlobalStoragePythonWrapper::contains( const std::string& key ) const
139{
140    return m_gs->contains( QString::fromStdString( key ) );
141}
142
143
144int
145GlobalStoragePythonWrapper::count() const
146{
147    return m_gs->count();
148}
149
150
151void
152GlobalStoragePythonWrapper::insert( const std::string& key,
153                       const bp::object& value )
154{
155    m_gs->insert( QString::fromStdString( key ),
156                CalamaresPython::variantFromPyObject( value ) );
157}
158
159bp::list
160GlobalStoragePythonWrapper::keys() const
161{
162    bp::list pyList;
163    const auto keys = m_gs->keys();
164    for ( const QString& key : keys )
165        pyList.append( key.toStdString() );
166    return pyList;
167}
168
169
170int
171GlobalStoragePythonWrapper::remove( const std::string& key )
172{
173    return m_gs->remove( QString::fromStdString( key ) );
174}
175
176
177bp::object
178GlobalStoragePythonWrapper::value( const std::string& key ) const
179{
180    return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
181}
182
183} // namespace CalamaresPython
184
185#endif // WITH_PYTHON
Note: See TracBrowser for help on using the repository browser.