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.
23 #include <drm_client.h>
24 #include <media-util.h>
25 #include "media-svc.h"
26 #include "media-svc-media.h"
27 #include "media-svc-debug.h"
28 #include "media-svc-util.h"
29 #include "media-svc-db-utils.h"
30 #include "media-svc-media-folder.h"
31 #include "media-svc-album.h"
32 #include "media-svc-noti.h"
34 static __thread int g_media_svc_item_validity_data_cnt = 1;
35 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
37 static __thread int g_media_svc_move_item_data_cnt = 1;
38 static __thread int g_media_svc_move_item_cur_data_cnt = 0;
40 static __thread int g_media_svc_insert_item_data_cnt = 1;
41 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
43 /* Flag for items to be published by notification */
44 static __thread int g_insert_with_noti = FALSE;
46 int media_svc_connect(MediaSvcHandle **handle)
48 int ret = MEDIA_INFO_ERROR_NONE;
49 MediaDBHandle *db_handle = NULL;
51 media_svc_debug_func();
53 #if 1 //Use libmedia_utils API
54 ret = media_db_connect(&db_handle);
55 if(ret != MS_MEDIA_ERR_NONE)
56 return _media_svc_error_convert(ret);
59 sqlite3 * db_handle = NULL;
61 ret = _media_svc_connect_db_with_handle(&db_handle);
62 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
66 return MEDIA_INFO_ERROR_NONE;
70 int media_svc_disconnect(MediaSvcHandle *handle)
72 MediaDBHandle * db_handle = (MediaDBHandle *)handle;
74 media_svc_debug_func();
76 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
78 #if 1 //Use libmedia_utils API
79 int ret = MEDIA_INFO_ERROR_NONE;
81 ret = media_db_disconnect(db_handle);
82 return _media_svc_error_convert(ret);
84 sqlite3 * db_handle = (sqlite3 *)handle;
86 return _media_svc_disconnect_db_with_handle(db_handle);
90 int media_svc_create_table(MediaSvcHandle *handle)
92 int ret = MEDIA_INFO_ERROR_NONE;
93 sqlite3 * db_handle = (sqlite3 *)handle;
95 media_svc_debug_func();
97 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
99 /*create media table*/
100 ret = _media_svc_create_media_table(handle);
101 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
103 /*create folder table*/
104 ret = _media_svc_create_folder_table(handle);
105 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
107 /*create playlist table*/
108 ret = _media_svc_create_playlist_table(handle);
109 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
111 /* create album table*/
112 ret = _media_svc_create_album_table(handle);
113 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
116 ret = _media_svc_create_tag_table(handle);
117 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
119 /*create bookmark table*/
120 ret = _media_svc_create_bookmark_table(handle);
121 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
123 #if 0 /*unify media and custom table*/
124 /*create custom table*/
125 ret = _media_svc_create_custom_table(handle);
126 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
129 return MEDIA_INFO_ERROR_NONE;
132 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type)
134 int ret = MEDIA_INFO_ERROR_NONE;
135 media_svc_storage_type_e type;
137 ret = _media_svc_get_store_type_by_path(path, &type);
138 media_svc_retvm_if(ret < MEDIA_INFO_ERROR_NONE, ret, "_media_svc_get_store_type_by_path failed : %d", ret);
140 *storage_type = type;
145 int media_svc_get_mime_type(const char *path, char *mimetype)
147 int ret = MEDIA_INFO_ERROR_NONE;
148 drm_bool_type_e is_drm = DRM_FALSE;
149 drm_content_info_s *drm_contentInfo = NULL;
151 ret = _media_svc_get_mime_type(path, mimetype, &is_drm, &drm_contentInfo);
153 SAFE_FREE(drm_contentInfo);
158 int media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
160 int ret = MEDIA_INFO_ERROR_NONE;
162 ret = _media_svc_get_media_type(path, mime_type, media_type);
167 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *path)
169 int ret = MEDIA_INFO_ERROR_NONE;
170 sqlite3 * db_handle = (sqlite3 *)handle;
173 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
174 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "Path is NULL");
176 ret = _media_svc_count_record_with_path(db_handle, path, &count);
177 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
180 media_svc_debug("item is exist in database");
181 return MEDIA_INFO_ERROR_NONE;
183 media_svc_debug("item is not exist in database");
184 return MEDIA_INFO_ERROR_DATABASE_NO_RECORD;
187 return MEDIA_INFO_ERROR_NONE;
190 int media_svc_insert_item_begin(MediaSvcHandle *handle, int data_cnt, int with_noti, int from_pid)
192 sqlite3 * db_handle = (sqlite3 *)handle;
194 media_svc_debug("Transaction data count : [%d]", data_cnt);
196 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
197 media_svc_retvm_if(data_cnt < 1, MEDIA_INFO_ERROR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
199 g_media_svc_insert_item_data_cnt = data_cnt;
200 g_media_svc_insert_item_cur_data_cnt = 0;
202 /* Prepare for making noti item list */
204 media_svc_debug("making noti list from pid[%d]", from_pid);
205 if (_media_svc_create_noti_list(data_cnt) != MEDIA_INFO_ERROR_NONE) {
206 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
209 _media_svc_set_noti_from_pid(from_pid);
210 g_insert_with_noti = TRUE;
213 return MEDIA_INFO_ERROR_NONE;
216 int media_svc_insert_item_end(MediaSvcHandle *handle)
218 int ret = MEDIA_INFO_ERROR_NONE;
219 sqlite3 * db_handle = (sqlite3 *)handle;
221 media_svc_debug_func();
223 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
225 if (g_media_svc_insert_item_cur_data_cnt > 0) {
227 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM);
228 if (g_insert_with_noti) {
229 media_svc_debug("sending noti list");
230 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
231 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
232 g_insert_with_noti = FALSE;
233 _media_svc_set_noti_from_pid(-1);
237 g_media_svc_insert_item_data_cnt = 1;
238 g_media_svc_insert_item_cur_data_cnt = 0;
243 int media_svc_insert_item_bulk(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path, int is_burst)
245 int ret = MEDIA_INFO_ERROR_NONE;
246 sqlite3 * db_handle = (sqlite3 *)handle;
247 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
248 media_svc_media_type_e media_type;
249 drm_content_info_s *drm_contentInfo = NULL;
251 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
252 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
254 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
255 media_svc_error("storage type is incorrect[%d]", storage_type);
256 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
259 media_svc_content_info_s content_info;
260 memset(&content_info, 0, sizeof(media_svc_content_info_s));
263 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
264 ret = _media_svc_set_media_info(&content_info, storage_type, path, &media_type, FALSE, &drm_contentInfo);
265 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
267 if(media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
269 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
270 ret = _media_svc_extract_image_metadata(&content_info, media_type);
272 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, drm_contentInfo);
274 SAFE_FREE(drm_contentInfo);
275 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
277 /*Set or Get folder id*/
278 ret = _media_svc_get_and_append_folder_id_by_path(handle, path, storage_type, folder_uuid);
279 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
281 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
282 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
284 if (g_media_svc_insert_item_data_cnt == 1) {
286 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, FALSE);
287 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
289 if (g_insert_with_noti)
290 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
292 } else if(g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
294 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, TRUE);
295 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
297 if (g_insert_with_noti)
298 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
300 g_media_svc_insert_item_cur_data_cnt ++;
302 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
304 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, TRUE);
305 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
307 if (g_insert_with_noti)
308 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
310 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM);
311 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
313 if (g_insert_with_noti) {
314 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
315 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
317 /* Recreate noti list */
318 if (_media_svc_create_noti_list(g_media_svc_insert_item_data_cnt) != MEDIA_INFO_ERROR_NONE) {
319 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
323 g_media_svc_insert_item_cur_data_cnt = 0;
326 media_svc_error("Error in media_svc_insert_item_bulk");
327 _media_svc_destroy_content_info(&content_info);
328 return MEDIA_INFO_ERROR_INTERNAL;
331 _media_svc_destroy_content_info(&content_info);
333 return MEDIA_INFO_ERROR_NONE;
336 int media_svc_insert_item_immediately(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path)
338 int ret = MEDIA_INFO_ERROR_NONE;
339 sqlite3 * db_handle = (sqlite3 *)handle;
340 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
341 media_svc_media_type_e media_type;
342 drm_content_info_s *drm_contentInfo = NULL;
344 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
345 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
347 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
348 media_svc_error("storage type is incorrect[%d]", storage_type);
349 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
352 media_svc_content_info_s content_info;
353 memset(&content_info, 0, sizeof(media_svc_content_info_s));
356 ret = _media_svc_set_media_info(&content_info, storage_type, path, &media_type, FALSE, &drm_contentInfo);
357 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
359 if(media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
361 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
362 ret = _media_svc_extract_image_metadata(&content_info, media_type);
363 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
365 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, drm_contentInfo);
366 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
369 /*Set or Get folder id*/
370 ret = _media_svc_get_and_append_folder_id_by_path(handle, path, storage_type, folder_uuid);
371 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
373 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
374 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, &content_info);
376 /* Extracting thumbnail */
377 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
378 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
382 ret = thumbnail_request_from_db_with_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height);
384 media_svc_error("thumbnail_request_from_db failed: %d", ret);
386 media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
387 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
389 media_svc_error("__media_svc_malloc_and_strncpy failed : %d", ret);
393 if (content_info.media_meta.width <= 0)
394 content_info.media_meta.width = width;
396 if (content_info.media_meta.height <= 0)
397 content_info.media_meta.height = height;
400 ret = _media_svc_insert_item_with_data(db_handle, &content_info, FALSE, FALSE);
402 if (ret == MEDIA_INFO_ERROR_NONE) {
403 media_svc_debug("Insertion is successful. Sending noti for this");
404 _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);
407 _media_svc_destroy_content_info(&content_info);
411 int media_svc_insert_folder(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path)
413 int ret = MEDIA_INFO_ERROR_NONE;
414 sqlite3 * db_handle = (sqlite3 *)handle;
416 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
417 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
419 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
420 media_svc_error("storage type is incorrect[%d]", storage_type);
421 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
424 media_svc_debug("storage[%d], folder_path[%s]", storage_type, path);
427 char *folder_name = NULL;
428 int folder_modified_date = 0;
429 char *folder_uuid = _media_info_generate_uuid();
430 if(folder_uuid == NULL ) {
431 media_svc_error("Invalid UUID");
432 return MEDIA_INFO_ERROR_INTERNAL;
435 folder_name = g_path_get_basename(path);
436 folder_modified_date = _media_svc_get_file_time(path);
438 ret = _media_svc_append_folder(handle, storage_type, folder_uuid, path, folder_name, folder_modified_date);
439 SAFE_FREE(folder_name);
441 if (ret < MEDIA_INFO_ERROR_NONE) {
442 media_svc_error("_media_svc_append_folder error [%d]", ret);
446 return MEDIA_INFO_ERROR_NONE;
449 int media_svc_move_item_begin(MediaSvcHandle *handle, int data_cnt)
451 sqlite3 * db_handle = (sqlite3 *)handle;
453 media_svc_debug("Transaction data count : [%d]", data_cnt);
455 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
456 media_svc_retvm_if(data_cnt < 1, MEDIA_INFO_ERROR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
458 g_media_svc_move_item_data_cnt = data_cnt;
459 g_media_svc_move_item_cur_data_cnt = 0;
461 return MEDIA_INFO_ERROR_NONE;
464 int media_svc_move_item_end(MediaSvcHandle *handle)
466 int ret = MEDIA_INFO_ERROR_NONE;
467 sqlite3 * db_handle = (sqlite3 *)handle;
469 media_svc_debug_func();
471 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
473 if (g_media_svc_move_item_cur_data_cnt > 0) {
475 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM);
478 /*clean up old folder path*/
479 ret = _media_svc_update_folder_table(handle);
480 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
482 g_media_svc_move_item_data_cnt = 1;
483 g_media_svc_move_item_cur_data_cnt = 0;
488 int media_svc_move_item(MediaSvcHandle *handle, media_svc_storage_type_e src_storage, const char *src_path,
489 media_svc_storage_type_e dest_storage, const char *dest_path)
491 int ret = MEDIA_INFO_ERROR_NONE;
492 sqlite3 * db_handle = (sqlite3 *)handle;
493 char *file_name = NULL;
494 char *folder_path = NULL;
495 int modified_time = 0;
496 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
497 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0,};
498 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0,};
501 media_svc_debug_func();
503 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
504 media_svc_retvm_if(!STRING_VALID(src_path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "src_path is NULL");
505 media_svc_retvm_if(!STRING_VALID(dest_path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "dest_path is NULL");
507 if ((src_storage != MEDIA_SVC_STORAGE_INTERNAL) && (src_storage != MEDIA_SVC_STORAGE_EXTERNAL)) {
508 media_svc_error("src_storage type is incorrect[%d]", src_storage);
509 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
511 if ((dest_storage != MEDIA_SVC_STORAGE_INTERNAL) && (dest_storage != MEDIA_SVC_STORAGE_EXTERNAL)) {
512 media_svc_error("dest_storage type is incorrect[%d]", dest_storage);
513 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
516 /*check and update folder*/
517 ret = _media_svc_get_and_append_folder_id_by_path(handle, dest_path, dest_storage, folder_uuid);
518 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
521 file_name = g_path_get_basename(dest_path);
523 /*get modified_time*/
524 modified_time = _media_svc_get_file_time(dest_path);
526 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
527 ret = _media_svc_get_media_type_by_path(handle, src_path, &media_type);
528 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
530 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
531 /*get old thumbnail_path*/
532 ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
533 media_svc_retv_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret);
535 /* If old thumb path is default or not */
536 if (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) == 0) {
537 strncpy(new_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(new_thumb_path));
539 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT);
543 if (g_media_svc_move_item_data_cnt == 1) {
546 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
547 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE);
549 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE);
551 SAFE_FREE(file_name);
552 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
554 media_svc_debug("Move is successful. Sending noti for this");
556 /* Get notification info */
557 media_svc_noti_item *noti_item = NULL;
558 ret = _media_svc_get_noti_info(handle, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
559 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
561 /* Send notification for move */
562 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
563 _media_svc_destroy_noti_item(noti_item);
565 /*update folder modified_time*/
566 folder_path = g_path_get_dirname(dest_path);
567 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, FALSE);
568 SAFE_FREE(folder_path);
569 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
571 ret = _media_svc_update_folder_table(handle);
572 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
574 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
577 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
578 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE);
580 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE);
582 SAFE_FREE(file_name);
583 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
585 /*update folder modified_time*/
586 folder_path = g_path_get_dirname(dest_path);
587 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, TRUE);
588 SAFE_FREE(folder_path);
589 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
591 g_media_svc_move_item_cur_data_cnt ++;
593 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
596 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
597 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE);
599 ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE);
601 SAFE_FREE(file_name);
602 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
604 /*update folder modified_time*/
605 folder_path = g_path_get_dirname(dest_path);
606 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, TRUE);
607 SAFE_FREE(folder_path);
608 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
611 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM);
612 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
614 g_media_svc_move_item_cur_data_cnt = 0;
617 media_svc_error("Error in media_svc_move_item");
618 return MEDIA_INFO_ERROR_INTERNAL;
621 /*rename thumbnail file*/
622 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
623 if((strlen(old_thumb_path) > 0) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
624 ret = _media_svc_rename_file(old_thumb_path,new_thumb_path);
626 media_svc_error("_media_svc_rename_file failed : %d", ret);
630 return MEDIA_INFO_ERROR_NONE;
633 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
635 sqlite3 * db_handle = (sqlite3 *)handle;
637 media_svc_debug("Transaction data count : [%d]", data_cnt);
639 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
640 media_svc_retvm_if(data_cnt < 1, MEDIA_INFO_ERROR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
642 g_media_svc_item_validity_data_cnt = data_cnt;
643 g_media_svc_item_validity_cur_data_cnt = 0;
645 return MEDIA_INFO_ERROR_NONE;
648 int media_svc_set_item_validity_end(MediaSvcHandle *handle)
650 int ret = MEDIA_INFO_ERROR_NONE;
651 sqlite3 * db_handle = (sqlite3 *)handle;
653 media_svc_debug_func();
655 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
657 if (g_media_svc_item_validity_cur_data_cnt > 0) {
659 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY);
662 g_media_svc_item_validity_data_cnt = 1;
663 g_media_svc_item_validity_cur_data_cnt = 0;
668 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *path, int validity)
670 int ret = MEDIA_INFO_ERROR_NONE;
671 sqlite3 * db_handle = (sqlite3 *)handle;
673 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
674 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
676 media_svc_debug("path=[%s], validity=[%d]", path, validity);
678 if (g_media_svc_item_validity_data_cnt == 1) {
680 return _media_svc_update_item_validity(db_handle, path, validity, FALSE);
682 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
684 ret = _media_svc_update_item_validity(db_handle, path, validity, TRUE);
685 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
687 g_media_svc_item_validity_cur_data_cnt ++;
689 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
691 ret = _media_svc_update_item_validity(db_handle, path, validity, TRUE);
692 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
694 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY);
695 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
697 g_media_svc_item_validity_cur_data_cnt = 0;
701 media_svc_error("Error in media_svc_set_item_validity");
702 return MEDIA_INFO_ERROR_INTERNAL;
705 return MEDIA_INFO_ERROR_NONE;
708 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *path)
710 int ret = MEDIA_INFO_ERROR_NONE;
711 sqlite3 * db_handle = (sqlite3 *)handle;
712 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0,};
714 media_svc_debug_func();
716 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
717 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
720 ret = _media_svc_get_media_type_by_path(db_handle, path, &media_type);
721 media_svc_retv_if((ret != MEDIA_INFO_ERROR_NONE), ret);
723 if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
724 /*Get thumbnail path to delete*/
725 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
726 media_svc_retv_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret);
727 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
729 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
730 media_svc_retv_if((ret != MEDIA_INFO_ERROR_NONE), ret);
733 /*Get thumbnail path to delete*/
734 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
735 media_svc_retv_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret);
739 /* Get notification info */
740 media_svc_noti_item *noti_item = NULL;
741 ret = _media_svc_get_noti_info(handle, path, MS_MEDIA_ITEM_FILE, ¬i_item);
742 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
745 ret = _media_svc_delete_item_by_path(db_handle, path);
746 if (ret != MEDIA_INFO_ERROR_NONE) {
747 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
748 _media_svc_destroy_noti_item(noti_item);
753 /* Send notification */
754 media_svc_debug("Deletion is successful. Sending noti for this");
755 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
756 _media_svc_destroy_noti_item(noti_item);
759 if ((strlen(thumb_path) > 0) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
761 /* Get count of media, which contains same thumbnail for music */
762 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
763 ret = _media_svc_get_thumbnail_count(db_handle, thumb_path, &thumb_count);
764 if (ret < MEDIA_INFO_ERROR_NONE) {
765 media_svc_error("Failed to get thumbnail count : %d", ret);
769 if (thumb_count == 1) {
770 if (_media_svc_remove_file(thumb_path) == FALSE) {
771 media_svc_error("fail to remove thumbnail file.");
776 return MEDIA_INFO_ERROR_NONE;
779 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, media_svc_storage_type_e storage_type)
781 int ret = MEDIA_INFO_ERROR_NONE;
782 sqlite3 * db_handle = (sqlite3 *)handle;
783 char * dirpath = NULL;
785 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
787 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
789 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
790 media_svc_error("storage type is incorrect[%d]", storage_type);
791 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
794 ret = _media_svc_truncate_table(db_handle, storage_type);
795 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
797 dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? MEDIA_SVC_THUMB_INTERNAL_PATH : MEDIA_SVC_THUMB_EXTERNAL_PATH;
799 /* remove thumbnails */
800 ret = _media_svc_remove_all_files_in_dir(dirpath);
801 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
803 return MEDIA_INFO_ERROR_NONE;
806 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, media_svc_storage_type_e storage_type)
808 sqlite3 * db_handle = (sqlite3 *)handle;
810 media_svc_debug_func();
812 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
814 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
815 media_svc_error("storage type is incorrect[%d]", storage_type);
816 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
819 /*Delete from DB and remove thumbnail files*/
820 return _media_svc_delete_invalid_items(db_handle, storage_type);
823 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *folder_path)
825 sqlite3 * db_handle = (sqlite3 *)handle;
827 media_svc_debug_func();
829 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
831 /*Delete from DB and remove thumbnail files*/
832 return _media_svc_delete_invalid_folder_items(db_handle, folder_path);
835 int media_svc_set_all_storage_items_validity(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, int validity)
837 sqlite3 * db_handle = (sqlite3 *)handle;
839 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
841 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
842 media_svc_error("storage type is incorrect[%d]", storage_type);
843 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
846 return _media_svc_update_storage_item_validity(db_handle, storage_type, validity);
849 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *folder_path, int validity, int recursive)
851 sqlite3 * db_handle = (sqlite3 *)handle;
853 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
854 media_svc_retvm_if(!STRING_VALID(folder_path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "folder_path is NULL");
857 return _media_svc_update_recursive_folder_item_validity(db_handle, folder_path, validity);
859 return _media_svc_update_folder_item_validity(db_handle, folder_path, validity);
862 int media_svc_refresh_item(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path)
864 int ret = MEDIA_INFO_ERROR_NONE;
865 sqlite3 * db_handle = (sqlite3 *)handle;
866 media_svc_media_type_e media_type;
867 drm_content_info_s *drm_contentInfo = NULL;
869 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
870 media_svc_retvm_if(!STRING_VALID(path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
872 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
873 media_svc_error("storage type is incorrect[%d]", storage_type);
874 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
878 media_svc_content_info_s content_info;
879 memset(&content_info, 0, sizeof(media_svc_content_info_s));
882 ret = _media_svc_set_media_info(&content_info, storage_type, path, &media_type, TRUE, &drm_contentInfo);
883 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
885 /* Initialize thumbnail information to remake thumbnail. */
886 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
887 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
888 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
890 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
891 ret = _media_svc_remove_file(thumb_path);
892 if (ret < MEDIA_INFO_ERROR_NONE) {
893 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
897 ret = _media_svc_update_thumbnail_path(handle, path, NULL);
898 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
900 if(media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
902 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
903 ret = _media_svc_extract_image_metadata(&content_info, media_type);
904 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
906 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, drm_contentInfo);
907 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
910 /* Extracting thumbnail */
911 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
912 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
916 ret = thumbnail_request_from_db_with_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height);
918 media_svc_error("thumbnail_request_from_db failed: %d", ret);
920 media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
921 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
923 media_svc_error("__media_svc_malloc_and_strncpy failed : %d", ret);
927 if (content_info.media_meta.width <= 0)
928 content_info.media_meta.width = width;
930 if (content_info.media_meta.height <= 0)
931 content_info.media_meta.height = height;
934 ret = _media_svc_update_item_with_data(db_handle, &content_info);
936 /* Get notification info */
937 media_svc_noti_item *noti_item = NULL;
938 ret = _media_svc_get_noti_info(handle, path, MS_MEDIA_ITEM_FILE, ¬i_item);
939 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
941 if (ret == MEDIA_INFO_ERROR_NONE) {
942 media_svc_debug("Update is successful. Sending noti for this");
943 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, content_info.path, media_type, noti_item->media_uuid, noti_item->mime_type);
945 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
948 _media_svc_destroy_content_info(&content_info);
949 _media_svc_destroy_noti_item(noti_item);
954 int media_svc_rename_folder(MediaSvcHandle *handle, const char *src_path, const char *dst_path)
956 sqlite3 * db_handle = (sqlite3 *)handle;
957 int ret = MEDIA_INFO_ERROR_NONE;
959 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
960 media_svc_retvm_if(src_path == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "src_path is NULL");
961 media_svc_retvm_if(dst_path == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "dst_path is NULL");
963 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
965 /* Get notification info */
966 media_svc_noti_item *noti_item = NULL;
967 ret = _media_svc_get_noti_info(handle, src_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
968 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
970 ret = _media_svc_sql_begin_trans(handle);
971 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
973 /* Update all folder record's path, which are matched by old parent path */
974 char *update_folder_path_sql = NULL;
975 char src_path_slash[MEDIA_SVC_PATHNAME_SIZE + 1];
976 char dst_path_slash[MEDIA_SVC_PATHNAME_SIZE + 1];
978 snprintf(src_path_slash, sizeof(src_path_slash), "%s/", src_path);
979 snprintf(dst_path_slash, sizeof(dst_path_slash), "%s/", dst_path);
981 update_folder_path_sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q', '%q');", src_path_slash, dst_path_slash);
983 //ret = _media_svc_sql_query(handle, update_folder_path_sql);
984 ret = media_db_request_update_db_batch(update_folder_path_sql);
985 sqlite3_free(update_folder_path_sql);
987 if (ret != SQLITE_OK) {
988 media_svc_error("failed to update folder path");
989 _media_svc_sql_rollback_trans(handle);
991 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
994 /* Update all folder record's modified date, which are changed above */
995 char *update_folder_modified_time_sql = NULL;
999 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d where path like '%q';", date, dst_path);
1001 ret = media_db_request_update_db_batch(update_folder_modified_time_sql);
1002 //ret = _media_svc_sql_query(handle, update_folder_modified_time_sql);
1003 sqlite3_free(update_folder_modified_time_sql);
1005 if (ret != SQLITE_OK) {
1006 media_svc_error("failed to update folder modified time");
1007 _media_svc_sql_rollback_trans(handle);
1009 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
1012 /* Update all items */
1013 char *select_all_sql = NULL;
1014 sqlite3_stmt *sql_stmt = NULL;
1015 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1017 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1019 select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from media where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", dst_path, dst_child_path);
1021 media_svc_debug("[SQL query] : %s", select_all_sql);
1023 ret = sqlite3_prepare_v2(handle, select_all_sql, -1, &sql_stmt, NULL);
1024 sqlite3_free((char *)select_all_sql);
1026 if (ret != SQLITE_OK) {
1027 media_svc_error ("prepare error [%s]", sqlite3_errmsg(handle));
1028 _media_svc_sql_rollback_trans(handle);
1029 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
1033 ret = sqlite3_step(sql_stmt);
1034 if (ret != SQLITE_ROW) {
1035 media_svc_debug("end of iteration");
1039 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0,};
1040 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0,};
1041 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0,};
1042 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0,};
1044 bool no_thumb = FALSE;
1046 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1047 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1048 media_uuid[sizeof(media_uuid) - 1] = '\0';
1050 media_svc_error("media UUID is NULL");
1051 return MEDIA_INFO_ERROR_DATABASE_INVALID;
1054 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1055 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1056 media_path[sizeof(media_path) - 1] = '\0';
1058 media_svc_error("media path is NULL");
1059 return MEDIA_INFO_ERROR_DATABASE_INVALID;
1062 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1063 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1064 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1066 media_svc_debug("media thumb path doesn't exist in DB");
1070 media_type = sqlite3_column_int(sql_stmt, 3);
1072 /* Update path, thumbnail path of this item */
1073 char *replaced_path = NULL;
1074 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1075 if (replaced_path == NULL) {
1076 media_svc_error("_media_svc_replace_path failed");
1077 SQLITE3_FINALIZE(sql_stmt);
1078 _media_svc_sql_rollback_trans(handle);
1079 return MEDIA_INFO_ERROR_INTERNAL;
1082 media_svc_debug("New media path : %s", replaced_path);
1083 media_svc_storage_type_e storage_type;
1086 /* If old thumb path is default or not */
1087 if (strncmp(media_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) == 0) {
1088 strncpy(media_new_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(media_new_thumb_path));
1090 ret = _media_svc_get_store_type_by_path(replaced_path, &storage_type);
1092 media_svc_error("_media_svc_get_store_type_by_path failed : %d", ret);
1093 SAFE_FREE(replaced_path);
1094 _media_svc_sql_rollback_trans(handle);
1095 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
1098 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT);
1100 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1101 SAFE_FREE(replaced_path);
1102 SQLITE3_FINALIZE(sql_stmt);
1103 _media_svc_sql_rollback_trans(handle);
1104 return MEDIA_INFO_ERROR_INTERNAL;
1108 //media_svc_debug("New media thumbnail path : %s", media_new_thumb_path);
1111 char *update_item_sql = NULL;
1114 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1116 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1117 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1119 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1123 ret = media_db_request_update_db_batch(update_item_sql);
1124 //ret = _media_svc_sql_query(handle, update_item_sql);
1125 sqlite3_free(update_item_sql);
1126 SAFE_FREE(replaced_path);
1128 if (ret != SQLITE_OK) {
1129 media_svc_error("failed to update item");
1130 SQLITE3_FINALIZE(sql_stmt);
1131 _media_svc_sql_rollback_trans(handle);
1133 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
1136 /* Rename thumbnail file of file system */
1137 if ((!no_thumb) && (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1138 && (strncmp(media_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
1139 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1141 media_svc_error("_media_svc_rename_file failed : %d", ret);
1146 SQLITE3_FINALIZE(sql_stmt);
1148 ret = _media_svc_sql_end_trans(handle);
1149 if (ret != MEDIA_INFO_ERROR_NONE) {
1150 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
1151 _media_svc_sql_rollback_trans(handle);
1155 media_svc_debug("Folder update is successful. Sending noti for this");
1156 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1157 _media_svc_destroy_noti_item(noti_item);
1159 return MEDIA_INFO_ERROR_NONE;
1162 int media_svc_request_update_db(const char *db_query)
1164 int ret = MEDIA_INFO_ERROR_NONE;
1166 media_svc_retvm_if(!STRING_VALID(db_query), MEDIA_INFO_ERROR_INVALID_PARAMETER, "db_query is NULL");
1168 ret = _media_svc_request_update_db(db_query);
1170 return _media_svc_error_convert(ret);
1173 int media_svc_send_dir_update_noti(MediaSvcHandle *handle, const char *dir_path)
1175 int ret = MEDIA_INFO_ERROR_NONE;
1176 sqlite3 * db_handle = (sqlite3 *)handle;
1178 media_svc_retvm_if(!STRING_VALID(dir_path), MEDIA_INFO_ERROR_INVALID_PARAMETER, "dir_path is NULL");
1180 /* Get notification info */
1181 media_svc_noti_item *noti_item = NULL;
1182 ret = _media_svc_get_noti_info(db_handle, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1183 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
1185 ret = _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL);
1186 _media_svc_destroy_noti_item(noti_item);
1191 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *folder_path, int *count)
1193 sqlite3 * db_handle = (sqlite3 *)handle;
1195 media_svc_debug_func();
1197 media_svc_retvm_if(db_handle == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "Handle is NULL");
1198 media_svc_retvm_if(folder_path == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "folder_path is NULL");
1199 media_svc_retvm_if(count == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "count is NULL");
1201 return _media_svc_count_invalid_folder_items(db_handle, folder_path, count);