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"
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_create_table(MediaSvcHandle *handle, uid_t uid)
114 int ret = MS_MEDIA_ERR_NONE;
115 sqlite3 *db_handle = (sqlite3 *)handle;
117 media_svc_debug_fenter();
119 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
120 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
122 /*create media table*/
123 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
124 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
126 /*create folder table*/
127 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
128 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
130 /*create playlist_map table*/
131 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
132 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
134 /*create playlist table*/
135 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
136 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
138 /* create album table*/
139 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
140 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
142 /*create tag_map table*/
143 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
144 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
147 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
148 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
150 /*create bookmark table*/
151 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
152 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
154 /*create storage table. from tizen 2.4*/
155 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
156 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
159 /*init storage table*/
160 ret = _media_svc_init_storage(db_handle, uid);
161 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
163 /*create face table. from tizen 3.0*/
164 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
165 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
167 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
168 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
170 _media_svc_destroy_table_query();
172 media_svc_debug_fleave();
174 return MS_MEDIA_ERR_NONE;
177 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
179 int ret = MS_MEDIA_ERR_NONE;
180 media_svc_storage_type_e type;
182 ret = _media_svc_get_storage_type_by_path(path, &type, uid);
183 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
185 *storage_type = type;
190 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
192 int ret = MS_MEDIA_ERR_NONE;
193 sqlite3 *db_handle = (sqlite3 *)handle;
195 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
196 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
198 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
203 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
205 int ret = MS_MEDIA_ERR_NONE;
206 sqlite3 *db_handle = (sqlite3 *)handle;
209 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
210 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
211 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
213 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
214 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
217 media_svc_debug("item is exist in database");
218 return MS_MEDIA_ERR_NONE;
220 media_svc_debug("item is not exist in database");
221 return MS_MEDIA_ERR_DB_NO_RECORD;
224 return MS_MEDIA_ERR_NONE;
227 int media_svc_insert_item_begin(MediaSvcHandle *handle, int data_cnt, int with_noti, int from_pid)
229 sqlite3 *db_handle = (sqlite3 *)handle;
231 media_svc_debug("Transaction data count : [%d]", data_cnt);
233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
234 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
236 g_media_svc_insert_item_data_cnt = data_cnt;
237 g_media_svc_insert_item_cur_data_cnt = 0;
239 /* Prepare for making noti item list */
241 media_svc_debug("making noti list from pid[%d]", from_pid);
242 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE) {
243 return MS_MEDIA_ERR_OUT_OF_MEMORY;
246 _media_svc_set_noti_from_pid(from_pid);
247 g_insert_with_noti = TRUE;
250 return MS_MEDIA_ERR_NONE;
253 int media_svc_insert_item_end(MediaSvcHandle *handle, uid_t uid)
255 int ret = MS_MEDIA_ERR_NONE;
256 sqlite3 *db_handle = (sqlite3 *)handle;
258 media_svc_debug_fenter();
260 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
262 if (g_media_svc_insert_item_cur_data_cnt > 0) {
264 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
265 if (g_insert_with_noti) {
266 media_svc_debug("sending noti list");
267 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
268 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
269 g_insert_with_noti = FALSE;
270 _media_svc_set_noti_from_pid(-1);
274 g_media_svc_insert_item_data_cnt = 1;
275 g_media_svc_insert_item_cur_data_cnt = 0;
280 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)
282 int ret = MS_MEDIA_ERR_NONE;
283 sqlite3 *db_handle = (sqlite3 *)handle;
284 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
285 media_svc_media_type_e media_type;
287 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
288 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
289 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
290 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
292 media_svc_content_info_s content_info;
293 memset(&content_info, 0, sizeof(media_svc_content_info_s));
296 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
297 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
298 if (ret != MS_MEDIA_ERR_NONE) {
302 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
304 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
305 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
307 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
309 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
311 /*Set or Get folder id*/
312 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
313 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
315 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
316 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
318 if (g_media_svc_insert_item_data_cnt == 1) {
320 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
321 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
323 if (g_insert_with_noti)
324 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
326 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
328 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
329 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
331 if (g_insert_with_noti)
332 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
334 g_media_svc_insert_item_cur_data_cnt++;
336 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
338 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
339 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
341 if (g_insert_with_noti)
342 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
344 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
345 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
347 if (g_insert_with_noti) {
348 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
349 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
351 /* Recreate noti list */
352 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
353 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
356 g_media_svc_insert_item_cur_data_cnt = 0;
359 media_svc_error("Error in media_svc_insert_item_bulk");
360 _media_svc_destroy_content_info(&content_info);
361 return MS_MEDIA_ERR_INTERNAL;
364 _media_svc_destroy_content_info(&content_info);
366 return MS_MEDIA_ERR_NONE;
369 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)
371 int ret = MS_MEDIA_ERR_NONE;
372 sqlite3 *db_handle = (sqlite3 *)handle;
373 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
374 media_svc_media_type_e media_type;
375 int ini_val = _media_svc_get_ini_value();
377 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
378 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
379 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
380 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
382 media_svc_content_info_s content_info;
383 memset(&content_info, 0, sizeof(media_svc_content_info_s));
386 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
387 if (ret != MS_MEDIA_ERR_NONE) {
391 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
393 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
394 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
396 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
399 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
401 /*Set or Get folder id*/
402 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
403 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
405 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
406 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
408 /* Extracting thumbnail */
410 if (content_info.thumbnail_path == NULL) {
411 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
412 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
416 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
417 if (ret == MS_MEDIA_ERR_NONE) {
418 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
421 if (content_info.media_meta.width <= 0)
422 content_info.media_meta.width = width;
424 if (content_info.media_meta.height <= 0)
425 content_info.media_meta.height = height;
431 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
433 if (ret == MS_MEDIA_ERR_NONE) {
434 media_svc_debug("Insertion is successful. Sending noti for this");
435 _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);
436 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
437 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
440 _media_svc_destroy_content_info(&content_info);
444 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
445 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
447 int ret = MS_MEDIA_ERR_NONE;
448 sqlite3 *db_handle = (sqlite3 *)handle;
449 char *file_name = NULL;
450 char *folder_path = NULL;
451 int modified_time = 0;
452 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
453 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
454 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
457 media_svc_debug_fenter();
459 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
460 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
461 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
462 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
463 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
465 /*check and update folder*/
466 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
467 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
470 file_name = g_path_get_basename(dest_path);
472 /*get modified_time*/
473 modified_time = _media_svc_get_file_time(dest_path);
475 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
476 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
477 if (ret != MS_MEDIA_ERR_NONE) {
478 media_svc_error("_media_svc_get_media_type_by_path failed");
479 SAFE_FREE(file_name);
483 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
484 /*get old thumbnail_path*/
485 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
486 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
487 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
488 SAFE_FREE(file_name);
492 /* If old thumb path is default or not */
493 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
494 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
495 strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
497 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
499 SAFE_FREE(default_thumbnail_path);
502 if (g_media_svc_move_item_data_cnt == 1) {
505 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
506 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
508 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
510 SAFE_FREE(file_name);
511 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
513 media_svc_debug("Move is successful. Sending noti for this");
515 /* Get notification info */
516 media_svc_noti_item *noti_item = NULL;
517 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
518 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
520 /* Send notification for move */
521 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
522 _media_svc_destroy_noti_item(noti_item);
524 /*update folder modified_time*/
525 folder_path = g_path_get_dirname(dest_path);
526 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
527 SAFE_FREE(folder_path);
528 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
530 ret = _media_svc_update_folder_table(db_handle, storage_id, uid);
531 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
533 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
536 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
537 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
539 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
541 SAFE_FREE(file_name);
542 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
544 /*update folder modified_time*/
545 folder_path = g_path_get_dirname(dest_path);
546 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
547 SAFE_FREE(folder_path);
548 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
550 g_media_svc_move_item_cur_data_cnt++;
552 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
555 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
556 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
558 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
560 SAFE_FREE(file_name);
561 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
563 /*update folder modified_time*/
564 folder_path = g_path_get_dirname(dest_path);
565 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
566 SAFE_FREE(folder_path);
567 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
570 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
571 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
573 g_media_svc_move_item_cur_data_cnt = 0;
576 media_svc_error("Error in media_svc_move_item");
577 return MS_MEDIA_ERR_INTERNAL;
580 /*rename thumbnail file*/
581 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
582 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)) {
583 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
584 if (ret != MS_MEDIA_ERR_NONE)
585 media_svc_error("_media_svc_rename_file failed : %d", ret);
589 return MS_MEDIA_ERR_NONE;
592 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
594 sqlite3 *db_handle = (sqlite3 *)handle;
596 media_svc_debug("Transaction data count : [%d]", data_cnt);
598 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
599 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
601 g_media_svc_item_validity_data_cnt = data_cnt;
602 g_media_svc_item_validity_cur_data_cnt = 0;
604 return MS_MEDIA_ERR_NONE;
607 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
609 int ret = MS_MEDIA_ERR_NONE;
610 sqlite3 *db_handle = (sqlite3 *)handle;
612 media_svc_debug_fenter();
614 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
616 if (g_media_svc_item_validity_cur_data_cnt > 0) {
618 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
621 g_media_svc_item_validity_data_cnt = 1;
622 g_media_svc_item_validity_cur_data_cnt = 0;
627 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *storage_id, const char *path, int validity, uid_t uid)
629 int ret = MS_MEDIA_ERR_NONE;
630 sqlite3 *db_handle = (sqlite3 *)handle;
632 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
633 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
634 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
636 media_svc_debug("path=[%s], validity=[%d]", path, validity);
638 if (g_media_svc_item_validity_data_cnt == 1) {
640 return _media_svc_update_item_validity(db_handle, storage_id, path, validity, FALSE, uid);
642 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
644 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
645 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
647 g_media_svc_item_validity_cur_data_cnt++;
649 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
651 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
652 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
654 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
655 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
657 g_media_svc_item_validity_cur_data_cnt = 0;
661 media_svc_error("Error in media_svc_set_item_validity");
662 return MS_MEDIA_ERR_INTERNAL;
665 return MS_MEDIA_ERR_NONE;
668 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
670 int ret = MS_MEDIA_ERR_NONE;
671 sqlite3 *db_handle = (sqlite3 *)handle;
672 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
674 media_svc_debug_fenter();
676 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
677 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
678 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
681 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
682 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
685 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
686 /*Get thumbnail path to delete*/
687 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
688 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
689 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
691 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
692 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
695 /*Get thumbnail path to delete*/
696 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
697 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
701 /*Get thumbnail path to delete*/
702 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
703 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
705 if (g_media_svc_insert_item_data_cnt == 1) {
707 /* Get notification info */
708 media_svc_noti_item *noti_item = NULL;
709 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
710 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
713 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
714 if (ret != MS_MEDIA_ERR_NONE) {
715 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
716 _media_svc_destroy_noti_item(noti_item);
721 /* Send notification */
722 media_svc_debug("Deletion is successful. Sending noti for this");
723 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
724 _media_svc_destroy_noti_item(noti_item);
726 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
727 if (ret != MS_MEDIA_ERR_NONE) {
728 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
735 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
736 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
739 // Get count of media, which contains same thumbnail for music
740 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
741 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
742 if (ret != MS_MEDIA_ERR_NONE) {
743 media_svc_error("Failed to get thumbnail count : %d", ret);
747 if (thumb_count == 1) {
749 ret = _media_svc_remove_file(thumb_path);
750 if (ret != MS_MEDIA_ERR_NONE) {
751 media_svc_error("fail to remove thumbnail file.");
756 SAFE_FREE(default_thumbnail_path);
758 return MS_MEDIA_ERR_NONE;
761 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
763 int ret = MS_MEDIA_ERR_NONE;
764 sqlite3 *db_handle = (sqlite3 *)handle;
766 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
768 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
769 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
770 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
772 ret = _media_svc_truncate_table(db_handle, storage_id, storage_type, uid);
773 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
775 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
776 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
777 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
779 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
781 /* remove thumbnails */
782 ret = _media_svc_remove_all_files_in_dir(dirpath);
783 SAFE_FREE(internal_thumb_path);
784 SAFE_FREE(external_thumb_path);
785 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
788 return MS_MEDIA_ERR_NONE;
791 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
793 sqlite3 *db_handle = (sqlite3 *)handle;
795 media_svc_debug_fenter();
797 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
798 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
799 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
801 /*Delete from DB and remove thumbnail files*/
802 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
805 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
807 sqlite3 *db_handle = (sqlite3 *)handle;
809 media_svc_debug_fenter();
811 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
812 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
814 /*Delete from DB and remove thumbnail files*/
815 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
818 int media_svc_set_all_storage_items_validity(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
820 sqlite3 *db_handle = (sqlite3 *)handle;
822 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
823 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
824 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
826 return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
829 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
831 sqlite3 *db_handle = (sqlite3 *)handle;
833 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
834 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
835 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
838 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
840 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
843 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
845 int ret = MS_MEDIA_ERR_NONE;
846 sqlite3 *db_handle = (sqlite3 *)handle;
847 media_svc_media_type_e media_type;
848 int ini_val = _media_svc_get_ini_value();
850 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
851 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
852 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
853 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
855 media_svc_content_info_s content_info;
856 memset(&content_info, 0, sizeof(media_svc_content_info_s));
859 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
860 if (ret != MS_MEDIA_ERR_NONE) {
864 /* Initialize thumbnail information to remake thumbnail. */
866 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
867 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
868 if (ret != MS_MEDIA_ERR_NONE) {
869 _media_svc_destroy_content_info(&content_info);
873 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)) {
874 ret = _media_svc_remove_file(thumb_path);
875 if (ret != MS_MEDIA_ERR_NONE) {
876 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
880 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
881 if (ret != MS_MEDIA_ERR_NONE) {
882 _media_svc_destroy_content_info(&content_info);
887 /* Get notification info */
888 media_svc_noti_item *noti_item = NULL;
889 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
890 if (ret != MS_MEDIA_ERR_NONE) {
891 _media_svc_destroy_content_info(&content_info);
895 media_type = noti_item->media_type;
896 content_info.media_type = media_type;
898 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
900 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
901 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
903 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
906 if (ret != MS_MEDIA_ERR_NONE) {
907 _media_svc_destroy_noti_item(noti_item);
908 _media_svc_destroy_content_info(&content_info);
912 /* Extracting thumbnail */
914 if (content_info.thumbnail_path == NULL) {
915 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
916 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
920 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
921 if (ret == MS_MEDIA_ERR_NONE) {
922 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
925 if (content_info.media_meta.width <= 0)
926 content_info.media_meta.width = width;
928 if (content_info.media_meta.height <= 0)
929 content_info.media_meta.height = height;
936 ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
938 if (ret == MS_MEDIA_ERR_NONE) {
939 media_svc_debug("Update is successful. Sending noti for this");
940 _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);
942 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
945 _media_svc_destroy_content_info(&content_info);
946 _media_svc_destroy_noti_item(noti_item);
951 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
953 sqlite3 *db_handle = (sqlite3 *)handle;
954 int ret = MS_MEDIA_ERR_NONE;
956 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
957 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
958 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
960 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
962 ret = _media_svc_sql_begin_trans(db_handle, uid);
963 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
965 /* Update all folder record's modified date, which are changed above */
966 char *update_folder_modified_time_sql = NULL;
970 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
972 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
973 //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
974 sqlite3_free(update_folder_modified_time_sql);
976 if (ret != SQLITE_OK) {
977 media_svc_error("failed to update folder modified time");
978 _media_svc_sql_rollback_trans(db_handle, uid);
980 return MS_MEDIA_ERR_DB_INTERNAL;
983 /* Update all items */
984 char *select_all_sql = NULL;
985 sqlite3_stmt *sql_stmt = NULL;
986 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
988 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
990 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);
992 media_svc_debug("[SQL query] : %s", select_all_sql);
994 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
995 if (ret != MS_MEDIA_ERR_NONE) {
996 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
997 _media_svc_sql_rollback_trans(db_handle, uid);
1001 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1002 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1003 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1004 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1005 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1006 /*int media_type; */
1007 bool no_thumb = FALSE;
1009 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1010 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1011 media_uuid[sizeof(media_uuid) - 1] = '\0';
1013 media_svc_error("media UUID is NULL");
1014 return MS_MEDIA_ERR_DB_INTERNAL;
1017 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1018 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1019 media_path[sizeof(media_path) - 1] = '\0';
1021 media_svc_error("media path is NULL");
1022 return MS_MEDIA_ERR_DB_INTERNAL;
1025 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1026 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1027 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1029 media_svc_debug("media thumb path doesn't exist in DB");
1033 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1035 /* Update path, thumbnail path of this item */
1036 char *replaced_path = NULL;
1037 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1038 if (replaced_path == NULL) {
1039 media_svc_error("_media_svc_replace_path failed");
1040 SQLITE3_FINALIZE(sql_stmt);
1041 _media_svc_sql_rollback_trans(db_handle, uid);
1042 return MS_MEDIA_ERR_INTERNAL;
1045 media_svc_debug("New media path : %s", replaced_path);
1046 media_svc_storage_type_e storage_type;
1049 /* If old thumb path is default or not */
1050 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1051 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1052 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1054 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1055 if (ret != MS_MEDIA_ERR_NONE) {
1056 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1057 SAFE_FREE(replaced_path);
1058 SAFE_FREE(default_thumbnail_path);
1059 _media_svc_sql_rollback_trans(db_handle, uid);
1063 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1064 if (ret != MS_MEDIA_ERR_NONE) {
1065 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1066 SAFE_FREE(replaced_path);
1067 SAFE_FREE(default_thumbnail_path);
1068 SQLITE3_FINALIZE(sql_stmt);
1069 _media_svc_sql_rollback_trans(db_handle, uid);
1074 SAFE_FREE(default_thumbnail_path);
1076 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1079 char *update_item_sql = NULL;
1082 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1085 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1086 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);
1088 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1091 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);
1095 ret = media_db_request_update_db_batch(update_item_sql, uid);
1096 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1097 sqlite3_free(update_item_sql);
1098 SAFE_FREE(replaced_path);
1100 if (ret != SQLITE_OK) {
1101 media_svc_error("failed to update item");
1102 SQLITE3_FINALIZE(sql_stmt);
1103 _media_svc_sql_rollback_trans(db_handle, uid);
1105 return MS_MEDIA_ERR_DB_INTERNAL;
1108 /* Rename thumbnail file of file system */
1109 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1110 if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1111 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1112 if (ret != MS_MEDIA_ERR_NONE) {
1113 media_svc_error("_media_svc_rename_file failed : %d", ret);
1117 SAFE_FREE(default_thumbnail_path);
1120 SQLITE3_FINALIZE(sql_stmt);
1122 ret = _media_svc_sql_end_trans(db_handle, uid);
1123 if (ret != MS_MEDIA_ERR_NONE) {
1124 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1125 _media_svc_sql_rollback_trans(db_handle, uid);
1129 media_svc_debug("Folder update is successful. Sending noti for this");
1130 /* Get notification info */
1131 media_svc_noti_item *noti_item = NULL;
1132 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1133 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1135 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1136 _media_svc_destroy_noti_item(noti_item);
1138 return MS_MEDIA_ERR_NONE;
1141 int media_svc_request_update_db(const char *db_query, uid_t uid)
1143 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1145 return _media_svc_sql_query(NULL, db_query, uid);
1148 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)
1150 int ret = MS_MEDIA_ERR_NONE;
1151 sqlite3 *db_handle = (sqlite3 *)handle;
1154 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1155 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1157 /* Get notification info */
1158 media_svc_noti_item *noti_item = NULL;
1159 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1160 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1162 if (folder_id != NULL) {
1165 uuid = noti_item->media_uuid;
1168 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1169 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1170 _media_svc_destroy_noti_item(noti_item);
1175 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1177 sqlite3 *db_handle = (sqlite3 *)handle;
1179 media_svc_debug_fenter();
1181 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1182 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1183 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1184 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1186 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1189 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, uid_t uid)
1191 sqlite3 *db_handle = (sqlite3 *)handle;
1193 media_svc_debug_fenter();
1195 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1197 return _media_svc_check_db_upgrade(db_handle, need_full_scan, uid);
1200 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1202 sqlite3 *db_handle = (sqlite3 *)handle;
1204 media_svc_debug_fenter();
1206 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1208 return _media_db_check_corrupt(db_handle);
1211 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)
1213 sqlite3 *db_handle = (sqlite3 *)handle;
1215 media_svc_debug_fenter();
1217 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1218 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1220 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1223 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1225 int ret = MS_MEDIA_ERR_NONE;
1226 sqlite3 *db_handle = (sqlite3 *)handle;
1227 time_t sto_time = 0;
1228 int cur_time = _media_svc_get_file_time(folder_path);
1229 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1231 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1233 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1234 if (ret == MS_MEDIA_ERR_NONE) {
1235 if (sto_time != cur_time) {
1236 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1243 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1245 sqlite3 *db_handle = (sqlite3 *)handle;
1247 media_svc_debug("Transaction data count : [%d]", data_cnt);
1249 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1250 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1252 g_media_svc_update_item_data_cnt = data_cnt;
1253 g_media_svc_update_item_cur_data_cnt = 0;
1255 return MS_MEDIA_ERR_NONE;
1258 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1260 int ret = MS_MEDIA_ERR_NONE;
1261 sqlite3 *db_handle = (sqlite3 *)handle;
1263 media_svc_debug_fenter();
1265 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1267 if (g_media_svc_update_item_cur_data_cnt > 0) {
1269 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1272 g_media_svc_update_item_data_cnt = 1;
1273 g_media_svc_update_item_cur_data_cnt = 0;
1278 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1280 int ret = MS_MEDIA_ERR_NONE;
1281 sqlite3 *db_handle = (sqlite3 *)handle;
1283 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1285 media_svc_media_type_e media_type;
1287 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1288 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1289 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1291 media_svc_content_info_s content_info;
1292 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1295 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1296 if (ret != MS_MEDIA_ERR_NONE) {
1300 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1301 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1303 return MS_MEDIA_ERR_NONE;
1306 if (ret != MS_MEDIA_ERR_NONE) {
1307 _media_svc_destroy_content_info(&content_info);
1311 if (g_media_svc_update_item_data_cnt == 1) {
1313 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1314 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1316 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1318 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1319 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1321 g_media_svc_update_item_cur_data_cnt++;
1323 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1325 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1326 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1328 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1329 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1331 g_media_svc_update_item_cur_data_cnt = 0;
1334 media_svc_error("Error in media_svc_update_item_meta");
1335 _media_svc_destroy_content_info(&content_info);
1336 return MS_MEDIA_ERR_INTERNAL;
1339 _media_svc_destroy_content_info(&content_info);
1344 int media_svc_publish_noti(MediaSvcHandle *handle, 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)
1346 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1349 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1351 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1353 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1356 int media_svc_check_pinyin_support(bool *support)
1358 *support = _media_svc_check_pinyin_support();
1360 return MS_MEDIA_ERR_NONE;
1363 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1365 int ret = MS_MEDIA_ERR_NONE;
1366 sqlite3 * db_handle = (sqlite3 *)handle;
1368 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1370 ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1371 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1373 ret = _media_svc_update_media_view(db_handle, uid);
1374 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1379 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1381 int ret = MS_MEDIA_ERR_NONE;
1382 sqlite3 * db_handle = (sqlite3 *)handle;
1384 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1385 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1387 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1392 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
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");
1398 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1400 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1405 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1407 int ret = MS_MEDIA_ERR_NONE;
1408 sqlite3 * db_handle = (sqlite3 *)handle;
1410 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1411 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1413 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1418 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1420 int ret = MS_MEDIA_ERR_NONE;
1421 sqlite3 * db_handle = (sqlite3 *)handle;
1423 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1425 ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1430 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1432 sqlite3 * db_handle = (sqlite3 *)handle;
1434 media_svc_debug_fenter();
1436 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1437 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1439 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1442 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1444 int ret = MS_MEDIA_ERR_NONE;
1446 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1447 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1449 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1450 new_content_info->size = content_info->size;
1451 new_content_info->modified_time = content_info->modified_time;
1452 new_content_info->is_drm = content_info->is_drm;
1453 new_content_info->media_type = content_info->media_type;
1455 if (STRING_VALID(content_info->mime_type)) {
1456 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1457 if (ret != MS_MEDIA_ERR_NONE)
1458 media_svc_error("strcpy mime_type failed");
1461 if (STRING_VALID(content_info->thumbnail_path)) {
1462 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1463 if (ret != MS_MEDIA_ERR_NONE)
1464 media_svc_error("strcpy thumbnail_path failed");
1467 new_content_info->media_meta.duration = content_info->media_meta.duration;
1468 new_content_info->media_meta.width = content_info->media_meta.width;
1469 new_content_info->media_meta.height = content_info->media_meta.height;
1472 if (content_info->added_time > 0)
1473 new_content_info->added_time = content_info->added_time;
1474 new_content_info->last_played_time = content_info->last_played_time;
1475 new_content_info->played_count = content_info->played_count;
1476 new_content_info->favourate= content_info->favourate;
1478 if (STRING_VALID(content_info->file_name)) {
1479 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1480 if (ret != MS_MEDIA_ERR_NONE)
1481 media_svc_error("strcpy file_name failed");
1484 if (STRING_VALID(content_info->media_meta.title)) {
1485 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1486 if (ret != MS_MEDIA_ERR_NONE)
1487 media_svc_error("strcpy title failed");
1490 if (STRING_VALID(content_info->media_meta.album)) {
1491 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1492 if (ret != MS_MEDIA_ERR_NONE)
1493 media_svc_error("strcpy album failed");
1496 if (STRING_VALID(content_info->media_meta.artist)) {
1497 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1498 if (ret != MS_MEDIA_ERR_NONE)
1499 media_svc_error("strcpy artist failed");
1502 if (STRING_VALID(content_info->media_meta.genre)) {
1503 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1504 if (ret != MS_MEDIA_ERR_NONE)
1505 media_svc_error("strcpy genre failed");
1508 if (STRING_VALID(content_info->media_meta.composer)) {
1509 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1510 if (ret != MS_MEDIA_ERR_NONE)
1511 media_svc_error("strcpy composer failed");
1514 if (STRING_VALID(content_info->media_meta.year)) {
1515 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1516 if (ret != MS_MEDIA_ERR_NONE)
1517 media_svc_error("strcpy year failed");
1520 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1521 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1522 if (ret != MS_MEDIA_ERR_NONE)
1523 media_svc_error("strcpy recorded_date failed");
1526 if (STRING_VALID(content_info->media_meta.copyright)) {
1527 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1528 if (ret != MS_MEDIA_ERR_NONE)
1529 media_svc_error("strcpy copyright failed");
1532 if (STRING_VALID(content_info->media_meta.track_num)) {
1533 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1534 if (ret != MS_MEDIA_ERR_NONE)
1535 media_svc_error("strcpy track_num failed");
1538 if (STRING_VALID(content_info->media_meta.description)) {
1539 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1540 if (ret != MS_MEDIA_ERR_NONE)
1541 media_svc_error("strcpy description failed");
1544 if (STRING_VALID(content_info->media_meta.weather)) {
1545 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1546 if (ret != MS_MEDIA_ERR_NONE)
1547 media_svc_error("strcpy weather failed");
1550 if (STRING_VALID(content_info->media_meta.category)) {
1551 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1552 if (ret != MS_MEDIA_ERR_NONE)
1553 media_svc_error("strcpy category failed");
1556 if (STRING_VALID(content_info->media_meta.keyword)) {
1557 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1558 if (ret != MS_MEDIA_ERR_NONE)
1559 media_svc_error("strcpy keyword failed");
1562 if (STRING_VALID(content_info->media_meta.location_tag)) {
1563 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1564 if (ret != MS_MEDIA_ERR_NONE)
1565 media_svc_error("strcpy location_tag failed");
1568 if (STRING_VALID(content_info->media_meta.content_name)) {
1569 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1570 if (ret != MS_MEDIA_ERR_NONE)
1571 media_svc_error("strcpy content_name failed");
1574 if (STRING_VALID(content_info->media_meta.age_rating)) {
1575 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1576 if (ret != MS_MEDIA_ERR_NONE)
1577 media_svc_error("strcpy age_rating failed");
1580 if (STRING_VALID(content_info->media_meta.author)) {
1581 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1582 if (ret != MS_MEDIA_ERR_NONE)
1583 media_svc_error("strcpy author failed");
1586 if (STRING_VALID(content_info->media_meta.provider)) {
1587 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1588 if (ret != MS_MEDIA_ERR_NONE)
1589 media_svc_error("strcpy provider failed");
1592 if (STRING_VALID(content_info->media_meta.datetaken)) {
1593 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1594 if (ret != MS_MEDIA_ERR_NONE)
1595 media_svc_error("strcpy datetaken failed");
1597 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1598 if (new_content_info->timeline == 0) {
1599 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1600 new_content_info->timeline = new_content_info->modified_time;
1602 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1606 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1607 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1608 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1609 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1611 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1612 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1613 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1614 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1615 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1616 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1618 new_content_info->media_meta.rating = content_info->media_meta.rating;
1623 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1625 int ret = MS_MEDIA_ERR_NONE;
1626 sqlite3 *db_handle = (sqlite3 *)handle;
1627 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1628 bool append_album = FALSE;
1630 /* Checking parameters if they are valid */
1631 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1632 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1633 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1635 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1636 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1638 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1639 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1640 return MS_MEDIA_ERR_INVALID_PARAMETER;
1643 if (content_info->size <= 0) {
1644 media_svc_error("invalid size condition[%d]", content_info->size);
1645 return MS_MEDIA_ERR_INVALID_PARAMETER;
1648 if (content_info->modified_time <= 0) {
1649 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1650 return MS_MEDIA_ERR_INVALID_PARAMETER;
1654 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1656 media_svc_content_info_s _new_content_info;
1657 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1659 media_svc_media_type_e media_type;
1661 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1662 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1664 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1665 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1667 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1668 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1670 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1673 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1675 /* Extracting thumbnail */
1676 int ini_val = _media_svc_get_ini_value();
1678 if (_new_content_info.thumbnail_path == NULL) {
1679 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1680 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1684 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1685 if (ret == MS_MEDIA_ERR_NONE) {
1686 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1689 if (_new_content_info.media_meta.width <= 0)
1690 _new_content_info.media_meta.width = width;
1692 if (_new_content_info.media_meta.height <= 0)
1693 _new_content_info.media_meta.height = height;
1699 /* set othere data to the structure, which is passed as parameters */
1700 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1702 /* Set or Get folder id */
1703 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);
1704 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1706 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1707 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1709 /* register album table data */
1712 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1713 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1715 if (ret != MS_MEDIA_ERR_NONE) {
1716 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1717 media_svc_debug("album does not exist. So start to make album art");
1718 append_album = TRUE;
1720 media_svc_debug("other error");
1721 append_album = FALSE;
1724 _new_content_info.album_id = album_id;
1725 append_album = FALSE;
1727 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1728 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1730 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1733 media_svc_debug("album already exists. don't need to make album art");
1734 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1735 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1740 if (append_album == TRUE) {
1742 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1743 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1744 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);
1746 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1748 _new_content_info.album_id = album_id;
1751 /* Insert to db - calling _media_svc_insert_item_with_data */
1752 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1754 if (ret == MS_MEDIA_ERR_NONE) {
1755 media_svc_debug("Insertion is successful.");
1758 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1759 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1762 _media_svc_destroy_content_info(&_new_content_info);
1764 /* handling returned value - important */
1768 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1770 _media_svc_destroy_content_info(content_info);
1773 int media_svc_generate_uuid(char **uuid)
1775 char *gen_uuid = NULL;
1776 gen_uuid = _media_info_generate_uuid();
1777 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1779 *uuid = strdup(gen_uuid);
1781 return MS_MEDIA_ERR_NONE;
1784 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1786 sqlite3 * db_handle = (sqlite3 *)handle;
1788 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1790 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1793 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1795 sqlite3 * db_handle = (sqlite3 *)handle;
1797 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1798 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1799 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1800 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1802 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1805 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1807 sqlite3 * db_handle = (sqlite3 *)handle;
1809 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1810 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1811 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1813 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1816 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)
1818 int ret = MS_MEDIA_ERR_NONE;
1819 sqlite3 *db_handle = (sqlite3 *)handle;
1821 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1822 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1823 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1824 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1826 ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1827 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1829 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1830 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1831 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1833 ret = _media_svc_update_media_view(db_handle, uid);
1834 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1840 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1842 int ret = MS_MEDIA_ERR_NONE;
1843 sqlite3 *db_handle = (sqlite3 *)handle;
1844 media_svc_storage_type_e storage_type;
1846 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1847 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1849 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1850 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1852 ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1853 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1855 ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1856 if (ret != MS_MEDIA_ERR_NONE) {
1857 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1860 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1861 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1862 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1864 ret = _media_svc_update_media_view(db_handle, uid);
1865 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1871 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1873 sqlite3 * db_handle = (sqlite3 *)handle;
1875 media_svc_debug("Transaction data count : [%d]", data_cnt);
1877 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1878 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1880 g_media_svc_insert_folder_data_cnt = data_cnt;
1881 g_media_svc_insert_folder_cur_data_cnt = 0;
1883 return MS_MEDIA_ERR_NONE;
1886 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1888 int ret = MS_MEDIA_ERR_NONE;
1889 sqlite3 * db_handle = (sqlite3 *)handle;
1891 media_svc_debug_fenter();
1893 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1895 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1897 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1900 g_media_svc_insert_folder_data_cnt = 1;
1901 g_media_svc_insert_folder_cur_data_cnt = 0;
1906 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1908 int ret = MS_MEDIA_ERR_NONE;
1909 sqlite3 * db_handle = (sqlite3 *)handle;
1910 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1912 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1913 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1914 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1915 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1917 if (g_media_svc_insert_folder_data_cnt == 1) {
1919 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1921 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1923 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1924 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1926 g_media_svc_insert_folder_cur_data_cnt ++;
1928 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1930 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1931 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1933 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1934 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1936 g_media_svc_insert_folder_cur_data_cnt = 0;
1939 media_svc_error("Error in media_svc_set_insert_folder");
1940 return MS_MEDIA_ERR_INTERNAL;
1946 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1948 int ret = MS_MEDIA_ERR_NONE;
1949 sqlite3 * db_handle = (sqlite3 *)handle;
1951 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1953 ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
1958 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1960 int ret = MS_MEDIA_ERR_NONE;
1961 sqlite3 * db_handle = (sqlite3 *)handle;
1963 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1965 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1970 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)
1972 int ret = MS_MEDIA_ERR_NONE;
1973 sqlite3 * db_handle = (sqlite3 *)handle;
1974 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1975 media_svc_media_type_e media_type;
1977 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1978 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1979 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1980 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1982 media_svc_content_info_s content_info;
1983 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1985 /*Set basic media info*/
1986 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1987 if (ret != MS_MEDIA_ERR_NONE) {
1988 media_svc_error("_media_svc_set_media_info fail %d", ret);
1992 //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);
1994 /*Set or Get folder id*/
1995 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1996 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1998 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1999 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2001 if (g_media_svc_insert_item_data_cnt == 1) {
2003 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2004 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2006 if (g_insert_with_noti)
2007 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2008 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2010 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2011 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2013 if (g_insert_with_noti)
2014 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2016 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2017 g_media_svc_insert_item_cur_data_cnt ++;
2019 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2021 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, 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);
2027 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2028 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2030 media_svc_debug("_media_svc_list_query_do over");
2032 if (g_insert_with_noti) {
2033 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2034 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2035 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2037 /* Recreate noti list */
2038 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2039 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2042 g_media_svc_insert_item_cur_data_cnt = 0;
2045 media_svc_error("Error in media_svc_insert_item_pass1");
2046 _media_svc_destroy_content_info(&content_info);
2047 return MS_MEDIA_ERR_INTERNAL;
2050 _media_svc_destroy_content_info(&content_info);
2052 return MS_MEDIA_ERR_NONE;
2055 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)
2057 int ret = MS_MEDIA_ERR_NONE;
2058 sqlite3 * db_handle = (sqlite3 *)handle;
2059 sqlite3_stmt *sql_stmt = NULL;
2060 media_svc_media_type_e media_type;
2061 media_svc_content_info_s content_info;
2062 GArray *db_data_array = NULL;
2063 media_svc_item_info_s *db_data = NULL;
2067 media_svc_debug_fenter();
2069 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2070 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2071 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2073 if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2074 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2075 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2076 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2077 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2078 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2080 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2081 } else { /*Storage Scanning*/
2082 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2085 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2086 if (ret != MS_MEDIA_ERR_NONE) {
2087 media_svc_error("error when get list. err = [%d]", ret);
2091 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2092 if (db_data_array == NULL) {
2093 media_svc_error("db_data_array is NULL. Out of memory");
2094 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2097 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2099 db_data = malloc(sizeof(media_svc_item_info_s));
2100 if (db_data == NULL) {
2101 media_svc_error("Out of memory");
2105 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2106 db_data->media_type = (int)sqlite3_column_int(sql_stmt,1);
2108 g_array_append_val(db_data_array, db_data);
2111 SQLITE3_FINALIZE(sql_stmt);
2113 while (db_data_array->len != 0) {
2115 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2116 g_array_remove_index (db_data_array, 0);
2118 if ((db_data == NULL) ||(db_data->path == NULL)) {
2119 media_svc_error("invalid db data");
2123 media_type = db_data->media_type;
2124 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2125 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2126 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2128 content_info.media_type = media_type;
2129 content_info.storage_type = storage_type;
2131 _media_svc_set_default_value(&content_info, FALSE);
2133 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2135 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2136 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2138 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2141 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2143 _media_svc_destroy_content_info(&content_info);
2144 SAFE_FREE(db_data->path);
2150 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2152 g_array_free(db_data_array, FALSE);
2153 db_data_array = NULL;
2155 media_svc_debug_fleave();
2157 return MS_MEDIA_ERR_NONE;
2160 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2162 int ret = MS_MEDIA_ERR_NONE;
2163 sqlite3 * db_handle = (sqlite3 *)handle;
2165 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2167 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2172 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2174 int ret = MS_MEDIA_ERR_NONE;
2175 sqlite3 * db_handle = (sqlite3 *)handle;
2178 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2179 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2180 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2182 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2183 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2186 media_svc_debug("item is exist in database");
2187 return MS_MEDIA_ERR_NONE;
2189 media_svc_debug("item is not exist in database");
2190 return MS_MEDIA_ERR_DB_NO_RECORD;
2193 return MS_MEDIA_ERR_NONE;
2196 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2198 int ret = MS_MEDIA_ERR_NONE;
2199 sqlite3 * db_handle = (sqlite3 *)handle;
2202 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2203 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2204 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2207 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2208 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2212 media_svc_debug("item is exist in database");
2213 return MS_MEDIA_ERR_NONE;
2215 media_svc_debug("item is not exist in database");
2216 return MS_MEDIA_ERR_DB_NO_RECORD;
2219 return MS_MEDIA_ERR_NONE;
2222 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2224 int ret = MS_MEDIA_ERR_NONE;
2225 sqlite3 * db_handle = (sqlite3 *)handle;
2227 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2228 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2230 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);