initial upload for tizen 2.0 beta
[apps/home/gallery.git] / src / data / gl-data-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 <errno.h>
18 #include <media_info.h>
19 #include <string.h>
20 #include "gl-data-util.h"
21 #include "gl-controlbar.h"
22 #include "gallery.h"
23 #include "gl-debug.h"
24 #include "gl-drm.h"
25 #include "gl-util.h"
26 #include "gl-strings.h"
27 #include "gl-data-type.h"
28
29 bool _gl_data_util_get_file_dir_name(const char *file_path, char *filename,
30                                      char *dir_name, char *dir_path)
31 {
32         GL_CHECK_FALSE(file_path);
33         gint i = 0;
34         gint count = 0;
35         for (i = strlen(file_path); i >= 0; i--) {
36                 if (file_path[i] != '\0') {
37                         count++;
38                 }
39                 if (file_path[i] == '/') {
40                         if (filename != NULL) {
41                                 memcpy(filename, &file_path[i + 1], --count);
42                                 *(filename + count) = '\0';
43                                 gl_dbg("File Name = %s", filename);
44                         }
45                         if (dir_path != NULL) {
46                                 memcpy(dir_path, &file_path[0], i);
47                                 *(dir_path + i) = '\0';
48                                 gl_dbg("Directory Name = %s", dir_path);
49                         }
50                         if (dir_name != NULL) {
51                                 count = 0;
52                                 for (--i; i >= 0; i--) {
53                                         count++;
54                                         if (file_path[i] == '/') {
55                                                 memcpy(dir_name, &file_path[i + 1], --count);
56                                                 *(dir_name + count) = '\0';
57                                                 gl_dbg("Directory Name = %s", dir_name);
58                                                 count = 0;
59                                                 return true;
60                                         }
61                                 }
62                         }
63                         return true;
64                 }
65         }
66
67         return false;
68 }
69
70 /*
71 *  create a gl_item
72 */
73 gl_item *_gl_data_util_calloc_gitem(void)
74 {
75         gl_item *gitem = (gl_item *)calloc(1, sizeof(gl_item));
76         GL_CHECK_NULL(gitem);
77         return gitem;
78 }
79
80 int _gl_data_util_free_gitem(gl_item *gitem)
81 {
82         GL_CHECK_VAL(gitem, -1);
83         if (gitem->item) {
84                 _gl_data_type_free_glitem((void **)&(gitem->item));
85                 gitem->item = NULL;
86         }
87
88         if (gitem->_reserved) {
89                 free(gitem->_reserved);
90                 gitem->_reserved = NULL;
91         }
92
93         free(gitem);
94         gitem = NULL;
95
96         return 0;
97 }
98
99 gl_cluster *_gl_data_util_calloc_gcluster(void)
100 {
101         gl_cluster *gcluster = (gl_cluster *)calloc(1, sizeof(gl_cluster));
102         GL_CHECK_NULL(gcluster);
103         return gcluster;
104 }
105
106 int _gl_data_util_free_gcluster(gl_cluster *gcluster)
107 {
108         GL_CHECK_VAL(gcluster, -1);
109         if (gcluster->cluster) {
110                 _gl_data_type_free_glitem((void **)&(gcluster->cluster));
111                 gcluster->cluster = NULL;
112         }
113
114         if (gcluster->_reserved) {
115                 free(gcluster->_reserved);
116                 gcluster->_reserved = NULL;
117         }
118
119         free(gcluster);
120         return 0;
121 }
122
123 gl_cluster *_gl_data_util_new_gcluster_all(void *data, int count)
124 {
125         gl_dbg("");
126         GL_CHECK_NULL(data);
127         gl_cluster *gcluster = NULL;
128         gl_album_s *mcluster = NULL;
129
130         gcluster = _gl_data_util_calloc_gcluster();
131         GL_CHECK_NULL(gcluster);
132
133         _gl_data_type_new_album(&mcluster);
134         if (mcluster == NULL) {
135                 free(gcluster);
136                 gcluster = NULL;
137                 return NULL;
138         }
139
140         mcluster->uuid = strdup(GL_ALBUM_ALL_ID);
141         mcluster->count = count;
142         mcluster->display_name = strdup(GL_ALBUM_ALL_NAME);
143         gcluster->ad = data;
144         gcluster->cluster = mcluster;
145         return gcluster;
146 }
147
148 gl_cluster_list *_gl_data_util_calloc_cluster_list(void)
149 {
150         gl_cluster_list *clus_list = (gl_cluster_list *)calloc(1,
151                                                                sizeof(gl_cluster_list));
152         GL_CHECK_NULL(clus_list);
153         return clus_list;
154 }
155
156 /* Clear eina_list got from DB */
157 int _gl_data_util_clear_gtype_item_list(Eina_List **elist)
158 {
159         void *current = NULL;
160
161         if (elist && *elist) {
162                 gl_dbg("Clear gtype items list");
163                 EINA_LIST_FREE(*elist, current) {
164                         if (current) {
165                                 _gl_data_type_free_glitem((void **)&current);
166                                 current = NULL;
167                         }
168                 }
169                 *elist = NULL;
170         }
171
172         return 0;
173 }
174
175 int _gl_data_util_clear_item_list(Eina_List **elist)
176 {
177         GL_CHECK_VAL(elist, -1);
178         gl_item *current = NULL;
179         if (*elist) {
180                 gl_dbg("Clear elist");
181                 EINA_LIST_FREE(*elist, current) {
182                         _gl_data_util_free_gitem(current);
183                         current = NULL;
184                 }
185                 *elist = NULL;
186         }
187
188         return 0;
189
190 }
191
192 int _gl_data_util_get_selected_cluster_id_list(void *data,
193                                                Eina_List **sel_id_list)
194 {
195         GL_CHECK_VAL(sel_id_list, -1);
196         GL_CHECK_VAL(data, -1);
197         gl_appdata *ad = (gl_appdata *)data;
198         GL_CHECK_VAL(ad->albuminfo.selected_albums_elist, -1);
199         gl_cluster *gcluster = NULL;
200         char *item_id = NULL;
201
202         /* Save ID of selected clusters */
203         EINA_LIST_FREE(ad->albuminfo.selected_albums_elist, gcluster) {
204                 if (gcluster && gcluster->cluster && gcluster->cluster->uuid) {
205                         item_id = strdup(gcluster->cluster->uuid);
206                         *sel_id_list = eina_list_append(*sel_id_list,
207                                                         (void *)item_id);
208                 }
209         }
210         return 0;
211 }
212
213 int _gl_data_util_get_selected_item_id_list(void *data,
214                                             Eina_List **sel_id_list)
215 {
216         GL_CHECK_VAL(sel_id_list, -1);
217         GL_CHECK_VAL(data, -1);
218         gl_appdata *ad = (gl_appdata *)data;
219         GL_CHECK_VAL(ad->selectedinfo.medias_elist, -1);
220         gl_item *gitem = NULL;
221         char *item_id = NULL;
222
223         /* Save ID of selected items */
224         EINA_LIST_FREE(ad->selectedinfo.medias_elist, gitem) {
225                 if (gitem && gitem->item && gitem->item->uuid) {
226                         item_id = strdup(gitem->item->uuid);
227                         *sel_id_list = eina_list_append(*sel_id_list,
228                                                         (void *)item_id);
229                 }
230         }
231         return 0;
232 }
233
234 /* Check ID is in the list or not */
235 bool _gl_data_util_check_selected_id(Eina_List **sel_id_list, const char *id)
236 {
237         GL_CHECK_FALSE(sel_id_list);
238         Eina_List *tmp_elist = NULL;
239         void *p_id = NULL;
240         GL_CHECK_FALSE(id);
241
242         if (eina_list_count(*sel_id_list) == 0) {
243                 gl_dbgE("sel_id_list is empty!");
244                 return false;
245         }
246
247         EINA_LIST_FOREACH(*sel_id_list, tmp_elist, p_id) {
248                 if (p_id == NULL) {
249                         gl_dbgE("Invalid p_id!");
250                         continue;
251                 }
252                 /* Get next one if they wasn't equal */
253                 if (g_strcmp0(id, (char *)p_id)) {
254                         p_id = NULL;
255                         continue;
256                 }
257                 *sel_id_list = eina_list_remove(*sel_id_list, p_id);
258                 free(p_id);
259                 p_id = NULL;
260                 return true;
261         }
262         return false;
263 }
264
265 /* Free list of selected IDs */
266 int _gl_data_util_free_selected_id_list(Eina_List **sel_id_list)
267 {
268         GL_CHECK_VAL(sel_id_list, -1);
269         if (*sel_id_list == NULL) {
270                 gl_dbg("sel_id_list is empty!");
271                 return -1;
272         }
273
274         void *p_id = NULL;
275         EINA_LIST_FREE(*sel_id_list, p_id) {
276                 if (p_id == NULL) {
277                         gl_dbgE("Invalid p_id!");
278                         continue;
279                 }
280                 free(p_id);
281                 p_id = NULL;
282         }
283         *sel_id_list = NULL;
284         return 0;
285 }
286
287 int _gl_data_util_check_album_selected_files(gl_cluster *album, int *drm_cnt,
288                                              int *img_cnt, int *sel_cnt)
289 {
290         GL_CHECK_VAL(album, -1);
291         GL_CHECK_VAL(album->cluster, -1);
292         GL_CHECK_VAL(album->cluster->uuid, -1);
293         GL_CHECK_VAL(album->ad, -1);
294         int _sel_cnt = 0;
295         int _img_cnt = 0;
296         int _drm_cnt = 0;
297         int err = -1;
298         Eina_List *itemlist = NULL;
299         gl_filter_s filter;
300
301         memset(&filter, 0x00, sizeof(gl_filter_s));
302         g_strlcpy(filter.cond, GL_CONDITION_IMAGE_VIDEO, CONDITION_LENGTH);
303         filter.sort_type = MEDIA_CONTENT_ORDER_DESC;
304         filter.collate_type = MEDIA_CONTENT_COLLATE_DEFAULT;
305         g_strlcpy(filter.sort_keyword, MEDIA_MODIFIED_TIME, KEYWORD_LENGTH);
306         filter.offset = GL_GET_ALL_RECORDS;
307         filter.count = GL_GET_ALL_RECORDS;
308         filter.with_meta = false;
309
310         if (!g_strcmp0(album->cluster->uuid, GL_ALBUM_ALL_ID)) {
311                 gl_dbg("All albums.");
312                 err = _gl_local_data_get_all_albums_media_list(&filter,
313                                                                &itemlist);
314         } else {
315                 err = _gl_local_data_get_album_media_list(&filter,
316                                                           album->cluster->uuid,
317                                                           &itemlist);
318         }
319
320         if ((err == 0) && (itemlist != NULL)) {
321                 gl_media_s *item = NULL;
322                 EINA_LIST_FREE(itemlist, item) {
323                         if (item == NULL || item->uuid == NULL) {
324                                 gl_dbgE("Invalid item!");
325                                 continue;
326                         }
327
328                         _sel_cnt++;
329                         if (album->cluster->type == GL_PHONE ||
330                             album->cluster->type == GL_MMC) {
331                                 if (item->type == MEDIA_CONTENT_TYPE_IMAGE)
332                                         _img_cnt++;
333                                 /* Update selected DRM files count */
334                                 if (gl_drm_is_drm_file(item->file_url))
335                                         _drm_cnt++;
336
337                         } else {
338                                 /* Checkme: now only image supported */
339                                 _img_cnt++;
340                         }
341                         item = NULL;
342                 }
343         }
344         gl_dbg("Selected items count: %d, image count: %d, drm count: %d.",
345                _sel_cnt, _img_cnt, _drm_cnt);
346         if (sel_cnt)
347                 *sel_cnt = _sel_cnt;
348         if (drm_cnt)
349                 *drm_cnt = _drm_cnt;
350         if (img_cnt)
351                 *img_cnt = _img_cnt;
352         return 0;
353 }
354