/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Copyright (C) 2019 Luxoft Sweden AB ** Copyright (C) 2018 Pelagicore AG ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtApplicationManager module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 or (at your option) any later version ** approved by the KDE Free Qt Foundation. The licenses are as published by ** the Free Software Foundation and appearing in the file LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #if defined(QT_GUI_LIB) # include #endif #if defined(Q_OS_UNIX) # include #endif #include "global.h" #include "applicationinfo.h" #include "installationreport.h" #include "logging.h" #include "exception.h" #include "applicationmanager.h" #include "applicationmodel.h" #include "applicationmanager_p.h" #include "application.h" #include "runtimefactory.h" #include "containerfactory.h" #include "quicklauncher.h" #include "abstractruntime.h" #include "abstractcontainer.h" #include "qml-utilities.h" #include "utilities.h" #include "qtyaml.h" #include "debugwrapper.h" #include "amnamespace.h" #include "package.h" #include "packagemanager.h" #include /*! \qmltype ApplicationManager \inqmlmodule QtApplicationManager.SystemUI \ingroup system-ui-singletons \brief The application model and controller. The ApplicationManager singleton type is the core of the application manager. It provides both a DBus and a QML API for all of its functionality. The type is derived from \c QAbstractListModel, so it can be used directly as a model in app-grid views. \target ApplicationManager Roles The following roles are available in this model: \table \header \li Role name \li Type \li Description \row \li \c applicationId \li string \li The unique Id of an application, represented as a string (e.g. \c Browser or \c com.pelagicore.music) \row \li \c name \li string \li The name of the application. If possible, already translated to the current locale. \row \li \c icon \li string \li The URL of the application's icon. \row \li \c isRunning \li bool \li A boolean value representing the run-state of the application. \row \li \c isStartingUp \li bool \li A boolean value indicating whether the application is starting up and not fully operational yet. \row \li \c isShuttingDown \li bool \li A boolean value indicating whether the application is currently shutting down. \row \li \c isBlocked \li bool \li A boolean value that gets set when the application manager needs to block the application from running: this is normally only the case while an update is applied. \row \li \c isUpdating \li bool \li A boolean value indicating whether the application is currently being installed or updated. If \c true, the \c updateProgress can be used to track the actual progress. \row \li \c isRemovable \li bool \li A boolean value indicating whether this application is user-removable; \c true for all dynamically installed third party applications and \c false for all system applications. \row \li \c updateProgress \li real \li While \c isUpdating is \c true, querying this role returns the actual progress as a floating-point value in the \c 0.0 to \c 1.0 range. \row \li \c codeFilePath \li string \li The filesystem path to the main "executable". The format of this file is dependent on the actual runtime though: for QML applications the "executable" is the \c main.qml file. \row \li \c categories \li list \li The categories this application is registered for via its meta-data file. \row \li \c version \li string \li The currently installed version of this application. \row \li \c application \li Application \li The underlying application object for quick access to the properties outside of a model delegate. \endtable \note The index-based API is currently not available via DBus. However, the same functionality is provided by the id-based API. After importing, you can just use the ApplicationManager singleton as follows: \qml import QtQuick 2.0 import QtApplicationManager.SystemUI 2.0 ListView { id: appList model: ApplicationManager delegate: Text { text: name + "(" + applicationId + ")" MouseArea { anchors.fill: parent onClick: ApplicationManager.startApplication(applicationId) } } } \endqml */ /*! \qmlproperty int ApplicationManager::count \readonly This property holds the number of applications available. */ /*! \qmlproperty bool ApplicationManager::singleProcess \readonly This property indicates whether the application manager runs in single- or multi-process mode. */ /*! \qmlproperty bool ApplicationManager::shuttingDown \readonly This property is there to inform the System UI, when the application manager has entered its shutdown phase. New application starts are already prevented, but the System UI might want to impose additional restrictions in this state. */ /*! \qmlproperty bool ApplicationManager::securityChecksEnabled \readonly This property holds whether security related checks are enabled. \sa no-security */ /*! \qmlproperty var ApplicationManager::systemProperties \readonly Returns the project specific \l{system properties} that were set via the config file. */ /*! \qmlproperty var ApplicationManager::containerSelectionFunction A JavaScript function callback that will be called whenever the application manager needs to instantiate a container for running an application. See \l {Container Selection Configuration} for more information. */ /*! \qmlsignal ApplicationManager::applicationWasActivated(string id, string aliasId) This signal is emitted when an application identified by \a id is (re-)started via the ApplicationManager API, possibly through an alias, provided in \a aliasId. The window manager should take care of raising the application's window in this case. */ /*! \qmlsignal ApplicationManager::applicationRunStateChanged(string id, enumeration runState) This signal is emitted when the \a runState of the application identified by \a id changed. Possible values for the \l {ApplicationObject::runState} {runState} are defined by the ApplicationObject type. For example this signal can be used to restart an application in multi-process mode when it has crashed: \qml Connections { target: ApplicationManager function onApplicationRunStateChanged() { if (runState === Am.NotRunning && ApplicationManager.application(id).lastExitStatus === Am.CrashExit) { ApplicationManager.startApplication(id); } } } \endqml See also Application::runState */ /*! \qmlsignal ApplicationManager::applicationAdded(string id) This signal is emitted after a new application, identified by \a id, has been installed via the ApplicationInstaller. The model has already been update before this signal is sent out. \note In addition to the normal "low-level" QAbstractListModel signals, the application manager will also emit these "high-level" signals for System-UIs that cannot work directly on the ApplicationManager model: applicationAdded, applicationAboutToBeRemoved and applicationChanged. */ /*! \qmlsignal ApplicationManager::applicationAboutToBeRemoved(string id) This signal is emitted before an existing application, identified by \a id, is removed via the ApplicationInstaller. \note In addition to the normal "low-level" QAbstractListModel signals, the application manager will also emit these "high-level" signals for System-UIs that cannot work directly on the ApplicationManager model: applicationAdded, applicationAboutToBeRemoved and applicationChanged. */ /*! \qmlsignal ApplicationManager::applicationChanged(string id, list changedRoles) Emitted whenever one or more data roles, denoted by \a changedRoles, changed on the application identified by \a id. An empty list in the \a changedRoles argument means that all roles should be considered modified. \note In addition to the normal "low-level" QAbstractListModel signals, the application manager will also emit these "high-level" signals for System-UIs that cannot work directly on the ApplicationManager model: applicationAdded, applicationAboutToBeRemoved and applicationChanged. */ /*! \qmlproperty bool ApplicationManager::windowManagerCompositorReady \readonly This property starts with the value \c false and will change to \c true once the Wayland compositor is ready to accept connections from other processes. In multi-process mode, this happens implicitly after the System UI's main QML has been loaded. */ enum Roles { Id = Qt::UserRole, Name, Icon, IsRunning, IsStartingUp, IsShuttingDown, IsBlocked, IsUpdating, IsRemovable, UpdateProgress, CodeFilePath, RuntimeName, RuntimeParameters, Capabilities, Categories, Version, ApplicationItem, LastExitCode, LastExitStatus }; QT_BEGIN_NAMESPACE_AM ApplicationManagerPrivate::ApplicationManagerPrivate() { currentLocale = QLocale::system().name(); //TODO: language changes roleNames.insert(Id, "applicationId"); roleNames.insert(Name, "name"); roleNames.insert(Icon, "icon"); roleNames.insert(IsRunning, "isRunning"); roleNames.insert(IsStartingUp, "isStartingUp"); roleNames.insert(IsShuttingDown, "isShuttingDown"); roleNames.insert(IsBlocked, "isBlocked"); roleNames.insert(IsUpdating, "isUpdating"); roleNames.insert(IsRemovable, "isRemovable"); roleNames.insert(UpdateProgress, "updateProgress"); roleNames.insert(CodeFilePath, "codeFilePath"); roleNames.insert(RuntimeName, "runtimeName"); roleNames.insert(RuntimeParameters, "runtimeParameters"); roleNames.insert(Capabilities, "capabilities"); roleNames.insert(Categories, "categories"); roleNames.insert(Version, "version"); roleNames.insert(ApplicationItem, "application"); roleNames.insert(LastExitCode, "lastExitCode"); roleNames.insert(LastExitStatus, "lastExitStatus"); } ApplicationManagerPrivate::~ApplicationManagerPrivate() { for (const QString &scheme : qAsConst(registeredMimeSchemes)) QDesktopServices::unsetUrlHandler(scheme); qDeleteAll(apps); } ApplicationManager *ApplicationManager::s_instance = nullptr; ApplicationManager *ApplicationManager::createInstance(bool singleProcess) { if (Q_UNLIKELY(s_instance)) qFatal("ApplicationManager::createInstance() was called a second time."); std::unique_ptr am(new ApplicationManager(singleProcess)); qmlRegisterSingletonType("QtApplicationManager.SystemUI", 2, 0, "ApplicationManager", &ApplicationManager::instanceForQml); qmlRegisterType("QtApplicationManager.SystemUI", 2, 0, "ApplicationModel"); qmlRegisterUncreatableType("QtApplicationManager.SystemUI", 2, 0, "ApplicationObject", qSL("Cannot create objects of type ApplicationObject")); qRegisterMetaType("Application*"); qmlRegisterUncreatableType("QtApplicationManager.SystemUI", 2, 0, "Runtime", qSL("Cannot create objects of type Runtime")); qRegisterMetaType("AbstractRuntime*"); qmlRegisterUncreatableType("QtApplicationManager.SystemUI", 2, 0, "Container", qSL("Cannot create objects of type Container")); qRegisterMetaType("AbstractContainer*"); qmlRegisterUncreatableType("QtApplicationManager.SystemUI", 2, 0, "Am", qSL("Cannot create objects of type Am")); qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); if (Q_UNLIKELY(!PackageManager::instance())) qFatal("ApplicationManager::createInstance() was called before a PackageManager singleton was instantiated."); s_instance = am.release(); connect(&PackageManager::instance()->internalSignals, &PackageManagerInternalSignals::registerApplication, s_instance, [](ApplicationInfo *applicationInfo, Package *package) { instance()->addApplication(applicationInfo, package); qCDebug(LogSystem).nospace().noquote() << " ++ application: " << applicationInfo->id() << " [package: " << package->id() << "]"; }); connect(&PackageManager::instance()->internalSignals, &PackageManagerInternalSignals::unregisterApplication, s_instance, [](ApplicationInfo *applicationInfo, Package *package) { instance()->removeApplication(applicationInfo, package); qCDebug(LogSystem).nospace().noquote() << " -- application: " << applicationInfo->id() << " [package: " << package->id() << "]"; }); return s_instance; } ApplicationManager *ApplicationManager::instance() { if (!s_instance) qFatal("ApplicationManager::instance() was called before createInstance()."); return s_instance; } QObject *ApplicationManager::instanceForQml(QQmlEngine *, QJSEngine *) { QQmlEngine::setObjectOwnership(instance(), QQmlEngine::CppOwnership); return instance(); } ApplicationManager::ApplicationManager(bool singleProcess, QObject *parent) : QAbstractListModel(parent) , d(new ApplicationManagerPrivate()) { d->singleProcess = singleProcess; connect(this, &QAbstractItemModel::rowsInserted, this, &ApplicationManager::countChanged); connect(this, &QAbstractItemModel::rowsRemoved, this, &ApplicationManager::countChanged); connect(this, &QAbstractItemModel::layoutChanged, this, &ApplicationManager::countChanged); connect(this, &QAbstractItemModel::modelReset, this, &ApplicationManager::countChanged); } ApplicationManager::~ApplicationManager() { delete d; s_instance = nullptr; } bool ApplicationManager::isSingleProcess() const { return d->singleProcess; } bool ApplicationManager::isShuttingDown() const { return d->shuttingDown; } bool ApplicationManager::securityChecksEnabled() const { return d->securityChecksEnabled; } void ApplicationManager::setSecurityChecksEnabled(bool enabled) { d->securityChecksEnabled = enabled; } QVariantMap ApplicationManager::systemProperties() const { return d->systemProperties; } void ApplicationManager::setSystemProperties(const QVariantMap &map) { d->systemProperties = map; } void ApplicationManager::setContainerSelectionConfiguration(const QList> &containerSelectionConfig) { d->containerSelectionConfig = containerSelectionConfig; } QJSValue ApplicationManager::containerSelectionFunction() const { return d->containerSelectionFunction; } void ApplicationManager::setContainerSelectionFunction(const QJSValue &callback) { if (callback.isCallable() && !callback.equals(d->containerSelectionFunction)) { d->containerSelectionFunction = callback; emit containerSelectionFunctionChanged(); } } bool ApplicationManager::isWindowManagerCompositorReady() const { return d->windowManagerCompositorReady; } void ApplicationManager::setWindowManagerCompositorReady(bool ready) { if (d->windowManagerCompositorReady != ready) { d->windowManagerCompositorReady = ready; emit windowManagerCompositorReadyChanged(ready); } } QVector ApplicationManager::applications() const { return d->apps; } Application *ApplicationManager::fromId(const QString &id) const { for (Application *app : d->apps) { if (app->id() == id) return app; } return nullptr; } QVector ApplicationManager::fromProcessId(qint64 pid) const { QVector apps; // pid could be an indirect child (e.g. when started via gdbserver) qint64 appmanPid = QCoreApplication::applicationPid(); while ((pid > 1) && (pid != appmanPid)) { for (Application *app : d->apps) { if (apps.contains(app)) continue; if (app->currentRuntime() && (app->currentRuntime()->applicationProcessId() == pid)) apps.append(app); } pid = getParentPid(pid); } return apps; } Application *ApplicationManager::fromSecurityToken(const QByteArray &securityToken) const { if (securityToken.size() != AbstractRuntime::SecurityTokenSize) return nullptr; for (Application *app : d->apps) { if (app->currentRuntime() && app->currentRuntime()->securityToken() == securityToken) return app; } return nullptr; } QVector ApplicationManager::schemeHandlers(const QString &scheme) const { QVector handlers; for (Application *app : d->apps) { const auto mimeTypes = app->supportedMimeTypes(); for (const QString &mime : mimeTypes) { int pos = mime.indexOf(QLatin1Char('/')); if ((pos > 0) && (mime.left(pos) == qL1S("x-scheme-handler")) && (mime.mid(pos + 1) == scheme)) { handlers << app; } } } return handlers; } QVector ApplicationManager::mimeTypeHandlers(const QString &mimeType) const { QVector handlers; for (Application *app : d->apps) { if (app->supportedMimeTypes().contains(mimeType)) handlers << app; } return handlers; } void ApplicationManager::registerMimeTypes() { #if defined(QT_GUI_LIB) QSet schemes; schemes << qSL("file") << qSL("http") << qSL("https"); for (Application *app : qAsConst(d->apps)) { const auto mimeTypes = app->supportedMimeTypes(); for (const QString &mime : mimeTypes) { int pos = mime.indexOf(QLatin1Char('/')); if ((pos > 0) && (mime.left(pos) == qL1S("x-scheme-handler"))) schemes << mime.mid(pos + 1); } } QSet registerSchemes = schemes; registerSchemes.subtract(d->registeredMimeSchemes); QSet unregisterSchemes = d->registeredMimeSchemes; unregisterSchemes.subtract(schemes); for (const QString &scheme : qAsConst(unregisterSchemes)) QDesktopServices::unsetUrlHandler(scheme); for (const QString &scheme : qAsConst(registerSchemes)) QDesktopServices::setUrlHandler(scheme, this, "openUrlRelay"); d->registeredMimeSchemes = schemes; #endif } bool ApplicationManager::startApplicationInternal(const QString &appId, const QString &documentUrl, const QString &documentMimeType, const QString &debugWrapperSpecification, const QVector &stdioRedirections) Q_DECL_NOEXCEPT_EXPR(false) { if (d->shuttingDown) throw Exception("Cannot start applications during shutdown"); Application *app = fromId(appId); if (!app) throw Exception("Cannot start application: id '%1' is not known").arg(appId); if (app->isBlocked()) throw Exception("Application %1 is blocked - cannot start").arg( app->id()); AbstractRuntime *runtime = app->currentRuntime(); auto runtimeManager = runtime ? runtime->manager() : RuntimeFactory::instance()->manager(app->runtimeName()); if (!runtimeManager) throw Exception("No RuntimeManager found for runtime: %1").arg(app->runtimeName()); bool inProcess = runtimeManager->inProcess(); // validate stdio redirections if (stdioRedirections.size() > 3) { throw Exception("Tried to start application %1 using an invalid standard IO redirection specification") .arg(app->id()); } bool hasStdioRedirections = !stdioRedirections.isEmpty(); if (hasStdioRedirections) { // we have an array - check if it just consists of -1 fds hasStdioRedirections = false; std::for_each(stdioRedirections.cbegin(), stdioRedirections.cend(), [&hasStdioRedirections](int fd) { if (fd >= 0) hasStdioRedirections = true; }); } // validate the debug-wrapper QStringList debugWrapperCommand; QMap debugEnvironmentVariables; if (!debugWrapperSpecification.isEmpty()) { if (isSingleProcess()) throw Exception("Using debug-wrappers is not supported when the application manager is running in single-process mode."); if (inProcess) { throw Exception("Using debug-wrappers is not supported when starting an app using an in-process runtime (%1).") .arg(runtimeManager->identifier()); } if (!DebugWrapper::parseSpecification(debugWrapperSpecification, debugWrapperCommand, debugEnvironmentVariables)) { throw Exception("Tried to start application %1 using an invalid debug-wrapper specification: %2") .arg(app->id(), debugWrapperSpecification); } } if (runtime) { switch (runtime->state()) { case Am::StartingUp: case Am::Running: if (!debugWrapperCommand.isEmpty()) { throw Exception("Application %1 is already running - cannot start with debug-wrapper: %2") .arg(app->id(), debugWrapperSpecification); } if (!documentUrl.isNull()) runtime->openDocument(documentUrl, documentMimeType); else if (!app->documentUrl().isNull()) runtime->openDocument(app->documentUrl(), documentMimeType); emitActivated(app); return true; case Am::ShuttingDown: return false; case Am::NotRunning: break; } } AbstractContainer *container = nullptr; QString containerId; if (!inProcess) { if (d->containerSelectionConfig.isEmpty()) { containerId = qSL("process"); } else { // check config file for (const auto &it : qAsConst(d->containerSelectionConfig)) { const QString &key = it.first; const QString &value = it.second; bool hasAsterisk = key.contains(qL1C('*')); if ((hasAsterisk && key.length() == 1) || (!hasAsterisk && key == app->id()) || QRegularExpression(QRegularExpression::wildcardToRegularExpression(key)).match(app->id()).hasMatch()) { containerId = value; break; } } } if (d->containerSelectionFunction.isCallable()) { QJSValueList args = { QJSValue(app->id()), QJSValue(containerId) }; containerId = d->containerSelectionFunction.call(args).toString(); } if (!ContainerFactory::instance()->manager(containerId)) throw Exception("No ContainerManager found for container: %1").arg(containerId); } bool attachRuntime = false; if (!runtime) { if (!inProcess) { if (QuickLauncher::instance()) { // we cannot use the quicklaunch pool, if // (a) a debug-wrapper is being used, // (b) stdio is redirected or // (c) the app requests special environment variables or // (d) the app requests a different OpenGL config from the AM const char *cannotUseQuickLaunch = nullptr; if (!debugWrapperCommand.isEmpty()) cannotUseQuickLaunch = "the app is started using a debug-wrapper"; else if (hasStdioRedirections) cannotUseQuickLaunch = "standard I/O is redirected"; else if (!app->runtimeParameters().value(qSL("environmentVariables")).toMap().isEmpty()) cannotUseQuickLaunch = "the app requests custom environment variables"; else if (app->info()->openGLConfiguration() != runtimeManager->systemOpenGLConfiguration()) cannotUseQuickLaunch = "the app requests a custom OpenGL configuration"; if (cannotUseQuickLaunch) { qCDebug(LogSystem) << "Cannot use quick-launch for application" << app->id() << "because" << cannotUseQuickLaunch; } else { // check quicklaunch pool QPair quickLaunch = QuickLauncher::instance()->take(containerId, app->info()->runtimeName()); container = quickLaunch.first; runtime = quickLaunch.second; if (container || runtime) { qCDebug(LogSystem) << "Found a quick-launch entry for container" << containerId << "and runtime" << app->info()->runtimeName() << "->" << container << runtime; if (!container && runtime) { runtime->deleteLater(); qCCritical(LogSystem) << "ERROR: QuickLauncher provided a runtime without a container."; return false; } } } } if (!container) { container = ContainerFactory::instance()->create(containerId, app, stdioRedirections, debugEnvironmentVariables, debugWrapperCommand); } else { container->setApplication(app); } if (!container) { qCCritical(LogSystem) << "ERROR: Couldn't create Container for Application (" << app->id() <<")!"; return false; } if (runtime) attachRuntime = true; } if (!runtime) runtime = RuntimeFactory::instance()->create(container, app); if (runtime) emit internalSignals.newRuntimeCreated(runtime); } if (!runtime) { qCCritical(LogSystem) << "ERROR: Couldn't create Runtime for Application (" << app->id() <<")!"; return false; } connect(runtime, &AbstractRuntime::stateChanged, this, [this, app](Am::RunState newRuntimeState) { app->setRunState(newRuntimeState); emit applicationRunStateChanged(app->id(), newRuntimeState); emitDataChanged(app, QVector { IsRunning, IsStartingUp, IsShuttingDown }); }); if (!documentUrl.isNull()) runtime->openDocument(documentUrl, documentMimeType); else if (!app->documentUrl().isNull()) runtime->openDocument(app->documentUrl(), documentMimeType); qCDebug(LogSystem) << "Starting application" << app->id() << "in container" << containerId << "using runtime" << runtimeManager->identifier(); if (!documentUrl.isEmpty()) qCDebug(LogSystem) << " documentUrl:" << documentUrl; if (inProcess) { bool ok = runtime->start(); if (ok) emitActivated(app); else runtime->deleteLater(); return ok; } else { // We can only start the app when both the container and the windowmanager are ready. // Using a state-machine would be one option, but then we would need that state-machine // object plus the per-app state. Relying on 2 lambdas is the easier choice for now. auto doStartInContainer = [this, app, attachRuntime, runtime]() -> bool { bool successfullyStarted = attachRuntime ? runtime->attachApplicationToQuickLauncher(app) : runtime->start(); if (successfullyStarted) emitActivated(app); else runtime->deleteLater(); // ~Runtime() will clean app->nonAliased()->m_runtime return successfullyStarted; }; auto tryStartInContainer = [container, doStartInContainer]() -> bool { if (container->isReady()) { // Since the container is already ready, start the app immediately return doStartInContainer(); } else { // We postpone the starting of the application to a later point in time, // since the container is not ready yet # if defined(Q_CC_MSVC) qApp->connect(container, &AbstractContainer::ready, doStartInContainer); // MSVC cannot distinguish between static and non-static overloads in lambdas # else connect(container, &AbstractContainer::ready, doStartInContainer); #endif return true; } }; if (isWindowManagerCompositorReady()) { return tryStartInContainer(); } else { connect(this, &ApplicationManager::windowManagerCompositorReadyChanged, tryStartInContainer); return true; } } } void ApplicationManager::stopApplicationInternal(Application *app, bool forceKill) { if (!app) return; AbstractRuntime *rt = app->currentRuntime(); if (rt) rt->stop(forceKill); } /*! \qmlmethod bool ApplicationManager::startApplication(string id, string document) Instructs the application manager to start the application identified by its unique \a id. The optional argument \a document will be supplied to the application as is - most commonly this is used to refer to a document to display. Returns \c true if the application \a id is valid and the application manager was able to start the runtime plugin. Returns \c false otherwise. Note that even though this call may indicate success, the application may still later fail to start correctly as the actual startup process within the runtime plugin may be asynchronous. \sa ApplicationObject::start */ bool ApplicationManager::startApplication(const QString &id, const QString &documentUrl) { try { return startApplicationInternal(id, documentUrl); } catch (const Exception &e) { qCWarning(LogSystem) << e.what(); return false; } } /*! \qmlmethod bool ApplicationManager::debugApplication(string id, string debugWrapper, string document) Instructs the application manager to start the application identified by its unique \a id, just like startApplication. The application is started via the given \a debugWrapper though. The optional argument \a document will be supplied to the application as is - most commonly this is used to refer to a document to display. Returns a \c bool value indicating success. See the full documentation at ApplicationManager::startApplication for more information. Please see the \l{Debugging} page for more information on how to setup and use these debug-wrappers. \sa ApplicationObject::debug */ bool ApplicationManager::debugApplication(const QString &id, const QString &debugWrapper, const QString &documentUrl) { try { return startApplicationInternal(id, documentUrl, QString(), debugWrapper); } catch (const Exception &e) { qCWarning(LogSystem) << e.what(); return false; } } /*! \qmlmethod ApplicationManager::stopApplication(string id, bool forceKill) Tells the application manager to stop an application identified by its unique \a id. The meaning of the \a forceKill parameter is runtime dependent, but in general you should always try to stop an application with \a forceKill set to \c false first in order to allow a clean shutdown. Use \a forceKill set to \c true only as a last resort to kill hanging applications. \sa ApplicationObject::stop */ void ApplicationManager::stopApplication(const QString &id, bool forceKill) { return stopApplicationInternal(fromId(id), forceKill); } /*! \qmlmethod ApplicationManager::stopAllApplications(bool forceKill) Tells the application manager to stop all running applications. The meaning of the \a forceKill parameter is runtime dependent, but in general you should always try to stop an application with \a forceKill set to \c false first in order to allow a clean shutdown. Use \a forceKill set to \c true only as a last resort to kill hanging applications. */ void ApplicationManager::stopAllApplications(bool forceKill) { for (Application *app : qAsConst(d->apps)) { AbstractRuntime *rt = app->currentRuntime(); if (rt) rt->stop(forceKill); } } /*! \qmlmethod bool ApplicationManager::openUrl(string url) Tries start an application that is capable of handling \a url. The application manager will first look at the URL's scheme: \list \li If it is \c{file:}, the operating system's MIME database will be consulted, which will try to find a MIME type match, based on file endings or file content. In case this is successful, the application manager will use this MIME type to find all of its applications that claim support for it (see the \l{mimeTypes field} in the application's manifest). A music player application that can handle \c mp3 and \c wav files, could add this to its manifest: \badcode mimeTypes: [ 'audio/mpeg', 'audio/wav' ] \endcode \li If it is something other than \c{file:}, the application manager will consult its internal database of applications that claim support for a matching \c{x-scheme-handler/...} MIME type. In order to have your web-browser application handle \c{http:} and \c{https:} URLs, you would have to have this in your application's manifest: \badcode mimeTypes: [ 'x-scheme-handler/http', 'x-scheme-handler/https' ] \endcode \endlist If there is at least one possible match, it depends on the signal openUrlRequested() being connected within the System UI: In case the signal is not connected, an arbitrary application from the matching set will be started. Otherwise the application manager will emit the openUrlRequested signal and return \c true. It is up to the receiver of this signal to choose from one of possible applications via acknowledgeOpenUrlRequest or deny the request entirely via rejectOpenUrlRequest. Not calling one of these two functions will result in memory leaks. If an application is started by one of the two mechanisms, the \a url is supplied to the application as a document to open via its ApplicationInterface. Returns \c true, if a match was found in the database, or \c false otherwise. \sa openUrlRequested, acknowledgeOpenUrlRequest, rejectOpenUrlRequest */ bool ApplicationManager::openUrl(const QString &urlStr) { //TODO: relay to a well-known Intent call // QDesktopServices::openUrl has a special behavior when called recursively, which makes sense // on the desktop, but is completely counter-productive for the AM. static bool recursionGuard = false; if (recursionGuard) { QMetaObject::invokeMethod(this, [urlStr, this]() { openUrl(urlStr); }, Qt::QueuedConnection); return true; // this is not correct, but the best we can do in this situation } recursionGuard = true; QUrl url(urlStr); QString mimeTypeName; QVector apps; if (url.isValid()) { QString scheme = url.scheme(); if (scheme != qL1S("file")) apps = schemeHandlers(scheme); if (apps.isEmpty()) { QMimeDatabase mdb; QMimeType mt = mdb.mimeTypeForUrl(url); mimeTypeName = mt.name(); apps = mimeTypeHandlers(mimeTypeName); } } if (!apps.isEmpty()) { if (!isSignalConnected(QMetaMethod::fromSignal(&ApplicationManager::openUrlRequested))) { // If the System UI does not react to the signal, then just use the first match. try { startApplicationInternal(apps.constFirst()->id(), urlStr, mimeTypeName); } catch (const Exception &e) { qCWarning(LogSystem) << "openUrl for" << urlStr << "requested app" << apps.constFirst()->id() << "which could not be started:" << e.errorString(); } } else { ApplicationManagerPrivate::OpenUrlRequest req { QUuid::createUuid().toString(), urlStr, mimeTypeName, QStringList() }; for (const auto &app : qAsConst(apps)) req.possibleAppIds << app->id(); d->openUrlRequests << req; emit openUrlRequested(req.requestId, req.urlStr, req.mimeTypeName, req.possibleAppIds); } } recursionGuard = false; return !apps.isEmpty(); } /*! \qmlsignal ApplicationManager::openUrlRequested(string requestId, string url, string mimeType, list possibleAppIds) This signal is emitted when the application manager is requested to open an URL. This can happen by calling \list \li Qt.openUrlExternally in an application, \li Qt.openUrlExternally in the System UI, \li ApplicationManager::openUrl in the System UI or \li \c io.qt.ApplicationManager.openUrl via D-Bus \endlist \note This signal is only emitted, if there is a receiver connected at all - see openUrl for the fallback behavior. The receiver of this signal can inspect the requested \a url an its \a mimeType. It can then either call acknowledgeOpenUrlRequest to choose from one of the supplied \a possibleAppIds or rejectOpenUrlRequest to ignore the request. In both cases the unique \a requestId needs to be sent to identify the request. Not calling one of these two functions will result in memory leaks. \sa openUrl, acknowledgeOpenUrlRequest, rejectOpenUrlRequest */ /*! \qmlmethod ApplicationManager::acknowledgeOpenUrlRequest(string requestId, string appId) Tells the application manager to go ahead with the request to open an URL, identified by \a requestId. The chosen \a appId needs to be one of the \c possibleAppIds supplied to the receiver of the openUrlRequested signal. \sa openUrl, openUrlRequested */ void ApplicationManager::acknowledgeOpenUrlRequest(const QString &requestId, const QString &appId) { for (auto it = d->openUrlRequests.cbegin(); it != d->openUrlRequests.cend(); ++it) { if (it->requestId == requestId) { if (it->possibleAppIds.contains(appId)) { try { startApplicationInternal(appId, it->urlStr, it->mimeTypeName); } catch (const Exception &e) { qCWarning(LogSystem) << "acknowledgeOpenUrlRequest for" << it->urlStr << "requested app" << appId << "which could not be started:" << e.errorString(); } } else { qCWarning(LogSystem) << "acknowledgeOpenUrlRequest for" << it->urlStr << "requested app" << appId << "which is not one of the registered possibilities:" << it->possibleAppIds; } d->openUrlRequests.erase(it); break; } } } /*! \qmlmethod ApplicationManager::rejectOpenUrlRequest(string requestId) Tells the application manager to ignore the request to open an URL, identified by \a requestId. \sa openUrl, openUrlRequested */ void ApplicationManager::rejectOpenUrlRequest(const QString &requestId) { for (auto it = d->openUrlRequests.cbegin(); it != d->openUrlRequests.cend(); ++it) { if (it->requestId == requestId) { d->openUrlRequests.erase(it); break; } } } /*! \qmlmethod list ApplicationManager::capabilities(string id) Returns a list of all capabilities granted by the user to the application identified by \a id. Returns an empty list if the application \a id is not valid. */ QStringList ApplicationManager::capabilities(const QString &id) const { Application *app = fromId(id); return app ? app->capabilities() : QStringList(); } /*! \qmlmethod string ApplicationManager::identifyApplication(int pid) Validates the process running with process-identifier \a pid as a process started by the application manager. \note If multiple applications are running within the same container process, this function will return only the first matching application. See identifyAllApplications() for a way to retrieve all application ids. Returns the application's \c id on success, or an empty string on failure. */ QString ApplicationManager::identifyApplication(qint64 pid) const { const auto apps = fromProcessId(pid); return !apps.isEmpty() ? apps.constFirst()->id() : QString(); } /*! \qmlmethod list ApplicationManager::identifyAllApplications(int pid) Validates the process running with process-identifier \a pid as a process started by the application manager. If multiple applications are running within the same container process, this function will return all those application ids. Returns a list with the applications' \c ids on success, or an empty list on failure. */ QStringList ApplicationManager::identifyAllApplications(qint64 pid) const { const auto apps = fromProcessId(pid); QStringList result; result.reserve(apps.size()); for (const auto &app : apps) result << app->id(); return result; } void ApplicationManager::shutDown() { d->shuttingDown = true; emit shuttingDownChanged(); auto shutdownHelper = [this]() { bool activeRuntime = false; for (Application *app : qAsConst(d->apps)) { AbstractRuntime *rt = app->currentRuntime(); if (rt) { activeRuntime = true; break; } } if (!activeRuntime) emit shutDownFinished(); }; for (Application *app : qAsConst(d->apps)) { AbstractRuntime *rt = app->currentRuntime(); if (rt) { connect(rt, &AbstractRuntime::destroyed, this, shutdownHelper); rt->stop(); } } shutdownHelper(); } void ApplicationManager::openUrlRelay(const QUrl &url) { if (QThread::currentThread() != thread()) { staticMetaObject.invokeMethod(this, "openUrlRelay", Qt::QueuedConnection, Q_ARG(QUrl, url)); return; } openUrl(url.toString()); } void ApplicationManager::emitDataChanged(Application *app, const QVector &roles) { int row = d->apps.indexOf(app); if (row >= 0) { emit dataChanged(index(row), index(row), roles); static const auto appChanged = QMetaMethod::fromSignal(&ApplicationManager::applicationChanged); if (isSignalConnected(appChanged)) { QStringList stringRoles; for (auto role : roles) stringRoles << qL1S(d->roleNames[role]); emit applicationChanged(app->id(), stringRoles); } } } void ApplicationManager::emitActivated(Application *app) { emit applicationWasActivated(app->id(), app->id()); emit app->activated(); } // item model part int ApplicationManager::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return d->apps.count(); } QVariant ApplicationManager::data(const QModelIndex &index, int role) const { if (index.parent().isValid() || !index.isValid()) return QVariant(); Application *app = d->apps.at(index.row()); switch (role) { case Id: return app->id(); case Name: return app->name(); case Icon: return app->icon(); case IsRunning: return app->currentRuntime() ? (app->currentRuntime()->state() == Am::Running) : false; case IsStartingUp: return app->currentRuntime() ? (app->currentRuntime()->state() == Am::StartingUp) : false; case IsShuttingDown: return app->currentRuntime() ? (app->currentRuntime()->state() == Am::ShuttingDown) : false; case IsBlocked: return app->isBlocked(); case IsUpdating: return app->state() != Application::Installed; case UpdateProgress: return app->progress(); case IsRemovable: return !app->isBuiltIn(); case CodeFilePath: return app->info()->absoluteCodeFilePath(); case RuntimeName: return app->runtimeName(); case RuntimeParameters: return app->runtimeParameters(); case Capabilities: return app->capabilities(); case Categories: return app->categories(); case Version: return app->version(); case ApplicationItem: return QVariant::fromValue(app); case LastExitCode: return app->lastExitCode(); case LastExitStatus: return app->lastExitStatus(); } return QVariant(); } QHash ApplicationManager::roleNames() const { return d->roleNames; } int ApplicationManager::count() const { return rowCount(); } /*! \qmlmethod object ApplicationManager::get(int index) Retrieves the model data at \a index as a JavaScript object. See the \l {ApplicationManager Roles}{role names} for the expected object fields. Returns an empty object if the specified \a index is invalid. \note This is very inefficient if you only want to access a single property from QML; use application() instead to access the Application object's properties directly. */ QVariantMap ApplicationManager::get(int index) const { if (index < 0 || index >= count()) { qCWarning(LogSystem) << "ApplicationManager::get(index): invalid index:" << index; return QVariantMap(); } QVariantMap map; QHash roles = roleNames(); for (auto it = roles.begin(); it != roles.end(); ++it) map.insert(qL1S(it.value()), data(this->index(index), it.key())); return map; } /*! \qmlmethod ApplicationObject ApplicationManager::application(int index) Returns the \l{ApplicationObject}{application} corresponding to the given \a index in the model, or \c null if the index is invalid. \note The object ownership of the returned Application object stays with the application manager. If you want to store this pointer, you can use the ApplicationManager's QAbstractListModel signals or the applicationAboutToBeRemoved signal to get notified if the object is about to be deleted on the C++ side. */ Application *ApplicationManager::application(int index) const { if (index < 0 || index >= count()) { qCWarning(LogSystem) << "ApplicationManager::application(index): invalid index:" << index; return nullptr; } return d->apps.at(index); } /*! \qmlmethod ApplicationObject ApplicationManager::application(string id) Returns the \l{ApplicationObject}{application} corresponding to the given application \a id, or \c null if the id does not exist. \note The object ownership of the returned Application object stays with the application manager. If you want to store this pointer, you can use the ApplicationManager's QAbstractListModel signals or the applicationAboutToBeRemoved signal to get notified if the object is about to be deleted on the C++ side. */ Application *ApplicationManager::application(const QString &id) const { auto index = indexOfApplication(id); return (index < 0) ? nullptr : application(index); } /*! \qmlmethod int ApplicationManager::indexOfApplication(string id) Maps the application corresponding to the given \a id to its position within the model. Returns \c -1 if the specified \a id is invalid. */ int ApplicationManager::indexOfApplication(const QString &id) const { for (int i = 0; i < d->apps.size(); ++i) { if (d->apps.at(i)->id() == id) return i; } return -1; } /*! \qmlmethod int ApplicationManager::indexOfApplication(ApplicationObject application) Maps the \a application to its position within this model. Returns \c -1 if the specified application is invalid. */ int ApplicationManager::indexOfApplication(Application *application) const { return d->apps.indexOf(application); } /*! \qmlmethod list ApplicationManager::applicationIds() Returns a list of all available application ids. This can be used to further query for specific information via get(). */ QStringList ApplicationManager::applicationIds() const { QStringList ids; ids.reserve(d->apps.size()); for (int i = 0; i < d->apps.size(); ++i) ids << d->apps.at(i)->id(); return ids; } /*! \qmlmethod object ApplicationManager::get(string id) Retrieves the model data for the application identified by \a id as a JavaScript object. See the \l {ApplicationManager Roles}{role names} for the expected object fields. Returns an empty object if the specified \a id is invalid. */ QVariantMap ApplicationManager::get(const QString &id) const { int index = indexOfApplication(id); return (index < 0) ? QVariantMap{} : get(index); } Am::RunState ApplicationManager::applicationRunState(const QString &id) const { int index = indexOfApplication(id); return (index < 0) ? Am::NotRunning : d->apps.at(index)->runState(); } void ApplicationManager::addApplication(ApplicationInfo *appInfo, Package *package) { // check for id clashes outside of the package (the scanner made sure the package itself is // consistent and doesn't have duplicates already) for (Application *checkApp : qAsConst(d->apps)) { if ((checkApp->id() == appInfo->id()) && (checkApp->package() != package)) { throw Exception("found an application with the same id in package %1") .arg(checkApp->packageInfo()->id()); } } auto app = new Application(appInfo, package); QQmlEngine::setObjectOwnership(app, QQmlEngine::CppOwnership); app->requests.startRequested = [this, app](const QString &documentUrl) { return startApplication(app->id(), documentUrl); }; app->requests.debugRequested = [this, app](const QString &debugWrapper, const QString &documentUrl) { return debugApplication(app->id(), debugWrapper, documentUrl); }; app->requests.stopRequested = [this, app](bool forceKill) { stopApplication(app->id(), forceKill); }; connect(app, &Application::blockedChanged, this, [this, app]() { emitDataChanged(app, QVector { IsBlocked }); }); connect(app, &Application::bulkChange, this, [this, app]() { emitDataChanged(app); }); beginInsertRows(QModelIndex(), d->apps.count(), d->apps.count()); d->apps << app; endInsertRows(); registerMimeTypes(); package->addApplication(app); emit applicationAdded(appInfo->id()); } void ApplicationManager::removeApplication(ApplicationInfo *appInfo, Package *package) { int index = -1; for (int i = 0; i < d->apps.size(); ++i) { if (d->apps.at(i)->info() == appInfo) { index = i; break; } } if (index < 0) return; Q_ASSERT(d->apps.at(index)->package() == package); emit applicationAboutToBeRemoved(appInfo->id()); package->removeApplication(d->apps.at(index)); beginRemoveRows(QModelIndex(), index, index); auto app = d->apps.takeAt(index); endRemoveRows(); registerMimeTypes(); delete app; } QT_END_NAMESPACE_AM #include "moc_applicationmanager.cpp"