tizen 2.4 release
[apps/home/settings.git] / setting-security / src / setting-security-enc-processing.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
25 #include <setting-security-enc-processing.h>
26 #include <setting-debug.h>
27 #include <utilX.h>
28 #include <Ecore_X.h>
29
30 #include <ode.h>
31 #include <dd-deviced.h>
32
33 static int setting_security_enc_processing_create(void *cb);
34 static int setting_security_enc_processing_destroy(void *cb);
35 static int setting_security_enc_processing_update(void *cb);
36 static int setting_security_enc_processing_cleanup(void *cb);
37
38 setting_view setting_view_security_enc_processing = {
39         .create = setting_security_enc_processing_create,
40         .destroy = setting_security_enc_processing_destroy,
41         .update = setting_security_enc_processing_update,
42         .cleanup = setting_security_enc_processing_cleanup,
43 };
44
45 /* ***************************************************
46  **
47  **basic func
48  **
49  ****************************************************/
50
51 static void __encrypting_key_grab(SettingSecurityUG *ad)
52 {
53         SETTING_TRACE_BEGIN;
54         Ecore_X_Window xwin = 0;
55         Ecore_X_Display *disp = NULL;
56
57         int ret = 0;
58
59         if (ad == NULL)
60                 return;
61
62         disp = ecore_x_display_get();
63         xwin = elm_win_xwindow_get((Evas_Object *)ug_get_window());
64
65         ret = eext_win_keygrab_set(xwin, "XF86PowerOff");
66         if (ret)
67                 return;
68         ret = eext_win_keygrab_set(xwin, "XF86Home");
69         if (ret)
70                 return;
71         ret = eext_win_keygrab_set(xwin, "XF86AudioRaiseVolume");
72         if (ret)
73                 return;
74         ret = eext_win_keygrab_set(xwin, "XF86AudioLowerVolume");
75         if (ret)
76                 return;
77
78         ad->event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, NULL, NULL);
79         SETTING_TRACE_END;
80 }
81 static void __encrypting_key_ungrab(SettingSecurityUG *ad)
82 {
83         SETTING_TRACE_BEGIN;
84         Ecore_X_Window xwin = 0;
85         Ecore_X_Display *disp = NULL;
86
87         int ret = 0;
88
89         if (ad == NULL)
90                 return;
91
92         disp = ecore_x_display_get();
93         xwin = elm_win_xwindow_get((Evas_Object *)ug_get_window());
94
95         ret = eext_win_keygrab_unset(xwin, "XF86PowerOff");
96         if (ret)
97                 return;
98         ret = eext_win_keygrab_unset(xwin, "XF86Home");
99         if (ret)
100                 return;
101         ret = eext_win_keygrab_unset(xwin, "XF86AudioRaiseVolume");
102         if (ret)
103                 return;
104         ret = eext_win_keygrab_unset(xwin, "XF86AudioLowerVolume");
105         if (ret)
106                 return;
107
108         ecore_event_handler_del(ad->event_handler);
109 }
110
111 static void __vconf_change_cb(keynode_t *key, void *data)
112 {
113         SETTING_TRACE_BEGIN;
114         ret_if(data == NULL);
115         SettingSecurityUG *ad = (SettingSecurityUG *) data;
116
117         /* To do : update progressbar */
118         char *vconf_name = vconf_keynode_get_name(key);
119         char *vconf_value = NULL;
120         char temp[MAX_COMMON_BUFFER_LEN] = {0,};
121
122         /*if (!safeStrCmp(vconf_name, VCONFKEY_ODE_ENCRYPT_PROGRESS)) {*/
123         if (!safeStrCmp(vconf_name, "db/ode/encrypt_progress")) {
124                 vconf_value = (char *)g_strdup(vconf_keynode_get_str(key));
125                 if (vconf_value == NULL)
126                         return;
127
128                 SETTING_TRACE_DEBUG("progress : %s", vconf_value);
129                 float i_value = atof(vconf_value);
130                 if (i_value <= 0)
131                         return;
132
133                 if (ad->cur_enc_mode == 0) {
134                         snprintf(temp, MAX_COMMON_BUFFER_LEN, _(ENCRYPTING_GUIDE_02), (int)i_value);
135                 } else if (ad->cur_enc_mode == 1) {
136                         snprintf(temp, MAX_COMMON_BUFFER_LEN, _(DECRYPTING_GUIDE_02), (int)i_value);
137                 }
138                 elm_object_part_text_set(ad->enc_layout, "description", temp);
139                 SETTING_TRACE_DEBUG("current status : %s", temp);
140
141                 elm_progressbar_value_set(ad->enc_progressbar, i_value / 100.0);
142                 SETTING_TRACE_DEBUG("progressbar is updated. %f", i_value / 100.0);
143                 G_FREE(vconf_value);
144
145                 if (i_value == 100) {
146                         SETTING_TRACE_DEBUG("100%");
147                         /* To do : Encryption completed. Restart target. */
148                         if (ode_crypto_complete() == 0) {
149                                 SETTING_TRACE_DEBUG("ode_crypto_complete() success.");
150                                 ode_deinit();
151                                 /*system("reboot"); */
152                         }
153                 }
154         }
155 }
156
157 int __listen_vconf_change(void *data)
158 {
159         SETTING_TRACE_BEGIN;
160         retv_if(data == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
161
162         int ret = 0;
163
164         ret = vconf_notify_key_changed(VCONFKEY_ODE_ENCRYPT_PROGRESS,
165                                        __vconf_change_cb, data);
166         setting_retvm_if(ret < 0, ret, "%s notifications Failed(%d)",
167                          (char *)VCONFKEY_ODE_ENCRYPT_PROGRESS, ret);
168
169         return ret;
170 }
171
172 static int setting_security_enc_processing_create(void *cb)
173 {
174         SETTING_TRACE_BEGIN;
175         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
176
177         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
178
179         if (ad->input_pwd == NULL)
180                 return SETTING_RETURN_FAIL;
181
182         Evas_Object *window = NULL;
183         Evas_Object *layout = NULL;
184         Evas_Object *progressbar = NULL;
185         char enc_type[16] = {0,};
186
187         layout = elm_layout_add(ad->win_main_layout);
188         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
189
190         elm_layout_file_set(layout, SETTING_THEME_EDJ_NAME, "encryption_layout");
191         /*elm_win_center(layout, EINA_TRUE, EINA_TRUE); */
192         /*elm_win_fullscreen_set(layout, EINA_TRUE); */
193         /*setting_resize_object(layout, 720, 1280); */
194
195         /*elm_win_resize_object_add(ad->win_get, layout); */
196
197         progressbar = elm_progressbar_add(layout);
198         elm_progressbar_value_set(progressbar, 0);
199         elm_object_part_content_set(layout, "progressbar", progressbar);
200
201         evas_object_show(layout);
202
203         ad->enc_progressbar = progressbar;
204         ad->enc_layout = layout;
205
206         /* grab home key and volume key */
207         __encrypting_key_grab(ad);
208         __listen_vconf_change(ad);
209
210         if (ode_init() < 0) {
211                 SETTING_TRACE_DEBUG("ode_init() failed");
212                 setting_security_enc_processing_destroy(ad);
213                 return SETTING_RETURN_FAIL;
214         }
215
216         if (ad->cur_enc_mode == 0) {
217                 elm_object_part_text_set(layout, "title", _(Keystr_Encrypting));
218                 elm_object_part_text_set(layout, "description", _(ENCRYPTING_GUIDE_01));
219                 safeCopyStr(enc_type, "inplace", 16);
220 #if TEMP
221                 if (ode_enable(enc_type, ad->input_pwd) == 0) {
222                         SETTING_TRACE_DEBUG("ode_enable(%s, %s) success", ad->input_pwd, enc_type);
223                 } else {
224                         SETTING_TRACE_DEBUG("ode_enable(%s, %s) failed", ad->input_pwd, enc_type);
225                 }
226 #endif
227         } else if (ad->cur_enc_mode == 1) {
228                 elm_object_part_text_set(layout, "title", _(Keystr_Decrypting));
229                 elm_object_part_text_set(layout, "description", _(DECRYPTING_GUIDE_02));
230                 safeCopyStr(enc_type, "disable", 16);
231 #if TEMP
232                 if (ode_disable(ad->input_pwd) == 0) {
233                         SETTING_TRACE_DEBUG("ode_disable(%s, %s) success", ad->input_pwd, enc_type);
234                 } else {
235                         SETTING_TRACE_DEBUG("ode_disable(%s, %s) failed", ad->input_pwd, enc_type);
236                 }
237 #endif
238         }
239
240         setting_view_security_enc_processing.is_create = 1;
241
242         return SETTING_RETURN_SUCCESS;
243
244 }
245
246 static int setting_security_enc_processing_destroy(void *cb)
247 {
248         /* error check */
249         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
250
251         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
252
253         vconf_ignore_key_changed(VCONFKEY_ODE_ENCRYPT_PROGRESS, __vconf_change_cb);
254
255         if (ad->enc_layout) {
256                 evas_object_del(ad->enc_layout);
257                 ad->enc_layout = NULL;
258         }
259
260         setting_view_security_enc_processing.is_create = 0;
261
262         return SETTING_RETURN_SUCCESS;
263 }
264
265 static int setting_security_enc_processing_update(void *cb)
266 {
267         /* error check */
268         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
269
270         return SETTING_RETURN_SUCCESS;
271 }
272
273 static int setting_security_enc_processing_cleanup(void *cb)
274 {
275         /* error check */
276         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
277
278         setting_security_enc_processing_destroy(cb);
279
280         return SETTING_RETURN_SUCCESS;
281 }