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) {
1202 uuid = 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);
1212 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1214 sqlite3 *db_handle = (sqlite3 *)handle;
1216 media_svc_debug_fenter();
1218 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1219 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1220 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1221 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1223 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1226 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, int user_version, uid_t uid)
1228 sqlite3 *db_handle = (sqlite3 *)handle;
1230 media_svc_debug_fenter();
1232 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1234 return _media_svc_check_db_upgrade(db_handle, need_full_scan, user_version, uid);
1237 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1239 sqlite3 *db_handle = (sqlite3 *)handle;
1241 media_svc_debug_fenter();
1243 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1245 return _media_db_check_corrupt(db_handle);
1248 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)
1250 sqlite3 *db_handle = (sqlite3 *)handle;
1252 media_svc_debug_fenter();
1254 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1255 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1257 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1260 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1262 int ret = MS_MEDIA_ERR_NONE;
1263 sqlite3 *db_handle = (sqlite3 *)handle;
1264 time_t sto_time = 0;
1265 int cur_time = _media_svc_get_file_time(folder_path);
1266 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1268 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1270 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1271 if (ret == MS_MEDIA_ERR_NONE) {
1272 if (sto_time != cur_time) {
1273 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1280 int media_svc_update_item_begin(int data_cnt)
1282 media_svc_debug("Transaction data count : [%d]", data_cnt);
1284 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1286 g_media_svc_update_item_data_cnt = data_cnt;
1287 g_media_svc_update_item_cur_data_cnt = 0;
1289 return MS_MEDIA_ERR_NONE;
1292 int media_svc_update_item_end(uid_t uid)
1294 int ret = MS_MEDIA_ERR_NONE;
1296 media_svc_debug_fenter();
1298 if (g_media_svc_update_item_cur_data_cnt > 0) {
1300 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1303 g_media_svc_update_item_data_cnt = 1;
1304 g_media_svc_update_item_cur_data_cnt = 0;
1309 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1311 int ret = MS_MEDIA_ERR_NONE;
1312 sqlite3 *db_handle = (sqlite3 *)handle;
1314 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1316 media_svc_media_type_e media_type;
1317 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1318 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1320 media_svc_content_info_s content_info;
1321 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1324 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1325 if (ret != MS_MEDIA_ERR_NONE) {
1329 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1330 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1332 return MS_MEDIA_ERR_NONE;
1335 if (ret != MS_MEDIA_ERR_NONE) {
1336 _media_svc_destroy_content_info(&content_info);
1340 if (g_media_svc_update_item_data_cnt == 1) {
1342 ret = _media_svc_update_meta_with_data(&content_info);
1343 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1345 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1347 ret = _media_svc_update_meta_with_data(&content_info);
1348 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1350 g_media_svc_update_item_cur_data_cnt++;
1352 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1354 ret = _media_svc_update_meta_with_data(&content_info);
1355 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1357 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1358 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1360 g_media_svc_update_item_cur_data_cnt = 0;
1363 media_svc_error("Error in media_svc_update_item_meta");
1364 _media_svc_destroy_content_info(&content_info);
1365 return MS_MEDIA_ERR_INTERNAL;
1368 _media_svc_destroy_content_info(&content_info);
1373 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)
1375 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1378 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1380 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1382 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1385 int media_svc_check_pinyin_support(bool *support)
1387 *support = _media_svc_check_pinyin_support();
1389 return MS_MEDIA_ERR_NONE;
1392 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1394 int ret = MS_MEDIA_ERR_NONE;
1395 sqlite3 * db_handle = (sqlite3 *)handle;
1397 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1399 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1400 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1402 ret = _media_svc_update_media_view(db_handle, uid);
1403 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1408 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1410 int ret = MS_MEDIA_ERR_NONE;
1411 sqlite3 * db_handle = (sqlite3 *)handle;
1413 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1414 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1416 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1421 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1423 int ret = MS_MEDIA_ERR_NONE;
1424 sqlite3 * db_handle = (sqlite3 *)handle;
1426 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1427 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1429 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1434 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1436 int ret = MS_MEDIA_ERR_NONE;
1437 sqlite3 * db_handle = (sqlite3 *)handle;
1439 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1440 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1442 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1447 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1449 int ret = MS_MEDIA_ERR_NONE;
1451 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1456 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1458 sqlite3 * db_handle = (sqlite3 *)handle;
1460 media_svc_debug_fenter();
1462 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1463 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1465 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1468 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1470 int ret = MS_MEDIA_ERR_NONE;
1472 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1473 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1475 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1476 new_content_info->size = content_info->size;
1477 new_content_info->modified_time = content_info->modified_time;
1478 new_content_info->is_drm = content_info->is_drm;
1479 new_content_info->media_type = content_info->media_type;
1481 if (STRING_VALID(content_info->mime_type)) {
1482 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1483 if (ret != MS_MEDIA_ERR_NONE)
1484 media_svc_error("strcpy mime_type failed");
1487 if (STRING_VALID(content_info->thumbnail_path)) {
1488 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1489 if (ret != MS_MEDIA_ERR_NONE)
1490 media_svc_error("strcpy thumbnail_path failed");
1493 new_content_info->media_meta.duration = content_info->media_meta.duration;
1494 new_content_info->media_meta.width = content_info->media_meta.width;
1495 new_content_info->media_meta.height = content_info->media_meta.height;
1498 if (content_info->added_time > 0)
1499 new_content_info->added_time = content_info->added_time;
1500 new_content_info->last_played_time = content_info->last_played_time;
1501 new_content_info->played_count = content_info->played_count;
1502 new_content_info->favourate = content_info->favourate;
1504 if (STRING_VALID(content_info->file_name)) {
1505 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1506 if (ret != MS_MEDIA_ERR_NONE)
1507 media_svc_error("strcpy file_name failed");
1510 if (STRING_VALID(content_info->media_meta.title)) {
1511 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1512 if (ret != MS_MEDIA_ERR_NONE)
1513 media_svc_error("strcpy title failed");
1516 if (STRING_VALID(content_info->media_meta.album)) {
1517 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1518 if (ret != MS_MEDIA_ERR_NONE)
1519 media_svc_error("strcpy album failed");
1522 if (STRING_VALID(content_info->media_meta.artist)) {
1523 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1524 if (ret != MS_MEDIA_ERR_NONE)
1525 media_svc_error("strcpy artist failed");
1528 if (STRING_VALID(content_info->media_meta.genre)) {
1529 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1530 if (ret != MS_MEDIA_ERR_NONE)
1531 media_svc_error("strcpy genre failed");
1534 if (STRING_VALID(content_info->media_meta.composer)) {
1535 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1536 if (ret != MS_MEDIA_ERR_NONE)
1537 media_svc_error("strcpy composer failed");
1540 if (STRING_VALID(content_info->media_meta.year)) {
1541 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1542 if (ret != MS_MEDIA_ERR_NONE)
1543 media_svc_error("strcpy year failed");
1546 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1547 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1548 if (ret != MS_MEDIA_ERR_NONE)
1549 media_svc_error("strcpy recorded_date failed");
1552 if (STRING_VALID(content_info->media_meta.copyright)) {
1553 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1554 if (ret != MS_MEDIA_ERR_NONE)
1555 media_svc_error("strcpy copyright failed");
1558 if (STRING_VALID(content_info->media_meta.track_num)) {
1559 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1560 if (ret != MS_MEDIA_ERR_NONE)
1561 media_svc_error("strcpy track_num failed");
1564 if (STRING_VALID(content_info->media_meta.description)) {
1565 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1566 if (ret != MS_MEDIA_ERR_NONE)
1567 media_svc_error("strcpy description failed");
1570 if (STRING_VALID(content_info->media_meta.weather)) {
1571 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1572 if (ret != MS_MEDIA_ERR_NONE)
1573 media_svc_error("strcpy weather failed");
1576 if (STRING_VALID(content_info->media_meta.category)) {
1577 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1578 if (ret != MS_MEDIA_ERR_NONE)
1579 media_svc_error("strcpy category failed");
1582 if (STRING_VALID(content_info->media_meta.keyword)) {
1583 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1584 if (ret != MS_MEDIA_ERR_NONE)
1585 media_svc_error("strcpy keyword failed");
1588 if (STRING_VALID(content_info->media_meta.location_tag)) {
1589 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1590 if (ret != MS_MEDIA_ERR_NONE)
1591 media_svc_error("strcpy location_tag failed");
1594 if (STRING_VALID(content_info->media_meta.content_name)) {
1595 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1596 if (ret != MS_MEDIA_ERR_NONE)
1597 media_svc_error("strcpy content_name failed");
1600 if (STRING_VALID(content_info->media_meta.age_rating)) {
1601 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1602 if (ret != MS_MEDIA_ERR_NONE)
1603 media_svc_error("strcpy age_rating failed");
1606 if (STRING_VALID(content_info->media_meta.author)) {
1607 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1608 if (ret != MS_MEDIA_ERR_NONE)
1609 media_svc_error("strcpy author failed");
1612 if (STRING_VALID(content_info->media_meta.provider)) {
1613 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1614 if (ret != MS_MEDIA_ERR_NONE)
1615 media_svc_error("strcpy provider failed");
1618 if (STRING_VALID(content_info->media_meta.datetaken)) {
1619 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1620 if (ret != MS_MEDIA_ERR_NONE)
1621 media_svc_error("strcpy datetaken failed");
1623 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1624 if (new_content_info->timeline == 0) {
1625 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1626 new_content_info->timeline = new_content_info->modified_time;
1628 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1632 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1633 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1634 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1635 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1637 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1638 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1639 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1640 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1641 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1642 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1644 new_content_info->media_meta.rating = content_info->media_meta.rating;
1649 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1651 int ret = MS_MEDIA_ERR_NONE;
1652 sqlite3 *db_handle = (sqlite3 *)handle;
1653 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1654 bool append_album = FALSE;
1656 /* Checking parameters if they are valid */
1657 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1658 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1659 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1661 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1662 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1664 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1665 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1666 return MS_MEDIA_ERR_INVALID_PARAMETER;
1669 if (content_info->size <= 0) {
1670 media_svc_error("invalid size condition[%d]", content_info->size);
1671 return MS_MEDIA_ERR_INVALID_PARAMETER;
1674 if (content_info->modified_time <= 0) {
1675 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1676 return MS_MEDIA_ERR_INVALID_PARAMETER;
1680 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1682 media_svc_content_info_s _new_content_info;
1683 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1685 media_svc_media_type_e media_type;
1687 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1688 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1690 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1691 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1693 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1694 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1696 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1699 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1701 /* Extracting thumbnail */
1702 int ini_val = _media_svc_get_ini_value();
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);
1715 if (_new_content_info.media_meta.width <= 0)
1716 _new_content_info.media_meta.width = width;
1718 if (_new_content_info.media_meta.height <= 0)
1719 _new_content_info.media_meta.height = height;
1725 /* set othere data to the structure, which is passed as parameters */
1726 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1728 /* Set or Get folder id */
1729 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);
1730 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1732 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1733 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1735 /* register album table data */
1738 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1739 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1741 if (ret != MS_MEDIA_ERR_NONE) {
1742 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1743 media_svc_debug("album does not exist. So start to make album art");
1744 append_album = TRUE;
1746 media_svc_debug("other error");
1747 append_album = FALSE;
1750 _new_content_info.album_id = album_id;
1751 append_album = FALSE;
1753 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1754 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1756 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1759 media_svc_debug("album already exists. don't need to make album art");
1760 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1761 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1766 if (append_album == TRUE) {
1768 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1769 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1770 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);
1772 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1774 _new_content_info.album_id = album_id;
1777 /* Insert to db - calling _media_svc_insert_item_with_data */
1778 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1780 if (ret == MS_MEDIA_ERR_NONE) {
1781 media_svc_debug("Insertion is successful.");
1784 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1785 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1788 _media_svc_destroy_content_info(&_new_content_info);
1790 /* handling returned value - important */
1794 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1796 _media_svc_destroy_content_info(content_info);
1799 int media_svc_generate_uuid(char **uuid)
1801 char *gen_uuid = NULL;
1802 gen_uuid = _media_info_generate_uuid();
1803 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1805 *uuid = strdup(gen_uuid);
1807 return MS_MEDIA_ERR_NONE;
1810 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1812 sqlite3 * db_handle = (sqlite3 *)handle;
1814 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1816 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1819 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1821 sqlite3 * db_handle = (sqlite3 *)handle;
1823 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1824 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1825 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1826 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1828 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1831 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1833 sqlite3 * db_handle = (sqlite3 *)handle;
1835 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1836 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1837 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1839 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1842 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)
1844 int ret = MS_MEDIA_ERR_NONE;
1845 sqlite3 *db_handle = (sqlite3 *)handle;
1847 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1848 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1849 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1850 media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1852 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1853 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1855 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1856 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1857 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1859 ret = _media_svc_update_media_view(db_handle, uid);
1860 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1866 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1868 int ret = MS_MEDIA_ERR_NONE;
1869 sqlite3 *db_handle = (sqlite3 *)handle;
1870 media_svc_storage_type_e storage_type;
1872 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1873 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1875 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1876 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1878 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1879 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1881 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1882 if (ret != MS_MEDIA_ERR_NONE) {
1883 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1886 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) || (storage_type == MEDIA_SVC_STORAGE_CLOUD)) {
1887 ret = _media_svc_drop_media_table(storage_id, uid);
1888 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1890 ret = _media_svc_update_media_view(db_handle, uid);
1891 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1897 int media_svc_insert_folder_begin(int data_cnt)
1899 media_svc_debug("Transaction data count : [%d]", data_cnt);
1901 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1903 g_media_svc_insert_folder_data_cnt = data_cnt;
1904 g_media_svc_insert_folder_cur_data_cnt = 0;
1906 return MS_MEDIA_ERR_NONE;
1909 int media_svc_insert_folder_end(uid_t uid)
1911 int ret = MS_MEDIA_ERR_NONE;
1913 media_svc_debug_fenter();
1915 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1917 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1920 g_media_svc_insert_folder_data_cnt = 1;
1921 g_media_svc_insert_folder_cur_data_cnt = 0;
1926 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1928 int ret = MS_MEDIA_ERR_NONE;
1929 sqlite3 * db_handle = (sqlite3 *)handle;
1930 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1932 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1933 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1934 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1935 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1937 if (g_media_svc_insert_folder_data_cnt == 1) {
1939 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
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 g_media_svc_insert_folder_cur_data_cnt++;
1948 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1950 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1951 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1953 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1954 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1956 g_media_svc_insert_folder_cur_data_cnt = 0;
1959 media_svc_error("Error in media_svc_set_insert_folder");
1960 return MS_MEDIA_ERR_INTERNAL;
1966 int media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
1968 int ret = MS_MEDIA_ERR_NONE;
1970 ret = _media_svc_delete_invalid_folder(storage_id, uid);
1975 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1977 int ret = MS_MEDIA_ERR_NONE;
1978 sqlite3 * db_handle = (sqlite3 *)handle;
1980 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1982 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1987 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)
1989 int ret = MS_MEDIA_ERR_NONE;
1990 sqlite3 * db_handle = (sqlite3 *)handle;
1991 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1992 media_svc_media_type_e media_type;
1994 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1995 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1996 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1997 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1999 media_svc_content_info_s content_info;
2000 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2002 /*Set basic media info*/
2003 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
2004 if (ret != MS_MEDIA_ERR_NONE) {
2005 media_svc_error("_media_svc_set_media_info fail %d", ret);
2009 //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);
2011 /*Set or Get folder id*/
2012 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2013 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2015 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2016 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2018 if (g_media_svc_insert_item_data_cnt == 1) {
2020 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2021 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2023 if (g_insert_with_noti)
2024 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2025 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2027 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2028 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2030 if (g_insert_with_noti)
2031 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2033 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2034 g_media_svc_insert_item_cur_data_cnt++;
2036 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2038 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2039 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2041 if (g_insert_with_noti)
2042 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2044 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2045 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2047 media_svc_debug("_media_svc_list_query_do over");
2049 if (g_insert_with_noti) {
2050 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2051 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2052 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2054 /* Recreate noti list */
2055 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2056 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2059 g_media_svc_insert_item_cur_data_cnt = 0;
2062 media_svc_error("Error in media_svc_insert_item_pass1");
2063 _media_svc_destroy_content_info(&content_info);
2064 return MS_MEDIA_ERR_INTERNAL;
2067 _media_svc_destroy_content_info(&content_info);
2069 return MS_MEDIA_ERR_NONE;
2072 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)
2074 int ret = MS_MEDIA_ERR_NONE;
2075 sqlite3 * db_handle = (sqlite3 *)handle;
2076 sqlite3_stmt *sql_stmt = NULL;
2077 media_svc_media_type_e media_type;
2078 media_svc_content_info_s content_info;
2079 GArray *db_data_array = NULL;
2080 media_svc_item_info_s *db_data = NULL;
2083 media_svc_debug_fenter();
2085 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2086 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2088 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2089 media_svc_error("storage type is incorrect[%d]", storage_type);
2090 return MS_MEDIA_ERR_INVALID_PARAMETER;
2093 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2094 if (db_data_array == NULL) {
2095 media_svc_error("db_data_array is NULL. Out of memory");
2096 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2100 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2101 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);
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, char *storage_id, 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);