diff options
Diffstat (limited to 'src/doc-local-data.c')
-rw-r--r-- | src/doc-local-data.c | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/src/doc-local-data.c b/src/doc-local-data.c new file mode 100644 index 0000000..04c990a --- /dev/null +++ b/src/doc-local-data.c @@ -0,0 +1,322 @@ +/* +* Copyright (c) 2000-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 <stdio.h> +#include <media_content.h> +#include <media_info.h> +#include <glib.h> +#include <string.h> +#include "doc-data-util.h" +#include "doc-local-data.h" +#include "doc-debug.h" + + +static bool _doc_local_data_clone_album(media_folder_h folder, bool b_path, + doc_folder_s **palbum) +{ + DOC_CHECK_FALSE(folder); + DOC_CHECK_FALSE(palbum); + doc_folder_s *album = NULL; + + album = (doc_folder_s *)calloc(1, sizeof(doc_folder_s)); + if (album == NULL) { + doc_dbgE("Failed to calloc!"); + return false; + } + album->gtype = DOC_TYPE_FOLDER; + + if (b_path) { + if (media_folder_get_path(folder, &(album->path)) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get folder path failed!"); + goto DOC_LOCAL_FAILED; + } + } + if (media_folder_get_folder_id(folder, &(album->uuid)) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get folder id failed!"); + goto DOC_LOCAL_FAILED; + } + if (media_folder_get_modified_time(folder, &(album->mtime)) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get modified time failed!"); + goto DOC_LOCAL_FAILED; + } + + media_content_storage_e storage_type; + if (media_folder_get_storage_type(folder, &storage_type) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get storage type failed!"); + goto DOC_LOCAL_FAILED; + } + + if (storage_type == MEDIA_CONTENT_STORAGE_INTERNAL) { /* The device's internal storage */ + album->type = DOC_PHONE; + } else if (storage_type == MEDIA_CONTENT_STORAGE_EXTERNAL) { /* The device's external storage */ + album->type = DOC_MMC; + } else { + doc_dbgE("Undefined mode[%d]!", storage_type); + } + + if (media_folder_get_name(folder, &(album->display_name)) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get folder name failed!"); + goto DOC_LOCAL_FAILED; + } + + *palbum = album; + return true; + +DOC_LOCAL_FAILED: + + doc_data_type_free_geitem((void **)(&album)); + *palbum = NULL; + return false; +} + +static bool _doc_local_data_get_media_list_cb(media_info_h media, void *data) +{ + DOC_CHECK_FALSE(data); + doc_transfer_data_s *td = (doc_transfer_data_s *)data; + DOC_CHECK_FALSE(td->userdata); + DOC_CHECK_FALSE(media); + doc_media_s *item = NULL; + + Eina_List **elist = (Eina_List **)(td->userdata); + + if (doc_data_util_clone_media(media, &item, td->with_meta)) { + *elist = eina_list_append(*elist, item); + return true; + } else { + return false; + } +} + +int doc_local_data_connect(void) +{ + int ret = -1; + + ret = media_content_connect(); + if (ret == MEDIA_CONTENT_ERROR_NONE) { + doc_dbg("DB connection is success"); + return 0; + } else { + doc_dbgE("DB connection is failed!"); + return -1; + } +} + +int doc_local_data_disconnect(void) +{ + int ret = -1; + + ret = media_content_disconnect(); + if (ret == MEDIA_CONTENT_ERROR_NONE) { + doc_dbg("DB disconnection is success"); + return 0; + } else { + doc_dbgE("DB disconnection is failed!"); + return -1; + } +} + +static bool _doc_local_data_get_album_list_cb(media_folder_h folder, + void *data) +{ + DOC_CHECK_FALSE(data); + doc_transfer_data_s *tmp_data = (doc_transfer_data_s *)data; + DOC_CHECK_FALSE(tmp_data->userdata); + DOC_CHECK_FALSE(folder); + Eina_List **elist = (Eina_List **)(tmp_data->userdata); + doc_folder_s *album = NULL; + + char *path = NULL; + if (media_folder_get_path(folder, &path) != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get folder path failed!"); + return false; + } + DOC_CHECK_FALSE(path); + + if (tmp_data->list_type != DOC_ALBUM_LIST_PATH) { + if (!g_strcmp0(DOC_CAMERA_PATH_PHONE, path) || + !g_strcmp0(DOC_CAMERA_PATH_MMC, path) || + !g_strcmp0(DOC_DOWNLOADS_PATH, path)) { + doc_dbgW("Camera or Downloads!"); + DOC_FREE(path); + return true; + } + } + + _doc_local_data_clone_album(folder, false, &album); + if (album == NULL) { + DOC_FREE(path); + doc_dbgE("Failed clone album!"); + return false; + } + album->path = path; + + *elist = eina_list_append(*elist, album); + return true; +} + +static int _doc_local_data_get_album_list(doc_filter_s *condition, Eina_List **elilst) +{ + DOC_CHECK_VAL(elilst, -1); + DOC_CHECK_VAL(condition, -1); + int ret = -1; + filter_h filter = NULL; + + ret = doc_data_util_create_filter(condition, &filter); + if (ret != 0) { + doc_dbgE("Create filter failed[%d]!", ret); + return -1; + } + + doc_transfer_data_s tran_data; + memset(&tran_data, 0x00, sizeof(doc_transfer_data_s)); + tran_data.userdata = (void **)elilst; + tran_data.album_id = NULL; + tran_data.with_meta = false; + tran_data.list_type = condition->list_type; + + doc_dbg("Get folders--start"); + ret = media_folder_foreach_folder_from_db(filter, + _doc_local_data_get_album_list_cb, + &tran_data); + doc_dbg("Get folders--over"); + + doc_data_util_destroy_filter(filter); + + if (ret != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get folders failed[%d]!", ret); + return -1; + } + + return 0; +} + +int doc_local_data_get_folder_by_path(char *path, doc_folder_s **folder) +{ + DOC_CHECK_VAL(path, -1); + DOC_CHECK_VAL(folder, -1); + int ret = -1; + Eina_List *list = NULL; + doc_filter_s condition; + doc_folder_s *_item = NULL; + int i = 0; + + if (strlen(path) <= 0) { + doc_dbgE("Invalid path!"); + return -1; + } + doc_dbg("path: %s", path); + + memset(&condition, 0x00, sizeof(doc_filter_s)); + condition.collate_type = MEDIA_CONTENT_COLLATE_NOCASE; + condition.sort_type = MEDIA_CONTENT_ORDER_DESC; + condition.offset = -1; + condition.count = -1; + condition.with_meta = false; + condition.list_type = DOC_ALBUM_LIST_PATH; + + snprintf(condition.cond, CONDITION_LENGTH, "%s AND %s=\'%s\'", + DOC_CONDITION_IMAGE_VIDEO, FOLDER_PATH, path); + + ret = _doc_local_data_get_album_list(&condition, &list); + if (ret != 0 || NULL == list) { + doc_dbgE("Failed to get album list[%d]!", ret); + ret = -1; + } else if (NULL != list) { + *folder = eina_list_nth(list, 0); + i = 1; + ret = 0; + } + + /* Free other items */ + if (list) { + int len = eina_list_count(list); + doc_dbg("len: %d", len); + + for (; i < len; i++) { + _item = eina_list_nth(list, i); + doc_data_type_free_geitem((void **)(&_item)); + } + + eina_list_free(list); + } + + return ret; +} + +int doc_local_data_get_all_media_count(doc_filter_s *condtion, int *item_cnt) +{ + DOC_CHECK_VAL(condtion, -1); + DOC_CHECK_VAL(item_cnt, -1); + int ret = -1; + filter_h filter = NULL; + + ret = doc_data_util_create_filter(condtion, &filter); + if (ret != 0) { + doc_dbgE("Create filter failed[%d]!", ret); + return -1; + } + + doc_dbg("Get media count--start"); + ret = media_info_get_media_count_from_db(filter, item_cnt); + doc_dbg("Get media count--over"); + + doc_data_util_destroy_filter(filter); + + if (ret != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get media count failed[d]!", ret); + return -1; + } + + return 0; +} + +int doc_local_data_get_all_folders_media_list(doc_filter_s *condition, + Eina_List **elist) +{ + DOC_CHECK_VAL(elist, -1); + DOC_CHECK_VAL(condition, -1); + int ret = -1; + filter_h filter = NULL; + + ret = doc_data_util_create_filter(condition, &filter); + if (ret != 0) { + doc_dbgE("Create filter failed!"); + return -1; + } + + doc_transfer_data_s tran_data; + memset(&tran_data, 0x00, sizeof(doc_transfer_data_s)); + tran_data.userdata = (void **)elist; + tran_data.album_id = NULL; + tran_data.with_meta = condition->with_meta; + + doc_dbg("Get all medias--start"); + ret = media_info_foreach_media_from_db(filter, + _doc_local_data_get_media_list_cb, + &tran_data); + doc_dbg("Get all medias--over"); + + doc_data_util_destroy_filter(filter); + + if (ret != MEDIA_CONTENT_ERROR_NONE) { + doc_dbgE("Get all medias failed[d]!", ret); + return -1; + } + + return 0; +} + |