c85280fb2aacbfc86b304978affb3600a8c0a0d6
[apps/core/preloaded/settings.git] / setting-reset / src / setting-reset-main.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-reset-main.h>
22
23 #define RESULT_FONT_SIZE 28//18
24 #define ITEM_NAME_FONT_SIZE 35//22
25
26 static int setting_reset_main_create(void *cb);
27 static int setting_reset_main_destroy(void *cb);
28 static int setting_reset_main_update(void *cb);
29 static int setting_reset_main_cleanup(void *cb);
30
31 setting_view setting_view_reset_main = {
32         .create = setting_reset_main_create,
33         .destroy = setting_reset_main_destroy,
34         .update = setting_reset_main_update,
35         .cleanup = setting_reset_main_cleanup,
36 };
37
38 static void setting_reset_main_click_softkey_cancel_cb(void *data,
39                                                        Evas_Object *obj,
40                                                        void *event_info);
41 #if SUPPORT_RESET_DETAILS
42 static void setting_reset_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj, void *event_info);
43 #endif
44
45
46
47 #if SUPPORT_FACTORY_RESET
48 static bool is_factory_reset_installed()
49 {
50         char *file_name = "/usr/bin/factory-reset";
51
52         struct stat st;
53         if(stat(file_name, &st) == 0)
54         {
55                 return true;
56         }
57         else
58         {
59                 SETTING_TRACE_ERROR(" %s is *NOT* present\n", file_name);
60                 return false;
61         }
62 }
63 #endif
64
65
66
67 /* ***************************************************
68  *
69  *basic func
70  *
71  ***************************************************/
72 static int setting_reset_main_create(void *cb)
73 {
74         SETTING_TRACE_BEGIN;
75         /* error check */
76         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
77
78         SettingResetUG *ad = (SettingResetUG *) cb;
79         Evas_Object *scroller = NULL;
80 #if SIMPLY_SUPPORT_ALL_RESET
81         ad->ly_main =
82             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
83                                                    ad->win_get,
84                                                    _("IDS_COM_BODY_RESET"),
85                                                    _("IDS_COM_BODY_BACK"), NULL,
86                                                    setting_reset_main_click_softkey_cancel_cb,
87                                                    NULL, ad, &scroller,
88                                                    &(ad->navi_bar));
89         /* Description */
90         setting_create_Gendial_field_def(scroller, &(ad->itc_1text), NULL,
91                                          ad, SWALLOW_Type_LAYOUT_SPECIALIZTION_X,
92                                          NULL, NULL, 0, RESET_ALL_DESC,
93                                          NULL, NULL);
94         /* Button */
95         Setting_GenGroupItem_Data *item_data =
96             (Setting_GenGroupItem_Data *) calloc(1,
97                                                  sizeof
98                                                  (Setting_GenGroupItem_Data));
99         setting_retvm_if(!item_data, SETTING_RETURN_FAIL, "calloc failed");
100         item_data->userdata = ad;
101         item_data->keyStr = (char *)g_strdup(RESET_ALL);
102         item_data->swallow_type = SWALLOW_Type_1BUTTON;
103
104         item_data->chk_change_cb = __reset_all_button_cb;
105         item_data->item = elm_genlist_item_append(scroller, &(ad->itc_1icon), item_data, NULL,
106                                                     ELM_GENLIST_ITEM_NONE,
107                                                     NULL, NULL);
108         setting_view_reset_main.is_create = 1;
109         ad->main_scroller = scroller;
110         return SETTING_RETURN_SUCCESS;
111 #endif
112
113 #if SUPPORT_RESET_DETAILS
114         ad->ly_main =
115             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
116                                                    ad->win_get,
117                                                    _("IDS_COM_BODY_RESET"),
118                                                    _("IDS_COM_BODY_BACK"), NULL,
119                                                    setting_reset_main_click_softkey_cancel_cb,
120                                                    NULL, ad, &scroller,
121                                                    &(ad->navi_bar));
122
123         Elm_Object_Item *item = NULL;;
124
125         item =
126             elm_genlist_item_append(scroller, &itc_seperator, NULL, NULL,
127                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
128         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
129
130         /* 1. Reset settings */
131         setting_create_Gendial_field_def(scroller, &itc_1text,
132                                                                 setting_reset_main_mouse_up_Gendial_list_cb,ad,
133                                                                 SWALLOW_Type_INVALID,
134                                                                 NULL, NULL,
135                                                                 0, RESET_SETTINGS_STR, NULL, NULL);
136 #if SUPPORT_SIM_CARD_PROFILE
137         /* 2. SIM card profile settings */
138         setting_create_Gendial_field_def(scroller, &itc_1text,
139                                                                 setting_reset_main_mouse_up_Gendial_list_cb,ad,
140                                                                 SWALLOW_Type_INVALID,
141                                                                 NULL, NULL,
142                                                                 0, SIM_CARD_PROFILE_SETTINGS_STR, NULL, NULL);
143 #endif
144 #if SUPPORT_FACTORY_RESET
145         /* 3. Factory reset */
146         setting_create_Gendial_field_def(scroller, &itc_1text,
147                                                                 setting_reset_main_mouse_up_Gendial_list_cb,ad,
148                                                                 SWALLOW_Type_INVALID,
149                                                                 NULL, NULL,
150                                                                 0,FACTORY_RESET_STR, NULL, NULL);
151
152 #endif
153 #if DISABLED_CODE
154         /* Description */
155         setting_create_Gendial_field_def(scroller, &itc_multiline_text, NULL,
156                                          ad, SWALLOW_Type_LAYOUT_SPECIALIZTION_X,
157                                          NULL, NULL, 0, RESET_ALL_DESC,
158                                          NULL, NULL);
159 #endif
160         setting_view_reset_main.is_create = 1;
161         ad->main_scroller = scroller;
162         return SETTING_RETURN_SUCCESS;
163
164 #endif
165
166 #if SUPPORT_RESET_OLD
167         Evas_Object *navi_bar = NULL;
168
169         Evas_Object *controllbar = NULL;
170
171         Evas_Object *view_layout = elm_layout_add(ad->win_main_layout);
172         elm_layout_file_set(view_layout, SETTING_THEME_EDJ_NAME,
173                             "selinfo_bottom");
174         evas_object_size_hint_weight_set(view_layout, EVAS_HINT_EXPAND, 0.0);
175
176         /*  Evas_Object * win_layout, Evas_Object * win_obj */
177         ad->ly_main =
178             setting_create_layout_navi_bar(ad->win_main_layout, ad->win_get,
179                                            _("IDS_COM_BODY_RESET"),
180                                            _("IDS_COM_SK_CANCEL"),
181                                            _("IDS_COM_BODY_RESET"), NULL,
182                                            setting_reset_main_click_softkey_cancel_cb,
183                                            setting_reset_main_click_softkey_reset_cb,
184                                            NULL, ad, view_layout, &navi_bar,
185                                            &controllbar);
186         elm_object_item_disabled_set(elm_toolbar_last_item_get
187                                          (controllbar), EINA_TRUE);
188
189         /* ******************************************create frame */
190         Setting_Done_List_Data *list_data = &(ad->list_data);   /* define a handle */
191         int idx = 0;
192         list_data->win_main = ad->ly_main;
193         list_data->UG_data = ad;
194         list_data->ly_parent = view_layout;
195         list_data->controllbar = controllbar;
196
197
198         list_data->chk_items[idx++].item_name = KeyStr_Select_All;
199         char *ug_args;
200         char *item_name;
201
202         int i = 0;
203         int j = 0;
204
205         for (; i < setting_cfg_get_category_length(); i++) {
206                 SETTING_TRACE("group is:%s", _(setting_cfg_get_category_name(i)));
207
208                 for (j = 0; j < setting_cfg_get_menu_length(i); j++) {
209                         /*SETTING_TRACE("i:%d\tj:%d", i, j);*/
210                         if (Cfg_Item_unResetable ==
211                             setting_cfg_get_resetable_flag_idx(i, j))
212                         {
213                             continue;
214                         }
215                         list_data->chk_items[idx].ug_args = ug_args =
216                                         setting_cfg_get_ug_args_idx(i, j);
217                         list_data->chk_items[idx].item_name = item_name =
218                                         setting_cfg_get_keyname_idx(i, j);
219
220                         SETTING_TRACE("process %s:\t%s",_(item_name), ug_args);
221                         idx++;
222                 }
223         }
224
225         list_data->cur_item_num = idx;
226         SETTING_TRACE("list_data->cur_item_num:%d", list_data->cur_item_num);
227         scroller = setting_create_done_list(list_data);
228         elm_object_part_content_set(view_layout, "elm.swallow.contents", scroller);
229
230         ad->view_layout = view_layout;
231         ad->navi_bar = navi_bar;
232         ad->controllbar = controllbar;
233         /* *******************************************update info */
234
235         setting_view_reset_main.is_create = 1;
236         ad->itc_1text.func.text_get = __result_gl_text_get;
237         return SETTING_RETURN_SUCCESS;
238 #endif
239 }
240
241 static int setting_reset_main_destroy(void *cb)
242 {
243         SETTING_TRACE_BEGIN;
244         /* error check */
245         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
246
247         SettingResetUG *ad = (SettingResetUG *) cb;
248         if (ad->notify) {
249                 evas_object_del(ad->notify);
250                 ad->notify = NULL;
251         }
252         if (ad->old_notify) {
253                 evas_object_del(ad->old_notify);
254                 ad->old_notify = NULL;
255         }
256         if (ad->pop_progress)
257         {
258                 evas_object_del(ad->pop_progress);
259                 ad->pop_progress = NULL;
260         }
261
262         if (ad->ask_popup)
263         {
264                 evas_object_del(ad->ask_popup);
265                 ad->ask_popup = NULL;
266         }
267         if (ad->animator)
268         {
269                 ecore_animator_del(ad->animator);
270                 ad->animator = NULL;
271         }
272
273         if (ad->ly_main != NULL) {
274                 evas_object_del(ad->ly_main);
275                 setting_view_reset_main.is_create = 0;
276         }
277
278         return SETTING_RETURN_SUCCESS;
279 }
280
281 static int setting_reset_main_update(void *cb)
282 {
283         SETTING_TRACE_BEGIN;
284         /* error check */
285         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
286
287         SettingResetUG *ad = (SettingResetUG *) cb;
288
289         if (ad->ly_main != NULL) {
290                 evas_object_show(ad->ly_main);
291         }
292
293         return SETTING_RETURN_SUCCESS;
294 }
295
296 static int setting_reset_main_cleanup(void *cb)
297 {
298         SETTING_TRACE_BEGIN;
299         /* error check */
300         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
301
302         SettingResetUG *ad = (SettingResetUG *) cb;
303
304         if (ad->ly_main != NULL) {
305                 evas_object_hide(ad->ly_main);
306         }
307
308         return SETTING_RETURN_SUCCESS;
309 }
310
311
312 /*
313         execute /usr/bin/factory-reset after creating a process with fork()
314  */
315
316 static void
317 setting_reset_main_result_password_ug_cb(ui_gadget_h ug, service_h result,
318                                          void *priv)
319 {
320         SETTING_TRACE_BEGIN;
321         /* error check */
322         retm_if(priv == NULL, "Data parameter is NULL");
323
324         SettingResetUG *ad = (SettingResetUG *) priv;
325
326         char *ret_str = NULL;
327         service_get_extra_data(result, "result", &ret_str);
328         SETTING_TRACE("result:%s", ret_str);
329
330         retm_if(ret_str == NULL, "result paremeter is NULL");
331
332         if (ret_str) {
333                 if (0 == safeStrCmp("Cancel", ret_str)) {
334                         /* Do not reset */
335                 }
336                 else if (0 == safeStrCmp("SETTING_PW_TYPE_RESET", ret_str)) {
337                         ug_destroy(ad->ug_loading);
338                         ad->ug_loading = NULL;
339                         /* do factory reset here. */
340                         int pid = -1;
341                         pid = fork();
342                         setting_retm_if(pid < 0, "failed to fork");
343
344                         if (pid == 0) {
345                                 char *environ[] = { NULL };
346                                 char *argv[] = {"factory-reset"};
347                                 execve("/usr/bin/factory-reset", argv, environ);
348                                 exit(0);
349                         }
350                 }
351         }
352 }
353
354 static void
355 setting_reset_main_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
356                                 void *priv)
357 {
358         SettingResetUG *ad = (SettingResetUG *) priv;
359         Evas_Object *base;
360
361         if (!ug || !priv)
362                 return;
363         SETTING_TRACE_BEGIN;
364
365         base = (Evas_Object *) ug_get_layout(ug);
366         if (!base)
367                 return;
368
369         switch (mode) {
370         case UG_MODE_FULLVIEW:
371                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
372                                                  EVAS_HINT_EXPAND);
373                 elm_win_resize_object_add(ad->win_get, base);
374                 evas_object_show(base);
375                 break;
376         default:
377                 break;
378         }
379         SETTING_TRACE_END;
380 }
381
382 static void setting_reset_main_destroy_password_ug_cb(ui_gadget_h ug,
383                                                       void *priv)
384 {
385         SETTING_TRACE_BEGIN;
386         /* if(ug) ug_destroy(ug); */
387         ret_if(!priv);
388         SettingResetUG *ad = (SettingResetUG *) priv;
389         if (ug) {
390                 ug_destroy(ug);
391                 ad->ug_loading = NULL;
392         }
393
394 }
395
396 static bool setting_reset_main_create_password_sg(void *data)
397 {
398         SETTING_TRACE_BEGIN;
399         /* error check */
400         retv_if(data == NULL, FALSE);
401
402         SettingResetUG *ad = (SettingResetUG *) data;
403
404         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
405         if (!cbs)
406                 return FALSE;
407         cbs->layout_cb = setting_reset_main_layout_ug_cb;
408         cbs->result_cb = setting_reset_main_result_password_ug_cb;
409         cbs->destroy_cb = setting_reset_main_destroy_password_ug_cb;
410         cbs->priv = (void *)ad;
411         service_h svc;
412         if (service_create(&svc)) {
413                 FREE(cbs);
414                 return FALSE;
415         }
416         service_add_extra_data(svc, "viewtype", "SETTING_PW_TYPE_RESET");
417
418         ad->ug_loading =
419             ug_create(ad->ug, "setting-password-efl", UG_MODE_FULLVIEW, svc, cbs);
420         if (NULL == ad->ug_loading) {   /* error handling */
421         }
422
423         service_destroy(svc);
424         FREE(cbs);
425
426         return TRUE;
427 }
428
429 static void __ask_create_factory_reset_resp_cb(void *data, Evas_Object *obj,
430                                            void *event_info)
431 {
432         SETTING_TRACE_BEGIN;
433
434         SettingResetUG *ad = (SettingResetUG *) data;
435         ret_if(NULL == data);
436
437         int response_type = btn_type(obj);
438         int screen_lock_type = 0;
439
440         if (POPUP_RESPONSE_OK == response_type) {
441                 SETTING_TRACE("OK");
442                 vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &screen_lock_type);
443                 if(screen_lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD
444                         || screen_lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD)
445                 {
446                         setting_reset_main_create_password_sg(ad);
447                 }
448                 else
449                 {
450                         /* do factory reset here. */
451                         int pid = -1;
452                         pid = fork();
453                         setting_retm_if(pid < 0, "failed to fork");
454
455                         if (pid == 0) {
456                                 char *environ[] = { NULL };
457                                 char *argv[] = {"factory-reset"};
458                                 execve("/usr/bin/factory-reset", argv, environ);
459                                 exit(0);
460                         }
461                 }
462         } else if (POPUP_RESPONSE_CANCEL == response_type) {
463                 SETTING_TRACE("NO");
464                 // do nothing
465         }
466
467         evas_object_del(ad->pop_factory_reset);
468         ad->pop_factory_reset = NULL;
469 }
470
471 #if SUPPORT_RESET_DETAILS
472 static void
473 setting_reset_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj, void *event_info)
474 {
475         /* error check */
476         setting_retm_if(data == NULL, "Data parameter is NULL");
477
478         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
479         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
480         elm_genlist_item_selected_set(item, 0);
481
482         Setting_GenGroupItem_Data *list_item =
483             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
484
485         SettingResetUG *ad = (SettingResetUG *) data;
486
487         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
488
489         if (!safeStrCmp(RESET_SETTINGS_STR, list_item->keyStr))
490         {
491                 setting_view_change(&setting_view_reset_main, &setting_view_reset_settings, ad);
492         }
493 #if SUPPORT_SIM_CARD_PROFILE
494         else if (!safeStrCmp(SIM_CARD_PROFILE_SETTINGS_STR, list_item->keyStr))
495         {
496                 /* TBD */
497                 SETTING_TRACE("SIM-CARD PROFILE");
498                 setting_create_simple_popup(NULL, ad->win_get, NULL, "NOT IMPLMENTED YET");
499         }
500 #endif
501 #if SUPPORT_FACTORY_RESET
502         else if (!safeStrCmp(FACTORY_RESET_STR, list_item->keyStr))
503         {
504                 /* TBD */
505                 SETTING_TRACE("FACTORY RESET");
506
507                 if(is_factory_reset_installed())
508                 {
509                         //setting_create_simple_popup(NULL, ad->win_get, NULL, "NOT IMPLMENTED YET");
510                         ad->pop_factory_reset = setting_create_popup_with_btn(ad,
511                                                           ad->win_get,
512                                                           NULL, _("Reset all settings and delete all data. This cannot be stopped once started"),
513                                                           __ask_create_factory_reset_resp_cb,
514                                                           0, 2, _("IDS_COM_SK_YES"),_("IDS_COM_SK_NO"));
515                 } else {
516                         setting_create_simple_popup(NULL, ad->win_get, NULL, "NOT SUPPORTED");
517                 }
518         }
519 #endif
520         /* to do : */
521 }
522 #endif
523
524 static void
525 setting_reset_main_click_softkey_cancel_cb(void *data, Evas_Object *obj,
526                                            void *event_info)
527 {
528         SETTING_TRACE_BEGIN;
529         /* error check */
530         retm_if(data == NULL, "Data parameter is NULL");
531
532         SettingResetUG *ad = (SettingResetUG *) data;
533         if (ad->list_data.selInfoPop) {
534                 evas_object_del(ad->list_data.selInfoPop);
535                 ad->list_data.selInfoPop = NULL;
536         }
537         /* Send destroy request */
538         ug_destroy_me(ad->ug);
539 }
540