registerShortcut
[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 <QProcess>
22 #include <dirent.h>
23 #include <stdio.h>
24 #include "hmi-debug.h"
25 #include "afm_user_daemon_proxy.h"
26
27 extern org::AGL::afm::user *afm_user_daemon_proxy;
28
29 #define BUF_SIZE 1024
30 void* HomescreenHandler::myThis = 0;
31
32 HomescreenHandler::HomescreenHandler(QObject *parent) :
33     QObject(parent),
34     mp_qhs(NULL)
35 {
36 }
37
38 HomescreenHandler::~HomescreenHandler()
39 {
40     if (mp_qhs != NULL) {
41         delete mp_qhs;
42     }
43 }
44
45 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
46 {
47     myThis = this;
48     mp_qwm = qwm;
49     m_myname = myname;
50
51     mp_qhs = new QLibHomeScreen();
52     mp_qhs->init(port, token);
53     mp_qhs->registerCallback(nullptr, HomescreenHandler::onRep_static);
54
55     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowWindow,[this](json_object *object){
56         HMI_DEBUG("Launcher","Surface launcher got Event_ShowWindow\n");
57         mp_qwm->activateWindow(m_myname);
58     });
59
60     mp_qhs->set_event_handler(QLibHomeScreen::Event_AppListChanged,[this](json_object *object){
61         HMI_DEBUG("Launcher","laucher: appListChanged [%s]\n", json_object_to_json_string(object));
62
63         struct json_object *obj_param, *obj_oper, *obj_data;
64         if(json_object_object_get_ex(object, "parameter", &obj_param)
65         && json_object_object_get_ex(obj_param, "operation", &obj_oper)
66         && json_object_object_get_ex(obj_param, "data", &obj_data)) {
67             QString oper = json_object_get_string(obj_oper);
68             if(oper == "uninstall") {
69                 /* { "application_id": "launcher",
70                  *   "type": "application-list-changed",
71                  *   "parameter":{
72                  *      "operation": "uninstall",
73                  *      "data": "onstestapp@0.1"
74                  *   }
75                  * } */
76                 QString id = json_object_get_string(obj_data);
77                 QStringList info;
78                 // icon, name, id
79                 info << "" << "" << id;
80                 emit appListUpdate(info);
81             }
82             else if(oper == "install") {
83                 /* { "application_id": "launcher",
84                  *   "type": "application-list-changed",
85                  *   "parameter": {
86                  *      "operation": "install",
87                  *      "data": {
88                  *          "description":"This is a demo onstestapp application",
89                  *          "name": "onstestapp",
90                  *          "shortname": "",
91                  *          "id": "onstestapp@0.1",
92                  *          "version": "0.1",
93                  *          "author": "Qt",
94                  *          "author-email": "",
95                  *          "width": "",
96                  *          "height": "",
97                  *          "icon": "\/var\/local\/lib\/afm\/applications\/onstestapp\/0.1\/icon.svg",
98                  *          "http-port": 31022
99                  *      }
100                  *    }
101                  * } */
102                 struct json_object *obj_icon, *obj_name, *obj_id;
103                 if(json_object_object_get_ex(obj_data, "icon", &obj_icon)
104                 && json_object_object_get_ex(obj_data, "name", &obj_name)
105                 && json_object_object_get_ex(obj_data, "id", &obj_id)) {
106                     QString icon = json_object_get_string(obj_icon);
107                     QString name = json_object_get_string(obj_name);
108                     QString id = json_object_get_string(obj_id);
109                     QStringList info;
110                     // icon, name, id
111                     info << icon << name << id;
112                     emit appListUpdate(info);
113                 }
114             }
115             else {
116                 HMI_ERROR("Launcher","error operation.\n");
117             }
118         }
119     });
120
121     mp_qhs->set_event_handler(QLibHomeScreen::Event_UpdateShortcut,[this](json_object *object){
122         HMI_DEBUG("Launcher","Surface launcher got Event_UpdateShortcut\n");
123         json_object *obj_p = json_object_object_get(object, "parameter");
124         json_object *obj_array = json_object_object_get(obj_p, "shortcut");
125         QStringList shortcut_list;
126         for(int i = 0; i < 3; i++)
127         {
128             shortcut_list << QString(QLatin1String(json_object_get_string(json_object_object_get(json_object_array_get_idx(obj_array, i), "shortcut_id"))));
129             shortcut_list << QString(QLatin1String(json_object_get_string(json_object_object_get(json_object_array_get_idx(obj_array, i), "shortcut_name"))));
130         }
131         HMI_DEBUG("Launcher","SEvent_UpdateShortcut id1 = %s", shortcut_list.at(2).toStdString().c_str());
132         emit updateShortcutList(shortcut_list);
133     });
134 }
135
136 void HomescreenHandler::tapShortcut(QString application_id)
137 {
138     HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
139     struct json_object* j_json = json_object_new_object();
140     struct json_object* value;
141     value = json_object_new_string("normal.full");
142     json_object_object_add(j_json, "area", value);
143
144     mp_qhs->showWindow(application_id.toStdString().c_str(), j_json);
145 }
146
147 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
148 {
149     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
150 }
151
152 void HomescreenHandler::onRep(struct json_object* reply_contents)
153 {
154     HMI_DEBUG("launcher","HomeScreen onReply %s", json_object_to_json_string(reply_contents));
155     struct json_object *obj_res, *obj_verb;
156     if(json_object_object_get_ex(reply_contents, "response", &obj_res)
157     && json_object_object_get_ex(obj_res, "verb", &obj_verb)
158     && !strcasecmp("getRunnables", json_object_get_string(obj_verb))) {
159         struct json_object *obj_data;
160         if(json_object_object_get_ex(obj_res, "data", &obj_data)) {
161             HMI_DEBUG("launcher","HomescreenHandler emit initAppList");
162             QString data = json_object_to_json_string(obj_data);
163             emit initAppList(data);
164         }
165     }
166 }
167
168 void HomescreenHandler::hideWindow(QString application_id)
169 {
170     mp_qhs->hideWindow(application_id.section('@', 0, 0).toStdString().c_str());
171 }
172
173 void HomescreenHandler::registerShortcut(QString shortcut_id, QString shortcut_name, QString position)
174 {
175 //    struct json_object* j_obj = json_object_new_object();
176 //    struct json_object* val_id = json_object_new_string(shortcut_id.toStdString().c_str());
177 //    struct json_object* val_name = json_object_new_string(shortcut_name.toStdString().c_str());
178 //    struct json_object* val_position = json_object_new_string(position.toStdString().c_str());
179 //    json_object_object_add(j_obj, "shortcut_id", val_id);
180 //    json_object_object_add(j_obj, "shortcut_name", val_name);
181 //    json_object_object_add(j_obj, "position", val_position);
182
183     mp_qhs->registerShortcut(shortcut_id, shortcut_name, position);
184 }
185
186 int HomescreenHandler::uninstallApplication(QString application_id)
187 {
188     int result = -1;
189     HMI_DEBUG("launcher","Application uninstall %s.", application_id.toStdString().c_str());
190
191     result = afm_user_daemon_proxy->uninstall(application_id).value().toInt();
192     HMI_DEBUG("launcher","ApplicationUninstall pid: %d.", result);
193
194 //    QProcess *process = new QProcess();
195 //    QString command = "/usr/bin/pkill " + application_id.section('@', 0, 0);
196 //    HMI_DEBUG("launcher", "command is %s", command.toStdString().c_str());
197 //    process->start(command);
198
199     return result;
200 }
201
202 void HomescreenHandler::sendAppToMeter(QString application_id)
203 {
204     HMI_DEBUG("Launcher","sendAppToMeter %s", application_id.toStdString().c_str());
205     struct json_object* j_json = json_object_new_object();
206     struct json_object* value;
207     value = json_object_new_string("master.split.sub");
208     json_object_object_add(j_json, "area", value);
209
210     mp_qhs->showWindow(application_id.section('@', 0, 0).toStdString().c_str(), j_json);
211 }
212
213 void HomescreenHandler::getRunnables(void)
214 {
215     mp_qhs->getRunnables();
216 }
217
218 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
219 {
220     mp_qhs->setQuickWindow(qw);
221 }