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