4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "media-svc-media.h"
21 #include "media-svc-debug.h"
22 #include "media-svc-util.h"
23 #include "media-svc-db-utils.h"
24 #include "media-svc-env.h"
25 #include "media-svc-media-folder.h"
26 #include "media-svc-album.h"
27 #include "media-svc-noti.h"
28 #include "media-svc-storage.h"
30 #include <iniparser.h>
32 #define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
34 static __thread int g_media_svc_cur_data_cnt = 0;
36 /* Flag for items to be published by notification */
37 static __thread bool g_insert_with_noti = false;
39 #define BATCH_ITEM_COUNT_MAX 100
41 int media_svc_check_table_exist(sqlite3 *handle, bool *exist)
43 return _media_svc_check_table_exist(handle, exist);
46 int media_svc_create_table(uid_t uid)
48 return _media_svc_create_table(uid);
51 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *path)
53 return _media_svc_check_data_by_path(handle, path);
56 int media_svc_get_modified_time(sqlite3 *handle, const char *path, int *modified_time)
58 return _media_svc_get_modified_time(handle, path, modified_time);
61 int media_svc_insert_item_begin(bool with_noti, int from_pid)
63 g_media_svc_cur_data_cnt = 0;
65 /* Prepare for making noti item list */
67 media_svc_debug("making noti list from pid[%d]", from_pid);
68 _media_svc_initialize_noti_list();
69 _media_svc_set_noti_from_pid(from_pid);
70 g_insert_with_noti = true;
73 return MS_MEDIA_ERR_NONE;
76 int media_svc_insert_item_end(uid_t uid)
78 int ret = MS_MEDIA_ERR_NONE;
80 media_svc_debug_fenter();
82 if (g_media_svc_cur_data_cnt > 0) {
83 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
84 if (g_insert_with_noti) {
85 media_svc_debug("sending noti list");
86 _media_svc_publish_noti_list();
87 g_insert_with_noti = false;
88 _media_svc_set_noti_from_pid(-1);
92 g_media_svc_cur_data_cnt = 0;
97 static int __media_svc_set_info(sqlite3 *handle,
98 const char *storage_id,
102 media_svc_content_info_s *info)
104 int ret = MS_MEDIA_ERR_NONE;
106 media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "handle is NULL");
109 ret = _media_svc_set_media_info(info, storage_id, path, false);
110 if (ret != MS_MEDIA_ERR_NONE)
113 switch (info->media_type) {
114 case MEDIA_SVC_MEDIA_TYPE_IMAGE:
115 ret = _media_svc_extract_image_metadata(info);
117 case MEDIA_SVC_MEDIA_TYPE_VIDEO:
118 _media_svc_extract_video_metadata(info);
120 case MEDIA_SVC_MEDIA_TYPE_MUSIC:
121 _media_svc_extract_audio_metadata(handle, is_direct, info, uid);
123 case MEDIA_SVC_MEDIA_TYPE_BOOK:
124 ret = _media_svc_extract_book_metadata(info);
127 /* The 'TITLE' should always be filled in */
128 info->media_meta.title = _media_svc_get_title_from_filename(info->file_name);
132 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, info);
134 /*Set or Get folder id*/
135 ret = _media_svc_get_and_append_folder_id_by_path(handle, is_direct, storage_id, path, &info->folder_id, uid);
136 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, info);
137 media_svc_retv_del_if(info->folder_id <= 0, MS_MEDIA_ERR_INTERNAL, info);
142 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
144 int ret = MS_MEDIA_ERR_NONE;
145 media_svc_content_info_s content_info = { 0, };
147 ret = __media_svc_set_info(handle, storage_id, path, uid, true, &content_info);
148 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
150 ret = _media_svc_insert_item_stack(&content_info);
151 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
153 if (g_insert_with_noti)
154 _media_svc_insert_item_to_noti_list(&content_info);
156 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
157 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
158 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
159 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
161 if (g_insert_with_noti)
162 _media_svc_publish_noti_list();
164 g_media_svc_cur_data_cnt = 0;
167 _media_svc_destroy_content_info(&content_info);
169 return MS_MEDIA_ERR_NONE;
172 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
174 int ret = MS_MEDIA_ERR_NONE;
175 media_svc_content_info_s content_info = { 0, };
177 ret = __media_svc_set_info(handle, storage_id, path, uid, false, &content_info);
178 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
180 /* Extracting thumbnail */
181 if (content_info.thumbnail_path == NULL) {
182 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
183 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
185 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
186 if (ret == MS_MEDIA_ERR_NONE)
187 content_info.thumbnail_path = g_strdup(thumb_path);
191 ret = _media_svc_insert_item(&content_info, uid);
192 if (ret == MS_MEDIA_ERR_NONE) {
193 media_svc_debug("Insertion is successful. Sending noti for this");
194 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
195 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
196 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
199 _media_svc_destroy_content_info(&content_info);
203 int media_svc_move_item(sqlite3 *handle,
204 const char *src_path,
205 const char *dest_path,
206 const char *media_id,
208 const char *mime_type,
211 int ret = MS_MEDIA_ERR_NONE;
212 char *file_name = NULL;
213 int modified_time = 0;
214 long long int folder_id = 0;
215 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
216 char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
218 media_svc_debug_fenter();
220 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
221 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
222 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
223 media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
224 media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
227 ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
228 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
230 /*check and update folder*/
231 ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, &folder_id, uid);
232 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
235 file_name = g_path_get_basename(dest_path);
237 /*get modified_time*/
238 modified_time = _media_svc_get_file_time(dest_path);
240 /*get old thumbnail_path and remove thumbnail */
241 ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
242 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
243 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
248 _media_svc_remove_file(old_thumb_path);
251 ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dest_path, file_name, modified_time, folder_id, uid);
253 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
255 media_svc_debug("Move is successful. Sending noti for this");
256 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
258 return MS_MEDIA_ERR_NONE;
261 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
263 int ret = MS_MEDIA_ERR_NONE;
265 ret = _media_svc_update_item_validity(path, validity, true, uid);
266 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
268 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
269 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
270 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
271 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
273 g_media_svc_cur_data_cnt = 0;
279 int media_svc_delete_item_by_path(sqlite3 *handle, const char *path, uid_t uid)
281 int ret = MS_MEDIA_ERR_NONE;
282 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
283 media_svc_noti_item *noti_item = NULL;
285 media_svc_debug_fenter();
287 /*Get thumbnail path to delete*/
288 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
289 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
291 /* Get notification info */
292 ret = _media_svc_get_noti_info(handle, path, ¬i_item);
293 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
296 ret = _media_svc_delete_item_by_path(path, uid);
297 if (ret != MS_MEDIA_ERR_NONE) {
298 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
299 _media_svc_destroy_noti_item(noti_item);
304 /* Send notification */
305 media_svc_debug("Deletion is successful. Sending noti for this");
306 _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
307 _media_svc_destroy_noti_item(noti_item);
310 _media_svc_remove_file(thumb_path);
312 return MS_MEDIA_ERR_NONE;
315 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *path, uid_t uid)
317 int ret = MS_MEDIA_ERR_NONE;
318 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
319 media_svc_content_info_s info = {0, };
320 media_svc_noti_item *noti = NULL;
322 media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
323 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
326 ret = _media_svc_set_media_info(&info, NULL, path, true);
327 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
329 /* Initialize thumbnail information to remake thumbnail. */
330 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
331 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
332 goto REFRESH_FINALIZE;
334 if (strlen(thumb_path) > 0) {
335 _media_svc_remove_file(thumb_path);
337 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
338 if (ret != MS_MEDIA_ERR_NONE)
339 goto REFRESH_FINALIZE;
342 switch (info.media_type) {
343 case MEDIA_SVC_MEDIA_TYPE_IMAGE:
344 ret = _media_svc_extract_image_metadata(&info);
346 case MEDIA_SVC_MEDIA_TYPE_VIDEO:
347 _media_svc_extract_video_metadata(&info);
349 case MEDIA_SVC_MEDIA_TYPE_MUSIC:
350 _media_svc_extract_audio_metadata(handle, is_direct, &info, uid);
352 case MEDIA_SVC_MEDIA_TYPE_BOOK:
353 ret = _media_svc_extract_book_metadata(&info);
356 /* The 'TITLE' should always be filled in */
357 info.media_meta.title = _media_svc_get_title_from_filename(info.file_name);
361 if (ret != MS_MEDIA_ERR_NONE)
362 goto REFRESH_FINALIZE;
364 /* Extracting thumbnail */
365 if (info.thumbnail_path == NULL) {
366 if (info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
367 memset(thumb_path, 0, sizeof(thumb_path));
369 ret = _media_svc_create_thumbnail(info.path, thumb_path, info.media_type, uid);
370 if (ret == MS_MEDIA_ERR_NONE)
371 info.thumbnail_path = g_strdup(thumb_path);
375 ret = _media_svc_update_item_with_data(is_direct, &info, uid);
377 if (ret == MS_MEDIA_ERR_NONE) {
379 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
380 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
381 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
382 if (ret != MS_MEDIA_ERR_NONE)
383 goto REFRESH_FINALIZE;
385 g_media_svc_cur_data_cnt = 0;
388 media_svc_debug("Update is successful. Sending noti for this");
389 ret = _media_svc_get_noti_info(handle, path, ¬i);
390 if (ret != MS_MEDIA_ERR_NONE)
391 goto REFRESH_FINALIZE;
393 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, info.path, info.media_type, noti->media_uuid, info.mime_type);
396 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
400 _media_svc_destroy_content_info(&info);
401 _media_svc_destroy_noti_item(noti);
406 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, int update_type, int pid)
408 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
410 return _media_svc_publish_dir_noti((media_item_update_type_e)update_type, dir_path, folder_id, pid);
413 int media_svc_publish_update_noti(const char *path, int media_type, const char *uuid, const char *mime_type)
415 return _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, path, media_type, uuid, mime_type);
418 int media_svc_set_storage_validity(const char *storage_id, int validity, uid_t uid)
420 return _media_svc_update_storage_validity(storage_id, validity, uid);
423 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
425 return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
428 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
430 return _media_svc_check_storage(handle, storage_id, storage_path, validity);
433 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
435 return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
438 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
440 int ret = MS_MEDIA_ERR_NONE;
442 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
443 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
444 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
446 ret = _media_svc_append_storage(storage_id, storage_path, uid);
447 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
449 /* Remove external storage that validity is 0 */
450 ret = _media_svc_delete_invalid_storage(handle, uid);
451 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
456 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
458 media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
459 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
460 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
462 return _media_svc_append_by_folder_path(handle, storage_id, path, uid);
465 int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
467 return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
470 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
472 return _media_svc_check_folder_by_path(handle, folder_path);
475 int media_svc_append_query(const char *query, uid_t uid)
477 return _media_svc_append_query_list(query, uid);
480 int media_svc_send_query(uid_t uid)
482 return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
485 int media_svc_get_media_type(const char *path, int *mediatype)
487 return _media_svc_get_media_type(path, mediatype);
490 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
492 int ret = MS_MEDIA_ERR_NONE;
493 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
494 sql_autoptr q = NULL;
496 // 1. Check media type
497 if (media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE && media_type != MEDIA_SVC_MEDIA_TYPE_VIDEO)
498 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
500 // 2. try to create thumbnail
501 ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
502 if (ret != MS_MEDIA_ERR_NONE) {
503 media_svc_error("Failed to create thumbnail [%d]", ret);
504 if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
508 // 3. Update creation result to media db
509 q = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path=%Q WHERE media_path=%Q", DB_TABLE_MEDIA, thumb_path, file_path);
511 ret = _media_svc_sql_query(q, uid);
512 if (ret != MS_MEDIA_ERR_NONE) {
513 media_svc_error("Failed to update media db [%d]", ret);
514 *thumbnail_path = g_strdup("");
516 *thumbnail_path = g_strdup(thumb_path);
522 static int __media_svc_get_ebook_search_type(void)
524 dictionary *dict = NULL;
525 static int _ebook_search_type = -1;
527 if (_ebook_search_type == -1) {
528 dict = iniparser_load(CONTENT_INI_DEFAULT_PATH);
530 media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH);
531 return MEDIA_SVC_SEARCH_TYPE_DIRECT;
534 _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
535 media_svc_debug("ebook_search_type [%d]", _ebook_search_type);
537 iniparser_freedict(dict);
540 return _ebook_search_type;
543 int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
545 int ret = MS_MEDIA_ERR_NONE;
546 GList *item_list = NULL;
548 const char *query = "SELECT media_path FROM media WHERE media_type=5 AND validity=1;";
550 media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
551 media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
552 media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");
554 ret = _media_svc_get_media(handle, query, &item_list);
555 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
557 if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) {
558 for (iter = item_list; iter; iter = g_list_next(iter))
559 _media_svc_update_wordbook((char *)iter->data, uid);
561 _media_svc_clean_wordbook(uid);
563 if (!_media_svc_get_matched_list(keyword, uid, result))
564 media_svc_error("_media_svc_get_matched_list failed");
566 for (iter = item_list; iter; iter = g_list_next(iter)) {
567 if (_media_svc_is_keyword_included((char *)iter->data, keyword))
568 *result = g_list_append(*result, g_strdup((gchar *)iter->data));
572 g_list_free_full(item_list, g_free);
577 int media_svc_check_db(sqlite3 *handle, uid_t uid)
579 int ret = MS_MEDIA_ERR_NONE;
582 ret = media_svc_check_table_exist(handle, &exist);
583 if (ret != MS_MEDIA_ERR_NONE)
587 ret = media_svc_create_table(uid);