1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
4  *   SPDX-License-Identifier: GPL-3.0-or-later
5  *
6  *   Calamares is Free Software: see the License-Identifier above.
7  *
8  */
9 
10 #include "PackageChooserViewStep.h"
11 
12 #include "Config.h"
13 #include "PackageChooserPage.h"
14 #include "PackageModel.h"
15 
16 #include "GlobalStorage.h"
17 #include "JobQueue.h"
18 #include "locale/TranslatableConfiguration.h"
19 #include "utils/CalamaresUtilsSystem.h"
20 #include "utils/Logger.h"
21 #include "utils/Variant.h"
22 
23 #include <QDesktopServices>
24 #include <QVariantMap>
25 
CALAMARES_PLUGIN_FACTORY_DEFINITION(PackageChooserViewStepFactory,registerPlugin<PackageChooserViewStep> ();)26 CALAMARES_PLUGIN_FACTORY_DEFINITION( PackageChooserViewStepFactory, registerPlugin< PackageChooserViewStep >(); )
27 
28 PackageChooserViewStep::PackageChooserViewStep( QObject* parent )
29     : Calamares::ViewStep( parent )
30     , m_config( new Config( this ) )
31     , m_widget( nullptr )
32     , m_stepName( nullptr )
33 {
34     emit nextStatusChanged( false );
35 }
36 
37 
~PackageChooserViewStep()38 PackageChooserViewStep::~PackageChooserViewStep()
39 {
40     if ( m_widget && m_widget->parent() == nullptr )
41     {
42         m_widget->deleteLater();
43     }
44     delete m_stepName;
45 }
46 
47 
48 QString
prettyName() const49 PackageChooserViewStep::prettyName() const
50 {
51     return m_stepName ? m_stepName->get() : tr( "Packages" );
52 }
53 
54 
55 QWidget*
widget()56 PackageChooserViewStep::widget()
57 {
58     if ( !m_widget )
59     {
60         m_widget = new PackageChooserPage( m_config->mode(), nullptr );
61         connect( m_widget, &PackageChooserPage::selectionChanged, [=]() {
62             emit nextStatusChanged( this->isNextEnabled() );
63         } );
64         hookupModel();
65     }
66     return m_widget;
67 }
68 
69 
70 bool
isNextEnabled() const71 PackageChooserViewStep::isNextEnabled() const
72 {
73     if ( !m_widget )
74     {
75         // No way to have changed anything
76         return true;
77     }
78 
79     switch ( m_config->mode() )
80     {
81     case PackageChooserMode::Optional:
82     case PackageChooserMode::OptionalMultiple:
83         // zero or one OR zero or more
84         return true;
85     case PackageChooserMode::Required:
86     case PackageChooserMode::RequiredMultiple:
87         // exactly one OR one or more
88         return m_widget->hasSelection();
89     }
90     __builtin_unreachable();
91 }
92 
93 
94 bool
isBackEnabled() const95 PackageChooserViewStep::isBackEnabled() const
96 {
97     return true;
98 }
99 
100 
101 bool
isAtBeginning() const102 PackageChooserViewStep::isAtBeginning() const
103 {
104     return true;
105 }
106 
107 
108 bool
isAtEnd() const109 PackageChooserViewStep::isAtEnd() const
110 {
111     return true;
112 }
113 
114 void
onActivate()115 PackageChooserViewStep::onActivate()
116 {
117     if ( !m_widget->hasSelection() )
118     {
119         m_widget->setSelection( m_config->defaultSelectionIndex() );
120     }
121 }
122 
123 void
onLeave()124 PackageChooserViewStep::onLeave()
125 {
126     m_config->updateGlobalStorage( m_widget->selectedPackageIds() );
127 }
128 
129 Calamares::JobList
jobs() const130 PackageChooserViewStep::jobs() const
131 {
132     Calamares::JobList l;
133     return l;
134 }
135 
136 void
setConfigurationMap(const QVariantMap & configurationMap)137 PackageChooserViewStep::setConfigurationMap( const QVariantMap& configurationMap )
138 {
139     m_config->setDefaultId( moduleInstanceKey() );
140     m_config->setConfigurationMap( configurationMap );
141 
142     bool labels_ok = false;
143     auto labels = CalamaresUtils::getSubMap( configurationMap, "labels", labels_ok );
144     if ( labels_ok )
145     {
146         if ( labels.contains( "step" ) )
147         {
148             m_stepName = new CalamaresUtils::Locale::TranslatedString( labels, "step" );
149         }
150     }
151 
152     if ( m_widget )
153     {
154         hookupModel();
155     }
156 }
157 
158 
159 void
hookupModel()160 PackageChooserViewStep::hookupModel()
161 {
162     if ( !m_config->model() || !m_widget )
163     {
164         cError() << "Can't hook up model until widget and model both exist.";
165         return;
166     }
167 
168     m_widget->setModel( m_config->model() );
169     m_widget->setIntroduction( m_config->introductionPackage() );
170 }
171