2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-02-07 21:25:55 +01:00
|
|
|
*
|
2020-08-20 22:42:19 +02:00
|
|
|
* SPDX-FileCopyrightText: 2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2020-02-07 21:25:55 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2020-02-07 21:25:55 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Qml.h"
|
|
|
|
|
2020-03-11 17:12:02 +01:00
|
|
|
#include "Branding.h"
|
2020-05-01 09:58:38 +02:00
|
|
|
#include "GlobalStorage.h"
|
|
|
|
#include "JobQueue.h"
|
2020-05-19 21:25:05 +02:00
|
|
|
#include "Settings.h"
|
2020-03-12 03:45:14 +01:00
|
|
|
#include "ViewManager.h"
|
2020-07-23 12:57:01 +02:00
|
|
|
#include "network/Manager.h"
|
2020-05-19 21:25:05 +02:00
|
|
|
#include "utils/Dirs.h"
|
2020-02-07 21:25:55 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QQuickItem>
|
2020-03-11 17:12:02 +01:00
|
|
|
#include <QString>
|
2020-02-07 21:25:55 +01:00
|
|
|
#include <QVariant>
|
|
|
|
|
2020-05-19 17:39:57 +02:00
|
|
|
static QDir s_qmlModulesDir( QString( CMAKE_INSTALL_FULL_DATADIR ) + "/qml" );
|
|
|
|
|
2020-02-07 21:25:55 +01:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
2020-05-19 17:39:57 +02:00
|
|
|
QDir
|
|
|
|
qmlModulesDir()
|
|
|
|
{
|
|
|
|
return s_qmlModulesDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setQmlModulesDir( const QDir& dir )
|
|
|
|
{
|
|
|
|
s_qmlModulesDir = dir;
|
|
|
|
}
|
|
|
|
|
2020-05-19 21:25:05 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-02-07 21:25:55 +01:00
|
|
|
|
|
|
|
void
|
2020-05-19 17:17:14 +02:00
|
|
|
callQmlFunction( QQuickItem* qmlObject, const char* method )
|
2020-02-07 21:25:55 +01:00
|
|
|
{
|
|
|
|
QByteArray methodSignature( method );
|
|
|
|
methodSignature.append( "()" );
|
|
|
|
|
|
|
|
if ( qmlObject && qmlObject->metaObject()->indexOfMethod( methodSignature ) >= 0 )
|
|
|
|
{
|
|
|
|
QVariant returnValue;
|
|
|
|
QMetaObject::invokeMethod( qmlObject, method, Q_RETURN_ARG( QVariant, returnValue ) );
|
|
|
|
if ( !returnValue.isNull() )
|
|
|
|
{
|
|
|
|
cDebug() << "QML" << methodSignature << "returned" << returnValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( qmlObject )
|
|
|
|
{
|
|
|
|
cDebug() << "QML" << methodSignature << "is missing.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 04:17:48 +01:00
|
|
|
/** @brief Appends to @p candidates suitable expansions of @p names
|
|
|
|
*
|
|
|
|
* Depending on @p method, adds search expansions for branding, or QRC,
|
|
|
|
* or both (with branding having precedence).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
addExpansions( QmlSearch method, QStringList& candidates, const QStringList& names )
|
2020-03-11 17:12:02 +01:00
|
|
|
{
|
|
|
|
QString bPath( QStringLiteral( "%1/%2.qml" ) );
|
|
|
|
QString qrPath( QStringLiteral( ":/%1.qml" ) );
|
|
|
|
|
|
|
|
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::BrandingOnly ) )
|
|
|
|
{
|
|
|
|
QString brandDir = Calamares::Branding::instance()->componentDirectory();
|
2020-03-12 04:17:48 +01:00
|
|
|
std::transform( names.constBegin(),
|
|
|
|
names.constEnd(),
|
|
|
|
std::back_inserter( candidates ),
|
2020-05-19 21:25:05 +02:00
|
|
|
[&]( const QString& s ) { return s.isEmpty() ? QString() : bPath.arg( brandDir, s ); } );
|
2020-03-11 17:12:02 +01:00
|
|
|
}
|
|
|
|
if ( ( method == QmlSearch::Both ) || ( method == QmlSearch::QrcOnly ) )
|
|
|
|
{
|
2020-03-12 04:17:48 +01:00
|
|
|
std::transform( names.constBegin(),
|
|
|
|
names.constEnd(),
|
|
|
|
std::back_inserter( candidates ),
|
2020-05-19 21:25:05 +02:00
|
|
|
[&]( const QString& s ) { return s.isEmpty() ? QString() : qrPath.arg( s ); } );
|
2020-03-12 04:17:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Does actual search and returns result.
|
|
|
|
*
|
|
|
|
* Empty items in @p candidates are ignored.
|
|
|
|
*/
|
|
|
|
static QString
|
|
|
|
searchQmlFile( QmlSearch method, const QString& configuredName, const QStringList& hints )
|
|
|
|
{
|
|
|
|
QStringList candidates;
|
|
|
|
if ( configuredName.startsWith( '/' ) )
|
|
|
|
{
|
|
|
|
candidates << configuredName;
|
2020-03-11 17:12:02 +01:00
|
|
|
}
|
2020-03-12 04:17:48 +01:00
|
|
|
addExpansions( method, candidates, hints );
|
|
|
|
|
2020-03-11 17:12:02 +01:00
|
|
|
for ( const QString& candidate : candidates )
|
|
|
|
{
|
|
|
|
if ( candidate.isEmpty() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cDebug() << Logger::SubEntry << "Looking at QML file" << candidate;
|
|
|
|
if ( QFile::exists( candidate ) )
|
|
|
|
{
|
|
|
|
if ( candidate.startsWith( ':' ) )
|
|
|
|
{
|
|
|
|
// Inconsistency: QFile only sees the file with :,
|
|
|
|
// but QML needs an explicit scheme (of qrc:)
|
|
|
|
return QStringLiteral( "qrc" ) + candidate;
|
|
|
|
}
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cDebug() << Logger::SubEntry << "None found.";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2020-03-12 04:17:48 +01:00
|
|
|
QString
|
|
|
|
searchQmlFile( QmlSearch method, const QString& configuredName, const Calamares::ModuleSystem::InstanceKey& i )
|
|
|
|
{
|
|
|
|
cDebug() << "Looking for QML for" << i.toString();
|
|
|
|
return searchQmlFile( method, configuredName, { configuredName, i.toString(), i.module() } );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
searchQmlFile( QmlSearch method, const QString& configuredName )
|
|
|
|
{
|
|
|
|
cDebug() << "Looking for QML for" << configuredName;
|
|
|
|
return searchQmlFile( method, configuredName, { configuredName } );
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:12:02 +01:00
|
|
|
const NamedEnumTable< QmlSearch >&
|
|
|
|
qmlSearchNames()
|
|
|
|
{
|
|
|
|
// *INDENT-OFF*
|
|
|
|
// clang-format off
|
|
|
|
static NamedEnumTable< QmlSearch > names {
|
|
|
|
{ QStringLiteral( "both" ), QmlSearch::Both },
|
|
|
|
{ QStringLiteral( "qrc" ), QmlSearch::QrcOnly },
|
|
|
|
{ QStringLiteral( "branding" ), QmlSearch::BrandingOnly }
|
|
|
|
};
|
|
|
|
// *INDENT-ON*
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2020-03-12 03:45:14 +01:00
|
|
|
void
|
2020-05-19 17:17:14 +02:00
|
|
|
registerQmlModels()
|
2020-03-12 03:45:14 +01:00
|
|
|
{
|
|
|
|
static bool done = false;
|
|
|
|
if ( !done )
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
// Because branding and viewmanager have a parent (CalamaresApplication
|
|
|
|
// and CalamaresWindow), they will not be deleted by QmlEngine.
|
|
|
|
// https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership
|
|
|
|
qmlRegisterSingletonType< Calamares::Branding >(
|
|
|
|
"io.calamares.ui", 1, 0, "Branding", []( QQmlEngine*, QJSEngine* ) -> QObject* {
|
|
|
|
return Calamares::Branding::instance();
|
|
|
|
} );
|
2020-05-01 09:15:46 +02:00
|
|
|
qmlRegisterSingletonType< Calamares::ViewManager >(
|
2021-03-03 15:54:11 +01:00
|
|
|
"io.calamares.ui", 1, 0, "ViewManager", []( QQmlEngine*, QJSEngine* ) -> QObject* {
|
2020-03-12 03:45:14 +01:00
|
|
|
return Calamares::ViewManager::instance();
|
|
|
|
} );
|
2020-05-01 09:58:38 +02:00
|
|
|
qmlRegisterSingletonType< Calamares::GlobalStorage >(
|
|
|
|
"io.calamares.core", 1, 0, "Global", []( QQmlEngine*, QJSEngine* ) -> QObject* {
|
|
|
|
return Calamares::JobQueue::instance()->globalStorage();
|
|
|
|
} );
|
2020-07-23 12:57:01 +02:00
|
|
|
qmlRegisterSingletonType< CalamaresUtils::Network::Manager >(
|
|
|
|
"io.calamares.core", 1, 0, "Network", []( QQmlEngine*, QJSEngine* ) -> QObject* {
|
|
|
|
return &CalamaresUtils::Network::Manager::instance();
|
|
|
|
} );
|
2020-03-12 03:45:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-11 17:12:02 +01:00
|
|
|
|
2020-02-07 21:25:55 +01:00
|
|
|
} // namespace CalamaresUtils
|