summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/application.cpp
blob: 722ad7532cd6a7895b410c0eaa0ca2e09e9b9d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite 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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or 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.GPL2 and 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "application.h"
#include "abstractruntime.h"
#include "applicationinfo.h"
#include "package.h"
#include "exception.h"
#include "logging.h"

#include <QDebug>

#if defined(Q_OS_UNIX)
#  include <signal.h>
#endif

/*!
    \qmltype ApplicationObject
    \inqmlmodule QtApplicationManager.SystemUI
    \ingroup system-ui-non-instantiable
    \brief The handle for an application known to the ApplicationManager.

    Most of the read-only properties map directly to values read from the application's
    \c info.yaml file - these are documented in the \l{Manifest Definition}.
*/

/*!
    \qmlproperty string ApplicationObject::id
    \readonly

    This property returns the unique id of the application.
*/
/*!
    \qmlproperty string ApplicationObject::runtimeName
    \readonly

    This property holds the name of the runtime, necessary to run the application's code.
*/
/*!
    \qmlproperty object ApplicationObject::runtimeParameters
    \readonly

    This property holds a QVariantMap that is passed onto, and interpreted by the application's
    runtime.
*/
/*!
    \qmlproperty url ApplicationObject::icon
    \readonly

    The URL of the application's icon - can be used as the source property of an \l Image.
*/
/*!
    \qmlproperty string ApplicationObject::documentUrl
    \readonly
    \obsolete

    This was used to distinguish between application aliases, which have been replaced by the
    intents mechanism.

    Always returns an empty string.
*/
/*!
    \qmlproperty bool ApplicationObject::builtIn
    \readonly

    This property describes, if this application is part of the built-in set of applications of the
    current System-UI.
*/
/*!
    \qmlproperty bool ApplicationObject::alias
    \readonly
    \obsolete

    This was used to distinguish between application aliases, which have been replaced by the
    intents mechanism.

    Always returns \c false.
*/
/*!
    \qmlproperty ApplicationObject ApplicationObject::nonAliased
    \readonly
    \obsolete

    This was used to distinguish between application aliases, which have been replaced by the
    intents mechanism.

    Always returns the object itself.
*/
/*!
    \qmlproperty list<string> ApplicationObject::capabilities
    \readonly

    A list of special access rights for the application - these capabilities can later be queried
    and verified by the middleware via the application-manager.
*/
/*!
    \qmlproperty list<string> ApplicationObject::supportedMimeTypes
    \readonly

    An array of MIME types the application can handle.
*/
/*!
    \qmlproperty list<string> ApplicationObject::categories
    \readonly

    A list of category names the application should be associated with. This is mainly for the
    automated app-store uploads as well as displaying the application within a fixed set of
    categories in the System-UI.
*/
/*!
    \qmlproperty object ApplicationObject::applicationProperties
    \readonly

    All user-defined properties of this application as listed in the private and protected sections
    of the \c applicationProperties field in the manifest file.
*/
/*!
    \qmlproperty Runtime ApplicationObject::runtime
    \readonly

    Will return a valid \l Runtime object, if the application is currently starting, running or
    shutting down. May return a \c null object, if the application was not yet started.
*/
/*!
    \qmlproperty int ApplicationObject::lastExitCode
    \readonly

    This property holds the last exit-code of the application's process in multi-process mode.
    On successful application shutdown, this value should normally be \c 0, but can be whatever
    the application returns from its \c main() function.
*/
/*!
    \qmlproperty enumeration ApplicationObject::lastExitStatus
    \readonly

    This property returns the last exit-status of the application's process in multi-process mode.

    \list
    \li Am.NormalExit - The application exited normally.
    \li Am.CrashExit - The application crashed.
    \li Am.ForcedExit - The application was killed by the application-manager, since it
                        ignored the quit request originating from a call to
                        ApplicationManager::stopApplication.
    \endlist

    \sa ApplicationInterface::quit, ApplicationInterface::acknowledgeQuit
*/
/*!
    \qmlproperty string ApplicationObject::version
    \readonly

    Holds the version of the application as a string.
*/
/*!
    \qmlproperty string ApplicationObject::codeDir
    \readonly

    The absolute path to the application's installation directory.
*/
/*!
    \qmlproperty enumeration ApplicationObject::state
    \readonly

    This property holds the current installation state of the application. It can be one of:

    \list
    \li ApplicationObject.Installed - The application is completely installed and ready to be used.
    \li ApplicationObject.BeingInstalled - The application is currently in the process of being installed.
    \li ApplicationObject.BeingUpdated - The application is currently in the process of being updated.
    \li ApplicationObject.BeingDowngraded - The application is currently in the process of being downgraded.
                                            That can only happen for a built-in application that was previously
                                            upgraded. It will then be brought back to its original, built-in,
                                            version and its state will go back to ApplicationObject.Installed.
    \li ApplicationObject.BeingRemoved - The application is currently in the process of being removed.
    \endlist
*/
/*!
    \qmlproperty enumeration ApplicationObject::runState
    \readonly

    This property holds the current run state of the application. It can be one of:

    \list
    \li Am.NotRunning - the application has not been started yet.
    \li Am.StartingUp - the application has been started and is initializing.
    \li Am.Running - the application is running.
    \li Am.ShuttingDown - the application has been stopped and is cleaning up (in multi-process mode
                          this state is only reached, if the application is terminating gracefully).
    \endlist
*/
/*!
    \qmlsignal ApplicationObject::activated()

    This signal is emitted when the application is started or when it's already running but has
    been requested to be brought to foreground or raised.
*/
/*!
    \qmlmethod string ApplicationObject::name(string language)

    Returns the name of the application in the given \a language, as provided in the the info.yaml
    file.
*/
/*!
    \qmlmethod bool ApplicationObject::start(string document)

    Starts the application. 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.
*/
/*!
    \qmlmethod bool ApplicationObject::debug(string debugWrapper, string document)

    Same as start() with the difference that it is started via the given \a debugWrapper. 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.

    Please see the \l{Debugging} page for more information on how to setup and use these debug-wrappers.

    Returns a \c bool value indicating success. See the full documentation at
    ApplicationManager::debugApplication for more information.
*/
/*!
    \qmlmethod ApplicationObject::stop(bool forceKill)

    Stops the application. 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 ApplicationManager::stopApplication
*/

