91df6f7530018a5900f8b7d2a70b69544b1c119d
[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 __arm__
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 #endif
59 #ifdef __i386__
60     // for the simulation
61     /*AppInfo ai;
62     ai.setName("org.test.app1");
63     ai.setDescription("testdesc1");
64     ai.setIconPath("https://www.automotivelinux.org/sites/cpstandard/files/logo.png");
65     m_apps.append(ai);
66     ai.setName("org.test.app2");
67     ai.setDescription("testdesc2");
68     m_apps.append(ai);*/
69 #endif
70 }
71
72 HomeScreenAppFrameworkBinderAgl::~HomeScreenAppFrameworkBinderAgl()
73 {
74     delete mp_appframeworkAdaptor;
75 }
76
77 QList<AppInfo> HomeScreenAppFrameworkBinderAgl::getAvailableApps()
78 {
79     return m_apps;
80 }
81
82 int HomeScreenAppFrameworkBinderAgl::launchApp(const QString &name)
83 {
84     int pid = -1;
85 #ifdef __arm__
86     qDebug("launchApp name: %s", name.toStdString().c_str());
87     QString jsonLaunch = "{\"id\":\"" + name + "\", \"mode\":\"local\"}";
88     qDebug("jsonLaunch %s", jsonLaunch.toStdString().c_str());
89     QString runIdString = mp_dBusAfmUserProxy->start(jsonLaunch);
90     qDebug("runIdString %s", runIdString.toStdString().c_str());
91     int runid = runIdString.toInt();
92
93     QString stateString = mp_dBusAfmUserProxy->state(runIdString);
94     qDebug("stateString %s", stateString.toStdString().c_str());
95
96     QJsonDocument jsonResponse = QJsonDocument::fromJson(stateString.toUtf8());
97     QJsonObject obj = jsonResponse.object();
98     QJsonArray pidArray = obj["pids"].toArray();
99     for (int i = 0; i < pidArray.size(); ++i)
100     {
101         qDebug("pid %d", pidArray[i].toInt());
102     }
103     if (1 == pidArray.size())
104     {
105         pid = pidArray[0].toInt();
106     }
107     if (2 == pidArray.size())
108     {
109         pid = pidArray[1].toInt();
110     }
111
112     qDebug("launchApp pid: %d", pid);
113 #endif
114 #ifdef __i386__
115     qDebug("launchApp name: %s", name.toStdString().c_str());
116     pid = qrand();
117 #endif
118
119     return pid;
120 }
121
122 void HomeScreenAppFrameworkBinderAgl::clearAppList()
123 {
124     //m_apps.clear();
125 }
126
127 void HomeScreenAppFrameworkBinderAgl::appendAppName(const char* name)
128 {
129     /*AppInfo newApp;
130     newApp.setName(QString(name));
131     m_apps.append(newApp);*/
132 }