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