2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <Elementary.h>
18 #include <media_content.h>
19 #include <app_debug.h>
20 #include <app_media.h>
22 #include "data/mediadata.h"
24 typedef int (*media_compare)(struct group_info *gi, app_media_info *info);
25 typedef char *(*group_name_get)(app_media_info *info);
27 static int _compare_cb_date(const void *, const void *);
28 static int _compare_cb_name(const void *, const void *);
30 static int _compare_title(struct group_info *, app_media_info *);
31 static char *_get_title(app_media_info *);
39 Eina_List *folder_list;
40 Eina_List *media_list;
44 const char *media_type;
48 Eina_List *folder_list;
49 Eina_List *media_list;
54 const char *order_keyword;
59 media_compare media_cmp;
60 group_name_get name_get;
63 static struct _sort_info g_sort_info[E_SORT_MAX] = {
65 .order_type = MEDIA_CONTENT_ORDER_DESC,
66 .order_keyword = MEDIA_MODIFIED_TIME,
67 .func = _compare_cb_date,
70 .order_type = MEDIA_CONTENT_ORDER_ASC,
71 .order_keyword = MEDIA_TITLE,
72 .func = _compare_cb_name,
76 static struct _list_info g_list_info[E_LIST_MAX] = {
78 .media_cmp = _compare_title,
79 .name_get = _get_title,
83 static bool _create_filter(struct _data *data, filter_h *filter, int type)
89 ret = media_filter_create(&tmp_filter);
90 if (ret != MEDIA_CONTENT_ERROR_NONE) {
91 _ERR("failed to create media filter");
95 snprintf(buf, sizeof(buf), "%s", data->media_type);
97 if (type == E_FILTER_FOLDER && data->source_type != E_SOURCE_ALL) {
100 snprintf(s, sizeof(s), " AND MEDIA_STORAGE_TYPE=%d",
105 media_filter_set_condition(tmp_filter, buf,
106 MEDIA_CONTENT_COLLATE_DEFAULT);
108 media_filter_set_order(tmp_filter,
109 g_sort_info[data->sort_type].order_type,
110 g_sort_info[data->sort_type].order_keyword,
111 MEDIA_CONTENT_COLLATE_NOCASE);
113 *filter = tmp_filter;
118 static int _compare_title(struct group_info *gi, app_media_info *info)
120 if (!gi || !gi->name || !info->title)
123 return strncasecmp(gi->name, info->title, 1);
126 static char *_get_title(app_media_info *info)
131 return strndup(info->title, 1);
134 static int _compare_cb_date(const void *data1, const void *data2)
136 app_media *am1, *am2;
137 app_media_info *info1, *info2;
139 am1 = (app_media *)data1;
140 am2 = (app_media *)data2;
142 info1 = app_media_get_info(am1);
143 info2 = app_media_get_info(am2);
145 if (!info1 || !info2)
148 if (info1->modified_time > info2->modified_time)
154 static int _compare_cb_name(const void *data1, const void *data2)
156 app_media *am1, *am2;
157 app_media_info *info1, *info2;
159 am1 = (app_media *)data1;
160 am2 = (app_media *)data2;
162 info1 = app_media_get_info(am1);
163 info2 = app_media_get_info(am2);
165 if (!info1 || !info2 || !info1->display_name || !info2->display_name)
168 return strcasecmp(info1->display_name, info2->display_name);
171 static Eina_List *_sort_list(Eina_List *list, int sort)
173 Eina_List *sorted_list;
175 sorted_list = eina_list_sort(list, 0, g_sort_info[sort].func);
180 static void _destroy_folderlist(Eina_List *list)
182 struct group_info *gi;
185 EINA_LIST_FREE(list, gi) {
187 EINA_LIST_FREE(gi->list, am)
188 app_media_destroy(am);
193 static bool _get_each_media_info(media_info_h media_h, void *dt)
203 am = app_media_create(media_h);
205 _ERR("failed to create app media");
209 data->item_list = eina_list_append(data->item_list, am);
214 static bool _get_each_folder_info(media_folder_h folder, void *dt)
218 struct group_info *gi;
227 gi = calloc(1, sizeof(*gi));
229 _ERR("failed to create folderdata");
233 if (media_folder_get_name(folder, &(gi->name))
234 != MEDIA_CONTENT_ERROR_NONE) {
235 _ERR("Media folder id Fetch error");
239 if (media_folder_get_folder_id(folder, &uuid)
240 != MEDIA_CONTENT_ERROR_NONE) {
241 _ERR("failed to fetch media folder id");
245 if (!_create_filter(data, &filter, E_FILTER_MEDIA)) {
246 _ERR("failed to create filter");
250 ret = media_folder_foreach_media_from_db(uuid, filter,
251 _get_each_media_info, data);
252 if (ret != MEDIA_CONTENT_ERROR_NONE) {
253 _ERR("failed to get media info");
257 gi->list = eina_list_clone(data->item_list);
258 data->media_list = eina_list_merge(data->media_list, data->item_list);
259 data->item_list = NULL;
261 data->folder_list = eina_list_append(data->folder_list, gi);
263 media_filter_destroy(filter);
269 media_filter_destroy(filter);
271 _destroy_folderlist(data->folder_list);
275 static bool _get_folderlist(struct _data *data)
280 if (!_create_filter(data, &filter, E_FILTER_FOLDER)) {
281 _ERR("failed to create filter");
285 ret = media_folder_foreach_folder_from_db(filter,
286 _get_each_folder_info, data);
287 if (ret != MEDIA_CONTENT_ERROR_NONE) {
288 _ERR("failed to get folder info");
289 media_filter_destroy(filter);
293 media_filter_destroy(filter);
298 struct mediadata *mediadata_create(const char *media_type,
299 int source_type, int sort_type)
301 struct mediadata *md;
305 if (!media_type || source_type < 0 || source_type >= E_SOURCE_MAX
306 || sort_type < 0 || sort_type >= E_SORT_MAX) {
307 _ERR("invalid argument");
311 md = calloc(1, sizeof(*md));
313 _ERR("failed to create mediadata");
317 ret = media_content_connect();
318 if (ret != MEDIA_CONTENT_ERROR_NONE) {
319 _ERR("failed to connect to media content");
324 data.media_type = media_type;
325 data.source_type = source_type;
326 data.sort_type = sort_type;
327 data.item_list = NULL;
328 data.folder_list = NULL;
329 data.media_list = NULL;
331 if (!_get_folderlist(&data)) {
332 _ERR("failed to get folderlist");
334 media_content_disconnect();
338 media_content_disconnect();
340 md->folder_list = data.folder_list;
341 md->media_list = _sort_list(data.media_list, data.sort_type);
346 void mediadata_destroy(struct mediadata *md)
349 _ERR("failed to get mediadata");
353 _destroy_folderlist(md->folder_list);
354 eina_list_free(md->media_list);
357 Eina_List *mediadata_get_list(struct mediadata *md, int list_type)
360 struct group_info *gi;
362 app_media_info *info;
365 _ERR("failed to get mediadata");
369 if (list_type < 0 || list_type >= E_LIST_MAX) {
370 _ERR("invalid argument");
376 EINA_LIST_FOREACH(md->media_list, l, am) {
377 info = app_media_get_info(am);
379 _ERR("failed to get media info");
380 mediadata_free_list(list);
384 if (g_list_info[list_type].media_cmp(gi, info)) {
385 gi = calloc(1, sizeof(*gi));
387 _ERR("failed to create group info");
388 mediadata_free_list(list);
392 gi->name = g_list_info[list_type].name_get(info);
394 list = eina_list_append(list, gi);
397 gi->list = eina_list_append(gi->list, am);
403 void mediadata_free_list(Eina_List *list)
406 struct group_info *gi;
408 EINA_LIST_FOREACH(list, l, gi) {
410 eina_list_free(gi->list);