Allow to set notifications for the progress bar
[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 <QGuiApplication>
18 #include <QFileInfo>
19 #include "homescreenhandler.h"
20 #include <functional>
21 #include "hmi-debug.h"
22
23 #include <qpa/qplatformnativeinterface.h>
24
25 void* HomescreenHandler::myThis = 0;
26
27 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *_launcher, QObject *parent) :
28     QObject(parent),
29     aglShell(_aglShell),
30     launcher(_launcher)
31 {
32
33 }
34
35 HomescreenHandler::~HomescreenHandler()
36 {
37     if (mp_hs != NULL) {
38         delete mp_hs;
39     }
40 }
41
42 void HomescreenHandler::init(int port, const char *token)
43 {
44     mp_hs = new LibHomeScreen();
45     mp_hs->init(port, token);
46
47     myThis = this;
48
49
50     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
51
52     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
53         const char *display_message = json_object_get_string(
54             json_object_object_get(object, "display_message"));
55         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
56     });
57
58     // should be handled in the top panel
59     mp_hs->set_event_handler(LibHomeScreen::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     // should be handled in the bottom panel
80     mp_hs->set_event_handler(LibHomeScreen::Event_ShowInformation,[this](json_object *object){
81        json_object *p_obj = json_object_object_get(object, "parameter");
82        const char *info = json_object_get_string(
83                    json_object_object_get(p_obj, "info"));
84
85        emit showInformation(QString(QLatin1String(info)));
86     });
87 }
88
89 static struct wl_output *
90 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
91 {
92         void *output = native->nativeResourceForScreen("output", screen);
93         return static_cast<struct ::wl_output*>(output);
94 }
95
96 void HomescreenHandler::tapShortcut(QString application_id)
97 {
98         HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
99         mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
100         launcher->setLaunching(true);
101
102         // this works (and it is redundant the first time), due to the default
103         // policy engine installed which actives the application, when starting
104         // the first time. Later calls to HomescreenHandler::tapShortcut will
105         // require calling 'agl_shell_activate_app'
106         aglShell->activate_app(nullptr, application_id);
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 }