Mobile & Wearable profile migration for NFC setting UI
[apps/native/ug-nfc-efl.git] / mobile / src / ug-nfc-setting-main.c
1 /*
2   * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3   *
4   * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <efl_extension.h>
18 #include <notification.h>
19
20 #include "ug-nfc-setting-main.h"
21
22 #ifndef UG_MODULE_API
23 #define UG_MODULE_API __attribute__ ((visibility("default")))
24 #endif
25
26 static Elm_Genlist_Item_Class itc_helptext;
27 static Elm_Genlist_Item_Class itc_onoff;
28
29 static bool pending_status = false;
30
31 static bool __get_pending_status(void)
32 {
33         return pending_status;
34 }
35
36 static void __set_pending_status(bool status)
37 {
38         pending_status = status;
39 }
40
41 static void __update_title_onoff_obj(void *data)
42 {
43         ugdata_t *ug_data = (ugdata_t *)data;
44         int boolval;
45
46         LOGD("BEGIN >>>>");
47
48         if (!ug_data)
49                 return;
50
51         if (__get_pending_status()) {
52                 elm_object_disabled_set(ug_data->ns_on_off, EINA_TRUE);
53                 return;
54         }
55
56         elm_object_disabled_set(ug_data->ns_on_off, EINA_FALSE);
57
58         if (!vconf_get_bool(VCONFKEY_NFC_STATE, &boolval) && boolval)
59                 elm_check_state_set(ug_data->ns_on_off, EINA_TRUE);
60         else
61                 elm_check_state_set(ug_data->ns_on_off, EINA_FALSE);
62
63         LOGD("END >>>>");
64 }
65
66 static bool __reply_to_launch_request(app_control_h service, app_control_result_e result)
67 {
68         app_control_h reply;
69         char *operation = NULL;
70         bool ret = false;
71
72         LOGD("BEGIN >>>>");
73
74         if (service != NULL) {
75                 app_control_create(&reply);
76                 app_control_get_operation(service, &operation);
77
78                 if (operation != NULL) {
79                         LOGD("reply to launch request : operation %s", operation);
80                         app_control_reply_to_launch_request(reply, service, result);
81                         ret = true;
82                 }
83
84                 app_control_destroy(reply);
85         }
86
87         LOGD("END >>>>");
88
89         return ret;
90 }
91
92 static void __nfc_activation_completed_cb(nfc_error_e error,
93         void *user_data)
94 {
95         LOGD("BEGIN >>>>");
96
97         if (error != NFC_ERROR_NONE) {
98                 LOGE("__nfc_activation_completed_cb failed");
99
100                 /* show failure popup */
101         }
102
103         LOGD("END >>>>");
104 }
105
106 static void __nfc_activation_changed_cb(bool activated , void *user_data)
107 {
108         ugdata_t *ug_data = (ugdata_t *)user_data;
109
110         LOGD("BEGIN >>>>");
111
112         LOGD("nfc mode %s ", activated ? "ON" : "OFF");
113
114         nfc_manager_unset_activation_changed_cb();
115
116         /* nfc setting ui updated */
117         __set_pending_status(false);
118
119         __update_title_onoff_obj(ug_data);
120
121         if (__reply_to_launch_request(ug_data->service, APP_CONTROL_RESULT_SUCCEEDED) == true)
122                 ug_destroy_me(ug_data->nfc_setting_ug);
123
124         LOGD("END >>>>");
125 }
126
127 static Eina_Bool __back_clicked_cb(void *data, Elm_Object_Item *it)
128 {
129         ugdata_t *ug_data = (ugdata_t *)data;
130
131         LOGD("BEGIN >>>>");
132
133         if (!ug_data) {
134                 LOGE("data is null");
135                 return EINA_FALSE;
136         }
137
138         __reply_to_launch_request(ug_data->service, APP_CONTROL_RESULT_FAILED);
139
140         ug_destroy_me(ug_data->nfc_setting_ug);
141
142         LOGD("END >>>>");
143
144         return EINA_FALSE;
145 }
146
147 static void __change_nfc_onoff_setting(void *data)
148 {
149         ugdata_t *ug_data = (ugdata_t *)data;
150         int result, boolval;
151
152         LOGD("BEGIN >>>>");
153
154         if (!ug_data)
155                 return;
156
157         if (!vconf_get_bool(VCONFKEY_NFC_STATE, &boolval)) {
158                 LOGD("vconf_get_bool status [%d]", boolval);
159
160                 if (NFC_ERROR_NONE == nfc_manager_initialize()) {
161
162                         /* Register activation changed callback */
163                         nfc_manager_set_activation_changed_cb(
164                                 __nfc_activation_changed_cb, ug_data);
165
166                         result = nfc_manager_set_activation(!boolval,
167                                 __nfc_activation_completed_cb, ug_data);
168                         if (result != NFC_ERROR_NONE) {
169                                 LOGE("nfc_manager_set_activation failed");
170                                 return;
171                         }
172
173                         __set_pending_status(true);
174                 } else {
175                         LOGE("nfc_manager_initialize FAIL!!!!");
176                 }
177
178
179         } else {
180                 LOGE("vconf_get_bool failed");
181         }
182
183         __update_title_onoff_obj(ug_data);
184
185         LOGD("END >>>>");
186 }
187
188 static void __title_ns_on_off_clicked_cb(void *data, Evas_Object *obj,
189         void *event_info)
190 {
191         ugdata_t *ug_data = (ugdata_t *)data;
192
193         LOGD("BEGIN >>>>");
194
195         if (ug_data == NULL) {
196                 LOGE("data is null");
197                 return;
198         }
199
200         if (__get_pending_status())
201                 return;
202
203         __change_nfc_onoff_setting(ug_data);
204
205         LOGD("END >>>>");
206 }
207
208 static void __nfc_sel(void *data, Evas_Object *obj, void *event_info)
209 {
210         LOGD("BEGIN >>>>");
211
212         __title_ns_on_off_clicked_cb(data, obj, event_info);
213
214         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
215
216         LOGD("END >>>>");
217 }
218
219 static Evas_Object *__gl_content_get(void *data, Evas_Object *obj,
220         const char *part)
221 {
222         ugdata_t *ug_data = (ugdata_t *)data;
223         int boolval = false;
224
225         LOGD("BEGIN >>>>");
226
227         if (!ug_data) {
228                 LOGE("invalid parameter");
229                 return NULL;
230         }
231
232         if (!strncmp(part, "elm.swallow.end", strlen(part))) {
233                 Evas_Object *btn = NULL;
234
235                 vconf_get_bool(VCONFKEY_NFC_STATE, &boolval);
236
237                 LOGD("elm.swallow.end");
238
239                 btn = elm_check_add(obj);
240                 elm_object_style_set(btn, "on&off");
241                 elm_check_state_set(btn, boolval);
242                 evas_object_propagate_events_set(btn, EINA_FALSE);
243                 evas_object_smart_callback_add(btn, "changed", __title_ns_on_off_clicked_cb, ug_data);
244                 evas_object_show(btn);
245
246                 ug_data->ns_on_off = btn;
247
248                 __update_title_onoff_obj(ug_data);
249
250                 return btn;
251         }
252
253         LOGD("END >>>>");
254
255         return NULL;
256 }
257
258 static char *__gl_text_get(void *data, Evas_Object *obj, const char *part)
259 {
260         return strcmp(part, "elm.text") == 0 ? strdup(IDS_NFC_NFC) : NULL;
261 }
262
263 static char *__gl_text_get_desc(void *data, Evas_Object *obj, const char *part)
264 {
265         return strcmp(part, "elm.text.multiline") == 0 ? strdup(IDS_NFC_DESCRIPTION_MSG) : NULL;
266 }
267
268 static Evas_Object *__create_nfc_list(void *data)
269 {
270         ugdata_t *ug_data = (ugdata_t *)data;
271         Evas_Object *genlist = NULL;
272         Elm_Object_Item *onoff_item;
273
274         LOGD("BEGIN >>>>");
275
276         /* make genlist */
277         genlist = elm_genlist_add(ug_data->base_naviframe);
278         if (genlist == NULL) {
279                 LOGE("genlist is null");
280                 return NULL;
281         }
282
283         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
284
285         itc_onoff.item_style = "type1";
286         itc_onoff.func.text_get = __gl_text_get;
287         itc_onoff.func.content_get = __gl_content_get;
288
289         itc_helptext.item_style = "multiline";
290         itc_helptext.func.text_get = __gl_text_get_desc;
291
292         /* Append ON / OFF list */
293         onoff_item = elm_genlist_item_append(genlist, &itc_onoff,
294                                 (void *) ug_data, NULL, ELM_GENLIST_ITEM_NONE,
295                                 __nfc_sel, (void *) ug_data);
296
297         elm_object_item_signal_emit(onoff_item, "elm,state,top", "");
298
299         /* Append help text */
300         static Elm_Object_Item *help_item;
301         help_item = elm_genlist_item_append(genlist, &itc_helptext, (void *)0,
302                 NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
303         elm_genlist_item_select_mode_set(help_item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
304         elm_object_item_access_unregister(help_item);
305
306         evas_object_show(genlist);
307
308         LOGD("END >>>>");
309
310         return genlist;
311 }
312
313 static Evas_Object *__create_bg(Evas_Object *parent, char *style)
314 {
315         Evas_Object *bg = elm_bg_add(parent);
316
317         LOGD("BEGIN >>>>");
318
319         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
320
321         if (style)
322                 elm_object_style_set(bg, style);
323
324         elm_win_resize_object_add(parent, bg);
325
326         evas_object_show(bg);
327
328         LOGD("END >>>>");
329
330         return bg;
331 }
332
333 static Evas_Object *__create_main_layout(Evas_Object *parent)
334 {
335         Evas_Object *layout;
336
337         LOGD("BEGIN >>>>");
338
339         if (parent == NULL)
340                 return NULL;
341
342         layout = elm_layout_add(parent);
343
344         elm_layout_theme_set(layout, "layout", "application", "default");
345
346         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
347
348         evas_object_show(layout);
349
350         LOGD("END >>>>");
351
352         return layout;
353 }
354
355 static void *__ug_nfc_create(ui_gadget_h ug, enum ug_mode mode,
356         app_control_h service, void *priv)
357 {
358         ugdata_t *ug_data = (ugdata_t *)priv;
359         Evas_Object *parent = NULL;
360         Evas_Object *l_button = NULL;
361         Evas_Object *nfc_setting_list = NULL;
362
363         LOGD("BEGIN >>>>");
364
365         /* set text domain */
366         bindtextdomain(NFCUG_TEXT_DOMAIN, NFCUG_LOCALEDIR);
367
368         /* create base view */
369         parent = ug_get_parent_layout(ug);
370         if (!parent)
371                 return NULL;
372
373         ug_data->ug_win_main = parent;
374         evas_object_show(ug_data->ug_win_main);
375         ug_data->nfc_setting_ug = ug;
376
377         ug_data->base_layout = __create_main_layout(ug_data->ug_win_main);
378         ug_data->bg = __create_bg(ug_data->ug_win_main, "group_list");
379         elm_object_part_content_set(ug_data->base_layout, "elm.swallow.bg", ug_data->bg);
380
381         ug_data->base_naviframe = elm_naviframe_add(ug_data->base_layout);
382         elm_object_part_content_set(ug_data->base_layout, "elm.swallow.content",
383                 ug_data->base_naviframe);
384
385         evas_object_show(ug_data->base_naviframe);
386
387         ug_data->service = service;
388         nfc_setting_list = __create_nfc_list(ug_data);
389
390         if (nfc_setting_list == NULL) {
391                 LOGE("create nfc list failed");
392                 return NULL;
393         }
394
395         /* Push navifreme */
396         l_button = elm_button_add(ug_data->base_naviframe);
397         elm_object_style_set(l_button, "naviframe/back_btn/default");
398         evas_object_smart_callback_add(l_button, "clicked", __back_clicked_cb, ug_data);
399
400         eext_object_event_callback_add(ug_data->base_naviframe, EEXT_CALLBACK_BACK,
401                 eext_naviframe_back_cb, NULL);
402
403         ug_data->base_navi_it = elm_naviframe_item_push(ug_data->base_naviframe,
404                 IDS_NFC_NFC, l_button, NULL, nfc_setting_list, NULL);
405
406         elm_naviframe_item_pop_cb_set(ug_data->base_navi_it, __back_clicked_cb, ug_data);
407
408         LOGD("END >>>>");
409
410         return ug_data->base_layout;
411 }
412
413 static void __ug_nfc_destroy(ui_gadget_h ug, app_control_h service, void *priv)
414 {
415         ugdata_t *ug_data = (ugdata_t *)priv;
416
417         LOGD("BEGIN >>>>");
418
419         if ((ug_data == NULL) || (ug == NULL))
420                 return;
421
422         if (nfc_manager_deinitialize() != NFC_ERROR_NONE)
423                 LOGE("NFC deinitialize failed");
424
425         evas_object_del(ug_get_layout(ug));
426
427         LOGD("END >>>>");
428 }
429
430 static void __ug_nfc_start(ui_gadget_h ug, app_control_h service, void *priv)
431 {
432         LOGD("BEGIN >>>>");
433
434         if (nfc_manager_is_supported() == false) {
435                 LOGE("NFC not supported");
436                 /* popup */
437         }
438
439         if (nfc_manager_initialize() != NFC_ERROR_NONE)
440                 LOGE("NFC initialize failed");
441
442         LOGD("END >>>>");
443 }
444
445 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
446 {
447         ugdata_t *ug_data;
448
449         LOGD("UG_MODULE_INIT!!");
450
451         if (!ops)
452                 return -1;
453
454         ug_data = (ugdata_t *)g_malloc0((gint)sizeof(ugdata_t));
455         if (!ug_data)
456                 return -1;
457
458         ops->create = __ug_nfc_create;
459         ops->start = __ug_nfc_start;
460         ops->destroy = __ug_nfc_destroy;
461         ops->priv = ug_data;
462
463         return 0;
464 }
465
466 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
467 {
468         ugdata_t *ug_data;
469
470         LOGD("UG_MODULE_EXIT!!");
471
472         if (!ops)
473                 return;
474
475         ug_data = ops->priv;
476
477         if (ug_data)
478                 free(ug_data);
479
480         ops->priv = NULL;
481 }