Initialize Tizen 2.3
[apps/home/settings.git] / setting-storage / src / setting-storage-default-storage.c
1 /*
2  * setting
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Flora License, Version 1.1 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an AS IS BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <setting-storage-default-storage.h>
18
19 static int setting_storage_default_storage_create(void *cb);
20 static int setting_storage_default_storage_destroy(void *cb);
21 static int setting_storage_default_storage_update(void *cb);
22 static int setting_storage_default_storage_cleanup(void *cb);
23 static char* __setting_storage_default_storage_desc_get(setting_int_slp_list slp_key);
24
25 setting_view setting_view_storage_default_storage = {
26         .create = setting_storage_default_storage_create,
27         .destroy = setting_storage_default_storage_destroy,
28         .update = setting_storage_default_storage_update,
29         .cleanup = setting_storage_default_storage_cleanup,
30 };
31
32 static void __setting_storage_default_storage_cancel_cb(void *data, Evas_Object *obj, void *event_info);
33 static void __setting_storage_default_storage_vconf_change_cb(keynode_t *key, void *data);
34 static void __setting_storage_default_storage_update_item_status(void *data, Eina_Bool disable_mmc_flag);
35 static void __setting_storage_default_storage_update_vconf(void *data);
36
37 /* ***************************************************
38  *
39  *basic func
40  *
41  ***************************************************/
42 static void __setting_storage_default_storage_exp_cb(void *data, Evas_Object *obj, void *event_info)
43 {
44         SETTING_TRACE_BEGIN;
45         setting_retm_if(NULL == data, "data is NULL");
46         setting_retm_if(NULL == event_info, "event_info is NULL");
47         SettingStorageUG *ad = (SettingStorageUG *) data;
48         Elm_Object_Item *parentItem = event_info;       /* parent item */
49
50         Setting_GenGroupItem_Data *data_parentItem = elm_object_item_data_get(parentItem);      /* parent data */
51         Evas_Object *scroller = elm_object_item_widget_get(parentItem);
52
53         Evas_Object *rgd = NULL;
54         if (
55 #if DISABLED_CODE
56                 data_parentItem == ad->data_wap ||
57 #endif
58                 data_parentItem == ad->data_bt
59             || data_parentItem == ad->data_cmr
60             || data_parentItem == ad->data_vrec
61 #if DISABLED_CODE
62             || data_parentItem == ad->data_rdo
63             || data_parentItem == ad->data_dvb_h
64 #endif
65                 ) {
66                 SETTING_TRACE("start drawing exp menu.");
67
68                 rgd = elm_radio_add(scroller);
69                 elm_radio_value_set(rgd, -1);
70                 setting_create_Gendial_exp_sub_field(scroller,
71                                                      &(ad->itc_1icon_1text_sub),
72                                                      NULL, NULL, parentItem,
73                                                      SWALLOW_Type_1RADIO, rgd,
74                                                      SETTING_DEF_MEMORY_PHONE,
75                                                      "IDS_COM_BODY_PHONE",
76                                                      NULL);
77
78                 Setting_GenGroupItem_Data *data_memory_card = NULL;
79                 data_memory_card =
80                         setting_create_Gendial_exp_sub_field(scroller,
81                                                      &(ad->itc_1icon_1text_sub),
82                                                      NULL, NULL, parentItem,
83                                                      SWALLOW_Type_1RADIO, rgd,
84                                                      SETTING_DEF_MEMORY_MMC,
85                                                      "IDS_COM_BODY_MEMORY_CARD",
86                                                      NULL);
87                 setting_retm_if(!data_memory_card, "data_memory_card is NULL");
88
89                 int mmc_mode = VCONFKEY_SYSMAN_MMC_REMOVED;
90                 int ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mode);
91                 setting_retm_if(ret != 0, "Get vconf failed");
92
93                 /* if mmc card is not mounted, set item disable */
94                 if (mmc_mode == VCONFKEY_SYSMAN_MMC_REMOVED ||
95                         mmc_mode == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
96                         setting_disable_genlist_item(data_memory_card->item);
97                 }
98
99                 setting_update_chk_status(rgd, data_parentItem->int_slp_setting_binded);
100         }
101 }
102
103 static int setting_storage_default_storage_create(void *cb)
104 {
105         SETTING_TRACE_BEGIN;
106         setting_retvm_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "cb is NULL");
107         SettingStorageUG *ad = (SettingStorageUG *)cb;
108
109         Evas_Object *scroller = NULL;
110         Elm_Object_Item *item = NULL;
111         setting_push_layout_navi_bar_genlist(ad->win_main_layout, ad->win_get,
112                                              _(SETTING_STORAGE_DEFAULT_STORAGE_STR),
113                                              _("IDS_COM_BODY_BACK"), NULL,
114                                              __setting_storage_default_storage_cancel_cb,
115                                              NULL, ad, &scroller, ad->navibar);
116
117         setting_enable_expandable_genlist(scroller, ad,
118                                           __setting_storage_default_storage_exp_cb, NULL);
119
120         item = elm_genlist_item_append(scroller, &(ad->itc_seperator), NULL, NULL,
121                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
122         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
123
124 #if DISABLED_CODE
125         /* 1 wap downloads */
126         ad->data_wap = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
127                                                     NULL, NULL, SWALLOW_Type_INVALID,
128                                                     "IDS_ST_BODY_WAP_DOWNLOADS",
129                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_WAP));
130         if (ad->data_wap) {
131                 ad->data_wap->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_WAP;
132         } else {
133                 SETTING_TRACE_ERROR("ad->data_wap is NULL");
134         }
135 #endif
136         /* 2 bluetooth */
137         ad->data_bt = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
138                                                     NULL, NULL, SWALLOW_Type_INVALID,
139                                                     "IDS_COM_BODY_BLUETOOTH",
140                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_BLUETOOTH));
141         if (ad->data_bt) {
142                 ad->data_bt->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_BLUETOOTH;
143         } else {
144                 SETTING_TRACE_ERROR("ad->data_bt is NULL");
145         }
146
147         /* 3 camera */
148         ad->data_cmr = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
149                                                     NULL, NULL, SWALLOW_Type_INVALID,
150                                                     "IDS_COM_BODY_CAMERA",
151                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_CAMERA));
152         if (ad->data_cmr) {
153                 ad->data_cmr->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_CAMERA;
154         } else {
155                 SETTING_TRACE_ERROR("ad->data_cmr is NULL");
156         }
157
158         /* 4 video recorder */
159         ad->data_vrec = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
160                                                     NULL, NULL, SWALLOW_Type_INVALID,
161                                                     "IDS_COM_BODY_VOICE_RECORDER",
162                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_VOICE_RECORDER));
163         if (ad->data_vrec) {
164                 ad->data_vrec->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_VOICE_RECORDER;
165         } else {
166                 SETTING_TRACE_ERROR("ad->data_vrec is NULL");
167         }
168
169 #if DISABLED_CODE
170         /* 5 FM radio */
171         ad->data_rdo = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
172                                                     NULL, NULL, SWALLOW_Type_INVALID,
173                                                     "IDS_COM_BODY_FM_RADIO",
174                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_FM_RADIO));
175         if (ad->data_rdo) {
176                 ad->data_rdo->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_FM_RADIO;
177         } else {
178                 SETTING_TRACE_ERROR("ad->data_rdo is NULL");
179         }
180
181
182         /* 6 DVB-H */
183         ad->data_dvb_h = setting_create_Gendial_exp_parent_field(scroller, &(ad->itc_2text_3_parent),
184                                                     NULL, NULL, SWALLOW_Type_INVALID,
185                                                     DVB_H_STR,
186                                                     __setting_storage_default_storage_desc_get(INT_SLP_SETTING_DEFAULT_MEM_DVB_H));
187         if (ad->data_dvb_h) {
188                 ad->data_dvb_h->int_slp_setting_binded = INT_SLP_SETTING_DEFAULT_MEM_DVB_H;
189         } else {
190                 SETTING_TRACE_ERROR("ad->data_shr is NULL");
191         }
192 #endif
193         item = elm_genlist_item_append(scroller, &itc_bottom_seperator, NULL, NULL,
194                                     ELM_GENLIST_ITEM_NONE, NULL, NULL);
195         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
196
197         int ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS,
198                         __setting_storage_default_storage_vconf_change_cb, ad);
199         if (ret != 0) {
200                 SETTING_TRACE_ERROR("call vconf_notify_key_changed failed");
201         }
202
203         setting_view_storage_default_storage.is_create = 1;
204         return SETTING_RETURN_SUCCESS;
205 }
206
207 static int setting_storage_default_storage_destroy(void *cb)
208 {
209         SETTING_TRACE_BEGIN;
210         setting_retvm_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "cb is NULL");
211         SettingStorageUG *ad = (SettingStorageUG *) cb;
212
213         int ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS,
214                         __setting_storage_default_storage_vconf_change_cb);
215         if (ret != 0) {
216                 SETTING_TRACE_ERROR("call vconf_ignore_key_changed failed");
217         }
218
219         elm_naviframe_item_pop(ad->navibar);
220         setting_view_storage_default_storage.is_create = 0;
221         return SETTING_RETURN_SUCCESS;
222 }
223
224 static int setting_storage_default_storage_update(void *cb)
225 {
226         SETTING_TRACE_BEGIN;
227         setting_retvm_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER, "cb is NULL");
228         return SETTING_RETURN_SUCCESS;
229 }
230
231 static char* __setting_storage_default_storage_desc_get(setting_int_slp_list slp_key)
232 {
233         //SETTING_TRACE_BEGIN;
234         const char* setting_default_storage_type[SETTING_DEF_MEMORY_MAX] = {
235                 _("IDS_COM_BODY_PHONE"),
236                 _("IDS_COM_BODY_MEMORY_CARD")
237         };
238         int ret = 0;
239         int value = 0;
240         int err = -1;
241
242         int mmc_mode = VCONFKEY_SYSMAN_MMC_REMOVED;
243         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_mode);
244         setting_retvm_if(ret != 0, NULL, "Get vconf failed");
245
246         /* if mmc card is not mounted, set vconf by 0(phone) */
247         if (mmc_mode == VCONFKEY_SYSMAN_MMC_REMOVED ||
248                 mmc_mode == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
249                 ret = setting_set_int_slp_key(slp_key, 0, &err);
250                 setting_retvm_if(ret != 0, NULL, "Set vconf failed");
251         }
252
253         ret = setting_get_int_slp_key(slp_key, &value, &err);
254
255         switch (slp_key) {
256         case INT_SLP_SETTING_DEFAULT_MEM_WAP:
257         case INT_SLP_SETTING_DEFAULT_MEM_BLUETOOTH:
258         case INT_SLP_SETTING_DEFAULT_MEM_CAMERA:
259         case INT_SLP_SETTING_DEFAULT_MEM_VOICE_RECORDER:
260         case INT_SLP_SETTING_DEFAULT_MEM_FM_RADIO:
261         case INT_SLP_SETTING_DEFAULT_MEM_ALL_SHARE:
262         case INT_SLP_SETTING_DEFAULT_MEM_ADOBE_AIR:
263         case INT_SLP_SETTING_DEFAULT_MEM_DVB_H:
264                 {
265                         if (ret == SETTING_RETURN_FAIL
266                             || value >= SETTING_DEF_MEMORY_MAX)
267                                 value = SETTING_DEF_MEMORY_PHONE;
268                         return (char *)setting_default_storage_type[value];
269                 }
270                 break;
271
272         default:
273                 ret = SETTING_GENERAL_ERR_NOT_FIND_VALUE_SWITCH;
274                 break;
275         }
276
277         return NULL;
278 }
279
280 static int setting_storage_default_storage_cleanup(void *cb)
281 {
282         SETTING_TRACE_BEGIN;
283         return setting_storage_default_storage_destroy(cb);
284 }
285
286 static void __setting_storage_default_storage_cancel_cb(void *data, Evas_Object *obj, void *event_info)
287 {
288         SETTING_TRACE_BEGIN;
289         setting_retm_if(data == NULL, "data is NULL");
290
291         SettingStorageUG *ad = (SettingStorageUG *) data;
292         setting_view_change(&setting_view_storage_default_storage,
293                             &setting_view_storage_main, ad);
294 }
295
296 static void __setting_storage_default_storage_vconf_change_cb(keynode_t *key, void *data)
297 {
298         SETTING_TRACE_BEGIN;
299         setting_retm_if(NULL == key, "key is NULL");
300         setting_retm_if(NULL == data, "data is NULL");
301         SettingStorageUG *ad = (SettingStorageUG *)data;
302         int status = vconf_keynode_get_int(key);
303         char *vconf_name = vconf_keynode_get_name(key);
304
305         if (!safeStrCmp(vconf_name, VCONFKEY_SYSMAN_MMC_STATUS)) {
306                 SETTING_TRACE("status:%d", status);
307                 if (VCONFKEY_SYSMAN_MMC_MOUNTED == status) {
308                         __setting_storage_default_storage_update_item_status(ad, EINA_FALSE);
309                 } else {
310                         __setting_storage_default_storage_update_item_status(ad, EINA_TRUE);
311                 }
312         }
313 }
314
315 static void __setting_storage_default_storage_update_item_status(void *data, Eina_Bool disable_mmc_flag)
316 {
317         SETTING_TRACE_BEGIN;
318         setting_retm_if(NULL == data, "data is NULL");
319         SettingStorageUG *ad = (SettingStorageUG *)data;
320
321 #if DISABLED_CODE
322         setting_retm_if(NULL == ad->data_wap, "ad->data_wap is NULL");
323         setting_retm_if(NULL == ad->data_wap->item, "ad->data_wap->item is NULL");
324 #endif
325         setting_retm_if(NULL == ad->data_bt, "ad->data_bt is NULL");
326         setting_retm_if(NULL == ad->data_bt->item, "ad->data_bt->item is NULL");
327         setting_retm_if(NULL == ad->data_cmr, "ad->data_cmr is NULL");
328         setting_retm_if(NULL == ad->data_vrec, "ad->data_vrec is NULL");
329 #if DISABLED_CODE
330         setting_retm_if(NULL == ad->data_rdo, "ad->data_rdo is NULL");
331         setting_retm_if(NULL == ad->data_dvb_h, "ad->data_dvb_h is NULL");
332 #endif
333
334         /* first item is data_wap */
335         Elm_Object_Item *list_item = ad->data_bt->item;
336         Setting_GenGroupItem_Data *data_sub_item = NULL;
337         while (list_item != NULL) {
338                 data_sub_item = (Setting_GenGroupItem_Data *)elm_object_item_data_get(list_item);
339
340                 if (data_sub_item == NULL) {
341                         /* data_sub_item is seperator*/
342                         list_item = elm_genlist_item_next_get(list_item);
343                         continue;
344                 }
345                 SETTING_TRACE("keystr = %s", data_sub_item->keyStr);
346                 /* disable_mmc_flag is true, means mmc is not mounted*/
347                 if (disable_mmc_flag) {
348                         if (!safeStrCmp(data_sub_item->keyStr, "IDS_COM_BODY_PHONE") &&
349                                 ! elm_genlist_item_selected_get(list_item)) {
350                                 elm_genlist_item_selected_set(list_item, 1);
351                         } else if (!safeStrCmp(data_sub_item->keyStr, "IDS_COM_BODY_MEMORY_CARD")) {
352                                 elm_genlist_item_selected_set(list_item, 0);
353                                 setting_disable_genlist_item(list_item);
354                         }
355                 } else {
356                         if (!safeStrCmp(data_sub_item->keyStr, "IDS_COM_BODY_MEMORY_CARD") &&
357                                 elm_object_item_disabled_get(list_item)) {
358                                 setting_enable_genlist_item(list_item);
359                         }
360                 }
361                 list_item = elm_genlist_item_next_get(list_item);
362         }
363
364         /* if mmc is not mounted, update vconf*/
365         if (disable_mmc_flag) {
366 #if DISABLED_CODE
367                 __setting_storage_default_storage_update_vconf(ad->data_wap);
368 #endif
369                 __setting_storage_default_storage_update_vconf(ad->data_bt);
370                 __setting_storage_default_storage_update_vconf(ad->data_cmr);
371                 __setting_storage_default_storage_update_vconf(ad->data_vrec);
372 #if DISABLED_CODE
373                 __setting_storage_default_storage_update_vconf(ad->data_rdo);
374                 __setting_storage_default_storage_update_vconf(ad->data_dvb_h);
375 #endif
376         }
377 }
378
379 static void __setting_storage_default_storage_update_vconf(void *data)
380 {
381         SETTING_TRACE_BEGIN;
382         setting_retm_if(NULL == data, "data is NULL");
383         Setting_GenGroupItem_Data *data_item = (Setting_GenGroupItem_Data *)data;
384         int err = 0;
385         int value = 0;
386         int ret = 0;
387
388         ret = setting_get_int_slp_key(data_item->int_slp_setting_binded, &value, &err);
389         setting_retm_if(0 != ret, "get vconf failed");
390
391         /* if mmc card is not mounted, set vconf to 0(phone) */
392         if (value != 0) {
393                 ret = setting_set_int_slp_key(data_item->int_slp_setting_binded, 0, &err);
394                 setting_retm_if(ret != 0, "Set vconf failed");
395
396                 G_FREE(data_item->sub_desc);
397                 data_item->sub_desc = (char *)g_strdup(_("IDS_COM_BODY_PHONE"));
398                 elm_object_item_data_set(data_item->item, data_item);
399                 elm_genlist_item_update(data_item->item);
400         }
401 }