diff --git a/src/modules/netinstall/NetInstallPage.cpp b/src/modules/netinstall/NetInstallPage.cpp index 80e6a58f9..17c16a472 100644 --- a/src/modules/netinstall/NetInstallPage.cpp +++ b/src/modules/netinstall/NetInstallPage.cpp @@ -76,6 +76,6 @@ NetInstallPage::onActivate() const QVariantList groups = gs->value( "NetinstallAdd" ).toList(); if ( !groups.isEmpty() ) { - m_config->model()->appendModelData( groups, "packageChooser" ); + m_config->model()->appendModelData( groups ); } } diff --git a/src/modules/netinstall/PackageModel.cpp b/src/modules/netinstall/PackageModel.cpp index 9ffaa2650..ac7443c15 100644 --- a/src/modules/netinstall/PackageModel.cpp +++ b/src/modules/netinstall/PackageModel.cpp @@ -14,6 +14,17 @@ #include "utils/Variant.h" #include "utils/Yaml.h" +/** @brief Appends groups to the tree + * + * Uses the data from @p groupList to add elements to the + * existing tree that m_rootItem points to. If m_rootItem + * is not valid, it does nothing + * + * Before adding anything to the model, it ensures that there + * is no existing data from the same source. If there is, that + * data is pruned first + * + */ static void setSelections2( const QStringList& selectNames, PackageTreeItem* item ) { @@ -28,6 +39,28 @@ setSelections2( const QStringList& selectNames, PackageTreeItem* item ) } } +/** @brief Collects all the "source" values from @p groupList + * + * Iterates over @p groupList and returns all nonempty "source" + * values from the maps. + * + */ +static QStringList +collectSources( const QVariantList& groupList ) +{ + QStringList sources; + for ( const QVariant& group : groupList ) + { + QVariantMap groupMap = group.toMap(); + if ( !groupMap[ "source" ].toString().isEmpty() ) + { + sources.append( groupMap[ "source" ].toString() ); + } + } + + return sources; +} + PackageModel::PackageModel( QObject* parent ) : QAbstractItemModel( parent ) { @@ -334,19 +367,24 @@ PackageModel::setupModelData( const QVariantList& l ) } void -PackageModel::appendModelData( const QVariantList& groupList, const QString& source ) +PackageModel::appendModelData( const QVariantList& groupList ) { if ( m_rootItem ) { Q_EMIT beginResetModel(); - // Prune any existing data from the same source - for ( int i = 0; i < m_rootItem->childCount(); i++ ) + const QStringList sources = collectSources( groupList ); + + if ( !sources.isEmpty() ) { - PackageTreeItem* child = m_rootItem->child( i ); - if ( child->source() == source ) + // Prune any existing data from the same source + for ( int i = 0; i < m_rootItem->childCount(); i++ ) { - m_rootItem->removeChild( i ); + PackageTreeItem* child = m_rootItem->child( i ); + if ( sources.contains( child->source() ) ) + { + m_rootItem->removeChild( i ); + } } } diff --git a/src/modules/netinstall/PackageModel.h b/src/modules/netinstall/PackageModel.h index 3e79faf98..5146c63dd 100644 --- a/src/modules/netinstall/PackageModel.h +++ b/src/modules/netinstall/PackageModel.h @@ -68,18 +68,7 @@ public: PackageTreeItem::List getPackages() const; PackageTreeItem::List getItemPackages( PackageTreeItem* item ) const; - /** @brief Appends groups to the tree - * - * Uses the data from @p groupList to add elements to the - * existing tree that m_rootItem points to. If m_rootItem - * is not valid, it does nothing - * - * Before adding anything to the model, it ensures that there - * is no existing data from the same source. If there is, that - * data is pruned first - * - */ - void appendModelData(const QVariantList& groupList, const QString &source ); + void appendModelData(const QVariantList& groupList); private: friend class ItemTests; diff --git a/src/modules/packagechooser/PackageModel.cpp b/src/modules/packagechooser/PackageModel.cpp index 3d1c0c044..f1d1184ad 100644 --- a/src/modules/packagechooser/PackageModel.cpp +++ b/src/modules/packagechooser/PackageModel.cpp @@ -140,14 +140,14 @@ QVariantList PackageListModel::getNetinstallDataForNames( const QStringList& ids ) const { QVariantList l; - for ( auto &p : m_packages ) + for ( auto& p : m_packages ) { if ( ids.contains( p.id ) ) { if ( !p.netinstallData.isEmpty() ) { QVariantMap newData = p.netinstallData; - newData["source"] = "packageChooser"; + newData[ "source" ] = QStringLiteral( "packageChooser" ); l.append( newData ); } }