fix issue and modify function parameter name
[src/libqthomescreen.git] / src / qlibhomescreen.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 "qlibhomescreen.h"
18 #include <QJsonDocument>
19 #include <QJsonObject>
20 #include "hmi-debug.h"
21 using namespace std;
22
23 #define _POPUPREPLY "on_screen_reply"
24 #define _REQ_POPUP_MESSAGE "on_screen_message"
25 #define _TAPSHORTCUT "tap_shortcut"
26 #define _KEY_DATA "data"
27 #define _KEY_APPLICATION_DATA "application_id"
28 #define _KEY_REPLY_MESSAGE "reply_message"
29 #define _KEY_REQUEST_MESSAGE "display_message"
30
31 static QLibHomeScreen* myThis;
32
33 // Note: qlibhomescreen will be integrated to libqtappfw
34 /**
35  * QLibHomeScreen construction function
36  *
37  * #### Parameters
38  * - parent [in] : object parent.
39  *
40  * #### Return
41  * - None
42  *
43  */
44 QLibHomeScreen::QLibHomeScreen(QObject *parent) :
45     QObject(parent),
46     mp_hs(NULL)
47 {
48     HMI_DEBUG("qlibhomescreen", "called.");
49 }
50
51 /**
52  * QLibHomeScreen destruction function
53  *
54  * #### Parameters
55  * - None
56  *
57  * #### Return
58  * - None
59  *
60  */
61 QLibHomeScreen::~QLibHomeScreen()
62 {
63     HMI_DEBUG("qlibhomescreen", "called.");
64     if (mp_hs != NULL) {
65         delete mp_hs;
66     }
67 }
68
69 /**
70  * init function
71  *
72  * call libhomescreen init function to connect to binder by websocket
73  *
74  * #### Parameters
75  * - prot  : port from application
76  * - token : token from application
77  *
78  * #### Return
79  * - None
80  *
81  */
82 void QLibHomeScreen::init(int port, const QString &token)
83 {
84     HMI_DEBUG("qlibhomescreen", "called.");
85     string ctoken = token.toStdString();
86     mp_hs = new LibHomeScreen();
87     mp_hs->init(port, ctoken.c_str());
88
89     myThis = this;
90 }
91
92
93 /**
94  * call on screen message
95  *
96  * use libhomescreen api to call onscreen message
97  *
98  * #### Parameters
99  * - message : message contents
100  *
101  * #### Return
102  * - Returns 0 on success or -1 in case of error.
103  *
104  */
105 int QLibHomeScreen::onScreenMessage(const QString &message)
106 {
107     HMI_DEBUG("qlibhomescreen", "called.");
108     string str = message.toStdString();
109     return mp_hs->onScreenMessage(str.c_str());
110 }
111
112 /**
113  * subscribe event
114  *
115  * use libhomescreen api to subscribe homescreen event
116  *
117  * #### Parameters
118  * - evetNave : homescreen event name
119  *
120  * #### Return
121  * - Returns 0 on success or -1 in case of error.
122  *
123  */
124 int QLibHomeScreen::subscribe(const QString &evetName)
125 {
126     HMI_DEBUG("qlibhomescreen", "called.");
127     string str = evetName.toStdString();
128     return mp_hs->subscribe(str);
129 }
130
131 /**
132  * unsubscribe event
133  *
134  * use libhomescreen api to unsubscribe homescreen event
135  *
136  * #### Parameters
137  * - evetNave : homescreen event name
138  *
139  * #### Return
140  * - Returns 0 on success or -1 in case of error.
141  *
142  */
143 int QLibHomeScreen::unsubscribe(const QString &evetName)
144 {
145     HMI_DEBUG("qlibhomescreen", "called.");
146     string str = evetName.toStdString();
147     return mp_hs->unsubscribe(str);
148 }
149
150 /**
151  * set homescreen event handler function
152  *
153  * #### Parameters
154  * - et : homescreen event name
155  * - f  : event handler function
156  *
157  * #### Return
158  * - None.
159  *
160  */
161 void QLibHomeScreen::set_event_handler(enum QEventType et, handler_fun f)
162 {
163     HMI_DEBUG("qlibhomescreen", "called.");
164     LibHomeScreen::EventType hs_et = (LibHomeScreen::EventType)et;
165     return this->mp_hs->set_event_handler(hs_et, std::move(f));
166 }
167
168 /**
169  * tapShortcut function
170  *
171  * #### Parameters
172  * - application_id : tapped application id
173   *
174  * #### Return
175  * - None.
176  *
177  */
178 void QLibHomeScreen::tapShortcut(QString application_id)
179 {
180     HMI_DEBUG("qlibhomescreen","tapShortcut %s", application_id.toStdString().c_str());
181     mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
182 }
183
184 /**
185  * show application by application id and display area
186  *
187  * #### Parameters
188  * - application_id  : application id
189  * - area  : display area liked {"area":"normal"}
190  *
191  * #### Return
192  * - None.
193  *
194  */
195 void QLibHomeScreen::showWindow(QString application_id, json_object* area)
196 {
197     mp_hs->showWindow(application_id.toStdString().c_str(), area);
198 }
199
200 /**
201  * show application by application id and display area
202  *
203  * #### Parameters
204  * - application_id  : application id
205  * - area  : display area liked "normal"
206  *
207  * #### Return
208  * - None.
209  *
210  */
211 void QLibHomeScreen::showWindow(QString application_id, QString area)
212 {
213     if(area.isNull()) {
214         mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
215     } else {
216         struct json_object *j_obj = json_object_new_object();
217         struct json_object *value = json_object_new_string(area.toStdString().c_str());
218         json_object_object_add(j_obj, "area", value);
219         mp_hs->showWindow(application_id.toStdString().c_str(), j_obj);
220     }
221 }
222
223 /**
224  * hide application by application id
225  *
226  * #### Parameters
227  * - application_id  : application id
228  *
229  * #### Return
230  * - None.
231  *
232  */
233 void QLibHomeScreen::hideWindow(QString application_id)
234 {
235     mp_hs->hideWindow(application_id.toStdString().c_str());
236 }
237
238 /**
239  * send onscreen reply to application
240  *
241  * #### Parameters
242  * - application_id  : application id
243  * - reply  : the reply contents
244  *
245  * #### Return
246  * - None.
247  *
248  */
249 void QLibHomeScreen::replyShowWindow(QString application_id, json_object* reply)
250 {
251     mp_hs->replyShowWindow(application_id.toStdString().c_str(), reply);
252 }
253
254 /**
255  * send onscreen reply to application
256  *
257  * #### Parameters
258  * - application_id  : application id
259  * - reply  : the reply contents which can convert to json
260  *
261  * #### Return
262  * - None.
263  *
264  */
265 void QLibHomeScreen::replyShowWindow(QString application_id, QString reply)
266 {
267     if(reply.isNull())
268         mp_hs->replyShowWindow(application_id.toStdString().c_str(), nullptr);
269     else
270         mp_hs->replyShowWindow(application_id.toStdString().c_str(), json_tokener_parse(reply.toStdString().c_str()));
271 }
272
273 /**
274  * show information
275  *
276  * push information to HomeScreen
277  *
278  * #### Parameters
279  * - info : information that want to show
280  *
281  * #### Return
282  * - None.
283  *
284  */
285 void QLibHomeScreen::showInformation(QString info)
286 {
287         struct json_object* j_obj = json_object_new_object();
288         struct json_object* val = json_object_new_string(info.toStdString().c_str());
289         json_object_object_add(j_obj, "info", val);
290
291     mp_hs->showInformation(j_obj);
292 }
293
294 /**
295  * show notification
296  *
297  * push notification to HomeScreen
298  *
299  * #### Parameters
300  * - icon : provided icon
301  * - text : text that want to show
302  *
303  * #### Resturn
304  * - None.
305  *
306  */
307 void QLibHomeScreen::showNotification(QString icon, QString text)
308 {
309         struct json_object* j_obj = json_object_new_object();
310         struct json_object* val_icon = json_object_new_string(icon.toStdString().c_str());
311         struct json_object* val_text = json_object_new_string(text.toStdString().c_str());
312         json_object_object_add(j_obj, "icon", val_icon);
313         json_object_object_add(j_obj, "text", val_text);
314
315     mp_hs->showNotification(j_obj);
316 }