QT_BEGIN_NAMESPACE_AM


///////////////////////////////////////////////////////////////////////////////////////////////////
// Application
///////////////////////////////////////////////////////////////////////////////////////////////////

Application::Application(ApplicationInfo *info, Package *package)
    : m_info(info)
    , m_package(package)
{
    Q_ASSERT(info);
    Q_ASSERT(package);

    // handle package blocking: all apps have to be stopped and the stop state has to be reported
    // back to the package
    connect(package, &Package::blockedChanged, this, [this](bool blocked) {
        emit blockedChanged(blocked);
        if (blocked && (runState() == Am::NotRunning))
            this->package()->applicationStoppedDueToBlock(id());
        else if (blocked)
            stop(true);
    });
    connect(this, &Application::runStateChanged, this, [this](Am::RunState runState) {
        if (isBlocked() && (runState == Am::NotRunning))
            this->package()->applicationStoppedDueToBlock(id());
    });
}

bool Application::start(const QString &documentUrl)
{
    if (requests.startRequested)
        return requests.startRequested(documentUrl);
    else
        return false;
}

bool Application::debug(const QString &debugWrapper, const QString &documentUrl)
{
    if (requests.debugRequested)
        return requests.debugRequested(debugWrapper, documentUrl);
    else
        return false;
}

void Application::stop(bool forceKill)
{
    if (requests.stopRequested)
        requests.stopRequested(forceKill);
}

ApplicationInfo *Application::info() const
{
    return m_info.data();
}

PackageInfo *Application::packageInfo() const
{
    return m_info->packageInfo();
}

Package *Application::package() const
{
    return m_package;
}

QString Application::id() const
{
    return m_info->id();
}

bool Application::isBuiltIn() const
{
    return packageInfo()->isBuiltIn();
}

QString Application::runtimeName() const
{
    return m_info->runtimeName();
}

QVariantMap Application::runtimeParameters() const
{
    return m_info->runtimeParameters();
}

QStringList Application::capabilities() const
{
    return m_info->capabilities();
}

QStringList Application::categories() const
{
    return package()->categories();
}

QUrl Application::icon() const
{
    return package()->icon();
}

QStringList Application::supportedMimeTypes() const
{
    return info()->supportedMimeTypes();
}

QString Application::name() const
{
    return package()->name();
}

QString Application::name(const QString &language) const
{
    return package()->names().value(language).toString();
}

bool Application::isBlocked() const
{
    return package()->isBlocked();
}

QVariantMap Application::applicationProperties() const
{
    return info()->applicationProperties();
}

bool Application::supportsApplicationInterface() const
{
    return info()->supportsApplicationInterface();
}

QString Application::codeDir() const
{
    switch (package()->state()) {
    default:
    case Package::Installed:
        return packageInfo()->baseDir().absolutePath();
    case Package::BeingInstalled:
    case Package::BeingUpdated:
        return packageInfo()->baseDir().absolutePath() + QLatin1Char('+');
    case Package::BeingRemoved:
        return packageInfo()->baseDir().absolutePath() + QLatin1Char('-');
    }
}

QString Application::version() const
{
    return packageInfo()->version();
}

Application::State Application::state() const
{
    return static_cast<State>(package()->state());
}

qreal Application::progress() const
{
    return package()->progress();
}

void Application::setRunState(Am::RunState runState)
{
    if (runState != m_runState) {
        m_runState = runState;
        emit runStateChanged(m_runState);
    }
}

void Application::setCurrentRuntime(AbstractRuntime *runtime)
{
    if (m_runtime == runtime)
        return;

    if (m_runtime)
        disconnect(m_runtime, nullptr, this, nullptr);

    m_runtime = runtime;
    emit runtimeChanged();

    if (m_runtime) {
        connect(m_runtime, &AbstractRuntime::finished, this, &Application::setLastExitCodeAndStatus);
        connect(m_runtime, &QObject::destroyed, this, [this]() {
            this->setCurrentRuntime(nullptr);
        });
    } else {
        setRunState(Am::NotRunning);
    }
}

void Application::setLastExitCodeAndStatus(int exitCode, Am::ExitStatus exitStatus)
{
    if (m_lastExitCode != exitCode) {
        m_lastExitCode = exitCode;
        emit lastExitCodeChanged();
    }

    Am::ExitStatus newStatus;
    if (exitStatus == Am::CrashExit) {
#if defined(Q_OS_UNIX)
        newStatus = (exitCode == SIGTERM || exitCode == SIGKILL) ? Am::ForcedExit : Am::CrashExit;
#else
        newStatus = Am::CrashExit;
#endif
    } else {
        newStatus = Am::NormalExit;
    }

    if (m_lastExitStatus != newStatus) {
        m_lastExitStatus = newStatus;
        emit lastExitStatusChanged();
    }
}

QT_END_NAMESPACE_AM

QDebug operator<<(QDebug debug, const QT_PREPEND_NAMESPACE_AM(Application) *app)
{
    debug << "Application Object:";
    if (app)
        debug << app->info()->toVariantMap();
    else
        debug << "(null)";
    return debug;
}