add new features in homescreen-service and homescreen
[apps/homescreen.git] / homescreen / 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)
39 {
40     mp_hs = new LibHomeScreen();
41     mp_hs->init(port, token);
42
43     myThis = this;
44
45     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
46
47     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
48         const char *display_message = json_object_get_string(
49             json_object_object_get(object, "display_message"));
50         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
51     });
52
53     mp_hs->set_event_handler(LibHomeScreen::Event_ShowNotification,[this](json_object *object){
54        const char *application_id = json_object_get_string(
55                    json_object_object_get(object, "application_id"));
56
57        json_object *p_obj = json_object_object_get(object, "parameter");
58        const char *icon = json_object_get_string(
59                    json_object_object_get(p_obj, "icon"));
60        const char *text = json_object_get_string(
61                    json_object_object_get(p_obj, "text"));
62        QFileInfo icon_file(icon);
63        QString icon_path;
64        if (icon_file.isFile() && icon_file.exists()) {
65            icon_path = QString(QLatin1String(icon));
66        } else {
67            icon_path = "./images/Utility_Logo_Grey-01.svg";
68        }
69
70        emit showNotification(QString(QLatin1String(application_id)), icon_path, QString(QLatin1String(text)));
71     });
72
73     mp_hs->set_event_handler(LibHomeScreen::Event_ShowInformation,[this](json_object *object){
74        json_object *p_obj = json_object_object_get(object, "parameter");
75        const char *info = json_object_get_string(
76                    json_object_object_get(p_obj, "info"));
77
78        emit showInformation(QString(QLatin1String(info)));
79     });
80 }
81
82 void HomescreenHandler::tapShortcut(QString application_id)
83 {
84     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
85     struct json_object* j_json = json_object_new_object();
86     struct json_object* value;
87     value = json_object_new_string("normal");
88     json_object_object_add(j_json, "area", value);
89
90     mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
91 }
92
93 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
94 {
95     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
96 }
97
98 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
99 {
100     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
101 }
102
103 void HomescreenHandler::onRep(struct json_object* reply_contents)
104 {
105     const char* str = json_object_to_json_string(reply_contents);
106     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
107 }
108
109 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
110 {
111     const char* str = json_object_to_json_string(event_contents);
112     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
113
114     if (event.compare("homescreen/on_screen_message") == 0) {
115         struct json_object *json_data = json_object_object_get(event_contents, "data");
116         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
117         const char* display_message = json_object_get_string(json_display_message);
118
119         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
120     }
121 }