4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "media-svc-media.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-util.h"
25 #include "media-svc-db-utils.h"
26 #include "media-svc-env.h"
27 #include "media-svc-media-folder.h"
28 #include "media-svc-album.h"
29 #include "media-svc-noti.h"
30 #include "media-svc-storage.h"
32 #include <iniparser.h>
34 #define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
36 //static __thread int g_media_svc_data_cnt = 0;
37 static __thread int g_media_svc_cur_data_cnt = 0;
39 /* Flag for items to be published by notification */
40 static __thread bool g_insert_with_noti = false;
42 #define BATCH_ITEM_COUNT_MAX 100
44 int media_svc_check_table_exist(sqlite3 *handle, bool *exist)
46 return _media_svc_check_table_exist(handle, exist);
49 int media_svc_create_table(uid_t uid)
51 int ret = MS_MEDIA_ERR_NONE;
52 media_svc_debug_fenter();
54 ret = _media_svc_init_table_query();
55 if (ret != MS_MEDIA_ERR_NONE) {
56 media_svc_error("_media_svc_init_table_query fail.");
60 /*create media table*/
61 ret = _media_svc_make_table_query(DB_TABLE_MEDIA, DB_LIST_MEDIA, uid);
62 if (ret != MS_MEDIA_ERR_NONE) {
63 media_svc_error("_media_svc_make_table_query fail.");
67 /*create folder table*/
68 ret = _media_svc_make_table_query(DB_TABLE_FOLDER, DB_LIST_FOLDER, uid);
69 if (ret != MS_MEDIA_ERR_NONE) {
70 media_svc_error("_media_svc_make_table_query fail.");
74 /*create playlist_map table*/
75 ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST_MAP, DB_LIST_PLAYLIST_MAP, uid);
76 if (ret != MS_MEDIA_ERR_NONE) {
77 media_svc_error("_media_svc_make_table_query fail.");
81 /*create playlist table*/
82 ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST, DB_LIST_PLAYLIST, uid);
83 if (ret != MS_MEDIA_ERR_NONE) {
84 media_svc_error("_media_svc_make_table_query fail.");
88 /* create album table*/
89 ret = _media_svc_make_table_query(DB_TABLE_ALBUM, DB_LIST_ALBUM, uid);
90 if (ret != MS_MEDIA_ERR_NONE) {
91 media_svc_error("_media_svc_make_table_query fail.");
95 /*create tag_map table*/
96 ret = _media_svc_make_table_query(DB_TABLE_TAG_MAP, DB_LIST_TAG_MAP, uid);
97 if (ret != MS_MEDIA_ERR_NONE) {
98 media_svc_error("_media_svc_make_table_query fail.");
103 ret = _media_svc_make_table_query(DB_TABLE_TAG, DB_LIST_TAG, uid);
104 if (ret != MS_MEDIA_ERR_NONE) {
105 media_svc_error("_media_svc_make_table_query fail.");
109 /*create bookmark table*/
110 ret = _media_svc_make_table_query(DB_TABLE_BOOKMARK, DB_LIST_BOOKMARK, uid);
111 if (ret != MS_MEDIA_ERR_NONE) {
112 media_svc_error("_media_svc_make_table_query fail.");
116 /*create storage table from tizen 2.4 */
117 ret = _media_svc_make_table_query(DB_TABLE_STORAGE, DB_LIST_STORAGE, uid);
118 if (ret != MS_MEDIA_ERR_NONE) {
119 media_svc_error("_media_svc_make_table_query fail.");
123 /*create face table. from tizen 3.0*/
124 ret = _media_svc_make_table_query(DB_TABLE_FACE_SCAN_LIST, DB_LIST_FACE_SCAN_LIST, uid);
125 if (ret != MS_MEDIA_ERR_NONE) {
126 media_svc_error("_media_svc_make_table_query fail.");
130 ret = _media_svc_make_table_query(DB_TABLE_FACE, DB_LIST_FACE, uid);
131 if (ret != MS_MEDIA_ERR_NONE) {
132 media_svc_error("_media_svc_make_table_query fail.");
136 _media_svc_destroy_table_query();
138 media_svc_debug_fleave();
143 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
145 return _media_svc_check_data_by_path(handle, path);
148 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
150 return _media_svc_get_modified_time(handle, path, modified_time);
153 int media_svc_insert_item_begin(bool with_noti, int from_pid)
155 g_media_svc_cur_data_cnt = 0;
157 /* Prepare for making noti item list */
159 media_svc_debug("making noti list from pid[%d]", from_pid);
160 _media_svc_initialize_noti_list();
161 _media_svc_set_noti_from_pid(from_pid);
162 g_insert_with_noti = true;
165 return MS_MEDIA_ERR_NONE;
168 int media_svc_insert_item_end(uid_t uid)
170 int ret = MS_MEDIA_ERR_NONE;
172 media_svc_debug_fenter();
174 if (g_media_svc_cur_data_cnt > 0) {
175 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
176 if (g_insert_with_noti) {
177 media_svc_debug("sending noti list");
178 _media_svc_publish_noti_list();
179 g_insert_with_noti = false;
180 _media_svc_set_noti_from_pid(-1);
184 g_media_svc_cur_data_cnt = 0;
189 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)
191 int ret = MS_MEDIA_ERR_NONE;
192 long long int folder_id = 0;
194 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
195 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
196 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
197 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
199 media_svc_content_info_s content_info;
200 memset(&content_info, 0, sizeof(media_svc_content_info_s));
203 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
204 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
205 if (ret != MS_MEDIA_ERR_NONE)
208 switch (content_info.media_type) {
209 case MEDIA_SVC_MEDIA_TYPE_IMAGE:
210 ret = _media_svc_extract_image_metadata(&content_info);
212 case MEDIA_SVC_MEDIA_TYPE_VIDEO:
213 case MEDIA_SVC_MEDIA_TYPE_SOUND:
214 case MEDIA_SVC_MEDIA_TYPE_MUSIC:
215 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
217 case MEDIA_SVC_MEDIA_TYPE_BOOK:
218 ret = _media_svc_extract_book_metadata(&content_info);
219 /* The 'TITLE' should always be filled in */
220 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
221 g_free(content_info.media_meta.title);
222 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
226 /* The 'TITLE' should always be filled in */
227 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
231 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
233 /*Set or Get folder id*/
234 ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, &folder_id, uid);
235 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
237 content_info.folder_id = folder_id;
238 media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);
240 ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
241 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
243 if (g_insert_with_noti)
244 _media_svc_insert_item_to_noti_list(&content_info);
246 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
247 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
248 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
249 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
251 if (g_insert_with_noti)
252 _media_svc_publish_noti_list();
254 g_media_svc_cur_data_cnt = 0;
257 _media_svc_destroy_content_info(&content_info);
259 return MS_MEDIA_ERR_NONE;
262 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)
264 int ret = MS_MEDIA_ERR_NONE;
265 long long int folder_id = 0;
267 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
268 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
269 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
270 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
272 media_svc_content_info_s content_info;
273 memset(&content_info, 0, sizeof(media_svc_content_info_s));
276 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
277 if (ret != MS_MEDIA_ERR_NONE)
280 switch (content_info.media_type) {
281 case MEDIA_SVC_MEDIA_TYPE_IMAGE:
282 ret = _media_svc_extract_image_metadata(&content_info);
284 case MEDIA_SVC_MEDIA_TYPE_VIDEO:
285 case MEDIA_SVC_MEDIA_TYPE_SOUND:
286 case MEDIA_SVC_MEDIA_TYPE_MUSIC:
287 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
289 case MEDIA_SVC_MEDIA_TYPE_BOOK:
290 ret = _media_svc_extract_book_metadata(&content_info);
291 /* The 'TITLE' should always be filled in */
292 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
293 g_free(content_info.media_meta.title);
294 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
298 /* The 'TITLE' should always be filled in */
299 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
303 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
305 /*Set or Get folder id*/
306 ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, &folder_id, uid);
307 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
309 content_info.folder_id = folder_id;
310 media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);
312 /* Extracting thumbnail */
313 if (content_info.thumbnail_path == NULL) {
314 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
315 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
317 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
318 if (ret == MS_MEDIA_ERR_NONE)
319 content_info.thumbnail_path = g_strdup(thumb_path);
323 ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
325 if (ret == MS_MEDIA_ERR_NONE) {
326 media_svc_debug("Insertion is successful. Sending noti for this");
327 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
328 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
329 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
332 _media_svc_destroy_content_info(&content_info);
336 int media_svc_move_item(sqlite3 *handle,
337 const char *src_path,
338 const char *dest_path,
339 const char *media_id,
341 const char *mime_type,
344 int ret = MS_MEDIA_ERR_NONE;
345 char *file_name = NULL;
346 char *folder_path = NULL;
347 int modified_time = 0;
348 long long int folder_id = 0;
349 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
350 char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
351 ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
352 ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
354 media_svc_debug_fenter();
356 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
357 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
358 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
359 media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
360 media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
363 ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
364 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
365 /* Get storage_type */
366 ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
367 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
368 ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
369 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
371 /*check and update folder*/
372 ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, &folder_id, uid);
373 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
376 file_name = g_path_get_basename(dest_path);
378 /*get modified_time*/
379 modified_time = _media_svc_get_file_time(dest_path);
381 /*get old thumbnail_path and remove thumbnail */
382 ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
383 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
384 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
389 _media_svc_remove_file(old_thumb_path);
392 ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_id, uid);
394 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
396 media_svc_debug("Move is successful. Sending noti for this");
397 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
399 /*update folder modified_time*/
400 folder_path = g_path_get_dirname(dest_path);
401 ret = _media_svc_update_folder_modified_time(folder_path, uid);
403 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
405 return MS_MEDIA_ERR_NONE;
408 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
410 int ret = MS_MEDIA_ERR_NONE;
412 ret = _media_svc_update_item_validity(path, validity, true, uid);
413 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
415 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
416 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
417 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
418 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
420 g_media_svc_cur_data_cnt = 0;
426 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
428 int ret = MS_MEDIA_ERR_NONE;
429 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
430 media_svc_noti_item *noti_item = NULL;
432 media_svc_debug_fenter();
434 /*Get thumbnail path to delete*/
435 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
436 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
438 /* Get notification info */
439 ret = _media_svc_get_noti_info(handle, path, ¬i_item);
440 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
443 ret = _media_svc_delete_item_by_path(path, uid);
444 if (ret != MS_MEDIA_ERR_NONE) {
445 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
446 _media_svc_destroy_noti_item(noti_item);
451 /* Send notification */
452 media_svc_debug("Deletion is successful. Sending noti for this");
453 _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
454 _media_svc_destroy_noti_item(noti_item);
457 _media_svc_remove_file(thumb_path);
459 return MS_MEDIA_ERR_NONE;
462 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)
464 int ret = MS_MEDIA_ERR_NONE;
465 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
466 media_svc_content_info_s content_info = {0, };
467 media_svc_noti_item *noti_item = NULL;
469 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
470 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
471 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
472 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
475 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
476 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
478 /* Initialize thumbnail information to remake thumbnail. */
479 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
480 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
481 goto REFRESH_FINALIZE;
483 if (STRING_VALID(thumb_path)) {
484 _media_svc_remove_file(thumb_path);
486 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
487 if (ret != MS_MEDIA_ERR_NONE)
488 goto REFRESH_FINALIZE;
491 /* Get notification info */
492 ret = _media_svc_get_noti_info(handle, path, ¬i_item);
493 if (ret != MS_MEDIA_ERR_NONE)
494 goto REFRESH_FINALIZE;
496 content_info.media_type = noti_item->media_type;
497 content_info.mime_type = g_strdup(noti_item->mime_type);
499 switch (content_info.media_type) {
500 case MEDIA_SVC_MEDIA_TYPE_IMAGE:
501 ret = _media_svc_extract_image_metadata(&content_info);
503 case MEDIA_SVC_MEDIA_TYPE_VIDEO:
504 case MEDIA_SVC_MEDIA_TYPE_SOUND:
505 case MEDIA_SVC_MEDIA_TYPE_MUSIC:
506 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
508 case MEDIA_SVC_MEDIA_TYPE_BOOK:
509 ret = _media_svc_extract_book_metadata(&content_info);
510 /* The 'TITLE' should always be filled in */
511 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
512 g_free(content_info.media_meta.title);
513 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
517 /* The 'TITLE' should always be filled in */
518 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
522 if (ret != MS_MEDIA_ERR_NONE)
523 goto REFRESH_FINALIZE;
525 /* Extracting thumbnail */
526 if (content_info.thumbnail_path == NULL) {
527 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
528 memset(thumb_path, 0, sizeof(thumb_path));
530 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
531 if (ret == MS_MEDIA_ERR_NONE)
532 content_info.thumbnail_path = g_strdup(thumb_path);
536 ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
538 if (ret == MS_MEDIA_ERR_NONE) {
540 /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
541 if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
542 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
543 if (ret != MS_MEDIA_ERR_NONE)
544 goto REFRESH_FINALIZE;
546 g_media_svc_cur_data_cnt = 0;
549 /* Except scanner case */
550 media_svc_debug("Update is successful. Sending noti for this");
551 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
554 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
558 _media_svc_destroy_content_info(&content_info);
559 _media_svc_destroy_noti_item(noti_item);
564 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
566 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
568 return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
571 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
573 int ret = MS_MEDIA_ERR_NONE;
574 char *root_path = NULL;
575 GPtrArray *path_list = NULL;
578 ret = ms_user_get_internal_root_path(uid, &root_path);
579 media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
581 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
582 if (ret != MS_MEDIA_ERR_NONE)
583 media_svc_error("Fail to send noti");
587 path_list = g_ptr_array_new_with_free_func(g_free);
588 _media_svc_get_storage_path(handle, &path_list);
590 for (i = 0; i < path_list->len; i++) {
591 root_path = g_ptr_array_index(path_list, i);
593 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
594 if (ret != MS_MEDIA_ERR_NONE)
595 media_svc_error("Fail to send noti");
598 g_ptr_array_free(path_list, TRUE);
601 static void __media_svc_foreach_update_media(gpointer data, gpointer user_data)
603 media_svc_content_info_s content_info = {0, };
605 if (_media_svc_extract_music_metadata_for_update(&content_info, (const char *)data) != MS_MEDIA_ERR_NONE) {
606 media_svc_error("Fail to extract metadata");
607 _media_svc_destroy_content_info(&content_info);
611 if (_media_svc_update_meta_with_data(&content_info) != MS_MEDIA_ERR_NONE)
612 media_svc_error("Fail to append item[%s]", content_info.path);
614 _media_svc_destroy_content_info(&content_info);
617 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
619 int ret = MS_MEDIA_ERR_NONE;
621 GList *path_list = NULL;
623 sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", DB_TABLE_MEDIA);
624 ret = _media_svc_get_media(handle, sql, &path_list);
625 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get media list");
628 g_list_foreach(path_list, __media_svc_foreach_update_media, NULL);
629 g_list_free_full(path_list, g_free);
631 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
632 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
636 __media_svc_noti_all_storage(handle, uid);
641 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)
643 return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
646 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
648 return _media_svc_get_pinyin_str(src_str, pinyin_str);
651 int media_svc_check_pinyin_support(bool *support)
653 *support = _media_svc_check_pinyin_support();
655 return MS_MEDIA_ERR_NONE;
658 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
660 int ret = MS_MEDIA_ERR_NONE;
662 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
664 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
665 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
670 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
672 return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
675 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
677 return _media_svc_check_storage(handle, storage_id, storage_path, validity);
680 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
682 return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
685 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)
687 int ret = MS_MEDIA_ERR_NONE;
689 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
690 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
691 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
692 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
694 ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
695 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
697 /* Remove external storage that validity is 0 */
698 ret = _media_svc_delete_invalid_storage(handle, uid);
699 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
704 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
706 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
707 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
708 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
709 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
711 return _media_svc_append_by_folder_path(handle, storage_id, path, storage_type, uid);
714 int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
716 return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
719 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
721 return _media_svc_check_folder_by_path(handle, folder_path);
724 int media_svc_append_query(const char *query, uid_t uid)
726 return _media_svc_append_query_list(query, uid);
729 int media_svc_send_query(uid_t uid)
731 return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
734 int media_svc_get_media_type(const char *path, int *mediatype)
736 return _media_svc_get_media_type(path, mediatype);
739 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
741 int ret = MS_MEDIA_ERR_NONE;
742 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
745 // 1. Check media type
746 if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
747 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
749 // 2. try to create thumbnail
750 ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
751 if (ret != MS_MEDIA_ERR_NONE) {
752 media_svc_error("Failed to create thumbnail [%d]", ret);
753 if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
757 // 3. Update creation result to media db
758 sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);
760 ret = _media_svc_sql_query(sql, uid);
761 SQLITE3_SAFE_FREE(sql);
762 if (ret != MS_MEDIA_ERR_NONE) {
763 media_svc_error("Failed to update media db [%d]", ret);
764 *thumbnail_path = g_strdup("");
766 *thumbnail_path = g_strdup(thumb_path);
772 static int __media_svc_get_ebook_search_type(void)
774 dictionary *dict = NULL;
775 static int _ebook_search_type = -1;
777 if (_ebook_search_type == -1) {
778 dict = iniparser_load(CONTENT_INI_DEFAULT_PATH);
780 media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH);
781 return MEDIA_SVC_SEARCH_TYPE_DIRECT;
784 _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
785 media_svc_debug("ebook_search_type [%d]", _ebook_search_type);
787 iniparser_freedict(dict);
790 return _ebook_search_type;
793 int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
795 int ret = MS_MEDIA_ERR_NONE;
796 GList *item_list = NULL;
800 media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
801 media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
802 media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");
804 query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;",
805 DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK);
807 ret = _media_svc_get_media(handle, query, &item_list);
808 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
810 if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) {
811 for (iter = item_list; iter; iter = g_list_next(iter))
812 _media_svc_update_wordbook((char *)iter->data, uid);
814 _media_svc_clean_wordbook(uid);
816 if (!_media_svc_get_matched_list(keyword, uid, result))
817 media_svc_error("_media_svc_get_matched_list failed");
819 for (iter = item_list; iter; iter = g_list_next(iter)) {
820 if (_media_svc_is_keyword_included((char *)iter->data, keyword))
821 *result = g_list_append(*result, g_strdup((gchar *)iter->data));
825 g_list_free_full(item_list, g_free);