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 ms_user_storage_type_t type = -1;
254 ret = ms_user_get_storage_type(uid, path, &type);
255 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type 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 = NULL;
827 char *external_thumb_path = NULL;
829 ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
830 ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
832 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
833 media_svc_error("fail to get thumbnail path");
834 SAFE_FREE(internal_thumb_path);
835 SAFE_FREE(external_thumb_path);
836 return MS_MEDIA_ERR_INTERNAL;
838 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
840 /* remove thumbnails */
841 if (STRING_VALID(dirpath))
842 ret = _media_svc_remove_all_files_in_dir(dirpath);
843 SAFE_FREE(internal_thumb_path);
844 SAFE_FREE(external_thumb_path);
845 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
848 return MS_MEDIA_ERR_NONE;
851 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
853 sqlite3 *db_handle = (sqlite3 *)handle;
855 media_svc_debug_fenter();
857 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
858 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
859 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
861 /*Delete from DB and remove thumbnail files*/
862 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
865 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
867 sqlite3 *db_handle = (sqlite3 *)handle;
869 media_svc_debug_fenter();
871 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
872 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
874 /*Delete from DB and remove thumbnail files*/
875 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
878 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
880 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
881 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
883 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
886 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
888 sqlite3 *db_handle = (sqlite3 *)handle;
890 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
891 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
892 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
895 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
897 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
900 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
902 int ret = MS_MEDIA_ERR_NONE;
903 sqlite3 *db_handle = (sqlite3 *)handle;
904 media_svc_media_type_e media_type;
906 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
907 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
908 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
909 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
911 media_svc_content_info_s content_info;
912 memset(&content_info, 0, sizeof(media_svc_content_info_s));
915 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
916 if (ret != MS_MEDIA_ERR_NONE)
919 /* Initialize thumbnail information to remake thumbnail. */
920 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
921 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
922 if (ret != MS_MEDIA_ERR_NONE) {
923 _media_svc_destroy_content_info(&content_info);
927 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)) {
928 ret = _media_svc_remove_file(thumb_path);
929 if (ret != MS_MEDIA_ERR_NONE)
930 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
933 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
934 if (ret != MS_MEDIA_ERR_NONE) {
935 _media_svc_destroy_content_info(&content_info);
939 /* Get notification info */
940 media_svc_noti_item *noti_item = NULL;
941 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
942 if (ret != MS_MEDIA_ERR_NONE) {
943 _media_svc_destroy_content_info(&content_info);
947 media_type = noti_item->media_type;
948 content_info.media_type = media_type;
950 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
951 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
952 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
953 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
954 media_svc_debug("Do nothing [%d]", media_type);
955 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
956 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
958 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
960 if (ret != MS_MEDIA_ERR_NONE) {
961 _media_svc_destroy_noti_item(noti_item);
962 _media_svc_destroy_content_info(&content_info);
966 /* Extracting thumbnail */
967 if (content_info.thumbnail_path == NULL) {
968 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
969 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
973 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
974 if (ret == MS_MEDIA_ERR_NONE)
975 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
977 if (content_info.media_meta.width <= 0)
978 content_info.media_meta.width = width;
980 if (content_info.media_meta.height <= 0)
981 content_info.media_meta.height = height;
985 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
987 if (ret == MS_MEDIA_ERR_NONE) {
988 media_svc_debug("Update is successful. Sending noti for this");
989 _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);
991 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
994 _media_svc_destroy_content_info(&content_info);
995 _media_svc_destroy_noti_item(noti_item);
1000 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
1002 sqlite3 *db_handle = (sqlite3 *)handle;
1003 int ret = MS_MEDIA_ERR_NONE;
1005 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1006 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
1007 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
1009 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1011 ret = _media_svc_sql_begin_trans(uid);
1012 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1014 /* Update all folder record's modified date, which are changed above */
1015 char *update_folder_modified_time_sql = NULL;
1019 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1021 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1022 SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
1024 if (ret != SQLITE_OK) {
1025 media_svc_error("failed to update folder modified time");
1026 _media_svc_sql_rollback_trans(uid);
1028 return MS_MEDIA_ERR_DB_INTERNAL;
1031 /* Update all items */
1032 char *select_all_sql = NULL;
1033 sqlite3_stmt *sql_stmt = NULL;
1034 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1036 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1038 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);
1040 media_svc_debug("[SQL query] : %s", select_all_sql);
1042 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1043 if (ret != MS_MEDIA_ERR_NONE) {
1044 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1045 _media_svc_sql_rollback_trans(uid);
1049 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1050 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1051 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1052 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1053 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1054 /*int media_type; */
1055 bool no_thumb = FALSE;
1057 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1058 _strncpy_safe(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1060 media_svc_error("media UUID is NULL");
1061 return MS_MEDIA_ERR_DB_INTERNAL;
1064 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1065 _strncpy_safe(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1067 media_svc_error("media path is NULL");
1068 return MS_MEDIA_ERR_DB_INTERNAL;
1071 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1072 _strncpy_safe(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1074 media_svc_debug("media thumb path doesn't exist in DB");
1078 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1080 /* Update path, thumbnail path of this item */
1081 char *replaced_path = NULL;
1082 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1083 if (replaced_path == NULL) {
1084 media_svc_error("_media_svc_replace_path failed");
1085 SQLITE3_FINALIZE(sql_stmt);
1086 _media_svc_sql_rollback_trans(uid);
1087 return MS_MEDIA_ERR_INTERNAL;
1090 media_svc_debug("New media path : %s", replaced_path);
1091 ms_user_storage_type_t storage_type = -1;
1094 /* If old thumb path is default or not */
1095 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1096 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1097 _strncpy_safe(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1099 ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
1100 if (ret != MS_MEDIA_ERR_NONE) {
1101 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
1102 SAFE_FREE(replaced_path);
1103 SAFE_FREE(default_thumbnail_path);
1104 _media_svc_sql_rollback_trans(uid);
1108 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1109 if (ret != MS_MEDIA_ERR_NONE) {
1110 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1111 SAFE_FREE(replaced_path);
1112 SAFE_FREE(default_thumbnail_path);
1113 SQLITE3_FINALIZE(sql_stmt);
1114 _media_svc_sql_rollback_trans(uid);
1119 SAFE_FREE(default_thumbnail_path);
1121 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1124 char *update_item_sql = NULL;
1127 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1130 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1131 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);
1133 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);
1135 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);
1139 ret = media_db_request_update_db_batch(update_item_sql, uid);
1140 SQLITE3_SAFE_FREE(update_item_sql);
1141 SAFE_FREE(replaced_path);
1143 if (ret != SQLITE_OK) {
1144 media_svc_error("failed to update item");
1145 SQLITE3_FINALIZE(sql_stmt);
1146 _media_svc_sql_rollback_trans(uid);
1148 return MS_MEDIA_ERR_DB_INTERNAL;
1151 /* Rename thumbnail file of file system */
1152 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1153 if ((!no_thumb) && (default_thumbnail_path != NULL) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1154 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1155 if (ret != MS_MEDIA_ERR_NONE)
1156 media_svc_error("_media_svc_rename_file failed : %d", ret);
1159 SAFE_FREE(default_thumbnail_path);
1162 SQLITE3_FINALIZE(sql_stmt);
1164 ret = _media_svc_sql_end_trans(uid);
1165 if (ret != MS_MEDIA_ERR_NONE) {
1166 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1167 _media_svc_sql_rollback_trans(uid);
1171 media_svc_debug("Folder update is successful. Sending noti for this");
1172 /* Get notification info */
1173 media_svc_noti_item *noti_item = NULL;
1174 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1175 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1177 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1178 _media_svc_destroy_noti_item(noti_item);
1180 return MS_MEDIA_ERR_NONE;
1183 int media_svc_request_update_db(const char *db_query, uid_t uid)
1185 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1187 return _media_svc_sql_query(db_query, uid);
1190 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)
1192 int ret = MS_MEDIA_ERR_NONE;
1193 sqlite3 *db_handle = (sqlite3 *)handle;
1196 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1197 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1199 /* Get notification info */
1200 media_svc_noti_item *noti_item = NULL;
1201 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1202 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1204 if (folder_id != NULL) {
1205 uuid = strndup(folder_id, strlen(folder_id));
1207 if (noti_item->media_uuid != NULL) {
1208 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1210 _media_svc_destroy_noti_item(noti_item);
1211 media_svc_error("folder uuid is wrong");
1212 return MS_MEDIA_ERR_DB_INTERNAL;
1216 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1217 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1218 _media_svc_destroy_noti_item(noti_item);
1224 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1226 sqlite3 *db_handle = (sqlite3 *)handle;
1228 media_svc_debug_fenter();
1230 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1231 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1232 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1233 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1235 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1238 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1240 sqlite3 *db_handle = (sqlite3 *)handle;
1242 media_svc_debug_fenter();
1244 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1246 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1249 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1251 sqlite3 *db_handle = (sqlite3 *)handle;
1253 media_svc_debug_fenter();
1255 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1257 return _media_db_check_corrupt(db_handle);
1260 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)
1262 sqlite3 *db_handle = (sqlite3 *)handle;
1264 media_svc_debug_fenter();
1266 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1267 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1269 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1272 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1274 int ret = MS_MEDIA_ERR_NONE;
1275 sqlite3 *db_handle = (sqlite3 *)handle;
1276 time_t sto_time = 0;
1277 int cur_time = _media_svc_get_file_time(folder_path);
1278 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1280 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1282 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1283 if (ret == MS_MEDIA_ERR_NONE) {
1284 if (sto_time != cur_time)
1285 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1291 int media_svc_update_item_begin(int data_cnt)
1293 media_svc_debug("Transaction data count : [%d]", data_cnt);
1295 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1297 g_media_svc_update_item_data_cnt = data_cnt;
1298 g_media_svc_update_item_cur_data_cnt = 0;
1300 return MS_MEDIA_ERR_NONE;
1303 int media_svc_update_item_end(uid_t uid)
1305 int ret = MS_MEDIA_ERR_NONE;
1307 media_svc_debug_fenter();
1309 if (g_media_svc_update_item_cur_data_cnt > 0)
1310 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1312 g_media_svc_update_item_data_cnt = 1;
1313 g_media_svc_update_item_cur_data_cnt = 0;
1318 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1320 int ret = MS_MEDIA_ERR_NONE;
1321 sqlite3 *db_handle = (sqlite3 *)handle;
1323 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1325 media_svc_media_type_e media_type;
1326 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1327 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1329 media_svc_content_info_s content_info;
1330 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1333 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1334 if (ret != MS_MEDIA_ERR_NONE)
1337 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1338 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1340 return MS_MEDIA_ERR_NONE;
1342 if (ret != MS_MEDIA_ERR_NONE) {
1343 _media_svc_destroy_content_info(&content_info);
1347 if (g_media_svc_update_item_data_cnt == 1) {
1349 ret = _media_svc_update_meta_with_data(&content_info);
1350 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1352 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1354 ret = _media_svc_update_meta_with_data(&content_info);
1355 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1357 g_media_svc_update_item_cur_data_cnt++;
1359 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1361 ret = _media_svc_update_meta_with_data(&content_info);
1362 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1364 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1365 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1367 g_media_svc_update_item_cur_data_cnt = 0;
1370 media_svc_error("Error in media_svc_update_item_meta");
1371 _media_svc_destroy_content_info(&content_info);
1372 return MS_MEDIA_ERR_INTERNAL;
1375 _media_svc_destroy_content_info(&content_info);
1380 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)
1382 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1385 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1387 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1389 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1392 int media_svc_check_pinyin_support(bool *support)
1394 *support = _media_svc_check_pinyin_support();
1396 return MS_MEDIA_ERR_NONE;
1399 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1401 int ret = MS_MEDIA_ERR_NONE;
1402 sqlite3 * db_handle = (sqlite3 *)handle;
1404 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1406 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1407 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1409 ret = _media_svc_update_media_view(db_handle, uid);
1410 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1415 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1417 int ret = MS_MEDIA_ERR_NONE;
1418 sqlite3 * db_handle = (sqlite3 *)handle;
1420 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1421 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1423 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1428 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1430 int ret = MS_MEDIA_ERR_NONE;
1431 sqlite3 * db_handle = (sqlite3 *)handle;
1433 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1434 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1436 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1441 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1443 int ret = MS_MEDIA_ERR_NONE;
1444 sqlite3 * db_handle = (sqlite3 *)handle;
1446 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1447 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1449 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1454 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1456 int ret = MS_MEDIA_ERR_NONE;
1458 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1463 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1465 sqlite3 * db_handle = (sqlite3 *)handle;
1467 media_svc_debug_fenter();
1469 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1470 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1472 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1475 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1477 int ret = MS_MEDIA_ERR_NONE;
1479 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1480 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1482 if (content_info->added_time > 0)
1483 new_content_info->added_time = content_info->added_time;
1484 new_content_info->last_played_time = content_info->last_played_time;
1485 new_content_info->played_count = content_info->played_count;
1486 new_content_info->favourate = content_info->favourate;
1488 if (STRING_VALID(content_info->file_name)) {
1489 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1490 if (ret != MS_MEDIA_ERR_NONE)
1491 media_svc_error("strcpy file_name failed");
1494 if (STRING_VALID(content_info->media_meta.title)) {
1495 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1496 if (ret != MS_MEDIA_ERR_NONE)
1497 media_svc_error("strcpy title failed");
1500 if (STRING_VALID(content_info->media_meta.album)) {
1501 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1502 if (ret != MS_MEDIA_ERR_NONE)
1503 media_svc_error("strcpy album failed");
1506 if (STRING_VALID(content_info->media_meta.artist)) {
1507 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1508 if (ret != MS_MEDIA_ERR_NONE)
1509 media_svc_error("strcpy artist failed");
1512 if (STRING_VALID(content_info->media_meta.genre)) {
1513 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1514 if (ret != MS_MEDIA_ERR_NONE)
1515 media_svc_error("strcpy genre failed");
1518 if (STRING_VALID(content_info->media_meta.composer)) {
1519 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1520 if (ret != MS_MEDIA_ERR_NONE)
1521 media_svc_error("strcpy composer failed");
1524 if (STRING_VALID(content_info->media_meta.year)) {
1525 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1526 if (ret != MS_MEDIA_ERR_NONE)
1527 media_svc_error("strcpy year failed");
1530 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1531 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1532 if (ret != MS_MEDIA_ERR_NONE)
1533 media_svc_error("strcpy recorded_date failed");
1536 if (STRING_VALID(content_info->media_meta.copyright)) {
1537 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1538 if (ret != MS_MEDIA_ERR_NONE)
1539 media_svc_error("strcpy copyright failed");
1542 if (STRING_VALID(content_info->media_meta.track_num)) {
1543 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1544 if (ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy track_num failed");
1548 if (STRING_VALID(content_info->media_meta.description)) {
1549 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1550 if (ret != MS_MEDIA_ERR_NONE)
1551 media_svc_error("strcpy description failed");
1554 if (STRING_VALID(content_info->media_meta.weather)) {
1555 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1556 if (ret != MS_MEDIA_ERR_NONE)
1557 media_svc_error("strcpy weather failed");
1560 if (STRING_VALID(content_info->media_meta.category)) {
1561 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1562 if (ret != MS_MEDIA_ERR_NONE)
1563 media_svc_error("strcpy category failed");
1566 if (STRING_VALID(content_info->media_meta.keyword)) {
1567 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1568 if (ret != MS_MEDIA_ERR_NONE)
1569 media_svc_error("strcpy keyword failed");
1572 if (STRING_VALID(content_info->media_meta.location_tag)) {
1573 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1574 if (ret != MS_MEDIA_ERR_NONE)
1575 media_svc_error("strcpy location_tag failed");
1578 if (STRING_VALID(content_info->media_meta.content_name)) {
1579 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1580 if (ret != MS_MEDIA_ERR_NONE)
1581 media_svc_error("strcpy content_name failed");
1584 if (STRING_VALID(content_info->media_meta.age_rating)) {
1585 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1586 if (ret != MS_MEDIA_ERR_NONE)
1587 media_svc_error("strcpy age_rating failed");
1590 if (STRING_VALID(content_info->media_meta.author)) {
1591 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1592 if (ret != MS_MEDIA_ERR_NONE)
1593 media_svc_error("strcpy author failed");
1596 if (STRING_VALID(content_info->media_meta.provider)) {
1597 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1598 if (ret != MS_MEDIA_ERR_NONE)
1599 media_svc_error("strcpy provider failed");
1602 if (STRING_VALID(content_info->media_meta.datetaken)) {
1603 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1604 if (ret != MS_MEDIA_ERR_NONE)
1605 media_svc_error("strcpy datetaken failed");
1607 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1608 if (new_content_info->timeline == 0) {
1609 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1610 new_content_info->timeline = new_content_info->modified_time;
1612 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1616 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1617 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1618 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1619 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1620 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1622 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1623 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1624 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1625 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1626 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1627 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1629 new_content_info->media_meta.rating = content_info->media_meta.rating;
1634 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1636 int ret = MS_MEDIA_ERR_NONE;
1637 sqlite3 *db_handle = (sqlite3 *)handle;
1638 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1639 bool append_album = FALSE;
1641 /* Checking parameters if they are valid */
1642 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1643 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1644 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1646 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1648 media_svc_content_info_s _new_content_info;
1649 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1651 media_svc_media_type_e media_type;
1653 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1654 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1656 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1657 media_svc_debug("Do nothing[%d]", media_type);
1658 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1659 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1661 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1663 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1665 /* Extracting thumbnail */
1666 if (_new_content_info.thumbnail_path == NULL) {
1667 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1668 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1672 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1673 if (ret == MS_MEDIA_ERR_NONE)
1674 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1676 if (_new_content_info.media_meta.width <= 0)
1677 _new_content_info.media_meta.width = width;
1679 if (_new_content_info.media_meta.height <= 0)
1680 _new_content_info.media_meta.height = height;
1684 /* set othere data to the structure, which is passed as parameters */
1685 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1687 /* Set or Get folder id */
1688 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);
1689 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1691 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1692 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1694 /* register album table data */
1697 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1698 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1700 if (ret != MS_MEDIA_ERR_NONE) {
1701 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1702 media_svc_debug("album does not exist. So start to make album art");
1703 append_album = TRUE;
1705 media_svc_debug("other error");
1706 append_album = FALSE;
1709 _new_content_info.album_id = album_id;
1710 append_album = FALSE;
1712 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1713 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1714 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1717 media_svc_debug("album already exists. don't need to make album art");
1718 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1719 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1724 if (append_album == TRUE) {
1725 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1726 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1727 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);
1729 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1731 _new_content_info.album_id = album_id;
1734 /* Insert to db - calling _media_svc_insert_item_with_data */
1735 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1737 if (ret == MS_MEDIA_ERR_NONE)
1738 media_svc_debug("Insertion is successful.");
1740 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1741 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1743 _media_svc_destroy_content_info(&_new_content_info);
1745 /* handling returned value - important */
1749 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1751 _media_svc_destroy_content_info(content_info);
1754 int media_svc_generate_uuid(char **uuid)
1756 char *gen_uuid = NULL;
1757 gen_uuid = _media_info_generate_uuid();
1758 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1760 *uuid = strdup(gen_uuid);
1762 return MS_MEDIA_ERR_NONE;
1765 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1767 sqlite3 * db_handle = (sqlite3 *)handle;
1769 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1771 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1774 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1776 sqlite3 * db_handle = (sqlite3 *)handle;
1778 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1779 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1780 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1781 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1783 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1786 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1788 sqlite3 * db_handle = (sqlite3 *)handle;
1790 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1791 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1792 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1794 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1797 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)
1799 int ret = MS_MEDIA_ERR_NONE;
1800 sqlite3 *db_handle = (sqlite3 *)handle;
1802 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1803 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1804 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1805 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1807 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1808 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1810 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1811 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1812 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1814 ret = _media_svc_update_media_view(db_handle, uid);
1815 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1821 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1823 int ret = MS_MEDIA_ERR_NONE;
1824 sqlite3 *db_handle = (sqlite3 *)handle;
1825 media_svc_storage_type_e storage_type;
1827 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1828 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1830 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1831 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1833 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1834 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1836 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1837 if (ret != MS_MEDIA_ERR_NONE)
1838 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1840 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1841 ret = _media_svc_drop_media_table(storage_id, uid);
1842 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1844 ret = _media_svc_update_media_view(db_handle, uid);
1845 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1851 int media_svc_insert_folder_begin(int data_cnt)
1853 media_svc_debug("Transaction data count : [%d]", data_cnt);
1855 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1857 g_media_svc_insert_folder_data_cnt = data_cnt;
1858 g_media_svc_insert_folder_cur_data_cnt = 0;
1860 return MS_MEDIA_ERR_NONE;
1863 int media_svc_insert_folder_end(uid_t uid)
1865 int ret = MS_MEDIA_ERR_NONE;
1867 media_svc_debug_fenter();
1869 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1870 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1872 g_media_svc_insert_folder_data_cnt = 1;
1873 g_media_svc_insert_folder_cur_data_cnt = 0;
1878 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1880 int ret = MS_MEDIA_ERR_NONE;
1881 sqlite3 * db_handle = (sqlite3 *)handle;
1882 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1884 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1885 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1886 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1887 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1889 if (g_media_svc_insert_folder_data_cnt == 1) {
1891 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1893 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1895 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1896 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1898 g_media_svc_insert_folder_cur_data_cnt++;
1900 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1902 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1903 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1905 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1906 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1908 g_media_svc_insert_folder_cur_data_cnt = 0;
1911 media_svc_error("Error in media_svc_set_insert_folder");
1912 return MS_MEDIA_ERR_INTERNAL;
1918 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1920 int ret = MS_MEDIA_ERR_NONE;
1922 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1927 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1929 int ret = MS_MEDIA_ERR_NONE;
1930 sqlite3 * db_handle = (sqlite3 *)handle;
1932 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1934 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1939 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)
1941 int ret = MS_MEDIA_ERR_NONE;
1942 sqlite3 * db_handle = (sqlite3 *)handle;
1943 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1944 media_svc_media_type_e media_type;
1946 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1947 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1948 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1949 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1951 media_svc_content_info_s content_info;
1952 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1954 /*Set basic media info*/
1955 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1956 if (ret != MS_MEDIA_ERR_NONE) {
1957 media_svc_error("_media_svc_set_media_info fail %d", ret);
1961 /*Set or Get folder id*/
1962 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1963 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1965 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1966 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1968 if (g_media_svc_insert_item_data_cnt == 1) {
1970 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1971 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1973 if (g_insert_with_noti)
1974 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1975 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1977 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1978 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1980 if (g_insert_with_noti)
1981 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1983 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1984 g_media_svc_insert_item_cur_data_cnt++;
1986 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1988 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1989 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1991 if (g_insert_with_noti)
1992 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1994 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1995 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1997 media_svc_debug("_media_svc_list_query_do over");
1999 if (g_insert_with_noti) {
2000 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2001 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2002 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2004 /* Recreate noti list */
2005 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2006 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2009 g_media_svc_insert_item_cur_data_cnt = 0;
2012 media_svc_error("Error in media_svc_insert_item_pass1");
2013 _media_svc_destroy_content_info(&content_info);
2014 return MS_MEDIA_ERR_INTERNAL;
2017 _media_svc_destroy_content_info(&content_info);
2019 return MS_MEDIA_ERR_NONE;
2022 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)
2024 int ret = MS_MEDIA_ERR_NONE;
2025 sqlite3 * db_handle = (sqlite3 *)handle;
2026 sqlite3_stmt *sql_stmt = NULL;
2027 media_svc_media_type_e media_type;
2028 media_svc_content_info_s content_info;
2029 GArray *db_data_array = NULL;
2030 media_svc_item_info_s *db_data = NULL;
2033 media_svc_debug_fenter();
2035 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2036 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2038 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2039 media_svc_error("storage type is incorrect[%d]", storage_type);
2040 return MS_MEDIA_ERR_INVALID_PARAMETER;
2043 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2044 if (db_data_array == NULL) {
2045 media_svc_error("db_data_array is NULL. Out of memory");
2046 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2050 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2051 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2052 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2054 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2055 media_svc_error("folder no recursive extract");
2057 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2059 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);
2060 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2061 media_svc_error("folder recursive extract");
2063 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2065 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);
2068 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2070 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2073 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2074 if (ret != MS_MEDIA_ERR_NONE) {
2075 media_svc_error("error when get list. err = [%d]", ret);
2076 g_array_free(db_data_array, FALSE);
2080 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2082 db_data = malloc(sizeof(media_svc_item_info_s));
2083 if (db_data == NULL) {
2084 media_svc_error("Out of memory");
2088 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2089 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2091 g_array_append_val(db_data_array, db_data);
2094 SQLITE3_FINALIZE(sql_stmt);
2095 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2097 while (db_data_array->len != 0) {
2099 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2100 g_array_remove_index(db_data_array, 0);
2102 if ((db_data == NULL) || (db_data->path == NULL)) {
2103 media_svc_error("invalid db data");
2107 media_type = db_data->media_type;
2108 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2109 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2110 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2112 content_info.media_type = media_type;
2113 content_info.storage_type = storage_type;
2115 _media_svc_set_default_value(&content_info, FALSE);
2117 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2118 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2119 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2120 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2121 media_svc_debug("Do nothing[%d]", media_type);
2122 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2123 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2125 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2127 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2129 _media_svc_destroy_content_info(&content_info);
2130 SAFE_FREE(db_data->path);
2136 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2138 _media_svc_publish_noti_list(idx);
2139 _media_svc_destroy_noti_list(idx);
2141 /* Recreate noti list */
2142 ret = _media_svc_create_noti_list(idx);
2146 while (db_data_array->len != 0) {
2148 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2149 g_array_remove_index(db_data_array, 0);
2152 SAFE_FREE(db_data->path);
2158 g_array_free(db_data_array, FALSE);
2159 db_data_array = NULL;
2161 media_svc_debug_fleave();
2163 return MS_MEDIA_ERR_NONE;
2166 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2168 int ret = MS_MEDIA_ERR_NONE;
2169 sqlite3 * db_handle = (sqlite3 *)handle;
2171 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2172 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2174 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2179 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2181 int ret = MS_MEDIA_ERR_NONE;
2183 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2188 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2190 int ret = MS_MEDIA_ERR_NONE;
2191 sqlite3 * db_handle = (sqlite3 *)handle;
2192 time_t modified_time = 0;
2193 int system_time = 0;
2195 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2197 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2199 system_time = _media_svc_get_file_time(path);
2200 media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2202 if (system_time != modified_time && system_time != 0)
2210 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2212 sqlite3 * db_handle = (sqlite3 *)handle;
2214 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2215 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2217 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2220 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2222 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2223 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2225 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2228 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2230 int ret = MS_MEDIA_ERR_NONE;
2231 sqlite3 * db_handle = (sqlite3 *)handle;
2233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2235 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2240 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2242 int ret = MS_MEDIA_ERR_NONE;
2243 sqlite3 * db_handle = (sqlite3 *)handle;
2246 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2247 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2248 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2250 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2251 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2254 media_svc_debug("item is exist in database");
2255 return MS_MEDIA_ERR_NONE;
2257 media_svc_debug("item is not exist in database");
2258 return MS_MEDIA_ERR_DB_NO_RECORD;
2261 return MS_MEDIA_ERR_NONE;
2264 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2266 int ret = MS_MEDIA_ERR_NONE;
2267 sqlite3 * db_handle = (sqlite3 *)handle;
2270 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2271 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2272 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2275 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2276 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2280 media_svc_debug("item is exist in database");
2281 return MS_MEDIA_ERR_NONE;
2283 media_svc_debug("item is not exist in database");
2284 return MS_MEDIA_ERR_DB_NO_RECORD;
2287 return MS_MEDIA_ERR_NONE;
2290 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2292 int ret = MS_MEDIA_ERR_NONE;
2293 sqlite3 * db_handle = (sqlite3 *)handle;
2295 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2296 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2298 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2303 int media_svc_append_query(const char *query, uid_t uid)
2305 int ret = MS_MEDIA_ERR_NONE;
2307 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2309 ret = _media_svc_append_query_list(query, uid);
2314 int media_svc_send_query(uid_t uid)
2316 int ret = MS_MEDIA_ERR_NONE;
2318 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2323 int media_svc_get_media_type(const char *path, int *mediatype)
2325 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2327 return _media_svc_get_media_type(path, mediatype);