[netinstall] Rip loading out of the UI page

- Create a config object in the ViewStep
- Model lives in the config object and loads there
- Give model to the UI page for display
This commit is contained in:
Adriaan de Groot 2020-03-27 15:51:03 +01:00
parent 9a35427113
commit 4cdfe1276a
6 changed files with 34 additions and 203 deletions

View File

@ -29,7 +29,6 @@
Config::Config( QObject* parent )
: QObject( parent )
, m_status( Status::Ok )
, m_model( new PackageModel( this ) )
{
}

View File

@ -51,6 +51,9 @@ public:
QString status() const;
void setStatus( Status s );
bool required() const { return m_required; }
void setRequired( bool r ) { m_required = r; }
/** @brief Retrieves the groups, with name, description and packages
*
* Loads data from the given URL. Once done, the data is parsed
@ -65,6 +68,8 @@ public:
*/
void loadGroupList( const QVariantList& groupData );
PackageModel* model() const { return m_model; }
signals:
void statusChanged( QString status );
@ -72,9 +77,10 @@ private slots:
void receivedGroupData(); ///< From async-loading group data
private:
Status m_status;
PackageModel* m_model;
PackageModel* m_model = nullptr;
QNetworkReply* m_reply = nullptr; // For fetching data
Status m_status = Status::Ok;
bool m_required = false;
};
#endif

View File

@ -37,19 +37,13 @@
NetInstallPage::NetInstallPage( QWidget* parent )
: QWidget( parent )
, ui( new Ui::Page_NetInst )
, m_reply( nullptr )
, m_groups( nullptr )
{
ui->setupUi( this );
setPageTitle( nullptr );
CALAMARES_RETRANSLATE_SLOT( &NetInstallPage::retranslate );
}
NetInstallPage::~NetInstallPage()
{
delete m_groups;
delete m_reply;
}
NetInstallPage::~NetInstallPage() {}
void
NetInstallPage::setPageTitle( CalamaresUtils::Locale::TranslatedString* t )
@ -75,165 +69,21 @@ NetInstallPage::retranslate()
}
}
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();
m_groups->setupModelData( CalamaresUtils::yamlSequenceToVariant( groups ) );
return true;
}
catch ( YAML::Exception& e )
{
CalamaresUtils::explainYamlException( e, yamlData, "netinstall groups data" );
return false;
}
}
/// @brief Convenience to zero out and deleteLater on the reply, used in dataIsHere
struct ReplyDeleter
{
QNetworkReply*& p;
~ReplyDeleter()
{
if ( p )
{
p->deleteLater();
}
p = nullptr;
}
};
void
NetInstallPage::dataIsHere()
{
if ( !m_reply || !m_reply->isFinished() )
{
cWarning() << "NetInstall data called too early.";
return;
}
cDebug() << "NetInstall group data received" << m_reply->url();
ReplyDeleter d { m_reply };
// 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();
ui->netinst_status->setText(
tr( "Network Installation. (Disabled: Unable to fetch package lists, check your network connection)" ) );
emit checkReady( !m_required );
return;
}
if ( !readGroups( m_reply->readAll() ) )
{
cWarning() << "netinstall groups data was received, but invalid.";
cDebug() << Logger::SubEntry << "Url: " << m_reply->url().toString();
cDebug() << Logger::SubEntry << "Headers: " << m_reply->rawHeaderList();
ui->netinst_status->setText( tr( "Network Installation. (Disabled: Received invalid groups data)" ) );
emit checkReady( !m_required );
return;
}
retranslate(); // For changed model
ui->groupswidget->setModel( m_groups );
ui->groupswidget->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
ui->groupswidget->header()->setSectionResizeMode( 1, QHeaderView::Stretch );
expandGroups();
emit checkReady( true );
}
void
NetInstallPage::expandGroups()
NetInstallPage::setModel( QAbstractItemModel* model )
{
ui->groupswidget->setModel( model );
// Go backwards because expanding a group may cause rows to appear below it
for ( int i = m_groups->rowCount() - 1; i >= 0; --i )
for ( int i = model->rowCount() - 1; i >= 0; --i )
{
auto index = m_groups->index( i, 0 );
if ( m_groups->data( index, PackageModel::MetaExpandRole ).toBool() )
auto index = model->index( i, 0 );
if ( model->data( index, PackageModel::MetaExpandRole ).toBool() )
{
ui->groupswidget->setExpanded( index, true );
}
}
}
PackageTreeItem::List
NetInstallPage::selectedPackages() const
{
if ( m_groups )
{
return m_groups->getPackages();
}
else
{
cWarning() << "no netinstall groups are available.";
return PackageTreeItem::List();
}
}
void
NetInstallPage::loadGroupList( const QString& confUrl )
{
using namespace CalamaresUtils::Network;
cDebug() << "NetInstall loading groups from" << confUrl;
QNetworkReply* reply = Manager::instance().asynchronouseGet(
QUrl( confUrl ),
RequestOptions( RequestOptions::FakeUserAgent | RequestOptions::FollowRedirect, std::chrono::seconds( 30 ) ) );
if ( !reply )
{
cDebug() << Logger::Continuation << "request failed immediately.";
ui->netinst_status->setText( tr( "Network Installation. (Disabled: Incorrect configuration)" ) );
}
else
{
m_reply = reply;
connect( reply, &QNetworkReply::finished, this, &NetInstallPage::dataIsHere );
}
}
void
NetInstallPage::loadGroupList( const QVariantList& l )
{
// This short-cuts through loading and just uses the data,
// containing cruft from dataIsHere() and readGroups().
m_groups = new PackageModel();
m_groups->setupModelData( l );
retranslate(); // For changed model
ui->groupswidget->setModel( m_groups );
ui->groupswidget->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );
ui->groupswidget->header()->setSectionResizeMode( 1, QHeaderView::Stretch );
expandGroups();
emit checkReady( true );
}
void
NetInstallPage::setRequired( bool b )
{
m_required = b;
}
void
NetInstallPage::onActivate()

