Tizen 2.4.0 rev3 SDK Public Release
[apps/home/settings.git] / setting-storage / src / setting-storage-utils.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 <storage.h>
21 #include <media_content.h>
22
23 #include "setting-storage-async-worker.h"
24 #include "setting-storage-utils.h"
25
26 void storageUg_popup_del(void *data, Evas_Object *obj, void *event_info)
27 {
28         SettingStorageUG *ad = data;
29
30         ret_if(data == NULL);
31
32         evas_object_del(ad->popup);
33         ad->popup = NULL;
34 }
35
36 void storageUg_get_internal_storage_status(double *total, double *avail)
37 {
38         int ret;
39         double tmp_total;
40         struct statvfs s;
41         const double sz_32G = 32. * 1073741824;
42         const double sz_16G = 16. * 1073741824;
43         const double sz_8G = 8. * 1073741824;
44
45         ret_if(NULL == total);
46         ret_if(NULL == avail);
47
48         ret = storage_get_internal_memory_size(&s);
49         if (0 == ret) {
50                 SETTING_TRACE("Total = %ld, Available = %ld", s.f_frsize * s.f_blocks,
51                               s.f_bsize * s.f_bavail);
52                 tmp_total = (double)s.f_frsize * s.f_blocks;
53                 *avail = (double)s.f_bsize * s.f_bavail;
54
55                 if (sz_16G < tmp_total)
56                         *total = sz_32G;
57                 else if (sz_8G < tmp_total)
58                         *total = sz_16G;
59                 else
60                         *total = sz_8G;
61         }
62 }
63
64
65 Elm_Object_Item *storageUg_append_separator(Evas_Object *genlist,
66                                             SettingStorageUG *ad)
67 {
68         Elm_Object_Item *item = NULL;
69
70         item = elm_genlist_item_append(genlist, &itc_seperator, NULL, NULL,
71                                        ELM_GENLIST_ITEM_NONE, NULL, NULL);
72         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
73
74         return item;
75 }
76
77 void storageUg_get_external_storage_status(const char *path, double *total,
78                                            double *avail)
79 {
80         struct statvfs s;
81
82         ret_if(NULL == path);
83         ret_if(NULL == total);
84         ret_if(NULL == avail);
85
86         if (!storage_get_external_memory_size(&s)) {
87                 SETTING_TRACE("f_frsize = %ld f_blocks = %ld f_bsize = %ld f_bavail = %ld ",
88                               s.f_frsize, s.f_blocks, s.f_bsize, s.f_bavail);
89                 *total = (double)s.f_frsize * s.f_blocks;
90                 *avail = (double)s.f_bsize * s.f_bavail;
91         }
92 }
93
94 void storageUg_size_to_str(double size, char *desc, int desc_size)
95 {
96         double tmp_size = 0.0;
97         const int KILOBYTE_VALUE = 1024;
98         const int MEGABYTE_VALUE = 1048576;
99         const int GIGABYTE_VALUE = 1073741824;
100
101         if (size < MEGABYTE_VALUE) {    /* size < 1MB: show x.xKB */
102                 tmp_size = size / KILOBYTE_VALUE;
103                 snprintf(desc, desc_size, "%4.2lf%s", tmp_size, _(STORAGEUG_STR_KB));
104         } else if (size < GIGABYTE_VALUE) {     /* size < 1GB: show x.xMB */
105                 tmp_size = size / MEGABYTE_VALUE;
106                 snprintf(desc, desc_size, "%4.2lf%s", tmp_size, _(STORAGEUG_STR_MB));
107         } else { /* 1G <= size: show x.xGB */
108                 tmp_size = size / GIGABYTE_VALUE;
109                 snprintf(desc, desc_size, "%4.2lf%s", tmp_size, _(STORAGEUG_STR_GB));
110         }
111 }
112
113
114 void storageUg_ug_layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
115 {
116         Evas_Object *base = NULL;
117
118         ret_if(priv == NULL);
119
120         base = ug_get_layout(ug);
121         if (base == NULL) {
122                 SETTING_TRACE_ERROR("ug_get_layout() Fail");
123                 return;
124         }
125
126         switch (mode) {
127                 case UG_MODE_FULLVIEW:
128                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
129                         evas_object_show(base);
130                         break;
131                 default:
132                         /* do nothing */
133                         break;
134         }
135 }
136
137 void storageUg_ug_destroy_cb(ui_gadget_h ug, void *priv)
138 {
139         SettingStorageUG *ad = priv;
140
141         ret_if(priv == NULL);
142
143         if (ug)
144                 setting_ug_destroy(ug);
145
146         elm_object_tree_focus_allow_set(ad->lo_main, EINA_TRUE);
147         setting_view_update(ad->main_view, ad);
148 }
149
150 void storageUg_fail_popup(SettingStorageUG *ad)
151 {
152         if (ad->popup) {
153                 evas_object_del(ad->popup);
154                 ad->popup = NULL;
155         }
156
157         ad->popup = setting_create_popup(ad, ad->win, NULL,
158                                                                          STORAGEUG_STR_FAIL, storageUg_popup_del,
159                                                                          SETTING_STORAGE_POPUP_TIMER, FALSE, FALSE, 0);
160 }
161
162 void storageUg_manage_app_ug(SettingStorageUG *ad)
163 {
164         app_control_h svc;
165         ui_gadget_h ug;
166         struct ug_cbs cbs;
167
168         ret_if(NULL == ad);
169
170         if (app_control_create(&svc))
171                 return;
172
173         app_control_add_extra_data(svc, "viewtype", "manage-applications");
174
175         memset(&cbs, 0, sizeof(struct ug_cbs));
176         cbs.layout_cb = storageUg_ug_layout_cb;
177         cbs.destroy_cb = storageUg_ug_destroy_cb;
178         cbs.priv = (void *)ad;
179
180         elm_object_tree_focus_allow_set(ad->lo_main, EINA_FALSE);
181         ug = setting_ug_create(ad->ug, "setting-manage-applications-efl", UG_MODE_FULLVIEW,
182                                svc, &cbs);
183         warn_if(NULL == ug, "setting_ug_create() Fail");
184
185         app_control_destroy(svc);
186 }
187
188 struct _calculated_sizes {
189         double video_total;
190         double audio_total;
191         double misces_total;
192 };
193
194 static bool storageUg_get_misces_item(media_info_h media, void *data)
195 {
196         unsigned long long size = 0;
197         struct _calculated_sizes *sizes = data;
198
199         retv_if(NULL == media, true);
200         retv_if(NULL == data, false);
201         char *file_path = NULL;
202         media_info_get_file_path(media, &file_path);
203         if (!ecore_file_exists(file_path)) {
204                 SETTING_TRACE_DEBUG("!ecore_file_exists(file_path)");
205                 FREE(file_path);
206                 return true;
207         }
208         media_info_get_size(media, &size);
209         sizes->misces_total += size;
210         FREE(file_path);
211
212         return true;
213 }
214
215
216 static bool storageUg_get_media_item(media_info_h media, void *data)
217 {
218         media_content_type_e type;
219         unsigned long long size = 0;
220         struct _calculated_sizes *sizes = data;
221
222         retv_if(NULL == media, true);
223         retv_if(NULL == data, false);
224
225         media_info_get_size(media, &size);
226         media_info_get_media_type(media, &type);
227         switch (type) {
228                 case MEDIA_CONTENT_TYPE_IMAGE:
229                 case MEDIA_CONTENT_TYPE_VIDEO:
230                         sizes->video_total += size;
231                         break;
232                 case MEDIA_CONTENT_TYPE_SOUND:
233                 case MEDIA_CONTENT_TYPE_MUSIC:
234                         sizes->audio_total += size;
235                         break;
236                 default:
237                         SETTING_TRACE_ERROR("Invalid Type(%d)", type);
238                         break;
239         }
240
241         return true;
242 }
243
244 enum {
245     STORAGEUG_TYPE_APP,
246     STORAGEUG_TYPE_PIC_VIDEO,
247     STORAGEUG_TYPE_AUDIO,
248     STORAGEUG_TYPE_MISCES,
249 };
250
251 void storageug_genlist_text_update(Setting_GenGroupItem_Data *item_data,
252                                    double size)
253 {
254         char desc[STORAGEUG_MAX_STR_LEN] = {0};
255
256         ret_if(NULL == item_data);
257         ret_if(NULL == item_data->item);
258
259         storageUg_size_to_str(size, desc, sizeof(desc));
260
261         G_FREE(item_data->sub_desc);
262         item_data->sub_desc = (char *)g_strdup(desc);
263         #if OLD_GENLIST_STYLE
264                 elm_genlist_item_fields_update(item_data->item, "elm.text.sub.left.bottom", ELM_GENLIST_ITEM_FIELD_TEXT);
265         #else
266                 elm_genlist_item_fields_update(item_data->item, "elm.text.sub", ELM_GENLIST_ITEM_FIELD_TEXT);
267         #endif
268 }
269
270 void storageUg_get_internal_detail_cb(int fn_result, SettingStorageUG *ad)
271 {
272         ret_if(NULL == ad);
273
274         ad->size_worker = NULL;
275
276         if (SETTING_RETURN_SUCCESS != fn_result) {
277                 SETTING_TRACE_ERROR("storageUg_get_internal_detail() Fail(%d)", fn_result);
278                 return;
279         }
280
281         ad->sz_sys = ad->sz_inter_total - ad->sz_apps - ad->sz_pics_videos
282                      - ad->sz_audio - ad->sz_misces - ad->sz_inter_avail;
283
284         storageug_genlist_text_update(ad->sys_mem, ad->sz_sys);
285         storageug_genlist_text_update(ad->pics_videos, ad->sz_pics_videos);
286         storageug_genlist_text_update(ad->audio, ad->sz_audio);
287         storageug_genlist_text_update(ad->misces, ad->sz_misces);
288
289         /* update storage pie */
290         elm_genlist_item_update(ad->pie_it);
291 }
292
293 static int storageUG_get_media_info(const char *cond, media_info_cb cb,
294                                     struct _calculated_sizes *sizes)
295 {
296         int ret;
297         filter_h filter = NULL;
298
299         /*Set Filter*/
300         ret = media_filter_create(&filter);
301         if (MEDIA_CONTENT_ERROR_NONE != ret) {
302                 SETTING_TRACE_ERROR("media_filter_create() Fail(%d)", ret);
303                 return ret;
304         }
305
306         ret = media_filter_set_condition(filter, cond, MEDIA_CONTENT_COLLATE_DEFAULT);
307         if (MEDIA_CONTENT_ERROR_NONE != ret) {
308                 media_filter_destroy(filter);
309                 SETTING_TRACE_ERROR("media_filter_set_condition() Fail(%d)", ret);
310                 return ret;
311         }
312
313         ret = media_info_foreach_media_from_db(filter, cb, sizes);
314         if (MEDIA_CONTENT_ERROR_NONE != ret) {
315                 media_filter_destroy(filter);
316                 SETTING_TRACE_ERROR("media_info_foreach_media_from_db() Fail(%d)", ret);
317                 return ret;
318         }
319
320         ret = media_filter_destroy(filter);
321         if (MEDIA_CONTENT_ERROR_NONE != ret) {
322                 SETTING_TRACE_ERROR("media_filter_destroy() Fail(%d)", ret);
323                 return ret;
324         }
325
326         return ret;
327 }
328 static void storageUG_get_cache_files_size(pkgmgr_client *pc, const pkg_size_info_t *size_info, void *user_data)
329 {
330         SETTING_TRACE_BEGIN;
331         setting_retm_if(NULL == user_data, "user_data is NULL");
332         setting_retm_if(NULL == size_info, "size_info is NULL");
333         /*char * path = app_get_cache_path(); */
334         /*SETTING_TRACE_DEBUG("cache path:%s",path); */
335         SettingStorageUG *ad = user_data;
336         ad->sz_caches = (double)(size_info->cache_size + size_info->ext_cache_size);
337         SETTING_TRACE_DEBUG("ad->sz_caches:%lf", ad->sz_caches);
338         storageug_genlist_text_update(ad->caches, ad->sz_caches);
339
340         setting_retm_if(!ad->pie_it, "!ad->pie_it")
341         elm_genlist_item_update(ad->pie_it);
342
343         pkgmgr_client_free(ad->pc_total_size);
344         ad->pc_total_size = NULL;
345         SETTING_TRACE_END;
346 }
347
348 int storageUg_get_internal_detail(SettingStorageUG *ad)
349 {
350         int ret;
351         const char *cond;
352         struct _calculated_sizes sizes = {0.0, 0.0, 0.0};
353
354         retv_if(NULL == ad, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
355
356         storageUG_STOP_POINT;
357
358         /*0-image, 1-video, 2-sound, 3-music, 4-other*/
359         cond = "(MEDIA_TYPE < 4) and MEDIA_PATH LIKE \'/opt/usr/%%\'";
360         ret = storageUG_get_media_info(cond, storageUg_get_media_item, &sizes);
361         warn_if(MEDIA_CONTENT_ERROR_NONE != ret, "storageUG_get_media_info() Fail(%d)", ret);
362
363         storageUG_STOP_POINT;
364
365         cond = "(MEDIA_TYPE = 4) and MEDIA_PATH LIKE \'/opt/usr/media/%%\'";;
366         ret = storageUG_get_media_info(cond, storageUg_get_misces_item, &sizes);
367         warn_if(MEDIA_CONTENT_ERROR_NONE != ret, "storageUG_get_media_info() Fail(%d)", ret);
368
369         storageUG_STOP_POINT;
370
371         ad->sz_pics_videos = sizes.video_total;
372         ad->sz_audio = sizes.audio_total;
373         ad->sz_misces = sizes.misces_total;
374
375         return SETTING_RETURN_SUCCESS;
376 }
377
378 void storageUG_update_cache_info(SettingStorageUG *ad)
379 {
380
381         /*package_manager_get_total_package_size_info(storageUG_get_cache_files_size, ad); */
382         int ret;
383
384         ret_if(NULL == ad);
385
386         if (ad->pc_total_size)
387                 pkgmgr_client_free(ad->pc_total_size);
388
389         ad->pc_total_size = pkgmgr_client_new(PC_REQUEST);
390         if (NULL == ad->pc_total_size) {
391                 SETTING_TRACE_ERROR("pkgmgr_client_new() Fail");
392                 return;
393         }
394
395         ret = pkgmgr_client_get_total_package_size_info(ad->pc_total_size, storageUG_get_cache_files_size, ad);
396
397         warn_if(ret, "pkgmgr_client_get_total_package_size_info() Fail(%d)", ret);
398 }
399
400 static int storageUg_get_apps_info(int req_id, const char *pkg_type,
401                                    const char *pkgid, const char *key, const char *val, const void *pmsg, void *data)
402 {
403         SettingStorageUG *ad = data;
404
405         retv_if(NULL == data, 0);
406         retv_if(NULL == val, 0);
407
408         ad->sz_apps = atof(val);
409
410         storageug_genlist_text_update(ad->apps, ad->sz_apps);
411         setting_retvm_if(!ad->pie_it, 0, "!ad->pie_it")
412         elm_genlist_item_update(ad->pie_it);
413         return 0;
414 }
415
416 void storageUG_update_apps_info(SettingStorageUG *ad)
417 {
418         int ret;
419
420         ret_if(NULL == ad);
421
422         if (ad->pc)
423                 pkgmgr_client_free(ad->pc);
424
425         ad->pc = pkgmgr_client_new(PC_REQUEST);
426         if (NULL == ad->pc) {
427                 SETTING_TRACE_ERROR("pkgmgr_client_new() Fail");
428                 return;
429         }
430
431         ret = pkgmgr_client_get_size(ad->pc, "get", PM_GET_ALL_PKGS, storageUg_get_apps_info,
432                                      ad);
433         warn_if(ret, "pkgmgr_client_get_size() Fail(%d)", ret);
434 }
435