tizen 2.3 release
[apps/home/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
22 #include <setting-reset-main.h>
23 #include <dd-deviced.h>
24 #if SUPPORT_ENCRYPTION
25 #include <ode.h>
26 #include <act_lock_api.h>
27 #endif
28
29 #include <dbus/dbus-glib-lowlevel.h>
30
31 #define FACTORY_RESET_DBUS_OBJPATH     "/org.tizen/factoryreset"
32 #define FACTORY_RESET_DBUS_DEST        "org.tizen.factoryreset"
33 #define FACTORY_RESET_DBUS_INTERFACE   FACTORY_RESET_DBUS_DEST".start"
34
35 #define RESULT_FONT_SIZE 28//18
36 #define ITEM_NAME_FONT_SIZE 35//22
37 #define PREDEF_FACTORY_RESET    "launchfr"
38
39 static int setting_reset_main_create(void *cb);
40 static int setting_reset_main_destroy(void *cb);
41 static int setting_reset_main_update(void *cb);
42 static int setting_reset_main_cleanup(void *cb);
43
44 setting_view setting_view_reset_main = {
45         .create = setting_reset_main_create,
46         .destroy = setting_reset_main_destroy,
47         .update = setting_reset_main_update,
48         .cleanup = setting_reset_main_cleanup,
49 };
50
51 static Eina_Bool setting_reset_main_click_softkey_cancel_cb(void *data, Elm_Object_Item *it);
52
53 #if SUPPORT_RESET_DETAILS
54 static void setting_reset_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj, void *event_info);
55 #endif
56
57
58
59 #if SUPPORT_FACTORY_RESET
60 /**
61  *@brief check the file "/usr/bin/factory-reset " exist.
62 */
63 static bool is_factory_reset_installed()
64 {
65         char *file_name = "/usr/bin/factory-reset";
66
67         struct stat st;
68         if(stat(file_name, &st) == 0) {
69                 return true;
70         }
71         else {
72                 SETTING_TRACE_ERROR(" %s is *NOT* present\n", file_name);
73                 return false;
74         }
75 }
76 #endif
77
78
79
80 /**
81  *@brief create the reset main view.
82 */
83 static int setting_reset_main_create(void *cb)
84 {
85         SETTING_TRACE_BEGIN;
86         /* error check */
87         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
88
89         SettingResetUG *ad = (SettingResetUG *) cb;
90         Evas_Object *scroller = NULL;
91
92 #if SUPPORT_RESET_DETAILS
93         ad->ly_main =
94             setting_create_layout_navi_bar_genlist(ad->win_main_layout,
95                                                    ad->win_get,
96                                                    _(KeyStr_Reset),
97                                                    NULL, NULL,
98                                                    NULL,
99                                                    NULL, ad, &scroller,
100                                                    &(ad->navi_bar));
101         ad->genlist = scroller;
102         evas_object_smart_callback_add(ad->genlist, "realized", __gl_realized_cb, ad);
103         Elm_Object_Item * navi_it = elm_naviframe_top_item_get(ad->navi_bar);
104         elm_naviframe_item_pop_cb_set(navi_it, setting_reset_main_click_softkey_cancel_cb, ad);
105         //Elm_Object_Item *item = NULL;;
106         Setting_GenGroupItem_Data *item_data = NULL;
107
108
109         ADD_GL_SEPARATOR(scroller);
110
111         bool isFoundOther = FALSE;
112 #if SUPPORT_FACTORY_RESET
113         /* 3. Factory reset */
114         item_data = setting_create_Gendial_field_def(scroller, &itc_1text,
115                                                                 setting_reset_main_mouse_up_Gendial_list_cb,ad,
116                                                                 SWALLOW_Type_INVALID,
117                                                                 NULL, NULL,
118                                                                 0,FACTORY_RESET_STR, NULL, NULL);
119         item_data->group_style = isFoundOther ? SETTING_GROUP_STYLE_BOTTOM : SETTING_GROUP_STYLE_NONE;
120 #endif
121         setting_view_reset_main.is_create = 1;
122         ad->main_scroller = scroller;
123         return SETTING_RETURN_SUCCESS;
124
125 #endif
126 }
127
128 static int setting_reset_main_destroy(void *cb)
129 {
130         SETTING_TRACE_BEGIN;
131         /* error check */
132         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
133
134         SettingResetUG *ad = (SettingResetUG *) cb;
135         evas_object_smart_callback_del(ad->genlist, "realized", __gl_realized_cb);
136
137         if (ad->notify) {
138                 evas_object_del(ad->notify);
139                 ad->notify = NULL;
140         }
141         if (ad->old_notify) {
142                 evas_object_del(ad->old_notify);
143                 ad->old_notify = NULL;
144         }
145         if (ad->pop_progress) {
146                 evas_object_del(ad->pop_progress);
147                 ad->pop_progress = NULL;
148         }
149
150         if (ad->ask_popup) {
151                 evas_object_del(ad->ask_popup);
152                 ad->ask_popup = NULL;
153         }
154         if (ad->animator) {
155                 ecore_animator_del(ad->animator);
156                 ad->animator = NULL;
157         }
158         if (ad->pop) {
159                 evas_object_del(ad->pop);
160                 ad->pop = NULL;
161         }
162         if (ad->pop_factory_reset) {
163                 evas_object_del(ad->pop_factory_reset);
164                 ad->pop_factory_reset = NULL;
165         }
166         if (ad->ly_main != NULL) {
167                 evas_object_del(ad->ly_main);
168                 setting_view_reset_main.is_create = 0;
169         }
170
171         return SETTING_RETURN_SUCCESS;
172 }
173
174 static int setting_reset_main_update(void *cb)
175 {
176         SETTING_TRACE_BEGIN;
177         /* error check */
178         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
179
180         SettingResetUG *ad = (SettingResetUG *) cb;
181
182         if (ad->ly_main != NULL) {
183                 evas_object_show(ad->ly_main);
184         }
185
186         return SETTING_RETURN_SUCCESS;
187 }
188
189 static int setting_reset_main_cleanup(void *cb)
190 {
191         SETTING_TRACE_BEGIN;
192         /* error check */
193         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
194
195         SettingResetUG *ad = (SettingResetUG *) cb;
196
197         if (ad->ly_main != NULL) {
198                 evas_object_hide(ad->ly_main);
199         }
200
201         return SETTING_RETURN_SUCCESS;
202 }
203
204 static void __setting_reset_main_factory_reset()
205 {
206         DBusMessage *message = NULL;
207         DBusConnection *conn = NULL;
208
209         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
210         if (!conn) {
211                 SETTING_TRACE_ERROR("dbus_bus_get() error");
212                 return;
213         }
214         message = dbus_message_new_signal(FACTORY_RESET_DBUS_OBJPATH,
215                         FACTORY_RESET_DBUS_INTERFACE,
216                         "setting");
217         if (!message) {
218                 SETTING_TRACE_ERROR("dbus_message_new_signal() error");
219                 dbus_connection_unref(conn);
220                 return;
221         }
222         dbus_message_set_destination(message, FACTORY_RESET_DBUS_DEST);
223         dbus_connection_send(conn, message, NULL);
224         dbus_message_unref(message);
225         dbus_connection_unref(conn);
226 }
227
228 /**
229  * @brief do some init before the factory reset.
230  */
231 static void __setting_reset_main_elm_profile_initialise()
232 {
233         SETTING_TRACE_BEGIN;
234         const char *profile = NULL;
235         const char *pdir = NULL;
236         Eina_List *l = NULL;
237         Eina_List *profs = NULL;
238
239         profs = elm_config_profile_list_get();
240         EINA_LIST_FOREACH(profs, l, profile)
241         {
242                 pdir = elm_config_profile_dir_get(profile, EINA_TRUE);
243                 if (pdir) {
244                         ecore_file_recursive_rm(pdir);
245                         elm_config_profile_dir_free(pdir);
246                 }
247         }
248         elm_config_profile_list_free(profs);
249 }
250 /**
251  * @brief do some init before the factory reset.
252  */
253 static void __setting_reset_main_initialise_for_factory_reset()
254 {
255         SETTING_TRACE_BEGIN;
256         __setting_reset_main_elm_profile_initialise();
257 }
258
259
260 /*
261   *     execute /usr/bin/factory-reset after creating a process with fork()
262   */
263 static void
264 setting_reset_main_result_password_ug_cb(ui_gadget_h ug, app_control_h result,
265                                          void *priv)
266 {
267         SETTING_TRACE_BEGIN;
268         /* error check */
269         retm_if(priv == NULL, "Data parameter is NULL");
270
271         SettingResetUG *ad = (SettingResetUG *) priv;
272
273         char *ret_str = NULL;
274         app_control_get_extra_data(result, "result", &ret_str);
275         SETTING_TRACE("result:%s", ret_str);
276
277         retm_if(ret_str == NULL, "result paremeter is NULL");
278
279         if (ret_str) {
280                 if (0 == safeStrCmp("Cancel", ret_str)) {
281                         /* Do not reset */
282                 }
283                 else if (!strncmp(ret_str, "SETTING_PW_TYPE_", strlen("SETTING_PW_TYPE_")))
284                 {
285                         setting_ug_destroy(ad->ug_loading);
286                         ad->ug_loading = NULL;
287
288                         /* before factory reset, do some initialization */
289                         __setting_reset_main_initialise_for_factory_reset();
290
291 #if SUPPORT_ENCRYPTION
292                         // If ode status is "encrypted" or "mounted', call ode_restore() instead of below.
293                         char *encryption_state = NULL;
294                         encryption_state = vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
295                         if(encryption_state && (safeStrCmp(encryption_state, "mounted") == 0 || safeStrCmp(encryption_state, "encrypted") == 0)) {
296                                 ode_init();
297                                 ode_restore();
298                                 ode_deinit();
299                         }
300                         else {
301                                 //deviced_call_predef_action(PREDEF_FACTORY_RESET, 0, NULL);
302                                 __setting_reset_main_factory_reset();
303                         }
304                         FREE(encryption_state);
305 #else
306                         //deviced_call_predef_action(PREDEF_FACTORY_RESET, 0, NULL);
307                         __setting_reset_main_factory_reset();
308 #endif
309                 }
310                 FREE(ret_str);
311         }
312 }
313
314 /**
315  * @brief the callback function for calling ug layout,it use for call checking screen lock ug.
316 */
317 static void
318 setting_reset_main_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
319                                 void *priv)
320 {
321         //SettingResetUG *ad = (SettingResetUG *) priv;
322         Evas_Object *base;
323
324         if (!ug || !priv) {
325                 return;
326         }
327         SETTING_TRACE_BEGIN;
328
329         base = (Evas_Object *) ug_get_layout(ug);
330         if (!base) {
331                 return;
332         }
333         switch (mode) {
334         case UG_MODE_FULLVIEW:
335                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
336                                                  EVAS_HINT_EXPAND);
337                 //elm_win_resize_object_add(ad->win_get, base);
338                 evas_object_show(base);
339                 break;
340         default:
341                 break;
342         }
343         SETTING_TRACE_END;
344 }
345
346 static void setting_reset_main_destroy_ug_cb(ui_gadget_h ug,
347                                                       void *priv)
348 {
349         SETTING_TRACE_BEGIN;
350         /* if(ug) ug_destroy(ug); */
351         ret_if(!priv);
352         SettingResetUG *ad = (SettingResetUG *) priv;
353         if (ug) {
354                 setting_ug_destroy(ug);
355                 ad->ug_loading = NULL;
356         }
357 }
358 /**
359  *@brief the function check the lockscreen.If the screen lock was set by password,It need to check the password before reset operation.
360 */
361 static bool setting_reset_main_create_password_sg(void *data, int cur_locktype)
362 {
363         SETTING_TRACE_BEGIN;
364         /* error check */
365         retv_if(data == NULL, FALSE);
366
367         SettingResetUG *ad = (SettingResetUG *) data;
368
369         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
370         if (!cbs)
371                 return FALSE;
372         cbs->layout_cb = setting_reset_main_layout_ug_cb;
373         cbs->result_cb = setting_reset_main_result_password_ug_cb;
374         cbs->destroy_cb = setting_reset_main_destroy_ug_cb;
375         cbs->priv = (void *)ad;
376         app_control_h svc;
377         if (app_control_create(&svc))
378         {
379                 FREE(cbs);
380                 return FALSE;
381         }
382
383         if(cur_locktype == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD)
384         {
385                 app_control_add_extra_data(svc, "viewtype", "SETTING_PW_TYPE_SIMPLE_PASSWORD");
386         }
387         else if(cur_locktype == SETTING_SCREEN_LOCK_TYPE_PASSWORD)
388         {
389                 app_control_add_extra_data(svc, "viewtype", "SETTING_PW_TYPE_PASSWORD");
390         }
391         else
392         {
393                 SETTING_TRACE_DEBUG("unsupported locktype : %d", cur_locktype);
394                 app_control_destroy(svc);
395                 FREE(cbs);
396                 return FALSE;
397         }
398
399         elm_object_tree_focus_allow_set(ad->ly_main, EINA_FALSE);
400         ad->ug_loading =
401             setting_ug_create(ad->ug, "setting-password-efl", UG_MODE_FULLVIEW, svc, cbs);
402         if (NULL == ad->ug_loading) {   /* error handling */
403         }
404
405         app_control_destroy(svc);
406         FREE(cbs);
407
408         return TRUE;
409 }
410
411 /**
412  *@brief callback function for factory reset popup,when you excute the factory reset,it will popup the message to check.
413 */
414 static void __ask_create_factory_reset_resp_cb(void *data, Evas_Object *obj,
415                                            void *event_info)
416 {
417         SETTING_TRACE_BEGIN;
418
419         SettingResetUG *ad = (SettingResetUG *) data;
420         ret_if(NULL == data);
421
422         int response_type = POPUP_RESPONSE_CANCEL;
423         char *utf8 = elm_entry_markup_to_utf8(elm_object_text_get(obj));
424         if (0 == safeStrCmp(utf8, _("IDS_ST_HEADER_RESET")))
425         {
426                 response_type = POPUP_RESPONSE_OK;
427         }
428         FREE(utf8);
429         int screen_lock_type = 0;
430
431         if (POPUP_RESPONSE_OK == response_type) {
432                 SETTING_TRACE("OK");
433                 vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &screen_lock_type);
434                 if(screen_lock_type == SETTING_SCREEN_LOCK_TYPE_PASSWORD
435                         || screen_lock_type == SETTING_SCREEN_LOCK_TYPE_SIMPLE_PASSWORD)
436                 {
437                         setting_reset_main_create_password_sg(ad, screen_lock_type);
438                 }
439                 else {
440                         /* before factory reset, do some initialization */
441                         __setting_reset_main_initialise_for_factory_reset();
442                         __setting_reset_main_factory_reset();
443                         //deviced_call_predef_action(PREDEF_FACTORY_RESET, 0, NULL);
444                 }
445         }
446         else if (POPUP_RESPONSE_CANCEL == response_type) {
447                 SETTING_TRACE("NO");
448                 // do nothing
449         }
450
451         evas_object_del(ad->pop_factory_reset);
452         ad->pop_factory_reset = NULL;
453 }
454
455 /**
456  *@brief the callback for reboot,when you reset  the factory mode,it will be called to reboot the target.
457 */
458 #ifdef TARGET
459 static void setting_reset_reboot_resp_cb(void *data, Evas_Object *obj,
460                                         void *event_info)
461 {
462         SETTING_TRACE_BEGIN;
463         int response_type = POPUP_RESPONSE_CANCEL;
464         char *utf8 = elm_entry_markup_to_utf8(elm_object_text_get(obj));
465         if (0 == safeStrCmp(utf8, _("IDS_ST_HEADER_RESET")))
466         {
467                 response_type = POPUP_RESPONSE_OK;
468         }
469         FREE(utf8);
470
471         if (POPUP_RESPONSE_OK == response_type) {                       // OK cliked
472                 // reboot here
473                 deviced_request_reboot();
474
475         }
476         else if (POPUP_RESPONSE_CANCEL == response_type) { // cancel clicked
477                 // DO NOTHING
478         }
479
480         SettingResetUG *ad = data;
481         if (ad->pop) {
482                 evas_object_del(ad->pop);
483                 ad->pop = NULL;
484         }
485 }
486 #endif
487
488 #if SUPPORT_RESET_DETAILS
489 static void
490 setting_reset_main_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj, void *event_info)
491 {
492         /* error check */
493         setting_retm_if(data == NULL, "Data parameter is NULL");
494
495         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
496         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
497         elm_genlist_item_selected_set(item, 0);
498
499         Setting_GenGroupItem_Data *list_item =
500             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
501
502         SettingResetUG *ad = (SettingResetUG *) data;
503
504         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
505
506         if (!safeStrCmp(RESET_SETTINGS_STR, list_item->keyStr)) {
507                 setting_view_change(&setting_view_reset_main, &setting_view_reset_settings, ad);
508         }
509 #if SUPPORT_FACTORY_RESET
510         else if (!safeStrCmp(FACTORY_RESET_STR, list_item->keyStr))
511         {
512                 /* TBD */
513                 SETTING_TRACE("FACTORY RESET");
514                 if(is_factory_reset_installed()) {
515                         char guide_str[2048 + 1] = {0,};
516                         int ret = snprintf(guide_str, 2048, "%s %s", _(FACTORY_RESET00), _(FACTORY_RESET02));
517                         if(ret < 0) {
518                                 SETTING_TRACE("snprintf failed");
519                         }
520                         //setting_create_simple_popup(NULL, ad->win_get, NULL, "NOT IMPLMENTED YET");
521                         ad->pop_factory_reset = setting_create_popup_with_btn(ad,
522                                                           ad->win_get,
523                                                           NULL, guide_str,
524                                                           __ask_create_factory_reset_resp_cb,
525                                                           0, 2, _("IDS_ST_HEADER_RESET"),_("IDS_COM_SK_CANCEL"));
526                 }
527                 else {
528                         setting_create_simple_popup(NULL, ad->win_get, NULL, _("IDS_MSGF_POP_UNSUPPORTED"));
529                 }
530         }
531 #endif
532         else if (!safeStrCmp(KeyStr_Accounts, list_item->keyStr))
533         {
534                 struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
535                 if (!cbs) {
536                         return;
537                 }
538
539                 cbs->layout_cb = setting_reset_main_layout_ug_cb;
540                 cbs->result_cb = NULL;
541                 cbs->destroy_cb = setting_reset_main_destroy_ug_cb;
542                 cbs->priv = (void *)ad;
543
544                 app_control_h svc;
545                 if (app_control_create(&svc)) {
546                         FREE(cbs);
547                         return;
548                 }
549                 app_control_add_extra_data(svc, "mode", "account_list");
550                 elm_object_tree_focus_allow_set(ad->ly_main, EINA_FALSE);
551                 ad->ug_loading =
552                     setting_ug_create(ad->ug, "setting-myaccount-efl", UG_MODE_FULLVIEW, svc, cbs);
553
554                 if (NULL == ad->ug_loading) {   /* error handling */
555                         SETTING_TRACE_ERROR("NULL == ad->ug_loading");
556                 }
557                 app_control_destroy(svc);
558                 FREE(cbs);
559         }
560 }
561 #endif
562
563
564 /**
565  *@brief the callback function for cancel button,when press the back key or cancel button,it will be called.
566 */
567 static Eina_Bool setting_reset_main_click_softkey_cancel_cb(void *data, Elm_Object_Item *it)
568 {
569         SETTING_TRACE_BEGIN;
570         /* error check */
571         retvm_if(data == NULL, EINA_FALSE, "Data parameter is NULL");
572
573         SettingResetUG *ad = (SettingResetUG *) data;
574         if (ad->list_data.selInfoPop) {
575                 evas_object_del(ad->list_data.selInfoPop);
576                 ad->list_data.selInfoPop = NULL;
577         }
578         /* Send destroy request */
579         ug_destroy_me(ad->ug);
580
581         return EINA_FALSE;
582 }
583