240f5c8cec87e1bddb9899f9a6871ac700c00000
[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_qhs(NULL)
28 {
29 }
30
31 HomescreenHandler::~HomescreenHandler()
32 {
33     if (mp_qhs != NULL) {
34         delete mp_qhs;
35     }
36 }
37
38 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
39 {
40     myThis = this;
41     mp_qwm = qwm;
42     m_myname = myname;
43
44     mp_qhs = new QLibHomeScreen();
45     mp_qhs->init(port, token);
46     mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
47
48     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
49         HMI_DEBUG("Launcher","Surface launcher got Event_ShowWindow\n");
50         mp_qwm->activateWindow(m_myname);
51     });
52
53     mp_qhs->set_event_handler(QLibHomeScreen::Event_AppListChanged,[this](json_object *object){
54         HMI_DEBUG("Launcher","laucher: appListChanged [%s]\n", json_object_to_json_string(object));
55
56         struct json_object *obj_param, *obj_oper, *obj_data;
57         if(json_object_object_get_ex(object, "parameter", &obj_param)
58         && json_object_object_get_ex(obj_param, "operation", &obj_oper)
59         && json_object_object_get_ex(obj_param, "data", &obj_data)) {
60             QString oper = json_object_get_string(obj_oper);
61             if(oper == "uninstall") {
62                 /* { "application_id": "launcher",
63                  *   "type": "application-list-changed",
64                  *   "parameter":{
65                  *      "operation": "uninstall",
66                  *      "data": "onstestapp@0.1"
67                  *   }
68                  * } */
69                 QString id = json_object_get_string(obj_data);
70                 QStringList info;
71                 // icon, name, id
72                 info << "" << "" << id;
73                 emit appListUpdate(info);
74             }
75             else if(oper == "install") {
76                 /* { "application_id": "launcher",
77                  *   "type": "application-list-changed",
78                  *   "parameter": {
79                  *      "operation": "install",
80                  *      "data": {
81                  *          "description":"This is a demo onstestapp application",
82                  *          "name": "onstestapp",
83                  *          "shortname": "",
84                  *          "id": "onstestapp@0.1",
85                  *          "version": "0.1",
86                  *          "author": "Qt",
87                  *          "author-email": "",
88                  *          "width": "",
89                  *          "height": "",
90                  *          "icon": "\/var\/local\/lib\/afm\/applications\/onstestapp\/0.1\/icon.svg",
91                  *          "http-port": 31022
92                  *      }
93                  *    }
94                  * } */
95                 struct json_object *obj_icon, *obj_name, *obj_id;
96                 if(json_object_object_get_ex(obj_data, "icon", &obj_icon)
97                 && json_object_object_get_ex(obj_data, "name", &obj_name)
98                 && json_object_object_get_ex(obj_data, "id", &obj_id)) {
99                     QString icon = json_object_get_string(obj_icon);
100                     QString name = json_object_get_string(obj_name);
101                     QString id = json_object_get_string(obj_id);
102                     QStringList info;
103                     // icon, name, id
104                     info << icon << name << id;
105                     emit appListUpdate(info);
106                 }
107             }
108             else {
109                 HMI_ERROR("Launcher","error operation.\n");
110             }
111         }
112     });
113 }
114
115 void HomescreenHandler::tapShortcut(QString application_id)
116 {
117     HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
118     struct json_object* j_json = json_object_new_object();
119     struct json_object* value;
120     value = json_object_new_string("normal.full");
121     json_object_object_add(j_json, "area", value);
122
123     mp_qhs->showWindow(application_id.toStdString().c_str(), j_json);
124 }
125
126 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
127 {
128     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
129 }
130
131 void HomescreenHandler::onRep(struct json_object* reply_contents)
132 {
133     HMI_DEBUG("launcher","HomeScreen onReply %s", json_object_to_json_string(reply_contents));
134     struct json_object *obj_res, *obj_verb;
135     if(json_object_object_get_ex(reply_contents, "response", &obj_res)
136     && json_object_object_get_ex(obj_res, "verb", &obj_verb)
137     && !strcasecmp("getRunnables", json_object_get_string(obj_verb))) {
138         struct json_object *obj_data;
139         if(json_object_object_get_ex(obj_res, "data", &obj_data)) {
140             HMI_DEBUG("launcher","HomescreenHandler emit initAppList");
141             QString data = json_object_to_json_string(obj_data);
142             emit initAppList(data);
143         }
144     }
145 }
146
147 void HomescreenHandler::getRunnables(void)
148 {
149     mp_qhs->getRunnables();
150 }
151
152 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
153 {
154     mp_qhs->setQuickWindow(qw);
155 }