source: calamares/trunk/fuentes/src/modules/contextualprocess/ContextualProcessJob.cpp @ 7538

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

sync with github

File size: 5.5 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "ContextualProcessJob.h"
20
21#include <QProcess>
22#include <QDateTime>
23#include <QThread>
24
25#include "CalamaresVersion.h"
26#include "JobQueue.h"
27#include "GlobalStorage.h"
28
29#include "utils/CalamaresUtils.h"
30#include "utils/CommandList.h"
31#include "utils/Logger.h"
32
33struct ValueCheck : public QPair<QString, CalamaresUtils::CommandList*>
34{
35    ValueCheck( const QString& value, CalamaresUtils::CommandList* commands )
36        : QPair<QString, CalamaresUtils::CommandList*>(value, commands)
37    {
38    }
39
40    // ~ValueCheck()
41    //
42    // There is no destructor.
43    //
44    // We don't own the commandlist, the binding holding this valuecheck
45    // does, so don't delete. This is closely tied to (temporaries created
46    // by) pass-by-value in QList::append().
47
48    QString value() const { return first; }
49    CalamaresUtils::CommandList* commands() const { return second; }
50} ;
51
52struct ContextualProcessBinding
53{
54    ContextualProcessBinding( const QString& varname )
55        : variable( varname )
56    {
57    }
58
59    ~ContextualProcessBinding();
60
61    /**
62     * @brief add commands to be executed when @p value is matched.
63     *
64     * Ownership of the CommandList passes to this binding.
65     */
66    void append( const QString& value, CalamaresUtils::CommandList* commands )
67    {
68        checks.append( ValueCheck( value, commands ) );
69        if ( value == QString( "*" ) )
70            wildcard = commands;
71    }
72
73    Calamares::JobResult run( const QString& value ) const
74    {
75        for ( const auto& c : checks )
76        {
77            if ( value == c.value() )
78                return c.commands()->run();
79        }
80
81        if ( wildcard )
82            return wildcard->run();
83
84        return Calamares::JobResult::ok();
85    }
86
87    QString variable;
88    QList<ValueCheck> checks;
89    CalamaresUtils::CommandList* wildcard{ nullptr };
90} ;
91
92
93ContextualProcessBinding::~ContextualProcessBinding()
94{
95    wildcard = nullptr;
96    for ( const auto& c : checks )
97    {
98        delete c.commands();
99    }
100}
101
102ContextualProcessJob::ContextualProcessJob( QObject* parent )
103    : Calamares::CppJob( parent )
104{
105}
106
107
108ContextualProcessJob::~ContextualProcessJob()
109{
110    qDeleteAll( m_commands );
111}
112
113
114QString
115ContextualProcessJob::prettyName() const
116{
117    return tr( "Contextual Processes Job" );
118}
119
120
121Calamares::JobResult
122ContextualProcessJob::exec()
123{
124    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
125
126    for ( const ContextualProcessBinding* binding : m_commands )
127    {
128        if ( gs->contains( binding->variable ) )
129        {
130            Calamares::JobResult r = binding->run( gs->value( binding->variable ).toString() );
131            if ( !r )
132                return r;
133        }
134        else
135            cWarning() << "ContextualProcess checks for unknown variable" << binding->variable;
136    }
137    return Calamares::JobResult::ok();
138}
139
140
141void
142ContextualProcessJob::setConfigurationMap( const QVariantMap& configurationMap )
143{
144    bool dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false );
145    int timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 );
146    if ( timeout < 1 )
147        timeout = 10;
148
149    for ( QVariantMap::const_iterator iter = configurationMap.cbegin(); iter != configurationMap.cend(); ++iter )
150    {
151        QString variableName = iter.key();
152        if ( variableName.isEmpty() || ( variableName == "dontChroot" ) || ( variableName == "timeout" ) )
153            continue;
154
155        if ( iter.value().type() != QVariant::Map )
156        {
157            cWarning() << moduleInstanceKey() << "bad configuration values for" << variableName;
158            continue;
159        }
160
161        auto binding = new ContextualProcessBinding( variableName );
162        m_commands.append( binding );
163        QVariantMap values = iter.value().toMap();
164        for ( QVariantMap::const_iterator valueiter = values.cbegin(); valueiter != values.cend(); ++valueiter )
165        {
166            QString valueString = valueiter.key();
167            if ( variableName.isEmpty() )
168            {
169                cWarning() << moduleInstanceKey() << "variable" << variableName << "unrecognized value" << valueiter.key();
170                continue;
171            }
172
173            CalamaresUtils::CommandList* commands = new CalamaresUtils::CommandList( valueiter.value(), !dontChroot, timeout );
174
175            binding->append( valueString, commands );
176        }
177    }
178}
179
180int
181ContextualProcessJob::count()
182{
183    return m_commands.count();
184}
185
186int
187ContextualProcessJob::count(const QString& variableName)
188{
189    for ( const ContextualProcessBinding* binding : m_commands )
190        if ( binding->variable == variableName )
191            return binding->checks.count();
192    return -1;
193}
194
195CALAMARES_PLUGIN_FACTORY_DEFINITION( ContextualProcessJobFactory, registerPlugin<ContextualProcessJob>(); )
Note: See TracBrowser for help on using the repository browser.