From 1fec95ac48717942be1be673119710d795f5a774 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 19 May 2020 21:25:05 +0200 Subject: [PATCH] [libcalamares] Move QML search-path initialization - QML files need to be searched in specific places; this was initialized by Calamares, but not for the text application. Move initialization into the library. --- src/calamares/CalamaresApplication.cpp | 60 ++----------------------- src/calamares/testmain.cpp | 7 +++ src/libcalamaresui/utils/Qml.cpp | 62 +++++++++++++++++++++++++- src/libcalamaresui/utils/Qml.h | 8 +++- 4 files changed, 77 insertions(+), 60 deletions(-) diff --git a/src/calamares/CalamaresApplication.cpp b/src/calamares/CalamaresApplication.cpp index e90a67bb3..43a48881c 100644 --- a/src/calamares/CalamaresApplication.cpp +++ b/src/calamares/CalamaresApplication.cpp @@ -120,34 +120,6 @@ CalamaresApplication::mainWindow() } -static QStringList -qmlDirCandidates( bool assumeBuilddir ) -{ - static const char QML[] = "qml"; - - QStringList qmlDirs; - if ( CalamaresUtils::isAppDataDirOverridden() ) - { - qmlDirs << CalamaresUtils::appDataDir().absoluteFilePath( QML ); - } - else - { - if ( assumeBuilddir ) - { - qmlDirs << QDir::current().absoluteFilePath( "src/qml" ); // In build-dir - } - if ( CalamaresUtils::haveExtraDirs() ) - for ( auto s : CalamaresUtils::extraDataDirs() ) - { - qmlDirs << ( s + QML ); - } - qmlDirs << CalamaresUtils::appDataDir().absoluteFilePath( QML ); - } - - return qmlDirs; -} - - static QStringList brandingFileCandidates( bool assumeBuilddir, const QString& brandingFilename ) { @@ -178,38 +150,12 @@ brandingFileCandidates( bool assumeBuilddir, const QString& brandingFilename ) void CalamaresApplication::initQmlPath() { - QDir importPath; // Right now, current-dir - QStringList qmlDirCandidatesByPriority = qmlDirCandidates( isDebug() ); - bool found = false; - - foreach ( const QString& path, qmlDirCandidatesByPriority ) - { - QDir dir( path ); - if ( dir.exists() && dir.isReadable() ) - { - importPath = dir; - found = true; - break; - } - } - - if ( !found || !importPath.exists() || !importPath.isReadable() ) +#ifdef WITH_QML + if ( !CalamaresUtils::initQmlModulesDir() ) { - cError() << "Cowardly refusing to continue startup without a QML directory." - << Logger::DebugList( qmlDirCandidatesByPriority ); - if ( CalamaresUtils::isAppDataDirOverridden() ) - { - cError() << "FATAL: explicitly configured application data directory is missing qml/"; - } - else - { - cError() << "FATAL: none of the expected QML paths exist."; - } ::exit( EXIT_FAILURE ); } - - cDebug() << "Using Calamares QML directory" << importPath.absolutePath(); - CalamaresUtils::setQmlModulesDir( importPath ); +#endif } diff --git a/src/calamares/testmain.cpp b/src/calamares/testmain.cpp index 3405b0530..57da890cc 100644 --- a/src/calamares/testmain.cpp +++ b/src/calamares/testmain.cpp @@ -33,6 +33,9 @@ #include "modulesystem/ModuleManager.h" #include "modulesystem/ViewModule.h" #include "utils/Logger.h" +#ifdef WITH_QML +#include "utils/Qml.h" +#endif #include "utils/Yaml.h" #include "viewpages/ExecutionViewStep.h" @@ -366,6 +369,10 @@ main( int argc, char* argv[] ) gs->insert( "localeConf", vm ); } +#ifdef WITH_QML + CalamaresUtils::initQmlModulesDir(); // don't care if failed +#endif + cDebug() << "Calamares module-loader testing" << module.moduleName(); Calamares::Module* m = load_module( module ); if ( !m ) diff --git a/src/libcalamaresui/utils/Qml.cpp b/src/libcalamaresui/utils/Qml.cpp index eabbf8d33..4f53aa317 100644 --- a/src/libcalamaresui/utils/Qml.cpp +++ b/src/libcalamaresui/utils/Qml.cpp @@ -21,7 +21,9 @@ #include "Branding.h" #include "GlobalStorage.h" #include "JobQueue.h" +#include "Settings.h" #include "ViewManager.h" +#include "utils/Dirs.h" #include "utils/Logger.h" #include @@ -46,6 +48,62 @@ setQmlModulesDir( const QDir& dir ) s_qmlModulesDir = dir; } +static QStringList +qmlDirCandidates( bool assumeBuilddir ) +{ + static const char QML[] = "qml"; + + QStringList qmlDirs; + if ( CalamaresUtils::isAppDataDirOverridden() ) + { + qmlDirs << CalamaresUtils::appDataDir().absoluteFilePath( QML ); + } + else + { + if ( assumeBuilddir ) + { + qmlDirs << QDir::current().absoluteFilePath( "src/qml" ); // In build-dir + } + if ( CalamaresUtils::haveExtraDirs() ) + for ( auto s : CalamaresUtils::extraDataDirs() ) + { + qmlDirs << ( s + QML ); + } + qmlDirs << CalamaresUtils::appDataDir().absoluteFilePath( QML ); + } + + return qmlDirs; +} + +bool +initQmlModulesDir() +{ + QStringList qmlDirCandidatesByPriority + = qmlDirCandidates( Calamares::Settings::instance() && Calamares::Settings::instance()->debugMode() ); + + for ( const QString& path : qmlDirCandidatesByPriority ) + { + QDir dir( path ); + if ( dir.exists() && dir.isReadable() ) + { + cDebug() << "Using Calamares QML directory" << dir.absolutePath(); + CalamaresUtils::setQmlModulesDir( dir ); + return true; + } + } + + cError() << "Cowardly refusing to continue startup without a QML directory." + << Logger::DebugList( qmlDirCandidatesByPriority ); + if ( CalamaresUtils::isAppDataDirOverridden() ) + { + cError() << "FATAL: explicitly configured application data directory is missing qml/"; + } + else + { + cError() << "FATAL: none of the expected QML paths exist."; + } + return false; +} void callQmlFunction( QQuickItem* qmlObject, const char* method ) @@ -85,14 +143,14 @@ addExpansions( QmlSearch method, QStringList& candidates, const QStringList& nam std::transform( names.constBegin(), names.constEnd(), std::back_inserter( candidates ), - [ & ]( const QString& s ) { return s.isEmpty() ? QString() : bPath.arg( brandDir, s ); } ); + [&]( const QString& s ) { return s.isEmpty() ? QString() : bPath.arg( brandDir, s ); } ); } if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::QrcOnly ) ) { std::transform( names.constBegin(), names.constEnd(), std::back_inserter( candidates ), - [ & ]( const QString& s ) { return s.isEmpty() ? QString() : qrPath.arg( s ); } ); + [&]( const QString& s ) { return s.isEmpty() ? QString() : qrPath.arg( s ); } ); } } diff --git a/src/libcalamaresui/utils/Qml.h b/src/libcalamaresui/utils/Qml.h index 2da36633a..a3fc6d114 100644 --- a/src/libcalamaresui/utils/Qml.h +++ b/src/libcalamaresui/utils/Qml.h @@ -35,7 +35,13 @@ UIDLLEXPORT QDir qmlModulesDir(); /// @brief sets specific directory for searching for QML files UIDLLEXPORT void setQmlModulesDir( const QDir& dir ); - +/** @brief initialize QML search path with branding directories + * + * Picks a suitable branding directory (from the build-dir in debug mode, + * otherwise based on the branding directory) and adds it to the + * QML modules directory; returns @c false if none is found. + */ +UIDLLEXPORT bool initQmlModulesDir(); /** @brief Sets up global Calamares models for QML *