Fix title being inserted as an empty string
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc.c
index 4afa99d..088d441 100755 (executable)
@@ -3,8 +3,6 @@
  *
  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
- *
  * 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,9 +17,6 @@
  *
  */
 
-#include <string.h>
-#include <errno.h>
-#include "media-svc.h"
 #include "media-svc-media.h"
 #include "media-svc-debug.h"
 #include "media-svc-util.h"
 #include "media-svc-noti.h"
 #include "media-svc-storage.h"
 
-static __thread int g_media_svc_data_cnt = 0;
+#include <iniparser.h>
+
+#define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
+
+//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 bool g_insert_with_noti = false;
 
-#define BATCH_REQUEST_MAX 300
+#define BATCH_ITEM_COUNT_MAX 100
 
-int media_svc_get_user_version(sqlite3 *handle, int *user_version)
+int media_svc_check_table_exist(sqlite3 *handle, bool *exist)
 {
-       return _media_svc_get_user_version(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_DB_VERSION);
-       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();
 
@@ -156,21 +140,7 @@ ERROR:
 
 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
 {
-       int ret = MS_MEDIA_ERR_NONE;
-       int count = -1;
-
-       ret = _media_svc_count_record_with_path(handle, 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_check_data_by_path(handle, path);
 }
 
 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
@@ -178,11 +148,8 @@ int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const c
        return _media_svc_get_modified_time(handle, path, modified_time);
 }
 
-int media_svc_insert_item_begin(int data_cnt, bool with_noti, int from_pid)
+int media_svc_insert_item_begin(bool with_noti, int from_pid)
 {
-       media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
-
-       g_media_svc_data_cnt = data_cnt;
        g_media_svc_cur_data_cnt = 0;
 
        /* Prepare for making noti item list */
@@ -207,34 +174,29 @@ int media_svc_insert_item_end(uid_t uid)
                if (g_insert_with_noti) {
                        media_svc_debug("sending noti list");
                        _media_svc_publish_noti_list();
-                       _media_svc_initialize_noti_list();
                        g_insert_with_noti = false;
                        _media_svc_set_noti_from_pid(-1);
                }
        }
 
-       g_media_svc_data_cnt = 0;
        g_media_svc_cur_data_cnt = 0;
 
        return ret;
 }
 
-int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, 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;
-       char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
+       long long int folder_id = 0;
 
        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_is_valid_storage_type(storage_type), 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));
+       media_svc_content_info_s content_info = { 0, };
 
        /*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, false);
+       ret = _media_svc_set_media_info(&content_info, storage_id, path, false);
        if (ret != MS_MEDIA_ERR_NONE)
                return ret;
 
@@ -243,38 +205,43 @@ int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_
                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:
-               ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
+               _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:
-               media_svc_debug("Do nothing[%d]", content_info.media_type);
+               /* 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(handle, true, 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);
+       content_info.folder_id = folder_id;
+       media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);
 
-       ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
+       ret = _media_svc_insert_item_stack(&content_info);
        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);
 
-       if (++g_media_svc_cur_data_cnt == g_media_svc_data_cnt) {
+       /* 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) {
+               if (g_insert_with_noti)
                        _media_svc_publish_noti_list();
-                       _media_svc_initialize_noti_list();
-               }
 
                g_media_svc_cur_data_cnt = 0;
        }
@@ -284,21 +251,19 @@ int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_
        return MS_MEDIA_ERR_NONE;
 }
 
-int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_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;
-       char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
+       long long int folder_id = 0;
 
        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_is_valid_storage_type(storage_type), 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));
+       media_svc_content_info_s content_info = { 0, };
 
        /*Set media info*/
-       ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
+       ret = _media_svc_set_media_info(&content_info, storage_id, path, false);
        if (ret != MS_MEDIA_ERR_NONE)
                return ret;
 
@@ -307,23 +272,29 @@ int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, m
                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:
