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.");
196 /*init storage table*/
197 ret = _media_svc_init_storage(db_handle, uid);
198 if (ret != MS_MEDIA_ERR_NONE) {
199 media_svc_error("_media_svc_make_table_query fail.");
204 /*create face table. from tizen 3.0*/
205 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
206 if (ret != MS_MEDIA_ERR_NONE) {
207 media_svc_error("_media_svc_make_table_query fail.");
211 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
212 if (ret != MS_MEDIA_ERR_NONE) {
213 media_svc_error("_media_svc_make_table_query fail.");
217 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
218 ret = _media_svc_sql_query(sql, uid);
219 if (ret != MS_MEDIA_ERR_NONE) {
220 media_svc_error("user_version update fail.");
224 _media_svc_destroy_table_query();
226 media_svc_debug_fleave();
228 return MS_MEDIA_ERR_NONE;
230 _media_svc_destroy_table_query();
232 media_svc_debug_fleave();
237 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
239 int ret = MS_MEDIA_ERR_NONE;
240 media_svc_storage_type_e type;
242 ret = _media_svc_get_storage_type_by_path(path, &type, uid);
243 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
245 *storage_type = type;
250 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
252 int ret = MS_MEDIA_ERR_NONE;
253 sqlite3 *db_handle = (sqlite3 *)handle;
255 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
256 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
258 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
263 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
265 int ret = MS_MEDIA_ERR_NONE;
266 sqlite3 *db_handle = (sqlite3 *)handle;
269 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
270 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
271 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
273 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
274 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
277 media_svc_debug("item is exist in database");
278 return MS_MEDIA_ERR_NONE;
280 media_svc_debug("item is not exist in database");
281 return MS_MEDIA_ERR_DB_NO_RECORD;
284 return MS_MEDIA_ERR_NONE;
287 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
289 media_svc_debug("Transaction data count : [%d]", data_cnt);
291 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
293 g_media_svc_insert_item_data_cnt = data_cnt;
294 g_media_svc_insert_item_cur_data_cnt = 0;
296 /* Prepare for making noti item list */
298 media_svc_debug("making noti list from pid[%d]", from_pid);
299 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
300 return MS_MEDIA_ERR_OUT_OF_MEMORY;
302 _media_svc_set_noti_from_pid(from_pid);
303 g_insert_with_noti = TRUE;
306 return MS_MEDIA_ERR_NONE;
309 int media_svc_insert_item_end(uid_t uid)
311 int ret = MS_MEDIA_ERR_NONE;
313 media_svc_debug_fenter();
315 if (g_media_svc_insert_item_cur_data_cnt > 0) {
317 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
318 if (g_insert_with_noti) {
319 media_svc_debug("sending noti list");
320 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
321 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
322 g_insert_with_noti = FALSE;
323 _media_svc_set_noti_from_pid(-1);
327 g_media_svc_insert_item_data_cnt = 1;
328 g_media_svc_insert_item_cur_data_cnt = 0;
333 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)
335 int ret = MS_MEDIA_ERR_NONE;
336 sqlite3 *db_handle = (sqlite3 *)handle;
337 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
338 media_svc_media_type_e media_type;
340 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
341 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
342 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
343 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
345 media_svc_content_info_s content_info;
346 memset(&content_info, 0, sizeof(media_svc_content_info_s));
349 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
350 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
351 if (ret != MS_MEDIA_ERR_NONE)
354 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
356 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
357 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
359 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
361 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
363 /*Set or Get folder id*/
364 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
365 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
367 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
368 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
370 if (g_media_svc_insert_item_data_cnt == 1) {
372 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
373 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
375 if (g_insert_with_noti)
376 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
378 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
380 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
381 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
383 if (g_insert_with_noti)
384 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
386 g_media_svc_insert_item_cur_data_cnt++;
388 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
390 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
391 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
393 if (g_insert_with_noti)
394 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
396 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
397 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
399 if (g_insert_with_noti) {
400 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
401 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
403 /* Recreate noti list */
404 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
405 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
408 g_media_svc_insert_item_cur_data_cnt = 0;
411 media_svc_error("Error in media_svc_insert_item_bulk");
412 _media_svc_destroy_content_info(&content_info);
413 return MS_MEDIA_ERR_INTERNAL;
416 _media_svc_destroy_content_info(&content_info);
418 return MS_MEDIA_ERR_NONE;
421 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)
423 int ret = MS_MEDIA_ERR_NONE;
424 sqlite3 *db_handle = (sqlite3 *)handle;
425 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
426 media_svc_media_type_e media_type;
427 int ini_val = _media_svc_get_ini_value();
429 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
430 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
431 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
432 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
434 media_svc_content_info_s content_info;
435 memset(&content_info, 0, sizeof(media_svc_content_info_s));
438 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
439 if (ret != MS_MEDIA_ERR_NONE) {
443 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
445 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
446 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
448 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
451 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
453 /*Set or Get folder id*/
454 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
455 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
457 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
458 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
460 /* Extracting thumbnail */
462 if (content_info.thumbnail_path == NULL) {
463 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
464 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
468 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
469 if (ret == MS_MEDIA_ERR_NONE)
470 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
472 if (content_info.media_meta.width <= 0)
473 content_info.media_meta.width = width;
475 if (content_info.media_meta.height <= 0)
476 content_info.media_meta.height = height;
482 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
484 if (ret == MS_MEDIA_ERR_NONE) {
485 media_svc_debug("Insertion is successful. Sending noti for this");
486 _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);
487 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
488 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
491 _media_svc_destroy_content_info(&content_info);
495 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
496 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
498 int ret = MS_MEDIA_ERR_NONE;
499 sqlite3 *db_handle = (sqlite3 *)handle;
500 char *file_name = NULL;
501 char *folder_path = NULL;
502 int modified_time = 0;
503 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
504 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
505 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
508 media_svc_debug_fenter();
510 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
511 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
512 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
513 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
514 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
516 /*check and update folder*/
517 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
518 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
521 file_name = g_path_get_basename(dest_path);
523 /*get modified_time*/
524 modified_time = _media_svc_get_file_time(dest_path);
526 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
527 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
528 if (ret != MS_MEDIA_ERR_NONE) {
529 media_svc_error("_media_svc_get_media_type_by_path failed");
530 SAFE_FREE(file_name);
534 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
535 /*get old thumbnail_path*/
536 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
537 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
538 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
539 SAFE_FREE(file_name);
543 /* If old thumb path is default or not */
544 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
545 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
546 strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
548 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
550 SAFE_FREE(default_thumbnail_path);
553 if (g_media_svc_move_item_data_cnt == 1) {
556 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
557 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);
559 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
561 SAFE_FREE(file_name);
562 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
564 media_svc_debug("Move is successful. Sending noti for this");
566 /* Get notification info */
567 media_svc_noti_item *noti_item = NULL;
568 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
569 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
571 /* Send notification for move */
572 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
573 _media_svc_destroy_noti_item(noti_item);
575 /*update folder modified_time*/
576 folder_path = g_path_get_dirname(dest_path);
577 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
578 SAFE_FREE(folder_path);
579 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
581 ret = _media_svc_update_folder_table(storage_id, uid);
582 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
584 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
587 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
588 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);
590 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
592 SAFE_FREE(file_name);
593 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
595 /*update folder modified_time*/
596 folder_path = g_path_get_dirname(dest_path);
597 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
598 SAFE_FREE(folder_path);
599 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
601 g_media_svc_move_item_cur_data_cnt++;
603 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
606 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
607 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);
609 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
611 SAFE_FREE(file_name);
612 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
614 /*update folder modified_time*/
615 folder_path = g_path_get_dirname(dest_path);
616 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
617 SAFE_FREE(folder_path);
618 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
621 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
622 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
624 g_media_svc_move_item_cur_data_cnt = 0;
627 media_svc_error("Error in media_svc_move_item");
628 return MS_MEDIA_ERR_INTERNAL;
631 /*rename thumbnail file*/
632 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
633 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)) {
634 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
635 if (ret != MS_MEDIA_ERR_NONE)
636 media_svc_error("_media_svc_rename_file failed : %d", ret);
640 return MS_MEDIA_ERR_NONE;
643 int media_svc_set_item_validity_begin(int data_cnt)
645 media_svc_debug("Transaction data count : [%d]", data_cnt);
647 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
649 g_media_svc_item_validity_data_cnt = data_cnt;
650 g_media_svc_item_validity_cur_data_cnt = 0;
652 return MS_MEDIA_ERR_NONE;
655 int media_svc_set_item_validity_end(uid_t uid)
657 int ret = MS_MEDIA_ERR_NONE;
659 media_svc_debug_fenter();
661 if (g_media_svc_item_validity_cur_data_cnt > 0) {
663 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
666 g_media_svc_item_validity_data_cnt = 1;
667 g_media_svc_item_validity_cur_data_cnt = 0;
672 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
674 int ret = MS_MEDIA_ERR_NONE;
676 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
677 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
679 media_svc_debug("path=[%s], validity=[%d]", path, validity);
681 if (g_media_svc_item_validity_data_cnt == 1) {
683 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
685 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
687 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
688 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
690 g_media_svc_item_validity_cur_data_cnt++;
692 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
694 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
695 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
697 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
698 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
700 g_media_svc_item_validity_cur_data_cnt = 0;
704 media_svc_error("Error in media_svc_set_item_validity");
705 return MS_MEDIA_ERR_INTERNAL;
708 return MS_MEDIA_ERR_NONE;
711 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
713 int ret = MS_MEDIA_ERR_NONE;
714 sqlite3 *db_handle = (sqlite3 *)handle;
715 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
717 media_svc_debug_fenter();
719 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
720 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
721 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
724 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
725 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
728 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
729 /*Get thumbnail path to delete*/
730 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
731 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
732 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
734 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
735 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
738 /*Get thumbnail path to delete*/
739 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
740 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
744 /*Get thumbnail path to delete*/
745 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
746 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
748 if (g_media_svc_insert_item_data_cnt == 1) {
750 /* Get notification info */
751 media_svc_noti_item *noti_item = NULL;
752 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
753 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
756 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
757 if (ret != MS_MEDIA_ERR_NONE) {
758 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
759 _media_svc_destroy_noti_item(noti_item);
764 /* Send notification */
765 media_svc_debug("Deletion is successful. Sending noti for this");
766 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
767 _media_svc_destroy_noti_item(noti_item);
769 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
770 if (ret != MS_MEDIA_ERR_NONE) {
771 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
778 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
779 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
782 // Get count of media, which contains same thumbnail for music
783 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
784 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
785 if (ret != MS_MEDIA_ERR_NONE) {
786 media_svc_error("Failed to get thumbnail count : %d", ret);
790 if (thumb_count == 1) {
792 ret = _media_svc_remove_file(thumb_path);
793 if (ret != MS_MEDIA_ERR_NONE) {
794 media_svc_error("fail to remove thumbnail file.");
799 SAFE_FREE(default_thumbnail_path);
801 return MS_MEDIA_ERR_NONE;
804 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
806 int ret = MS_MEDIA_ERR_NONE;
808 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
810 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
811 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
813 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
814 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
816 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
817 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
818 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
820 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
822 /* remove thumbnails */
823 if (STRING_VALID(dirpath))
824 ret = _media_svc_remove_all_files_in_dir(dirpath);
825 SAFE_FREE(internal_thumb_path);
826 SAFE_FREE(external_thumb_path);
827 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
830 return MS_MEDIA_ERR_NONE;
833 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
835 sqlite3 *db_handle = (sqlite3 *)handle;
837 media_svc_debug_fenter();
839 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
840 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
841 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
843 /*Delete from DB and remove thumbnail files*/
844 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
847 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
849 sqlite3 *db_handle = (sqlite3 *)handle;
851 media_svc_debug_fenter();
853 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
854 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
856 /*Delete from DB and remove thumbnail files*/
857 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
860 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
862 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
863 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
865 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
868 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
870 sqlite3 *db_handle = (sqlite3 *)handle;
872 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
873 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
874 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
877 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
879 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
882 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
884 int ret = MS_MEDIA_ERR_NONE;
885 sqlite3 *db_handle = (sqlite3 *)handle;
886 media_svc_media_type_e media_type;
887 int ini_val = _media_svc_get_ini_value();
889 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
890 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
891 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
892 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
894 media_svc_content_info_s content_info;
895 memset(&content_info, 0, sizeof(media_svc_content_info_s));
898 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
899 if (ret != MS_MEDIA_ERR_NONE) {
903 /* Initialize thumbnail information to remake thumbnail. */
905 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
906 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
907 if (ret != MS_MEDIA_ERR_NONE) {
908 _media_svc_destroy_content_info(&content_info);
912 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)) {
913 ret = _media_svc_remove_file(thumb_path);
914 if (ret != MS_MEDIA_ERR_NONE) {
915 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
919 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
920 if (ret != MS_MEDIA_ERR_NONE) {
921 _media_svc_destroy_content_info(&content_info);
926 /* Get notification info */
927 media_svc_noti_item *noti_item = NULL;
928 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
929 if (ret != MS_MEDIA_ERR_NONE) {
930 _media_svc_destroy_content_info(&content_info);
934 media_type = noti_item->media_type;
935 content_info.media_type = media_type;
937 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
939 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
940 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
942 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
945 if (ret != MS_MEDIA_ERR_NONE) {
946 _media_svc_destroy_noti_item(noti_item);
947 _media_svc_destroy_content_info(&content_info);
951 /* Extracting thumbnail */
953 if (content_info.thumbnail_path == NULL) {
954 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
955 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
959 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
960 if (ret == MS_MEDIA_ERR_NONE) {
961 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
964 if (content_info.media_meta.width <= 0)
965 content_info.media_meta.width = width;
967 if (content_info.media_meta.height <= 0)
968 content_info.media_meta.height = height;
975 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
977 if (ret == MS_MEDIA_ERR_NONE) {
978 media_svc_debug("Update is successful. Sending noti for this");
979 _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);
981 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
984 _media_svc_destroy_content_info(&content_info);
985 _media_svc_destroy_noti_item(noti_item);
990 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
992 sqlite3 *db_handle = (sqlite3 *)handle;
993 int ret = MS_MEDIA_ERR_NONE;
995 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
996 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
997 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
999 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1001 ret = _media_svc_sql_begin_trans(uid);
1002 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1004 /* Update all folder record's modified date, which are changed above */
1005 char *update_folder_modified_time_sql = NULL;
1009 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1011 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1012 sqlite3_free(update_folder_modified_time_sql);
1014 if (ret != SQLITE_OK) {
1015 media_svc_error("failed to update folder modified time");
1016 _media_svc_sql_rollback_trans(uid);
1018 return MS_MEDIA_ERR_DB_INTERNAL;
1021 /* Update all items */
1022 char *select_all_sql = NULL;
1023 sqlite3_stmt *sql_stmt = NULL;
1024 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1026 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1028 select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from media where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", dst_path, dst_child_path);
1030 media_svc_debug("[SQL query] : %s", select_all_sql);
1032 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1033 if (ret != MS_MEDIA_ERR_NONE) {
1034 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1035 _media_svc_sql_rollback_trans(uid);
1039 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1040 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1041 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1042 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1043 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1044 /*int media_type; */
1045 bool no_thumb = FALSE;
1047 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1048 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1049 media_uuid[sizeof(media_uuid) - 1] = '\0';
1051 media_svc_error("media UUID is NULL");
1052 return MS_MEDIA_ERR_DB_INTERNAL;
1055 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1056 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1057 media_path[sizeof(media_path) - 1] = '\0';
1059 media_svc_error("media path is NULL");
1060 return MS_MEDIA_ERR_DB_INTERNAL;
1063 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1064 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1065 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1067 media_svc_debug("media thumb path doesn't exist in DB");
1071 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1073 /* Update path, thumbnail path of this item */
1074 char *replaced_path = NULL;
1075 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1076 if (replaced_path == NULL) {
1077 media_svc_error("_media_svc_replace_path failed");
1078 SQLITE3_FINALIZE(sql_stmt);
1079 _media_svc_sql_rollback_trans(uid);
1080 return MS_MEDIA_ERR_INTERNAL;
1083 media_svc_debug("New media path : %s", replaced_path);
1084 media_svc_storage_type_e storage_type;
1087 /* If old thumb path is default or not */
1088 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1089 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1090 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path) - 1);
1092 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1093 if (ret != MS_MEDIA_ERR_NONE) {
1094 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1095 SAFE_FREE(replaced_path);
1096 SAFE_FREE(default_thumbnail_path);
1097 _media_svc_sql_rollback_trans(uid);
1101 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1102 if (ret != MS_MEDIA_ERR_NONE) {
1103 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1104 SAFE_FREE(replaced_path);
1105 SAFE_FREE(default_thumbnail_path);
1106 SQLITE3_FINALIZE(sql_stmt);
1107 _media_svc_sql_rollback_trans(uid);
1112 SAFE_FREE(default_thumbnail_path);
1114 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1117 char *update_item_sql = NULL;
1120 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1123 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1124 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1126 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1129 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1133 ret = media_db_request_update_db_batch(update_item_sql, uid);
1134 sqlite3_free(update_item_sql);
1135 SAFE_FREE(replaced_path);
1137 if (ret != SQLITE_OK) {
1138 media_svc_error("failed to update item");
1139 SQLITE3_FINALIZE(sql_stmt);
1140 _media_svc_sql_rollback_trans(uid);
1142 return MS_MEDIA_ERR_DB_INTERNAL;
1145 /* Rename thumbnail file of file system */
1146 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1147 if ((!no_thumb) && (default_thumbnail_path != NULL) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1148 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1149 if (ret != MS_MEDIA_ERR_NONE) {
1150 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 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1205 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1206 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1207 _media_svc_destroy_noti_item(noti_item);
1213 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1215 sqlite3 *db_handle = (sqlite3 *)handle;
1217 media_svc_debug_fenter();
1219 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1220 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1221 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1222 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1224 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1227 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1229 sqlite3 *db_handle = (sqlite3 *)handle;
1231 media_svc_debug_fenter();
1233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1235 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1238 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1240 sqlite3 *db_handle = (sqlite3 *)handle;
1242 media_svc_debug_fenter();
1244 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1246 return _media_db_check_corrupt(db_handle);
1249 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)
1251 sqlite3 *db_handle = (sqlite3 *)handle;
1253 media_svc_debug_fenter();
1255 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1256 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1258 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1261 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1263 int ret = MS_MEDIA_ERR_NONE;
1264 sqlite3 *db_handle = (sqlite3 *)handle;
1265 time_t sto_time = 0;
1266 int cur_time = _media_svc_get_file_time(folder_path);
1267 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1269 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1271 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1272 if (ret == MS_MEDIA_ERR_NONE) {
1273 if (sto_time != cur_time) {
1274 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1281 int media_svc_update_item_begin(int data_cnt)
1283 media_svc_debug("Transaction data count : [%d]", data_cnt);
1285 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1287 g_media_svc_update_item_data_cnt = data_cnt;
1288 g_media_svc_update_item_cur_data_cnt = 0;
1290 return MS_MEDIA_ERR_NONE;
1293 int media_svc_update_item_end(uid_t uid)
1295 int ret = MS_MEDIA_ERR_NONE;
1297 media_svc_debug_fenter();
1299 if (g_media_svc_update_item_cur_data_cnt > 0) {
1301 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1304 g_media_svc_update_item_data_cnt = 1;
1305 g_media_svc_update_item_cur_data_cnt = 0;
1310 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1312 int ret = MS_MEDIA_ERR_NONE;
1313 sqlite3 *db_handle = (sqlite3 *)handle;
1315 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1317 media_svc_media_type_e media_type;
1318 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1319 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1321 media_svc_content_info_s content_info;
1322 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1325 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1326 if (ret != MS_MEDIA_ERR_NONE) {
1330 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1331 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1333 return MS_MEDIA_ERR_NONE;
1336 if (ret != MS_MEDIA_ERR_NONE) {
1337 _media_svc_destroy_content_info(&content_info);
1341 if (g_media_svc_update_item_data_cnt == 1) {
1343 ret = _media_svc_update_meta_with_data(&content_info);
1344 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1346 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1348 ret = _media_svc_update_meta_with_data(&content_info);
1349 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1351 g_media_svc_update_item_cur_data_cnt++;
1353 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1355 ret = _media_svc_update_meta_with_data(&content_info);
1356 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1358 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1359 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1361 g_media_svc_update_item_cur_data_cnt = 0;
1364 media_svc_error("Error in media_svc_update_item_meta");
1365 _media_svc_destroy_content_info(&content_info);
1366 return MS_MEDIA_ERR_INTERNAL;
1369 _media_svc_destroy_content_info(&content_info);
1374 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)
1376 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1379 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1381 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1383 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1386 int media_svc_check_pinyin_support(bool *support)
1388 *support = _media_svc_check_pinyin_support();
1390 return MS_MEDIA_ERR_NONE;
1393 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1395 int ret = MS_MEDIA_ERR_NONE;
1396 sqlite3 * db_handle = (sqlite3 *)handle;
1398 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1400 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1401 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1403 ret = _media_svc_update_media_view(db_handle, uid);
1404 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1409 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1411 int ret = MS_MEDIA_ERR_NONE;
1412 sqlite3 * db_handle = (sqlite3 *)handle;
1414 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1415 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1417 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1422 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1424 int ret = MS_MEDIA_ERR_NONE;
1425 sqlite3 * db_handle = (sqlite3 *)handle;
1427 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1428 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1430 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1435 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1437 int ret = MS_MEDIA_ERR_NONE;
1438 sqlite3 * db_handle = (sqlite3 *)handle;
1440 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1441 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1443 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1448 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1450 int ret = MS_MEDIA_ERR_NONE;
1452 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1457 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1459 sqlite3 * db_handle = (sqlite3 *)handle;
1461 media_svc_debug_fenter();
1463 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1464 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1466 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1469 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1471 int ret = MS_MEDIA_ERR_NONE;
1473 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1474 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1476 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1477 new_content_info->size = content_info->size;
1478 new_content_info->modified_time = content_info->modified_time;
1479 new_content_info->is_drm = content_info->is_drm;
1480 new_content_info->media_type = content_info->media_type;
1482 if (STRING_VALID(content_info->mime_type)) {
1483 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1484 if (ret != MS_MEDIA_ERR_NONE)
1485 media_svc_error("strcpy mime_type failed");
1488 if (STRING_VALID(content_info->thumbnail_path)) {
1489 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1490 if (ret != MS_MEDIA_ERR_NONE)
1491 media_svc_error("strcpy thumbnail_path failed");
1494 new_content_info->media_meta.duration = content_info->media_meta.duration;
1495 new_content_info->media_meta.width = content_info->media_meta.width;
1496 new_content_info->media_meta.height = content_info->media_meta.height;
1499 if (content_info->added_time > 0)
1500 new_content_info->added_time = content_info->added_time;
1501 new_content_info->last_played_time = content_info->last_played_time;
1502 new_content_info->played_count = content_info->played_count;
1503 new_content_info->favourate = content_info->favourate;
1505 if (STRING_VALID(content_info->file_name)) {
1506 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1507 if (ret != MS_MEDIA_ERR_NONE)
1508 media_svc_error("strcpy file_name failed");
1511 if (STRING_VALID(content_info->media_meta.title)) {
1512 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1513 if (ret != MS_MEDIA_ERR_NONE)
1514 media_svc_error("strcpy title failed");
1517 if (STRING_VALID(content_info->media_meta.album)) {
1518 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1519 if (ret != MS_MEDIA_ERR_NONE)
1520 media_svc_error("strcpy album failed");
1523 if (STRING_VALID(content_info->media_meta.artist)) {
1524 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1525 if (ret != MS_MEDIA_ERR_NONE)
1526 media_svc_error("strcpy artist failed");
1529 if (STRING_VALID(content_info->media_meta.genre)) {
1530 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1531 if (ret != MS_MEDIA_ERR_NONE)
1532 media_svc_error("strcpy genre failed");
1535 if (STRING_VALID(content_info->media_meta.composer)) {
1536 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1537 if (ret != MS_MEDIA_ERR_NONE)
1538 media_svc_error("strcpy composer failed");
1541 if (STRING_VALID(content_info->media_meta.year)) {
1542 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1543 if (ret != MS_MEDIA_ERR_NONE)
1544 media_svc_error("strcpy year failed");
1547 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1548 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1549 if (ret != MS_MEDIA_ERR_NONE)
1550 media_svc_error("strcpy recorded_date failed");
1553 if (STRING_VALID(content_info->media_meta.copyright)) {
1554 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1555 if (ret != MS_MEDIA_ERR_NONE)
1556 media_svc_error("strcpy copyright failed");
1559 if (STRING_VALID(content_info->media_meta.track_num)) {
1560 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1561 if (ret != MS_MEDIA_ERR_NONE)
1562 media_svc_error("strcpy track_num failed");
1565 if (STRING_VALID(content_info->media_meta.description)) {
1566 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1567 if (ret != MS_MEDIA_ERR_NONE)
1568 media_svc_error("strcpy description failed");
1571 if (STRING_VALID(content_info->media_meta.weather)) {
1572 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1573 if (ret != MS_MEDIA_ERR_NONE)
1574 media_svc_error("strcpy weather failed");
1577 if (STRING_VALID(content_info->media_meta.category)) {
1578 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1579 if (ret != MS_MEDIA_ERR_NONE)
1580 media_svc_error("strcpy category failed");
1583 if (STRING_VALID(content_info->media_meta.keyword)) {
1584 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1585 if (ret != MS_MEDIA_ERR_NONE)
1586 media_svc_error("strcpy keyword failed");
1589 if (STRING_VALID(content_info->media_meta.location_tag)) {
1590 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1591 if (ret != MS_MEDIA_ERR_NONE)
1592 media_svc_error("strcpy location_tag failed");
1595 if (STRING_VALID(content_info->media_meta.content_name)) {
1596 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1597 if (ret != MS_MEDIA_ERR_NONE)
1598 media_svc_error("strcpy content_name failed");
1601 if (STRING_VALID(content_info->media_meta.age_rating)) {
1602 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1603 if (ret != MS_MEDIA_ERR_NONE)
1604 media_svc_error("strcpy age_rating failed");
1607 if (STRING_VALID(content_info->media_meta.author)) {
1608 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1609 if (ret != MS_MEDIA_ERR_NONE)
1610 media_svc_error("strcpy author failed");
1613 if (STRING_VALID(content_info->media_meta.provider)) {
1614 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1615 if (ret != MS_MEDIA_ERR_NONE)
1616 media_svc_error("strcpy provider failed");
1619 if (STRING_VALID(content_info->media_meta.datetaken)) {
1620 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1621 if (ret != MS_MEDIA_ERR_NONE)
1622 media_svc_error("strcpy datetaken failed");
1624 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1625 if (new_content_info->timeline == 0) {
1626 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1627 new_content_info->timeline = new_content_info->modified_time;
1629 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1633 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1634 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1635 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1636 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1638 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1639 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1640 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1641 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1642 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1643 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1645 new_content_info->media_meta.rating = content_info->media_meta.rating;
1650 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1652 int ret = MS_MEDIA_ERR_NONE;
1653 sqlite3 *db_handle = (sqlite3 *)handle;
1654 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1655 bool append_album = FALSE;
1657 /* Checking parameters if they are valid */
1658 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1659 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1660 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1662 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1663 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1665 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1666 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1667 return MS_MEDIA_ERR_INVALID_PARAMETER;
1670 if (content_info->size <= 0) {
1671 media_svc_error("invalid size condition[%d]", content_info->size);
1672 return MS_MEDIA_ERR_INVALID_PARAMETER;
1675 if (content_info->modified_time <= 0) {
1676 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1677 return MS_MEDIA_ERR_INVALID_PARAMETER;
1681 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1683 media_svc_content_info_s _new_content_info;
1684 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1686 media_svc_media_type_e media_type;
1688 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1689 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1691 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1692 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1694 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1695 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1697 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1700 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1702 /* Extracting thumbnail */
1703 int ini_val = _media_svc_get_ini_value();
1705 if (_new_content_info.thumbnail_path == NULL) {
1706 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1707 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1711 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1712 if (ret == MS_MEDIA_ERR_NONE) {
1713 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1716 if (_new_content_info.media_meta.width <= 0)
1717 _new_content_info.media_meta.width = width;
1719 if (_new_content_info.media_meta.height <= 0)
1720 _new_content_info.media_meta.height = height;
1726 /* set othere data to the structure, which is passed as parameters */
1727 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1729 /* Set or Get folder id */
1730 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);
1731 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1733 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1734 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1736 /* register album table data */
1739 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1740 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1742 if (ret != MS_MEDIA_ERR_NONE) {
1743 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1744 media_svc_debug("album does not exist. So start to make album art");
1745 append_album = TRUE;
1747 media_svc_debug("other error");
1748 append_album = FALSE;
1751 _new_content_info.album_id = album_id;
1752 append_album = FALSE;
1754 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1755 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1757 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1760 media_svc_debug("album already exists. don't need to make album art");
1761 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1762 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1767 if (append_album == TRUE) {
1769 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1770 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1771 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);
1773 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1775 _new_content_info.album_id = album_id;
1778 /* Insert to db - calling _media_svc_insert_item_with_data */
1779 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1781 if (ret == MS_MEDIA_ERR_NONE) {
1782 media_svc_debug("Insertion is successful.");
1785 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1786 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1789 _media_svc_destroy_content_info(&_new_content_info);
1791 /* handling returned value - important */
1795 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1797 _media_svc_destroy_content_info(content_info);
1800 int media_svc_generate_uuid(char **uuid)
1802 char *gen_uuid = NULL;
1803 gen_uuid = _media_info_generate_uuid();
1804 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1806 *uuid = strdup(gen_uuid);
1808 return MS_MEDIA_ERR_NONE;
1811 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1813 sqlite3 * db_handle = (sqlite3 *)handle;
1815 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1817 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1820 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1822 sqlite3 * db_handle = (sqlite3 *)handle;
1824 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1825 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1826 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1827 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1829 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1832 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1834 sqlite3 * db_handle = (sqlite3 *)handle;
1836 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1837 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1838 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1840 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1843 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)
1845 int ret = MS_MEDIA_ERR_NONE;
1846 sqlite3 *db_handle = (sqlite3 *)handle;
1848 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1849 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1850 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1851 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1853 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1854 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1856 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1857 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1858 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1860 ret = _media_svc_update_media_view(db_handle, uid);
1861 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1867 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1869 int ret = MS_MEDIA_ERR_NONE;
1870 sqlite3 *db_handle = (sqlite3 *)handle;
1871 media_svc_storage_type_e storage_type;
1873 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1874 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1876 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1877 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1879 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1880 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1882 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1883 if (ret != MS_MEDIA_ERR_NONE) {
1884 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1887 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) || (storage_type == MEDIA_SVC_STORAGE_CLOUD)) {
1888 ret = _media_svc_drop_media_table(storage_id, uid);
1889 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1891 ret = _media_svc_update_media_view(db_handle, uid);
1892 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1898 int media_svc_insert_folder_begin(int data_cnt)
1900 media_svc_debug("Transaction data count : [%d]", data_cnt);
1902 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1904 g_media_svc_insert_folder_data_cnt = data_cnt;
1905 g_media_svc_insert_folder_cur_data_cnt = 0;
1907 return MS_MEDIA_ERR_NONE;
1910 int media_svc_insert_folder_end(uid_t uid)
1912 int ret = MS_MEDIA_ERR_NONE;
1914 media_svc_debug_fenter();
1916 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1918 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1921 g_media_svc_insert_folder_data_cnt = 1;
1922 g_media_svc_insert_folder_cur_data_cnt = 0;
1927 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1929 int ret = MS_MEDIA_ERR_NONE;
1930 sqlite3 * db_handle = (sqlite3 *)handle;
1931 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1933 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1934 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1935 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1936 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1938 if (g_media_svc_insert_folder_data_cnt == 1) {
1940 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1942 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1944 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1945 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1947 g_media_svc_insert_folder_cur_data_cnt++;
1949 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1951 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1952 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1954 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1955 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1957 g_media_svc_insert_folder_cur_data_cnt = 0;
1960 media_svc_error("Error in media_svc_set_insert_folder");
1961 return MS_MEDIA_ERR_INTERNAL;
1967 int media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
1969 int ret = MS_MEDIA_ERR_NONE;
1971 ret = _media_svc_delete_invalid_folder(storage_id, uid);
1976 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1978 int ret = MS_MEDIA_ERR_NONE;
1979 sqlite3 * db_handle = (sqlite3 *)handle;
1981 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1983 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1988 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)
1990 int ret = MS_MEDIA_ERR_NONE;
1991 sqlite3 * db_handle = (sqlite3 *)handle;
1992 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1993 media_svc_media_type_e media_type;
1995 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1996 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1997 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1998 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2000 media_svc_content_info_s content_info;
2001 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2003 /*Set basic media info*/
2004 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
2005 if (ret != MS_MEDIA_ERR_NONE) {
2006 media_svc_error("_media_svc_set_media_info fail %d", ret);
2010 //media_svc_debug("total %d, cur %d insert flag %d", g_media_svc_insert_item_data_cnt, g_media_svc_insert_item_cur_data_cnt, g_insert_with_noti);
2012 /*Set or Get folder id*/
2013 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2014 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2016 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2017 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2019 if (g_media_svc_insert_item_data_cnt == 1) {
2021 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2022 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2024 if (g_insert_with_noti)
2025 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2026 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2028 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2029 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2031 if (g_insert_with_noti)
2032 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2034 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2035 g_media_svc_insert_item_cur_data_cnt++;
2037 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2039 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2040 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2042 if (g_insert_with_noti)
2043 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2045 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2046 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2048 media_svc_debug("_media_svc_list_query_do over");
2050 if (g_insert_with_noti) {
2051 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2052 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2053 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2055 /* Recreate noti list */
2056 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2057 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2060 g_media_svc_insert_item_cur_data_cnt = 0;
2063 media_svc_error("Error in media_svc_insert_item_pass1");
2064 _media_svc_destroy_content_info(&content_info);
2065 return MS_MEDIA_ERR_INTERNAL;
2068 _media_svc_destroy_content_info(&content_info);
2070 return MS_MEDIA_ERR_NONE;
2073 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)
2075 int ret = MS_MEDIA_ERR_NONE;
2076 sqlite3 * db_handle = (sqlite3 *)handle;
2077 sqlite3_stmt *sql_stmt = NULL;
2078 media_svc_media_type_e media_type;
2079 media_svc_content_info_s content_info;
2080 GArray *db_data_array = NULL;
2081 media_svc_item_info_s *db_data = NULL;
2084 media_svc_debug_fenter();
2086 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2087 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2089 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2090 media_svc_error("storage type is incorrect[%d]", storage_type);
2091 return MS_MEDIA_ERR_INVALID_PARAMETER;
2094 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2095 if (db_data_array == NULL) {
2096 media_svc_error("db_data_array is NULL. Out of memory");
2097 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2101 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2102 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2103 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2106 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2107 media_svc_error("folder no recursive extract");
2108 if (is_burst == 1) {
2109 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2111 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);
2113 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2114 media_svc_error("folder recursive extract");
2115 if (is_burst == 1) {
2116 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2118 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);
2121 if (is_burst == 1) {
2122 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2124 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2128 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2129 if (ret != MS_MEDIA_ERR_NONE) {
2130 media_svc_error("error when get list. err = [%d]", ret);
2131 g_array_free(db_data_array, FALSE);
2135 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2137 db_data = malloc(sizeof(media_svc_item_info_s));
2138 if (db_data == NULL) {
2139 media_svc_error("Out of memory");
2143 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2144 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2146 g_array_append_val(db_data_array, db_data);
2149 SQLITE3_FINALIZE(sql_stmt);
2150 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2152 while (db_data_array->len != 0) {
2154 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2155 g_array_remove_index(db_data_array, 0);
2157 if ((db_data == NULL) || (db_data->path == NULL)) {
2158 media_svc_error("invalid db data");
2162 media_type = db_data->media_type;
2163 //media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2164 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2165 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2167 content_info.media_type = media_type;
2168 content_info.storage_type = storage_type;
2170 _media_svc_set_default_value(&content_info, FALSE);
2172 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2174 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2175 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2177 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2180 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2182 _media_svc_destroy_content_info(&content_info);
2183 SAFE_FREE(db_data->path);
2189 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2191 _media_svc_publish_noti_list(idx);
2192 _media_svc_destroy_noti_list(idx);
2194 /* Recreate noti list */
2195 ret = _media_svc_create_noti_list(idx);
2198 g_array_free(db_data_array, FALSE);
2199 db_data_array = NULL;
2201 media_svc_debug_fleave();
2203 return MS_MEDIA_ERR_NONE;
2206 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2208 int ret = MS_MEDIA_ERR_NONE;
2209 sqlite3 * db_handle = (sqlite3 *)handle;
2211 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2212 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2214 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2219 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2221 int ret = MS_MEDIA_ERR_NONE;
2223 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2228 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2230 int ret = MS_MEDIA_ERR_NONE;
2231 sqlite3 * db_handle = (sqlite3 *)handle;
2232 time_t modified_time = 0;
2233 int system_time = 0;
2235 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2237 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2239 system_time = _media_svc_get_file_time(path);
2240 media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2242 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);