apply FSL(Flora Software License)
[apps/home/gallery.git] / ug / ug-gallery-efl / src / ge-util.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://www.tizenopensource.org/license
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17 #include <sys/stat.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20 #include <errno.h>
21 #include <media-thumbnail.h>
22 #include "ge-debug.h"
23 #include "ge-albums.h"
24 #include "ge-gridview.h"
25 #include "ge-data.h"
26 #include "ge-util.h"
27 #include "ge-ui-util.h"
28
29 #define GE_TIME_MSEC_PER_SEC 1000
30 #define GE_TIME_SEC_PER_MIN 60
31 #define GE_TIME_MIN_PER_HOUR 60
32 #define GE_TIME_SEC_PER_HOUR (GE_TIME_MIN_PER_HOUR * GE_TIME_SEC_PER_MIN)
33
34 /* Global evas objects, which is used in the entire application */
35 static ge_item* ge_current_mb_item;
36 static ge_cluster* ge_current_album_item;
37 static int ge_ug_iv_enter = 0;
38
39 ge_item* _ge_get_current_item(void)
40 {
41         return ge_current_mb_item;
42 }
43
44 void _ge_set_current_item(ge_item* gitem)
45 {
46         ge_current_mb_item = gitem;
47 }
48
49 ge_cluster* _ge_get_current_album(void)
50 {
51         return ge_current_album_item;
52 }
53
54 void _ge_set_current_album(ge_cluster* album_item)
55 {
56         ge_current_album_item = album_item;
57 }
58
59 int _ge_ug_iv_get(void)
60 {
61         return ge_ug_iv_enter;
62 }
63
64 void _ge_ug_iv_set(int ug_iv_state)
65 {
66         ge_ug_iv_enter = ug_iv_state;
67 }
68
69 static int _ge_refresh_albums_list(ge_ugdata *ugd)
70 {
71         GE_CHECK_VAL(ugd, -1);
72         ge_cluster* old_cur_cluster = _ge_get_current_album();
73         char cluster_id[GE_MTYPE_ITEN_ID_LEN_MAX] = { 0, };
74         int i = 0;
75         ge_cluster *current = NULL;
76         bool b_get_cur_album = false;
77
78         if(old_cur_cluster && old_cur_cluster->cluster &&
79            old_cur_cluster->cluster->uuid)
80         {
81                 g_strlcpy(cluster_id, old_cur_cluster->cluster->uuid,
82                           GE_MTYPE_ITEN_ID_LEN_MAX);
83                 b_get_cur_album = true;
84         }
85         else
86         {
87                 ge_dbg("None album selected, current album is unavailable\n");
88         }
89         _ge_data_get_clusters_list(ugd);
90
91         GE_CHECK_VAL(ugd->cluster_list, -1);
92         int length = eina_list_count(ugd->cluster_list->clist);
93         ge_dbg("album length: %d, current album id: %s", length, cluster_id);
94
95         //need to reset current cluster
96         _ge_set_current_album(NULL);
97         if(b_get_cur_album)
98         {
99                 for (i = 0; i < length; i++)
100                 {
101                         current = eina_list_nth(ugd->cluster_list->clist, i);
102                         if(current && current->cluster &&
103                            current->cluster->uuid) {
104                                 ge_dbg("current album : %s", current->cluster->display_name);
105                         } else {
106                                 ge_dbgE("Current is NULL");
107                                 break;
108                         }
109                         if(!g_strcmp0(current->cluster->uuid, cluster_id))
110                         {
111                                 _ge_data_update_items_list(ugd, current);
112                                 _ge_set_current_album(current);
113                                 return 0;
114                         }
115                 }
116                 /* Current album was removed, clear items list */
117                 if (_ge_data_get_selected_list())
118                         _ge_data_selected_list_finalize();
119                 _ge_data_free_items_list();
120         }
121
122         return -1;
123 }
124
125 static void _ge_db_update_noti_cb(keynode_t *key, void* data)
126 {
127         ge_dbgW("VCONFKEY_FILEMANAGER_DB_STATUS changed!");
128         GE_CHECK(data);
129         ge_ugdata* ugd = (ge_ugdata*)data;
130         int mmc_state = -1;
131         int mode = GE_UPDATE_NONE;
132
133         vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &mmc_state);
134         if(mmc_state == -1) {
135                 ge_dbgE("vconf_get_int failed!");
136                 return;
137         }
138
139         if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
140                 ge_dbg("mmc_state[%d]: VCONFKEY_SYSMAN_MMC_MOUNTED", mmc_state);
141                 mode = GE_UPDATE_MMC_ADDED;
142         } else if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED ||
143                    mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
144                 ge_dbg("mmc_state[%d]: VCONFKEY_SYSMAN_MMC_REMOVED", mmc_state);
145                 mode = GE_UPDATE_MMC_REMOVED;
146         }
147
148         /* Update view */
149         ge_update_view(ugd, mode);
150 }
151
152 Eina_Bool ge_update_view(ge_ugdata* ugd, int mode)
153 {
154         ge_dbg("mode: %d", mode);
155         GE_CHECK_FALSE(ugd);
156         int view_mode = _ge_get_view_mode();
157         ge_dbg("view_mode: %d", view_mode);
158
159         if (view_mode == GE_AlbumsView_Mode)
160         {
161                 _ge_refresh_albums_list(ugd);
162                 ge_albums_update_view(ugd);
163         }
164         else if (view_mode == GE_ThumbnailView_Mode)
165         {
166                 _ge_refresh_albums_list(ugd);
167                 if(_ge_get_current_album() == NULL)
168                 {
169                         ge_dbg("current album is empty, back to albums view!");
170                         ge_albums_back_to_view(ugd);
171                         elm_naviframe_item_pop(ugd->navi_bar);
172
173                         return EINA_FALSE;
174                 }
175
176                 ge_grid_update_view(ugd);
177         }
178         else if (view_mode == GE_ThumbnailEdit_Mode)
179         {
180                 ge_cluster* cur_album = _ge_get_current_album();
181
182                 /* MMC removed */
183                 if(mode == GE_UPDATE_MMC_REMOVED && cur_album &&
184                    cur_album->cluster &&
185                    cur_album->cluster->type == GE_MMC) {
186                         ge_dbgW("MMC album, change to albums view!");
187                         _ge_refresh_albums_list(ugd);
188                         ge_albums_back_to_view(ugd);
189                         elm_naviframe_item_pop(ugd->navi_bar);
190                         return EINA_FALSE;
191                 }
192
193                 /* Update albums list and items list */
194                 _ge_refresh_albums_list(ugd);
195                 if(_ge_data_get_count_all() <= 0) {
196                         ge_dbgW("current album is empty, back to albums view!");
197                         ge_albums_back_to_view(ugd);
198                         elm_naviframe_item_pop(ugd->navi_bar);
199                         return EINA_FALSE;
200                 }
201
202                 ge_grid_update_view(ugd);
203
204                 if(_ge_data_get_count_all() <= 0) {
205                         ge_dbgW("current album is empty, back to albums view!");
206                         ge_albums_back_to_view(ugd);
207                         elm_naviframe_item_pop(ugd->navi_bar);
208                         return EINA_FALSE;
209                 }
210         }
211
212         return EINA_TRUE;
213 }
214
215 int ge_reg_db_update_noti(ge_ugdata* ugd)
216 {
217         ge_dbg("");
218         GE_CHECK_VAL(ugd, -1);
219         int error_code = 0;
220
221         error_code = vconf_notify_key_changed(VCONFKEY_FILEMANAGER_DB_STATUS,
222                                               _ge_db_update_noti_cb, ugd);
223         if(error_code == -1)
224                 ge_dbgE("vconf_notify_key_changed FAIL!");
225
226         return 0;
227 }
228
229 int ge_dereg_db_update_noti(void)
230 {
231         ge_dbg("");
232         int error_code = -1;
233
234         error_code = vconf_ignore_key_changed(VCONFKEY_FILEMANAGER_DB_STATUS,
235                                               _ge_db_update_noti_cb);
236         if(error_code == -1)
237                 ge_dbgE("vconf_ignore_key_changed FAIL!");
238
239         return 0;
240 }
241
242 bool _ge_is_image_valid(void *data, char *filepath)
243 {
244         GE_CHECK_FALSE(data);
245         ge_ugdata *ugd = (ge_ugdata *)data;
246         GE_CHECK_FALSE(filepath);
247
248         Evas_Object *image = NULL;
249         int width = 0;
250         int height = 0;
251         Evas *evas = NULL;
252
253         evas = evas_object_evas_get(ugd->win);
254         GE_CHECK_FALSE(evas);
255
256         image = evas_object_image_add(evas);
257         GE_CHECK_FALSE(image);
258
259         evas_object_image_filled_set(image, 0);
260         evas_object_image_load_scale_down_set(image, 0);
261         evas_object_image_file_set(image, filepath, NULL);
262         evas_object_image_size_get(image, &width, &height);
263         if(image) {
264                 evas_object_del(image);
265                 image = NULL;
266         }
267
268         if (width <= 0 || height <= 0) {
269                 ge_dbg("Cannot load file : %s", filepath);
270                 return false;
271         }
272
273         return true;
274 }
275
276 /* Fetch thumbnail from media-service, if it's invalid, generate new one */
277 int _ge_get_thumb(const char *file_path, char **thumb_path)
278 {
279         GE_CHECK_VAL(thumb_path, -1);
280         GE_CHECK_VAL(file_path, -1);
281         /* Return if thumbnail is valid */
282         if (*thumb_path && ecore_file_exists(*thumb_path) &&
283             (ecore_file_size(*thumb_path) > 0)) {
284                 return 0;
285         }
286
287         /* Free allocated memory first */
288         if (*thumb_path) {
289                 ge_dbgW("Wrong thumb path[%s]", *thumb_path);
290                 free(*thumb_path);
291                 *thumb_path = NULL;
292         }
293         ge_dbg("File[%s]", file_path);
294
295         char *new_path = calloc(1, GE_FILE_PATH_LEN_MAX);
296         GE_CHECK_VAL(new_path, -1);
297
298         /* Generate thumbnail via thumbnail-service */
299         int ret = -1;
300         ret = thumbnail_request_from_db(file_path, new_path,
301                                         GE_FILE_PATH_LEN_MAX);
302         if (ret < 0) {
303                 ge_dbgE("thumbnail_request_from_db failed!");
304                 return -1;
305         }
306
307         *thumb_path = new_path;
308         ge_dbg("Request thumbnail[%s]", new_path);
309         return 0;
310 }
311
312 /* Change int to char * of video duration, caller need to free the allocated memory */
313 char *_ge_get_duration_string(unsigned int v_dur)
314 {
315         char *dur_str = calloc(1, GE_FILE_PATH_LEN_MAX);
316         GE_CHECK_NULL(dur_str);
317         if (v_dur > 0) {
318                 int duration = floor(v_dur / GE_TIME_MSEC_PER_SEC);
319                 int dur_hr = 0;
320                 int dur_min = 0;
321                 int dur_sec = 0;
322                 int tmp = 0;
323
324                 if (duration >= GE_TIME_SEC_PER_HOUR) {
325                         dur_sec = duration % GE_TIME_SEC_PER_MIN;
326                         tmp = floor(duration / GE_TIME_SEC_PER_MIN);
327                         dur_min = tmp % GE_TIME_MIN_PER_HOUR;
328                         dur_hr = floor(tmp / GE_TIME_MIN_PER_HOUR);
329                 } else if (duration >= GE_TIME_SEC_PER_MIN) {
330                         dur_hr = 0;
331                         dur_min = floor(duration / GE_TIME_SEC_PER_MIN);
332                         dur_sec = duration % GE_TIME_SEC_PER_MIN;
333                 } else {
334                         dur_hr = 0;
335                         dur_min = 0;
336                         dur_sec = duration % GE_TIME_SEC_PER_MIN;
337                 }
338
339                 snprintf(dur_str, GE_FILE_PATH_LEN_MAX, "%02d:%02d:%02d",
340                          dur_hr, dur_min, dur_sec);
341         } else {
342                 snprintf(dur_str, GE_FILE_PATH_LEN_MAX, "00:00:00");
343         }
344         dur_str[strlen(dur_str)] = '\0';
345         return dur_str;
346 }
347