tizen 2.3 release
[apps/home/settings.git] / setting-storage / src / setting-storage-SD.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <glib.h>
20 #include <sys/mount.h>
21 #if SUPPORT_ENCRYPTION
22 #include <ode.h>
23 #endif
24 #include "setting-storage-utils.h"
25 #include "setting-storage-SD.h"
26
27 typedef void (*storageUg_mmc_cb) (int result, void *data);
28
29 enum {
30         STORAGEUG_SD_REQ_NONE,
31         STORAGEUG_SD_REQ_MOUNT,
32         STORAGEUG_SD_REQ_UNMOUNT,
33         STORAGEUG_SD_REQ_FORMAT
34 };
35
36 static void storageUg_SD_finish_progress(int result, void* data)
37 {
38         SettingStorageUG *ad = data;
39
40         ret_if(data == NULL);
41
42         if (ad->popup)
43         {
44                 evas_object_del(ad->popup);
45                 ad->popup = NULL;
46         }
47
48         if (result < 0)
49         {
50                 SETTING_TRACE_ERROR("result(%d) Invalid", result);
51                 storageUg_fail_popup(ad);
52         }
53         ad->sd_request = STORAGEUG_SD_REQ_NONE;
54 }
55
56 static void storageUg_SD_unmount(SettingStorageUG *ad, storageUg_mmc_cb cb)
57 {
58         int ret;
59
60         ret_if(ad == NULL);
61
62         ad->popup = setting_create_popup_with_progressbar(ad, ad->win, PROGRESSBAR_STYLE,
63                         NULL, STORAGEUG_STR_UNMOUNTING_SD, storageUg_popup_del, 0, TRUE, TRUE);
64
65         ad->mmc_content.mmc_cb = cb;
66         ad->mmc_content.user_data = ad;
67         ret = deviced_request_unmount_mmc(&ad->mmc_content, MNT_FORCE);
68         if (ret == -1)
69         {
70                 SETTING_TRACE_ERROR("deviced_request_unmount_mmc() Fail");
71                 storageUg_fail_popup(ad);
72         }
73         ad->sd_request = STORAGEUG_SD_REQ_UNMOUNT;
74 }
75
76 static void storageUg_SD_unmount_resp(void *data, Evas_Object *obj,
77                 void *event_info)
78 {
79         int response_type;
80         SettingStorageUG *ad = data;
81
82         ret_if(data == NULL);
83
84         response_type = btn_type(obj);
85         if (ad->popup)
86         {
87                 evas_object_del(ad->popup);
88                 ad->popup = NULL;
89         }
90
91         if (POPUP_RESPONSE_OK == response_type)
92                 storageUg_SD_unmount(ad, storageUg_SD_finish_progress);
93 }
94
95 static void storageUg_SD_handle_mount_unmount(void *data)
96 {
97         SettingStorageUG *ad = data;
98
99         ret_if(data == NULL);
100
101         SETTING_TRACE("mmc_status = %d", ad->mmc_status);
102
103         if (ad->popup) {
104                 evas_object_del(ad->popup);
105                 ad->popup = NULL;
106         }
107
108         if (VCONFKEY_SYSMAN_MMC_MOUNTED == ad->mmc_status)
109         {
110                 ad->popup = setting_create_popup_with_btn(ad, ad->win, NULL,
111                                 STORAGEUG_STR_SD_UNMOUNT_POPUP_MSG, storageUg_SD_unmount_resp, 0,
112                                 2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
113         }
114         else
115         {
116                 int ret;
117
118                 ad->popup = setting_create_popup_with_progressbar(ad, ad->win,
119                                 PROGRESSBAR_STYLE, NULL, NULL, storageUg_popup_del, 0, TRUE, TRUE);
120
121                 ad->mmc_content.mmc_cb = storageUg_SD_finish_progress;
122                 ad->mmc_content.user_data = ad;
123                 ret = deviced_request_mount_mmc(&ad->mmc_content);
124                 /*  if fail, popup failed info */
125                 if (ret == -1)
126                 {
127                         SETTING_TRACE_ERROR("deviced_request_mount_mmc() Fail");
128                         storageUg_fail_popup(ad);
129                         return;
130                 }
131                 ad->sd_request = STORAGEUG_SD_REQ_MOUNT;
132         }
133 }
134
135 static void storageUg_SD_finish_format(int result, void* data)
136 {
137         SettingStorageUG *ad = data;
138
139         ret_if(data == NULL);
140
141         if (ad->popup) {
142                 evas_object_del(ad->popup);
143                 ad->popup = NULL;
144         }
145
146         ad->sd_request = STORAGEUG_SD_REQ_NONE;
147
148         if (result < 0)
149         {
150                 SETTING_TRACE_ERROR("result(%d) Invalid", result);
151                 storageUg_fail_popup(ad);
152                 return;
153         }
154 }
155
156 static void storageUg_SD_format(int result, void* data)
157 {
158         int ret;
159         SettingStorageUG *ad = data;
160
161         ret_if(data == NULL);
162
163         if (ad->popup)
164         {
165                 evas_object_del(ad->popup);
166                 ad->popup = NULL;
167         }
168
169         if (result < 0)
170         {
171                 SETTING_TRACE_ERROR("result(%d) Invalid", result);
172                 storageUg_fail_popup(ad);
173                 ad->sd_request = STORAGEUG_SD_REQ_NONE;
174                 return;
175         }
176
177         ad->popup = setting_create_popup_with_progressbar(ad, ad->win, PROGRESSBAR_STYLE,
178                         NULL, STORAGEUG_STR_FORMATTING_SD, NULL, 0, TRUE, TRUE);
179
180         ad->mmc_content.mmc_cb = storageUg_SD_finish_format;
181         ad->mmc_content.user_data = ad;
182         ret = deviced_request_format_mmc(&ad->mmc_content);
183         if (ret == -1)
184         {
185                 SETTING_TRACE_ERROR("fail to call deviced_request_format_mmc");
186                 storageUg_fail_popup(ad);
187         }
188         ad->sd_request = STORAGEUG_SD_REQ_FORMAT;
189 }
190
191
192 static void storageUg_SD_format_se_confirm_resp(void *data, Evas_Object *obj,
193                 void *event_info)
194 {
195         int response_type;
196         SettingStorageUG *ad = data;
197
198         ret_if(data == NULL);
199
200         response_type = btn_type(obj);
201         if (ad->popup)
202         {
203                 evas_object_del(ad->popup);
204                 ad->popup = NULL;
205         }
206
207         if (POPUP_RESPONSE_OK == response_type)
208                 storageUg_SD_unmount(ad, storageUg_SD_format);
209 }
210
211
212 static void storageUg_SD_format_se_confirm(SettingStorageUG *ad)
213 {
214         if (ad->popup)
215                 evas_object_del(ad->popup);
216
217         ad->popup = setting_create_popup_with_btn(ad, ad->win, NULL,
218                         STORAGEUG_STR_FORMAT_SECOND_Q, storageUg_SD_format_se_confirm_resp, 0,
219                         2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
220
221 }
222
223 static void storageUg_passwd_ug_result_cb(ui_gadget_h ug, app_control_h service,
224                 void *priv)
225 {
226         char *result = NULL;
227         SettingStorageUG *ad = priv;
228
229         ret_if(NULL == priv);
230
231         app_control_get_extra_data(service, "result", &result);
232         if(safeStrCmp(result, "SETTING_PW_TYPE_ENTER_LOCK_TYPE") == 0)
233                 storageUg_SD_format_se_confirm(ad);
234         else
235                 storageUg_fail_popup(ad);
236
237         FREE(result);
238
239         elm_object_tree_focus_allow_set(ad->lo_main, EINA_TRUE);
240         setting_ug_destroy(ug);
241 }
242
243 static inline void storageUg_passwd_ug(SettingStorageUG *ad)
244 {
245         app_control_h svc;
246         ui_gadget_h ug;
247         struct ug_cbs cbs;
248
249         ret_if(NULL == ad);
250
251         if (app_control_create(&svc))
252                 return;
253
254         app_control_add_extra_data(svc, "viewtype", "SETTING_PW_TYPE_ENTER_LOCK_TYPE");
255
256         memset(&cbs, 0, sizeof(struct ug_cbs));
257         cbs.layout_cb = storageUg_ug_layout_cb;
258         cbs.result_cb = storageUg_passwd_ug_result_cb;
259         cbs.destroy_cb = storageUg_ug_destroy_cb;
260         cbs.priv = (void *)ad;
261
262         elm_object_tree_focus_allow_set(ad->lo_main, EINA_FALSE);
263         ug = setting_ug_create(ad->ug, "setting-password-efl", UG_MODE_FULLVIEW, svc, &cbs);
264         warn_if(NULL == ug, "setting_ug_create() Fail");
265
266         app_control_destroy(svc);
267 }
268
269 static inline int storageUg_checkencrypt()
270 {
271         int sde_status = 1;
272 #if SUPPORT_ENCRYPTION
273         if(ode_init() == 0)
274         {
275                 sde_status = sde_checkencrypt();
276                 ode_deinit();
277         }
278 #endif
279         return !sde_status;
280 }
281
282 static inline void storageUg_SD_prepare_format(SettingStorageUG *ad)
283 {
284         int is_encrypt;
285
286         is_encrypt = storageUg_checkencrypt();
287         if (is_encrypt)
288                 storageUg_passwd_ug(ad);
289         else
290                 storageUg_SD_format_se_confirm(ad);
291 }
292
293 static void storageUg_SD_format_first_confirm_resp(void *data,
294                 Evas_Object *obj, void *event_info)
295 {
296         int response_type;
297         SettingStorageUG *ad = data;
298
299         ret_if(data == NULL);
300
301         response_type = btn_type(obj);
302         if (ad->popup)
303         {
304                 evas_object_del(ad->popup);
305                 ad->popup = NULL;
306         }
307
308         if (POPUP_RESPONSE_OK == response_type)
309         {
310                 storageUg_SD_prepare_format(ad);
311         }
312
313 }
314
315
316 static inline void storageUg_SD_handle_format(SettingStorageUG *ad)
317 {
318         char *popup_msg;
319
320         if (ad->popup) {
321                 evas_object_del(ad->popup);
322                 ad->popup = NULL;
323         }
324
325         popup_msg = STORAGEUG_STR_FORMAT_USE_MSG;
326
327         ad->popup = setting_create_popup_with_btn(ad, ad->win, NULL, popup_msg,
328                         storageUg_SD_format_first_confirm_resp, 0,
329                         2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
330 }
331
332 static void storageUg_SD_sel(void *data, Evas_Object *obj, void *event_info)
333 {
334         SettingStorageUG *ad = data;
335         Elm_Object_Item *item = event_info;
336
337         ret_if(NULL == data);
338         ret_if(NULL == event_info);
339
340         elm_genlist_item_selected_set(item, 0);
341
342         if (item == ad->sd_mount->item)
343                 storageUg_SD_handle_mount_unmount(ad);
344         else if (item == ad->sd_format->item)
345                 storageUg_SD_handle_format(ad);
346         else
347                 SETTING_TRACE_ERROR("item(%p) Invalid", item);
348 }
349
350 static Setting_GenGroupItem_Data* storageUg_SD_gl_insert_after(
351                                         Evas_Object *genlist,
352                                         const Elm_Genlist_Item_Class *itc,
353                                         Elm_Object_Item *before,
354                                         setting_call_back_func gl_sel,
355                                         void *sel_data,
356                                         const char *keyStr,
357                                         char *sub_desc,
358                                         setting_group_style group_style,
359                                         SettingStorageUG *ad)
360 {
361         Setting_GenGroupItem_Data *it_data = calloc(1, sizeof(Setting_GenGroupItem_Data));
362         retvm_if(NULL == it_data, NULL, "calloc failed");
363
364         it_data->keyStr = (char *)g_strdup(keyStr);
365         it_data->sub_desc = (char *)g_strdup(sub_desc);
366         it_data->swallow_type = SWALLOW_Type_INVALID;
367         it_data->group_style = group_style;
368         it_data->userdata = ad;
369
370         it_data->item = elm_genlist_item_insert_after(genlist, itc, it_data, NULL, before,
371                                         ELM_GENLIST_ITEM_NONE, gl_sel, sel_data);
372
373         return it_data;
374 }
375
376 static inline void storageUg_SD_info_removed(SettingStorageUG *ad)
377 {
378
379         ad->sd_mount = storageUg_SD_gl_insert_after(ad->gl_main, &itc_2text_2,
380                         ad->sd_card->item, NULL, NULL, STORAGEUG_STR_MOUNT_SD, STORAGEUG_STR_INSERT,
381                         SETTING_GROUP_STYLE_NONE, NULL);
382         if (ad->sd_mount)
383         {
384                 elm_object_item_disabled_set(ad->sd_mount->item, EINA_TRUE);
385         }
386         else
387         {
388                 SETTING_TRACE_ERROR("ad->sd_mount is NULL");
389         }
390 }
391
392 static inline void storageUg_SD_info_inserted_not_mounted(SettingStorageUG *ad)
393 {
394         ad->sd_mount = storageUg_SD_gl_insert_after(ad->gl_main, &itc_1text,
395                         ad->sd_card->item, storageUg_SD_sel, ad, STORAGEUG_STR_MOUNT_SD, NULL,
396                         SETTING_GROUP_STYLE_TOP, ad);
397         warn_if(NULL == ad->sd_mount, "ad->sd_mount is NULL");
398
399         ad->sd_format = storageUg_SD_gl_insert_after(ad->gl_main, &itc_1text,
400                         ad->sd_mount->item, storageUg_SD_sel, ad, STORAGEUG_STR_FORMAT_SD, NULL,
401                         SETTING_GROUP_STYLE_BOTTOM, ad);
402         if (ad->sd_format)
403         {
404                 int status = -1;
405
406                 vconf_get_int(VCONFKEY_SYSMAN_MMC_MOUNT, &status);
407                 if (VCONFKEY_SYSMAN_MMC_MOUNT_COMPLETED == status
408                                 || VCONFKEY_SYSMAN_MMC_MOUNT_ALREADY == status)
409                 {
410                         setting_disable_genlist_item(ad->sd_format->item);
411                 }
412         }
413         else
414         {
415                 SETTING_TRACE_ERROR("ad->sd_format is NULL");
416         }
417 }
418
419 static inline void storageUg_SD_info_mounted(SettingStorageUG *ad)
420 {
421         double total = 0.0;
422         double avail = 0.0;
423         char total_str[STORAGEUG_MAX_STR_LEN] = {0};
424         char avail_str[STORAGEUG_MAX_STR_LEN] = {0};
425         const char *MMC_path = "/opt/storage/sdcard";
426
427         storageUg_get_external_storage_status(MMC_path, &total, &avail);
428         storageUg_size_to_str(total, total_str, sizeof(total_str));
429         storageUg_size_to_str(avail, avail_str, sizeof(avail_str));
430
431         ad->sd_total = storageUg_SD_gl_insert_after(ad->gl_main, &itc_2text_2,
432                         ad->sd_card->item, NULL, ad, STORAGEUG_STR_TOTAL, total_str,
433                         SETTING_GROUP_STYLE_TOP, ad);
434         if (ad->sd_total) {
435                 elm_genlist_item_select_mode_set(ad->sd_total->item,
436                                 ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
437                 ad->sd_avail = storageUg_SD_gl_insert_after(ad->gl_main, &itc_2text_2,
438                                 ad->sd_total->item, NULL, ad, STORAGEUG_STR_AVAIL_SPACE, avail_str,
439                                 SETTING_GROUP_STYLE_CENTER, ad);
440                 if (ad->sd_avail)
441                 {
442                         elm_genlist_item_select_mode_set(ad->sd_avail->item,
443                                         ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
444                 }
445                 else
446                 {
447                         SETTING_TRACE_ERROR("ad->sd_avail is NULL");
448                 }
449                 ad->sd_mount = storageUg_SD_gl_insert_after(ad->gl_main, &itc_1text,
450                                 ad->sd_avail->item, storageUg_SD_sel, ad, STORAGEUG_STR_UNMOUNT_SD, NULL,
451                                 SETTING_GROUP_STYLE_CENTER, ad);
452                 warn_if(NULL == ad->sd_mount, "ad->sd_mount is NULL");
453
454                 ad->sd_format = storageUg_SD_gl_insert_after(ad->gl_main, &itc_1text,
455                                 ad->sd_mount->item, storageUg_SD_sel, ad, STORAGEUG_STR_FORMAT_SD, NULL,
456                                 SETTING_GROUP_STYLE_BOTTOM, ad);
457                 warn_if(NULL == ad->sd_format, "ad->sd_format is NULL");
458
459         }
460         else
461         {
462                 SETTING_TRACE_ERROR("ad->sd_total is NULL");
463         }
464 }
465
466 static inline void storageUg_SD_remove_info(SettingStorageUG *ad)
467 {
468         if (ad->sd_mount)
469         {
470                 elm_object_item_del(ad->sd_mount->item);
471                 ad->sd_mount = NULL;
472         }
473         if (ad->sd_total)
474         {
475                 elm_object_item_del(ad->sd_total->item);
476                 ad->sd_total = NULL;
477         }
478         if (ad->sd_avail)
479         {
480                 elm_object_item_del(ad->sd_avail->item);
481                 ad->sd_avail = NULL;
482         }
483         if (ad->sd_format)
484         {
485                 elm_object_item_del(ad->sd_format->item);
486                 ad->sd_format = NULL;
487         }
488 }
489
490 void storageUg_main_append_SD_info(SettingStorageUG *ad)
491 {
492         char *str;
493         int mmc_mode;
494
495         ret_if(ad == NULL);
496
497         mmc_mode = ad->mmc_status;
498
499         SETTING_TRACE_DEBUG("mmc_mode: %d", mmc_mode);
500
501         storageUg_SD_remove_info(ad);
502
503         if (-1 == mmc_mode)
504                 mmc_mode = VCONFKEY_SYSMAN_MMC_REMOVED;
505
506         if (VCONFKEY_SYSMAN_MMC_REMOVED == mmc_mode)
507                 storageUg_SD_info_removed(ad);
508         else if (VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED == mmc_mode)
509                 storageUg_SD_info_inserted_not_mounted(ad);
510         else if (VCONFKEY_SYSMAN_MMC_MOUNTED == mmc_mode)
511                 storageUg_SD_info_mounted(ad);
512         else
513                 SETTING_TRACE_ERROR("mmc_mode(%d) Invalid", mmc_mode);
514
515         str = vconf_get_str(storageUg_ENCRYPT_stat);
516         warn_if(NULL == str, "vconf_get_str(%s) Fail", storageUg_ENCRYPT_stat);
517         if (!safeStrCmp(str, "encryption_start") || !safeStrCmp(str, "decryption_start"))
518         {
519                 if (ad->sd_mount) setting_disable_genlist_item(ad->sd_mount->item);
520                 if (ad->sd_format) setting_disable_genlist_item(ad->sd_format->item);
521         }
522 }
523
524 void storageUg_SD_change_cb(keynode_t *node, void *user_data)
525 {
526         SettingStorageUG *ad = user_data;
527
528         ret_if(NULL == user_data);
529
530         ad->mmc_status = vconf_keynode_get_int(node);
531
532         storageUg_main_append_SD_info(ad);
533 }