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.
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-env.h"
31 #include "media-svc-media-folder.h"
32 #include "media-svc-album.h"
33 #include "media-svc-noti.h"
34 #include "media-svc-storage.h"
36 static __thread int g_media_svc_item_validity_data_cnt = 1;
37 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
39 static __thread int g_media_svc_move_item_data_cnt = 1;
40 static __thread int g_media_svc_move_item_cur_data_cnt = 0;
42 static __thread int g_media_svc_insert_item_data_cnt = 1;
43 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
45 static __thread int g_media_svc_update_item_data_cnt = 1;
46 static __thread int g_media_svc_update_item_cur_data_cnt = 0;
48 static __thread int g_media_svc_insert_folder_data_cnt = 1;
49 static __thread int g_media_svc_insert_folder_cur_data_cnt = 0;
51 /* Flag for items to be published by notification */
52 static __thread int g_insert_with_noti = FALSE;
54 #define DEFAULT_MEDIA_SVC_STORAGE_ID "media"
55 #define BATCH_REQUEST_MAX 300
59 } media_svc_item_info_s;
61 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type, bool check_all)
63 if (check_all == TRUE) {
64 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
65 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
66 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
67 media_svc_error("storage type is incorrect[%d]", storage_type);
71 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
72 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
73 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
74 media_svc_error("storage type is incorrect[%d]", storage_type);
82 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
84 int ret = MS_MEDIA_ERR_NONE;
85 MediaDBHandle *db_handle = NULL;
87 media_svc_debug_fenter();
89 ret = media_db_connect(&db_handle, uid, need_write);
90 if (ret != MS_MEDIA_ERR_NONE)
94 return MS_MEDIA_ERR_NONE;
97 int media_svc_disconnect(MediaSvcHandle *handle)
99 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
101 media_svc_debug_fenter();
103 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
105 int ret = MS_MEDIA_ERR_NONE;
107 ret = media_db_disconnect(db_handle);
111 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
113 sqlite3 *db_handle = (sqlite3 *)handle;
115 return _media_svc_get_user_version(db_handle, user_version);
118 int media_svc_create_table(uid_t uid)
120 int ret = MS_MEDIA_ERR_NONE;
123 media_svc_debug_fenter();
125 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
126 if (ret != MS_MEDIA_ERR_NONE) {
127 media_svc_error("_media_svc_init_table_query fail.");
131 /*create media table*/
132 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
133 if (ret != MS_MEDIA_ERR_NONE) {
134 media_svc_error("_media_svc_make_table_query fail.");
138 /*create folder table*/
139 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
140 if (ret != MS_MEDIA_ERR_NONE) {
141 media_svc_error("_media_svc_make_table_query fail.");
145 /*create playlist_map table*/
146 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
147 if (ret != MS_MEDIA_ERR_NONE) {
148 media_svc_error("_media_svc_make_table_query fail.");
152 /*create playlist table*/
153 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
154 if (ret != MS_MEDIA_ERR_NONE) {
155 media_svc_error("_media_svc_make_table_query fail.");
159 /* create album table*/
160 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
161 if (ret != MS_MEDIA_ERR_NONE) {
162 media_svc_error("_media_svc_make_table_query fail.");
166 /*create tag_map table*/
167 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
168 if (ret != MS_MEDIA_ERR_NONE) {
169 media_svc_error("_media_svc_make_table_query fail.");
174 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
175 if (ret != MS_MEDIA_ERR_NONE) {
176 media_svc_error("_media_svc_make_table_query fail.");
180 /*create bookmark table*/
181 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
182 if (ret != MS_MEDIA_ERR_NONE) {
183 media_svc_error("_media_svc_make_table_query fail.");
187 /*create storage table from tizen 2.4 */
188 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
189 if (ret != MS_MEDIA_ERR_NONE) {
190 media_svc_error("_media_svc_make_table_query fail.");
194 /*create uhd table from tizen 3.0 */
195 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_UHD, MEDIA_SVC_DB_LIST_UHD, uid);
196 if (ret != MS_MEDIA_ERR_NONE) {
197 media_svc_error("_media_svc_make_table_query fail.");
201 /*create pvr table from tizen 3.0 */
202 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PVR, MEDIA_SVC_DB_LIST_PVR, uid);
203 if (ret != MS_MEDIA_ERR_NONE) {
204 media_svc_error("_media_svc_make_table_query fail.");
208 /*init storage table*/
209 ret = _media_svc_init_storage(db_handle, uid);
210 if (ret != MS_MEDIA_ERR_NONE) {
211 media_svc_error("_media_svc_make_table_query fail.");
216 /*create face table. from tizen 3.0*/
217 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
218 if (ret != MS_MEDIA_ERR_NONE) {
219 media_svc_error("_media_svc_make_table_query fail.");
223 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
224 if (ret != MS_MEDIA_ERR_NONE) {
225 media_svc_error("_media_svc_make_table_query fail.");
229 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
230 ret = _media_svc_sql_query(sql, uid);
231 if (ret != MS_MEDIA_ERR_NONE) {
232 media_svc_error("user_version update fail.");
236 _media_svc_destroy_table_query();
238 media_svc_debug_fleave();
240 return MS_MEDIA_ERR_NONE;
242 _media_svc_destroy_table_query();
244 media_svc_debug_fleave();
249 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
251 int ret = MS_MEDIA_ERR_NONE;
252 media_svc_storage_type_e type;
254 ret = _media_svc_get_storage_type_by_path(path, &type, uid);
255 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
257 *storage_type = type;
262 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
264 int ret = MS_MEDIA_ERR_NONE;
265 sqlite3 *db_handle = (sqlite3 *)handle;
267 media_svc_retvm_if(db_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");
270 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
275 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
277 int ret = MS_MEDIA_ERR_NONE;
278 sqlite3 *db_handle = (sqlite3 *)handle;
281 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
282 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
283 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
285 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
286 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
289 media_svc_debug("item is exist in database");
290 return MS_MEDIA_ERR_NONE;
292 media_svc_debug("item is not exist in database");
293 return MS_MEDIA_ERR_DB_NO_RECORD;
296 return MS_MEDIA_ERR_NONE;
299 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
301 media_svc_debug("Transaction data count : [%d]", data_cnt);
303 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
305 g_media_svc_insert_item_data_cnt = data_cnt;
306 g_media_svc_insert_item_cur_data_cnt = 0;
308 /* Prepare for making noti item list */
310 media_svc_debug("making noti list from pid[%d]", from_pid);
311 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
312 return MS_MEDIA_ERR_OUT_OF_MEMORY;
314 _media_svc_set_noti_from_pid(from_pid);
315 g_insert_with_noti = TRUE;
318 return MS_MEDIA_ERR_NONE;
321 int media_svc_insert_item_end(uid_t uid)
323 int ret = MS_MEDIA_ERR_NONE;
325 media_svc_debug_fenter();
327 if (g_media_svc_insert_item_cur_data_cnt > 0) {
329 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
330 if (g_insert_with_noti) {
331 media_svc_debug("sending noti list");
332 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
333 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
334 g_insert_with_noti = FALSE;
335 _media_svc_set_noti_from_pid(-1);
339 g_media_svc_insert_item_data_cnt = 1;
340 g_media_svc_insert_item_cur_data_cnt = 0;
345 int media_svc_insert_item_bulk(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid)
347 int ret = MS_MEDIA_ERR_NONE;
348 sqlite3 *db_handle = (sqlite3 *)handle;
349 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
350 media_svc_media_type_e media_type;
352 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
353 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
354 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
355 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
357 media_svc_content_info_s content_info;
358 memset(&content_info, 0, sizeof(media_svc_content_info_s));
361 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
362 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
363 if (ret != MS_MEDIA_ERR_NONE)
366 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
367 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
368 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
369 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
370 media_svc_debug("Do nothing[%d]", media_type);
371 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
372 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
374 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
375 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
377 /*Set or Get folder id*/
378 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
379 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
381 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
382 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
384 if (g_media_svc_insert_item_data_cnt == 1) {
386 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
387 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
389 if (g_insert_with_noti)
390 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
392 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
394 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
395 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
397 if (g_insert_with_noti)
398 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
400 g_media_svc_insert_item_cur_data_cnt++;
402 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
404 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
405 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
407 if (g_insert_with_noti)
408 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
410 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
411 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
413 if (g_insert_with_noti) {
414 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
415 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
417 /* Recreate noti list */
418 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
419 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
422 g_media_svc_insert_item_cur_data_cnt = 0;
425 media_svc_error("Error in media_svc_insert_item_bulk");
426 _media_svc_destroy_content_info(&content_info);
427 return MS_MEDIA_ERR_INTERNAL;
430 _media_svc_destroy_content_info(&content_info);
432 return MS_MEDIA_ERR_NONE;
435 int media_svc_insert_item_immediately(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
437 int ret = MS_MEDIA_ERR_NONE;
438 sqlite3 *db_handle = (sqlite3 *)handle;
439 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
440 media_svc_media_type_e media_type;
442 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
443 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
444 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
445 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
447 media_svc_content_info_s content_info;
448 memset(&content_info, 0, sizeof(media_svc_content_info_s));
451 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
452 if (ret != MS_MEDIA_ERR_NONE)
455 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
456 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
457 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
458 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
460 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
461 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
463 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
466 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
468 /*Set or Get folder id*/
469 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
470 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
472 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
473 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
475 /* Extracting thumbnail */
476 if (content_info.thumbnail_path == NULL) {
477 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
478 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
482 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
483 if (ret == MS_MEDIA_ERR_NONE)
484 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
486 if (content_info.media_meta.width <= 0)
487 content_info.media_meta.width = width;
489 if (content_info.media_meta.height <= 0)
490 content_info.media_meta.height = height;
494 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
496 if (ret == MS_MEDIA_ERR_NONE) {
497 media_svc_debug("Insertion is successful. Sending noti for this");
498 _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);
499 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
500 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
503 _media_svc_destroy_content_info(&content_info);
507 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
508 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
510 int ret = MS_MEDIA_ERR_NONE;
511 sqlite3 *db_handle = (sqlite3 *)handle;
512 char *file_name = NULL;
513 char *folder_path = NULL;
514 int modified_time = 0;
515 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
516 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
517 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
520 media_svc_debug_fenter();
522 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
523 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
524 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
525 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
526 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
528 /*check and update folder*/
529 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
530 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
533 file_name = g_path_get_basename(dest_path);
535 /*get modified_time*/
536 modified_time = _media_svc_get_file_time(dest_path);
538 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
539 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
540 if (ret != MS_MEDIA_ERR_NONE) {
541 media_svc_error("_media_svc_get_media_type_by_path failed");
542 SAFE_FREE(file_name);
546 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
547 /*get old thumbnail_path*/
548 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
549 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
550 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
551 SAFE_FREE(file_name);
555 /* If old thumb path is default or not */
556 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
557 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
558 _strncpy_safe(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
560 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
562 SAFE_FREE(default_thumbnail_path);
565 if (g_media_svc_move_item_data_cnt == 1) {
568 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
569 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
571 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
573 SAFE_FREE(file_name);
574 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
576 media_svc_debug("Move is successful. Sending noti for this");
578 /* Get notification info */
579 media_svc_noti_item *noti_item = NULL;
580 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
581 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
583 /* Send notification for move */
584 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
585 _media_svc_destroy_noti_item(noti_item);
587 /*update folder modified_time*/
588 folder_path = g_path_get_dirname(dest_path);
589 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
590 SAFE_FREE(folder_path);
591 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
593 ret = _media_svc_update_folder_table(storage_id, uid);
594 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
596 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
599 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
600 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
602 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
604 SAFE_FREE(file_name);
605 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
607 /*update folder modified_time*/
608 folder_path = g_path_get_dirname(dest_path);
609 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
610 SAFE_FREE(folder_path);
611 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
613 g_media_svc_move_item_cur_data_cnt++;
615 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
617 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
618 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
620 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
622 SAFE_FREE(file_name);
623 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
625 /*update folder modified_time*/
626 folder_path = g_path_get_dirname(dest_path);
627 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
628 SAFE_FREE(folder_path);
629 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
632 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
633 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
635 g_media_svc_move_item_cur_data_cnt = 0;
638 media_svc_error("Error in media_svc_move_item");
639 SAFE_FREE(file_name);
640 return MS_MEDIA_ERR_INTERNAL;
643 /*rename thumbnail file*/
644 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
645 if ((strlen(old_thumb_path) > 0) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
646 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
647 if (ret != MS_MEDIA_ERR_NONE)
648 media_svc_error("_media_svc_rename_file failed : %d", ret);
652 return MS_MEDIA_ERR_NONE;
655 int media_svc_set_item_validity_begin(int data_cnt)
657 media_svc_debug("Transaction data count : [%d]", data_cnt);
659 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
661 g_media_svc_item_validity_data_cnt = data_cnt;
662 g_media_svc_item_validity_cur_data_cnt = 0;
664 return MS_MEDIA_ERR_NONE;
667 int media_svc_set_item_validity_end(uid_t uid)
669 int ret = MS_MEDIA_ERR_NONE;
671 media_svc_debug_fenter();
673 if (g_media_svc_item_validity_cur_data_cnt > 0)
674 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
676 g_media_svc_item_validity_data_cnt = 1;
677 g_media_svc_item_validity_cur_data_cnt = 0;
682 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
684 int ret = MS_MEDIA_ERR_NONE;
686 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
687 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
689 media_svc_debug("path=[%s], validity=[%d]", path, validity);
691 if (g_media_svc_item_validity_data_cnt == 1) {
693 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
695 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
697 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
698 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
700 g_media_svc_item_validity_cur_data_cnt++;
702 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
704 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
705 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
707 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
708 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
710 g_media_svc_item_validity_cur_data_cnt = 0;
714 media_svc_error("Error in media_svc_set_item_validity");
715 return MS_MEDIA_ERR_INTERNAL;
718 return MS_MEDIA_ERR_NONE;
721 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
723 int ret = MS_MEDIA_ERR_NONE;
724 sqlite3 *db_handle = (sqlite3 *)handle;
725 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
727 media_svc_debug_fenter();
729 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
730 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
731 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
734 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
735 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
738 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
739 /*Get thumbnail path to delete*/
740 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
741 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
742 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
744 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
745 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
748 /*Get thumbnail path to delete*/
749 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
750 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
754 /*Get thumbnail path to delete*/
755 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
756 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
758 if (g_media_svc_insert_item_data_cnt == 1) {
760 /* Get notification info */
761 media_svc_noti_item *noti_item = NULL;
762 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
763 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
766 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
767 if (ret != MS_MEDIA_ERR_NONE) {
768 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
769 _media_svc_destroy_noti_item(noti_item);
774 /* Send notification */
775 media_svc_debug("Deletion is successful. Sending noti for this");
776 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
777 _media_svc_destroy_noti_item(noti_item);
779 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
780 if (ret != MS_MEDIA_ERR_NONE) {
781 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
788 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
789 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
792 // Get count of media, which contains same thumbnail for music
793 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
794 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
795 if (ret != MS_MEDIA_ERR_NONE) {
796 media_svc_error("Failed to get thumbnail count : %d", ret);
800 if (thumb_count == 1) {
802 ret = _media_svc_remove_file(thumb_path);
803 if (ret != MS_MEDIA_ERR_NONE)
804 media_svc_error("fail to remove thumbnail file.");
808 SAFE_FREE(default_thumbnail_path);
810 return MS_MEDIA_ERR_NONE;
813 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
815 int ret = MS_MEDIA_ERR_NONE;
817 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
819 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
820 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
822 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
823 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
825 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
826 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
827 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
829 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
831 /* remove thumbnails */
832 if (STRING_VALID(dirpath))
833 ret = _media_svc_remove_all_files_in_dir(dirpath);
834 SAFE_FREE(internal_thumb_path);
835 SAFE_FREE(external_thumb_path);
836 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
839 return MS_MEDIA_ERR_NONE;
842 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
844 sqlite3 *db_handle = (sqlite3 *)handle;
846 media_svc_debug_fenter();
848 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
849 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
850 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
852 /*Delete from DB and remove thumbnail files*/
853 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
856 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
858 sqlite3 *db_handle = (sqlite3 *)handle;
860 media_svc_debug_fenter();
862 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
863 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
865 /*Delete from DB and remove thumbnail files*/
866 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
869 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
871 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
872 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
874 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
877 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
879 sqlite3 *db_handle = (sqlite3 *)handle;
881 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
882 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
883 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
886 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
888 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
891 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
893 int ret = MS_MEDIA_ERR_NONE;
894 sqlite3 *db_handle = (sqlite3 *)handle;
895 media_svc_media_type_e media_type;
897 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
898 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
899 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
900 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
902 media_svc_content_info_s content_info;
903 memset(&content_info, 0, sizeof(media_svc_content_info_s));
906 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
907 if (ret != MS_MEDIA_ERR_NONE)
910 /* Initialize thumbnail information to remake thumbnail. */
911 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
912 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
913 if (ret != MS_MEDIA_ERR_NONE) {
914 _media_svc_destroy_content_info(&content_info);
918 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
919 ret = _media_svc_remove_file(thumb_path);
920 if (ret != MS_MEDIA_ERR_NONE)
921 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
924 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
925 if (ret != MS_MEDIA_ERR_NONE) {
926 _media_svc_destroy_content_info(&content_info);
930 /* Get notification info */
931 media_svc_noti_item *noti_item = NULL;
932 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
933 if (ret != MS_MEDIA_ERR_NONE) {
934 _media_svc_destroy_content_info(&content_info);
938 media_type = noti_item->media_type;
939 content_info.media_type = media_type;
941 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
942 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
943 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
944 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
945 media_svc_debug("Do nothing [%d]", media_type);
946 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
947 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
949 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
951 if (ret != MS_MEDIA_ERR_NONE) {
952 _media_svc_destroy_noti_item(noti_item);
953 _media_svc_destroy_content_info(&content_info);
957 /* Extracting thumbnail */
958 if (content_info.thumbnail_path == NULL) {
959 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
960 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
964 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
965 if (ret == MS_MEDIA_ERR_NONE)
966 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
968 if (content_info.media_meta.width <= 0)
969 content_info.media_meta.width = width;
971 if (content_info.media_meta.height <= 0)
972 content_info.media_meta.height = height;
976 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
978 if (ret == MS_MEDIA_ERR_NONE) {
979 media_svc_debug("Update is successful. Sending noti for this");
980 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
982 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
985 _media_svc_destroy_content_info(&content_info);
986 _media_svc_destroy_noti_item(noti_item);
991 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
993 sqlite3 *db_handle = (sqlite3 *)handle;
994 int ret = MS_MEDIA_ERR_NONE;
996 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
997 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
998 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
1000 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1002 ret = _media_svc_sql_begin_trans(uid);
1003 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1005 /* Update all folder record's modified date, which are changed above */
1006 char *update_folder_modified_time_sql = NULL;
1010 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1012 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1013 SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
1015 if (ret != SQLITE_OK) {
1016 media_svc_error("failed to update folder modified time");
1017 _media_svc_sql_rollback_trans(uid);
1019 return MS_MEDIA_ERR_DB_INTERNAL;
1022 /* Update all items */
1023 char *select_all_sql = NULL;
1024 sqlite3_stmt *sql_stmt = NULL;
1025 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1027 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1029 select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from '%q' where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", storage_id, dst_path, dst_child_path);
1031 media_svc_debug("[SQL query] : %s", select_all_sql);
1033 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1034 if (ret != MS_MEDIA_ERR_NONE) {
1035 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1036 _media_svc_sql_rollback_trans(uid);
1040 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1041 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1042 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1043 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1044 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1045 /*int media_type; */
1046 bool no_thumb = FALSE;
1048 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1049 _strncpy_safe(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1051 media_svc_error("media UUID is NULL");
1052 return MS_MEDIA_ERR_DB_INTERNAL;
1055 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1056 _strncpy_safe(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1058 media_svc_error("media path is NULL");
1059 return MS_MEDIA_ERR_DB_INTERNAL;
1062 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1063 _strncpy_safe(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1065 media_svc_debug("media thumb path doesn't exist in DB");
1069 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1071 /* Update path, thumbnail path of this item */
1072 char *replaced_path = NULL;
1073 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1074 if (replaced_path == NULL) {
1075 media_svc_error("_media_svc_replace_path failed");
1076 SQLITE3_FINALIZE(sql_stmt);
1077 _media_svc_sql_rollback_trans(uid);
1078 return MS_MEDIA_ERR_INTERNAL;
1081 media_svc_debug("New media path : %s", replaced_path);
1082 media_svc_storage_type_e storage_type;
1085 /* If old thumb path is default or not */
1086 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1087 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1088 _strncpy_safe(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1090 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1091 if (ret != MS_MEDIA_ERR_NONE) {
1092 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1093 SAFE_FREE(replaced_path);
1094 SAFE_FREE(default_thumbnail_path);
1095 _media_svc_sql_rollback_trans(uid);
1099 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1100 if (ret != MS_MEDIA_ERR_NONE) {
1101 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1102 SAFE_FREE(replaced_path);
1103 SAFE_FREE(default_thumbnail_path);
1104 SQLITE3_FINALIZE(sql_stmt);
1105 _media_svc_sql_rollback_trans(uid);
1110 SAFE_FREE(default_thumbnail_path);
1112 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1115 char *update_item_sql = NULL;
1118 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1121 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1122 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
1124 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_thumb_path, media_uuid);
1126 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
1130 ret = media_db_request_update_db_batch(update_item_sql, uid);
1131 SQLITE3_SAFE_FREE(update_item_sql);
1132 SAFE_FREE(replaced_path);
1134 if (ret != SQLITE_OK) {
1135 media_svc_error("failed to update item");
1136 SQLITE3_FINALIZE(sql_stmt);
1137 _media_svc_sql_rollback_trans(uid);
1139 return MS_MEDIA_ERR_DB_INTERNAL;
1142 /* Rename thumbnail file of file system */
1143 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1144 if ((!no_thumb) && (default_thumbnail_path != NULL) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1145 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1146 if (ret != MS_MEDIA_ERR_NONE)
1147 media_svc_error("_media_svc_rename_file failed : %d", ret);
1150 SAFE_FREE(default_thumbnail_path);
1153 SQLITE3_FINALIZE(sql_stmt);
1155 ret = _media_svc_sql_end_trans(uid);
1156 if (ret != MS_MEDIA_ERR_NONE) {
1157 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1158 _media_svc_sql_rollback_trans(uid);
1162 media_svc_debug("Folder update is successful. Sending noti for this");
1163 /* Get notification info */
1164 media_svc_noti_item *noti_item = NULL;
1165 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1166 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1168 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1169 _media_svc_destroy_noti_item(noti_item);
1171 return MS_MEDIA_ERR_NONE;
1174 int media_svc_request_update_db(const char *db_query, uid_t uid)
1176 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1178 return _media_svc_sql_query(db_query, uid);
1181 int media_svc_send_dir_update_noti(MediaSvcHandle *handle, const char *storage_id, const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
1183 int ret = MS_MEDIA_ERR_NONE;
1184 sqlite3 *db_handle = (sqlite3 *)handle;
1187 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1188 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1190 /* Get notification info */
1191 media_svc_noti_item *noti_item = NULL;
1192 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1193 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1195 if (folder_id != NULL) {
1196 uuid = strndup(folder_id, strlen(folder_id));
1198 if (noti_item->media_uuid != NULL) {
1199 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1201 _media_svc_destroy_noti_item(noti_item);
1202 media_svc_error("folder uuid is wrong");
1203 return MS_MEDIA_ERR_DB_INTERNAL;
1207 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1208 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1209 _media_svc_destroy_noti_item(noti_item);
1215 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1217 sqlite3 *db_handle = (sqlite3 *)handle;
1219 media_svc_debug_fenter();
1221 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1222 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1223 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1224 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1226 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1229 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1231 sqlite3 *db_handle = (sqlite3 *)handle;
1233 media_svc_debug_fenter();
1235 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1237 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1240 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1242 sqlite3 *db_handle = (sqlite3 *)handle;
1244 media_svc_debug_fenter();
1246 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1248 return _media_db_check_corrupt(db_handle);
1251 int media_svc_get_folder_list(MediaSvcHandle *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
1253 sqlite3 *db_handle = (sqlite3 *)handle;
1255 media_svc_debug_fenter();
1257 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1258 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1260 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1263 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1265 int ret = MS_MEDIA_ERR_NONE;
1266 sqlite3 *db_handle = (sqlite3 *)handle;
1267 time_t sto_time = 0;
1268 int cur_time = _media_svc_get_file_time(folder_path);
1269 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1271 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1273 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1274 if (ret == MS_MEDIA_ERR_NONE) {
1275 if (sto_time != cur_time)
1276 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1282 int media_svc_update_item_begin(int data_cnt)
1284 media_svc_debug("Transaction data count : [%d]", data_cnt);
1286 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1288 g_media_svc_update_item_data_cnt = data_cnt;
1289 g_media_svc_update_item_cur_data_cnt = 0;
1291 return MS_MEDIA_ERR_NONE;
1294 int media_svc_update_item_end(uid_t uid)
1296 int ret = MS_MEDIA_ERR_NONE;
1298 media_svc_debug_fenter();
1300 if (g_media_svc_update_item_cur_data_cnt > 0)
1301 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1303 g_media_svc_update_item_data_cnt = 1;
1304 g_media_svc_update_item_cur_data_cnt = 0;
1309 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1311 int ret = MS_MEDIA_ERR_NONE;
1312 sqlite3 *db_handle = (sqlite3 *)handle;
1314 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1316 media_svc_media_type_e media_type;
1317 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1318 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1320 media_svc_content_info_s content_info;
1321 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1324 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1325 if (ret != MS_MEDIA_ERR_NONE)
1328 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1329 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1331 return MS_MEDIA_ERR_NONE;
1333 if (ret != MS_MEDIA_ERR_NONE) {
1334 _media_svc_destroy_content_info(&content_info);
1338 if (g_media_svc_update_item_data_cnt == 1) {
1340 ret = _media_svc_update_meta_with_data(&content_info);
1341 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1343 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1345 ret = _media_svc_update_meta_with_data(&content_info);
1346 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1348 g_media_svc_update_item_cur_data_cnt++;
1350 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1352 ret = _media_svc_update_meta_with_data(&content_info);
1353 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1355 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1356 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1358 g_media_svc_update_item_cur_data_cnt = 0;
1361 media_svc_error("Error in media_svc_update_item_meta");
1362 _media_svc_destroy_content_info(&content_info);
1363 return MS_MEDIA_ERR_INTERNAL;
1366 _media_svc_destroy_content_info(&content_info);
1371 int media_svc_publish_noti(media_item_type_e update_item, media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
1373 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1376 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1378 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1380 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1383 int media_svc_check_pinyin_support(bool *support)
1385 *support = _media_svc_check_pinyin_support();
1387 return MS_MEDIA_ERR_NONE;
1390 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1392 int ret = MS_MEDIA_ERR_NONE;
1393 sqlite3 * db_handle = (sqlite3 *)handle;
1395 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1397 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1398 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1400 ret = _media_svc_update_media_view(db_handle, uid);
1401 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1406 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1408 int ret = MS_MEDIA_ERR_NONE;
1409 sqlite3 * db_handle = (sqlite3 *)handle;
1411 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1412 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1414 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1419 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1421 int ret = MS_MEDIA_ERR_NONE;
1422 sqlite3 * db_handle = (sqlite3 *)handle;
1424 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1425 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1427 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1432 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1434 int ret = MS_MEDIA_ERR_NONE;
1435 sqlite3 * db_handle = (sqlite3 *)handle;
1437 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1438 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1440 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1445 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1447 int ret = MS_MEDIA_ERR_NONE;
1449 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1454 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1456 sqlite3 * db_handle = (sqlite3 *)handle;
1458 media_svc_debug_fenter();
1460 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1461 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1463 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1466 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1468 int ret = MS_MEDIA_ERR_NONE;
1470 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1471 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1473 if (content_info->added_time > 0)
1474 new_content_info->added_time = content_info->added_time;
1475 new_content_info->last_played_time = content_info->last_played_time;
1476 new_content_info->played_count = content_info->played_count;
1477 new_content_info->favourate = content_info->favourate;
1479 if (STRING_VALID(content_info->file_name)) {
1480 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1481 if (ret != MS_MEDIA_ERR_NONE)
1482 media_svc_error("strcpy file_name failed");
1485 if (STRING_VALID(content_info->media_meta.title)) {
1486 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1487 if (ret != MS_MEDIA_ERR_NONE)
1488 media_svc_error("strcpy title failed");
1491 if (STRING_VALID(content_info->media_meta.album)) {
1492 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1493 if (ret != MS_MEDIA_ERR_NONE)
1494 media_svc_error("strcpy album failed");
1497 if (STRING_VALID(content_info->media_meta.artist)) {
1498 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1499 if (ret != MS_MEDIA_ERR_NONE)
1500 media_svc_error("strcpy artist failed");
1503 if (STRING_VALID(content_info->media_meta.genre)) {
1504 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1505 if (ret != MS_MEDIA_ERR_NONE)
1506 media_svc_error("strcpy genre failed");
1509 if (STRING_VALID(content_info->media_meta.composer)) {
1510 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1511 if (ret != MS_MEDIA_ERR_NONE)
1512 media_svc_error("strcpy composer failed");
1515 if (STRING_VALID(content_info->media_meta.year)) {
1516 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1517 if (ret != MS_MEDIA_ERR_NONE)
1518 media_svc_error("strcpy year failed");
1521 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1522 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1523 if (ret != MS_MEDIA_ERR_NONE)
1524 media_svc_error("strcpy recorded_date failed");
1527 if (STRING_VALID(content_info->media_meta.copyright)) {
1528 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1529 if (ret != MS_MEDIA_ERR_NONE)
1530 media_svc_error("strcpy copyright failed");
1533 if (STRING_VALID(content_info->media_meta.track_num)) {
1534 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1535 if (ret != MS_MEDIA_ERR_NONE)
1536 media_svc_error("strcpy track_num failed");
1539 if (STRING_VALID(content_info->media_meta.description)) {
1540 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1541 if (ret != MS_MEDIA_ERR_NONE)
1542 media_svc_error("strcpy description failed");
1545 if (STRING_VALID(content_info->media_meta.weather)) {
1546 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1547 if (ret != MS_MEDIA_ERR_NONE)
1548 media_svc_error("strcpy weather failed");
1551 if (STRING_VALID(content_info->media_meta.category)) {
1552 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1553 if (ret != MS_MEDIA_ERR_NONE)
1554 media_svc_error("strcpy category failed");
1557 if (STRING_VALID(content_info->media_meta.keyword)) {
1558 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1559 if (ret != MS_MEDIA_ERR_NONE)
1560 media_svc_error("strcpy keyword failed");
1563 if (STRING_VALID(content_info->media_meta.location_tag)) {
1564 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1565 if (ret != MS_MEDIA_ERR_NONE)
1566 media_svc_error("strcpy location_tag failed");
1569 if (STRING_VALID(content_info->media_meta.content_name)) {
1570 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1571 if (ret != MS_MEDIA_ERR_NONE)
1572 media_svc_error("strcpy content_name failed");
1575 if (STRING_VALID(content_info->media_meta.age_rating)) {
1576 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1577 if (ret != MS_MEDIA_ERR_NONE)
1578 media_svc_error("strcpy age_rating failed");
1581 if (STRING_VALID(content_info->media_meta.author)) {
1582 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1583 if (ret != MS_MEDIA_ERR_NONE)
1584 media_svc_error("strcpy author failed");
1587 if (STRING_VALID(content_info->media_meta.provider)) {
1588 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1589 if (ret != MS_MEDIA_ERR_NONE)
1590 media_svc_error("strcpy provider failed");
1593 if (STRING_VALID(content_info->media_meta.datetaken)) {
1594 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1595 if (ret != MS_MEDIA_ERR_NONE)
1596 media_svc_error("strcpy datetaken failed");
1598 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1599 if (new_content_info->timeline == 0) {
1600 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1601 new_content_info->timeline = new_content_info->modified_time;
1603 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1607 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1608 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1609 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1610 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1611 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1613 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1614 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1615 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1616 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1617 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1618 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1620 new_content_info->media_meta.rating = content_info->media_meta.rating;
1625 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1627 int ret = MS_MEDIA_ERR_NONE;
1628 sqlite3 *db_handle = (sqlite3 *)handle;
1629 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1630 bool append_album = FALSE;
1632 /* Checking parameters if they are valid */
1633 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1634 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1635 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1637 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1639 media_svc_content_info_s _new_content_info;
1640 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1642 media_svc_media_type_e media_type;
1644 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1645 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1647 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1648 media_svc_debug("Do nothing[%d]", media_type);
1649 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1650 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1652 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1654 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1656 /* Extracting thumbnail */
1657 if (_new_content_info.thumbnail_path == NULL) {
1658 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1659 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1663 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1664 if (ret == MS_MEDIA_ERR_NONE)
1665 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1667 if (_new_content_info.media_meta.width <= 0)
1668 _new_content_info.media_meta.width = width;
1670 if (_new_content_info.media_meta.height <= 0)
1671 _new_content_info.media_meta.height = height;
1675 /* set othere data to the structure, which is passed as parameters */
1676 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1678 /* Set or Get folder id */
1679 ret = _media_svc_get_and_append_folder_id_by_path(handle, _new_content_info.storage_uuid, _new_content_info.path, _new_content_info.storage_type, folder_uuid, uid);
1680 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1682 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1683 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1685 /* register album table data */
1688 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1689 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1691 if (ret != MS_MEDIA_ERR_NONE) {
1692 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1693 media_svc_debug("album does not exist. So start to make album art");
1694 append_album = TRUE;
1696 media_svc_debug("other error");
1697 append_album = FALSE;
1700 _new_content_info.album_id = album_id;
1701 append_album = FALSE;
1703 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1704 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1705 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1708 media_svc_debug("album already exists. don't need to make album art");
1709 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1710 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1715 if (append_album == TRUE) {
1716 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1717 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1718 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, _new_content_info.thumbnail_path, &album_id, uid);
1720 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1722 _new_content_info.album_id = album_id;
1725 /* Insert to db - calling _media_svc_insert_item_with_data */
1726 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1728 if (ret == MS_MEDIA_ERR_NONE)
1729 media_svc_debug("Insertion is successful.");
1731 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1732 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1734 _media_svc_destroy_content_info(&_new_content_info);
1736 /* handling returned value - important */
1740 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1742 _media_svc_destroy_content_info(content_info);
1745 int media_svc_generate_uuid(char **uuid)
1747 char *gen_uuid = NULL;
1748 gen_uuid = _media_info_generate_uuid();
1749 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1751 *uuid = strdup(gen_uuid);
1753 return MS_MEDIA_ERR_NONE;
1756 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1758 sqlite3 * db_handle = (sqlite3 *)handle;
1760 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1762 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1765 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1767 sqlite3 * db_handle = (sqlite3 *)handle;
1769 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1770 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1771 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1772 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1774 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1777 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1779 sqlite3 * db_handle = (sqlite3 *)handle;
1781 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1782 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1783 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1785 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1788 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, const char *storage_path, media_svc_storage_type_e storage_type, uid_t uid)
1790 int ret = MS_MEDIA_ERR_NONE;
1791 sqlite3 *db_handle = (sqlite3 *)handle;
1793 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1794 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1795 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1796 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1798 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1799 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1801 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1802 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1803 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1805 ret = _media_svc_update_media_view(db_handle, uid);
1806 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1812 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1814 int ret = MS_MEDIA_ERR_NONE;
1815 sqlite3 *db_handle = (sqlite3 *)handle;
1816 media_svc_storage_type_e storage_type;
1818 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1819 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1821 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1822 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1824 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1825 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1827 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1828 if (ret != MS_MEDIA_ERR_NONE)
1829 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1831 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1832 ret = _media_svc_drop_media_table(storage_id, uid);
1833 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1835 ret = _media_svc_update_media_view(db_handle, uid);
1836 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1842 int media_svc_insert_folder_begin(int data_cnt)
1844 media_svc_debug("Transaction data count : [%d]", data_cnt);
1846 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1848 g_media_svc_insert_folder_data_cnt = data_cnt;
1849 g_media_svc_insert_folder_cur_data_cnt = 0;
1851 return MS_MEDIA_ERR_NONE;
1854 int media_svc_insert_folder_end(uid_t uid)
1856 int ret = MS_MEDIA_ERR_NONE;
1858 media_svc_debug_fenter();
1860 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1861 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1863 g_media_svc_insert_folder_data_cnt = 1;
1864 g_media_svc_insert_folder_cur_data_cnt = 0;
1869 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1871 int ret = MS_MEDIA_ERR_NONE;
1872 sqlite3 * db_handle = (sqlite3 *)handle;
1873 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1875 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1876 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1877 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1878 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1880 if (g_media_svc_insert_folder_data_cnt == 1) {
1882 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1884 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1886 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1887 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1889 g_media_svc_insert_folder_cur_data_cnt++;
1891 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1893 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1894 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1896 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1897 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1899 g_media_svc_insert_folder_cur_data_cnt = 0;
1902 media_svc_error("Error in media_svc_set_insert_folder");
1903 return MS_MEDIA_ERR_INTERNAL;
1909 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1911 int ret = MS_MEDIA_ERR_NONE;
1913 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1918 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1920 int ret = MS_MEDIA_ERR_NONE;
1921 sqlite3 * db_handle = (sqlite3 *)handle;
1923 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1925 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1930 int media_svc_insert_item_pass1(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid)
1932 int ret = MS_MEDIA_ERR_NONE;
1933 sqlite3 * db_handle = (sqlite3 *)handle;
1934 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1935 media_svc_media_type_e media_type;
1937 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1938 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1939 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1940 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1942 media_svc_content_info_s content_info;
1943 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1945 /*Set basic media info*/
1946 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1947 if (ret != MS_MEDIA_ERR_NONE) {
1948 media_svc_error("_media_svc_set_media_info fail %d", ret);
1952 /*Set or Get folder id*/
1953 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1954 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1956 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1957 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1959 if (g_media_svc_insert_item_data_cnt == 1) {
1961 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1962 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1964 if (g_insert_with_noti)
1965 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1966 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1968 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1969 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1971 if (g_insert_with_noti)
1972 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1974 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1975 g_media_svc_insert_item_cur_data_cnt++;
1977 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1979 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1980 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1982 if (g_insert_with_noti)
1983 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1985 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1986 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1988 media_svc_debug("_media_svc_list_query_do over");
1990 if (g_insert_with_noti) {
1991 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1992 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1993 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1995 /* Recreate noti list */
1996 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1997 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2000 g_media_svc_insert_item_cur_data_cnt = 0;
2003 media_svc_error("Error in media_svc_insert_item_pass1");
2004 _media_svc_destroy_content_info(&content_info);
2005 return MS_MEDIA_ERR_INTERNAL;
2008 _media_svc_destroy_content_info(&content_info);
2010 return MS_MEDIA_ERR_NONE;
2013 int media_svc_insert_item_pass2(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int scan_type, const char *extract_path, int is_burst, uid_t uid)
2015 int ret = MS_MEDIA_ERR_NONE;
2016 sqlite3 * db_handle = (sqlite3 *)handle;
2017 sqlite3_stmt *sql_stmt = NULL;
2018 media_svc_media_type_e media_type;
2019 media_svc_content_info_s content_info;
2020 GArray *db_data_array = NULL;
2021 media_svc_item_info_s *db_data = NULL;
2024 media_svc_debug_fenter();
2026 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2027 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2029 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2030 media_svc_error("storage type is incorrect[%d]", storage_type);
2031 return MS_MEDIA_ERR_INVALID_PARAMETER;
2034 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2035 if (db_data_array == NULL) {
2036 media_svc_error("db_data_array is NULL. Out of memory");
2037 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2041 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2042 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2043 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2045 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2046 media_svc_error("folder no recursive extract");
2048 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2050 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' LIMIT %d", storage_id, folder_uuid, BATCH_REQUEST_MAX);
2051 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2052 media_svc_error("folder recursive extract");
2054 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2056 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' LIMIT %d", storage_id, extract_path, BATCH_REQUEST_MAX);
2059 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2061 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2064 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2065 if (ret != MS_MEDIA_ERR_NONE) {
2066 media_svc_error("error when get list. err = [%d]", ret);
2067 g_array_free(db_data_array, FALSE);
2071 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2073 db_data = malloc(sizeof(media_svc_item_info_s));
2074 if (db_data == NULL) {
2075 media_svc_error("Out of memory");
2079 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2080 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2082 g_array_append_val(db_data_array, db_data);
2085 SQLITE3_FINALIZE(sql_stmt);
2086 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2088 while (db_data_array->len != 0) {
2090 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2091 g_array_remove_index(db_data_array, 0);
2093 if ((db_data == NULL) || (db_data->path == NULL)) {
2094 media_svc_error("invalid db data");
2098 media_type = db_data->media_type;
2099 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2100 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2101 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2103 content_info.media_type = media_type;
2104 content_info.storage_type = storage_type;
2106 _media_svc_set_default_value(&content_info, FALSE);
2108 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2109 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2110 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2111 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2112 media_svc_debug("Do nothing[%d]", media_type);
2113 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2114 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2116 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2118 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2120 _media_svc_destroy_content_info(&content_info);
2121 SAFE_FREE(db_data->path);
2127 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2129 _media_svc_publish_noti_list(idx);
2130 _media_svc_destroy_noti_list(idx);
2132 /* Recreate noti list */
2133 ret = _media_svc_create_noti_list(idx);
2137 while (db_data_array->len != 0) {
2139 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2140 g_array_remove_index(db_data_array, 0);
2143 SAFE_FREE(db_data->path);
2149 g_array_free(db_data_array, FALSE);
2150 db_data_array = NULL;
2152 media_svc_debug_fleave();
2154 return MS_MEDIA_ERR_NONE;
2157 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2159 int ret = MS_MEDIA_ERR_NONE;
2160 sqlite3 * db_handle = (sqlite3 *)handle;
2162 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2163 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2165 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2170 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2172 int ret = MS_MEDIA_ERR_NONE;
2174 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2179 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2181 int ret = MS_MEDIA_ERR_NONE;
2182 sqlite3 * db_handle = (sqlite3 *)handle;
2183 time_t modified_time = 0;
2184 int system_time = 0;
2186 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2188 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2190 system_time = _media_svc_get_file_time(path);
2191 media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2193 if (system_time != modified_time && system_time != 0)
2201 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2203 sqlite3 * db_handle = (sqlite3 *)handle;
2205 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2206 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2208 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2211 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2213 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2214 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2216 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2219 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2221 int ret = MS_MEDIA_ERR_NONE;
2222 sqlite3 * db_handle = (sqlite3 *)handle;
2224 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2226 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2231 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2233 int ret = MS_MEDIA_ERR_NONE;
2234 sqlite3 * db_handle = (sqlite3 *)handle;
2237 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2238 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2239 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2241 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2242 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2245 media_svc_debug("item is exist in database");
2246 return MS_MEDIA_ERR_NONE;
2248 media_svc_debug("item is not exist in database");
2249 return MS_MEDIA_ERR_DB_NO_RECORD;
2252 return MS_MEDIA_ERR_NONE;
2255 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2257 int ret = MS_MEDIA_ERR_NONE;
2258 sqlite3 * db_handle = (sqlite3 *)handle;
2261 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2262 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2263 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2266 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2267 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2271 media_svc_debug("item is exist in database");
2272 return MS_MEDIA_ERR_NONE;
2274 media_svc_debug("item is not exist in database");
2275 return MS_MEDIA_ERR_DB_NO_RECORD;
2278 return MS_MEDIA_ERR_NONE;
2281 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2283 int ret = MS_MEDIA_ERR_NONE;
2284 sqlite3 * db_handle = (sqlite3 *)handle;
2286 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2287 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2289 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2294 int media_svc_append_query(const char *query, uid_t uid)
2296 int ret = MS_MEDIA_ERR_NONE;
2298 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2300 ret = _media_svc_append_query_list(query, uid);
2305 int media_svc_send_query(uid_t uid)
2307 int ret = MS_MEDIA_ERR_NONE;
2309 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2314 int media_svc_get_media_type(const char *path, int *mediatype)
2316 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2318 return _media_svc_get_media_type(path, mediatype);