source: calamares/trunk/fuentes/src/modules/dummycpp/DummyCppJob.cpp @ 7538

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

sync with github

File size: 4.7 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Teo Mrnjavac <teo@kde.org> (original dummypython code)
4 *   Copyright 2016, Kevin Kofler <kevin.kofler@chello.at>
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 "DummyCppJob.h"
21
22#include <QProcess>
23#include <QDateTime>
24#include <QThread>
25
26#include "CalamaresVersion.h"
27#include "JobQueue.h"
28#include "GlobalStorage.h"
29
30#include "utils/Logger.h"
31
32DummyCppJob::DummyCppJob( QObject* parent )
33    : Calamares::CppJob( parent )
34{
35}
36
37
38DummyCppJob::~DummyCppJob()
39{
40}
41
42
43QString
44DummyCppJob::prettyName() const
45{
46    return tr( "Dummy C++ Job" );
47}
48
49
50static QString variantListToString( const QVariantList& variantList );
51static QString variantMapToString( const QVariantMap& variantMap );
52static QString variantHashToString( const QVariantHash& variantHash );
53
54
55static QString
56variantToString( const QVariant& variant )
57{
58    switch ( variant.type() )
59    {
60    case QVariant::Map:
61        return variantMapToString( variant.toMap() );
62
63    case QVariant::Hash:
64        return variantHashToString( variant.toHash() );
65
66    case QVariant::List:
67    case QVariant::StringList:
68        return variantListToString( variant.toList() );
69
70    default:
71        return variant.toString();
72    }
73}
74
75
76static QString
77variantListToString( const QVariantList& variantList )
78{
79    QStringList result;
80    for ( const QVariant& variant : variantList )
81        result.append( variantToString( variant ) );
82    return '{' + result.join(',') + '}';
83}
84
85
86static QString
87variantMapToString( const QVariantMap& variantMap )
88{
89    QStringList result;
90    for ( auto it = variantMap.constBegin(); it != variantMap.constEnd(); ++it )
91        result.append( it.key() + '=' + variantToString( it.value() ) );
92    return '[' + result.join(',') + ']';
93}
94
95
96static QString
97variantHashToString( const QVariantHash& variantHash )
98{
99    QStringList result;
100    for ( auto it = variantHash.constBegin(); it != variantHash.constEnd(); ++it )
101        result.append( it.key() + '=' + variantToString( it.value() ) );
102    return '<' + result.join(',') + '>';
103}
104
105
106Calamares::JobResult
107DummyCppJob::exec()
108{
109    // Ported from dummypython
110    QProcess::execute( "/bin/sh", QStringList() << "-c" << "touch ~/calamares-dummycpp" );
111    QString accumulator = QDateTime::currentDateTimeUtc().toString( Qt::ISODate ) + '\n';
112    accumulator += QStringLiteral( "Calamares version: " ) + CALAMARES_VERSION_SHORT + '\n';
113    accumulator += QStringLiteral( "This job's name: " ) + prettyName() + '\n';
114    accumulator += QStringLiteral( "Configuration map: %1\n" ).arg( variantMapToString( m_configurationMap ) );
115    accumulator += QStringLiteral( "   *** globalstorage test ***\n" );
116    Calamares::GlobalStorage *globalStorage = Calamares::JobQueue::instance()->globalStorage();
117    accumulator += QStringLiteral( "lala: " ) + (globalStorage->contains( "lala" ) ? QStringLiteral( "true" ) : QStringLiteral( "false" )) + '\n';
118    accumulator += QStringLiteral( "foo: " ) + (globalStorage->contains( "foo" ) ? QStringLiteral( "true" ) : QStringLiteral( "false" )) + '\n';
119    accumulator += QStringLiteral( "count: " ) + QString::number( globalStorage->count() ) + '\n';
120    globalStorage->insert( "item2", "value2" );
121    globalStorage->insert( "item3", 3 );
122    accumulator += QStringLiteral( "keys: %1\n" ).arg( globalStorage->keys().join( ',' ) );
123    accumulator += QStringLiteral( "remove: %1\n" ).arg( QString::number( globalStorage->remove( "item2" ) ) );
124    accumulator += QStringLiteral( "values: %1 %2 %3\n" ).arg(
125        globalStorage->value( "foo" ).toString(),
126        globalStorage->value( "item2" ).toString(),
127        globalStorage->value( "item3" ).toString() );
128
129    emit progress( 0.1 );
130    cDebug() << "[DUMMYCPP]: " << accumulator;
131
132    globalStorage->debugDump();
133    emit progress( 0.5 );
134
135    QThread::sleep( 3 );
136
137    return Calamares::JobResult::ok();
138}
139
140
141void
142DummyCppJob::setConfigurationMap( const QVariantMap& configurationMap )
143{
144    m_configurationMap = configurationMap;
145}
146
147CALAMARES_PLUGIN_FACTORY_DEFINITION( DummyCppJobFactory, registerPlugin<DummyCppJob>(); )
Note: See TracBrowser for help on using the repository browser.