summaryrefslogtreecommitdiff
path: root/src/libcalamares/GlobalStorage.cpp
blob: ea09dabcbc9b7023f57e79e18dcdbb65e32a2809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* === This file is part of Calamares - <http://github.com/calamares> ===
 *
 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
 *
 *   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"
#include "JobQueue.h"

#include "utils/Logger.h"

#ifdef WITH_PYTHON
#include "PythonHelper.h"


#undef slots
#include <boost/python/list.hpp>
#include <boost/python/str.hpp>

namespace bp = boost::python;
#endif

namespace Calamares {

GlobalStorage::GlobalStorage()
    : QObject( nullptr )
{
}


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 );
    emit changed();
}


QStringList
GlobalStorage::keys() const
{
    return m.keys();
}


int
GlobalStorage::remove( const QString& key )
{
    int nItems = m.remove( key );
    emit changed();
    return nItems;
}


QVariant
GlobalStorage::value( const QString& key ) const
{
    return m.value( key );
}

void
GlobalStorage::debugDump() const
{
    for ( auto it = m.cbegin(); it != m.cend(); ++it )
    {
        cDebug() << it.key() << '\t' << it.value();
    }
}

} // namespace Calamares

#ifdef WITH_PYTHON

namespace CalamaresPython
{

Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr;

// 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.
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
    : m_gs( gs ? gs : s_gs_instance )
{
    if (!m_gs)
    {
        s_gs_instance = new Calamares::GlobalStorage;
        m_gs = s_gs_instance;
    }
}

bool
GlobalStoragePythonWrapper::contains( const std::string& key ) const
{
    return m_gs->contains( QString::fromStdString( key ) );
}


int
GlobalStoragePythonWrapper::count() const
{
    return m_gs->count();
}


void
GlobalStoragePythonWrapper::insert( const std::string& key,
                       const bp::object& value )
{
    m_gs->insert( QString::fromStdString( key ),
                CalamaresPython::variantFromPyObject( value ) );
}

bp::list
GlobalStoragePythonWrapper::keys() const
{
    bp::list pyList;
    const auto keys = m_gs->keys();
    for ( const QString& key : keys )
        pyList.append( key.toStdString() );
    return pyList;
}


int
GlobalStoragePythonWrapper::remove( const std::string& key )
{
    return m_gs->remove( QString::fromStdString( key ) );
}


bp::object
GlobalStoragePythonWrapper::value( const std::string& key ) const
{
    return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
}

} // namespace CalamaresPython


const QString GS::HAS_ISOREPO_KEY = "has-isorepo";
const QString GS::IS_ONLINE_KEY = "hasInternet";
const QString GS::DESKTOP_PACKAGES_KEY = "default-desktop" ;
const QString GS::PARTITIONS_KEY = "partitions";
const QString GS::DEVICE_KEY = "device";
const QString GS::FS_KEY = "fs";
const QString GS::MOUNTPOINT_KEY = "mountPoint";
const QString GS::UUID_KEY = "uuid";


#endif // WITH_PYTHON