9035fb1fd5e0e13a85235d22df09b3d541491a05
[apps/launcher.git] / launcher / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 <QFileInfo>
18 #include "homescreenhandler.h"
19 #include <functional>
20 #include "hmi-debug.h"
21
22 void* HomescreenHandler::myThis = 0;
23
24 HomescreenHandler::HomescreenHandler(QObject *parent) :
25     QObject(parent),
26     mp_hs(NULL)
27 {
28
29 }
30
31 HomescreenHandler::~HomescreenHandler()
32 {
33     if (mp_hs != NULL) {
34         delete mp_hs;
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_hs = new LibHomeScreen();
45     mp_hs->init(port, token);
46     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
47
48     mp_hs->set_event_handler(LibHomeScreen::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_hs->set_event_handler(LibHomeScreen::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                 QString id = json_object_get_string(obj_data);
63                 QStringList info;
64                 // icon, name, id
65                 info << "" << "" << id;
66                 emit appListUpdate(info);
67             }
68             else if(oper == "install") {
69                 struct json_object *obj_icon, *obj_name, *obj_id;
70                 if(json_object_object_get_ex(obj_data, "icon", &obj_icon)
71                 && json_object_object_get_ex(obj_data, "name", &obj_name)
72                 && json_object_object_get_ex(obj_data, "id", &obj_id)) {
73                     QString icon = json_object_get_string(obj_icon);
74                     QString name = json_object_get_string(obj_name);
75                     QString id = json_object_get_string(obj_id);
76                     QStringList info;
77                     // icon, name, id
78                     info << icon << name << id;
79                     emit appListUpdate(info);
80                 }
81             }
82             else {
83                 HMI_ERROR("Launcher","error operation.\n");
84             }
85         }
86     });
87 }
88
89 void HomescreenHandler::tapShortcut(QString application_id)
90 {
91     HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
92     struct json_object* j_json = json_object_new_object();
93     struct json_object* value;
94     value = json_object_new_string("normal.full");
95     json_object_object_add(j_json, "area", value);
96
97     mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
98 }
99
100 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
101 {
102     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
103 }
104
105 void HomescreenHandler::onRep(struct json_object* reply_contents)
106 {
107     HMI_DEBUG("launcher","HomeScreen onReply %s", json_object_to_json_string(reply_contents));
108     struct json_object *obj_res, *obj_verb;
109     if(json_object_object_get_ex(reply_contents, "response", &obj_res)
110     && json_object_object_get_ex(obj_res, "verb", &obj_verb)
111     && !strcasecmp("getRunnables", json_object_get_string(obj_verb))) {
112         struct json_object *obj_data;
113         if(json_object_object_get_ex(obj_res, "data", &obj_data)) {
114             HMI_DEBUG("launcher","HomescreenHandler emit initAppList");
115             QString data = json_object_to_json_string(obj_data);
116             emit initAppList(data);
117         }
118     }
119 }
120
121 void HomescreenHandler::getRunnables(void)
122 {
123     mp_hs->getRunnables();
124 }