-               ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
+               _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:
-               media_svc_debug("Do nothing[%d]", content_info.media_type);
+               /* 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(handle, false, 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) {
@@ -336,8 +307,7 @@ int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, m
                }
        }
 
-       ret = _media_svc_insert_item_with_data(false, &content_info, 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_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
@@ -350,22 +320,20 @@ int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, m
 }
 
 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)
+                                               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;
        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 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;
 
        media_svc_debug_fenter();
 
@@ -378,14 +346,9 @@ int media_svc_move_item(sqlite3 *handle,
        /* Get storage_id */
        ret = _media_svc_get_storage_uuid(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);
-       media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
 
        /*check and update folder*/
-       ret = _media_svc_get_and_append_folder_id_by_path(handle, false, 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*/
@@ -398,15 +361,15 @@ int media_svc_move_item(sqlite3 *handle,
        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;
        }
 
        _media_svc_remove_file(old_thumb_path);
 
        /*move item*/
-       ret = _media_svc_update_item_by_path(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");
@@ -414,8 +377,8 @@ int media_svc_move_item(sqlite3 *handle,
 
        /*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;
@@ -428,7 +391,8 @@ int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
        ret = _media_svc_update_item_validity(path, validity, true, uid);
        media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
 
-       if (++g_media_svc_cur_data_cnt == g_media_svc_data_cnt) {
+       /* 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);
 
@@ -474,48 +438,37 @@ int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const
        return MS_MEDIA_ERR_NONE;
 }
 
-int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, ms_user_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;
        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(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_is_valid_storage_type(storage_type), 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, true);
-       if (ret != MS_MEDIA_ERR_NONE)
-               return ret;
+       ret = _media_svc_set_media_info(&content_info, NULL, 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(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;
-       }
+       if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
+               goto REFRESH_FINALIZE;
 
-       if (STRING_VALID(thumb_path)) {
+       if (strlen(thumb_path) > 0) {
                _media_svc_remove_file(thumb_path);
 
                ret = _media_svc_update_thumbnail_path(path, NULL, uid);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       _media_svc_destroy_content_info(&content_info);
-                       return ret;
-               }
+               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(handle, path, &noti_item);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               _media_svc_destroy_content_info(&content_info);
-               return ret;
-       }
+       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);
@@ -525,20 +478,23 @@ int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_
                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:
-               ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
+               _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:
-               media_svc_debug("Do nothing[%d]", content_info.media_type);
+               /* The 'TITLE' should always be filled in */
+               content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name);
                break;
        }
 
-       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) {
@@ -554,138 +510,46 @@ int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_
        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_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_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
+int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, int update_type, int pid)
 {
        media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
 
-       return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
+       return _media_svc_publish_dir_noti((media_item_update_type_e)update_type, dir_path, folder_id, pid);
 }
 
-int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
+int media_svc_publish_update_noti(const char *path, int media_type, const char *uuid, const char *mime_type)
 {
-       media_svc_debug_fenter();
-
-       return _media_svc_check_db_upgrade(handle, user_version, uid);
+       return _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, path, media_type, uuid, mime_type);
 }
 
