change seq
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017, 2018, 2019 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_qhs(NULL)
27 {
28 }
29
30 HomescreenHandler::~HomescreenHandler()
31 {
32     if (mp_qhs != NULL) {
33         delete mp_qhs;
34     }
35 }
36
37 void HomescreenHandler::init(int port, const char *token, QLibWindowmanager *qwm, QString myname)
38 {
39     mp_qhs = new QLibHomeScreen();
40     mp_qhs->init(port, token);
41
42     myThis = this;
43     mp_qwm = qwm;
44     m_myname = myname;
45
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_OnScreenMessage, [this](json_object *object){
54         const char *display_message = json_object_get_string(
55             json_object_object_get(object, "display_message"));
56         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
57     });
58
59     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowNotification,[this](json_object *object){
60        json_object *p_obj = json_object_object_get(object, "parameter");
61        const char *icon = json_object_get_string(
62                    json_object_object_get(p_obj, "icon"));
63        const char *text = json_object_get_string(
64                    json_object_object_get(p_obj, "text"));
65        const char *app_id = json_object_get_string(
66                    json_object_object_get(p_obj, "caller"));
67        HMI_DEBUG("HomeScreen","Event_ShowNotification icon=%s, text=%s, caller=%s", icon, text, app_id);
68        QFileInfo icon_file(icon);
69        QString icon_path;
70        if (icon_file.isFile() && icon_file.exists()) {
71            icon_path = QString(QLatin1String(icon));
72        } else {
73            icon_path = "./images/Utility_Logo_Grey-01.svg";
74        }
75
76        emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
77     });
78
79     mp_qhs->set_event_handler(QLibHomeScreen::Event_ShowInformation,[this](json_object *object){
80        json_object *p_obj = json_object_object_get(object, "parameter");
81        const char *info = json_object_get_string(
82                    json_object_object_get(p_obj, "info"));
83
84        emit showInformation(QString(QLatin1String(info)));
85     });
86 }
87
88 void HomescreenHandler::tapShortcut(QString application_id)
89 {
90     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
91     struct json_object* j_json = json_object_new_object();
92     struct json_object* value;
93     value = json_object_new_string("normal.full");
94     json_object_object_add(j_json, "area", value);
95
96     mp_qhs->showWindow(application_id.toStdString().c_str(), j_json);
97 }
98
99 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
100 {
101     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
102 }
103
104 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
105 {
106     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
107 }
108
109 void HomescreenHandler::onRep(struct json_object* reply_contents)
110 {
111     const char* str = json_object_to_json_string(reply_contents);
112     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
113 }
114
115 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
116 {
117     const char* str = json_object_to_json_string(event_contents);
118     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
119
120     if (event.compare("homescreen/on_screen_message") == 0) {
121         struct json_object *json_data = json_object_object_get(event_contents, "data");
122         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
123         const char* display_message = json_object_get_string(json_display_message);
124
125         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
126     }
127 }
128
129 void HomescreenHandler::setQuickWindow(QQuickWindow *qw)
130 {
131     mp_qhs->setQuickWindow(qw);
132 }