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