summaryrefslogtreecommitdiff
path: root/src/modules/netinstall/NetInstallPage.cpp
blob: 77cc2d8305cea3f24c2f871f2c7c1bf2d690d5f7 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *   Copyright 2016, Luca Giambonini <almack@chakraos.org>
 *   Copyright 2016, Lisa Vitolo     <shainer@chakraos.org>
 *   Copyright 2017, Kyle Robbertze  <krobbertze@gmail.com>
 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
 *   Copyright 2017, Gabriel Craciunescu <crazy@frugalware.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 "NetInstallPage.h"

#include "PackageModel.h"

#include "ui_page_netinst.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "ViewManager.h" // TODO: remove this

#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "utils/YamlUtils.h"

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

#include <QHeaderView>

#include <yaml-cpp/yaml.h>

using CalamaresUtils::yamlToVariant;


const char NetInstallPage::INIT_COMBO_LABEL_TEXT[] = "Select Init System";
const char NetInstallPage::WMDE_COMBO_LABEL_TEXT[] = "Select Graphical Environment";
const char NetInstallPage::NETINSTALL_CHECK_TEXT[] = "NetInstall";


NetInstallPage::NetInstallPage( QWidget* parent )
    : QWidget( parent )
    , ui( new Ui::Page_NetInst )
    , m_networkManager( this )
    , m_groups( nullptr )
{
    ui->setupUi( this );

    ui->initLabel->setText( tr( INIT_COMBO_LABEL_TEXT ) );
    ui->wmDeLabel->setText( tr( WMDE_COMBO_LABEL_TEXT ) );
    ui->initCombobox->setToolTip( tr( INIT_COMBO_LABEL_TEXT ) );
    ui->wmDeCombobox->setToolTip( tr( WMDE_COMBO_LABEL_TEXT ) );
    ui->netinstallCheckbox->setText( tr( NETINSTALL_CHECK_TEXT ) );
    ui->netinstallCheckbox->setChecked( false ); // deferred to onActivate();
    ui->netinstallCheckbox->setEnabled( false ); // deferred to onActivate();
    ui->groupswidget->setHeaderHidden(true);

connect(this, &NetInstallPage::skipThis,
        Calamares::ViewManager::instance(), &Calamares::ViewManager::next); // TODO: remove this
}

bool
NetInstallPage::readGroups( const QByteArray& yamlData )
{
    try
    {
        YAML::Node groups = YAML::Load( yamlData.constData() );

        if ( !groups.IsSequence() )
            cWarning() << "netinstall groups data does not form a sequence.";
        Q_ASSERT( groups.IsSequence() );
        m_groups = new PackageModel( groups );
        CALAMARES_RETRANSLATE(
            m_groups->setHeaderData( 0, Qt::Horizontal, tr( "Name" ) );
            m_groups->setHeaderData( 1, Qt::Horizontal, tr( "Description" ) ); )
        return true;

    }
    catch ( YAML::Exception& e )
    {
        CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
        return false;
    }
}

void
NetInstallPage::dataIsHere( QNetworkReply* reply )
{
    bool is_valid_package_data = false;

    // If m_required is *false* then we still say we're ready
    // even if the reply is corrupt or missing.
    if ( reply->error() != QNetworkReply::NoError )
    {
        cWarning() << "unable to fetch netinstall package lists.";
        cDebug() << "  ..Netinstall reply error: " << reply->error();
        cDebug() << "  ..Request for url: " << reply->url().toString() << " failed with: " << reply->errorString();
        ui->netinst_status->setText( tr( "Network Installation. (Disabled: Unable to fetch package lists, check your network connection)" ) );
    }
    else
    {
        // Parse received YAML to create the PackageModel
        is_valid_package_data = readGroups( reply->readAll() );

        if ( !is_valid_package_data )
        {
            cWarning() << "netinstall groups data was received, but invalid.";
            cDebug() << "  ..Url:     " <<  reply->url().toString();
            cDebug() << "  ..Headers: " <<  reply->rawHeaderList();
            ui->netinst_status->setText( tr( "Network Installation. (Disabled: Received invalid groups data)" ) );
        }

        reply->deleteLater();
    }

    populateGroupsWidget( is_valid_package_data );
}

void
NetInstallPage::parseGroupList( const QVariantList& package_groups )
{
    // Convert netinstall.conf packages lists to YAML
    YAML::Emitter package_groups_yaml;
    package_groups_yaml.SetOutputCharset(YAML::EscapeNonAscii);
    package_groups_yaml << YAML::BeginSeq;
    for ( int groups_n = 0; groups_n < package_groups.length(); ++groups_n )
    {
        QVariantMap package_group = package_groups.value(groups_n).toMap();
        std::string group_name = package_group["name"].toString().toStdString();
        std::string group_desc = package_group["description"].toString().toStdString();
        std::string group_critical = package_group["critical"].toString().toStdString();
        QStringList group_packages = package_group.value( "packages" ).toStringList();
        std::vector< std::string > packages;
        foreach ( const QString& package , group_packages )
            packages.push_back( package.toStdString() );

cDebug() << "NetInstallPage::parseGroupList() package_group[name]=" << QString::fromStdString(group_name);
cDebug() << "NetInstallPage::parseGroupList() package_group[description]=" << QString::fromStdString(group_desc);
cDebug() << "NetInstallPage::parseGroupList() package_group[critical]=" << QString::fromStdString(group_critical);
cDebug() << "NetInstallPage::parseGroupList() package_group[packages]=" << group_packages;

        package_groups_yaml << YAML::BeginMap;
        package_groups_yaml << YAML::Key << "name"        << YAML::Value << group_name;
        package_groups_yaml << YAML::Key << "description" << YAML::Value << group_desc;
        package_groups_yaml << YAML::Key << "critical"    << YAML::Value << group_critical;
        package_groups_yaml << YAML::Key << "packages"    << YAML::Value << packages;
        package_groups_yaml << YAML::EndMap;
    }
    package_groups_yaml << YAML::EndSeq;

// DBG << "NetInstallPage::parseGroupList() err=" << QString(package_groups_yaml.GetLastError());

    // Parse generated YAML to create the PackageModel
    bool is_valid_package_data = readGroups( QByteArray( package_groups_yaml.c_str() ) );

    if ( !is_valid_package_data )
        ui->netinst_status->setText( tr( "Package Selection. (Disabled: Invalid groups data)" ) );

    populateGroupsWidget( is_valid_package_data );
}

