source: calamares/trunk/fuentes/src/modules/netinstall/NetInstallPage.cpp @ 7538

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

sync with github

File size: 4.6 KB
Line 
1/*
2 *   Copyright 2016, Luca Giambonini <almack@chakraos.org>
3 *   Copyright 2016, Lisa Vitolo     <shainer@chakraos.org>
4 *   Copyright 2017, Kyle Robbertze  <krobbertze@gmail.com>
5 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
6 *   Copyright 2017, Gabriel Craciunescu <crazy@frugalware.org>
7 *
8 *   Calamares is free software: you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation, either version 3 of the License, or
11 *   (at your option) any later version.
12 *
13 *   Calamares is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "NetInstallPage.h"
23
24#include "PackageModel.h"
25
26#include "ui_page_netinst.h"
27#include "JobQueue.h"
28
29#include "utils/Logger.h"
30#include "utils/Retranslator.h"
31#include "utils/YamlUtils.h"
32
33#include <QNetworkAccessManager>
34#include <QNetworkRequest>
35#include <QNetworkReply>
36
37#include <QHeaderView>
38
39#include <yaml-cpp/yaml.h>
40
41using CalamaresUtils::yamlToVariant;
42
43NetInstallPage::NetInstallPage( QWidget* parent )
44    : QWidget( parent )
45    , ui( new Ui::Page_NetInst )
46    , m_networkManager( this )
47    , m_groups( nullptr )
48{
49    ui->setupUi( this );
50}
51
52bool
53NetInstallPage::readGroups( const QByteArray& yamlData )
54{
55    try
56    {
57        YAML::Node groups = YAML::Load( yamlData.constData() );
58
59        if ( !groups.IsSequence() )
60            cWarning() << "netinstall groups data does not form a sequence.";
61        Q_ASSERT( groups.IsSequence() );
62        m_groups = new PackageModel( groups );
63        CALAMARES_RETRANSLATE(
64            m_groups->setHeaderData( 0, Qt::Horizontal, tr( "Name" ) );
65            m_groups->setHeaderData( 1, Qt::Horizontal, tr( "Description" ) ); )
66        return true;
67
68    }
69    catch ( YAML::Exception& e )
70    {
71        CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
72        return false;
73    }
74}
75
76void
77NetInstallPage::dataIsHere( QNetworkReply* reply )
78{
79    // If m_required is *false* then we still say we're ready
80    // even if the reply is corrupt or missing.
81    if ( reply->error() != QNetworkReply::NoError )
82    {
83        cWarning() << "unable to fetch netinstall package lists.";
84        cDebug() << "  ..Netinstall reply error: " << reply->error();
85        cDebug() << "  ..Request for url: " << reply->url().toString() << " failed with: " << reply->errorString();
86        ui->netinst_status->setText( tr( "Network Installation. (Disabled: Unable to fetch package lists, check your network connection)" ) );
87        emit checkReady( !m_required );
88        return;
89    }
90
91    if ( !readGroups( reply->readAll() ) )
92    {
93        cWarning() << "netinstall groups data was received, but invalid.";
94        cDebug() << "  ..Url:     " <<  reply->url().toString();
95        cDebug() << "  ..Headers: " <<  reply->rawHeaderList();
96        ui->netinst_status->setText( tr( "Network Installation. (Disabled: Received invalid groups data)" ) );
97        reply->deleteLater();
98        emit checkReady( !m_required );
99        return;
100    }
101
102    ui->groupswidget->setModel( m_groups );
103    ui->groupswidget->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
104    ui->groupswidget->header()->setSectionResizeMode( 1, QHeaderView::Stretch );
105
106    reply->deleteLater();
107    emit checkReady( true );
108}
109
110PackageModel::PackageItemDataList
111NetInstallPage::selectedPackages() const
112{
113    if ( m_groups )
114        return m_groups->getPackages();
115    else
116    {
117        cWarning() << "no netinstall groups are available.";
118        return PackageModel::PackageItemDataList();
119    }
120}
121
122void
123NetInstallPage::loadGroupList( const QString& confUrl )
124{
125    QNetworkRequest request;
126    request.setUrl( QUrl( confUrl ) );
127    // Follows all redirects except unsafe ones (https to http).
128    request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
129    // Not everybody likes the default User Agent used by this class (looking at you,
130    // sourceforge.net), so let's set a more descriptive one.
131    request.setRawHeader( "User-Agent", "Mozilla/5.0 (compatible; Calamares)" );
132
133    connect( &m_networkManager, &QNetworkAccessManager::finished,
134             this, &NetInstallPage::dataIsHere );
135    m_networkManager.get( request );
136}
137
138void
139NetInstallPage::setRequired( bool b )
140{
141    m_required = b;
142}
143
144
145void
146NetInstallPage::onActivate()
147{
148    ui->groupswidget->setFocus();
149}
Note: See TracBrowser for help on using the repository browser.