From beb5896fa237f3b6bf8045ee94a8fa7b0d7fcc11 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 6 Aug 2019 14:33:57 +0200 Subject: [PATCH] [packagechooser] Start implementation of AppData loading - Doing a manual read of the XML, since existing appdata libraries don't seem to have a convenient entry for what I need. - Expand tests to loading AppData (currently, they fail). --- src/modules/packagechooser/CMakeLists.txt | 15 +++++ src/modules/packagechooser/PackageModel.cpp | 64 ++++++++++++++++++--- src/modules/packagechooser/PackageModel.h | 14 ++++- src/modules/packagechooser/Tests.cpp | 17 ++++++ src/modules/packagechooser/Tests.h | 1 + 5 files changed, 101 insertions(+), 10 deletions(-) diff --git a/src/modules/packagechooser/CMakeLists.txt b/src/modules/packagechooser/CMakeLists.txt index 4663ccce7..70a86a3bb 100644 --- a/src/modules/packagechooser/CMakeLists.txt +++ b/src/modules/packagechooser/CMakeLists.txt @@ -1,4 +1,15 @@ find_package( Qt5 COMPONENTS Core Gui Widgets REQUIRED ) +set( _extra_libraries "" ) + +### OPTIONAL AppData XML support in PackageModel +# +# +find_package(Qt5 COMPONENTS Xml) +if ( Qt5Xml_FOUND ) + add_definitions( -DHAVE_XML ) + list( APPEND _extra_libraries Qt5::Xml ) +endif() + calamares_add_plugin( packagechooser TYPE viewmodule @@ -13,6 +24,7 @@ calamares_add_plugin( packagechooser page_package.ui LINK_PRIVATE_LIBRARIES calamaresui + ${_extra_libraries} SHARED_LIB ) @@ -23,8 +35,11 @@ if( ECM_FOUND AND BUILD_TESTING ) packagechoosertest LINK_LIBRARIES ${CALAMARES_LIBRARIES} + calamares_viewmodule_packagechooser Qt5::Core Qt5::Test + Qt5::Gui + ${_extra_libraries} ) calamares_automoc( packagechoosertest) endif() diff --git a/src/modules/packagechooser/PackageModel.cpp b/src/modules/packagechooser/PackageModel.cpp index 3283cbe6f..f13564d5e 100644 --- a/src/modules/packagechooser/PackageModel.cpp +++ b/src/modules/packagechooser/PackageModel.cpp @@ -21,6 +21,11 @@ #include "utils/Logger.h" #include "utils/Variant.h" +#ifdef HAVE_XML +#include +#include +#endif + const NamedEnumTable< PackageChooserMode >& roleNames() { @@ -41,13 +46,6 @@ roleNames() return names; } -PackageItem -PackageItem::fromAppStream( const QString& filename ) -{ - // TODO: implement this - return PackageItem {}; -} - PackageItem::PackageItem() {} PackageItem::PackageItem( const QString& a_id, @@ -99,6 +97,56 @@ PackageItem::PackageItem::PackageItem( const QVariantMap& item_map ) } } +#ifdef HAVE_XML +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; +} + +QString +getChildText( const QDomNode& n, const QString& tagName ) +{ + QDomElement e = n.firstChildElement( tagName ); + return e.isNull() ? QString() : e.text(); +} +#endif + +PackageItem +PackageItem::fromAppData( const QString& fileName ) +{ +#ifdef HAVE_XML + QDomDocument doc = loadAppData( fileName ); + if ( doc.isNull() ) + { + return PackageItem(); + } + + QDomElement componentNode = doc.documentElement(); + if ( !componentNode.isNull() && componentNode.tagName() == "component" ) + { + QString id = getChildText( componentNode, "id" ); + cDebug() << "Got AppData id" << id; + } + + return PackageItem(); +#else + return PackageItem(); +#endif +} + PackageListModel::PackageListModel( QObject* parent ) : QAbstractListModel( parent ) @@ -117,7 +165,7 @@ void PackageListModel::addPackage( PackageItem&& p ) { // Only add valid packages - if ( !p.name.isEmpty() ) + if ( p.isValid() ) { int c = m_packages.count(); beginInsertRows( QModelIndex(), c, c ); diff --git a/src/modules/packagechooser/PackageModel.h b/src/modules/packagechooser/PackageModel.h index ce384096c..7e79b98f1 100644 --- a/src/modules/packagechooser/PackageModel.h +++ b/src/modules/packagechooser/PackageModel.h @@ -75,8 +75,18 @@ struct PackageItem */ PackageItem( const QVariantMap& map ); - // TODO: implement this - PackageItem fromAppStream( const QString& filename ); + /** @brief Is this item valid? + * + * A valid item has an untranslated name available. + */ + bool isValid() const { return !name.isEmpty(); } + + /** @brief Loads an AppData XML file and returns a PackageItem + * + * Requires XML support in libcalamares, if not present will + * return invalid PackageItems. + */ + static PackageItem fromAppData( const QString& filename ); }; using PackageList = QVector< PackageItem >; diff --git a/src/modules/packagechooser/Tests.cpp b/src/modules/packagechooser/Tests.cpp index c016f1808..6cbab8e38 100644 --- a/src/modules/packagechooser/Tests.cpp +++ b/src/modules/packagechooser/Tests.cpp @@ -18,6 +18,8 @@ #include "Tests.h" +#include "PackageModel.h" + #include QTEST_GUILESS_MAIN( PackageChooserTests ) @@ -36,3 +38,18 @@ PackageChooserTests::testBogus() { QVERIFY( true ); } + +void +PackageChooserTests::testAppData() +{ + // Path from the build-dir + QString appdataName( "../io.calamares.calamares.appdata.xml" ); + QVERIFY( QFile::exists( appdataName ) ); + + PackageItem p = PackageItem::fromAppData( appdataName ); +#ifdef HAVE_XML + QVERIFY( p.isValid() ); +#else + QVERIFY( !p.isValid() ); +#endif +} diff --git a/src/modules/packagechooser/Tests.h b/src/modules/packagechooser/Tests.h index bc257f5a5..62efe92cc 100644 --- a/src/modules/packagechooser/Tests.h +++ b/src/modules/packagechooser/Tests.h @@ -31,6 +31,7 @@ public: private Q_SLOTS: void initTestCase(); void testBogus(); + void testAppData(); }; #endif