void
NetInstallPage::populateGroupsWidget( bool is_valid_package_data )
{
    if ( is_valid_package_data )
    {
        ui->groupswidget->setModel( m_groups );
        ui->groupswidget->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
        ui->groupswidget->header()->setSectionResizeMode( 1, QHeaderView::Stretch );
    }

    emit checkReady( is_valid_package_data || !m_required );
}

void
NetInstallPage::populateComboboxes(QVariantMap local_storage)
{
    QVariantMap initsystems_data = local_storage.value( GS::INITSYSTEMS_KEY ).toMap();
    QVariantMap desktops_data = local_storage.value( GS::DESKTOPS_KEY ).toMap();
    QVariantMap::iterator initsystems_iter = initsystems_data.begin();
    QVariantMap::iterator desktops_iter = desktops_data.begin();

    for ( ; initsystems_iter != initsystems_data.end(); ++initsystems_iter )
    {
cLog() << "NetInstallPage::populateComboboxes() initsystems_iter.key()=" << initsystems_iter.key();

        QVariantMap initsystems_data = initsystems_iter.value().toMap();
        QVariant init_key = QVariant(initsystems_iter.key());
        QString init_name = initsystems_data.value( GS::INIT_NAME_KEY ).toString();

        ui->initCombobox->addItem(init_name, init_key);

cLog() << "NetInstallPage::populateComboboxes() added init_key=" << init_key << " init_name" << init_name;
    }

    for ( ; desktops_iter != desktops_data.end(); ++desktops_iter )
    {
cLog() << "NetInstallPage::populateComboboxes() desktops_iter.key()=" << desktops_iter.key();

        QVariantMap desktops_data = desktops_iter.value().toMap();
        QVariant de_key = QVariant(desktops_iter.key());
        QIcon de_icon = QIcon(desktops_data.value( GS::DE_ICON_KEY ).toString());
        QString de_name = desktops_data.value( GS::DE_NAME_KEY ).toString();

        ui->wmDeCombobox->addItem(de_icon, de_name, de_key);

cLog() << "NetInstallPage::populateComboboxes() added de_icon=" << desktops_data.value( GS::DE_ICON_KEY ).toString() << " de_key=" << de_key << " de_name" << de_name;
    }

    ui->initCombobox->setCurrentIndex( 1 );
    ui->wmDeCombobox->setCurrentIndex( 0 );
}

PackageModel::PackageItemDataList
NetInstallPage::selectedPackages() const
{
    if ( m_groups )
        return m_groups->getPackages();
    else
    {
        cWarning() << "no netinstall groups are available.";
        return PackageModel::PackageItemDataList();
    }
}

QString
NetInstallPage::getInitSystem() const
{
  return ui->initCombobox->itemData(ui->initCombobox->currentIndex()).toString();
}

QString
NetInstallPage::getWmDeKey() const
{
  return ui->wmDeCombobox->itemData(ui->wmDeCombobox->currentIndex()).toString();
}

bool
NetInstallPage::getShouldNetInstall() const
{
  return ui->netinstallCheckbox->isChecked();
}

void
NetInstallPage::loadGroupList( const QString& confUrl )
{
    QNetworkRequest request;
    request.setUrl( QUrl( confUrl ) );
    // Follows all redirects except unsafe ones (https to http).
    request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
    // Not everybody likes the default User Agent used by this class (looking at you,
    // sourceforge.net), so let's set a more descriptive one.
    request.setRawHeader( "User-Agent", "Mozilla/5.0 (compatible; Calamares)" );

    connect( &m_networkManager, &QNetworkAccessManager::finished,
             this, &NetInstallPage::dataIsHere );
    m_networkManager.get( request );
}

void
NetInstallPage::setRequired( bool b )
{
    m_required = b;
}

void
NetInstallPage::onActivate()
{
    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
    bool has_isorepo = gs->value( GS::HAS_ISOREPO_KEY ).toBool();
    bool is_online = gs->value( GS::IS_ONLINE_KEY ).toBool();

    ui->netinstallCheckbox->setChecked( !has_isorepo );
    ui->netinstallCheckbox->setEnabled( has_isorepo && is_online );
    ui->groupswidget->setFocus();

// DEBUG: new features
ui->initCombobox      ->setEnabled( false ); // TODO: remove this
ui->wmDeCombobox      ->setEnabled( false ); // TODO: remove this
ui->netinstallCheckbox->setEnabled( false ); // TODO: remove this
emit skipThis();                             // TODO: remove this
}