summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/application.h
blob: 2dad10251e522cb27f0b0d545c3e8143b0515938 (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
/****************************************************************************
**
** Copyright (C) 2015 Pelagicore AG
** Contact: http://www.pelagicore.com/
**
** This file is part of the Pelagicore Application Manager.
**
** SPDX-License-Identifier: GPL-3.0
**
** $QT_BEGIN_LICENSE:GPL3$
** Commercial License Usage
** Licensees holding valid commercial Pelagicore Application Manager
** 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
** Pelagicore. For licensing terms and conditions, contact us at:
** http://www.pelagicore.com.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3 requirements will be
** met: http://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#pragma once

#include <QString>
#include <QMap>
#include <QVariant>
#include <QStringList>
#include <QDir>

#include "global.h"
#include "installationreport.h"

class AbstractRuntime;
class ApplicationManager;
class JsonApplicationScanner;
class InstallationReport;

class AM_EXPORT Application
{
public:
    enum Type { Gui, Headless };

    QString id() const                           { return m_id; }
    QString absoluteCodeFilePath() const         { return m_codeFilePath.isEmpty() ? QString() : baseDir().absoluteFilePath(m_codeFilePath); }
    QString codeFilePath() const                 { return m_codeFilePath; }
    QString runtimeName() const                  { return m_runtimeName; }
    QVariantMap runtimeParameters() const        { return m_runtimeParameters; }
    QMap<QString, QString> displayNames() const  { return m_displayName; }
    QString displayName(const QString &language) const { return m_displayName.value(language); }
    QString displayIcon() const                  { return m_displayIcon.isEmpty() ? QString() : baseDir().absoluteFilePath(m_displayIcon); }

    bool isPreloaded() const                     { return m_preload; }
    qreal importance() const                     { return m_importance; }
    bool isBuiltIn() const                       { return m_builtIn; }

    QStringList capabilities() const             { return m_capabilities; }
    QStringList supportedMimeTypes() const       { return m_mimeTypes; }
    QStringList categories() const               { return m_categories; }
    Type type() const                            { return m_type; }

    enum BackgroundMode
    {
        Auto,
        Never,
        ProvidesVoIP,
        PlaysAudio,
        TracksLocation
    };
    BackgroundMode backgroundMode() const       { return m_backgroundMode; }

    QString version() const                     { return m_version; }

    bool validate(QString *error) const;
    QVariantMap toVariantMap() const;
    static Application *fromVariantMap(const QVariantMap &map, QString *error = 0);
    void mergeInto(Application *app) const;

    const InstallationReport *installationReport() const { return m_installationReport.data(); }
    void setInstallationReport(InstallationReport *report) { m_installationReport.reset(report); }
    QDir baseDir() const;
    uint uid() const                { return m_uid; }

    // dynamic part
    AbstractRuntime *currentRuntime() const { return m_runtime; }
    void setCurrentRuntime(AbstractRuntime *rt) const { m_runtime = rt; }
    bool isLocked() const          { return m_locked.load() == 1; }
    bool lock() const              { return m_locked.testAndSetOrdered(0, 1); }
    bool unlock() const            { return m_locked.testAndSetOrdered(1, 0); }

    enum State {
        Installed,
        BeingInstalled,
        BeingUpdated,
        BeingRemoved
    };
    State state() const             { return m_state; }
    qreal progress() const          { return m_progress; }

    void setBaseDir(const QString &path); //TODO: replace baseDir handling with something that works :)

private:
    Application();

    // static part from info.json
    QString m_id;

    QString m_codeFilePath; // relative to info.json location
    QString m_runtimeName;
    QVariantMap m_runtimeParameters;
    QMap<QString, QString> m_displayName; // language -> name
    QString m_displayIcon; // relative to info.json location


    bool m_preload = false;
    qreal m_importance = 0; // relative to all others, with 0 being "normal"
    bool m_builtIn = false; // system app - not removable

    QStringList m_capabilities;
    QStringList m_categories;
    QStringList m_mimeTypes;

    BackgroundMode m_backgroundMode = Auto;

    QString m_version;

    // added by installer
    QScopedPointer<InstallationReport> m_installationReport;
    QDir m_baseDir;
    uint m_uid = uint(-1); // unix user id - move to installationReport

    Type m_type = Gui;

    // dynamic part
    mutable AbstractRuntime *m_runtime = 0;
    mutable QAtomicInt m_locked;
    mutable QAtomicInt m_mounted;

    mutable State m_state = Installed;
    mutable qreal m_progress = 0;

    friend QDataStream &operator<<(QDataStream &ds, const Application &app);
    friend QDataStream &operator>>(QDataStream &ds, Application &app);
    friend class ApplicationManager; // needed to update installation status
    friend class ApplicationDatabase; // needed to create Application objects
    friend class InstallationTask; // needed to set m_uid and m_builtin during the installation

    Q_DISABLE_COPY(Application)
};

QDataStream &operator<<(QDataStream &ds, const Application &app);
QDataStream &operator>>(QDataStream &ds, Application &app);

QDebug operator<<(QDebug debug, const Application *app);