tizen 2.4 release
[apps/home/settings.git] / setting-storage / src / setting-storage-USB.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 <dd-usbhost.h>
20
21 #include "setting-storage-utils.h"
22 #include "setting-storage-USB.h"
23
24 enum {
25     STORAGEUG_USB_REQ_NONE,
26     STORAGEUG_USB_REQ_MOUNT,
27     STORAGEUG_USB_REQ_UNMOUNT,
28     STORAGEUG_USB_REQ_UNMOUNT_BEFORE_FORMAT,
29     STORAGEUG_USB_REQ_FORMAT
30 };
31
32 static inline void storageUg_USB_unmount(SettingStorageUG *ad)
33 {
34         int ret;
35         ret = unmount_usb_storage(ad->usb_otg_path);
36         if (ret < 0) {
37                 SETTING_TRACE_ERROR("unmount_usb_storage(%s) Fail(%d)", ad->usb_otg_path, ret);
38                 storageUg_fail_popup(ad);
39                 ad->usb_request = STORAGEUG_USB_REQ_NONE;
40         } else {
41                 ad->popup = setting_create_popup_with_progressbar(ad, ad->win, PROGRESSBAR_STYLE,
42                                                                   NULL, STORAGEUG_STR_UNMOUNTING, storageUg_popup_del, 0, TRUE, TRUE, 0);
43         }
44 }
45
46 static inline void storageUg_USB_mount(SettingStorageUG *ad)
47 {
48         int ret;
49
50         ret = mount_usb_storage(ad->usb_otg_path);
51         if (ret < 0) {
52                 SETTING_TRACE_ERROR("mount_usb_storage(%s) Fail(%d)", ad->usb_otg_path, ret);
53                 storageUg_fail_popup(ad);
54                 ad->usb_request = STORAGEUG_USB_REQ_NONE;
55         } else {
56                 ad->popup = setting_create_popup_with_progressbar(ad, ad->win, PROGRESSBAR_STYLE,
57                                                                   NULL, NULL, storageUg_popup_del, 2, TRUE, TRUE, 0);
58                 ad->usb_request = STORAGEUG_USB_REQ_MOUNT;
59         }
60 }
61
62 static inline int storageUg_USB_format(SettingStorageUG *ad)
63 {
64         int ret;
65
66         ret = format_usb_storage(ad->usb_otg_path);
67         if (ret < 0) {
68                 SETTING_TRACE_ERROR("format_usb_storage(%s) Fail(%d)", ad->usb_otg_path, ret);
69                 storageUg_fail_popup(ad);
70                 return SETTING_RETURN_FAIL;
71         } else {
72                 ad->popup = setting_create_popup_with_progressbar(ad, ad->win, PROGRESSBAR_STYLE,
73                                                                   NULL, STORAGEUG_STR_FORMATTING, storageUg_popup_del, 0, TRUE, TRUE, 0);
74         }
75         return SETTING_RETURN_SUCCESS;
76 }
77
78 static void storageUg_USB_unmount_resp(void *data, Evas_Object *obj,
79                                        void *event_info)
80 {
81         int response_type;
82         SettingStorageUG *ad = data;
83
84         ret_if(data == NULL);
85
86         response_type = btn_type(obj);
87         if (ad->popup) {
88                 evas_object_del(ad->popup);
89                 ad->popup = NULL;
90         }
91
92         if (POPUP_RESPONSE_OK == response_type) {
93                 ad->usb_request = STORAGEUG_USB_REQ_UNMOUNT;
94                 storageUg_USB_unmount(ad);
95         }
96 }
97
98 static void storageUg_USB_format_se_confirm(void *data, Evas_Object *obj,
99                                             void *event_info)
100 {
101         int response_type;
102         SettingStorageUG *ad = data;
103
104         ret_if(data == NULL);
105
106         response_type = btn_type(obj);
107         if (ad->popup) {
108                 evas_object_del(ad->popup);
109                 ad->popup = NULL;
110         }
111
112         if (POPUP_RESPONSE_OK == response_type) {
113                 ad->usb_request = STORAGEUG_USB_REQ_UNMOUNT_BEFORE_FORMAT;
114                 storageUg_USB_unmount(ad);
115         }
116 }
117
118 static void storageUg_USB_format_first_confirm(void *data,
119                                                Evas_Object *obj, void *event_info)
120 {
121         int response_type;
122         SettingStorageUG *ad = data;
123
124         ret_if(data == NULL);
125
126         response_type = btn_type(obj);
127         if (ad->popup) {
128                 evas_object_del(ad->popup);
129                 ad->popup = NULL;
130         }
131
132         if (POPUP_RESPONSE_OK == response_type) {
133                 if (ad->popup)
134                         evas_object_del(ad->popup);
135
136                 ad->popup = setting_create_popup(ad, ad->win, NULL,
137                                                                                  STORAGEUG_STR_USB_FORMAT_SECOND_Q, storageUg_USB_format_se_confirm,
138                                                                                  0, FALSE, FALSE,
139                                                                                  2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
140         }
141
142 }
143
144 static void storageUg_main_USB_sel(void *data, Evas_Object *obj, void *event_info)
145 {
146         SettingStorageUG *ad = data;
147         Elm_Object_Item *item = event_info;
148         Setting_GenGroupItem_Data *list_item;
149
150         ret_if(NULL == data);
151         ret_if(NULL == event_info);
152
153         elm_genlist_item_selected_set(item, 0);
154         list_item = elm_object_item_data_get(item);
155         setting_retm_if(NULL == list_item, "list_item is NULL");
156
157         SETTING_TRACE("clicking item[%s]", list_item->keyStr);
158
159         if (ad->popup) {
160                 evas_object_del(ad->popup);
161                 ad->popup = NULL;
162         }
163
164         if (list_item == ad->usb_mount) {
165                 storageUg_USB_mount(ad);
166         } else if (list_item == ad->usb_unmount) {
167                 ad->popup = setting_create_popup(ad, ad->win, NULL,
168                                                                                  STORAGEUG_STR_USB_UNMOUNT_POPUP_MSG, storageUg_USB_unmount_resp,
169                                                                                  0, FALSE, FALSE,
170                                                                                  2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
171
172         } else if (list_item == ad->usb_format) {
173                 ad->popup = setting_create_popup(ad, ad->win, NULL,
174                                                                                  STORAGEUG_STR_USB_FORMAT_Q, storageUg_USB_format_first_confirm,
175                                                                                  0, FALSE, FALSE,
176                                                                                  2, STORAGEUG_STR_OK, STORAGEUG_STR_CANCEL);
177         }
178 }
179
180 static inline void storageUg_USB_remove_info(SettingStorageUG *ad)
181 {
182         if (ad->usb_card) {
183                 elm_object_item_del(ad->usb_card->item);
184                 ad->usb_card = NULL;
185         }
186         if (ad->usb_mount) {
187                 elm_object_item_del(ad->usb_mount->item);
188                 ad->usb_mount = NULL;
189         }
190         if (ad->usb_total) {
191                 elm_object_item_del(ad->usb_total->item);
192                 ad->usb_total = NULL;
193         }
194         if (ad->usb_available) {
195                 elm_object_item_del(ad->usb_available->item);
196                 ad->usb_available = NULL;
197         }
198         if (ad->usb_unmount) {
199                 elm_object_item_del(ad->usb_unmount->item);
200                 ad->usb_unmount = NULL;
201         }
202         if (ad->usb_format) {
203                 elm_object_item_del(ad->usb_format->item);
204                 ad->usb_format = NULL;
205         }
206         if (ad->usb_sep_item) {
207                 elm_object_item_del(ad->usb_sep_item);
208                 ad->usb_sep_item = NULL;
209         }
210 }
211
212 static inline void storageUg_USB_append_mounted_info(SettingStorageUG *ad)
213 {
214         double total = 0.0;
215         double avail = 0.0;
216         char total_str[STORAGEUG_MAX_STR_LEN] = {0};
217         char avail_str[STORAGEUG_MAX_STR_LEN] = {0};
218
219         storageUg_get_external_storage_status(ad->usb_otg_path, &total, &avail);
220         storageUg_size_to_str(total, total_str, sizeof(total_str));
221         storageUg_size_to_str(avail, avail_str, sizeof(avail_str));
222
223         /* Total space */
224         ad->usb_total = setting_create_Gendial_field_def(ad->gl_main, &(ad->itc_2text_2),
225                                                          NULL, ad, SWALLOW_Type_INVALID, NULL, NULL, 0, STORAGEUG_STR_TOTAL,
226                                                          total_str, NULL);
227         if (ad->usb_total) {
228                 ad->usb_total->userdata = ad;
229                 elm_genlist_item_select_mode_set(ad->usb_total->item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
230         } else {
231                 SETTING_TRACE_ERROR("ad->usb_total is NULL");
232         }
233
234         /* Avaliable */
235         ad->usb_available = setting_create_Gendial_field_def(ad->gl_main, &(ad->itc_2text_2),
236                                                              NULL, ad, SWALLOW_Type_INVALID, NULL, NULL, 0, STORAGEUG_STR_AVAIL_SPACE,
237                                                              avail_str, NULL);
238         if (ad->usb_available) {
239                 ad->usb_available->userdata = ad;
240                 elm_genlist_item_select_mode_set(ad->usb_available->item,
241                                                  ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
242         } else {
243                 SETTING_TRACE_ERROR("ad->usb_available is NULL");
244         }
245
246         /* Usb Unmount*/
247         ad->usb_unmount = setting_create_Gendial_field_def(ad->gl_main, &(ad->itc_1text),
248                                                            storageUg_main_USB_sel, ad, SWALLOW_Type_INVALID, NULL, NULL, 0,
249                                                            STORAGEUG_STR_UNMOUNT_USB, NULL, NULL);
250         if (ad->usb_unmount) {
251                 ad->usb_unmount->userdata = ad;
252         } else {
253                 SETTING_TRACE_ERROR("ad->usb_unmount is NULL");
254         }
255
256         /* Usb Format*/
257         ad->usb_format = setting_create_Gendial_field_def(ad->gl_main, &(ad->itc_1text),
258                                                           storageUg_main_USB_sel, ad, SWALLOW_Type_INVALID, NULL, NULL, 0,
259                                                           STORAGEUG_STR_FORMAT_USB, NULL, NULL);
260         if (ad->usb_format) {
261                 ad->usb_format->userdata = ad;
262         } else {
263                 SETTING_TRACE_ERROR("ad->usb_format is NULL");
264         }
265 }
266
267 static inline void storageUg_main_append_USB_info(SettingStorageUG *ad)
268 {
269         ret_if(NULL == ad);
270
271         if (ad->usb_card)
272                 storageUg_USB_remove_info(ad);
273
274         if (ad->usb_otg_status == SETTING_STORAGE_USB_OTG_REMOVE)
275                 return;
276
277         /* USB OTG storage */
278         ad->usb_card = setting_create_Gendial_field_titleItem(ad->gl_main,
279                                                               &(ad->itc_group_item), STORAGEUG_STR_USB, NULL);
280
281         if (SETTING_STORAGE_USB_OTG_MOUNT == ad->usb_otg_status) {
282                 storageUg_USB_append_mounted_info(ad);
283         } else {
284                 /* Usb Mount*/
285                 ad->usb_mount = setting_create_Gendial_field_def(ad->gl_main, &(ad->itc_1text),
286                                                                  storageUg_main_USB_sel, ad, SWALLOW_Type_INVALID, NULL, NULL, 0,
287                                                                  STORAGEUG_STR_MOUNT_USB, NULL, NULL);
288                 if (ad->usb_mount) {
289                         ad->usb_mount->userdata = ad;
290                 } else {
291                         SETTING_TRACE_ERROR("ad->usb_mount is NULL");
292                 }
293         }
294
295         ad->usb_sep_item = storageUg_append_separator(ad->gl_main, ad);
296 }
297
298 static void storageUg_USB_cb(char *type, char *path, int mount, void *data)
299 {
300         int ret;
301         SettingStorageUG *ad = data;
302
303         ret_if(NULL == type);
304         ret_if(NULL == path);
305         ret_if(NULL == data);
306
307         SETTING_TRACE_DEBUG("path:%s type:%s", path, type);
308
309         FREE(ad->usb_otg_path);
310         ad->usb_otg_path = SAFE_STRDUP(path);
311
312         if (ad->popup) {
313                 evas_object_del(ad->popup);
314                 ad->popup = NULL;
315         }
316
317         if (!strcmp(type, "storage_remove")) {
318                 ad->usb_otg_status = SETTING_STORAGE_USB_OTG_REMOVE;
319         } else if (!strcmp(type, "storage_mount")) {
320                 ad->usb_otg_status = SETTING_STORAGE_USB_OTG_MOUNT;
321                 if (STORAGEUG_USB_REQ_UNMOUNT == ad->usb_request) {
322                         SETTING_TRACE_ERROR("unmount_usb_storage() Fail");
323                         storageUg_fail_popup(ad);
324                 }
325         } else if (!strcmp(type, "storage_unmount")) {
326                 ad->usb_otg_status = SETTING_STORAGE_USB_OTG_UNMOUNT;
327                 if (STORAGEUG_USB_REQ_UNMOUNT_BEFORE_FORMAT == ad->usb_request) {
328                         ret = storageUg_USB_format(ad);
329                         if (SETTING_RETURN_SUCCESS == ret) {
330                                 ad->usb_request = STORAGEUG_USB_REQ_FORMAT;
331                         } else {
332                                 ad->usb_request = STORAGEUG_USB_REQ_NONE;
333                                 storageUg_USB_mount(ad);
334                         }
335                         return;
336                 } else if (STORAGEUG_USB_REQ_FORMAT == ad->usb_request) {
337                         SETTING_TRACE_ERROR("format_usb_storage() Fail");
338                         storageUg_fail_popup(ad);
339                 } else if (STORAGEUG_USB_REQ_MOUNT == ad->usb_request) {
340                         SETTING_TRACE_ERROR("mount_usb_storage() Fail");
341                         storageUg_fail_popup(ad);
342                 }
343         } else if (!strcmp(type, "storage_updated")) {
344                 if (mount == 0)
345                         ad->usb_otg_status = SETTING_STORAGE_USB_OTG_UNMOUNT;
346                 else
347                         ad->usb_otg_status = SETTING_STORAGE_USB_OTG_MOUNT;
348         }
349         ad->usb_request = STORAGEUG_USB_REQ_NONE;
350         storageUg_main_append_USB_info(ad);
351 }
352
353 void storageUg_init_USB(SettingStorageUG *ad)
354 {
355         int ret;
356
357         ret = init_usbhost_signal();
358         retm_if(ret < 0, "init_usbhost_signal() Fail(%d)", ret);
359
360         ret = register_usb_storage_change_handler(storageUg_USB_cb, ad);
361         if (ret < 0) {
362                 SETTING_TRACE_ERROR("register_usb_storage_change_handler() Fail(%d)", ret);
363                 deinit_usbhost_signal();
364                 return;
365         }
366
367         ret = request_usb_storage_info();
368         warn_if(ret < 0, "request_usb_storage_info() Fail(%d)", ret);
369 }
370
371 void storageUg_deinit_USB()
372 {
373         deinit_usbhost_signal();
374 }