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