Start app and get runnables list by homescreen
[apps/launcher.git] / launcher / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QFileInfo>
19 #include "homescreenhandler.h"
20 #include <functional>
21 #include "hmi-debug.h"
22
23 void* HomescreenHandler::myThis = 0;
24
25 HomescreenHandler::HomescreenHandler(QObject *parent) :
26     QObject(parent),
27     mp_hs(NULL)
28 {
29
30 }
31
32 HomescreenHandler::~HomescreenHandler()
33 {
34     if (mp_hs != NULL) {
35         delete mp_hs;
36     }
37 }
38
39 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
40 {
41     myThis = this;
42     mp_qwm = qwm;
43     m_myname = myname;
44
45     mp_hs = new LibHomeScreen();
46     mp_hs->init(port, token);
47     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
48
49     mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow,[this](json_object *object){
50         HMI_DEBUG("Launcher","Surface launcher got Event_ShowWindow\n");
51         mp_qwm->activateWindow(m_myname);
52     });
53
54     mp_hs->set_event_handler(LibHomeScreen::Event_AppListChanged,[this](json_object *object){
55         HMI_DEBUG("Launcher","laucher: appListChanged [%s]\n", json_object_to_json_string(object));
56
57         struct json_object *obj_param, *obj_oper, *obj_data;
58         if(json_object_object_get_ex(object, "parameter", &obj_param)
59         && json_object_object_get_ex(obj_param, "operation", &obj_oper)
60         && json_object_object_get_ex(obj_param, "data", &obj_data)) {
61             QString oper = json_object_get_string(obj_oper);
62             if(oper == "uninstall") {
63                 /* { "application_id": "launcher",
64                  *   "type": "application-list-changed",
65                  *   "parameter":{
66                  *      "operation": "uninstall",
67                  *      "data": "onstestapp@0.1"
68                  *   }
69                  * } */
70                 QString id = json_object_get_string(obj_data);
71                 QStringList info;
72                 // icon, name, id
73                 info << "" << "" << id;
74                 emit appListUpdate(info);
75             }
76             else if(oper == "install") {
77                 /* { "application_id": "launcher",
78                  *   "type": "application-list-changed",
79                  *   "parameter": {
80                  *      "operation": "install",
81                  *      "data": {
82                  *          "description":"This is a demo onstestapp application",
83                  *          "name": "onstestapp",
84                  *          "shortname": "",
85                  *          "id": "onstestapp@0.1",
86                  *          "version": "0.1",
87                  *          "author": "Qt",
88                  *          "author-email": "",
89                  *          "width": "",
90                  *          "height": "",
91                  *          "icon": "\/var\/local\/lib\/afm\/applications\/onstestapp\/0.1\/icon.svg",
92                  *          "http-port": 31022
93                  *      }
94                  *    }
95                  * } */
96                 struct json_object *obj_icon, *obj_name, *obj_id;
97                 if(json_object_object_get_ex(obj_data, "icon", &obj_icon)
98                 && json_object_object_get_ex(obj_data, "name", &obj_name)
99                 && json_object_object_get_ex(obj_data, "id", &obj_id)) {
100                     QString icon = json_object_get_string(obj_icon);
101                     QString name = json_object_get_string(obj_name);
102                     QString id = json_object_get_string(obj_id);
103                     QStringList info;
104                     // icon, name, id
105                     info << icon << name << id;
106                     emit appListUpdate(info);
107                 }
108             }
109             else {
110                 HMI_ERROR("Launcher","error operation.\n");
111             }
112         }
113     });
114 }
115
116 void HomescreenHandler::tapShortcut(QString application_id)
117 {
118     HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
119     struct json_object* j_json = json_object_new_object();
120     struct json_object* value;
121     value = json_object_new_string("normal.full");
122     json_object_object_add(j_json, "area", value);
123
124     mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
125 }
126
127 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
128 {
129     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
130 }
131
132 void HomescreenHandler::onRep(struct json_object* reply_contents)
133 {
134     HMI_DEBUG("launcher","HomeScreen onReply %s", json_object_to_json_string(reply_contents));
135     struct json_object *obj_res, *obj_verb;
136     if(json_object_object_get_ex(reply_contents, "response", &obj_res)
137     && json_object_object_get_ex(obj_res, "verb", &obj_verb)
138     && !strcasecmp("getRunnables", json_object_get_string(obj_verb))) {
139         struct json_object *obj_data;
140         if(json_object_object_get_ex(obj_res, "data", &obj_data)) {
141             HMI_DEBUG("launcher","HomescreenHandler emit initAppList");
142             QString data = json_object_to_json_string(obj_data);
143             emit initAppList(data);
144         }
145     }
146 }
147
148 void HomescreenHandler::getRunnables(void)
149 {
150     mp_hs->getRunnables();
151 }