Fix Coverity issues
[apps/native/ug-nfc-efl.git] / wearable / app / src / nsa-view-tap-n-pay.c
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * PROPRIETARY/CONFIDENTIAL
5  *
6  * This software is the confidential and proprietary information of
7  * SAMSUNG ELECTRONICS ("Confidential Information").
8  * You shall not disclose such Confidential Information and shall
9  * use it only in accordance with the terms of the license agreement
10  * you entered into with SAMSUNG ELECTRONICS.
11  * SAMSUNG make no representations or warranties about the suitability
12  * of the software, either express or implied, including but not
13  * limited to the implied warranties of merchantability, fitness for
14  * a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as
16  * a result of using, modifying or distributing this software or its derivatives.
17
18  */
19
20
21 #include <app.h>
22 #include <nfc.h>
23 #include <nfc_internal.h>
24 #include <vconf.h>
25 #include <pkgmgr-info.h>
26 #include "nsa-view.h"
27 #include "nsa-main.h"
28 #include "nsa-debug.h"
29 #include "nsa-util.h"
30 #include "nsa-ui-widget.h"
31 #include "nsa-popup.h"
32 #include "nsa-string.h"
33
34 #define APP_CONTROL_DEFAULT_CHANGED     \
35         "http://tizen.org/appcontrol/operation/nfc/card_emulation/default_changed"
36
37 #define MAX_AIDS_COUNT  50
38
39 typedef struct {
40         int index;
41         appdata *ad;
42         Elm_Object_Item *it;
43 } item_data_t;
44
45 static void __add_item_to_wallet_list(const char *handler, int count, void *data)
46 {
47         appdata *ad = data;
48         wallet_info_t *winfo;
49
50         NSA_MEM_MALLOC(winfo, 1, wallet_info_t);
51         ret_if(winfo == NULL);
52         NSA_MEM_STRDUP(winfo->appid, handler);
53         winfo->aid_count = count;
54
55         g_array_append_val(ad->payment_wallet_list, winfo);
56         ad->payment_wallet_cnt++;
57 }
58
59 static bool __registered_handler_cb(const char *handler, int count, void *user_data)
60 {
61         appdata *ad = user_data;
62
63         retv_if(ad == NULL, false);
64         retv_if(ad->payment_wallet_list == NULL, false);
65
66         NSA_DEBUG("package name: [%s]", handler);
67         NSA_DEBUG("AID number: [%d]", count);
68
69         __add_item_to_wallet_list(handler, count, ad);
70
71         return true;
72 }
73
74 static void __get_wallet_info(void *ad)
75 {
76         NSA_DEBUG("__get_wallet_info");
77
78         nfc_se_foreach_registered_handlers(NFC_CARD_EMULATION_CATEGORY_PAYMENT,
79                 __registered_handler_cb, ad);
80 }
81
82 static Evas_Object *__create_no_contents_ly(void *data)
83 {
84         appdata *ad = data;
85         Evas_Object *sc, *ly, *image, *circle_obj;
86         Evas_Object *title_label;
87         Evas_Object *desc_label;
88         char title_text[256];
89         char desc_text[256];
90
91         retv_if(ad == NULL, NULL);
92
93         /* create scroller */
94         sc = elm_scroller_add(ad->navi_frame);
95         evas_object_size_hint_weight_set(sc, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
96         evas_object_size_hint_align_set(sc, EVAS_HINT_FILL, EVAS_HINT_FILL);
97         elm_scroller_single_direction_set(sc, ELM_SCROLLER_SINGLE_DIRECTION_HARD);
98         elm_object_style_set(sc, "effect");
99         evas_object_show(sc);
100
101         circle_obj = eext_circle_object_scroller_add(sc, ad->circle_surface);
102         eext_circle_object_genlist_scroller_policy_set(circle_obj,
103                 ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
104         eext_rotary_object_event_activated_set(circle_obj, EINA_TRUE);
105
106         /* create layout */
107         ly = nsa_create_edj_layout(sc, "tap_n_pay/nocontents");
108         retv_if(ly == NULL, NULL);
109
110         image = elm_image_add(ly);
111         retv_if(image == NULL, NULL);
112         elm_image_file_set(image, IMAGES_PATH"/b_setting_payment_help_image.png", NULL);
113         elm_object_part_content_set(ly, "elm.swallow.icon", image);
114
115         title_label = elm_label_add(ly);
116         evas_object_size_hint_weight_set(title_label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
117         evas_object_size_hint_align_set(title_label, EVAS_HINT_FILL, EVAS_HINT_FILL);
118
119         elm_label_wrap_width_set(title_label, 226);
120         snprintf(title_text, 256, "<font=Tizen:style=Medium font_size=30 color=#0094ffff align=center text_class=tizen>%s</font>", IDS_TAP_AND_PAY);
121         elm_object_text_set(title_label, title_text);
122         elm_object_part_content_set(ly, "elm.text.title", title_label);
123
124         desc_label = elm_label_add(ly);
125         evas_object_size_hint_weight_set(desc_label, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
126         evas_object_size_hint_align_set(desc_label, EVAS_HINT_FILL, EVAS_HINT_FILL);
127
128         elm_label_wrap_width_set(desc_label, 226);
129         elm_label_line_wrap_set(desc_label, ELM_WRAP_MIXED);
130         snprintf(desc_text, 256, "<font=Tizen:style=Medium font_size=25 align=center text_class=tizen>%s</font>", IDS_NO_INSTALLED_PAYMENT_APPS);
131         elm_object_text_set(desc_label, desc_text);
132         elm_object_part_content_set(ly, "elm.text", desc_label);
133
134         /* content set */
135         elm_object_content_set(sc, ly);
136
137         return sc;
138 }
139
140
141 static void __max_AID_popup_response_cb(void *data,
142         Evas_Object *obj, void *event_info)
143 {
144         item_data_t *item = (item_data_t *)data;
145         appdata *ad = item->ad;
146         char *wallet_handler;
147
148         ret_if(ad == NULL);
149         ret_if(ad->popup == NULL);
150
151         NSA_DEBUG("__max_AID_popup_response_cb");
152
153         wallet_handler = vconf_get_str(VCONFKEY_NFC_PAYMENT_HANDLERS);
154         if (wallet_handler != NULL) {
155                 int i;
156
157                 for (i = 0; i < ad->payment_wallet_cnt; i++) {
158                         wallet_info_t *winfo;
159
160                         winfo = g_array_index(ad->payment_wallet_list, wallet_info_t *, i);
161                         if (!winfo)
162                                 continue;
163
164                         if (strlen(wallet_handler) > 0 &&
165                                 strcmp(winfo->appid, wallet_handler) == 0) {
166                                 NSA_DEBUG("matched, [%s]", winfo->appid);
167
168                                 if (ad->radio_main != NULL)
169                                         elm_radio_value_set(ad->radio_main, i);
170                         }
171                 }
172
173                 NSA_MEM_FREE(wallet_handler);
174         }
175
176         elm_popup_dismiss(ad->popup);
177 }
178
179
180 static char *__get_launch_app_id(char *pkgid)
181 {
182         pkgmgrinfo_pkginfo_h pkginfo = NULL;
183         char *main_appid = NULL;
184         int ret;
185
186         retv_if(pkgid == NULL, NULL);
187
188         NSA_DEBUG("__get_launch_app_id [%s]", pkgid);
189
190         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo);
191         if (ret != 0)
192                 return strdup(pkgid);
193
194         ret = pkgmgrinfo_pkginfo_get_mainappid(pkginfo, &main_appid);
195
196         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
197
198         if (ret == 0)
199                 return strdup(main_appid);
200         else
201                 return strdup(pkgid);
202 }
203
204 static void __launch_application(char *appid)
205 {
206         app_control_h service;
207         int ret;
208
209         ret_if(appid == NULL);
210
211         NSA_DEBUG("__launch_application [%s]", appid);
212
213         app_control_create(&service);
214         ret_if(service == NULL);
215
216         ret = app_control_set_operation(service, APP_CONTROL_DEFAULT_CHANGED);
217         if (ret != APP_CONTROL_ERROR_NONE) {
218                 NSA_DEBUG_ERR("app_control_set_operation failed[%d]", ret);
219                 goto END;
220         }
221
222         ret = app_control_set_app_id(service, appid);
223         if (ret != APP_CONTROL_ERROR_NONE) {
224                 NSA_DEBUG_ERR("app_control_set_app_id failed[%d]", ret);
225                 goto END;
226         }
227
228         ret = app_control_send_launch_request(service, NULL, NULL);
229         if (ret != APP_CONTROL_ERROR_NONE)
230                 NSA_DEBUG_ERR("app_control_send_launch_request failed[%d]", ret);
231
232 END:
233         app_control_destroy(service);
234 }
235
236
237 static bool __set_default_wallet(item_data_t *item)
238 {
239         char *new_wallet = NULL;
240         char *old_wallet = NULL;
241         wallet_info_t *winfo;
242         bool duplicate = false;
243         appdata *ad = item->ad;
244
245         winfo = g_array_index(ad->payment_wallet_list, wallet_info_t *, item->index);
246
247         new_wallet = winfo->appid;
248         old_wallet = vconf_get_str(VCONFKEY_NFC_PAYMENT_HANDLERS);
249
250         if (old_wallet != NULL) {
251                 if (strlen(new_wallet) > 0 && strcmp(old_wallet, new_wallet) == 0) {
252                         duplicate = true;
253                         NSA_DEBUG("Same wallet is selected!");
254                 }
255                 NSA_MEM_FREE(old_wallet);
256         }
257
258         if (!duplicate) {
259                 int ret;
260                 char *launch_app_id = NULL;
261
262                 if (winfo->aid_count > MAX_AIDS_COUNT) {
263                         if (ad->radio_main != NULL)
264                                 elm_radio_value_set(ad->radio_main, -1);
265
266                         ad->popup = nsa_create_popup_text_1button(
267                                 ad->main_layout,
268                                 IDS_UNABLE_TO_SELECT_PAYMENT_APP_WITH_MORE_THAN_50_REGISTERED_CARDS,
269                                 IMAGES_PATH"/tw_ic_popup_btn_check.png",
270                                 __max_AID_popup_response_cb,
271                                 (void *)item);
272
273                         return false;
274                 }
275
276                 ret = vconf_set_str(VCONFKEY_NFC_PAYMENT_HANDLERS, new_wallet);
277                 if (ret != VCONF_OK) {
278                         NSA_DEBUG_ERR("error vconf_set_str : %d", ret);
279
280                         return false;
281                 }
282
283                 launch_app_id = __get_launch_app_id(new_wallet);
284
285                 __launch_application(launch_app_id);
286
287                 NSA_MEM_FREE(launch_app_id);
288
289                 return true;
290         }
291
292         return false;
293 }
294
295 static void __gl_list_clicked_cb(void *data, Evas_Object *obj,
296         void *event_info)
297 {
298         Elm_Object_Item* it = (Elm_Object_Item *)event_info;
299         item_data_t *item = (item_data_t *)data;
300         appdata *ad;
301
302         elm_genlist_item_selected_set(it, EINA_FALSE);
303
304         ret_if(item == NULL);
305         ret_if(item->ad == NULL);
306
307         ad = item->ad;
308
309         ret_if(ad->payment_wallet_list == NULL);
310
311         NSA_DEBUG("selected value for the group:%d", item->index);
312
313         if (__set_default_wallet(item)) {
314                 elm_radio_value_set(ad->radio_main, item->index);
315                 elm_genlist_item_bring_in(item->it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
316
317                 NSA_DEBUG("__set_default_wallet success");
318         } else {
319                 NSA_DEBUG_ERR("set default wallet failed");
320         }
321 }
322
323 static char *__gl_text_get_title(void *data, Evas_Object *obj,
324         const char *part)
325 {
326         if (!strcmp(part, "elm.text"))
327                 return strdup(IDS_TAP_AND_PAY);
328         else
329                 return NULL;
330 }
331
332 static char *__gl_text_get(void *data, Evas_Object *obj,
333         const char *part)
334 {
335         if (!strcmp(part, "elm.text")) {
336                 item_data_t *item = data;
337                 appdata *ad = item->ad;
338                 wallet_info_t *winfo;
339                 char *appid;
340
341                 retv_if(ad == NULL, NULL);
342                 /* How to get index? */
343                 winfo = g_array_index(ad->payment_wallet_list, wallet_info_t *, item->index);
344                 retv_if(winfo == NULL, NULL);
345                 appid = winfo->appid;
346                 if (appid != NULL) {
347                         pkgmgrinfo_pkginfo_h pkginfo = NULL;
348                         char *label = NULL;
349                         int ret;
350
351                         NSA_DEBUG("pkg id: %s", appid);
352
353                         ret = pkgmgrinfo_pkginfo_get_pkginfo(appid, &pkginfo);
354                         if (ret != 0) {
355                                 NSA_DEBUG_ERR("ret [%d]", ret);
356                                 return NULL;
357                         }
358
359                         ret = pkgmgrinfo_pkginfo_get_label(pkginfo, &label);
360                         if (ret != 0) {
361                                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
362                                 return NULL;
363                         } else {
364                                 char *text = NULL;
365
366                                 NSA_MEM_STRDUP(text, label);
367                                 if (text != NULL)
368                                         NSA_DEBUG("text: %s", text);
369
370                                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
371                                 return text;
372                         }
373                 } else {
374                         return NULL;
375                 }
376         } else {
377                 return NULL;
378         }
379 }
380
381 static Evas_Object *__gl_content_get(void *data, Evas_Object *obj,
382         const char *part)
383 {
384         if (!strcmp(part, "elm.icon")) {
385                 item_data_t *item = (item_data_t *) data;
386                 appdata *ad = item->ad;
387                 char *wallet_handler;
388                 Evas_Object *radio_main;
389                 Evas_Object *radio;
390
391                 retv_if(item == NULL, NULL);
392
393                 radio_main = evas_object_data_get(obj, "payment_rdg");
394                 retv_if(radio_main == NULL, NULL);
395
396                 radio = elm_radio_add(obj);
397                 retv_if(radio == NULL, NULL);
398
399                 NSA_DEBUG("item->index [%d]", item->index);
400
401                 elm_object_style_set(radio, "list");
402                 elm_radio_state_value_set(radio, item->index);
403                 elm_radio_group_add(radio, radio_main);
404
405                 wallet_handler = vconf_get_str(VCONFKEY_NFC_PAYMENT_HANDLERS);
406
407                 if (wallet_handler == NULL) {
408                         NSA_DEBUG_ERR("vconf_get_str error");
409                 } else {
410                         wallet_info_t *winfo;
411                         char *appid;
412
413                         winfo = g_array_index(ad->payment_wallet_list, wallet_info_t *, item->index);
414                         if (winfo == NULL) {
415                                 NSA_MEM_FREE(wallet_handler);
416                                 return NULL;
417                         }
418                         appid = winfo->appid;
419                         if (strlen(wallet_handler) > 0 && strcmp(appid, wallet_handler) == 0) {
420                                 NSA_DEBUG("matched, [%s]", appid);
421
422                                 elm_radio_value_set(radio_main, item->index);
423                                 elm_genlist_item_bring_in(item->it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
424                         }
425                         NSA_MEM_FREE(wallet_handler);
426                 }
427
428                 return radio;
429         } else {
430                 return NULL;
431         }
432 }
433
434 static Evas_Object *__create_tap_and_pay_list(void *data)
435 {
436         int i;
437         appdata *ad = data;
438         Evas_Object *genlist;
439         Elm_Genlist_Item_Class *itc_title, *itc_list, *itc_padding;
440         Evas_Object *circle_obj;
441
442         genlist = elm_genlist_add(ad->navi_frame);
443         retv_if(genlist == NULL, NULL);
444
445         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
446
447         itc_title = elm_genlist_item_class_new();
448         itc_title->item_style = "title";
449         itc_title->func.text_get = __gl_text_get_title;
450         itc_title->func.content_get = NULL;
451         itc_title->func.del = NULL;
452
453         itc_list = elm_genlist_item_class_new();
454         itc_list->item_style = "1text.1icon.1";
455         itc_list->func.text_get = __gl_text_get;
456         itc_list->func.content_get = __gl_content_get;
457         itc_list->func.state_get = NULL;
458         itc_list->func.del = NULL;
459
460         itc_padding = elm_genlist_item_class_new();
461         itc_padding->item_style = "padding";
462
463         elm_genlist_item_append(genlist, itc_title, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
464
465         for (i = 0; i < ad->payment_wallet_cnt; i++) {
466                 item_data_t *item;
467
468                 NSA_MEM_MALLOC(item, 1, item_data_t);
469
470                 item->index = i;
471                 item->ad = ad;
472                 item->it = elm_genlist_item_append(genlist, itc_list, item, NULL, ELM_GENLIST_ITEM_NONE, __gl_list_clicked_cb, item);
473         }
474
475         elm_genlist_item_append(genlist, itc_padding,
476                 NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
477
478         ad->radio_main = elm_radio_add(genlist);
479         elm_radio_state_value_set(ad->radio_main, -1);
480         elm_radio_value_set(ad->radio_main, -1);
481         evas_object_data_set(genlist, "payment_rdg", ad->radio_main);
482
483         elm_genlist_item_class_free(itc_title);
484         elm_genlist_item_class_free(itc_list);
485         elm_genlist_item_class_free(itc_padding);
486
487         circle_obj = eext_circle_object_genlist_add(genlist, ad->circle_surface);
488         eext_circle_object_genlist_scroller_policy_set(circle_obj,
489                 ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
490         eext_rotary_object_event_activated_set(circle_obj, EINA_TRUE);
491
492         evas_object_show(genlist);
493
494         return genlist;
495 }
496
497 bool _nsa_view_tap_n_pay_create(void *data)
498 {
499         appdata *ad = data;
500         Evas_Object *layout = NULL;
501
502         ad->payment_wallet_list = g_array_new(FALSE, FALSE, sizeof(wallet_info_t *));
503         ad->payment_wallet_cnt = 0;
504
505         /* get wallet info */
506         __get_wallet_info(ad);
507
508         /* Draw UI */
509         if (ad->payment_wallet_cnt == 0)
510                 layout = __create_no_contents_ly(ad);
511         else
512                 layout = __create_tap_and_pay_list(ad);
513
514         retv_if(layout == NULL, false);
515
516         elm_naviframe_item_push(ad->navi_frame, NULL, NULL, NULL, layout, "empty");
517         ad->current_view = V_TAP_N_PAY;
518
519         return true;
520 }