X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fcommon%2Fmedia-svc.c;h=c8837f0b93cd9bd4ba15a99b83cc9995d6225569;hb=f64c28a18827b4995b8cd73d98ea8b875205895f;hp=5270419907397b8aac76ba2250ee155507705513;hpb=1de28a09f680297bed1f92f185e349ef1d1924b2;p=platform%2Fcore%2Fmultimedia%2Flibmedia-service.git diff --git a/src/common/media-svc.c b/src/common/media-svc.c index 5270419..c8837f0 100755 --- a/src/common/media-svc.c +++ b/src/common/media-svc.c @@ -3,8 +3,6 @@ * * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. * - * Contact: Hyunjun Ko , Haejeong Kim - * * 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 @@ -19,10 +17,6 @@ * */ -#include -#include -#include -#include "media-svc.h" #include "media-svc-media.h" #include "media-svc-debug.h" #include "media-svc-util.h" @@ -33,180 +27,109 @@ #include "media-svc-noti.h" #include "media-svc-storage.h" -static __thread int g_media_svc_item_validity_data_cnt = 1; -static __thread int g_media_svc_item_validity_cur_data_cnt = 0; - -static __thread int g_media_svc_insert_item_data_cnt = 1; -static __thread int g_media_svc_insert_item_cur_data_cnt = 0; +#include -static __thread int g_media_svc_update_item_data_cnt = 1; -static __thread int g_media_svc_update_item_cur_data_cnt = 0; +#define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini" -static __thread int g_media_svc_insert_folder_data_cnt = 1; -static __thread int g_media_svc_insert_folder_cur_data_cnt = 0; +//static __thread int g_media_svc_data_cnt = 0; +static __thread int g_media_svc_cur_data_cnt = 0; /* Flag for items to be published by notification */ -static __thread int g_insert_with_noti = FALSE; - -#define BATCH_REQUEST_MAX 300 - -static bool __media_svc_check_storage(media_svc_storage_type_e storage_type) -{ - if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) - && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) - && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) { - media_svc_error("storage type is incorrect[%d]", storage_type); - return FALSE; - } - - return TRUE; -} - -int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write) -{ - int ret = MS_MEDIA_ERR_NONE; - MediaDBHandle *db_handle = NULL; - - media_svc_debug_fenter(); - - ret = media_db_connect(&db_handle, uid, need_write); - if (ret != MS_MEDIA_ERR_NONE) - return ret; +static __thread bool g_insert_with_noti = false; - *handle = db_handle; - return MS_MEDIA_ERR_NONE; -} - -int media_svc_disconnect(MediaSvcHandle *handle) -{ - MediaDBHandle *db_handle = (MediaDBHandle *)handle; - - media_svc_debug_fenter(); - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - return media_db_disconnect(db_handle); -} - -int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid) -{ - MediaDBHandle *db_handle = (MediaDBHandle *)handle; - - media_svc_debug_fenter(); - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - return _media_svc_do_cleanup(db_handle, uid); -} +#define BATCH_ITEM_COUNT_MAX 100 -int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version) +int media_svc_check_table_exist(sqlite3 *handle, bool *exist) { - sqlite3 *db_handle = (sqlite3 *)handle; - - return _media_svc_get_user_version(db_handle, user_version); + return _media_svc_check_table_exist(handle, exist); } int media_svc_create_table(uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - char *sql = NULL; - media_svc_debug_fenter(); - ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA); + ret = _media_svc_init_table_query(); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_init_table_query fail."); goto ERROR; } /*create media table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid); + ret = _media_svc_make_table_query(DB_TABLE_MEDIA, DB_LIST_MEDIA, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create folder table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid); + ret = _media_svc_make_table_query(DB_TABLE_FOLDER, DB_LIST_FOLDER, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create playlist_map table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid); + ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST_MAP, DB_LIST_PLAYLIST_MAP, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create playlist table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid); + ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST, DB_LIST_PLAYLIST, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /* create album table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid); + ret = _media_svc_make_table_query(DB_TABLE_ALBUM, DB_LIST_ALBUM, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create tag_map table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid); + ret = _media_svc_make_table_query(DB_TABLE_TAG_MAP, DB_LIST_TAG_MAP, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create tag table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid); + ret = _media_svc_make_table_query(DB_TABLE_TAG, DB_LIST_TAG, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create bookmark table*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid); + ret = _media_svc_make_table_query(DB_TABLE_BOOKMARK, DB_LIST_BOOKMARK, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create storage table from tizen 2.4 */ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid); + ret = _media_svc_make_table_query(DB_TABLE_STORAGE, DB_LIST_STORAGE, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } /*create face table. from tizen 3.0*/ - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid); + ret = _media_svc_make_table_query(DB_TABLE_FACE_SCAN_LIST, DB_LIST_FACE_SCAN_LIST, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } - ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid); + ret = _media_svc_make_table_query(DB_TABLE_FACE, DB_LIST_FACE, uid); if (ret != MS_MEDIA_ERR_NONE) { media_svc_error("_media_svc_make_table_query fail."); goto ERROR; } - - sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER); - ret = _media_svc_sql_query(sql, uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("user_version update fail."); - goto ERROR; - } - - _media_svc_destroy_table_query(); - - media_svc_debug_fleave(); - - return MS_MEDIA_ERR_NONE; ERROR: _media_svc_destroy_table_query(); @@ -215,73 +138,26 @@ ERROR: return ret; } -int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - ms_user_storage_type_e type = -1; - - ret = ms_user_get_storage_type(uid, path, &type); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret); - - *storage_type = type; - - return ret; -} - -int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size) +int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path) { - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - - ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size); - - return ret; + return _media_svc_check_data_by_path(handle, path); } -int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path) +int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time) { - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - int count = -1; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL"); - - ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - if (count > 0) { - media_svc_debug("item is exist in database"); - return MS_MEDIA_ERR_NONE; - } else { - media_svc_debug("item is not exist in database"); - return MS_MEDIA_ERR_DB_NO_RECORD; - } - - return MS_MEDIA_ERR_NONE; + return _media_svc_get_modified_time(handle, path, modified_time); } -int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid) +int media_svc_insert_item_begin(bool with_noti, int from_pid) { - media_svc_debug("Transaction data count : [%d]", data_cnt); - - media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1"); - - g_media_svc_insert_item_data_cnt = data_cnt; - g_media_svc_insert_item_cur_data_cnt = 0; + g_media_svc_cur_data_cnt = 0; /* Prepare for making noti item list */ if (with_noti) { media_svc_debug("making noti list from pid[%d]", from_pid); - if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE) - return MS_MEDIA_ERR_OUT_OF_MEMORY; - + _media_svc_initialize_noti_list(); _media_svc_set_noti_from_pid(from_pid); - g_insert_with_noti = TRUE; + g_insert_with_noti = true; } return MS_MEDIA_ERR_NONE; @@ -293,107 +169,82 @@ int media_svc_insert_item_end(uid_t uid) media_svc_debug_fenter(); - if (g_media_svc_insert_item_cur_data_cnt > 0) { - - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid); + if (g_media_svc_cur_data_cnt > 0) { + ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid); if (g_insert_with_noti) { media_svc_debug("sending noti list"); - _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt); - _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt); - g_insert_with_noti = FALSE; + _media_svc_publish_noti_list(); + g_insert_with_noti = false; _media_svc_set_noti_from_pid(-1); } } - g_media_svc_insert_item_data_cnt = 1; - g_media_svc_insert_item_cur_data_cnt = 0; + g_media_svc_cur_data_cnt = 0; return ret; } -int media_svc_insert_item_bulk(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid) +int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, }; - media_svc_media_type_e media_type; + long long int folder_id = 0; - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); media_svc_content_info_s content_info; memset(&content_info, 0, sizeof(media_svc_content_info_s)); /*Set media info*/ - /* if drm_contentinfo is not NULL, the file is OMA DRM.*/ - ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE); + ret = _media_svc_set_media_info(&content_info, storage_id, path, false); if (ret != MS_MEDIA_ERR_NONE) return ret; - if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER - || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR) - || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD) - || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) - media_svc_debug("Do nothing[%d]", media_type); - else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) - ret = _media_svc_extract_image_metadata(db_handle, &content_info); - else - ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid); + switch (content_info.media_type) { + case MEDIA_SVC_MEDIA_TYPE_IMAGE: + ret = _media_svc_extract_image_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_VIDEO: + ret = _media_svc_extract_video_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_SOUND: + case MEDIA_SVC_MEDIA_TYPE_MUSIC: + _media_svc_extract_audio_metadata(handle, true, &content_info, uid); + break; + case MEDIA_SVC_MEDIA_TYPE_BOOK: + ret = _media_svc_extract_book_metadata(&content_info); + break; + default: + /* The 'TITLE' should always be filled in */ + content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name); + break; + } + media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); /*Set or Get folder id*/ - ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid); + ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, &folder_id, uid); media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - content_info.folder_uuid = g_strdup(folder_uuid); - media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info); - - if (g_media_svc_insert_item_data_cnt == 1) { - - ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - if (g_insert_with_noti) - _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++); - - } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) { - - ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); + content_info.folder_id = folder_id; + media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info); - if (g_insert_with_noti) - _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt); - - g_media_svc_insert_item_cur_data_cnt++; + ret = _media_svc_insert_item_stack(&content_info); + media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) { + if (g_insert_with_noti) + _media_svc_insert_item_to_noti_list(&content_info); - ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid); + /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */ + if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) { + ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid); media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); if (g_insert_with_noti) - _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt); - - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - if (g_insert_with_noti) { - _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1); - _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1); - - /* Recreate noti list */ - ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - } - - g_media_svc_insert_item_cur_data_cnt = 0; + _media_svc_publish_noti_list(); - } else { - media_svc_error("Error in media_svc_insert_item_bulk"); - _media_svc_destroy_content_info(&content_info); - return MS_MEDIA_ERR_INTERNAL; + g_media_svc_cur_data_cnt = 0; } _media_svc_destroy_content_info(&content_info); @@ -401,62 +252,67 @@ int media_svc_insert_item_bulk(MediaSvcHandle *handle, const char *storage_id, m return MS_MEDIA_ERR_NONE; } -int media_svc_insert_item_immediately(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid) +int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, }; - media_svc_media_type_e media_type; + long long int folder_id = 0; - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); media_svc_content_info_s content_info; memset(&content_info, 0, sizeof(media_svc_content_info_s)); /*Set media info*/ - ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE); + ret = _media_svc_set_media_info(&content_info, storage_id, path, false); if (ret != MS_MEDIA_ERR_NONE) return ret; - if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER - || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR) - || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD) - || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) { - /*Do nothing.*/ - } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) { - ret = _media_svc_extract_image_metadata(db_handle, &content_info); - } else { - ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid); + switch (content_info.media_type) { + case MEDIA_SVC_MEDIA_TYPE_IMAGE: + ret = _media_svc_extract_image_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_VIDEO: + ret = _media_svc_extract_video_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_SOUND: + case MEDIA_SVC_MEDIA_TYPE_MUSIC: + _media_svc_extract_audio_metadata(handle, false, &content_info, uid); + break; + case MEDIA_SVC_MEDIA_TYPE_BOOK: + ret = _media_svc_extract_book_metadata(&content_info); + break; + default: + /* The 'TITLE' should always be filled in */ + content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name); + break; } media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); /*Set or Get folder id*/ - ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid); + ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, &folder_id, uid); media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - content_info.folder_uuid = g_strdup(folder_uuid); - media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info); + content_info.folder_id = folder_id; + media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info); /* Extracting thumbnail */ if (content_info.thumbnail_path == NULL) { - if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { + if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; - ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid); + ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid); if (ret == MS_MEDIA_ERR_NONE) content_info.thumbnail_path = g_strdup(thumb_path); } } - ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid); - + ret = _media_svc_insert_item(&content_info, uid); if (ret == MS_MEDIA_ERR_NONE) { media_svc_debug("Insertion is successful. Sending noti for this"); - _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type); + _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type); } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) { media_svc_error("This item is already inserted. This may be normal operation because other process already did this"); } @@ -465,40 +321,36 @@ int media_svc_insert_item_immediately(MediaSvcHandle *handle, const char *storag return ret; } -int media_svc_move_item(MediaSvcHandle *handle, const char *src_path, const char *dest_path, uid_t uid) +int media_svc_move_item(sqlite3 *handle, + const char *src_path, + const char *dest_path, + const char *media_id, + int media_type, + const char *mime_type, + uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; char *file_name = NULL; char *folder_path = NULL; int modified_time = 0; - char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, }; + long long int folder_id = 0; char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, }; - char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, }; char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, }; - ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL; - ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL; - int media_type = -1; media_svc_debug_fenter(); - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL"); media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL"); + media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL"); + media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL"); /* Get storage_id */ - ret = _media_svc_get_storage_uuid(db_handle, src_path, org_stg_id, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - ret = _media_svc_get_storage_uuid(db_handle, dest_path, dst_stg_id, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - /* Get storage_type */ - ret = ms_user_get_storage_type(uid, src_path, &org_stg_type); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type); + ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); /*check and update folder*/ - ret = _media_svc_get_and_append_folder_id_by_path(db_handle, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid); + ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, &folder_id, uid); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); /*get filename*/ @@ -507,1077 +359,371 @@ int media_svc_move_item(MediaSvcHandle *handle, const char *src_path, const char /*get modified_time*/ modified_time = _media_svc_get_file_time(dest_path); - ret = _media_svc_get_media_type_by_path(db_handle, org_stg_id, src_path, &media_type); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("_media_svc_get_media_type_by_path failed"); - SAFE_FREE(file_name); - return ret; - } - /*get old thumbnail_path and remove thumbnail */ - ret = _media_svc_get_thumbnail_path_by_path(db_handle, src_path, old_thumb_path); + ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path); if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) { media_svc_error("_media_svc_get_thumbnail_path_by_path failed"); - SAFE_FREE(file_name); + g_free(file_name); return ret; } - if (STRING_VALID(old_thumb_path)) { - ret = _media_svc_remove_file(old_thumb_path); - if (ret != MS_MEDIA_ERR_NONE) - media_svc_error("_media_svc_remove_file failed : %d", ret); - } + _media_svc_remove_file(old_thumb_path); /*move item*/ - ret = _media_svc_update_item_by_path(org_stg_id, src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid); - SAFE_FREE(file_name); + ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dest_path, file_name, modified_time, folder_id, uid); + g_free(file_name); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); media_svc_debug("Move is successful. Sending noti for this"); - - /* Get notification info */ - media_svc_noti_item *noti_item = NULL; - ret = _media_svc_get_noti_info(db_handle, dst_stg_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - /* Send notification for move */ - _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type); - _media_svc_destroy_noti_item(noti_item); + _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type); /*update folder modified_time*/ folder_path = g_path_get_dirname(dest_path); - ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid); - SAFE_FREE(folder_path); + ret = _media_svc_update_folder_modified_time(folder_path, uid); + g_free(folder_path); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); return MS_MEDIA_ERR_NONE; } -int media_svc_set_item_validity_begin(int data_cnt) -{ - media_svc_debug("Transaction data count : [%d]", data_cnt); - - media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1"); - - g_media_svc_item_validity_data_cnt = data_cnt; - g_media_svc_item_validity_cur_data_cnt = 0; - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_set_item_validity_end(uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - - media_svc_debug_fenter(); - - if (g_media_svc_item_validity_cur_data_cnt > 0) - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid); - - g_media_svc_item_validity_data_cnt = 1; - g_media_svc_item_validity_cur_data_cnt = 0; - - return ret; -} - -int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid) +int media_svc_set_item_validity(const char *path, int validity, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - - media_svc_debug("path=[%s], validity=[%d]", path, validity); - - if (g_media_svc_item_validity_data_cnt == 1) { - - return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid); - - } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) { - - ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - g_media_svc_item_validity_cur_data_cnt++; - - } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) { - - ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); + ret = _media_svc_update_item_validity(path, validity, true, uid); + media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid); + /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */ + if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) { + ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - g_media_svc_item_validity_cur_data_cnt = 0; - - } else { - - media_svc_error("Error in media_svc_set_item_validity"); - return MS_MEDIA_ERR_INTERNAL; + g_media_svc_cur_data_cnt = 0; } - return MS_MEDIA_ERR_NONE; + return ret; } -int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid) +int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, }; + media_svc_noti_item *noti_item = NULL; media_svc_debug_fenter(); - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - - int media_type = -1; - ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type); - media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret); - /*Get thumbnail path to delete*/ - ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path); + ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path); media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret); - if (g_media_svc_insert_item_data_cnt == 1) { - - /* Get notification info */ - media_svc_noti_item *noti_item = NULL; - ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - /*Delete item*/ - ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("_media_svc_delete_item_by_path failed : %d", ret); - _media_svc_destroy_noti_item(noti_item); - - return ret; - } + /* Get notification info */ + ret = _media_svc_get_noti_info(handle, path, ¬i_item); + media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - /* Send notification */ - media_svc_debug("Deletion is successful. Sending noti for this"); - _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type); + /*Delete item*/ + ret = _media_svc_delete_item_by_path(path, uid); + if (ret != MS_MEDIA_ERR_NONE) { + media_svc_error("_media_svc_delete_item_by_path failed : %d", ret); _media_svc_destroy_noti_item(noti_item); - } else { - ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("_media_svc_delete_item_by_path failed : %d", ret); - return ret; - } + return ret; } + /* Send notification */ + media_svc_debug("Deletion is successful. Sending noti for this"); + _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type); + _media_svc_destroy_noti_item(noti_item); + /*Delete thumbnail*/ - if (STRING_VALID(thumb_path)) { - ret = _media_svc_remove_file(thumb_path); - if (ret != MS_MEDIA_ERR_NONE) - media_svc_error("fail to remove thumbnail file."); - } + _media_svc_remove_file(thumb_path); return MS_MEDIA_ERR_NONE; } -int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid) -{ - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_debug_fenter(); - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - /*Delete from DB and remove thumbnail files*/ - return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid); -} - -int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid) -{ - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid); -} - -int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid) -{ - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL"); - - if (recursive) - return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid); - else - return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid); -} - -int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid) +int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - media_svc_media_type_e media_type; char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; + media_svc_content_info_s content_info = {0, }; + media_svc_noti_item *noti_item = NULL; - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - media_svc_content_info_s content_info; - memset(&content_info, 0, sizeof(media_svc_content_info_s)); /*Set media info*/ - ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE); - if (ret != MS_MEDIA_ERR_NONE) - return ret; + ret = _media_svc_set_media_info(&content_info, storage_id, path, true); + media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); /* Initialize thumbnail information to remake thumbnail. */ - ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path); - if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) { - _media_svc_destroy_content_info(&content_info); - return ret; - } + ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path); + if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) + goto REFRESH_FINALIZE; - if (STRING_VALID(thumb_path)) { - if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) { - ret = _media_svc_remove_file(thumb_path); - if (ret != MS_MEDIA_ERR_NONE) - media_svc_error("_media_svc_remove_file failed : %s", thumb_path); - } + if (strlen(thumb_path) > 0) { + _media_svc_remove_file(thumb_path); - ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid); - if (ret != MS_MEDIA_ERR_NONE) { - _media_svc_destroy_content_info(&content_info); - return ret; - } + ret = _media_svc_update_thumbnail_path(path, NULL, uid); + if (ret != MS_MEDIA_ERR_NONE) + goto REFRESH_FINALIZE; } /* Get notification info */ - media_svc_noti_item *noti_item = NULL; - ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item); - if (ret != MS_MEDIA_ERR_NONE) { - _media_svc_destroy_content_info(&content_info); - return ret; + ret = _media_svc_get_noti_info(handle, path, ¬i_item); + if (ret != MS_MEDIA_ERR_NONE) + goto REFRESH_FINALIZE; + + content_info.media_type = noti_item->media_type; + content_info.mime_type = g_strdup(noti_item->mime_type); + + switch (content_info.media_type) { + case MEDIA_SVC_MEDIA_TYPE_IMAGE: + ret = _media_svc_extract_image_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_VIDEO: + ret = _media_svc_extract_video_metadata(&content_info); + break; + case MEDIA_SVC_MEDIA_TYPE_SOUND: + case MEDIA_SVC_MEDIA_TYPE_MUSIC: + _media_svc_extract_audio_metadata(handle, is_direct, &content_info, uid); + break; + case MEDIA_SVC_MEDIA_TYPE_BOOK: + ret = _media_svc_extract_book_metadata(&content_info); + break; + default: + /* The 'TITLE' should always be filled in */ + content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name); + break; } - media_type = noti_item->media_type; - content_info.media_type = media_type; - - if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER - || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR) - || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD) - || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) - media_svc_debug("Do nothing [%d]", media_type); - else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) - ret = _media_svc_extract_image_metadata(db_handle, &content_info); - else - ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid); - - if (ret != MS_MEDIA_ERR_NONE) { - _media_svc_destroy_noti_item(noti_item); - _media_svc_destroy_content_info(&content_info); - return ret; - } + if (ret != MS_MEDIA_ERR_NONE) + goto REFRESH_FINALIZE; /* Extracting thumbnail */ if (content_info.thumbnail_path == NULL) { - if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { + if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { memset(thumb_path, 0, sizeof(thumb_path)); - ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid); + ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid); if (ret == MS_MEDIA_ERR_NONE) content_info.thumbnail_path = g_strdup(thumb_path); } } - ret = _media_svc_update_item_with_data(storage_id, &content_info, uid); + ret = _media_svc_update_item_with_data(is_direct, &content_info, uid); if (ret == MS_MEDIA_ERR_NONE) { - media_svc_debug("Update is successful. Sending noti for this"); - _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type); + if (is_direct) { + /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */ + if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) { + ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid); + if (ret != MS_MEDIA_ERR_NONE) + goto REFRESH_FINALIZE; + + g_media_svc_cur_data_cnt = 0; + } + } else { + /* Except scanner case */ + media_svc_debug("Update is successful. Sending noti for this"); + _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type); + } } else { media_svc_error("_media_svc_update_item_with_data failed : %d", ret); } +REFRESH_FINALIZE: _media_svc_destroy_content_info(&content_info); _media_svc_destroy_noti_item(noti_item); return ret; } -int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid) +int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, int update_type, int pid) { - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - char *name = NULL; - char *name_pinyin = NULL; - bool pinyin_support = FALSE; - char *sql = NULL; - int media_type = 0; - GArray *query_array = NULL; + media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL"); - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL"); - media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL"); + return _media_svc_publish_dir_noti((media_item_update_type_e)update_type, dir_path, folder_id, pid); +} - media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path); +int media_svc_publish_update_noti(const char *path, int media_type, const char *uuid, const char *mime_type) +{ + return _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, path, media_type, uuid, mime_type); +} - ret = _media_svc_sql_begin_trans(uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); +int media_svc_set_storage_validity(const char *storage_id, int validity, uid_t uid) +{ + return _media_svc_update_storage_validity(storage_id, validity, uid); +} - /*1. Rename directory */ - /*Update Pinyin If Support Pinyin*/ - name = g_path_get_basename(dst_path); - media_svc_check_pinyin_support(&pinyin_support); - if (pinyin_support) { - media_svc_get_pinyin(name, &name_pinyin); - sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q", - MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path); - } else { - sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q", - MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path); - } +int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid) +{ + return _media_svc_get_storage_uuid(handle, path, storage_id, uid); +} - ret = media_db_request_update_db_batch(sql, uid); - SQLITE3_SAFE_FREE(sql); - SAFE_FREE(name); - SAFE_FREE(name_pinyin); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request"); - - /* 2. Update sub-dir, sub-file */ - /* Sub folder */ - sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path); - ret = media_db_request_update_db_batch(sql, uid); - SQLITE3_SAFE_FREE(sql); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request"); +int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity) +{ + return _media_svc_check_storage(handle, storage_id, storage_path, validity); +} - /* Sub files */ - sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path); - ret = media_db_request_update_db_batch(sql, uid); - SQLITE3_SAFE_FREE(sql); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request"); +int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid) +{ + return _media_svc_update_storage_path(handle, storage_id, storage_path, uid); +} - ret = _media_svc_sql_end_trans(uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback"); - return ret; - } +int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid) +{ + int ret = MS_MEDIA_ERR_NONE; - /*3. Get and update thumbnail_path if exists */ - sqlite3_stmt *sql_stmt = NULL; + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); + media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL"); - sql = sqlite3_mprintf("SELECT path, thumbnail_path, media_type from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path); - media_svc_debug("[SQL query] : %s", sql); + ret = _media_svc_append_storage(storage_id, storage_path, uid); + media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret); - ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret); + /* Remove external storage that validity is 0 */ + ret = _media_svc_delete_invalid_storage(handle, uid); + media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret); + return ret; +} - query_array = g_array_new(FALSE, FALSE, sizeof(char *)); - media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed"); +int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid) +{ + media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); + media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - while (sqlite3_step(sql_stmt) == SQLITE_ROW) { - char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; - char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; - char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; - char *thumb_query = NULL; + return _media_svc_append_by_folder_path(handle, storage_id, path, uid); +} - if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) { - SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path)); - } else { - media_svc_error("media path is NULL"); - SQLITE3_FINALIZE(sql_stmt); - return MS_MEDIA_ERR_DB_INTERNAL; - } +int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid) +{ + return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid); +} - if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) { - SAFE_STRLCPY(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path)); - } else { - media_svc_error("thumbnail path is NULL"); - SQLITE3_FINALIZE(sql_stmt); - return MS_MEDIA_ERR_DB_INTERNAL; - } - - media_type = sqlite3_column_int(sql_stmt, 2); - - ret = _media_svc_get_thumbnail_path(media_type, media_new_thumb_path, media_path, THUMB_EXT, uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret); - SQLITE3_FINALIZE(sql_stmt); - return ret; - } - - thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path); - g_array_append_val(query_array, thumb_query); - - ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path); - if (ret != MS_MEDIA_ERR_NONE) - media_svc_error("_media_svc_rename_file failed : %d", ret); - } - - SQLITE3_FINALIZE(sql_stmt); - - while (query_array->len != 0) { - sql = g_array_index(query_array , char*, 0); - g_array_remove_index(query_array, 0); - ret = media_db_request_update_db(sql, uid); - SQLITE3_SAFE_FREE(sql); - if (ret != MS_MEDIA_ERR_NONE) - media_svc_error("media_db_request_update_db failed : %d", ret); - } - - g_array_free(query_array, FALSE); - query_array = NULL; - - media_svc_debug("Folder update is successful. Sending noti for this"); - /* Get notification info */ - media_svc_noti_item *noti_item = NULL; - ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL); - _media_svc_destroy_noti_item(noti_item); - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_request_update_db(const char *db_query, uid_t uid) +int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path) { - media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL"); - - return _media_svc_sql_query(db_query, uid); -} - -int media_svc_send_dir_update_noti(MediaSvcHandle *handle, const char *storage_id, const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - char *uuid = NULL; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL"); - - /* Get notification info */ - media_svc_noti_item *noti_item = NULL; - ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - if (folder_id != NULL) { - uuid = strndup(folder_id, strlen(folder_id)); - } else { - if (noti_item->media_uuid != NULL) { - uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid)); - } else { - _media_svc_destroy_noti_item(noti_item); - media_svc_error("folder uuid is wrong"); - return MS_MEDIA_ERR_DB_INTERNAL; - } - } - - ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid); - _media_svc_destroy_noti_item(noti_item); - SAFE_FREE(uuid); - - return ret; + return _media_svc_check_folder_by_path(handle, folder_path); } -int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid) +int media_svc_append_query(const char *query, uid_t uid) { - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_debug_fenter(); - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - return _media_svc_check_db_upgrade(db_handle, user_version, uid); + return _media_svc_append_query_list(query, uid); } -int media_svc_update_item_begin(int data_cnt) +int media_svc_send_query(uid_t uid) { - media_svc_debug("Transaction data count : [%d]", data_cnt); - - media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1"); - - g_media_svc_update_item_data_cnt = data_cnt; - g_media_svc_update_item_cur_data_cnt = 0; - - return MS_MEDIA_ERR_NONE; + return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid); } -int media_svc_update_item_end(uid_t uid) +int media_svc_get_media_type(const char *path, int *mediatype) { - int ret = MS_MEDIA_ERR_NONE; - - media_svc_debug_fenter(); - - if (g_media_svc_update_item_cur_data_cnt > 0) - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid); - - g_media_svc_update_item_data_cnt = 1; - g_media_svc_update_item_cur_data_cnt = 0; - - return ret; + return _media_svc_get_media_type(path, mediatype); } -int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid) +int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - media_svc_media_type_e media_type; - media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - media_svc_content_info_s content_info; - memset(&content_info, 0, sizeof(media_svc_content_info_s)); - - /*Set media info*/ - ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE); - if (ret != MS_MEDIA_ERR_NONE) - return ret; + char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, }; + char *sql = NULL; - if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) - ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type); - else { - _media_svc_destroy_content_info(&content_info); - return MS_MEDIA_ERR_NONE; - } + // 1. Check media type + if (media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE && media_type != MEDIA_SVC_MEDIA_TYPE_VIDEO) + return MS_MEDIA_ERR_THUMB_UNSUPPORTED; + // 2. try to create thumbnail + ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid); if (ret != MS_MEDIA_ERR_NONE) { - _media_svc_destroy_content_info(&content_info); - return ret; + media_svc_error("Failed to create thumbnail [%d]", ret); + if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED) + return ret; } - if (g_media_svc_update_item_data_cnt == 1) { - - ret = _media_svc_update_meta_with_data(&content_info); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) { - - ret = _media_svc_update_meta_with_data(&content_info); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - g_media_svc_update_item_cur_data_cnt++; - - } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) { - - ret = _media_svc_update_meta_with_data(&content_info); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info); - - g_media_svc_update_item_cur_data_cnt = 0; + // 3. Update creation result to media db + sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path); + ret = _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); + if (ret != MS_MEDIA_ERR_NONE) { + media_svc_error("Failed to update media db [%d]", ret); + *thumbnail_path = g_strdup(""); } else { - media_svc_error("Error in media_svc_update_item_meta"); - _media_svc_destroy_content_info(&content_info); - return MS_MEDIA_ERR_INTERNAL; + *thumbnail_path = g_strdup(thumb_path); } - _media_svc_destroy_content_info(&content_info); - - return ret; -} - -int media_svc_publish_noti(media_item_type_e update_item, media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type) -{ - return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type); -} - -int media_svc_get_pinyin(const char *src_str, char **pinyin_str) -{ - media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL"); - - return _media_svc_get_pinyin_str(src_str, pinyin_str); -} - -int media_svc_check_pinyin_support(bool *support) -{ - *support = _media_svc_check_pinyin_support(); - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - ret = _media_svc_update_storage_validity(storage_id, validity, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret); - - ret = _media_svc_update_media_view(db_handle, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret); - - return ret; -} - -int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - - ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid); - return ret; } -int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL"); - - ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path); - - return ret; -} - -int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int *count) -{ - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_debug_fenter(); - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL"); - - return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, count); -} - -static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info) -{ - media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - - if (content_info->added_time > 0) - new_content_info->added_time = content_info->added_time; - new_content_info->last_played_time = content_info->last_played_time; - new_content_info->played_count = content_info->played_count; - new_content_info->favourate = content_info->favourate; - - /* Can be NULL if user not to set display_name using media_info_set_displayname().. */ - if (STRING_VALID(content_info->file_name)) { - /* Already filled in _media_svc_set_media_info() */ - SAFE_FREE(new_content_info->file_name); - new_content_info->file_name = g_strdup(content_info->file_name); - } - new_content_info->media_meta.title = g_strdup(content_info->media_meta.title); - new_content_info->media_meta.album = g_strdup(content_info->media_meta.album); - new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist); - new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre); - new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer); - new_content_info->media_meta.year = g_strdup(content_info->media_meta.year); - new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date); - new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright); - new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num); - new_content_info->media_meta.description = g_strdup(content_info->media_meta.description); - new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather); - new_content_info->media_meta.category = g_strdup(content_info->media_meta.category); - new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword); - new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag); - new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name); - new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating); - new_content_info->media_meta.author = g_strdup(content_info->media_meta.author); - new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider); - - if (STRING_VALID(content_info->media_meta.datetaken)) { - new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken); - - new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken); - if (new_content_info->timeline == 0) { - media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken); - new_content_info->timeline = new_content_info->modified_time; - } else { - media_svc_debug("Timeline : %ld", new_content_info->timeline); - } - } - - new_content_info->media_meta.is_360 = content_info->media_meta.is_360; - /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */ - /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */ - /* new_content_info->media_meta.channel = content_info->media_meta.channel; */ - /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */ - - if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE) - new_content_info->media_meta.longitude = content_info->media_meta.longitude; - if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE) - new_content_info->media_meta.latitude = content_info->media_meta.latitude; - if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE) - new_content_info->media_meta.altitude = content_info->media_meta.altitude; - - new_content_info->media_meta.rating = content_info->media_meta.rating; - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid) +static int __media_svc_get_ebook_search_type(void) { - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, }; - bool append_album = FALSE; - - /* Checking parameters if they are valid */ - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL"); - media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL"); - - media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type); - - media_svc_content_info_s _new_content_info; - memset(&_new_content_info, 0, sizeof(media_svc_content_info_s)); - - media_svc_media_type_e media_type; - - ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info"); - - if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) - media_svc_debug("Do nothing[%d]", media_type); - else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) - ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info); - else - ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid); - - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info); - - /* Extracting thumbnail */ - if (_new_content_info.thumbnail_path == NULL) { - if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { - char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, }; + dictionary *dict = NULL; + static int _ebook_search_type = -1; - ret = _media_svc_create_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), media_type, uid); - if (ret == MS_MEDIA_ERR_NONE) - _new_content_info.thumbnail_path = g_strdup(thumb_path); + if (_ebook_search_type == -1) { + dict = iniparser_load(CONTENT_INI_DEFAULT_PATH); + if (!dict) { + media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH); + return MEDIA_SVC_SEARCH_TYPE_DIRECT; } - } - - /* set othere data to the structure, which is passed as parameters */ - __media_svc_copy_para_to_content(content_info, &_new_content_info); - - /* Set or Get folder id */ - ret = _media_svc_get_and_append_folder_id_by_path(handle, _new_content_info.storage_uuid, _new_content_info.path, _new_content_info.storage_type, folder_uuid, uid); - media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info); - - _new_content_info.folder_uuid = g_strdup(folder_uuid); - media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info); - - /* register album table data */ - - int album_id = 0; - if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) { - ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id); - - if (ret != MS_MEDIA_ERR_NONE) { - if (ret == MS_MEDIA_ERR_DB_NO_RECORD) { - media_svc_debug("album does not exist. So start to make album art"); - append_album = TRUE; - } else { - media_svc_debug("other error"); - append_album = FALSE; - } - } else { - _new_content_info.album_id = album_id; - append_album = FALSE; - - if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) || - (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) { - media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown."); - } else { - - media_svc_debug("album already exists. don't need to make album art"); - ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path); - media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info); - } - } - } - - if (append_album == TRUE) { - if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) && - (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) - ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, _new_content_info.thumbnail_path, &album_id, uid); - else - ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid); - - _new_content_info.album_id = album_id; - } - - /* Insert to db - calling _media_svc_insert_item_with_data */ - ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid); - - if (ret == MS_MEDIA_ERR_NONE) - media_svc_debug("Insertion is successful."); - - if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) - media_svc_error("This item is already inserted. This may be normal operation because other process already did this"); - - _media_svc_destroy_content_info(&_new_content_info); - - /* handling returned value - important */ - return ret; -} - -void media_svc_destroy_content_info(media_svc_content_info_s *content_info) -{ - _media_svc_destroy_content_info(content_info); -} - -int media_svc_generate_uuid(char **uuid) -{ - char *gen_uuid = NULL; - gen_uuid = _media_info_generate_uuid(); - media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid"); - - *uuid = strdup(gen_uuid); - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid) -{ - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL"); - media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL"); - - return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid); -} - -int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid) -{ - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL"); - - return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid); -} - -int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, media_svc_storage_type_e storage_type, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 *db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret); - - ret = _media_svc_create_media_table_with_id(storage_id, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret); - - ret = _media_svc_update_media_view(db_handle, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret); - - /* Remove external storage that validity is 0 */ - ret = _media_svc_delete_invalid_storage(db_handle, storage_type, uid); - media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret); - - return ret; -} - -int media_svc_insert_folder_begin(int data_cnt) -{ - media_svc_debug("Transaction data count : [%d]", data_cnt); - - media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1"); - - g_media_svc_insert_folder_data_cnt = data_cnt; - g_media_svc_insert_folder_cur_data_cnt = 0; - - return MS_MEDIA_ERR_NONE; -} - -int media_svc_insert_folder_end(uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - - media_svc_debug_fenter(); - - if (g_media_svc_insert_folder_cur_data_cnt > 0) - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid); - - g_media_svc_insert_folder_data_cnt = 1; - g_media_svc_insert_folder_cur_data_cnt = 0; - - return ret; -} - -int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,}; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type"); - - if (g_media_svc_insert_folder_data_cnt == 1) { - - ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid); - } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) { + _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0); + media_svc_debug("ebook_search_type [%d]", _ebook_search_type); - ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - g_media_svc_insert_folder_cur_data_cnt++; - - } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) { - - ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - g_media_svc_insert_folder_cur_data_cnt = 0; - - } else { - media_svc_error("Error in media_svc_set_insert_folder"); - return MS_MEDIA_ERR_INTERNAL; + iniparser_freedict(dict); } - return ret; -} - -int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - - ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid); - - return ret; + return _ebook_search_type; } -int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid) +int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result) { int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; + GList *item_list = NULL; + GList *iter = NULL; + char *query = NULL; - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); + media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL"); + media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL"); + media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL"); - ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid); + query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;", + DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK); - return ret; -} + ret = _media_svc_get_media(handle, query, &item_list); + media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed"); -int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - int count = -1; + if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) { + for (iter = item_list; iter; iter = g_list_next(iter)) + _media_svc_update_wordbook((char *)iter->data, uid); - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL"); - media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL"); + _media_svc_clean_wordbook(uid); - ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - - if (count > 0) { - media_svc_debug("item is exist in database"); - return MS_MEDIA_ERR_NONE; + if (!_media_svc_get_matched_list(keyword, uid, result)) + media_svc_error("_media_svc_get_matched_list failed"); } else { - media_svc_debug("item is not exist in database"); - return MS_MEDIA_ERR_DB_NO_RECORD; + for (iter = item_list; iter; iter = g_list_next(iter)) { + if (_media_svc_is_keyword_included((char *)iter->data, keyword)) + *result = g_list_append(*result, g_strdup((gchar *)iter->data)); + } } - return MS_MEDIA_ERR_NONE; -} - -int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3 * db_handle = (sqlite3 *)handle; - - media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); - media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - - ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id); - - return ret; -} - -int media_svc_append_query(const char *query, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - - media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL"); - - ret = _media_svc_append_query_list(query, uid); + g_list_free_full(item_list, g_free); return ret; } -int media_svc_send_query(uid_t uid) +int media_svc_check_db(sqlite3 *handle, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; + bool exist = false; - ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid); - - return ret; -} - -int media_svc_get_media_type(const char *path, int *mediatype) -{ - media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - - return _media_svc_get_media_type(path, mediatype); -} - -int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path) -{ - int ret = MS_MEDIA_ERR_NONE; - char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, }; - char *sql = NULL; - - // 1. Check media type - if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO) - return MS_MEDIA_ERR_UNSUPPORTED_CONTENT; - - // 2. try to create thumbnail - ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("Failed to create thumbnail [%d]", ret); - if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT) - return ret; - } - - // 3. Update creation result to media db - sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q';", storage_id, thumb_path, file_path); + ret = media_svc_check_table_exist(handle, &exist); + if (ret != MS_MEDIA_ERR_NONE) + return ret; - ret = _media_svc_sql_query(sql, uid); - SQLITE3_SAFE_FREE(sql); - if (ret != MS_MEDIA_ERR_NONE) { - media_svc_error("Failed to update media db [%d]", ret); - *thumbnail_path = g_strdup(""); - } else { - *thumbnail_path = g_strdup(thumb_path); - } + if (!exist) + ret = media_svc_create_table(uid); return ret; -} +} \ No newline at end of file