e76a6d8c69bbe443ce02bd5a69359764cc42c3b7
[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     mp_window(NULL)
48 {
49     HMI_DEBUG("qlibhomescreen", "called.");
50 }
51
52 /**
53  * QLibHomeScreen destruction function
54  *
55  * #### Parameters
56  * - None
57  *
58  * #### Return
59  * - None
60  *
61  */
62 QLibHomeScreen::~QLibHomeScreen()
63 {
64     HMI_DEBUG("qlibhomescreen", "called.");
65     if (mp_hs != NULL) {
66         delete mp_hs;
67     }
68 }
69
70 /**
71  * init function
72  *
73  * call libhomescreen init function to connect to binder by websocket
74  *
75  * #### Parameters
76  * - prot  : port from application
77  * - token : token from application
78  *
79  * #### Return
80  * - None
81  *
82  */
83 void QLibHomeScreen::init(int port, const QString &token)
84 {
85     HMI_DEBUG("qlibhomescreen", "called.");
86     string ctoken = token.toStdString();
87     mp_hs = new LibHomeScreen();
88     mp_hs->init(port, ctoken.c_str());
89
90     myThis = this;
91 }
92
93
94 /**
95  * call on screen message
96  *
97  * use libhomescreen api to call onscreen message
98  *
99  * #### Parameters
100  * - message : message contents
101  *
102  * #### Return
103  * - Returns 0 on success or -1 in case of error.
104  *
105  */
106 int QLibHomeScreen::onScreenMessage(const QString &message)
107 {
108     HMI_DEBUG("qlibhomescreen", "called.");
109     string str = message.toStdString();
110     return mp_hs->onScreenMessage(str.c_str());
111 }
112
113 /**
114  * subscribe event
115  *
116  * use libhomescreen api to subscribe homescreen event
117  *
118  * #### Parameters
119  * - evetNave : homescreen event name
120  *
121  * #### Return
122  * - Returns 0 on success or -1 in case of error.
123  *
124  */
125 int QLibHomeScreen::subscribe(const QString &evetName)
126 {
127     HMI_DEBUG("qlibhomescreen", "called.");
128     string str = evetName.toStdString();
129     return mp_hs->subscribe(str);
130 }
131
132 /**
133  * unsubscribe event
134  *
135  * use libhomescreen api to unsubscribe homescreen event
136  *
137  * #### Parameters
138  * - evetNave : homescreen event name
139  *
140  * #### Return
141  * - Returns 0 on success or -1 in case of error.
142  *
143  */
144 int QLibHomeScreen::unsubscribe(const QString &evetName)
145 {
146     HMI_DEBUG("qlibhomescreen", "called.");
147     string str = evetName.toStdString();
148     return mp_hs->unsubscribe(str);
149 }
150
151 /**
152  * set homescreen event handler function
153  *
154  * #### Parameters
155  * - et : homescreen event name
156  * - f  : event handler function
157  *
158  * #### Return
159  * - None.
160  *
161  */
162 void QLibHomeScreen::set_event_handler(enum QEventType et, handler_fun f)
163 {
164     HMI_DEBUG("qlibhomescreen", "called.");
165     LibHomeScreen::EventType hs_et = (LibHomeScreen::EventType)et;
166     return this->mp_hs->set_event_handler(hs_et, std::move(f));
167 }
168
169 /**
170  * tapShortcut function
171  *
172  * #### Parameters
173  * - application_id : tapped application id
174   *
175  * #### Return
176  * - None.
177  *
178  */
179 void QLibHomeScreen::tapShortcut(QString application_id)
180 {
181     HMI_DEBUG("qlibhomescreen","tapShortcut %s", application_id.toStdString().c_str());
182     mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
183 }
184
185 /**
186  * show application by application id and display area
187  *
188  * #### Parameters
189  * - application_id  : application id
190  * - area  : display area liked {"area":"normal"}
191  *
192  * #### Return
193  * - None.
194  *
195  */
196 void QLibHomeScreen::showWindow(QString application_id, json_object* area)
197 {
198     mp_hs->showWindow(application_id.toStdString().c_str(), area);
199 }
200
201 /**
202  * show application by application id and display area
203  *
204  * #### Parameters
205  * - application_id  : application id
206  * - area  : display area liked "normal"
207  *
208  * #### Return
209  * - None.
210  *
211  */
212 void QLibHomeScreen::showWindow(QString application_id, QString area)
213 {
214     if(area.isNull()) {
215         mp_hs->showWindow(application_id.toStdString().c_str(), nullptr);
216     } else {
217         struct json_object *j_obj = json_object_new_object();
218         struct json_object *value = json_object_new_string(area.toStdString().c_str());
219         json_object_object_add(j_obj, "area", value);
220         mp_hs->showWindow(application_id.toStdString().c_str(), j_obj);
221     }
222 }
223
224 /**
225  * hide application by application id
226  *
227  * #### Parameters
228  * - application_id  : application id
229  *
230  * #### Return
231  * - None.
232  *
233  */
234 void QLibHomeScreen::hideWindow(QString application_id)
235 {
236     mp_hs->hideWindow(application_id.toStdString().c_str());
237 }
238
239 /**
240  * send onscreen reply to application
241  *
242  * #### Parameters
243  * - application_id  : application id
244  * - reply  : the reply contents
245  *
246  * #### Return
247  * - None.
248  *
249  */
250 void QLibHomeScreen::replyShowWindow(QString application_id, json_object* reply)
251 {
252     mp_hs->replyShowWindow(application_id.toStdString().c_str(), reply);
253 }
254
255 /**
256  * send onscreen reply to application
257  *
258  * #### Parameters
259  * - application_id  : application id
260  * - reply  : the reply contents which can convert to json
261  *
262  * #### Return
263  * - None.
264  *
265  */
266 void QLibHomeScreen::replyShowWindow(QString application_id, QString reply)
267 {
268     if(reply.isNull())
269         mp_hs->replyShowWindow(application_id.toStdString().c_str(), nullptr);
270     else
271         mp_hs->replyShowWindow(application_id.toStdString().c_str(), json_tokener_parse(reply.toStdString().c_str()));
272 }
273
274 /**
275  * show information
276  *
277  * push information to HomeScreen
278  *
279  * #### Parameters
280  * - info : information that want to show
281  *
282  * #### Return
283  * - None.
284  *
285  */
286 void QLibHomeScreen::showInformation(QString info)
287 {
288         struct json_object* j_obj = json_object_new_object();
289         struct json_object* val = json_object_new_string(info.toStdString().c_str());
290         json_object_object_add(j_obj, "info", val);
291
292     mp_hs->showInformation(j_obj);
293 }
294
295 /**
296  * show notification
297  *
298  * push notification to HomeScreen
299  *
300  * #### Parameters
301  * - icon : provided icon
302  * - text : text that want to show
303  *
304  * #### Resturn
305  * - None.
306  *
307  */
308 void QLibHomeScreen::showNotification(QString icon, QString text)
309 {
310         struct json_object* j_obj = json_object_new_object();
311         struct json_object* val_icon = json_object_new_string(icon.toStdString().c_str());
312         struct json_object* val_text = json_object_new_string(text.toStdString().c_str());
313         json_object_object_add(j_obj, "icon", val_icon);
314         json_object_object_add(j_obj, "text", val_text);
315
316     mp_hs->showNotification(j_obj);
317 }
318
319 /**
320  * get runnables list from homescreen-service
321  *
322  * #### Parameters
323  * - Nothing
324  *
325  * #### Resturn
326  * - None.
327  *
328  */
329 void QLibHomeScreen::getRunnables(void)
330 {
331     mp_hs->getRunnables();
332 }
333
334 /**
335  * publlish subscription to homescreen-service
336  *
337  * #### Parameters
338  * - Nothing
339  *
340  * #### Resturn
341  * - None.
342  *
343  */
344 void QLibHomeScreen::slotPublishSubscription(void) {
345     HMI_NOTICE("qlibhomescreen","slotPublishSubscription");
346     if(mp_hs != NULL) {
347         mp_hs->publishSubscription();
348         QObject::disconnect(m_loading);
349     }  
350 }
351
352 /**
353  * This function register callback function for reply/event message from home screen
354  *
355  * #### Parameters
356  * - event_cb [in] : This argument should be specified to the callback for subscribed event
357  * - reply_cb [in] : This argument should be specified to the reply callback for call function
358  *
359  * #### Return
360  * Nothing
361  *
362  * #### Note
363  * Event callback is invoked by home screen for event you subscribed.
364  * If you would like to get event, please call subscribe function before/after this function
365  */
366 void QLibHomeScreen::registerCallback(
367         void (*event_cb)(const std::string& event, struct json_object* event_contents),
368         void (*reply_cb)(struct json_object* reply_contents),
369         void (*hangup_cb)(void))
370 {
371     if(mp_hs != NULL)
372         mp_hs->registerCallback(event_cb, reply_cb, hangup_cb);
373 }
374
375 void QLibHomeScreen::setQuickWindow(QQuickWindow *qw)
376 {
377     mp_window = qw;
378     if(mp_window != NULL) {
379         HMI_NOTICE("qlibhomescreen", "connect frameSwapped signal.");
380         m_loading = QObject::connect(mp_window, SIGNAL(frameSwapped()), this, SLOT(slotPublishSubscription()));
381     }
382 }