summaryrefslogtreecommitdiff
path: root/daemon/jskitmanager.cpp
blob: 698b22b8e85d523ba5658c3ccacc52e7e3545450 (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
#include <QQmlEngine>
#include <QJSValueIterator>
#include "jskitmanager.h"
#include "jskitmanager_p.h"

JSKitPebble::JSKitPebble(JSKitManager *mgr)
    : QObject(mgr)
{
}

JSKitPebble::~JSKitPebble()
{
}

JSKitManager::JSKitManager(AppManager *apps, AppMsgManager *appmsg, QObject *parent) :
    QObject(parent), _apps(apps), _appmsg(appmsg), _engine(0)
{
    connect(_appmsg, &AppMsgManager::appStarted, this, &JSKitManager::handleAppStarted);
    connect(_appmsg, &AppMsgManager::appStopped, this, &JSKitManager::handleAppStopped);
}

JSKitManager::~JSKitManager()
{
    if (_engine) {
        stopJsApp();
    }
}

void JSKitManager::handleAppStarted(const QUuid &uuid)
{
    const auto &info = _apps->info(uuid);
    if (!info.uuid.isNull() && info.isJSKit) {
        logger()->debug() << "Preparing to start JSKit app" << info.uuid << info.shortName;
        _curApp = info;
        startJsApp();
    }
}

void JSKitManager::handleAppStopped(const QUuid &uuid)
{
    if (!_curApp.uuid.isNull()) {
        if (_curApp.uuid != uuid) {
            logger()->warn() << "Closed app with invalid UUID";
        }

        _curApp = AppManager::AppInfo();
    }
}

void JSKitManager::startJsApp()
{
    if (_engine) stopJsApp();
    _engine = new QJSEngine(this);
    _jspebble = new JSKitPebble(this);

    QJSValue globalObj = _engine->globalObject();

    globalObj.setProperty("Pebble", _engine->newQObject(_jspebble));

    QJSValueIterator it(globalObj);
    while (it.hasNext()) {
        it.next();
        logger()->debug() << "JS property:" << it.name();
    }
}

void JSKitManager::stopJsApp()
{
    if (!_engine) return; // Nothing to do!

    _engine->collectGarbage();

    delete _engine;
    _engine = 0;
    delete _jspebble;
    _jspebble = 0;
}