mirror of https://github.com/cutefishos/calamares
commit
052afd4b42
@ -0,0 +1,234 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @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 <QDomDocument>
|
||||||
|
#include <QDomNodeList>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
/** @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 <screenshots> element contains zero or more <screenshot>
|
||||||
|
* elements, which can have a *type* associated with them.
|
||||||
|
* Scan the screenshot elements, return the <image> 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 <name> and <description> elements
|
||||||
|
*
|
||||||
|
* Builds up a map of the <name> elements (which may have a *lang*
|
||||||
|
* attribute to indicate translations and paragraphs of the
|
||||||
|
* <description> element (also with lang). Uses the <summary>
|
||||||
|
* 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();
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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
|
@ -0,0 +1,159 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @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 <AppStreamQt/image.h>
|
||||||
|
#include <AppStreamQt/pool.h>
|
||||||
|
#include <AppStreamQt/screenshot.h>
|
||||||
|
|
||||||
|
/// @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;
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
||||||
|
*
|
||||||
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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
|
Loading…
Reference in New Issue