-static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
+int media_svc_set_storage_validity(const char *storage_id, int validity, uid_t uid)
 {
-       int ret = MS_MEDIA_ERR_NONE;
-       char *root_path = NULL;
-       GPtrArray *path_list = NULL;
-       int i = 0;
-
-       ret = ms_user_get_internal_root_path(uid, &root_path);
-       media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
-
-       ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
-       if (ret != MS_MEDIA_ERR_NONE)
-               media_svc_error("Fail to send noti");
-
-       SAFE_FREE(root_path);
-
-       path_list = g_ptr_array_new_with_free_func(g_free);
-       _media_svc_get_storage_path(handle, &path_list);
-
-       for (i = 0; i < path_list->len; i++) {
-               root_path = g_ptr_array_index(path_list, i);
-
-               ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
-               if (ret != MS_MEDIA_ERR_NONE)
-                       media_svc_error("Fail to send noti");
-       }
-
-       g_ptr_array_free(path_list, TRUE);
-}
-
-int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
-{
-       int ret = MS_MEDIA_ERR_NONE;
-       int i = 0;
-       char *sql = NULL;
-       char *file_path = NULL;
-       media_svc_content_info_s content_info;
-       GPtrArray *path_list = NULL;
-
-       path_list = g_ptr_array_new_with_free_func(g_free);
-       media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
-
-       sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", MEDIA_SVC_DB_TABLE_MEDIA);
-       ret = _media_svc_get_media(handle, sql, &path_list);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("Fail to get media list");
-               g_ptr_array_free(path_list, TRUE);
-               return ret;
-       }
-
-       for (i = 0; i < path_list->len; i++) {
-               file_path = g_ptr_array_index(path_list, i);
-
-               memset(&content_info, 0, sizeof(media_svc_content_info_s));
-               ret = _media_svc_extract_music_metadata_for_update(&content_info, file_path);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("Fail to extract metadata");
-                       _media_svc_destroy_content_info(&content_info);
-                       continue;
-               }
-
-               ret = _media_svc_update_meta_with_data(&content_info);
-               if (ret != MS_MEDIA_ERR_NONE)
-                       media_svc_error("Fail to append item[%s]", content_info.path);
-
-               _media_svc_destroy_content_info(&content_info);
-       }
-
-       g_ptr_array_free(path_list, TRUE);
-
-       ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
-       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
-
-       /* Noti for this */
-       __media_svc_noti_all_storage(handle, uid);
-
-       return ret;
-}
-
-int media_svc_publish_noti(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_type, path, media_type, uuid, mime_type);
-}
-
-int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
-{
-       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(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
-{
-       int ret = MS_MEDIA_ERR_NONE;
-
-       media_svc_retvm_if(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);
-
-       return ret;
+       return _media_svc_update_storage_validity(storage_id, validity, uid);
 }
 
 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
@@ -693,17 +557,6 @@ int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id
        return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
 }
 
-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(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
 {
        return _media_svc_check_storage(handle, storage_id, storage_path, validity);
@@ -714,16 +567,15 @@ int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char
        return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
 }
 
-int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
+int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
 
        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");
-       media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
 
-       ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
+       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);
 
        /* Remove external storage that validity is 0 */
@@ -733,38 +585,23 @@ int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char
        return ret;
 }
 
-int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
+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");
-       media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
 
-       return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
+       return _media_svc_append_by_folder_path(handle, storage_id, path, uid);
 }
 
-int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
+int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
 {
-       return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
+       return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
 }
 
-int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
+int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
 {
-       int ret = MS_MEDIA_ERR_NONE;
-       int count = -1;
-
-       ret = _media_svc_count_folder_with_path(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;
-       } 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_check_folder_by_path(handle, folder_path);
 }
 
 int media_svc_append_query(const char *query, uid_t uid)
@@ -789,19 +626,19 @@ int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid,
        char *sql = NULL;
 
        // 1. Check media type
-       if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
-               return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
+       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_error("Failed to create thumbnail [%d]", ret);
-               if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
+               if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
                        return ret;
        }
 
        // 3. Update creation result to media db
-       sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
+       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);
@@ -814,3 +651,76 @@ int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid,
 
        return ret;
 }
+
+static int __media_svc_get_ebook_search_type(void)
+{
+       dictionary *dict = NULL;
+       static int _ebook_search_type = -1;
+
+       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;
+               }
+
+               _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
+               media_svc_debug("ebook_search_type [%d]", _ebook_search_type);
+
+               iniparser_freedict(dict);
+       }
+
+       return _ebook_search_type;
+}
+
+int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       GList *item_list = NULL;
+       GList *iter = NULL;
+       char *query = 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");
+
+       query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;",
+                                                       DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK);
+
+       ret = _media_svc_get_media(handle, query, &item_list);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
+
+       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_clean_wordbook(uid);
+
+               if (!_media_svc_get_matched_list(keyword, uid, result))
+                       media_svc_error("_media_svc_get_matched_list failed");
+       } else {
+               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));
+               }
+       }
+
+       g_list_free_full(item_list, g_free);
+
+       return ret;
+}
+
+int media_svc_check_db(sqlite3 *handle, uid_t uid)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       bool exist = false;
+
+       ret = media_svc_check_table_exist(handle, &exist);
+       if (ret != MS_MEDIA_ERR_NONE)
+               return ret;
+
+       if (!exist)
+               ret = media_svc_create_table(uid);
+
+       return ret;
+}
\ No newline at end of file