tizen 2.3.1 release
[apps/home/b2-clocksetting.git] / src / setting-privacy.c
1 /*
2  *  Copyright (c) 2014 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 /*
18  * setting-privacy.c
19  *
20  *  Created on: Jan 7, 2014
21  *      Author: Sunyeop Hwang
22  */
23
24
25 #include "setting-privacy.h"
26 #include "setting_data_vconf.h"
27 #include "util.h"
28
29 static void _privacy_lock_cb(void *data, Evas_Object *obj, void *event_info);
30 static void _privacy_see_pattern_cb(void *data, Evas_Object *obj, void *event_info);
31 static void _privacy_help_cb(void *data, Evas_Object *obj, void *event_info);
32 static void _privacy_pattern_enable_cb(void *data, Evas_Object *obj, void *event_info);
33 static void _privacy_pattern_disable_cb(void *data, Evas_Object *obj, void *event_info);
34 static void _create_privacy_pattern_list(void *data);
35
36 static struct _privacy_menu_item privacy_menu_list[] = {
37         { "IDS_LCKSCN_BODY_PRIVACY_LOCK_ABB",   _privacy_lock_cb },
38         { "IDS_ST_MBODY_HELP",                  _privacy_help_cb }
39 };
40
41
42 static struct _privacy_menu_item privacy_pattern_menu_list[] = {
43         { "IDS_LCKSCN_HEADER_PIN",      _privacy_pattern_enable_cb },
44         { "IDS_LCKSCN_BODY_NONE",       _privacy_pattern_disable_cb }
45 };
46
47 static char *lock_type_str[] = {
48         "IDS_LCKSCN_BODY_NONE",
49         "IDS_LCKSCN_HEADER_PIN"
50 };
51
52 static Evas_Object *g_privacy_genlist = NULL;
53
54 int _get_lock_type_value()
55 {
56         int value = 0;
57         if (vconf_get_int("db/setting/lock_type", &value) != 0) {
58                 ERR("error get vconf value!!");
59         }
60         return value;
61 }
62
63 int _set_lock_type_value(int value)
64 {
65         if (vconf_set_int("db/setting/lock_type", value) != 0) {
66                 ERR("error set vconf value!!");
67                 return FALSE;
68         }
69         return TRUE;
70 }
71
72 int _get_see_pattern_value()
73 {
74         int value = 0;
75         if (vconf_get_bool("db/setting/see_pattern", &value) != 0) {
76                 ERR("error get vconf value!!");
77         }
78         return value;
79 }
80
81 int _set_see_pattern_value(int value)
82 {
83         if (vconf_set_bool("db/setting/see_pattern", value) != 0) {
84                 ERR("error set vconf value!!");
85                 return FALSE;
86         }
87         return TRUE;
88 }
89
90 static void _privacy_lock_setting_cb(app_control_h service, app_control_h reply, app_control_result_e result, void *data)
91 {
92         appdata *ad = data;
93         if (!ad) {
94                 ERR("appdata is null");
95                 return;
96         }
97
98         if (result == APP_CONTROL_RESULT_SUCCEEDED) {
99                 _set_lock_type_value(1);
100                 elm_naviframe_item_pop(ad->nf);
101
102                 if (g_privacy_genlist) {
103                         elm_genlist_realized_items_update(g_privacy_genlist);
104                 }
105         }
106 }
107
108 static void _privacy_lock_verify_cb(app_control_h service, app_control_h reply, app_control_result_e result, void *data)
109 {
110         appdata *ad = data;
111         if (!ad) {
112                 ERR("appdata is null");
113                 return;
114         }
115
116         if (result == APP_CONTROL_RESULT_SUCCEEDED) {
117                 _create_privacy_pattern_list(ad);
118         }
119 }
120
121 static void _gl_privacy_del(void *data, Evas_Object *obj)
122 {
123         Privacy_Item_Data *id = data;
124         if (id)
125                 free(id);
126 }
127
128 char *_gl_privacy_pattern_title_get(void *data, Evas_Object *obj, const char *part)
129 {
130         char buf[1024] = {0,};
131         Item_Data *id = data;
132         int index = id->index;
133         char *device_info = NULL;
134
135         if (!strcmp(part, "elm.text")) {
136                 snprintf(buf, sizeof(buf) - 1, "%s", _(privacy_pattern_menu_list[index].name));
137         }
138
139         return strdup(buf);
140 }
141
142 static void _privacy_pattern_enable_cb(void *data, Evas_Object *obj, void *event_info)
143 {
144         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
145
146         appdata *ad = data;
147         if (!ad) {
148                 ERR("appdata is null");
149                 return;
150         }
151
152         app_control_h service;
153         app_control_create(&service);
154         app_control_set_app_id(service, "org.tizen.w-lockscreen-setting");
155
156         app_control_add_extra_data(service, "type", "setting");
157         app_control_send_launch_request(service, _privacy_lock_setting_cb, ad);
158         app_control_destroy(service);
159 }
160
161 static void _privacy_pattern_disable_cb(void *data, Evas_Object *obj, void *event_info)
162 {
163         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
164
165         appdata *ad = data;
166         if (!ad) {
167                 ERR("appdata is null");
168                 return;
169         }
170
171         _set_lock_type_value(0);
172         elm_naviframe_item_pop(ad->nf);
173
174         if (g_privacy_genlist) {
175                 elm_genlist_realized_items_update(g_privacy_genlist);
176         }
177 }
178
179 static void _create_privacy_pattern_list(void *data)
180 {
181         appdata *ad = data;
182
183         if (!ad) {
184                 ERR("appdata is null!!");
185                 return;
186         }
187
188         Evas_Object *genlist = NULL;
189         Evas_Object *layout = NULL;
190         Elm_Object_Item *nf_it = NULL;
191         struct _privacy_menu_item *menu_list = NULL;
192         int idx = 0;
193
194         Elm_Genlist_Item_Class *itc = elm_genlist_item_class_new();
195         itc->item_style = "1text";
196         itc->func.text_get = _gl_privacy_pattern_title_get;
197         itc->func.del = _gl_privacy_del;
198
199         layout = elm_layout_add(ad->nf);
200         elm_layout_file_set(layout, EDJE_PATH, "setting/genlist/layout");
201         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
202
203         genlist = elm_genlist_add(layout);
204         elm_genlist_block_count_set(genlist, 14);
205
206         menu_list = privacy_pattern_menu_list;
207
208         for (idx = 0; idx < sizeof(privacy_pattern_menu_list) / sizeof(struct _privacy_menu_item); idx++) {
209                 Privacy_Item_Data *id = calloc(sizeof(Privacy_Item_Data), 1);
210                 if (id) {
211                         id->index = idx;
212                         id->item = elm_genlist_item_append(
213                                                    genlist,             /* genlist object */
214                                                    itc,                 /* item class */
215                                                    id,                  /* data */
216                                                    NULL,
217                                                    ELM_GENLIST_ITEM_NONE,
218                                                    menu_list[idx].func, /* call back */
219                                                    ad);
220                 }
221         }
222
223         elm_genlist_item_class_free(itc);
224
225         elm_object_part_content_set(layout, "elm.genlist", genlist);
226
227         nf_it = elm_naviframe_item_push(ad->nf, NULL, NULL, NULL, layout, NULL);
228         elm_naviframe_item_title_enabled_set(nf_it, EINA_FALSE, EINA_FALSE);
229 }
230
231 char *_gl_privacy_title_get(void *data, Evas_Object *obj, const char *part)
232 {
233         char buf[1024] = {0,};
234         Item_Data *id = data;
235         int index = id->index;
236         char *device_info = NULL;
237
238         if (!strcmp(part, "elm.text.1") || !strcmp(part, "elm.text")) {
239                 snprintf(buf, sizeof(buf) - 1, "%s", _(privacy_menu_list[index].name));
240         } else if (!strcmp(part, "elm.text.2")) {
241                 snprintf(buf, sizeof(buf) - 1, "%s", _(lock_type_str[_get_lock_type_value()]));
242         }
243
244         return strdup(buf);
245 }
246
247 Evas_Object *_gl_privacy_content_get(void *data, Evas_Object *obj, const char *part)
248 {
249         Evas_Object *check = NULL;
250
251         Privacy_Item_Data *id = data;
252
253         if (!strcmp(part, "elm.icon")) {
254                 check = elm_check_add(obj);
255                 elm_check_state_set(check, (_get_see_pattern_value()) ? EINA_TRUE : EINA_FALSE);
256                 /*evas_object_smart_callback_add(check, "changed", _see_pattern_chk_changed_cb, (void *)1); */
257                 evas_object_size_hint_align_set(check, EVAS_HINT_FILL, EVAS_HINT_FILL);
258                 evas_object_size_hint_weight_set(check, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
259
260                 id->check = check;
261         }
262         return check;
263 }
264
265 static void _privacy_lock_cb(void *data, Evas_Object *obj, void *event_info)
266 {
267         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
268
269         appdata *ad = data;
270         if (!ad) {
271                 ERR("appdata is null");
272                 return;
273         }
274
275         if (!_get_lock_type_value()) {
276                 _create_privacy_pattern_list(ad);
277         } else {
278                 app_control_h service;
279                 app_control_create(&service);
280                 app_control_set_app_id(service, "org.tizen.w-lockscreen-setting");
281
282                 app_control_add_extra_data(service, "type", "verify");
283                 app_control_send_launch_request(service, _privacy_lock_verify_cb, ad);
284                 app_control_destroy(service);
285         }
286 }
287
288 void _create_help_popup(void *data)
289 {
290         Evas_Object *popup = NULL;
291         Evas_Object *btn = NULL;
292         Evas_Object *scroller = NULL;
293         Evas_Object *label = NULL;
294
295         appdata *ad = (appdata *) data;
296         if (ad == NULL)
297                 return;
298
299         popup = elm_popup_add(ad->nf);
300         elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
301         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
302         elm_object_translatable_part_text_set(popup, "title,text", "IDS_ST_MBODY_HELP");
303
304         ad->popup = popup;
305
306         scroller = elm_scroller_add(popup);
307         evas_object_size_hint_weight_set(scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
308         elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
309         elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
310         elm_object_style_set(scroller, "effect");
311         elm_object_content_set(popup, scroller);
312         evas_object_show(scroller);
313
314         label = elm_label_add(scroller);
315         elm_object_style_set(label, "popup/default");
316         elm_label_line_wrap_set(label, ELM_WRAP_MIXED);
317         elm_object_translatable_text_set(label, "IDS_LCKSCN_BODY_THE_PRIVACY_LOCK_OPTION_WILL_BE_SHOWN_WHEN_BLUETOOTH_IS_DISCONNECTED");
318
319         evas_object_size_hint_weight_set(label, EVAS_HINT_EXPAND, 0.0);
320         evas_object_size_hint_align_set(label, EVAS_HINT_FILL, EVAS_HINT_FILL);
321         elm_object_content_set(scroller, label);
322         evas_object_show(label);
323
324         ea_object_event_callback_add(popup, EA_CALLBACK_BACK, setting_popup_back_cb, ad);
325
326         evas_object_show(popup);
327 }
328
329 static void _privacy_help_cb(void *data, Evas_Object *obj, void *event_info)
330 {
331         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
332
333         appdata *ad = data;
334         if (ad == NULL) {
335                 DBG("%s", "_privcay_help_cb - ad is null");
336                 return;
337         }
338
339         _create_help_popup(ad);
340 }
341
342 Evas_Object *create_privacy_list(void *data)
343 {
344         appdata *ad = data;
345
346         if (!ad) {
347                 ERR("appdata is null!!");
348                 return NULL;
349         }
350
351         Evas_Object *genlist = NULL;
352         Evas_Object *layout = NULL;
353         struct _privacy_menu_item *menu_list = NULL;
354         int idx = 0;
355
356         Elm_Genlist_Item_Class *itc = NULL;
357
358         Elm_Genlist_Item_Class *itc_2text = elm_genlist_item_class_new();
359         itc_2text->item_style = "2text";
360         itc_2text->func.text_get = _gl_privacy_title_get;
361         itc_2text->func.del = _gl_privacy_del;
362
363         Elm_Genlist_Item_Class *itc_1text = elm_genlist_item_class_new();
364         itc_1text->item_style = "1text";
365         itc_1text->func.text_get = _gl_privacy_title_get;
366         itc_1text->func.del = _gl_privacy_del;
367
368         layout = elm_layout_add(ad->nf);
369         elm_layout_file_set(layout, EDJE_PATH, "setting/genlist/layout");
370         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
371
372         genlist = elm_genlist_add(layout);
373         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
374         /*elm_genlist_block_count_set(genlist, 14); */
375
376         menu_list = privacy_menu_list;
377
378         for (idx = 0; idx < sizeof(privacy_menu_list) / sizeof(struct _privacy_menu_item); idx++) {
379                 if (idx == 0) {
380                         itc = itc_2text;
381                 } else {
382                         itc = itc_1text;
383                 }
384
385                 Privacy_Item_Data *id = calloc(sizeof(Privacy_Item_Data), 1);
386                 if (id) {
387                         id->index = idx;
388                         id->item = elm_genlist_item_append(
389                                                    genlist,             /* genlist object */
390                                                    itc,                 /* item class */
391                                                    id,                  /* data */
392                                                    NULL,
393                                                    ELM_GENLIST_ITEM_NONE,
394                                                    menu_list[idx].func, /* call back */
395                                                    ad);
396                 }
397         }
398
399         elm_genlist_item_class_free(itc_2text);
400         elm_genlist_item_class_free(itc_1text);
401         itc = NULL;
402
403         g_privacy_genlist = genlist;
404
405         elm_object_part_content_set(layout, "elm.genlist", genlist);
406
407         return layout;
408 }
409
410 Eina_Bool clear_privacy_cb(void *data, Elm_Object_Item *it)
411 {
412         g_privacy_genlist = NULL;
413
414         return EINA_TRUE;
415 }
416