2020-03-24 12:02:16 +01:00
|
|
|
/*
|
2020-08-22 01:19:58 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016 Luca Giambonini <almack@chakraos.org>
|
|
|
|
* SPDX-FileCopyrightText: 2016 Lisa Vitolo <shainer@chakraos.org>
|
|
|
|
* SPDX-FileCopyrightText: 2017 Kyle Robbertze <krobbertze@gmail.com>
|
|
|
|
* SPDX-FileCopyrightText: 2017-2018 2020, Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2017 Gabriel Craciunescu <crazy@frugalware.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-24 12:02:16 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-03-24 12:02:16 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Config.h"
|
|
|
|
|
2021-02-08 22:57:38 +01:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
2020-03-24 13:13:18 +01:00
|
|
|
#include "network/Manager.h"
|
|
|
|
#include "utils/Logger.h"
|
2020-03-28 11:45:44 +01:00
|
|
|
#include "utils/RAII.h"
|
2021-02-08 22:57:38 +01:00
|
|
|
#include "utils/Variant.h"
|
2020-03-24 13:13:18 +01:00
|
|
|
#include "utils/Yaml.h"
|
|
|
|
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2020-03-24 12:02:16 +01:00
|
|
|
Config::Config( QObject* parent )
|
2020-03-24 13:13:18 +01:00
|
|
|
: QObject( parent )
|
|
|
|
, m_model( new PackageModel( this ) )
|
2020-03-24 12:02:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:18 +01:00
|
|
|
Config::~Config() {}
|
|
|
|
|
2020-03-27 14:54:06 +01:00
|
|
|
QString
|
|
|
|
Config::status() const
|
|
|
|
{
|
|
|
|
switch ( m_status )
|
|
|
|
{
|
|
|
|
case Status::Ok:
|
|
|
|
return QString();
|
|
|
|
case Status::FailedBadConfiguration:
|
|
|
|
return tr( "Network Installation. (Disabled: Incorrect configuration)" );
|
|
|
|
case Status::FailedBadData:
|
|
|
|
return tr( "Network Installation. (Disabled: Received invalid groups data)" );
|
|
|
|
case Status::FailedInternalError:
|
|
|
|
return tr( "Network Installation. (Disabled: internal error)" );
|
|
|
|
case Status::FailedNetworkError:
|
|
|
|
return tr( "Network Installation. (Disabled: Unable to fetch package lists, check your network connection)" );
|
|
|
|
}
|
2020-10-25 18:52:38 +01:00
|
|
|
__builtin_unreachable();
|
2020-03-27 14:54:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-24 12:02:16 +01:00
|
|
|
void
|
2020-03-27 14:54:06 +01:00
|
|
|
Config::setStatus( Status s )
|
2020-03-24 12:02:16 +01:00
|
|
|
{
|
|
|
|
m_status = s;
|
2020-03-27 14:54:06 +01:00
|
|
|
emit statusChanged( status() );
|
2020-03-24 12:02:16 +01:00
|
|
|
}
|
2020-03-24 13:13:18 +01:00
|
|
|
|
2021-02-08 22:57:38 +01:00
|
|
|
QString
|
|
|
|
Config::sidebarLabel() const
|
|
|
|
{
|
|
|
|
return m_sidebarLabel ? m_sidebarLabel->get() : tr( "Package selection" );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
Config::titleLabel() const
|
|
|
|
{
|
|
|
|
return m_titleLabel ? m_titleLabel->get() : QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-24 13:13:18 +01:00
|
|
|
void
|
|
|
|
Config::loadGroupList( const QVariantList& groupData )
|
|
|
|
{
|
|
|
|
m_model->setupModelData( groupData );
|
2020-03-27 16:12:48 +01:00
|
|
|
emit statusReady();
|
2020-03-24 13:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::loadGroupList( const QUrl& url )
|
|
|
|
{
|
|
|
|
if ( !url.isValid() )
|
|
|
|
{
|
2020-03-27 14:54:06 +01:00
|
|
|
setStatus( Status::FailedBadConfiguration );
|
2020-03-24 13:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace CalamaresUtils::Network;
|
|
|
|
|
|
|
|
cDebug() << "NetInstall loading groups from" << url;
|
2020-03-27 21:14:47 +01:00
|
|
|
QNetworkReply* reply = Manager::instance().asynchronousGet(
|
2020-03-24 13:13:18 +01:00
|
|
|
url,
|
|
|
|
RequestOptions( RequestOptions::FakeUserAgent | RequestOptions::FollowRedirect, std::chrono::seconds( 30 ) ) );
|
|
|
|
|
|
|
|
if ( !reply )
|
|
|
|
{
|
|
|
|
cDebug() << Logger::Continuation << "request failed immediately.";
|
2020-03-27 14:54:06 +01:00
|
|
|
setStatus( Status::FailedBadConfiguration );
|
2020-03-24 13:13:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_reply = reply;
|
|
|
|
connect( reply, &QNetworkReply::finished, this, &Config::receivedGroupData );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Config::receivedGroupData()
|
|
|
|
{
|
|
|
|
if ( !m_reply || !m_reply->isFinished() )
|
|
|
|
{
|
|
|
|
cWarning() << "NetInstall data called too early.";
|
2020-03-27 14:54:06 +01:00
|
|
|
setStatus( Status::FailedInternalError );
|
2020-03-24 13:13:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cDebug() << "NetInstall group data received" << m_reply->size() << "bytes from" << m_reply->url();
|
|
|
|
|
2020-04-16 23:20:22 +02:00
|
|
|
cqDeleter< QNetworkReply > d { m_reply };
|
2020-03-24 13:13:18 +01:00
|
|
|
|
|
|
|
// If m_required is *false* then we still say we're ready
|
|
|
|
// even if the reply is corrupt or missing.
|
|
|
|
if ( m_reply->error() != QNetworkReply::NoError )
|
|
|
|
{
|
|
|
|
cWarning() << "unable to fetch netinstall package lists.";
|
|
|
|
cDebug() << Logger::SubEntry << "Netinstall reply error: " << m_reply->error();
|
|
|
|
cDebug() << Logger::SubEntry << "Request for url: " << m_reply->url().toString()
|
|
|
|
<< " failed with: " << m_reply->errorString();
|
2020-03-27 14:54:06 +01:00
|
|
|
setStatus( Status::FailedNetworkError );
|
2020-03-24 13:13:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray yamlData = m_reply->readAll();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
YAML::Node groups = YAML::Load( yamlData.constData() );
|
|
|
|
|
2020-04-16 23:20:22 +02:00
|
|
|
if ( groups.IsSequence() )
|
|
|
|
{
|
|
|
|
loadGroupList( CalamaresUtils::yamlSequenceToVariant( groups ) );
|
|
|
|
}
|
|
|
|
else if ( groups.IsMap() )
|
|
|
|
{
|
|
|
|
auto map = CalamaresUtils::yamlMapToVariant( groups );
|
|
|
|
loadGroupList( map.value( "groups" ).toList() );
|
|
|
|
}
|
|
|
|
else
|
2020-03-24 13:13:18 +01:00
|
|
|
{
|
|
|
|
cWarning() << "NetInstall groups data does not form a sequence.";
|
|
|
|
}
|
2020-04-16 23:20:22 +02:00
|
|
|
if ( m_model->rowCount() < 1 )
|
|
|
|
{
|
|
|
|
cWarning() << "NetInstall groups data was empty.";
|
|
|
|
}
|
2020-03-24 13:13:18 +01:00
|
|
|
}
|
|
|
|
catch ( YAML::Exception& e )
|
|
|
|
{
|
|
|
|
CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
|
2020-03-27 14:54:06 +01:00
|
|
|
setStatus( Status::FailedBadData );
|
2020-03-24 13:13:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 22:57:38 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
Config::setConfigurationMap( const QVariantMap& configurationMap )
|
|
|
|
{
|
|
|
|
setRequired( CalamaresUtils::getBool( configurationMap, "required", false ) );
|
|
|
|
|
|
|
|
// Get the translations, if any
|
|
|
|
bool bogus = false;
|
|
|
|
auto label = CalamaresUtils::getSubMap( configurationMap, "label", bogus );
|
|
|
|
|
|
|
|
if ( label.contains( "sidebar" ) )
|
|
|
|
{
|
|
|
|
m_sidebarLabel = new CalamaresUtils::Locale::TranslatedString( label, "sidebar", metaObject()->className() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly, load the groups data
|
|
|
|
QString groupsUrl = CalamaresUtils::getString( configurationMap, "groupsUrl" );
|
|
|
|
if ( !groupsUrl.isEmpty() )
|
|
|
|
{
|
|
|
|
// Keep putting groupsUrl into the global storage,
|
|
|
|
// even though it's no longer used for in-module data-passing.
|
|
|
|
Calamares::JobQueue::instance()->globalStorage()->insert( "groupsUrl", groupsUrl );
|
|
|
|
if ( groupsUrl == QStringLiteral( "local" ) )
|
|
|
|
{
|
|
|
|
QVariantList l = configurationMap.value( "groups" ).toList();
|
|
|
|
loadGroupList( l );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loadGroupList( groupsUrl );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|