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 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
558 if (g_media_svc_move_item_data_cnt == 1) {
561 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
562 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);
564 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
566 SAFE_FREE(file_name);
567 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
569 media_svc_debug("Move is successful. Sending noti for this");
571 /* Get notification info */
572 media_svc_noti_item *noti_item = NULL;
573 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
574 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
576 /* Send notification for move */
577 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
578 _media_svc_destroy_noti_item(noti_item);
580 /*update folder modified_time*/
581 folder_path = g_path_get_dirname(dest_path);
582 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
583 SAFE_FREE(folder_path);
584 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
586 ret = _media_svc_update_folder_table(storage_id, uid);
587 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
589 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
592 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
593 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);
595 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
597 SAFE_FREE(file_name);
598 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
600 /*update folder modified_time*/
601 folder_path = g_path_get_dirname(dest_path);
602 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
603 SAFE_FREE(folder_path);
604 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
606 g_media_svc_move_item_cur_data_cnt++;
608 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
610 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
611 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);
613 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
615 SAFE_FREE(file_name);
616 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
618 /*update folder modified_time*/
619 folder_path = g_path_get_dirname(dest_path);
620 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
621 SAFE_FREE(folder_path);
622 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
625 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
626 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
628 g_media_svc_move_item_cur_data_cnt = 0;
631 media_svc_error("Error in media_svc_move_item");
632 SAFE_FREE(file_name);
633 return MS_MEDIA_ERR_INTERNAL;
636 /*rename thumbnail file*/
637 if (STRING_VALID(old_thumb_path)) {
638 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
639 if (ret != MS_MEDIA_ERR_NONE)
640 media_svc_error("_media_svc_rename_file failed : %d", ret);
643 return MS_MEDIA_ERR_NONE;
646 int media_svc_set_item_validity_begin(int data_cnt)
648 media_svc_debug("Transaction data count : [%d]", data_cnt);
650 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
652 g_media_svc_item_validity_data_cnt = data_cnt;
653 g_media_svc_item_validity_cur_data_cnt = 0;
655 return MS_MEDIA_ERR_NONE;
658 int media_svc_set_item_validity_end(uid_t uid)
660 int ret = MS_MEDIA_ERR_NONE;
662 media_svc_debug_fenter();
664 if (g_media_svc_item_validity_cur_data_cnt > 0)
665 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
667 g_media_svc_item_validity_data_cnt = 1;
668 g_media_svc_item_validity_cur_data_cnt = 0;
673 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
675 int ret = MS_MEDIA_ERR_NONE;
677 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
678 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
680 media_svc_debug("path=[%s], validity=[%d]", path, validity);
682 if (g_media_svc_item_validity_data_cnt == 1) {
684 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
686 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
688 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
689 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
691 g_media_svc_item_validity_cur_data_cnt++;
693 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
695 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
696 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
698 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
699 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
701 g_media_svc_item_validity_cur_data_cnt = 0;
705 media_svc_error("Error in media_svc_set_item_validity");
706 return MS_MEDIA_ERR_INTERNAL;
709 return MS_MEDIA_ERR_NONE;
712 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
714 int ret = MS_MEDIA_ERR_NONE;
715 sqlite3 *db_handle = (sqlite3 *)handle;
716 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
718 media_svc_debug_fenter();
720 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
721 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
722 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
725 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
726 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
729 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
730 /*Get thumbnail path to delete*/
731 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
732 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
733 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
735 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
736 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
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);
745 /*Get thumbnail path to delete*/
746 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
747 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
749 if (g_media_svc_insert_item_data_cnt == 1) {
751 /* Get notification info */
752 media_svc_noti_item *noti_item = NULL;
753 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
754 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
757 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
758 if (ret != MS_MEDIA_ERR_NONE) {
759 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
760 _media_svc_destroy_noti_item(noti_item);
765 /* Send notification */
766 media_svc_debug("Deletion is successful. Sending noti for this");
767 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
768 _media_svc_destroy_noti_item(noti_item);
770 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
771 if (ret != MS_MEDIA_ERR_NONE) {
772 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
779 if (STRING_VALID(thumb_path)) {
780 ret = _media_svc_remove_file(thumb_path);
781 if (ret != MS_MEDIA_ERR_NONE)
782 media_svc_error("fail to remove thumbnail file.");
785 return MS_MEDIA_ERR_NONE;
788 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
790 int ret = MS_MEDIA_ERR_NONE;
792 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
794 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
795 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
797 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
798 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
800 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
801 char *internal_thumb_path = NULL;
802 char *external_thumb_path = NULL;
804 ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
805 ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
807 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
808 media_svc_error("fail to get thumbnail path");
809 SAFE_FREE(internal_thumb_path);
810 SAFE_FREE(external_thumb_path);
811 return MS_MEDIA_ERR_INTERNAL;
813 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
815 /* remove thumbnails */
816 if (STRING_VALID(dirpath))
817 ret = _media_svc_remove_all_files_in_dir(dirpath);
818 SAFE_FREE(internal_thumb_path);
819 SAFE_FREE(external_thumb_path);
820 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
823 return MS_MEDIA_ERR_NONE;
826 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
828 sqlite3 *db_handle = (sqlite3 *)handle;
830 media_svc_debug_fenter();
832 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
833 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
834 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
836 /*Delete from DB and remove thumbnail files*/
837 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
840 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
842 sqlite3 *db_handle = (sqlite3 *)handle;
844 media_svc_debug_fenter();
846 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
847 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
849 /*Delete from DB and remove thumbnail files*/
850 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
853 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
855 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
856 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
858 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
861 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
863 sqlite3 *db_handle = (sqlite3 *)handle;
865 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
866 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
867 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
870 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
872 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
875 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
877 int ret = MS_MEDIA_ERR_NONE;
878 sqlite3 *db_handle = (sqlite3 *)handle;
879 media_svc_media_type_e media_type;
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(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
884 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
886 media_svc_content_info_s content_info;
887 memset(&content_info, 0, sizeof(media_svc_content_info_s));
890 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
891 if (ret != MS_MEDIA_ERR_NONE)
894 /* Initialize thumbnail information to remake thumbnail. */
895 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
896 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
897 if (ret != MS_MEDIA_ERR_NONE) {
898 _media_svc_destroy_content_info(&content_info);
902 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
903 ret = _media_svc_remove_file(thumb_path);
904 if (ret != MS_MEDIA_ERR_NONE)
905 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
908 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
909 if (ret != MS_MEDIA_ERR_NONE) {
910 _media_svc_destroy_content_info(&content_info);
914 /* Get notification info */
915 media_svc_noti_item *noti_item = NULL;
916 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
917 if (ret != MS_MEDIA_ERR_NONE) {
918 _media_svc_destroy_content_info(&content_info);
922 media_type = noti_item->media_type;
923 content_info.media_type = media_type;
925 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
926 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
927 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
928 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
929 media_svc_debug("Do nothing [%d]", media_type);
930 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
931 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
933 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
935 if (ret != MS_MEDIA_ERR_NONE) {
936 _media_svc_destroy_noti_item(noti_item);
937 _media_svc_destroy_content_info(&content_info);
941 /* Extracting thumbnail */
942 if (content_info.thumbnail_path == NULL) {
943 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
944 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
948 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
949 if (ret == MS_MEDIA_ERR_NONE)
950 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
952 if (content_info.media_meta.width <= 0)
953 content_info.media_meta.width = width;
955 if (content_info.media_meta.height <= 0)
956 content_info.media_meta.height = height;
960 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
962 if (ret == MS_MEDIA_ERR_NONE) {
963 media_svc_debug("Update is successful. Sending noti for this");
964 _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);
966 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
969 _media_svc_destroy_content_info(&content_info);
970 _media_svc_destroy_noti_item(noti_item);
975 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
977 sqlite3 *db_handle = (sqlite3 *)handle;
978 int ret = MS_MEDIA_ERR_NONE;
980 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
981 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
982 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
984 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
986 ret = _media_svc_sql_begin_trans(uid);
987 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
989 /* Update all folder record's modified date, which are changed above */
990 char *update_folder_modified_time_sql = NULL;
994 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
996 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
997 SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
999 if (ret != SQLITE_OK) {
1000 media_svc_error("failed to update folder modified time");
1001 _media_svc_sql_rollback_trans(uid);
1003 return MS_MEDIA_ERR_DB_INTERNAL;
1006 /* Update all items */
1007 char *select_all_sql = NULL;
1008 sqlite3_stmt *sql_stmt = NULL;
1009 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1011 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1013 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);
1015 media_svc_debug("[SQL query] : %s", select_all_sql);
1017 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1018 if (ret != MS_MEDIA_ERR_NONE) {
1019 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1020 _media_svc_sql_rollback_trans(uid);
1024 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1025 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1026 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1027 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1028 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1029 /*int media_type; */
1030 bool no_thumb = FALSE;
1032 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1033 _strncpy_safe(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1035 media_svc_error("media UUID is NULL");
1036 return MS_MEDIA_ERR_DB_INTERNAL;
1039 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1040 _strncpy_safe(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1042 media_svc_error("media path is NULL");
1043 return MS_MEDIA_ERR_DB_INTERNAL;
1046 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1047 _strncpy_safe(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1049 media_svc_debug("media thumb path doesn't exist in DB");
1053 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1055 /* Update path, thumbnail path of this item */
1056 char *replaced_path = NULL;
1057 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1058 if (replaced_path == NULL) {
1059 media_svc_error("_media_svc_replace_path failed");
1060 SQLITE3_FINALIZE(sql_stmt);
1061 _media_svc_sql_rollback_trans(uid);
1062 return MS_MEDIA_ERR_INTERNAL;
1065 media_svc_debug("New media path : %s", replaced_path);
1066 ms_user_storage_type_t storage_type = -1;
1069 ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
1070 if (ret != MS_MEDIA_ERR_NONE) {
1071 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
1072 SAFE_FREE(replaced_path);
1073 _media_svc_sql_rollback_trans(uid);
1077 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1078 if (ret != MS_MEDIA_ERR_NONE) {
1079 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1080 SAFE_FREE(replaced_path);
1081 SQLITE3_FINALIZE(sql_stmt);
1082 _media_svc_sql_rollback_trans(uid);
1086 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1089 char *update_item_sql = NULL;
1092 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1095 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1096 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);
1098 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);
1100 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);
1104 ret = media_db_request_update_db_batch(update_item_sql, uid);
1105 SQLITE3_SAFE_FREE(update_item_sql);
1106 SAFE_FREE(replaced_path);
1108 if (ret != SQLITE_OK) {
1109 media_svc_error("failed to update item");
1110 SQLITE3_FINALIZE(sql_stmt);
1111 _media_svc_sql_rollback_trans(uid);
1113 return MS_MEDIA_ERR_DB_INTERNAL;
1116 /* Rename thumbnail file of file system */
1118 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1119 if (ret != MS_MEDIA_ERR_NONE)
1120 media_svc_error("_media_svc_rename_file failed : %d", ret);
1125 SQLITE3_FINALIZE(sql_stmt);
1127 ret = _media_svc_sql_end_trans(uid);
1128 if (ret != MS_MEDIA_ERR_NONE) {
1129 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1130 _media_svc_sql_rollback_trans(uid);
1134 media_svc_debug("Folder update is successful. Sending noti for this");
1135 /* Get notification info */
1136 media_svc_noti_item *noti_item = NULL;
1137 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1138 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1140 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1141 _media_svc_destroy_noti_item(noti_item);
1143 return MS_MEDIA_ERR_NONE;
1146 int media_svc_request_update_db(const char *db_query, uid_t uid)
1148 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1150 return _media_svc_sql_query(db_query, uid);
1153 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)
1155 int ret = MS_MEDIA_ERR_NONE;
1156 sqlite3 *db_handle = (sqlite3 *)handle;
1159 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1160 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1162 /* Get notification info */
1163 media_svc_noti_item *noti_item = NULL;
1164 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1165 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1167 if (folder_id != NULL) {
1168 uuid = strndup(folder_id, strlen(folder_id));
1170 if (noti_item->media_uuid != NULL) {
1171 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1173 _media_svc_destroy_noti_item(noti_item);
1174 media_svc_error("folder uuid is wrong");
1175 return MS_MEDIA_ERR_DB_INTERNAL;
1179 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1180 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1181 _media_svc_destroy_noti_item(noti_item);
1187 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1189 sqlite3 *db_handle = (sqlite3 *)handle;
1191 media_svc_debug_fenter();
1193 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1194 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1195 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1196 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1198 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1201 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1203 sqlite3 *db_handle = (sqlite3 *)handle;
1205 media_svc_debug_fenter();
1207 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1209 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1212 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1214 sqlite3 *db_handle = (sqlite3 *)handle;
1216 media_svc_debug_fenter();
1218 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1220 return _media_db_check_corrupt(db_handle);
1223 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)
1225 sqlite3 *db_handle = (sqlite3 *)handle;
1227 media_svc_debug_fenter();
1229 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1230 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1232 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1235 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1237 int ret = MS_MEDIA_ERR_NONE;
1238 sqlite3 *db_handle = (sqlite3 *)handle;
1239 time_t sto_time = 0;
1240 int cur_time = _media_svc_get_file_time(folder_path);
1241 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1243 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1245 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1246 if (ret == MS_MEDIA_ERR_NONE) {
1247 if (sto_time != cur_time)
1248 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1254 int media_svc_update_item_begin(int data_cnt)
1256 media_svc_debug("Transaction data count : [%d]", data_cnt);
1258 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1260 g_media_svc_update_item_data_cnt = data_cnt;
1261 g_media_svc_update_item_cur_data_cnt = 0;
1263 return MS_MEDIA_ERR_NONE;
1266 int media_svc_update_item_end(uid_t uid)
1268 int ret = MS_MEDIA_ERR_NONE;
1270 media_svc_debug_fenter();
1272 if (g_media_svc_update_item_cur_data_cnt > 0)
1273 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1275 g_media_svc_update_item_data_cnt = 1;
1276 g_media_svc_update_item_cur_data_cnt = 0;
1281 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1283 int ret = MS_MEDIA_ERR_NONE;
1284 sqlite3 *db_handle = (sqlite3 *)handle;
1286 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1288 media_svc_media_type_e media_type;
1289 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1290 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1292 media_svc_content_info_s content_info;
1293 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1296 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1297 if (ret != MS_MEDIA_ERR_NONE)
1300 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1301 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1303 return MS_MEDIA_ERR_NONE;
1305 if (ret != MS_MEDIA_ERR_NONE) {
1306 _media_svc_destroy_content_info(&content_info);
1310 if (g_media_svc_update_item_data_cnt == 1) {
1312 ret = _media_svc_update_meta_with_data(&content_info);
1313 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1315 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1317 ret = _media_svc_update_meta_with_data(&content_info);
1318 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1320 g_media_svc_update_item_cur_data_cnt++;
1322 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1324 ret = _media_svc_update_meta_with_data(&content_info);
1325 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1327 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1328 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1330 g_media_svc_update_item_cur_data_cnt = 0;
1333 media_svc_error("Error in media_svc_update_item_meta");
1334 _media_svc_destroy_content_info(&content_info);
1335 return MS_MEDIA_ERR_INTERNAL;
1338 _media_svc_destroy_content_info(&content_info);
1343 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)
1345 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1348 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1350 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1352 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1355 int media_svc_check_pinyin_support(bool *support)
1357 *support = _media_svc_check_pinyin_support();
1359 return MS_MEDIA_ERR_NONE;
1362 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1364 int ret = MS_MEDIA_ERR_NONE;
1365 sqlite3 * db_handle = (sqlite3 *)handle;
1367 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1369 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1370 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1372 ret = _media_svc_update_media_view(db_handle, uid);
1373 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1378 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1380 int ret = MS_MEDIA_ERR_NONE;
1381 sqlite3 * db_handle = (sqlite3 *)handle;
1383 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1384 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1386 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1391 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1393 int ret = MS_MEDIA_ERR_NONE;
1394 sqlite3 * db_handle = (sqlite3 *)handle;
1396 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1397 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1399 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1404 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1406 int ret = MS_MEDIA_ERR_NONE;
1407 sqlite3 * db_handle = (sqlite3 *)handle;
1409 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1410 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1412 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1417 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1419 int ret = MS_MEDIA_ERR_NONE;
1421 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1426 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1428 sqlite3 * db_handle = (sqlite3 *)handle;
1430 media_svc_debug_fenter();
1432 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1433 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1435 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1438 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1440 int ret = MS_MEDIA_ERR_NONE;
1442 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1443 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1445 if (content_info->added_time > 0)
1446 new_content_info->added_time = content_info->added_time;
1447 new_content_info->last_played_time = content_info->last_played_time;
1448 new_content_info->played_count = content_info->played_count;
1449 new_content_info->favourate = content_info->favourate;
1451 if (STRING_VALID(content_info->file_name)) {
1452 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1453 if (ret != MS_MEDIA_ERR_NONE)
1454 media_svc_error("strcpy file_name failed");
1457 if (STRING_VALID(content_info->media_meta.title)) {
1458 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1459 if (ret != MS_MEDIA_ERR_NONE)
1460 media_svc_error("strcpy title failed");
1463 if (STRING_VALID(content_info->media_meta.album)) {
1464 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1465 if (ret != MS_MEDIA_ERR_NONE)
1466 media_svc_error("strcpy album failed");
1469 if (STRING_VALID(content_info->media_meta.artist)) {
1470 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1471 if (ret != MS_MEDIA_ERR_NONE)
1472 media_svc_error("strcpy artist failed");
1475 if (STRING_VALID(content_info->media_meta.genre)) {
1476 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1477 if (ret != MS_MEDIA_ERR_NONE)
1478 media_svc_error("strcpy genre failed");
1481 if (STRING_VALID(content_info->media_meta.composer)) {
1482 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1483 if (ret != MS_MEDIA_ERR_NONE)
1484 media_svc_error("strcpy composer failed");
1487 if (STRING_VALID(content_info->media_meta.year)) {
1488 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1489 if (ret != MS_MEDIA_ERR_NONE)
1490 media_svc_error("strcpy year failed");
1493 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1494 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1495 if (ret != MS_MEDIA_ERR_NONE)
1496 media_svc_error("strcpy recorded_date failed");
1499 if (STRING_VALID(content_info->media_meta.copyright)) {
1500 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1501 if (ret != MS_MEDIA_ERR_NONE)
1502 media_svc_error("strcpy copyright failed");
1505 if (STRING_VALID(content_info->media_meta.track_num)) {
1506 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1507 if (ret != MS_MEDIA_ERR_NONE)
1508 media_svc_error("strcpy track_num failed");
1511 if (STRING_VALID(content_info->media_meta.description)) {
1512 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1513 if (ret != MS_MEDIA_ERR_NONE)
1514 media_svc_error("strcpy description failed");
1517 if (STRING_VALID(content_info->media_meta.weather)) {
1518 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1519 if (ret != MS_MEDIA_ERR_NONE)
1520 media_svc_error("strcpy weather failed");
1523 if (STRING_VALID(content_info->media_meta.category)) {
1524 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1525 if (ret != MS_MEDIA_ERR_NONE)
1526 media_svc_error("strcpy category failed");
1529 if (STRING_VALID(content_info->media_meta.keyword)) {
1530 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1531 if (ret != MS_MEDIA_ERR_NONE)
1532 media_svc_error("strcpy keyword failed");
1535 if (STRING_VALID(content_info->media_meta.location_tag)) {
1536 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1537 if (ret != MS_MEDIA_ERR_NONE)
1538 media_svc_error("strcpy location_tag failed");
1541 if (STRING_VALID(content_info->media_meta.content_name)) {
1542 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1543 if (ret != MS_MEDIA_ERR_NONE)
1544 media_svc_error("strcpy content_name failed");
1547 if (STRING_VALID(content_info->media_meta.age_rating)) {
1548 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1549 if (ret != MS_MEDIA_ERR_NONE)
1550 media_svc_error("strcpy age_rating failed");
1553 if (STRING_VALID(content_info->media_meta.author)) {
1554 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1555 if (ret != MS_MEDIA_ERR_NONE)
1556 media_svc_error("strcpy author failed");
1559 if (STRING_VALID(content_info->media_meta.provider)) {
1560 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1561 if (ret != MS_MEDIA_ERR_NONE)
1562 media_svc_error("strcpy provider failed");
1565 if (STRING_VALID(content_info->media_meta.datetaken)) {
1566 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1567 if (ret != MS_MEDIA_ERR_NONE)
1568 media_svc_error("strcpy datetaken failed");
1570 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1571 if (new_content_info->timeline == 0) {
1572 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1573 new_content_info->timeline = new_content_info->modified_time;
1575 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1579 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1580 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1581 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1582 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1583 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1585 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1586 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1587 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1588 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1589 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1590 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1592 new_content_info->media_meta.rating = content_info->media_meta.rating;
1597 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1599 int ret = MS_MEDIA_ERR_NONE;
1600 sqlite3 *db_handle = (sqlite3 *)handle;
1601 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1602 bool append_album = FALSE;
1604 /* Checking parameters if they are valid */
1605 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1606 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1607 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1609 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1611 media_svc_content_info_s _new_content_info;
1612 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1614 media_svc_media_type_e media_type;
1616 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1617 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1619 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1620 media_svc_debug("Do nothing[%d]", media_type);
1621 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1622 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1624 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1626 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1628 /* Extracting thumbnail */
1629 if (_new_content_info.thumbnail_path == NULL) {
1630 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1631 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1635 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1636 if (ret == MS_MEDIA_ERR_NONE)
1637 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1639 if (_new_content_info.media_meta.width <= 0)
1640 _new_content_info.media_meta.width = width;
1642 if (_new_content_info.media_meta.height <= 0)
1643 _new_content_info.media_meta.height = height;
1647 /* set othere data to the structure, which is passed as parameters */
1648 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1650 /* Set or Get folder id */
1651 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);
1652 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1654 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1655 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1657 /* register album table data */
1660 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1661 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1663 if (ret != MS_MEDIA_ERR_NONE) {
1664 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1665 media_svc_debug("album does not exist. So start to make album art");
1666 append_album = TRUE;
1668 media_svc_debug("other error");
1669 append_album = FALSE;
1672 _new_content_info.album_id = album_id;
1673 append_album = FALSE;
1675 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1676 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1677 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1680 media_svc_debug("album already exists. don't need to make album art");
1681 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1682 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1687 if (append_album == TRUE) {
1688 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1689 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1690 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);
1692 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1694 _new_content_info.album_id = album_id;
1697 /* Insert to db - calling _media_svc_insert_item_with_data */
1698 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1700 if (ret == MS_MEDIA_ERR_NONE)
1701 media_svc_debug("Insertion is successful.");
1703 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1704 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1706 _media_svc_destroy_content_info(&_new_content_info);
1708 /* handling returned value - important */
1712 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1714 _media_svc_destroy_content_info(content_info);
1717 int media_svc_generate_uuid(char **uuid)
1719 char *gen_uuid = NULL;
1720 gen_uuid = _media_info_generate_uuid();
1721 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1723 *uuid = strdup(gen_uuid);
1725 return MS_MEDIA_ERR_NONE;
1728 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1730 sqlite3 * db_handle = (sqlite3 *)handle;
1732 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1734 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1737 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1739 sqlite3 * db_handle = (sqlite3 *)handle;
1741 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1742 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1743 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1744 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1746 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1749 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1751 sqlite3 * db_handle = (sqlite3 *)handle;
1753 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1754 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1755 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1757 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1760 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)
1762 int ret = MS_MEDIA_ERR_NONE;
1763 sqlite3 *db_handle = (sqlite3 *)handle;
1765 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1766 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1767 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1768 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1770 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1771 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1773 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1774 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1775 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1777 ret = _media_svc_update_media_view(db_handle, uid);
1778 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1784 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1786 int ret = MS_MEDIA_ERR_NONE;
1787 sqlite3 *db_handle = (sqlite3 *)handle;
1788 media_svc_storage_type_e storage_type;
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");
1793 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1794 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1796 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1797 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1799 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1800 if (ret != MS_MEDIA_ERR_NONE)
1801 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1803 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1804 ret = _media_svc_drop_media_table(storage_id, uid);
1805 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1807 ret = _media_svc_update_media_view(db_handle, uid);
1808 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1814 int media_svc_insert_folder_begin(int data_cnt)
1816 media_svc_debug("Transaction data count : [%d]", data_cnt);
1818 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1820 g_media_svc_insert_folder_data_cnt = data_cnt;
1821 g_media_svc_insert_folder_cur_data_cnt = 0;
1823 return MS_MEDIA_ERR_NONE;
1826 int media_svc_insert_folder_end(uid_t uid)
1828 int ret = MS_MEDIA_ERR_NONE;
1830 media_svc_debug_fenter();
1832 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1833 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1835 g_media_svc_insert_folder_data_cnt = 1;
1836 g_media_svc_insert_folder_cur_data_cnt = 0;
1841 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1843 int ret = MS_MEDIA_ERR_NONE;
1844 sqlite3 * db_handle = (sqlite3 *)handle;
1845 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1847 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1848 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1849 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1850 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1852 if (g_media_svc_insert_folder_data_cnt == 1) {
1854 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1856 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1858 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1859 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1861 g_media_svc_insert_folder_cur_data_cnt++;
1863 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1865 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1866 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1868 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1869 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1871 g_media_svc_insert_folder_cur_data_cnt = 0;
1874 media_svc_error("Error in media_svc_set_insert_folder");
1875 return MS_MEDIA_ERR_INTERNAL;
1881 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1883 int ret = MS_MEDIA_ERR_NONE;
1885 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1890 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1892 int ret = MS_MEDIA_ERR_NONE;
1893 sqlite3 * db_handle = (sqlite3 *)handle;
1895 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1897 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1902 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)
1904 int ret = MS_MEDIA_ERR_NONE;
1905 sqlite3 * db_handle = (sqlite3 *)handle;
1906 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1907 media_svc_media_type_e media_type;
1909 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1910 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1911 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1912 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1914 media_svc_content_info_s content_info;
1915 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1917 /*Set basic media info*/
1918 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1919 if (ret != MS_MEDIA_ERR_NONE) {
1920 media_svc_error("_media_svc_set_media_info fail %d", ret);
1924 /*Set or Get folder id*/
1925 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1926 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1928 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1929 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1931 if (g_media_svc_insert_item_data_cnt == 1) {
1933 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1934 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1936 if (g_insert_with_noti)
1937 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1938 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1940 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1941 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1943 if (g_insert_with_noti)
1944 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1946 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1947 g_media_svc_insert_item_cur_data_cnt++;
1949 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1951 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1952 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1954 if (g_insert_with_noti)
1955 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1957 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1958 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1960 media_svc_debug("_media_svc_list_query_do over");
1962 if (g_insert_with_noti) {
1963 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1964 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1965 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1967 /* Recreate noti list */
1968 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1969 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1972 g_media_svc_insert_item_cur_data_cnt = 0;
1975 media_svc_error("Error in media_svc_insert_item_pass1");
1976 _media_svc_destroy_content_info(&content_info);
1977 return MS_MEDIA_ERR_INTERNAL;
1980 _media_svc_destroy_content_info(&content_info);
1982 return MS_MEDIA_ERR_NONE;
1985 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)
1987 int ret = MS_MEDIA_ERR_NONE;
1988 sqlite3 * db_handle = (sqlite3 *)handle;
1989 sqlite3_stmt *sql_stmt = NULL;
1990 media_svc_media_type_e media_type;
1991 media_svc_content_info_s content_info;
1992 GArray *db_data_array = NULL;
1993 media_svc_item_info_s *db_data = NULL;
1996 media_svc_debug_fenter();
1998 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1999 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2001 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2002 media_svc_error("storage type is incorrect[%d]", storage_type);
2003 return MS_MEDIA_ERR_INVALID_PARAMETER;
2006 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2007 if (db_data_array == NULL) {
2008 media_svc_error("db_data_array is NULL. Out of memory");
2009 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2013 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2014 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2015 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2017 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2018 media_svc_error("folder no recursive extract");
2020 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2022 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);
2023 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2024 media_svc_error("folder recursive extract");
2026 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2028 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);
2031 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2033 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2036 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2037 if (ret != MS_MEDIA_ERR_NONE) {
2038 media_svc_error("error when get list. err = [%d]", ret);
2039 g_array_free(db_data_array, FALSE);
2043 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2045 db_data = malloc(sizeof(media_svc_item_info_s));
2046 if (db_data == NULL) {
2047 media_svc_error("Out of memory");
2051 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2052 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2054 g_array_append_val(db_data_array, db_data);
2057 SQLITE3_FINALIZE(sql_stmt);
2058 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2060 while (db_data_array->len != 0) {
2062 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2063 g_array_remove_index(db_data_array, 0);
2065 if ((db_data == NULL) || (db_data->path == NULL)) {
2066 media_svc_error("invalid db data");
2070 media_type = db_data->media_type;
2071 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2072 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2073 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2075 content_info.media_type = media_type;
2076 content_info.storage_type = storage_type;
2078 _media_svc_set_default_value(&content_info, FALSE);
2080 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2081 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2082 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2083 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2084 media_svc_debug("Do nothing[%d]", media_type);
2085 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2086 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2088 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2090 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2092 _media_svc_destroy_content_info(&content_info);
2093 SAFE_FREE(db_data->path);
2099 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2101 _media_svc_publish_noti_list(idx);
2102 _media_svc_destroy_noti_list(idx);
2104 /* Recreate noti list */
2105 ret = _media_svc_create_noti_list(idx);
2109 while (db_data_array->len != 0) {
2111 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2112 g_array_remove_index(db_data_array, 0);
2115 SAFE_FREE(db_data->path);
2121 g_array_free(db_data_array, FALSE);
2122 db_data_array = NULL;
2124 media_svc_debug_fleave();
2126 return MS_MEDIA_ERR_NONE;
2129 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2131 int ret = MS_MEDIA_ERR_NONE;
2132 sqlite3 * db_handle = (sqlite3 *)handle;
2134 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2135 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2137 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2142 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2144 int ret = MS_MEDIA_ERR_NONE;
2146 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2151 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2153 int ret = MS_MEDIA_ERR_NONE;
2154 sqlite3 * db_handle = (sqlite3 *)handle;
2155 time_t modified_time = 0;
2156 int system_time = 0;
2158 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2160 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2162 system_time = _media_svc_get_file_time(path);
2163 media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2165 if (system_time != modified_time && system_time != 0)
2173 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2175 sqlite3 * db_handle = (sqlite3 *)handle;
2177 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2178 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2180 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2183 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2185 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2186 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2188 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2191 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2193 int ret = MS_MEDIA_ERR_NONE;
2194 sqlite3 * db_handle = (sqlite3 *)handle;
2196 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2198 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2203 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2205 int ret = MS_MEDIA_ERR_NONE;
2206 sqlite3 * db_handle = (sqlite3 *)handle;
2209 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2210 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2211 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2213 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2214 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2217 media_svc_debug("item is exist in database");
2218 return MS_MEDIA_ERR_NONE;
2220 media_svc_debug("item is not exist in database");
2221 return MS_MEDIA_ERR_DB_NO_RECORD;
2224 return MS_MEDIA_ERR_NONE;
2227 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2229 int ret = MS_MEDIA_ERR_NONE;
2230 sqlite3 * db_handle = (sqlite3 *)handle;
2233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2234 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2235 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2238 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2239 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2243 media_svc_debug("item is exist in database");
2244 return MS_MEDIA_ERR_NONE;
2246 media_svc_debug("item is not exist in database");
2247 return MS_MEDIA_ERR_DB_NO_RECORD;
2250 return MS_MEDIA_ERR_NONE;
2253 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2255 int ret = MS_MEDIA_ERR_NONE;
2256 sqlite3 * db_handle = (sqlite3 *)handle;
2258 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2259 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2261 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2266 int media_svc_append_query(const char *query, uid_t uid)
2268 int ret = MS_MEDIA_ERR_NONE;
2270 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2272 ret = _media_svc_append_query_list(query, uid);
2277 int media_svc_send_query(uid_t uid)
2279 int ret = MS_MEDIA_ERR_NONE;
2281 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2286 int media_svc_get_media_type(const char *path, int *mediatype)
2288 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2290 return _media_svc_get_media_type(path, mediatype);