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 && (storage_type != MEDIA_SVC_STORAGE_CLOUD)) {
68 media_svc_error("storage type is incorrect[%d]", storage_type);
72 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
73 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
74 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
75 media_svc_error("storage type is incorrect[%d]", storage_type);
83 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
85 int ret = MS_MEDIA_ERR_NONE;
86 MediaDBHandle *db_handle = NULL;
88 media_svc_debug_fenter();
90 ret = media_db_connect(&db_handle, uid, need_write);
91 if (ret != MS_MEDIA_ERR_NONE)
95 return MS_MEDIA_ERR_NONE;
98 int media_svc_disconnect(MediaSvcHandle *handle)
100 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
102 media_svc_debug_fenter();
104 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
106 int ret = MS_MEDIA_ERR_NONE;
108 ret = media_db_disconnect(db_handle);
112 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
114 sqlite3 *db_handle = (sqlite3 *)handle;
116 return _media_svc_get_user_version(db_handle, user_version);
119 int media_svc_create_table(uid_t uid)
121 int ret = MS_MEDIA_ERR_NONE;
124 media_svc_debug_fenter();
126 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
127 if (ret != MS_MEDIA_ERR_NONE) {
128 media_svc_error("_media_svc_init_table_query fail.");
132 /*create media table*/
133 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
134 if (ret != MS_MEDIA_ERR_NONE) {
135 media_svc_error("_media_svc_make_table_query fail.");
139 /*create folder table*/
140 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
141 if (ret != MS_MEDIA_ERR_NONE) {
142 media_svc_error("_media_svc_make_table_query fail.");
146 /*create playlist_map table*/
147 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
148 if (ret != MS_MEDIA_ERR_NONE) {
149 media_svc_error("_media_svc_make_table_query fail.");
153 /*create playlist table*/
154 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
155 if (ret != MS_MEDIA_ERR_NONE) {
156 media_svc_error("_media_svc_make_table_query fail.");
160 /* create album table*/
161 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
162 if (ret != MS_MEDIA_ERR_NONE) {
163 media_svc_error("_media_svc_make_table_query fail.");
167 /*create tag_map table*/
168 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
169 if (ret != MS_MEDIA_ERR_NONE) {
170 media_svc_error("_media_svc_make_table_query fail.");
175 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
176 if (ret != MS_MEDIA_ERR_NONE) {
177 media_svc_error("_media_svc_make_table_query fail.");
181 /*create bookmark table*/
182 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
183 if (ret != MS_MEDIA_ERR_NONE) {
184 media_svc_error("_media_svc_make_table_query fail.");
188 /*create storage table from tizen 2.4 */
189 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
190 if (ret != MS_MEDIA_ERR_NONE) {
191 media_svc_error("_media_svc_make_table_query fail.");
195 /*create uhd table from tizen 3.0 */
196 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_UHD, MEDIA_SVC_DB_LIST_UHD, uid);
197 if (ret != MS_MEDIA_ERR_NONE) {
198 media_svc_error("_media_svc_make_table_query fail.");
202 /*create pvr table from tizen 3.0 */
203 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PVR, MEDIA_SVC_DB_LIST_PVR, uid);
204 if (ret != MS_MEDIA_ERR_NONE) {
205 media_svc_error("_media_svc_make_table_query fail.");
209 /*init storage table*/
210 ret = _media_svc_init_storage(db_handle, uid);
211 if (ret != MS_MEDIA_ERR_NONE) {
212 media_svc_error("_media_svc_make_table_query fail.");
217 /*create face table. from tizen 3.0*/
218 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
219 if (ret != MS_MEDIA_ERR_NONE) {
220 media_svc_error("_media_svc_make_table_query fail.");
224 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
225 if (ret != MS_MEDIA_ERR_NONE) {
226 media_svc_error("_media_svc_make_table_query fail.");
230 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
231 ret = _media_svc_sql_query(sql, uid);
232 if (ret != MS_MEDIA_ERR_NONE) {
233 media_svc_error("user_version update fail.");
237 _media_svc_destroy_table_query();
239 media_svc_debug_fleave();
241 return MS_MEDIA_ERR_NONE;
243 _media_svc_destroy_table_query();
245 media_svc_debug_fleave();
250 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
252 int ret = MS_MEDIA_ERR_NONE;
253 media_svc_storage_type_e type;
255 ret = _media_svc_get_storage_type_by_path(path, &type, uid);
256 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
258 *storage_type = type;
263 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
265 int ret = MS_MEDIA_ERR_NONE;
266 sqlite3 *db_handle = (sqlite3 *)handle;
268 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
269 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
271 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
276 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
278 int ret = MS_MEDIA_ERR_NONE;
279 sqlite3 *db_handle = (sqlite3 *)handle;
282 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
283 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
284 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
286 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
287 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
290 media_svc_debug("item is exist in database");
291 return MS_MEDIA_ERR_NONE;
293 media_svc_debug("item is not exist in database");
294 return MS_MEDIA_ERR_DB_NO_RECORD;
297 return MS_MEDIA_ERR_NONE;
300 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
302 media_svc_debug("Transaction data count : [%d]", data_cnt);
304 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
306 g_media_svc_insert_item_data_cnt = data_cnt;
307 g_media_svc_insert_item_cur_data_cnt = 0;
309 /* Prepare for making noti item list */
311 media_svc_debug("making noti list from pid[%d]", from_pid);
312 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
313 return MS_MEDIA_ERR_OUT_OF_MEMORY;
315 _media_svc_set_noti_from_pid(from_pid);
316 g_insert_with_noti = TRUE;
319 return MS_MEDIA_ERR_NONE;
322 int media_svc_insert_item_end(uid_t uid)
324 int ret = MS_MEDIA_ERR_NONE;
326 media_svc_debug_fenter();
328 if (g_media_svc_insert_item_cur_data_cnt > 0) {
330 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
331 if (g_insert_with_noti) {
332 media_svc_debug("sending noti list");
333 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
334 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
335 g_insert_with_noti = FALSE;
336 _media_svc_set_noti_from_pid(-1);
340 g_media_svc_insert_item_data_cnt = 1;
341 g_media_svc_insert_item_cur_data_cnt = 0;
346 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)
348 int ret = MS_MEDIA_ERR_NONE;
349 sqlite3 *db_handle = (sqlite3 *)handle;
350 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
351 media_svc_media_type_e media_type;
353 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
354 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
355 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
356 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
358 media_svc_content_info_s content_info;
359 memset(&content_info, 0, sizeof(media_svc_content_info_s));
362 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
363 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
364 if (ret != MS_MEDIA_ERR_NONE)
367 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
368 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
369 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
370 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
371 media_svc_debug("Do nothing[%d]", media_type);
372 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
373 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
375 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
376 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
378 /*Set or Get folder id*/
379 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
380 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
382 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
383 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
385 if (g_media_svc_insert_item_data_cnt == 1) {
387 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
388 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
390 if (g_insert_with_noti)
391 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
393 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
395 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
396 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
398 if (g_insert_with_noti)
399 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
401 g_media_svc_insert_item_cur_data_cnt++;
403 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
405 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
406 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
408 if (g_insert_with_noti)
409 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
411 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
412 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
414 if (g_insert_with_noti) {
415 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
416 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
418 /* Recreate noti list */
419 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
420 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
423 g_media_svc_insert_item_cur_data_cnt = 0;
426 media_svc_error("Error in media_svc_insert_item_bulk");
427 _media_svc_destroy_content_info(&content_info);
428 return MS_MEDIA_ERR_INTERNAL;
431 _media_svc_destroy_content_info(&content_info);
433 return MS_MEDIA_ERR_NONE;
436 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)
438 int ret = MS_MEDIA_ERR_NONE;
439 sqlite3 *db_handle = (sqlite3 *)handle;
440 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
441 media_svc_media_type_e media_type;
443 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
444 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
445 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
446 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
448 media_svc_content_info_s content_info;
449 memset(&content_info, 0, sizeof(media_svc_content_info_s));
452 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
453 if (ret != MS_MEDIA_ERR_NONE)
456 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
457 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
458 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
459 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
461 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
462 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
464 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
467 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
469 /*Set or Get folder id*/
470 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
471 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
473 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
474 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
476 /* Extracting thumbnail */
477 if (content_info.thumbnail_path == NULL) {
478 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
479 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
483 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
484 if (ret == MS_MEDIA_ERR_NONE)
485 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
487 if (content_info.media_meta.width <= 0)
488 content_info.media_meta.width = width;
490 if (content_info.media_meta.height <= 0)
491 content_info.media_meta.height = height;
495 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
497 if (ret == MS_MEDIA_ERR_NONE) {
498 media_svc_debug("Insertion is successful. Sending noti for this");
499 _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);
500 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
501 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
504 _media_svc_destroy_content_info(&content_info);
508 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
509 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
511 int ret = MS_MEDIA_ERR_NONE;
512 sqlite3 *db_handle = (sqlite3 *)handle;
513 char *file_name = NULL;
514 char *folder_path = NULL;
515 int modified_time = 0;
516 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
517 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
518 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
521 media_svc_debug_fenter();
523 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
524 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
525 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
526 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
527 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
529 /*check and update folder*/
530 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
531 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
534 file_name = g_path_get_basename(dest_path);
536 /*get modified_time*/
537 modified_time = _media_svc_get_file_time(dest_path);
539 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
540 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
541 if (ret != MS_MEDIA_ERR_NONE) {
542 media_svc_error("_media_svc_get_media_type_by_path failed");
543 SAFE_FREE(file_name);
547 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
548 /*get old thumbnail_path*/
549 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
550 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
551 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
552 SAFE_FREE(file_name);
556 /* If old thumb path is default or not */
557 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
558 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
559 strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
561 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
563 SAFE_FREE(default_thumbnail_path);
566 if (g_media_svc_move_item_data_cnt == 1) {
569 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
570 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);
572 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
574 SAFE_FREE(file_name);
575 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
577 media_svc_debug("Move is successful. Sending noti for this");
579 /* Get notification info */
580 media_svc_noti_item *noti_item = NULL;
581 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
582 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
584 /* Send notification for move */
585 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
586 _media_svc_destroy_noti_item(noti_item);
588 /*update folder modified_time*/
589 folder_path = g_path_get_dirname(dest_path);
590 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
591 SAFE_FREE(folder_path);
592 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
594 ret = _media_svc_update_folder_table(storage_id, uid);
595 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
597 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
600 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
601 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);
603 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
605 SAFE_FREE(file_name);
606 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
608 /*update folder modified_time*/
609 folder_path = g_path_get_dirname(dest_path);
610 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
611 SAFE_FREE(folder_path);
612 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
614 g_media_svc_move_item_cur_data_cnt++;
616 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
618 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
619 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);
621 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
623 SAFE_FREE(file_name);
624 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
626 /*update folder modified_time*/
627 folder_path = g_path_get_dirname(dest_path);
628 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
629 SAFE_FREE(folder_path);
630 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
633 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
634 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
636 g_media_svc_move_item_cur_data_cnt = 0;
639 media_svc_error("Error in media_svc_move_item");
640 SAFE_FREE(file_name);
641 return MS_MEDIA_ERR_INTERNAL;
644 /*rename thumbnail file*/
645 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
646 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)) {
647 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
648 if (ret != MS_MEDIA_ERR_NONE)
649 media_svc_error("_media_svc_rename_file failed : %d", ret);
653 return MS_MEDIA_ERR_NONE;
656 int media_svc_set_item_validity_begin(int data_cnt)
658 media_svc_debug("Transaction data count : [%d]", data_cnt);
660 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
662 g_media_svc_item_validity_data_cnt = data_cnt;
663 g_media_svc_item_validity_cur_data_cnt = 0;
665 return MS_MEDIA_ERR_NONE;
668 int media_svc_set_item_validity_end(uid_t uid)
670 int ret = MS_MEDIA_ERR_NONE;
672 media_svc_debug_fenter();
674 if (g_media_svc_item_validity_cur_data_cnt > 0)
675 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
677 g_media_svc_item_validity_data_cnt = 1;
678 g_media_svc_item_validity_cur_data_cnt = 0;
683 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
685 int ret = MS_MEDIA_ERR_NONE;
687 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
688 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
690 media_svc_debug("path=[%s], validity=[%d]", path, validity);
692 if (g_media_svc_item_validity_data_cnt == 1) {
694 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
696 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
698 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
699 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
701 g_media_svc_item_validity_cur_data_cnt++;
703 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
705 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
706 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
708 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
709 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
711 g_media_svc_item_validity_cur_data_cnt = 0;
715 media_svc_error("Error in media_svc_set_item_validity");
716 return MS_MEDIA_ERR_INTERNAL;
719 return MS_MEDIA_ERR_NONE;
722 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
724 int ret = MS_MEDIA_ERR_NONE;
725 sqlite3 *db_handle = (sqlite3 *)handle;
726 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
728 media_svc_debug_fenter();
730 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
731 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
732 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
735 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
736 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
739 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
740 /*Get thumbnail path to delete*/
741 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
742 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
743 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
745 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
746 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
749 /*Get thumbnail path to delete*/
750 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
751 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
755 /*Get thumbnail path to delete*/
756 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
757 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
759 if (g_media_svc_insert_item_data_cnt == 1) {
761 /* Get notification info */
762 media_svc_noti_item *noti_item = NULL;
763 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
764 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
767 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
768 if (ret != MS_MEDIA_ERR_NONE) {
769 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
770 _media_svc_destroy_noti_item(noti_item);
775 /* Send notification */
776 media_svc_debug("Deletion is successful. Sending noti for this");
777 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
778 _media_svc_destroy_noti_item(noti_item);
780 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
781 if (ret != MS_MEDIA_ERR_NONE) {
782 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
789 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
790 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
793 // Get count of media, which contains same thumbnail for music
794 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
795 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
796 if (ret != MS_MEDIA_ERR_NONE) {
797 media_svc_error("Failed to get thumbnail count : %d", ret);
801 if (thumb_count == 1) {
803 ret = _media_svc_remove_file(thumb_path);
804 if (ret != MS_MEDIA_ERR_NONE)
805 media_svc_error("fail to remove thumbnail file.");
809 SAFE_FREE(default_thumbnail_path);
811 return MS_MEDIA_ERR_NONE;
814 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
816 int ret = MS_MEDIA_ERR_NONE;
818 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
820 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
821 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
823 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
824 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
826 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
827 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
828 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
830 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
832 /* remove thumbnails */
833 if (STRING_VALID(dirpath))
834 ret = _media_svc_remove_all_files_in_dir(dirpath);
835 SAFE_FREE(internal_thumb_path);
836 SAFE_FREE(external_thumb_path);
837 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
840 return MS_MEDIA_ERR_NONE;
843 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
845 sqlite3 *db_handle = (sqlite3 *)handle;
847 media_svc_debug_fenter();
849 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
850 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
851 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
853 /*Delete from DB and remove thumbnail files*/
854 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
857 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
859 sqlite3 *db_handle = (sqlite3 *)handle;
861 media_svc_debug_fenter();
863 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
864 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
866 /*Delete from DB and remove thumbnail files*/
867 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
870 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
872 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
873 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
875 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
878 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
880 sqlite3 *db_handle = (sqlite3 *)handle;
882 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
883 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
884 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
887 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
889 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
892 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
894 int ret = MS_MEDIA_ERR_NONE;
895 sqlite3 *db_handle = (sqlite3 *)handle;
896 media_svc_media_type_e media_type;
898 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
899 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
900 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
901 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
903 media_svc_content_info_s content_info;
904 memset(&content_info, 0, sizeof(media_svc_content_info_s));
907 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
908 if (ret != MS_MEDIA_ERR_NONE)
911 /* Initialize thumbnail information to remake thumbnail. */
912 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
913 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
914 if (ret != MS_MEDIA_ERR_NONE) {
915 _media_svc_destroy_content_info(&content_info);
919 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)) {
920 ret = _media_svc_remove_file(thumb_path);
921 if (ret != MS_MEDIA_ERR_NONE)
922 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
925 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
926 if (ret != MS_MEDIA_ERR_NONE) {
927 _media_svc_destroy_content_info(&content_info);
931 /* Get notification info */
932 media_svc_noti_item *noti_item = NULL;
933 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
934 if (ret != MS_MEDIA_ERR_NONE) {
935 _media_svc_destroy_content_info(&content_info);
939 media_type = noti_item->media_type;
940 content_info.media_type = media_type;
942 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
943 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
944 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
945 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
946 media_svc_debug("Do nothing [%d]", media_type);
947 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
948 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
950 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
952 if (ret != MS_MEDIA_ERR_NONE) {
953 _media_svc_destroy_noti_item(noti_item);
954 _media_svc_destroy_content_info(&content_info);
958 /* Extracting thumbnail */
959 if (content_info.thumbnail_path == NULL) {
960 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
961 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
965 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
966 if (ret == MS_MEDIA_ERR_NONE)
967 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
969 if (content_info.media_meta.width <= 0)
970 content_info.media_meta.width = width;
972 if (content_info.media_meta.height <= 0)
973 content_info.media_meta.height = height;
977 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
979 if (ret == MS_MEDIA_ERR_NONE) {
980 media_svc_debug("Update is successful. Sending noti for this");
981 _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);
983 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
986 _media_svc_destroy_content_info(&content_info);
987 _media_svc_destroy_noti_item(noti_item);
992 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
994 sqlite3 *db_handle = (sqlite3 *)handle;
995 int ret = MS_MEDIA_ERR_NONE;
997 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
998 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
999 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
1001 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1003 ret = _media_svc_sql_begin_trans(uid);
1004 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1006 /* Update all folder record's modified date, which are changed above */
1007 char *update_folder_modified_time_sql = NULL;
1011 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1013 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1014 sqlite3_free(update_folder_modified_time_sql);
1016 if (ret != SQLITE_OK) {
1017 media_svc_error("failed to update folder modified time");
1018 _media_svc_sql_rollback_trans(uid);
1020 return MS_MEDIA_ERR_DB_INTERNAL;
1023 /* Update all items */
1024 char *select_all_sql = NULL;
1025 sqlite3_stmt *sql_stmt = NULL;
1026 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1028 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1030 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);
1032 media_svc_debug("[SQL query] : %s", select_all_sql);
1034 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1035 if (ret != MS_MEDIA_ERR_NONE) {
1036 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1037 _media_svc_sql_rollback_trans(uid);
1041 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1042 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1043 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1044 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1045 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1046 /*int media_type; */
1047 bool no_thumb = FALSE;
1049 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1050 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1051 media_uuid[sizeof(media_uuid) - 1] = '\0';
1053 media_svc_error("media UUID is NULL");
1054 return MS_MEDIA_ERR_DB_INTERNAL;
1057 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1058 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1059 media_path[sizeof(media_path) - 1] = '\0';
1061 media_svc_error("media path is NULL");
1062 return MS_MEDIA_ERR_DB_INTERNAL;
1065 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1066 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1067 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1069 media_svc_debug("media thumb path doesn't exist in DB");
1073 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1075 /* Update path, thumbnail path of this item */
1076 char *replaced_path = NULL;
1077 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1078 if (replaced_path == NULL) {
1079 media_svc_error("_media_svc_replace_path failed");
1080 SQLITE3_FINALIZE(sql_stmt);
1081 _media_svc_sql_rollback_trans(uid);
1082 return MS_MEDIA_ERR_INTERNAL;
1085 media_svc_debug("New media path : %s", replaced_path);
1086 media_svc_storage_type_e storage_type;
1089 /* If old thumb path is default or not */
1090 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1091 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1092 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path) - 1);
1094 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1095 if (ret != MS_MEDIA_ERR_NONE) {
1096 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1097 SAFE_FREE(replaced_path);
1098 SAFE_FREE(default_thumbnail_path);
1099 _media_svc_sql_rollback_trans(uid);
1103 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1104 if (ret != MS_MEDIA_ERR_NONE) {
1105 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1106 SAFE_FREE(replaced_path);
1107 SAFE_FREE(default_thumbnail_path);
1108 SQLITE3_FINALIZE(sql_stmt);
1109 _media_svc_sql_rollback_trans(uid);
1114 SAFE_FREE(default_thumbnail_path);
1116 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1119 char *update_item_sql = NULL;
1122 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1125 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1126 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
1128 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);
1130 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);
1134 ret = media_db_request_update_db_batch(update_item_sql, uid);
1135 sqlite3_free(update_item_sql);
1136 SAFE_FREE(replaced_path);
1138 if (ret != SQLITE_OK) {
1139 media_svc_error("failed to update item");
1140 SQLITE3_FINALIZE(sql_stmt);
1141 _media_svc_sql_rollback_trans(uid);
1143 return MS_MEDIA_ERR_DB_INTERNAL;
1146 /* Rename thumbnail file of file system */
1147 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1148 if ((!no_thumb) && (default_thumbnail_path != NULL) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1149 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1150 if (ret != MS_MEDIA_ERR_NONE)
1151 media_svc_error("_media_svc_rename_file failed : %d", ret);
1154 SAFE_FREE(default_thumbnail_path);
1157 SQLITE3_FINALIZE(sql_stmt);
1159 ret = _media_svc_sql_end_trans(uid);
1160 if (ret != MS_MEDIA_ERR_NONE) {
1161 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1162 _media_svc_sql_rollback_trans(uid);
1166 media_svc_debug("Folder update is successful. Sending noti for this");
1167 /* Get notification info */
1168 media_svc_noti_item *noti_item = NULL;
1169 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1170 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1172 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1173 _media_svc_destroy_noti_item(noti_item);
1175 return MS_MEDIA_ERR_NONE;
1178 int media_svc_request_update_db(const char *db_query, uid_t uid)
1180 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1182 return _media_svc_sql_query(db_query, uid);
1185 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)
1187 int ret = MS_MEDIA_ERR_NONE;
1188 sqlite3 *db_handle = (sqlite3 *)handle;
1191 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1192 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1194 /* Get notification info */
1195 media_svc_noti_item *noti_item = NULL;
1196 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1197 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1199 if (folder_id != NULL) {
1200 uuid = strndup(folder_id, strlen(folder_id));
1202 if (noti_item->media_uuid != NULL) {
1203 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1205 _media_svc_destroy_noti_item(noti_item);
1206 media_svc_error("folder uuid is wrong");
1207 return MS_MEDIA_ERR_DB_INTERNAL;
1211 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1212 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1213 _media_svc_destroy_noti_item(noti_item);
1219 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1221 sqlite3 *db_handle = (sqlite3 *)handle;
1223 media_svc_debug_fenter();
1225 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1226 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1227 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1228 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1230 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1233 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1235 sqlite3 *db_handle = (sqlite3 *)handle;
1237 media_svc_debug_fenter();
1239 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1241 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1244 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1246 sqlite3 *db_handle = (sqlite3 *)handle;
1248 media_svc_debug_fenter();
1250 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1252 return _media_db_check_corrupt(db_handle);
1255 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)
1257 sqlite3 *db_handle = (sqlite3 *)handle;
1259 media_svc_debug_fenter();
1261 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1262 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1264 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1267 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1269 int ret = MS_MEDIA_ERR_NONE;
1270 sqlite3 *db_handle = (sqlite3 *)handle;
1271 time_t sto_time = 0;
1272 int cur_time = _media_svc_get_file_time(folder_path);
1273 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1275 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1277 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1278 if (ret == MS_MEDIA_ERR_NONE) {
1279 if (sto_time != cur_time)
1280 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1286 int media_svc_update_item_begin(int data_cnt)
1288 media_svc_debug("Transaction data count : [%d]", data_cnt);
1290 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1292 g_media_svc_update_item_data_cnt = data_cnt;
1293 g_media_svc_update_item_cur_data_cnt = 0;
1295 return MS_MEDIA_ERR_NONE;
1298 int media_svc_update_item_end(uid_t uid)
1300 int ret = MS_MEDIA_ERR_NONE;
1302 media_svc_debug_fenter();
1304 if (g_media_svc_update_item_cur_data_cnt > 0)
1305 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1307 g_media_svc_update_item_data_cnt = 1;
1308 g_media_svc_update_item_cur_data_cnt = 0;
1313 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1315 int ret = MS_MEDIA_ERR_NONE;
1316 sqlite3 *db_handle = (sqlite3 *)handle;
1318 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1320 media_svc_media_type_e media_type;
1321 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1322 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1324 media_svc_content_info_s content_info;
1325 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1328 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1329 if (ret != MS_MEDIA_ERR_NONE)
1332 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1333 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1335 return MS_MEDIA_ERR_NONE;
1337 if (ret != MS_MEDIA_ERR_NONE) {
1338 _media_svc_destroy_content_info(&content_info);
1342 if (g_media_svc_update_item_data_cnt == 1) {
1344 ret = _media_svc_update_meta_with_data(&content_info);
1345 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1347 } else if (g_media_svc_update_item_cur_data_cnt < (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 g_media_svc_update_item_cur_data_cnt++;
1354 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1356 ret = _media_svc_update_meta_with_data(&content_info);
1357 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1359 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1360 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1362 g_media_svc_update_item_cur_data_cnt = 0;
1365 media_svc_error("Error in media_svc_update_item_meta");
1366 _media_svc_destroy_content_info(&content_info);
1367 return MS_MEDIA_ERR_INTERNAL;
1370 _media_svc_destroy_content_info(&content_info);
1375 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)
1377 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1380 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1382 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1384 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1387 int media_svc_check_pinyin_support(bool *support)
1389 *support = _media_svc_check_pinyin_support();
1391 return MS_MEDIA_ERR_NONE;
1394 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1396 int ret = MS_MEDIA_ERR_NONE;
1397 sqlite3 * db_handle = (sqlite3 *)handle;
1399 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1401 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1402 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1404 ret = _media_svc_update_media_view(db_handle, uid);
1405 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1410 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1412 int ret = MS_MEDIA_ERR_NONE;
1413 sqlite3 * db_handle = (sqlite3 *)handle;
1415 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1416 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1418 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1423 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1425 int ret = MS_MEDIA_ERR_NONE;
1426 sqlite3 * db_handle = (sqlite3 *)handle;
1428 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1429 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1431 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1436 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1438 int ret = MS_MEDIA_ERR_NONE;
1439 sqlite3 * db_handle = (sqlite3 *)handle;
1441 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1442 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1444 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1449 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1451 int ret = MS_MEDIA_ERR_NONE;
1453 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1458 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1460 sqlite3 * db_handle = (sqlite3 *)handle;
1462 media_svc_debug_fenter();
1464 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1465 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1467 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1470 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1472 int ret = MS_MEDIA_ERR_NONE;
1474 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1475 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1477 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1478 new_content_info->size = content_info->size;
1479 new_content_info->modified_time = content_info->modified_time;
1480 new_content_info->is_drm = content_info->is_drm;
1481 new_content_info->media_type = content_info->media_type;
1483 if (STRING_VALID(content_info->mime_type)) {
1484 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1485 if (ret != MS_MEDIA_ERR_NONE)
1486 media_svc_error("strcpy mime_type failed");
1489 if (STRING_VALID(content_info->thumbnail_path)) {
1490 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1491 if (ret != MS_MEDIA_ERR_NONE)
1492 media_svc_error("strcpy thumbnail_path failed");
1495 new_content_info->media_meta.duration = content_info->media_meta.duration;
1496 new_content_info->media_meta.width = content_info->media_meta.width;
1497 new_content_info->media_meta.height = content_info->media_meta.height;
1500 if (content_info->added_time > 0)
1501 new_content_info->added_time = content_info->added_time;
1502 new_content_info->last_played_time = content_info->last_played_time;
1503 new_content_info->played_count = content_info->played_count;
1504 new_content_info->favourate = content_info->favourate;
1506 if (STRING_VALID(content_info->file_name)) {
1507 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1508 if (ret != MS_MEDIA_ERR_NONE)
1509 media_svc_error("strcpy file_name failed");
1512 if (STRING_VALID(content_info->media_meta.title)) {
1513 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1514 if (ret != MS_MEDIA_ERR_NONE)
1515 media_svc_error("strcpy title failed");
1518 if (STRING_VALID(content_info->media_meta.album)) {
1519 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1520 if (ret != MS_MEDIA_ERR_NONE)
1521 media_svc_error("strcpy album failed");
1524 if (STRING_VALID(content_info->media_meta.artist)) {
1525 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1526 if (ret != MS_MEDIA_ERR_NONE)
1527 media_svc_error("strcpy artist failed");
1530 if (STRING_VALID(content_info->media_meta.genre)) {
1531 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1532 if (ret != MS_MEDIA_ERR_NONE)
1533 media_svc_error("strcpy genre failed");
1536 if (STRING_VALID(content_info->media_meta.composer)) {
1537 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1538 if (ret != MS_MEDIA_ERR_NONE)
1539 media_svc_error("strcpy composer failed");
1542 if (STRING_VALID(content_info->media_meta.year)) {
1543 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1544 if (ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy year failed");
1548 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1549 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1550 if (ret != MS_MEDIA_ERR_NONE)
1551 media_svc_error("strcpy recorded_date failed");
1554 if (STRING_VALID(content_info->media_meta.copyright)) {
1555 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1556 if (ret != MS_MEDIA_ERR_NONE)
1557 media_svc_error("strcpy copyright failed");
1560 if (STRING_VALID(content_info->media_meta.track_num)) {
1561 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1562 if (ret != MS_MEDIA_ERR_NONE)
1563 media_svc_error("strcpy track_num failed");
1566 if (STRING_VALID(content_info->media_meta.description)) {
1567 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1568 if (ret != MS_MEDIA_ERR_NONE)
1569 media_svc_error("strcpy description failed");
1572 if (STRING_VALID(content_info->media_meta.weather)) {
1573 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1574 if (ret != MS_MEDIA_ERR_NONE)
1575 media_svc_error("strcpy weather failed");
1578 if (STRING_VALID(content_info->media_meta.category)) {
1579 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1580 if (ret != MS_MEDIA_ERR_NONE)
1581 media_svc_error("strcpy category failed");
1584 if (STRING_VALID(content_info->media_meta.keyword)) {
1585 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1586 if (ret != MS_MEDIA_ERR_NONE)
1587 media_svc_error("strcpy keyword failed");
1590 if (STRING_VALID(content_info->media_meta.location_tag)) {
1591 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1592 if (ret != MS_MEDIA_ERR_NONE)
1593 media_svc_error("strcpy location_tag failed");
1596 if (STRING_VALID(content_info->media_meta.content_name)) {
1597 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1598 if (ret != MS_MEDIA_ERR_NONE)
1599 media_svc_error("strcpy content_name failed");
1602 if (STRING_VALID(content_info->media_meta.age_rating)) {
1603 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1604 if (ret != MS_MEDIA_ERR_NONE)
1605 media_svc_error("strcpy age_rating failed");
1608 if (STRING_VALID(content_info->media_meta.author)) {
1609 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1610 if (ret != MS_MEDIA_ERR_NONE)
1611 media_svc_error("strcpy author failed");
1614 if (STRING_VALID(content_info->media_meta.provider)) {
1615 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1616 if (ret != MS_MEDIA_ERR_NONE)
1617 media_svc_error("strcpy provider failed");
1620 if (STRING_VALID(content_info->media_meta.datetaken)) {
1621 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1622 if (ret != MS_MEDIA_ERR_NONE)
1623 media_svc_error("strcpy datetaken failed");
1625 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1626 if (new_content_info->timeline == 0) {
1627 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1628 new_content_info->timeline = new_content_info->modified_time;
1630 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1634 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1635 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1636 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1637 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1638 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1640 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1641 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1642 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1643 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1644 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1645 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1647 new_content_info->media_meta.rating = content_info->media_meta.rating;
1652 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1654 int ret = MS_MEDIA_ERR_NONE;
1655 sqlite3 *db_handle = (sqlite3 *)handle;
1656 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1657 bool append_album = FALSE;
1659 /* Checking parameters if they are valid */
1660 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1661 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1662 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1664 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1665 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1667 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1668 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1669 return MS_MEDIA_ERR_INVALID_PARAMETER;
1672 if (content_info->size <= 0) {
1673 media_svc_error("invalid size condition[%d]", content_info->size);
1674 return MS_MEDIA_ERR_INVALID_PARAMETER;
1677 if (content_info->modified_time <= 0) {
1678 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1679 return MS_MEDIA_ERR_INVALID_PARAMETER;
1683 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1685 media_svc_content_info_s _new_content_info;
1686 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1688 media_svc_media_type_e media_type;
1690 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1691 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1693 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1694 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1695 media_svc_debug("Do nothing[%d]", media_type);
1696 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1697 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1699 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1701 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1703 /* Extracting thumbnail */
1704 if (_new_content_info.thumbnail_path == NULL) {
1705 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1706 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1710 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1711 if (ret == MS_MEDIA_ERR_NONE)
1712 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1714 if (_new_content_info.media_meta.width <= 0)
1715 _new_content_info.media_meta.width = width;
1717 if (_new_content_info.media_meta.height <= 0)
1718 _new_content_info.media_meta.height = height;
1723 /* set othere data to the structure, which is passed as parameters */
1724 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1726 /* Set or Get folder id */
1727 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);
1728 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1730 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1731 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1733 /* register album table data */
1736 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1737 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1739 if (ret != MS_MEDIA_ERR_NONE) {
1740 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1741 media_svc_debug("album does not exist. So start to make album art");
1742 append_album = TRUE;
1744 media_svc_debug("other error");
1745 append_album = FALSE;
1748 _new_content_info.album_id = album_id;
1749 append_album = FALSE;
1751 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1752 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1754 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1757 media_svc_debug("album already exists. don't need to make album art");
1758 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1759 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1764 if (append_album == TRUE) {
1766 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1767 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1768 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);
1770 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1772 _new_content_info.album_id = album_id;
1775 /* Insert to db - calling _media_svc_insert_item_with_data */
1776 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1778 if (ret == MS_MEDIA_ERR_NONE)
1779 media_svc_debug("Insertion is successful.");
1781 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1782 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1784 _media_svc_destroy_content_info(&_new_content_info);
1786 /* handling returned value - important */
1790 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1792 _media_svc_destroy_content_info(content_info);
1795 int media_svc_generate_uuid(char **uuid)
1797 char *gen_uuid = NULL;
1798 gen_uuid = _media_info_generate_uuid();
1799 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1801 *uuid = strdup(gen_uuid);
1803 return MS_MEDIA_ERR_NONE;
1806 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1808 sqlite3 * db_handle = (sqlite3 *)handle;
1810 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1812 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1815 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1817 sqlite3 * db_handle = (sqlite3 *)handle;
1819 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1820 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1821 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1822 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1824 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1827 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1829 sqlite3 * db_handle = (sqlite3 *)handle;
1831 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1832 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1833 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1835 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1838 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, const char *storage_path, const char *storage_account, media_svc_storage_type_e storage_type, uid_t uid)
1840 int ret = MS_MEDIA_ERR_NONE;
1841 sqlite3 *db_handle = (sqlite3 *)handle;
1843 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1844 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1845 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1846 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1848 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1849 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1851 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1852 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1853 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1855 ret = _media_svc_update_media_view(db_handle, uid);
1856 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1862 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1864 int ret = MS_MEDIA_ERR_NONE;
1865 sqlite3 *db_handle = (sqlite3 *)handle;
1866 media_svc_storage_type_e storage_type;
1868 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1869 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1871 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1872 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1874 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1875 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1877 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1878 if (ret != MS_MEDIA_ERR_NONE)
1879 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1881 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) || (storage_type == MEDIA_SVC_STORAGE_CLOUD)) {
1882 ret = _media_svc_drop_media_table(storage_id, uid);
1883 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1885 ret = _media_svc_update_media_view(db_handle, uid);
1886 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1892 int media_svc_insert_folder_begin(int data_cnt)
1894 media_svc_debug("Transaction data count : [%d]", data_cnt);
1896 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1898 g_media_svc_insert_folder_data_cnt = data_cnt;
1899 g_media_svc_insert_folder_cur_data_cnt = 0;
1901 return MS_MEDIA_ERR_NONE;
1904 int media_svc_insert_folder_end(uid_t uid)
1906 int ret = MS_MEDIA_ERR_NONE;
1908 media_svc_debug_fenter();
1910 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1911 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1913 g_media_svc_insert_folder_data_cnt = 1;
1914 g_media_svc_insert_folder_cur_data_cnt = 0;
1919 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1921 int ret = MS_MEDIA_ERR_NONE;
1922 sqlite3 * db_handle = (sqlite3 *)handle;
1923 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1925 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1926 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1927 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1928 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1930 if (g_media_svc_insert_folder_data_cnt == 1) {
1932 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1934 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1936 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1937 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1939 g_media_svc_insert_folder_cur_data_cnt++;
1941 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1943 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1944 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1946 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1947 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1949 g_media_svc_insert_folder_cur_data_cnt = 0;
1952 media_svc_error("Error in media_svc_set_insert_folder");
1953 return MS_MEDIA_ERR_INTERNAL;
1959 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1961 int ret = MS_MEDIA_ERR_NONE;
1963 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1968 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1970 int ret = MS_MEDIA_ERR_NONE;
1971 sqlite3 * db_handle = (sqlite3 *)handle;
1973 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1975 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1980 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)
1982 int ret = MS_MEDIA_ERR_NONE;
1983 sqlite3 * db_handle = (sqlite3 *)handle;
1984 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1985 media_svc_media_type_e media_type;
1987 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1988 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1989 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1990 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1992 media_svc_content_info_s content_info;
1993 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1995 /*Set basic media info*/
1996 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1997 if (ret != MS_MEDIA_ERR_NONE) {
1998 media_svc_error("_media_svc_set_media_info fail %d", ret);
2002 /*Set or Get folder id*/
2003 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2004 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2006 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2007 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2009 if (g_media_svc_insert_item_data_cnt == 1) {
2011 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2012 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2014 if (g_insert_with_noti)
2015 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2016 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2018 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2019 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2021 if (g_insert_with_noti)
2022 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2024 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2025 g_media_svc_insert_item_cur_data_cnt++;
2027 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2029 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2030 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2032 if (g_insert_with_noti)
2033 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2035 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2036 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2038 media_svc_debug("_media_svc_list_query_do over");
2040 if (g_insert_with_noti) {
2041 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2042 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2043 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2045 /* Recreate noti list */
2046 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2047 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2050 g_media_svc_insert_item_cur_data_cnt = 0;
2053 media_svc_error("Error in media_svc_insert_item_pass1");
2054 _media_svc_destroy_content_info(&content_info);
2055 return MS_MEDIA_ERR_INTERNAL;
2058 _media_svc_destroy_content_info(&content_info);
2060 return MS_MEDIA_ERR_NONE;
2063 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)
2065 int ret = MS_MEDIA_ERR_NONE;
2066 sqlite3 * db_handle = (sqlite3 *)handle;
2067 sqlite3_stmt *sql_stmt = NULL;
2068 media_svc_media_type_e media_type;
2069 media_svc_content_info_s content_info;
2070 GArray *db_data_array = NULL;
2071 media_svc_item_info_s *db_data = NULL;
2074 media_svc_debug_fenter();
2076 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2077 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2079 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2080 media_svc_error("storage type is incorrect[%d]", storage_type);
2081 return MS_MEDIA_ERR_INVALID_PARAMETER;
2084 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2085 if (db_data_array == NULL) {
2086 media_svc_error("db_data_array is NULL. Out of memory");
2087 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2091 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2092 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2093 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2095 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2096 media_svc_error("folder no recursive extract");
2098 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2100 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);
2101 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2102 media_svc_error("folder recursive extract");
2104 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2106 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);
2109 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2111 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2114 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2115 if (ret != MS_MEDIA_ERR_NONE) {
2116 media_svc_error("error when get list. err = [%d]", ret);
2117 g_array_free(db_data_array, FALSE);
2121 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2123 db_data = malloc(sizeof(media_svc_item_info_s));
2124 if (db_data == NULL) {
2125 media_svc_error("Out of memory");
2129 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2130 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2132 g_array_append_val(db_data_array, db_data);
2135 SQLITE3_FINALIZE(sql_stmt);
2136 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2138 while (db_data_array->len != 0) {
2140 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2141 g_array_remove_index(db_data_array, 0);
2143 if ((db_data == NULL) || (db_data->path == NULL)) {
2144 media_svc_error("invalid db data");
2148 media_type = db_data->media_type;
2149 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2150 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2151 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2153 content_info.media_type = media_type;
2154 content_info.storage_type = storage_type;
2156 _media_svc_set_default_value(&content_info, FALSE);
2158 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2159 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2160 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2161 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2162 media_svc_debug("Do nothing[%d]", media_type);
2163 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2164 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2166 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2168 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2170 _media_svc_destroy_content_info(&content_info);
2171 SAFE_FREE(db_data->path);
2177 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2179 _media_svc_publish_noti_list(idx);
2180 _media_svc_destroy_noti_list(idx);
2182 /* Recreate noti list */
2183 ret = _media_svc_create_noti_list(idx);
2187 while (db_data_array->len != 0) {
2189 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2190 g_array_remove_index(db_data_array, 0);
2193 SAFE_FREE(db_data->path);
2199 g_array_free(db_data_array, FALSE);
2200 db_data_array = NULL;
2202 media_svc_debug_fleave();
2204 return MS_MEDIA_ERR_NONE;
2207 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2209 int ret = MS_MEDIA_ERR_NONE;
2210 sqlite3 * db_handle = (sqlite3 *)handle;
2212 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2213 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2215 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2220 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2222 int ret = MS_MEDIA_ERR_NONE;
2224 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2229 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2231 int ret = MS_MEDIA_ERR_NONE;
2232 sqlite3 * db_handle = (sqlite3 *)handle;
2233 time_t modified_time = 0;
2234 int system_time = 0;
2236 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2238 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2240 system_time = _media_svc_get_file_time(path);
2241 media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2243 if (system_time != modified_time && system_time != 0)
2251 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2253 sqlite3 * db_handle = (sqlite3 *)handle;
2255 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2256 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2258 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2261 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2263 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2264 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2266 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2269 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2271 int ret = MS_MEDIA_ERR_NONE;
2272 sqlite3 * db_handle = (sqlite3 *)handle;
2274 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2276 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2281 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2283 int ret = MS_MEDIA_ERR_NONE;
2284 sqlite3 * db_handle = (sqlite3 *)handle;
2287 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2288 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2289 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2291 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2292 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2295 media_svc_debug("item is exist in database");
2296 return MS_MEDIA_ERR_NONE;
2298 media_svc_debug("item is not exist in database");
2299 return MS_MEDIA_ERR_DB_NO_RECORD;
2302 return MS_MEDIA_ERR_NONE;
2305 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2307 int ret = MS_MEDIA_ERR_NONE;
2308 sqlite3 * db_handle = (sqlite3 *)handle;
2311 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2312 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2313 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2316 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2317 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2321 media_svc_debug("item is exist in database");
2322 return MS_MEDIA_ERR_NONE;
2324 media_svc_debug("item is not exist in database");
2325 return MS_MEDIA_ERR_DB_NO_RECORD;
2328 return MS_MEDIA_ERR_NONE;
2331 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2333 int ret = MS_MEDIA_ERR_NONE;
2334 sqlite3 * db_handle = (sqlite3 *)handle;
2336 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2337 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2339 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2344 int media_svc_append_query(const char *query, uid_t uid)
2346 int ret = MS_MEDIA_ERR_NONE;
2348 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2350 ret = _media_svc_append_query_list(query, uid);
2355 int media_svc_send_query(uid_t uid)
2357 int ret = MS_MEDIA_ERR_NONE;
2359 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2364 int media_svc_get_media_type(const char *path, int *mediatype)
2366 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2368 return _media_svc_get_media_type(path, mediatype);