Only launch apps once
[staging/HomeScreen.git] / HomeScreenAppFrameworkBinderAGL / src / homescreenappframeworkbinderagl.cpp
1 /*
2  * Copyright (C) 2016 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "homescreenappframeworkbinderagl.h"
18
19
20 HomeScreenAppFrameworkBinderAgl::HomeScreenAppFrameworkBinderAgl(QObject *parent) :
21     QObject(parent),
22     mp_appframeworkAdaptor(0),
23     m_apps(),
24     mp_dBusAfmUserProxy(0)
25 {
26     qDebug("D-Bus: register as org.agl.homescreenappframeworkbinder");
27
28     //m_apps.clear();
29
30     // publish app framework interface
31     mp_appframeworkAdaptor = new AppframeworkAdaptor((QObject*)this);
32     QDBusConnection dbus = QDBusConnection::sessionBus();
33     dbus.registerObject("/AppFramework", this);
34     dbus.registerService("org.agl.homescreenappframeworkbinder");
35
36     qDebug("D-Bus: connect to org.AGL.afm.user /org/AGL/afm/user");
37     mp_dBusAfmUserProxy = new org::AGL::afm::user("org.AGL.afm.user",
38                                               "/org/AGL/afm/user",
39                                               QDBusConnection::sessionBus(),
40                                               0);
41
42 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
43     QString runnables = mp_dBusAfmUserProxy->runnables("{\"dummy\": \"entry\"}");
44     qDebug("runnables: %s", runnables.toStdString().c_str());
45
46     QJsonDocument jsonResponse = QJsonDocument::fromJson(runnables.toUtf8());
47     QJsonArray appsArray = jsonResponse.array();
48     for (int i = 0; i < appsArray.size(); ++i)
49     {
50         qDebug("new app %d", i);
51         QJsonObject appObject = appsArray[i].toObject();
52         AppInfo appInfo;
53         appInfo.read(appObject);
54         qDebug("name %s", appInfo.name().toStdString().c_str());
55         m_apps.append(appInfo);
56
57     }
58 #else
59     // for the simulation
60     /*AppInfo ai;
61     ai.setName("org.test.app1");
62     ai.setDescription("testdesc1");
63     ai.setIconPath("https://www.automotivelinux.org/sites/cpstandard/files/logo.png");
64     m_apps.append(ai);
65     ai.setName("org.test.app2");
66     ai.setDescription("testdesc2");
67     m_apps.append(ai);*/
68 #endif
69 }
70
71 HomeScreenAppFrameworkBinderAgl::~HomeScreenAppFrameworkBinderAgl()
72 {
73     delete mp_appframeworkAdaptor;
74 }
75
76 QList<AppInfo> HomeScreenAppFrameworkBinderAgl::getAvailableApps()
77 {
78     return m_apps;
79 }
80
81 int HomeScreenAppFrameworkBinderAgl::launchApp(const QString &name)
82 {
83     int pid = -1;
84 #ifdef HAVE_IVI_LAYERMANAGEMENT_API
85     qDebug("launchApp name: %s", name.toStdString().c_str());
86     QString jsonLaunch = "{\"id\":\"" + name + "\", \"mode\":\"local\"}";
87     qDebug("jsonLaunch %s", jsonLaunch.toStdString().c_str());
88     QString stateString = mp_dBusAfmUserProxy->once(jsonLaunch);
89     qDebug("stateString %s", stateString.toStdString().c_str());
90
91     QJsonDocument jsonResponse = QJsonDocument::fromJson(stateString.toUtf8());
92     QJsonObject obj = jsonResponse.object();
93     QJsonArray pidArray = obj["pids"].toArray();
94     for (int i = 0; i < pidArray.size(); ++i)
95     {
96         qDebug("pid %d", pidArray[i].toInt());
97     }
98     if (1 == pidArray.size())
99     {
100         pid = pidArray[0].toInt();
101     }
102     if (2 == pidArray.size())
103     {
104         pid = pidArray[1].toInt();
105     }
106
107     qDebug("launchApp pid: %d", pid);
108 #else
109     qDebug("launchApp name: %s", name.toStdString().c_str());
110     pid = qrand();
111 #endif
112
113     return pid;
114 }
115
116 void HomeScreenAppFrameworkBinderAgl::clearAppList()
117 {
118     //m_apps.clear();
119 }
120
121 void HomeScreenAppFrameworkBinderAgl::appendAppName(const char* name)
122 {
123     /*AppInfo newApp;
124     newApp.setName(QString(name));
125     m_apps.append(newApp);*/
126 }