View File

@ -56,56 +56,27 @@ public:
*/
void setPageTitle( CalamaresUtils::Locale::TranslatedString* );
/** @brief Sets the model of packages to display
*
* While setting up the UI, expand entries that should be pre-expanded.
*
* Follows the *expanded* key / the startExpanded field in the
* group entries of the model. Call this after filling up the model.
*/
void setModel( QAbstractItemModel* );
void onActivate();
/** @brief Retrieves the groups, with name, description and packages
*
* Loads data from the given URL. This should be called before
* displaying the page.
*/
void loadGroupList( const QString& url );
/// @brief Retrieve pre-processed and fetched group data
void loadGroupList( const QVariantList& l );
// Sets the "required" state of netinstall data. Influences whether
// corrupt or unavailable data causes checkReady() to be emitted
// true (not-required) or false.
void setRequired( bool );
bool getRequired() const { return m_required; }
// Returns the list of packages belonging to groups that are
// selected in the view in this given moment. No data is cached here, so
// this function does not have constant time.
PackageTreeItem::List selectedPackages() const;
public slots:
void dataIsHere();
void retranslate();
signals:
void checkReady( bool );
private:
// Takes the YAML data representing the groups and reads them into the
// m_groups and m_groupOrder internal structures. See the README.md
// of this module to know the format expected of the YAML files.
bool readGroups( const QByteArray& yamlData );
/** @brief Expand entries that should be pre-expanded
*
* Follows the *expanded* key / the startExpanded field in the
* group entries of the model. Call this after filling up the model.
*/
void expandGroups();
Ui::Page_NetInst* ui;
std::unique_ptr< CalamaresUtils::Locale::TranslatedString > m_title; // Above the treeview
QNetworkReply* m_reply;
PackageModel* m_groups;
bool m_required;
};
#endif // NETINSTALLPAGE_H

View File

@ -33,8 +33,8 @@ CALAMARES_PLUGIN_FACTORY_DEFINITION( NetInstallViewStepFactory, registerPlugin<
NetInstallViewStep::NetInstallViewStep( QObject* parent )
: Calamares::ViewStep( parent )
, m_widget( new NetInstallPage() )
, m_nextEnabled( false )
, m_sidebarLabel( nullptr )
, m_nextEnabled( false )
{
emit nextStatusChanged( true );
connect( m_widget, &NetInstallPage::checkReady, this, &NetInstallViewStep::nextIsReady );
@ -127,7 +127,7 @@ NetInstallViewStep::onActivate()
void
NetInstallViewStep::onLeave()
{
auto packages = m_widget->selectedPackages();
auto packages = m_config.model()->getPackages();
cDebug() << "Netinstall: Processing" << packages.length() << "packages.";
static const char PACKAGEOP[] = "packageOperations";
@ -201,7 +201,8 @@ NetInstallViewStep::nextIsReady( bool b )
void
NetInstallViewStep::setConfigurationMap( const QVariantMap& configurationMap )
{
m_widget->setRequired( CalamaresUtils::getBool( configurationMap, "required", false ) );
m_config.setRequired( CalamaresUtils::getBool( configurationMap, "required", false ) );
m_widget->setModel( m_config.model() );
QString groupsUrl = CalamaresUtils::getString( configurationMap, "groupsUrl" );
if ( !groupsUrl.isEmpty() )
@ -212,11 +213,11 @@ NetInstallViewStep::setConfigurationMap( const QVariantMap& configurationMap )
if ( groupsUrl == QStringLiteral( "local" ) )
{
QVariantList l = configurationMap.value( "groups" ).toList();
m_widget->loadGroupList( l );
m_config.loadGroupList( l );
}
else
{
m_widget->loadGroupList( groupsUrl );
m_config.loadGroupList( groupsUrl );
}
}

View File

@ -20,6 +20,8 @@
#ifndef NETINSTALLVIEWSTEP_H
#define NETINSTALLVIEWSTEP_H
#include "Config.h"
#include "DllMacro.h"
#include "locale/TranslatableConfiguration.h"
#include "utils/PluginFactory.h"
@ -60,9 +62,11 @@ public slots:
void nextIsReady( bool );
private:
Config m_config;
NetInstallPage* m_widget;
bool m_nextEnabled;
CalamaresUtils::Locale::TranslatedString* m_sidebarLabel; // As it appears in the sidebar
bool m_nextEnabled;
};
CALAMARES_PLUGIN_FACTORY_DECLARATION( NetInstallViewStepFactory )