diff options
author | Jehun Lim <jehun.lim@samsung.com> | 2015-09-17 19:48:28 +0900 |
---|---|---|
committer | Jehun Lim <jehun.lim@samsung.com> | 2015-09-17 19:53:20 +0900 |
commit | 094b7a75ddfd27cb0f352a290f7b79dbb6dd9d40 (patch) | |
tree | f947401a67282a2dd84303eb5bcc5fcc7753581d | |
parent | fd58c1c3e5a976a0d862b8ebd243d3505d33d19a (diff) | |
download | air_mediahub-094b7a75ddfd27cb0f352a290f7b79dbb6dd9d40.tar.gz air_mediahub-094b7a75ddfd27cb0f352a290f7b79dbb6dd9d40.tar.bz2 air_mediahub-094b7a75ddfd27cb0f352a290f7b79dbb6dd9d40.zip |
datamgr: add albumdata
Change-Id: If2785168188eac4d29a898f30a76188b0a95ce48
Signed-off-by: Jehun Lim <jehun.lim@samsung.com>
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | include/data/albumdata.h | 37 | ||||
-rw-r--r-- | include/data/datamgr.h | 4 | ||||
-rw-r--r-- | src/data/albumdata.c | 476 | ||||
-rw-r--r-- | src/data/datamgr.c | 5 |
5 files changed, 521 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fff0708..e775fec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,7 @@ src/util/playermgr.c src/util/ctxpopup.c src/util/progressbar.c src/data/datamgr.c +src/data/albumdata.c src/data/mediadata.c src/grid/grid_movie.c src/grid/grid_gallery.c diff --git a/include/data/albumdata.h b/include/data/albumdata.h new file mode 100644 index 0000000..6bc7ed2 --- /dev/null +++ b/include/data/albumdata.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _AIR_MEDIAHUB_ALBUMDATA_H__ +#define _AIR_MEDIAHUB_ALBUMDATA_H__ + +struct data_ops; + +enum album_group_type { + E_ALBUM_NAME = 0, + E_ALBUM_ARTIST, + E_ALBUM_MAX +}; + +struct album_info { + int id; + char *name; + char *artist; + char *album_art; +}; + +struct data_ops *albumdata_get_ops(void); + +#endif /* _AIR_MEDIAHUB_ALBUMDATA_H__ */ diff --git a/include/data/datamgr.h b/include/data/datamgr.h index cd017f2..fb87dd5 100644 --- a/include/data/datamgr.h +++ b/include/data/datamgr.h @@ -18,11 +18,13 @@ #define _AIR_MEDIAHUB_DATAMGR_H__ #include "data/mediadata.h" +#include "data/albumdata.h" enum data_type { E_DATA_MEDIA = 0, + E_DATA_ALBUM, E_DATA_MAX - /* Album and folder will be added later */ + /* Folder will be added later */ }; enum source_type { diff --git a/src/data/albumdata.c b/src/data/albumdata.c new file mode 100644 index 0000000..f38622d --- /dev/null +++ b/src/data/albumdata.c @@ -0,0 +1,476 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <Elementary.h> +#include <media_content.h> +#include <app_debug.h> + +#include "data/datamgr.h" + +struct albumdata; + +typedef bool (*get_album_list)(struct albumdata *ad); +typedef int (*media_compare)(struct group_info *gi, struct album_info *ai); +typedef void *(*media_compare_data_get)(struct album_info *ai); +typedef char *(*group_name_get)(struct album_info *ai); + +static bool _get_name_list(struct albumdata *ad); +static int _compare_name(struct group_info *, struct album_info *); +static void *_get_data_name(struct album_info *); +static char *_get_name(struct album_info *); + +static bool _get_artist_list(struct albumdata *ad); +static int _compare_artist(struct group_info *, struct album_info *); +static void *_get_data_artist(struct album_info *); +static char *_get_artist(struct album_info *); + + +struct albumdata { + Eina_List *album_list; + + const char *media_type; + int source_type; +}; + +struct _group_info { + const char *sort_keyword; + get_album_list get_list; + media_compare media_cmp; + media_compare_data_get data_get; + group_name_get name_get; +}; + +static struct _group_info g_group_info[E_ALBUM_MAX] = { + [E_ALBUM_NAME] = { + .sort_keyword = MEDIA_ALBUM, + .get_list = _get_name_list, + .media_cmp = _compare_name, + .data_get = _get_data_name, + .name_get = _get_name, + }, + [E_ALBUM_ARTIST] = { + .sort_keyword = MEDIA_ALBUM_ARTIST, + .get_list = _get_artist_list, + .media_cmp = _compare_artist, + .data_get = _get_data_artist, + .name_get = _get_artist + }, +}; + +static bool _create_filter(struct albumdata *ad, filter_h *filter, + const char *cond, int group_type) +{ + filter_h tmp_filter; + int ret; + char buf[1024]; + + ret = media_filter_create(&tmp_filter); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to create media filter"); + return false; + } + + snprintf(buf, sizeof(buf), "%s", ad->media_type); + + if (ad->source_type != E_SOURCE_ALL) { + char s1[64]; + + snprintf(s1, sizeof(s1), " AND MEDIA_STORAGE_TYPE=%d", + ad->source_type); + + strcat(buf, s1); + } + + if (cond) { + char s2[64]; + + snprintf(s2, sizeof(s2), " AND %s", cond); + + strcat(buf, s2); + } + + media_filter_set_condition(tmp_filter, buf, + MEDIA_CONTENT_COLLATE_DEFAULT); + + media_filter_set_order(tmp_filter, MEDIA_CONTENT_ORDER_ASC, + g_group_info[group_type].sort_keyword, + MEDIA_CONTENT_COLLATE_DEFAULT); + + *filter = tmp_filter; + + return true; +} + +static int _compare_name(struct group_info *gi, struct album_info *ai) +{ + if (!gi || !gi->data || !ai->name) + return -1; + + return strncasecmp(gi->data, ai->name, 1); +} + +static void *_get_data_name(struct album_info *ai) +{ + if (!ai->name) + return NULL; + + return strdup(ai->name); +} + +static char *_get_name(struct album_info *ai) +{ + if (!ai->name) + return NULL; + + return strndup(ai->name, 1); +} + +static int _compare_artist(struct group_info *gi, struct album_info *ai) +{ + if (!gi || !gi->data || !ai->artist) + return -1; + + return strncasecmp(gi->data, ai->artist, 1); +} + +static void *_get_data_artist(struct album_info *ai) +{ + if (!ai->artist) + return NULL; + + return strdup(ai->artist); +} + +static char *_get_artist(struct album_info *ai) +{ + if (!ai->artist) + return NULL; + + return strndup(ai->artist, 1); +} + +static void _destroy_album_list(Eina_List *list) +{ + struct album_info *ai; + + EINA_LIST_FREE(list, ai) { + free(ai->name); + free(ai->artist); + free(ai->album_art); + + free(ai); + } +} + +static bool _get_each_album_info(media_album_h album, void *data) +{ + Eina_List **list; + struct album_info *ai; + + if (!data) + return false; + + list = data; + + ai = calloc(1, sizeof(*ai)); + if (!ai) { + _ERR("failed to allocate memory"); + return false; + } + + if (media_album_get_album_id(album, &(ai->id)) + != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to fetch album id"); + free(ai); + return false; + } + + if (media_album_get_name(album, &(ai->name)) + != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to fetch album name"); + free(ai); + return false; + } + + if (media_album_get_artist(album, &(ai->artist)) + != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to fetch album artist"); + free(ai); + return false; + } + + if (media_album_get_album_art(album, &(ai->album_art)) + != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to fetch album art"); + free(ai); + return false; + } + + *list = eina_list_append(*list, ai); + + return true; +} + +static bool _get_each_group_info(const char *group_name, void *data) +{ + filter_h filter; + struct albumdata *ad; + int ret; + char buf[1024]; + + if (!data || !group_name) + return false; + + ad = data; + + snprintf(buf, sizeof(buf), "MEDIA_ALBUM_ARTIST=\"%s\"", group_name); + + if (!_create_filter(ad, &filter, buf, E_ALBUM_ARTIST)) { + _ERR("failed to create filter"); + return false; + } + + /* only one album info for the artist */ + media_filter_set_offset(filter, 0, 1); + + ret = media_album_foreach_album_from_db(filter, + _get_each_album_info, &ad->album_list); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to get album info"); + media_filter_destroy(filter); + return false; + } + + media_filter_destroy(filter); + + return true; +} + +static bool _get_name_list(struct albumdata *ad) +{ + filter_h filter; + int ret; + + ret = media_content_connect(); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to connect to media content"); + return false; + } + + if (!_create_filter(ad, &filter, NULL, E_ALBUM_NAME)) { + _ERR("failed to create filter"); + media_content_disconnect(); + return false; + } + + ret = media_album_foreach_album_from_db(filter, + _get_each_album_info, &ad->album_list); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to get album info"); + _destroy_album_list(ad->album_list); + media_filter_destroy(filter); + media_content_disconnect(); + return false; + } + + media_filter_destroy(filter); + media_content_disconnect(); + + return true; +} + +static bool _get_artist_list(struct albumdata *ad) +{ + filter_h filter; + int ret; + + ret = media_content_connect(); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to connect to media content"); + return false; + } + + if (!_create_filter(ad, &filter, NULL, E_ALBUM_ARTIST)) { + _ERR("failed to create filter"); + media_content_disconnect(); + return false; + } + + ret = media_group_foreach_group_from_db(filter, + MEDIA_CONTENT_GROUP_ALBUM_ARTIST, + _get_each_group_info, ad); + if (ret != MEDIA_CONTENT_ERROR_NONE) { + _ERR("failed to get group info"); + _destroy_album_list(ad->album_list); + media_filter_destroy(filter); + media_content_disconnect(); + return false; + } + + media_filter_destroy(filter); + media_content_disconnect(); + + return true; +} + +static void *_create(const char *media_type, int source_type) +{ + struct albumdata *ad; + + if (!media_type || source_type < 0 || source_type >= E_SOURCE_MAX) { + _ERR("invalid argument"); + return NULL; + } + + ad = calloc(1, sizeof(*ad)); + if (!ad) { + _ERR("failed to allocate ad"); + return NULL; + } + + ad->media_type = media_type; + ad->source_type = source_type; + + return (void *)ad; +} + +static void _destroy(void *handle) +{ + struct albumdata *ad; + + if (!handle) { + _ERR("failed to get albumdata handle"); + return; + } + + ad = handle; + + _destroy_album_list(ad->album_list); + + free(ad); +} + +static Eina_List *_get_list(void *handle) +{ + struct albumdata *ad; + + if (!handle) { + _ERR("failed to get albumdata handle"); + return NULL; + } + + ad = handle; + + return ad->album_list; +} + +static int _get_count(void *handle, int type) +{ + struct albumdata *ad; + + if (!handle) { + _ERR("failed to get albumdata handle"); + return -1; + } + + return eina_list_count(ad->album_list); +} + +static void _free_group_list(Eina_List *list) +{ + Eina_List *l; + struct group_info *gi; + + EINA_LIST_FOREACH(list, l, gi) { + free(gi->name); + free(gi->data); + eina_list_free(gi->list); + + free(gi); + } +} + +static void _free_group(Eina_List *list) +{ + _free_group_list(list); +} + +static Eina_List *_get_group(void *handle, int type) +{ + Eina_List *list, *l; + struct albumdata *ad; + struct group_info *gi; + struct album_info *ai; + + if (!handle) { + _ERR("failed to get albumdata handle"); + return NULL; + } + + if (type < 0 || type >= E_ALBUM_MAX) { + _ERR("invalid argument"); + return NULL; + } + + ad = handle; + + if (ad->album_list) { + _destroy_album_list(ad->album_list); + ad->album_list = NULL; + } + + if (!g_group_info[type].get_list(ad)) { + _ERR("failed to get album list"); + return NULL; + } + + gi = NULL; + list = NULL; + EINA_LIST_FOREACH(ad->album_list, l, ai) { + if (g_group_info[type].media_cmp(gi, ai)) { + gi = calloc(1, sizeof(*gi)); + if (!gi) { + _ERR("failed to create group info"); + _free_group_list(list); + return NULL; + } + + gi->name = g_group_info[type].name_get(ai); + gi->data = g_group_info[type].data_get(ai); + + list = eina_list_append(list, gi); + } + + gi->list = eina_list_append(gi->list, ai); + } + + return list; +} + +static struct data_ops _ops = { + .create = _create, + .destroy = _destroy, + .get_list = _get_list, + .get_count = _get_count, + .get_group = _get_group, + .free_group = _free_group, +}; + +struct data_ops *albumdata_get_ops(void) +{ + return &_ops; +}; + diff --git a/src/data/datamgr.c b/src/data/datamgr.c index ac8a31d..2de137f 100644 --- a/src/data/datamgr.c +++ b/src/data/datamgr.c @@ -28,7 +28,10 @@ struct _type_info { static struct _type_info g_type_info[E_DATA_MAX] = { [E_DATA_MEDIA] = { .func_get_ops = mediadata_get_ops - } + }, + [E_DATA_ALBUM] = { + .func_get_ops = albumdata_get_ops + }, }; struct datamgr *datamgr_create(int data_type, const char *media_type, |