Tizen 2.1 base
[apps/home/settings.git] / setting-security / src / setting-security-sim-settings.c
1 /*
2  * setting
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <setting-common-data-type.h>
18 #include <setting-common-data-slp-setting.h>
19 #include <setting-common-draw-widget.h>
20 #include <setting-common-view.h>
21
22 #include <setting-security-sim-settings.h>
23 #include <setting-security-main.h>
24 #include <setting-debug.h>
25
26 static int setting_security_sim_settings_create(void *cb);
27 static int setting_security_sim_settings_destroy(void *cb);
28 static int setting_security_sim_settings_update(void *cb);
29 static int setting_security_sim_settings_cleanup(void *cb);
30
31 setting_view setting_view_security_sim_settings = {
32         .create = setting_security_sim_settings_create,
33         .destroy = setting_security_sim_settings_destroy,
34         .update = setting_security_sim_settings_update,
35         .cleanup = setting_security_sim_settings_cleanup,
36 };
37
38 /* ***************************************************
39  **
40  **basic func
41  **
42  ****************************************************/
43
44 static int setting_security_sim_settings_create(void *cb)
45 {
46         SETTING_TRACE_BEGIN;
47         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
48
49         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
50
51         Evas_Object *scroller;
52         Elm_Object_Item *item = NULL;
53
54         /* add basic layout */
55         setting_push_layout_navi_bar_genlist(ad->win_main_layout,
56                                                    ad->win_get,
57                                                    _("SIM settings"),
58                                                    dgettext("sys_string", "IDS_COM_BODY_BACK"), NULL,
59                                                    setting_security_sim_settings_click_softkey_back_cb,
60                                                    NULL, ad, &scroller, ad->navi_bar);
61
62         /* separator */
63         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
64         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
65
66         /* to do : sim settings menu */
67
68         /* f. sim lock */
69         //if TAPI is responsed in time, ad->sim_status will have invalid value,
70         //or else ad->data_sim_lk will be updated by setting_security_sim_get_facility_cb
71         ad->data_sim_lk = setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
72                                                         setting_security_sim_settings_mouse_up_Gendial_list_cb, ad,
73                                                         SWALLOW_Type_1TOGGLE,
74                                                         NULL, NULL,
75                                                         ad->sim_status,"IDS_ST_BODY_SIM_LOCK", NULL, setting_security_sim_settings_chk_btn_cb);
76         if(ad->data_sim_lk)
77                 ad->data_sim_lk->userdata = ad;
78         else {
79                 SETTING_TRACE_ERROR("ad->data_sim_lk is null");
80                 return SETTING_RETURN_FAIL;
81         }
82
83         //setting_security_main_update_view(ad, "IDS_ST_BODY_SIM_LOCK");
84
85         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
86         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
87
88         /* g. pin lock */
89         //if TAPI is responsed in time, ad->sim_status will have invalid value,
90         //or else ad->data_pin_lk will be updated by setting_security_sim_get_facility_cb
91         ad->data_pin_lk = setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
92                                                         setting_security_sim_settings_mouse_up_Gendial_list_cb, ad,
93                                                         SWALLOW_Type_1TOGGLE,
94                                                         NULL, NULL,
95                                                         ad->pin1_status,"IDS_COM_BODY_PIN_LOCK", NULL, setting_security_sim_settings_chk_btn_cb);
96         if(ad->data_pin_lk)
97                 ad->data_pin_lk->userdata = ad;
98
99         /* h. change pin1 */
100         ad->data_change_pin1 = setting_create_Gendial_field_def(scroller, &(ad->itc_1text),
101                                                                 setting_security_sim_settings_mouse_up_Gendial_list_cb,ad,
102                                                                 SWALLOW_Type_INVALID,
103                                                                 NULL, NULL,
104                                                                 0,"IDS_ST_BODY_CHANGE_PIN1", NULL, NULL);
105
106         if(!ad->pin1_status)
107                 setting_disable_genlist_item(ad->data_change_pin1->item);
108
109         /* i. change pin2 */
110         ad->data_change_pin2 = setting_create_Gendial_field_def(scroller, &(ad->itc_1text),
111                                                                 setting_security_sim_settings_mouse_up_Gendial_list_cb,ad,
112                                                                 SWALLOW_Type_INVALID,
113                                                                 NULL, NULL,
114                                                                 0,"IDS_ST_BODY_CHANGE_PIN2", NULL, NULL);
115
116 #if SUPPORT_FDN
117         /* j. FDN */
118         //if TAPI is responsed in time, ad->sim_status will have invalid value,
119         //or else ad->data_fdn will be updated by setting_security_sim_get_facility_cb
120         ad->data_fdn = setting_create_Gendial_field_def(scroller, &(ad->itc_1text_1icon),
121                                                         setting_security_sim_settings_mouse_up_Gendial_list_cb, ad,
122                                                         SWALLOW_Type_1TOGGLE,
123                                                         NULL, NULL,
124                                                         ad->pin2_status, Keystr_FDN, NULL, setting_security_sim_settings_chk_btn_cb);
125         if(ad->data_fdn)
126                 ad->data_fdn->userdata = ad;
127 #endif
128
129         setting_view_security_sim_settings.is_create = 1;
130
131         return SETTING_RETURN_SUCCESS;
132
133 }
134
135 static int setting_security_sim_settings_destroy(void *cb)
136 {
137         SETTING_TRACE_BEGIN;
138         /* error check */
139         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
140
141         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
142
143         elm_naviframe_item_pop(ad->navi_bar);
144
145         setting_view_security_sim_settings.is_create = 0;
146
147         return SETTING_RETURN_SUCCESS;
148 }
149
150 static int setting_security_sim_settings_update(void *cb)
151 {
152         SETTING_TRACE_BEGIN;
153         /* error check */
154         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
155
156         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
157
158         SETTING_TRACE("Update SIM menu");
159         if (ad->data_sim_lk)//it indicates TAPI is not responsed in time
160         {
161                 ad->data_sim_lk->chk_status = ad->sim_status;
162                 elm_check_state_set(ad->data_sim_lk->eo_check, ad->sim_status);
163         }
164
165         SETTING_TRACE("Update PIN menu");
166         if (ad->data_pin_lk)//it indicates TAPI is not responsed in time
167         {
168                 ad->data_pin_lk->chk_status = ad->pin1_status;
169                 elm_check_state_set(ad->data_pin_lk->eo_check, ad->pin1_status);
170                 if(ad->data_pin_lk->chk_status == 0)
171                         setting_disable_genlist_item(ad->data_change_pin1->item);
172                 else
173                         setting_enable_genlist_item(ad->data_change_pin1->item);
174         }
175
176         return SETTING_RETURN_SUCCESS;
177 }
178
179 static int setting_security_sim_settings_cleanup(void *cb)
180 {
181         SETTING_TRACE_BEGIN;
182         /* error check */
183         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
184
185         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
186         return setting_security_sim_settings_destroy(ad);
187 }
188
189 /* ***************************************************
190  **
191  **call back func
192  **
193  ****************************************************/
194
195 static void get_pin_lock_info_cb(TapiHandle *handle, int result, void *data, void *user_data)
196 {
197         SETTING_TRACE_BEGIN;
198
199         TelSimPinOperationResult_t sec_rt = result;
200         TelSimLockInfo_t *lock = data;
201
202         SettingSecurityUG *ad = (SettingSecurityUG *)user_data;
203
204         /* delete popup */
205         if(ad->sim_popup)
206         {
207                 evas_object_del(ad->sim_popup);
208                 ad->sim_popup = NULL;
209         }
210
211         int old_state = elm_check_state_get(ad->data_pin_lk->eo_check);
212         if (old_state) {
213                 SETTING_TRACE("Current status of PIN Lock is ON");
214                 ad->pw_type = SETTING_SEC_PW_PIN_LOCK_OFF;
215         } else {
216                 SETTING_TRACE("Current status of PIN Lock is OFF");
217                 ad->pw_type = SETTING_SEC_PW_PIN_LOCK_ON;
218         }
219
220         SETTING_TRACE_DEBUG("sec_ret[%d], lock_type[%d], lock_status[%d], retry_count[%d]", sec_rt, lock->lock_type, lock->lock_status, lock->retry_count);
221         if(lock->lock_status == 2) /* PUK */
222         {
223                 SETTING_TRACE("Current status of PIN Lock is Blocked");
224                 ad->pw_type = SETTING_SEC_PW_PIN1_BLOCKED;
225         }
226         else if(lock->lock_status == 5) /* Blocked */
227         {
228                 setting_create_simple_popup(NULL, ad->win_get,
229                                 NULL, _("PUK is blocked. Can't use PIN Lock"));
230                 return;
231         }
232
233         setting_security_create_password_sg(ad);
234 }
235
236  void _draw_pin_onoff_status(void *data, Evas_Object *check)
237 {
238         SETTING_TRACE("PIN Lock");
239
240         ret_if(data == NULL);
241
242         SettingSecurityUG *ad = (SettingSecurityUG *) data;
243         int value = 0;
244         int err = 0;
245         int ret = 0;
246
247         ad->sel_item = 0;       /* 0:click pin lock, 1:change pin1 code, 2:fdn, 3: change pin2 code */
248
249         ret = setting_get_int_slp_key(INT_SLP_SETTING_SIM_SLOT, &value, &err);
250
251         ret = _handle_sim_exception(ad, value);
252         if(ret == SETTING_RETURN_FAIL)
253                 return;
254
255         /* check sim status */
256         int tapi_ret = TAPI_API_INVALID_INPUT;
257         TelSimCardType_t sim_card = TAPI_SIM_CARD_TYPE_UNKNOWN;
258
259         tapi_ret = tel_get_sim_type(ad->handle, &sim_card);
260         SETTING_TRACE("SIM Type is %d", (int)sim_card);
261         if (sim_card == (TelSimCardType_t) TAPI_SIM_CARD_TYPE_UNKNOWN) {
262                 SETTING_TRACE_DEBUG
263                             ("%s*** [ERR] tel_get_sim_type. sim_card=%d ***%s",
264                              SETTING_FONT_RED, sim_card, SETTING_FONT_BLACK);
265                 setting_create_simple_popup(NULL, ad->win_get,
266                                 NULL, _("IDS_COM_BODY_INVALID_SIM_CARD"));
267                 return;
268         }
269
270         /* create popup */
271         if(ad->sim_popup)
272         {
273                 evas_object_del(ad->sim_popup);
274                 ad->sim_popup = NULL;
275         }
276         ad->sim_popup = setting_create_popup_without_btn(ad, ad->win_get,
277                                                                                 NULL, _("Waiting response of SIM ..."), 
278                                                                                 NULL,
279                                                                                 0, FALSE, FALSE);
280
281         if(tel_get_sim_lock_info(ad->handle, TAPI_SIM_LOCK_SC, get_pin_lock_info_cb, ad) != 0)
282         {
283                 SETTING_TRACE_ERROR("failed to call tel_get_sim_lock_info()");
284         }
285 }
286
287 static void get_sim_lock_info_cb(TapiHandle *handle, int result, void *data, void *user_data)
288 {
289         SETTING_TRACE_BEGIN;
290         ret_if(data == NULL || user_data == NULL);
291
292         TelSimPinOperationResult_t sec_rt = result;
293         TelSimLockInfo_t *lock = data;
294
295         SettingSecurityUG *ad = (SettingSecurityUG *)user_data;
296
297         /* delete popup */
298         if(ad->sim_popup)
299         {
300                 evas_object_del(ad->sim_popup);
301                 ad->sim_popup = NULL;
302         }
303
304         int old_state = elm_check_state_get(ad->data_sim_lk->eo_check);
305         if (old_state) {
306                 SETTING_TRACE("Current status of SIM Lock is ON");
307                 ad->pw_type = SETTING_SEC_PW_SIM_LOCK_OFF;
308         } else {
309                 SETTING_TRACE("Current status of SIM Lock is OFF");
310                 ad->pw_type = SETTING_SEC_PW_SIM_LOCK_ON;
311         }
312
313         SETTING_TRACE_DEBUG("sec_ret[%d], lock_type[%d], lock_status[%d], retry_count[%d]", sec_rt, lock->lock_type, lock->lock_status, lock->retry_count);
314
315         if(lock->lock_status == 5) /* Blocked */
316         {
317                 setting_create_simple_popup(NULL, ad->win_get,
318                                 NULL, _("SIM is blocked. Can't use SIM Lock"));
319                 return;
320         }
321
322         setting_security_create_password_sg(ad);
323 }
324
325 void _draw_sim_onoff_status(void *data, Evas_Object *check)
326 {
327         ret_if(data == NULL);
328
329         SettingSecurityUG *ad = (SettingSecurityUG *) data;
330         int value = 0;
331         int err = 0;
332         int ret = 0;
333
334         ret = setting_get_int_slp_key(INT_SLP_SETTING_SIM_SLOT, &value, &err);
335         ret = _handle_sim_exception(ad, value);
336         if(ret == SETTING_RETURN_FAIL)
337                 return;
338
339         /* create popup */
340         if(ad->sim_popup)
341         {
342                 evas_object_del(ad->sim_popup);
343                 ad->sim_popup = NULL;
344         }
345         ad->sim_popup = setting_create_popup_without_btn(ad, ad->win_get,
346                                                                                 NULL, _("Waiting response of SIM ..."), 
347                                                                                 NULL,
348                                                                                 0, FALSE, FALSE);
349
350         if(tel_get_sim_lock_info(ad->handle, TAPI_SIM_LOCK_PS, get_sim_lock_info_cb, ad) != 0)
351         {
352                 SETTING_TRACE_ERROR("failed to call tel_get_sim_lock_info()");
353         }
354 }
355
356 static void get_change_pin_info_cb(TapiHandle *handle, int result, void *data, void *user_data)
357 {
358         SETTING_TRACE_BEGIN;
359         ret_if(data == NULL || user_data == NULL);
360
361         TelSimPinOperationResult_t sec_rt = result;
362         TelSimLockInfo_t *lock = data;
363
364         SettingSecurityUG *ad = (SettingSecurityUG *)user_data;
365
366         SETTING_TRACE_DEBUG("sec_ret[%d], lock_type[%d], lock_status[%d], retry_count[%d]", sec_rt, lock->lock_type, lock->lock_status, lock->retry_count);
367         if(lock->lock_type == TAPI_SIM_LOCK_SC && lock->lock_status == 2) /* PUK required */
368         {
369                 SETTING_TRACE("Current status of PIN Lock is Blocked");
370                 ad->pw_type = SETTING_SEC_PW_PIN1_BLOCKED;
371         }
372         else if(lock->lock_type == TAPI_SIM_LOCK_SC && lock->lock_status == 1) /* PIN required */
373         {
374                 ad->pw_type = SETTING_SEC_PW_CHANGE_PIN1;
375         }
376         else if(lock->lock_type == TAPI_SIM_LOCK_FD && lock->lock_status == 4) /* PUK2 required */
377         {
378                 SETTING_TRACE("Current status of Change PIN2 is Blocked");
379                 ad->pw_type = SETTING_SEC_PW_PIN2_BLOCKED;
380         }
381         else if(lock->lock_type == TAPI_SIM_LOCK_FD && lock->lock_status == 3) /* PIN2 required */
382         {
383                 ad->pw_type = SETTING_SEC_PW_CHANGE_PIN2;
384         }
385         else if(lock->lock_status == 5) /* Blocked */
386         {
387                 setting_create_simple_popup(NULL, ad->win_get,
388                                 NULL, _("Permanent block SIM"));
389                 return;
390         }
391
392         setting_security_create_password_sg(ad);
393 }
394
395 void _mouse_up_change_pin(void *data, int sel_item)
396 {
397         ret_if(data == NULL);
398
399         SettingSecurityUG *ad = (SettingSecurityUG *) data;
400
401         ad->sel_item = sel_item;        /* 0:click pin lock, 1:change pin1 code, 2:fdn, 3: change pin2 code */
402         int ret;
403         int value = 0;
404         int err;
405
406         ret = setting_get_int_slp_key(INT_SLP_SETTING_SIM_SLOT, &value, &err);
407
408         switch (value) {
409                 case VCONFKEY_TELEPHONY_SIM_INSERTED:
410                         break;
411                 case VCONFKEY_TELEPHONY_SIM_NOT_PRESENT:
412                         setting_create_simple_popup(NULL, ad->win_get,
413                                                     NULL, _("IDS_ST_SECURITY_BODY_NOIMSI"));
414                         SETTING_TRACE_DEBUG
415                             ("%s*** [ERR] INCORRECTED SIM. sim_slot_type=%d ***%s",
416                              SETTING_FONT_RED, value, SETTING_FONT_BLACK);
417                         return;
418                         break;
419                 case VCONFKEY_TELEPHONY_SIM_CARD_ERROR:
420                 case VCONFKEY_TELEPHONY_SIM_UNKNOWN:
421                         setting_create_simple_popup(NULL, ad->win_get,
422                                                     NULL, _("IDS_COM_BODY_INVALID_SIM_CARD"));
423                         SETTING_TRACE_DEBUG
424                             ("%s*** [ERR] INCORRECTED SIM. sim_slot_type=%d ***%s",
425                              SETTING_FONT_RED, value, SETTING_FONT_BLACK);
426                         return;
427                         break;
428                 default:
429                         break;
430         }
431
432         /* check sim status */
433         TapiResult_t tapi_ret = TAPI_API_INVALID_INPUT;
434         TelSimCardType_t sim_card = TAPI_SIM_CARD_TYPE_UNKNOWN;
435
436         tapi_ret = tel_get_sim_type(ad->handle, &sim_card);
437
438         if (sim_card == (TelSimCardType_t) SETTING_TAPI_UNKNOWN_SIM_TYPE) {
439                 SETTING_TRACE_DEBUG
440                             ("%s*** [ERR] tel_get_sim_type. sim_card=%d ***%s",
441                              SETTING_FONT_RED, sim_card, SETTING_FONT_BLACK);
442                 setting_create_simple_popup(NULL, ad->win_get,
443                                             NULL, _("IDS_COM_BODY_INVALID_SIM_CARD"));
444                 return;
445         }
446         if(sel_item == 1 && (ad->pin1_status == TAPI_SIM_FACILITY_ENABLED))
447         {
448                 if(tel_get_sim_lock_info(ad->handle, TAPI_SIM_LOCK_SC, get_change_pin_info_cb, ad) != 0)
449                 {
450                         SETTING_TRACE_ERROR("failed to call tel_get_sim_lock_info()");
451                 }
452         }
453         else if(sel_item == 3)
454         {
455                 if(tel_get_sim_lock_info(ad->handle, TAPI_SIM_LOCK_FD, get_change_pin_info_cb, ad) != 0)
456                 {
457                         SETTING_TRACE_ERROR("failed to call tel_get_sim_lock_info()");
458                 }
459                 //ad->pw_type = SETTING_SEC_PW_CHANGE_PIN2;
460                 //setting_security_create_password_sg(ad);
461         }
462 }
463
464 static void
465 setting_security_sim_settings_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj,
466                                                void *event_info)
467 {
468         SETTING_TRACE_BEGIN;
469         /* error check */
470         setting_retm_if(data == NULL, "Data parameter is NULL");
471
472         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
473         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
474         elm_genlist_item_selected_set(item, 0);
475         Setting_GenGroupItem_Data *list_item =
476             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
477
478
479         SettingSecurityUG *ad = (SettingSecurityUG *) data;
480
481         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
482
483         if (!safeStrCmp("IDS_ST_BODY_SIM_LOCK", list_item->keyStr)) {
484                 //it invokes async TAPIs to enable/disable, so don't change the status at first to avoid rollback
485                 _draw_sim_onoff_status(ad, list_item->eo_check);
486
487         }else if (!safeStrCmp("IDS_COM_BODY_PIN_LOCK", list_item->keyStr)) {
488                 //it invokes async TAPIs to enable/disable, so don't change the status at first to avoid rollback
489                 _draw_pin_onoff_status(ad, list_item->eo_check);
490
491         } else if (!safeStrCmp("IDS_ST_BODY_CHANGE_PIN1", list_item->keyStr)) {
492                 _mouse_up_change_pin(ad, 1); /* 0:click pin lock, 1:change pin1 code, 2:fdn, 3: change pin2 code */
493         } else if (!safeStrCmp("IDS_ST_BODY_CHANGE_PIN2", list_item->keyStr)) {
494                 _mouse_up_change_pin(ad, 3); /* 0:click pin lock, 1:change pin1 code, 2:fdn, 3: change pin2 code */
495         }
496 }
497
498 static void
499 setting_security_sim_settings_chk_btn_cb(void *data, Evas_Object *obj,
500                                  void *event_info)
501 {
502         SETTING_TRACE_BEGIN;
503         /* error check */
504         retm_if(data == NULL, "[Setting > Reset] Data parameter is NULL");
505         Setting_GenGroupItem_Data *list_item = (Setting_GenGroupItem_Data *) data;
506         SettingSecurityUG *ad = list_item->userdata;
507
508         list_item->chk_status = elm_check_state_get(obj);       /*  for genlist update status */
509
510         if (!safeStrCmp("IDS_ST_BODY_SIM_LOCK", list_item->keyStr)) {
511                 //Rollback state of toggle.
512                 //it invokes async TAPIs to enable/disable, so don't change the status at first to avoid rollback
513                 list_item->chk_status = !list_item->chk_status;
514                 elm_check_state_set(obj, list_item->chk_status);
515                 _draw_sim_onoff_status(ad, obj);
516
517         }else if (!safeStrCmp("IDS_COM_BODY_PIN_LOCK", list_item->keyStr)) {
518                 //Rollback state of toggle.
519                 //it invokes async TAPIs to enable/disable, so don't change the status at first to avoid rollback
520                 list_item->chk_status = !list_item->chk_status;
521                 elm_check_state_set(obj, list_item->chk_status);
522                 _draw_pin_onoff_status(ad, obj);
523
524         }
525 }
526
527
528 static void
529 setting_security_sim_settings_click_softkey_back_cb(void *data, Evas_Object *obj,
530                                             void *event_info)
531 {
532         SETTING_TRACE_BEGIN;
533         /* error check */
534         retm_if(data == NULL, "[Setting > Security] Data parameter is NULL");
535
536         SettingSecurityUG *ad = (SettingSecurityUG *) data;
537
538         setting_view_change(&setting_view_security_sim_settings, &setting_view_security_main, ad);
539         SETTING_TRACE_END;
540 }