Tizen release 1.0
[apps/home/settings.git] / setting-security / src / setting-security.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved 
5  *
6  * This file is part of org.tizen.setting
7  * Written by Hyejin Kim <hyejin0906.kim@samsung.com>
8  *
9  * PROPRIETARY/CONFIDENTIAL
10  * 
11  * This software is the confidential and proprietary information of 
12  * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
13  * disclose such Confidential Information and shall use it only in
14  * accordance with the terms of the license agreement you entered
15  * into with SAMSUNG ELECTRONICS.
16  *
17  * SAMSUNG make no representations or warranties about the suitability 
18  * of the software, either express or implied, including but not limited
19  * to the implied warranties of merchantability, fitness for a particular
20  * purpose, or non-infringement. SAMSUNG shall not be liable for any
21  * damages suffered by licensee as a result of using, modifying or
22  * distributing this software or its derivatives.
23  *
24  */
25 #include <setting-security.h>
26 #define Max_Passwd_View_Type_Len        64
27 #ifndef UG_MODULE_API
28 #define UG_MODULE_API __attribute__ ((visibility("default")))
29 #endif
30
31 static struct _security_item security_its[] = {
32         { SETTING_SEC_PW_POWER_ON, "SETTING_PW_TYPE_POWER_ON_LOCK" },
33         { SETTING_SEC_PW_PHONE_LOCK, "SETTING_PW_TYPE_PHONE_LOCK" },
34         { SETTING_SEC_PW_PHONE_LOCK_ON, "SETTING_PW_TYPE_PHONE_LOCK_ON" },
35         { SETTING_SEC_PW_SIMPLE_PASSWD, "SETTING_PW_TYPE_SIMPLE_PASSWORD" },
36         { SETTING_SEC_PW_CHANGE_PASSWD, "SETTING_PW_TYPE_CHANGE_PASSWORD" },
37         { SETTING_SEC_PW_SIM_LOCK_ON, "SETTING_PW_TYPE_SIM_LOCK_ON" },
38         { SETTING_SEC_PW_SIM_LOCK_OFF, "SETTING_PW_TYPE_SIM_LOCK_OFF" },
39         { SETTING_SEC_PW_PIN_LOCK_ON, "SETTING_PW_TYPE_PIN_LOCK_ON" },
40         { SETTING_SEC_PW_PIN_LOCK_OFF, "SETTING_PW_TYPE_PIN_LOCK_OFF" },
41         { SETTING_SEC_PW_CHANGE_PIN1, "SETTING_PW_TYPE_CHANGE_PIN1" },
42         { SETTING_SEC_PW_FDN_MODE_ON, "SETTING_PW_TYPE_FDN_MODE_ON" },
43         { SETTING_SEC_PW_FDN_MODE_OFF, "SETTING_PW_TYPE_FDN_MODE_OFF" },
44         { SETTING_SEC_PW_CHANGE_PIN2, "SETTING_PW_TYPE_CHANGE_PIN2" },
45         { SETTING_SEC_PW_PIN1_BLOCKED, "SETTING_PW_TYPE_PIN1_BLOCKED" },
46         { SETTING_SEC_PW_PIN2_BLOCKED, "SETTING_PW_TYPE_PIN2_BLOCKED" },
47         { SETTING_SEC_PW_MAX, NULL }
48 };
49
50 static void setting_security_ug_cb_resize(void *data, Evas *e,
51                                           Evas_Object *obj, void *event_info)
52 {
53         ret_if(data == NULL);
54
55         SettingSecurityUG *ad = (SettingSecurityUG *) data;     /* ad is point to data */
56         setting_view_update(&setting_view_security_main, ad);
57 }
58
59 static void *setting_security_ug_on_create(struct ui_gadget *ug,
60                                            enum ug_mode mode, bundle *data,
61                                            void *priv)
62 {
63         setting_retvm_if((!ug || !priv), NULL, "!ug || !priv");
64
65         SettingSecurityUG *securityUG = priv;
66         securityUG->ug = ug;
67
68         securityUG->win_main_layout = (Evas_Object *) ug_get_parent_layout(ug);
69         securityUG->win_get = (Evas_Object *) ug_get_window();
70         evas_object_show(securityUG->win_main_layout);
71         securityUG->evas = evas_object_evas_get(securityUG->win_main_layout);
72
73         setting_retvm_if(securityUG->win_main_layout == NULL, NULL,
74                          "[Setting >> About] cannot get main window ");
75
76         /* register view node table */
77         setting_view_node_table_intialize();
78         setting_view_node_table_register(&setting_view_security_main, NULL);
79
80         setting_create_Gendial_itc("dialogue/1text", &(securityUG->itc_1text));
81         setting_create_Gendial_itc("dialogue/2text.3", &(securityUG->itc_2text_2));
82         setting_create_Gendial_itc("dialogue/1text.1icon", &(securityUG->itc_1text_1icon));
83
84         securityUG->itc_seperator.item_style = "dialogue/separator/21/with_line"; 
85         securityUG->itc_seperator.func.text_get = NULL;
86         securityUG->itc_seperator.func.content_get = NULL;
87         securityUG->itc_seperator.func.state_get = NULL;
88         securityUG->itc_seperator.func.del = NULL;
89
90         securityUG->update_view_timer = NULL;
91         /*  creating a view. */
92         setting_view_node_set_cur_view(&setting_view_security_main);
93         setting_view_create(&setting_view_security_main, (void *)securityUG);
94         evas_object_event_callback_add(securityUG->win_main_layout,
95                                        EVAS_CALLBACK_RESIZE,
96                                        setting_security_ug_cb_resize,
97                                        securityUG);
98         return securityUG->ly_main;
99 }
100
101 static void setting_security_ug_on_start(struct ui_gadget *ug, bundle *data,
102                                          void *priv)
103 {
104 }
105
106 static void setting_security_ug_on_pause(struct ui_gadget *ug, bundle *data,
107                                          void *priv)
108 {
109 }
110
111 static void setting_security_ug_on_resume(struct ui_gadget *ug, bundle *data,
112                                           void *priv)
113 {
114 }
115
116 static void setting_security_ug_on_destroy(struct ui_gadget *ug, bundle *data,
117                                            void *priv)
118 {
119         setting_retm_if((!ug || !priv), "!ug || !priv");
120         SettingSecurityUG *securityUG = priv;
121
122         evas_object_event_callback_del(securityUG->win_main_layout, EVAS_CALLBACK_RESIZE, setting_security_ug_cb_resize);       /* fix flash issue for gallery */
123         securityUG->ug = ug;
124
125         /*  delete the allocated objects. */
126         setting_view_destroy(&setting_view_security_main, securityUG);
127         if (NULL != ug_get_layout(securityUG->ug)) {
128                 evas_object_hide((Evas_Object *) ug_get_layout(securityUG->ug));
129                 evas_object_del((Evas_Object *) ug_get_layout(securityUG->ug));
130         }
131 }
132
133 static void setting_security_ug_on_message(struct ui_gadget *ug, bundle *msg,
134                                            bundle *data, void *priv)
135 {
136
137 }
138
139 static void setting_security_ug_on_event(struct ui_gadget *ug,
140                                          enum ug_event event, bundle *data,
141                                          void *priv)
142 {
143         switch (event) {
144         case UG_EVENT_LOW_MEMORY:
145                 break;
146         case UG_EVENT_LOW_BATTERY:
147                 break;
148         case UG_EVENT_LANG_CHANGE:
149                 break;
150         case UG_EVENT_ROTATE_PORTRAIT:
151                 break;
152         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
153                 break;
154         case UG_EVENT_ROTATE_LANDSCAPE:
155                 break;
156         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
157                 break;
158         case UG_EVENT_REGION_CHANGE:
159                 break;
160         default:
161                 break;
162         }
163 }
164
165 static void setting_security_ug_on_key_event(struct ui_gadget *ug,
166                                              enum ug_key_event event,
167                                              bundle *data, void *priv)
168 {
169         if (!ug || !priv)
170                 return;
171         SettingSecurityUG *ad = (SettingSecurityUG *) priv;     /* ad is point to priv */
172         switch (event) {
173         case UG_KEY_EVENT_END:
174                 {
175                         if (elm_naviframe_top_item_get(ad->navi_bar) ==
176                            elm_naviframe_bottom_item_get(ad->navi_bar)) {
177                                 ug_destroy_me(ug);
178                         } else {
179                                 setting_view_cb_at_endKey(ad);
180                         }
181                 }
182                 break;
183
184         default:
185                 break;
186         }
187 }
188
189 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
190 {
191         SettingSecurityUG *securityUG = calloc(1, sizeof(SettingSecurityUG));
192         setting_retvm_if(!securityUG, -1,
193                          "Create SettingSecurityUG obj failed");
194
195         ops->create = setting_security_ug_on_create;
196         ops->start = setting_security_ug_on_start;
197         ops->pause = setting_security_ug_on_pause;
198         ops->resume = setting_security_ug_on_resume;
199         ops->destroy = setting_security_ug_on_destroy;
200         ops->message = setting_security_ug_on_message;
201         ops->event = setting_security_ug_on_event;
202         ops->key_event = setting_security_ug_on_key_event;
203         ops->priv = securityUG;
204         ops->opt = UG_OPT_INDICATOR_ENABLE;
205
206         return 0;
207 }
208
209 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
210 {
211         struct SettingSecurityUG *securityUG;
212         setting_retm_if(!ops, "ops == NULL");
213
214         securityUG = ops->priv;
215         if (securityUG)
216                 FREE(securityUG);
217 }
218
219 /* ***************************************************
220  *
221  *general func
222  *
223  ***************************************************/
224 static void setting_security_layout_passwd_ug_cb(struct ui_gadget *ug,
225                                                  enum ug_mode mode, void *priv)
226 {
227         if (!ug || !priv)
228                 return;
229
230         SettingSecurityUG *ad = (SettingSecurityUG *) priv;
231         Evas_Object *base = ug_get_layout(ug);
232         if (!base)
233                 return;
234
235         switch (mode) {
236         case UG_MODE_FULLVIEW:
237                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
238                                                  EVAS_HINT_EXPAND);
239                 elm_win_resize_object_add(ad->win_get, base);
240                 evas_object_show(base);
241                 break;
242         default:
243                 break;
244         }
245         
246         return;
247 }
248
249 static void setting_security_destroy_password_ug_cb(struct ui_gadget *ug,
250                                                     void *priv)
251 {
252         ret_if(priv == NULL || ug == NULL);
253         SettingSecurityUG *ad = (SettingSecurityUG *) priv;
254         if (ug) {
255                 ug_destroy(ug);
256                 ad->ug_passwd = NULL;
257         }
258 }
259
260 gboolean setting_security_create_password_sg(void *data)
261 {
262         /* error check */
263         retv_if(data == NULL, FALSE);
264         
265         SettingSecurityUG *ad = (SettingSecurityUG *) data;     /* ad is point to data */
266         
267         /* prevent the ug from being loaded again due to window event queuing */
268         /* added by JTS: CQ H0100135346 */
269         if (ad->ug_passwd) {
270                 SETTING_TRACE("Password UG is already loaded.");
271                 return FALSE;
272         }
273         
274         if((ad->pw_type < 0) || (ad->pw_type > SETTING_SEC_PW_MAX))
275                 return FALSE;
276
277         char str[Max_Passwd_View_Type_Len + 1] = { 0, };        
278         safeCopyStr(str, security_its[ad->pw_type].pw_type_string, Max_Passwd_View_Type_Len);
279
280         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
281
282         if (!cbs)
283                 return FALSE;
284         cbs->layout_cb = setting_security_layout_passwd_ug_cb;
285         cbs->result_cb = setting_security_result_password_ug_cb;
286         cbs->destroy_cb = setting_security_destroy_password_ug_cb;
287         cbs->priv = (void *)ad;
288         bundle *b = bundle_create();
289         if (!b) {
290                 FREE(cbs);
291                 return FALSE;
292         }
293         bundle_add(b, "viewtype", str);
294
295         ad->ug_passwd = ug_create(ad->ug, "setting-password-efl", UG_MODE_FULLVIEW, b, cbs);
296         if (NULL == ad->ug_passwd) {    /* error handling */
297                 evas_object_show(ad->ly_main);
298         }
299
300         bundle_free(b);
301         FREE(cbs);
302
303         return TRUE;
304 }
305
306
307 /* ***************************************************
308  *
309  *call back func
310  *
311  ***************************************************/
312 void setting_check_onoff_pin_lock_state(TelSimPinType_t type,
313                                         Evas_Object *chk_pin_lk)
314 {
315         ret_if(chk_pin_lk == NULL);
316
317         int tapi_ret = TAPI_API_SUCCESS;
318         TelSimPinStatus_t pin1_status = -1;
319         elm_check_state_set(chk_pin_lk, 0);     /* default state */
320
321         tapi_ret = tel_get_sim_security_status(type, &pin1_status);
322         if (tapi_ret != TAPI_API_SUCCESS)
323                 return;
324
325         SETTING_TRACE("setting_check_onoff_pin_lock_state type: %d, status :%d",
326                       type, pin1_status);
327
328         int state = 0;
329         switch (pin1_status) {
330         case TAPI_SIM_PIN_STATUS_ENABLED:
331                 state = 1;
332                 break;
333         case TAPI_SIM_PIN_STATUS_DISABLED:
334         case TAPI_SIM_PIN_STATUS_BLOCKED:
335         case TAPI_SIM_PIN_STATUS_PUK_BLOCKED:
336         case TAPI_SIM_PIN_STATUS_UNKNOWN:
337                 state = 0;
338                 break;
339         default:
340                 break;
341         }
342         elm_check_state_set(chk_pin_lk, state); /* default state */
343
344 }
345
346 void
347 setting_security_result_password_ug_cb(struct ui_gadget *ug, bundle *b_result,
348                                        void *priv)
349 {
350         /* error check */
351         retm_if(priv == NULL, "Data paremeter is NULL");
352
353         SettingSecurityUG *ad = (SettingSecurityUG *) priv;     /* ad is point to priv */
354
355         if (ad->data_sim_lk)
356                 ad->chk_sim_lk = ad->data_sim_lk->eo_check;
357         if (ad->data_pin_lk)
358                 ad->chk_pin_lk = ad->data_pin_lk->eo_check;
359         if (ad->data_phone_lk)
360                 ad->chk_phone_lk = ad->data_phone_lk->eo_check;
361         if (ad->data_simple_pw)
362                 ad->chk_simple_pw= ad->data_simple_pw->eo_check;
363         const char *result = bundle_get_val(b_result, "result");
364
365         /* divided pw view type */
366         SETTING_TRACE("ad->pw_type: %d result :%s", ad->pw_type, result);
367         ret_if(NULL == result);
368
369         if (0 == safeStrCmp(result, "SETTING_PW_TYPE_PHONE_LOCK"))
370                 setting_check_onoff_status(ad->chk_phone_lk, BOOL_SLP_SETTING_POWER_ON_LOCK);
371         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_SIM_LOCK_ON"))
372                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_SIM, ad->chk_sim_lk);
373         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_SIM_LOCK_OFF"))
374                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_SIM, ad->chk_sim_lk);
375         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_PIN_LOCK_ON"))
376                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_PIN1, ad->chk_pin_lk);
377         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_PIN_LOCK_OFF"))
378                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_PIN1, ad->chk_pin_lk);
379         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_SIMPLE_PASSWORD"))
380         {
381                 setting_check_onoff_status(ad->chk_simple_pw, BOOL_SLP_SETTING_SIMPLE_PASSWORD);        /* reset check state */
382         }
383         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_CHANGE_PIN1"))
384                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_PIN1, ad->chk_pin_lk);
385         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_PIN1_BLOCKED"))
386                 setting_check_onoff_pin_lock_state(TAPI_SIM_PTYPE_PIN1, ad->chk_pin_lk);
387         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_POWER_ON_LOCK"))
388         {
389         } 
390         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_PHONE_LOCK_DISABLE"))
391         {
392                 if (ad->data_phone_lk)
393                         setting_disable_genlist_item(ad->data_phone_lk->item);
394         }
395         else if (0 == safeStrCmp(result, "SETTING_PW_TYPE_SIM_LOCK_DISABLE"))
396         {
397                 if (ad->data_sim_lk)
398                         setting_disable_genlist_item(ad->data_sim_lk->item);
399         }
400         evas_object_show(ad->ly_main);
401 }
402
403 #ifdef Status
404 #undef Status
405 #endif
406
407 UG_MODULE_API int setting_plugin_reset(bundle *data, void *priv)
408 {
409         SETTING_TRACE_BEGIN;
410         int ret = 0;
411 #if SUPPORT_RUN_SYSTEM_COMMAND
412         ret += excuteCmd(SETTING_POSTINST_FILE, 1, "security"); 
413 #else
414         ret += vconf_set_str(VCONFKEY_SETAPPL_PHONE_PASSWD_STR, "");
415
416         /* it may write some stragne thing into /opt/data/setting/set_info */
417         struct stat st; 
418         if (stat(PWD_FILE, &st) == 0)//file exist
419         {
420                 if (remove(PWD_FILE) != 0) //delete the file
421                 {
422                         perror("remove");
423                         SETTING_TRACE("Failed to remove %s", PWD_FILE);
424                         ret += -1;
425                 }
426         }
427         ret += vconf_set_bool(VCONFKEY_SETAPPL_STATE_POWER_ON_LOCK_BOOL, FALSE);
428         ret += vconf_set_bool(VCONFKEY_SETAPPL_STATE_SIMPLE_PASSWORD_BOOL, TRUE);
429 #endif
430         return ret;
431 }
432