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