change bg image opacity, add check icon condition
[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 "homescreenhandler.h"
18 #include <functional>
19 #include "hmi-debug.h"
20
21 void* HomescreenHandler::myThis = 0;
22
23 HomescreenHandler::HomescreenHandler(QObject *parent) :
24     QObject(parent),
25     mp_hs(NULL)
26 {
27
28 }
29
30 HomescreenHandler::~HomescreenHandler()
31 {
32     if (mp_hs != NULL) {
33         delete mp_hs;
34     }
35 }
36
37 void HomescreenHandler::init(int port, const char *token)
38 {
39     mp_hs = new LibHomeScreen();
40     mp_hs->init(port, token);
41
42     myThis = this;
43
44     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
45
46     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
47         const char *display_message = json_object_get_string(
48             json_object_object_get(object, "display_message"));
49         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
50     });
51
52     mp_hs->set_event_handler(LibHomeScreen::Event_ShowNotification,[this](json_object *object){
53        const char *application_id = json_object_get_string(
54                    json_object_object_get(object, "application_id"));
55
56        json_object *p_obj = json_object_object_get(object, "parameter");
57        const char *icon = json_object_get_string(
58                    json_object_object_get(p_obj, "icon"));
59        const char *text = json_object_get_string(
60                    json_object_object_get(p_obj, "text"));
61        QFileInfo icon_file(icon);
62        QString icon_path;
63        if (icon_file.isFile() && icon_file.exists()) {
64            icon_path = QString(QLatin1String(icon));
65        } else {
66            icon_path = "./images/Utility_Logo_Grey-01.svg";
67        }
68
69        emit showNotification(QString(QLatin1String(application_id)), icon_path, QString(QLatin1String(text)));
70     });
71
72     mp_hs->set_event_handler(LibHomeScreen::Event_ShowInformation,[this](json_object *object){
73        json_object *p_obj = json_object_object_get(object, "parameter");
74        const char *info = json_object_get_string(
75                    json_object_object_get(p_obj, "info"));
76
77        emit showInformation(QString(QLatin1String(info)));
78     });
79 }
80
81 void HomescreenHandler::tapShortcut(QString application_id, bool is_full)
82 {
83     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
84     struct json_object* j_json = json_object_new_object();
85     struct json_object* value;
86     if(is_full) {
87         value = json_object_new_string("fullscreen");
88         HMI_DEBUG("HomeScreen","tapShortcut fullscreen");
89     } else {
90         value = json_object_new_string("normal");
91         HMI_DEBUG("HomeScreen","tapShortcut normal");
92     }
93     json_object_object_add(j_json, "area", value);
94     mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
95 }
96
97 void HomescreenHandler::setCurrentApplication(QString application_id)
98 {
99     HMI_DEBUG("HomeScreen","setCurrentApplication %s", application_id.toStdString().c_str());
100     current_applciation = application_id;
101 }
102
103 QString HomescreenHandler::getCurrentApplication()
104 {
105     HMI_DEBUG("HomeScreen","getCurrentApplication %s", current_applciation.toStdString().c_str());
106     return current_applciation;
107 }
108
109 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
110 {
111     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
112 }
113
114 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
115 {
116     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
117 }
118
119 void HomescreenHandler::onRep(struct json_object* reply_contents)
120 {
121     const char* str = json_object_to_json_string(reply_contents);
122     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
123 }
124
125 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
126 {
127     const char* str = json_object_to_json_string(event_contents);
128     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
129
130     if (event.compare("homescreen/on_screen_message") == 0) {
131         struct json_object *json_data = json_object_object_get(event_contents, "data");
132         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
133         const char* display_message = json_object_get_string(json_display_message);
134
135         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
136     }
137 }