diff --git a/CMakeLists.txt b/CMakeLists.txt index 5461aa3c8..321989ac0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,9 @@ # SKIP_MODULES : a space or semicolon-separated list of directory names # under src/modules that should not be built. # USE_ : fills in SKIP_MODULES for modules called - +# WITH_ : try to enable (these usually default to ON). For +# a list of WITH_ grep CMakeCache.txt after running +# CMake once. # BUILD_ : choose additional things to build # DEBUG_ : special developer flags for debugging # diff --git a/src/libcalamares/locale/LabelModel.cpp b/src/libcalamares/locale/LabelModel.cpp index 87fca9d85..bcb8af057 100644 --- a/src/libcalamares/locale/LabelModel.cpp +++ b/src/libcalamares/locale/LabelModel.cpp @@ -29,6 +29,7 @@ namespace Locale LabelModel::LabelModel( const QStringList& locales, QObject* parent ) : QAbstractListModel( parent ) + , m_localeIds( locales ) { Q_ASSERT( locales.count() > 0 ); m_locales.reserve( locales.count() ); @@ -132,7 +133,7 @@ LabelModel::find( const QString& countryCode ) const LabelModel* availableTranslations() { - static LabelModel* model = new LabelModel( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); + static LabelModel* model = new LabelModel( QStringLiteral( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) ); return model; } diff --git a/src/libcalamares/locale/LabelModel.h b/src/libcalamares/locale/LabelModel.h index 5082440d3..03daddbf3 100644 --- a/src/libcalamares/locale/LabelModel.h +++ b/src/libcalamares/locale/LabelModel.h @@ -54,6 +54,9 @@ public: */ const Label& locale( int row ) const; + /// @brief Returns all of the locale Ids (e.g. en_US) put into this model. + const QStringList& localeIds() const { return m_localeIds; } + /** @brief Searches for an item that matches @p predicate * * Returns the row number of the first match, or -1 if there isn't one. @@ -67,6 +70,7 @@ public: private: QVector< Label > m_locales; + QStringList m_localeIds; }; /** @brief Returns a model with all available translations. diff --git a/src/modules/packagechooser/CMakeLists.txt b/src/modules/packagechooser/CMakeLists.txt index 70a86a3bb..eeae655c9 100644 --- a/src/modules/packagechooser/CMakeLists.txt +++ b/src/modules/packagechooser/CMakeLists.txt @@ -1,15 +1,39 @@ find_package( Qt5 COMPONENTS Core Gui Widgets REQUIRED ) set( _extra_libraries "" ) +set( _extra_src "" ) ### OPTIONAL AppData XML support in PackageModel # # -find_package(Qt5 COMPONENTS Xml) -if ( Qt5Xml_FOUND ) - add_definitions( -DHAVE_XML ) - list( APPEND _extra_libraries Qt5::Xml ) +option( WITH_APPDATA "Support appdata: items in PackageChooser (requires QtXml)" ON ) +if ( WITH_APPDATA ) + find_package(Qt5 COMPONENTS Xml) + if ( Qt5Xml_FOUND ) + add_definitions( -DHAVE_XML ) + list( APPEND _extra_libraries Qt5::Xml ) + list( APPEND _extra_src ItemAppData.cpp ) + endif() +endif() + +### OPTIONAL AppStream support in PackageModel +# +# +option( WITH_APPSTREAM "Support appstream: items in PackageChooser (requires libappstream-qt)" ON ) +if ( WITH_APPSTREAM ) + find_package(AppStreamQt) + set_package_properties( + AppStreamQt PROPERTIES + DESCRIPTION "Support for AppStream (cache) data" + URL "https://github.com/ximion/appstream" + PURPOSE "AppStream provides package data" + TYPE OPTIONAL + ) + if ( AppStreamQt_FOUND ) + add_definitions( -DHAVE_APPSTREAM ) + list( APPEND _extra_libraries AppStreamQt ) + list( APPEND _extra_src ItemAppStream.cpp ) + endif() endif() - calamares_add_plugin( packagechooser TYPE viewmodule @@ -18,6 +42,7 @@ calamares_add_plugin( packagechooser PackageChooserPage.cpp PackageChooserViewStep.cpp PackageModel.cpp + ${_extra_src} RESOURCES packagechooser.qrc UI diff --git a/src/modules/packagechooser/ItemAppData.cpp b/src/modules/packagechooser/ItemAppData.cpp new file mode 100644 index 000000000..ed0ba9223 --- /dev/null +++ b/src/modules/packagechooser/ItemAppData.cpp @@ -0,0 +1,234 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +/** @brief Loading items from AppData XML files. + * + * Only used if QtXML is found, implements PackageItem::fromAppData(). + */ +#include "PackageModel.h" + +#include "utils/Logger.h" +#include "utils/Variant.h" + +#include +#include +#include + +/** @brief try to load the given @p fileName XML document + * + * Returns a QDomDocument, which will be valid iff the file can + * be read and contains valid XML data. + */ +static inline QDomDocument +loadAppData( const QString& fileName ) +{ + QFile file( fileName ); + if ( !file.open( QIODevice::ReadOnly ) ) + { + return QDomDocument(); + } + QDomDocument doc( "AppData" ); + if ( !doc.setContent( &file ) ) + { + file.close(); + return QDomDocument(); + } + file.close(); + return doc; +} + +/** @brief gets the text of child element @p tagName + */ +static inline QString +getChildText( const QDomNode& n, const QString& tagName ) +{ + QDomElement e = n.firstChildElement( tagName ); + return e.isNull() ? QString() : e.text(); +} + +/** @brief Gets a suitable screenshot path + * + * The element contains zero or more + * elements, which can have a *type* associated with them. + * Scan the screenshot elements, return the path + * for the one labeled with type=default or, if there is no + * default, the first element. + */ +static inline QString +getScreenshotPath( const QDomNode& n ) +{ + QDomElement shotsNode = n.firstChildElement( "screenshots" ); + if ( shotsNode.isNull() ) + { + return QString(); + } + + const QDomNodeList shotList = shotsNode.childNodes(); + int firstScreenshot = -1; // Use which screenshot node? + for ( int i = 0; i < shotList.count(); ++i ) + { + if ( !shotList.at( i ).isElement() ) + { + continue; + } + QDomElement e = shotList.at( i ).toElement(); + if ( e.tagName() != "screenshot" ) + { + continue; + } + // If none has the "type=default" attribute, use the first one + if ( firstScreenshot < 0 ) + { + firstScreenshot = i; + } + // But type=default takes precedence. + if ( e.hasAttribute( "type" ) && e.attribute( "type" ) == "default" ) + { + firstScreenshot = i; + break; + } + } + + if ( firstScreenshot >= 0 ) + { + return shotList.at( firstScreenshot ).firstChildElement( "image" ).text(); + } + + return QString(); +} + +/** @brief Returns language of the given element @p e + * + * Transforms the attribute value for xml:lang to something + * suitable for TranslatedString (e.g. [lang]). + */ +static inline QString +getLanguage( const QDomElement& e ) +{ + QString language = e.attribute( "xml:lang" ); + if ( !language.isEmpty() ) + { + language.replace( '-', '_' ); + language.prepend( '[' ); + language.append( ']' ); + } + return language; +} + +/** @brief Scan the list of @p children for @p tagname elements and add them to the map + * + * Uses @p mapname instead of @p tagname for the entries in map @p m + * to allow renaming from XML to map keys (in particular for + * TranslatedString). Also transforms xml:lang attributes to suitable + * key-decorations on @p mapname. + */ +static inline void +fillMap( QVariantMap& m, const QDomNodeList& children, const QString& tagname, const QString& mapname ) +{ + for ( int i = 0; i < children.count(); ++i ) + { + if ( !children.at( i ).isElement() ) + { + continue; + } + + QDomElement e = children.at( i ).toElement(); + if ( e.tagName() != tagname ) + { + continue; + } + + m[ mapname + getLanguage( e ) ] = e.text(); + } +} + +/** @brief gets the and elements +* +* Builds up a map of the elements (which may have a *lang* +* attribute to indicate translations and paragraphs of the +* element (also with lang). Uses the +* elements to supplement the description if no description +* is available for a given language. +* +* Returns a map with keys suitable for use by TranslatedString. +*/ +static inline QVariantMap +getNameAndSummary( const QDomNode& n ) +{ + QVariantMap m; + + const QDomNodeList children = n.childNodes(); + fillMap( m, children, "name", "name" ); + fillMap( m, children, "summary", "description" ); + + const QDomElement description = n.firstChildElement( "description" ); + if ( !description.isNull() ) + { + fillMap( m, description.childNodes(), "p", "description" ); + } + + return m; +} + +PackageItem +fromAppData( const QVariantMap& item_map ) +{ + QString fileName = CalamaresUtils::getString( item_map, "appdata" ); + if ( fileName.isEmpty() ) + { + cWarning() << "Can't load AppData without a suitable key."; + return PackageItem(); + } + cDebug() << "Loading AppData XML from" << fileName; + + QDomDocument doc = loadAppData( fileName ); + if ( doc.isNull() ) + { + return PackageItem(); + } + + QDomElement componentNode = doc.documentElement(); + if ( !componentNode.isNull() && componentNode.tagName() == "component" ) + { + // An "id" entry in the Calamares config overrides ID in the AppData + QString id = CalamaresUtils::getString( item_map, "id" ); + if ( id.isEmpty() ) + { + id = getChildText( componentNode, "id" ); + } + if ( id.isEmpty() ) + { + return PackageItem(); + } + + // A "screenshot" entry in the Calamares config overrides AppData + QString screenshotPath = CalamaresUtils::getString( item_map, "screenshot" ); + if ( screenshotPath.isEmpty() ) + { + screenshotPath = getScreenshotPath( componentNode ); + } + + QVariantMap map = getNameAndSummary( componentNode ); + map.insert( "id", id ); + map.insert( "screenshot", screenshotPath ); + + return PackageItem( map ); + } + + return PackageItem(); +} diff --git a/src/modules/packagechooser/ItemAppData.h b/src/modules/packagechooser/ItemAppData.h new file mode 100644 index 000000000..72617ff0c --- /dev/null +++ b/src/modules/packagechooser/ItemAppData.h @@ -0,0 +1,37 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +#ifndef ITEMAPPDATA_H +#define ITEMAPPDATA_H + +#include "PackageModel.h" + +/** @brief Loads an AppData XML file and returns a PackageItem + * + * The @p map must have a key *appdata*. That is used as the + * primary source of information, but keys *id* and *screenshotPath* + * may be used to override parts of the AppData -- so that the + * ID is under the control of Calamares, and the screenshot can be + * forced to a local path available on the installation medium. + * + * Requires XML support in libcalamares, if not present will + * return invalid PackageItems. + */ +PackageItem fromAppData( const QVariantMap& map ); + +#endif diff --git a/src/modules/packagechooser/ItemAppStream.cpp b/src/modules/packagechooser/ItemAppStream.cpp new file mode 100644 index 000000000..83837a9ca --- /dev/null +++ b/src/modules/packagechooser/ItemAppStream.cpp @@ -0,0 +1,159 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +/** @brief Loading items from AppData XML files. + * + * Only used if QtXML is found, implements PackageItem::fromAppData(). + */ +#include "PackageModel.h" + +#include "locale/LabelModel.h" +#include "utils/Logger.h" +#include "utils/Variant.h" + +#include +#include +#include + +/// @brief Return number of pixels in a size, for < ordering purposes +static inline quint64 +sizeOrder( const QSize& size ) +{ + return size.width() * size.height(); +} + +/// @brief Sets a screenshot in @p map from @p screenshot, if a usable one is found +static void +setScreenshot( QVariantMap& map, const AppStream::Screenshot& screenshot ) +{ + if ( screenshot.images().count() < 1 ) + { + return; + } + + // Pick the smallest + QUrl url; + quint64 size = sizeOrder( screenshot.images().first().size() ); + for ( const auto& img : screenshot.images() ) + { + if ( sizeOrder( img.size() ) <= size ) + { + url = img.url(); + } + } + + if ( url.isValid() ) + { + map.insert( "screenshot", url.toString() ); + } +} + +/// @brief Interpret an AppStream Component +static PackageItem +fromComponent( AppStream::Component& component ) +{ + QVariantMap map; + map.insert( "id", component.id() ); + map.insert( "package", component.packageNames().join( "," ) ); + + // Assume that the pool has loaded "ALL" locales, but it might be set + // to any of them; get the en_US locale as "untranslated" and then + // loop over Calamares locales (since there is no way to query for + // available locales in the Component) to see if there's anything else. + component.setActiveLocale( QStringLiteral( "en_US" ) ); + QString en_name = component.name(); + QString en_description = component.description(); + map.insert( "name", en_name ); + map.insert( "description", en_description ); + + for ( const QString& locale : CalamaresUtils::Locale::availableTranslations()->localeIds() ) + { + component.setActiveLocale( locale ); + QString name = component.name(); + if ( name != en_name ) + { + map.insert( QStringLiteral( "name[%1]" ).arg( locale ), name ); + } + QString description = component.description(); + if ( description != en_description ) + { + map.insert( QStringLiteral( "description[%1]" ).arg( locale ), description ); + } + } + + + auto screenshots = component.screenshots(); + if ( screenshots.count() > 0 ) + { + bool done = false; + for ( const auto& s : screenshots ) + { + if ( s.isDefault() ) + { + setScreenshot( map, s ); + done = true; + break; + } + } + if ( !done ) + { + setScreenshot( map, screenshots.first() ); + } + } + + return PackageItem( map ); +} + +PackageItem +fromAppStream( AppStream::Pool& pool, const QVariantMap& item_map ) +{ + QString appstreamId = CalamaresUtils::getString( item_map, "appstream" ); + if ( appstreamId.isEmpty() ) + { + cWarning() << "Can't load AppStream without a suitable appstreamId."; + return PackageItem(); + } + cDebug() << "Loading AppStream data for" << appstreamId; + + auto itemList = pool.componentsById( appstreamId ); + if ( itemList.count() < 1 ) + { + cWarning() << "No AppStream data for" << appstreamId; + return PackageItem(); + } + if ( itemList.count() > 1 ) + { + cDebug() << "Multiple AppStream data for" << appstreamId << "using first."; + } + + auto r = fromComponent( itemList.first() ); + if ( r.isValid() ) + { + QString id = CalamaresUtils::getString( item_map, "id" ); + QString screenshotPath = CalamaresUtils::getString( item_map, "screenshot" ); + if ( !id.isEmpty() ) + { + r.id = id; + } + if ( !screenshotPath.isEmpty() ) + { + r.screenshot = screenshotPath; + } + } + return r; +} diff --git a/src/modules/packagechooser/ItemAppStream.h b/src/modules/packagechooser/ItemAppStream.h new file mode 100644 index 000000000..c44b84b06 --- /dev/null +++ b/src/modules/packagechooser/ItemAppStream.h @@ -0,0 +1,43 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * 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 . + */ + +#ifndef ITEMAPPSTREAM_H +#define ITEMAPPSTREAM_H + +#include "PackageModel.h" + +namespace AppStream +{ +class Pool; +} + +/** @brief Loads an item from AppStream data. + * + * The @p map must have a key *appstream*. That is used as the + * primary source of information from the AppStream cache, but + * keys *id* and *screenshotPath* may be used to override parts + * of the AppStream data -- so that the ID is under the control + * of Calamares, and the screenshot can be forced to a local path + * available on the installation medium. + * + * Requires AppStreamQt, if not present will return invalid + * PackageItems. + */ +PackageItem fromAppStream( AppStream::Pool& pool, const QVariantMap& map ); + +#endif diff --git a/src/modules/packagechooser/PackageChooserViewStep.cpp b/src/modules/packagechooser/PackageChooserViewStep.cpp index c2e849d5f..7758986ba 100644 --- a/src/modules/packagechooser/PackageChooserViewStep.cpp +++ b/src/modules/packagechooser/PackageChooserViewStep.cpp @@ -18,6 +18,14 @@ #include "PackageChooserViewStep.h" +#ifdef HAVE_XML +#include "ItemAppData.h" +#endif +#ifdef HAVE_APPSTREAM +#include "ItemAppStream.h" +#include +#include +#endif #include "PackageChooserPage.h" #include "PackageModel.h" @@ -203,6 +211,11 @@ PackageChooserViewStep::fillModel( const QVariantList& items ) return; } +#ifdef HAVE_APPSTREAM + std::unique_ptr< AppStream::Pool > pool; + bool poolOk = false; +#endif + cDebug() << "Loading PackageChooser model items from config"; int item_index = 0; for ( const auto& item_it : items ) @@ -217,7 +230,28 @@ PackageChooserViewStep::fillModel( const QVariantList& items ) if ( item_map.contains( "appdata" ) ) { - m_model->addPackage( PackageItem::fromAppData( item_map ) ); +#ifdef HAVE_XML + m_model->addPackage( fromAppData( item_map ) ); +#else + cWarning() << "Loading AppData XML is not supported."; +#endif + } + else if ( item_map.contains( "appstream" ) ) + { +#ifdef HAVE_APPSTREAM + if ( !pool ) + { + pool = std::make_unique< AppStream::Pool >(); + pool->setLocale( QStringLiteral( "ALL" ) ); + poolOk = pool->load(); + } + if ( pool && poolOk ) + { + m_model->addPackage( fromAppStream( *pool, item_map ) ); + } +#else + cWarning() << "Loading AppStream data is not supported."; +#endif } else { diff --git a/src/modules/packagechooser/PackageModel.cpp b/src/modules/packagechooser/PackageModel.cpp index 59c6973ba..12995fad5 100644 --- a/src/modules/packagechooser/PackageModel.cpp +++ b/src/modules/packagechooser/PackageModel.cpp @@ -21,12 +21,6 @@ #include "utils/Logger.h" #include "utils/Variant.h" -#ifdef HAVE_XML -#include -#include -#include -#endif - const NamedEnumTable< PackageChooserMode >& roleNames() { @@ -94,219 +88,6 @@ PackageItem::PackageItem::PackageItem( const QVariantMap& item_map ) } } -#ifdef HAVE_XML -/** @brief try to load the given @p fileName XML document - * - * Returns a QDomDocument, which will be valid iff the file can - * be read and contains valid XML data. - */ -static inline QDomDocument -loadAppData( const QString& fileName ) -{ - QFile file( fileName ); - if ( !file.open( QIODevice::ReadOnly ) ) - { - return QDomDocument(); - } - QDomDocument doc( "AppData" ); - if ( !doc.setContent( &file ) ) - { - file.close(); - return QDomDocument(); - } - file.close(); - return doc; -} - -/** @brief gets the text of child element @p tagName - */ -static inline QString -getChildText( const QDomNode& n, const QString& tagName ) -{ - QDomElement e = n.firstChildElement( tagName ); - return e.isNull() ? QString() : e.text(); -} - -/** @brief Gets a suitable screenshot path - * - * The element contains zero or more - * elements, which can have a *type* associated with them. - * Scan the screenshot elements, return the path - * for the one labeled with type=default or, if there is no - * default, the first element. - */ -static inline QString -getScreenshotPath( const QDomNode& n ) -{ - QDomElement shotsNode = n.firstChildElement( "screenshots" ); - if ( shotsNode.isNull() ) - { - return QString(); - } - - const QDomNodeList shotList = shotsNode.childNodes(); - int firstScreenshot = -1; // Use which screenshot node? - for ( int i = 0; i < shotList.count(); ++i ) - { - if ( !shotList.at( i ).isElement() ) - { - continue; - } - QDomElement e = shotList.at( i ).toElement(); - if ( e.tagName() != "screenshot" ) - { - continue; - } - // If none has the "type=default" attribute, use the first one - if ( firstScreenshot < 0 ) - { - firstScreenshot = i; - } - // But type=default takes precedence. - if ( e.hasAttribute( "type" ) && e.attribute( "type" ) == "default" ) - { - firstScreenshot = i; - break; - } - } - - if ( firstScreenshot >= 0 ) - { - return shotList.at( firstScreenshot ).firstChildElement( "image" ).text(); - } - - return QString(); -} - -/** @brief Returns language of the given element @p e - * - * Transforms the attribute value for xml:lang to something - * suitable for TranslatedString (e.g. [lang]). - */ -static inline QString -getLanguage( const QDomElement& e ) -{ - QString language = e.attribute( "xml:lang" ); - if ( !language.isEmpty() ) - { - language.replace( '-', '_' ); - language.prepend( '[' ); - language.append( ']' ); - } - return language; -} - -/** @brief Scan the list of @p children for @p tagname elements and add them to the map - * - * Uses @p mapname instead of @p tagname for the entries in map @p m - * to allow renaming from XML to map keys (in particular for - * TranslatedString). Also transforms xml:lang attributes to suitable - * key-decorations on @p mapname. - */ -static inline void -fillMap( QVariantMap& m, const QDomNodeList& children, const QString& tagname, const QString& mapname ) -{ - for ( int i = 0; i < children.count(); ++i ) - { - if ( !children.at( i ).isElement() ) - { - continue; - } - - QDomElement e = children.at( i ).toElement(); - if ( e.tagName() != tagname ) - { - continue; - } - - m[ mapname + getLanguage( e ) ] = e.text(); - } -} - -/** @brief gets the and elements -* -* Builds up a map of the elements (which may have a *lang* -* attribute to indicate translations and paragraphs of the -* element (also with lang). Uses the -* elements to supplement the description if no description -* is available for a given language. -* -* Returns a map with keys suitable for use by TranslatedString. -*/ -static inline QVariantMap -getNameAndSummary( const QDomNode& n ) -{ - QVariantMap m; - - const QDomNodeList children = n.childNodes(); - fillMap( m, children, "name", "name" ); - fillMap( m, children, "summary", "description" ); - - const QDomElement description = n.firstChildElement( "description" ); - if ( !description.isNull() ) - { - fillMap( m, description.childNodes(), "p", "description" ); - } - - return m; -} -#endif - -PackageItem -PackageItem::fromAppData( const QVariantMap& item_map ) -{ -#ifdef HAVE_XML - QString fileName = CalamaresUtils::getString( item_map, "appdata" ); - if ( fileName.isEmpty() ) - { - cWarning() << "Can't load AppData without a suitable key."; - return PackageItem(); - } - cDebug() << "Loading AppData XML from" << fileName; - - QDomDocument doc = loadAppData( fileName ); - if ( doc.isNull() ) - { - return PackageItem(); - } - - QDomElement componentNode = doc.documentElement(); - if ( !componentNode.isNull() && componentNode.tagName() == "component" ) - { - // An "id" entry in the Calamares config overrides ID in the AppData - QString id = CalamaresUtils::getString( item_map, "id" ); - if ( id.isEmpty() ) - { - id = getChildText( componentNode, "id" ); - } - if ( id.isEmpty() ) - { - return PackageItem(); - } - - // A "screenshot" entry in the Calamares config overrides AppData - QString screenshotPath = CalamaresUtils::getString( item_map, "screenshot" ); - if ( screenshotPath.isEmpty() ) - { - screenshotPath = getScreenshotPath( componentNode ); - } - - QVariantMap map = getNameAndSummary( componentNode ); - map.insert( "id", id ); - map.insert( "screenshot", screenshotPath ); - - return PackageItem( map ); - } - - return PackageItem(); -#else - cWarning() << "Loading AppData XML is not supported."; - - return PackageItem(); -#endif -} - - PackageListModel::PackageListModel( QObject* parent ) : QAbstractListModel( parent ) { diff --git a/src/modules/packagechooser/PackageModel.h b/src/modules/packagechooser/PackageModel.h index 869e124f0..8362bee33 100644 --- a/src/modules/packagechooser/PackageModel.h +++ b/src/modules/packagechooser/PackageModel.h @@ -80,19 +80,6 @@ struct PackageItem * A valid item has an untranslated name available. */ bool isValid() const { return !name.isEmpty(); } - - /** @brief Loads an AppData XML file and returns a PackageItem - * - * The @p map must have a key *appdata*. That is used as the - * primary source of information, but keys *id* and *screenshotPath* - * may be used to override parts of the AppData -- so that the - * ID is under the control of Calamares, and the screenshot can be - * forced to a local path available on the installation medium. - * - * Requires XML support in libcalamares, if not present will - * return invalid PackageItems. - */ - static PackageItem fromAppData( const QVariantMap& map ); }; using PackageList = QVector< PackageItem >; diff --git a/src/modules/packagechooser/Tests.cpp b/src/modules/packagechooser/Tests.cpp index 537ecbd3c..639d06d65 100644 --- a/src/modules/packagechooser/Tests.cpp +++ b/src/modules/packagechooser/Tests.cpp @@ -18,6 +18,12 @@ #include "Tests.h" +#ifdef HAVE_XML +#include "ItemAppData.h" +#endif +#ifdef HAVE_APPSTREAM +#include "ItemAppStream.h" +#endif #include "PackageModel.h" #include "utils/Logger.h" @@ -62,8 +68,8 @@ PackageChooserTests::testAppData() QVariantMap m; m.insert( "appdata", appdataName ); - PackageItem p1 = PackageItem::fromAppData( m ); #ifdef HAVE_XML + PackageItem p1 = fromAppData( m ); QVERIFY( p1.isValid() ); QCOMPARE( p1.id, "io.calamares.calamares.desktop" ); QCOMPARE( p1.name.get(), "Calamares" ); @@ -76,12 +82,10 @@ PackageChooserTests::testAppData() m.insert( "id", "calamares" ); m.insert( "screenshot", ":/images/calamares.png" ); - PackageItem p2 = PackageItem::fromAppData( m ); + PackageItem p2 = fromAppData( m ); QVERIFY( p2.isValid() ); QCOMPARE( p2.id, "calamares" ); QCOMPARE( p2.description.get( QLocale( "nl" ) ), "Calamares is een installatieprogramma voor Linux distributies." ); QVERIFY( !p2.screenshot.isNull() ); -#else - QVERIFY( !p1.isValid() ); #endif } diff --git a/src/modules/packagechooser/packagechooser.conf b/src/modules/packagechooser/packagechooser.conf index e9b2d9329..eebe4dfb4 100644 --- a/src/modules/packagechooser/packagechooser.conf +++ b/src/modules/packagechooser/packagechooser.conf @@ -20,14 +20,15 @@ # or one-or-more). mode: required -# Items to display in the chooser. In general, this should be a +# Items to display in the chooser. In general, this should be a # pretty short list to avoid overwhelming the UI. This is a list # of objects, and the items are displayed in list order. # # Either provide the data for an item in the list (using the keys -# below), or use existing AppData XML files as a source for the data. +# below), or use existing AppData XML files, or use AppStream cache +# as a source for the data. # -# For data provided by the list: the item has an id, which is used in +# For data provided by the list: the item has an id, which is used in # setting the value of *packagechooser_*). The following fields # are mandatory: # @@ -60,6 +61,14 @@ mode: required # be loaded and the screenshot will be missing. An item with *appdata* # **may** specify an ID or screenshot path, as above. This will override # the settings from AppData. +# +# For data provided by AppStream cache: the item has an *appstream* +# key which matches the AppStream identifier in the cache (e.g. +# *org.kde.kwrite.desktop*). Data is retrieved from the AppStream +# cache for that ID. The package name is set from the AppStream data. +# +# An item for AppStream may also contain an *id* and a *screenshot* +# key which will override the data from AppStream. items: - id: "" package: "" @@ -81,4 +90,5 @@ items: - id: calamares appdata: ../io.calamares.calamares.appdata.xml screenshot: ":/images/calamares.png" - + - id: kate + appstream: org.kde.kwrite.desktop