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 if (strncmp(old_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
494 strncpy(new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(new_thumb_path));
496 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
500 if (g_media_svc_move_item_data_cnt == 1) {
503 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
504 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);
506 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);
508 SAFE_FREE(file_name);
509 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
511 media_svc_debug("Move is successful. Sending noti for this");
513 /* Get notification info */
514 media_svc_noti_item *noti_item = NULL;
515 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
516 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
518 /* Send notification for move */
519 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
520 _media_svc_destroy_noti_item(noti_item);
522 /*update folder modified_time*/
523 folder_path = g_path_get_dirname(dest_path);
524 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
525 SAFE_FREE(folder_path);
526 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
528 ret = _media_svc_update_folder_table(db_handle, storage_id, uid);
529 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
531 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
534 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
535 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);
537 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);
539 SAFE_FREE(file_name);
540 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
542 /*update folder modified_time*/
543 folder_path = g_path_get_dirname(dest_path);
544 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
545 SAFE_FREE(folder_path);
546 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
548 g_media_svc_move_item_cur_data_cnt++;
550 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
553 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
554 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);
556 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);
558 SAFE_FREE(file_name);
559 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
561 /*update folder modified_time*/
562 folder_path = g_path_get_dirname(dest_path);
563 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
564 SAFE_FREE(folder_path);
565 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
568 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
569 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
571 g_media_svc_move_item_cur_data_cnt = 0;
574 media_svc_error("Error in media_svc_move_item");
575 return MS_MEDIA_ERR_INTERNAL;
578 /*rename thumbnail file*/
579 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
580 if ((strlen(old_thumb_path) > 0) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
581 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
582 if (ret != MS_MEDIA_ERR_NONE)
583 media_svc_error("_media_svc_rename_file failed : %d", ret);
587 return MS_MEDIA_ERR_NONE;
590 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
592 sqlite3 *db_handle = (sqlite3 *)handle;
594 media_svc_debug("Transaction data count : [%d]", data_cnt);
596 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
597 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
599 g_media_svc_item_validity_data_cnt = data_cnt;
600 g_media_svc_item_validity_cur_data_cnt = 0;
602 return MS_MEDIA_ERR_NONE;
605 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
607 int ret = MS_MEDIA_ERR_NONE;
608 sqlite3 *db_handle = (sqlite3 *)handle;
610 media_svc_debug_fenter();
612 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
614 if (g_media_svc_item_validity_cur_data_cnt > 0) {
616 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
619 g_media_svc_item_validity_data_cnt = 1;
620 g_media_svc_item_validity_cur_data_cnt = 0;
625 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *storage_id, const char *path, int validity, uid_t uid)
627 int ret = MS_MEDIA_ERR_NONE;
628 sqlite3 *db_handle = (sqlite3 *)handle;
630 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
631 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
632 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
634 media_svc_debug("path=[%s], validity=[%d]", path, validity);
636 if (g_media_svc_item_validity_data_cnt == 1) {
638 return _media_svc_update_item_validity(db_handle, storage_id, path, validity, FALSE, uid);
640 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
642 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
643 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
645 g_media_svc_item_validity_cur_data_cnt++;
647 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
649 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
650 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
652 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
653 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
655 g_media_svc_item_validity_cur_data_cnt = 0;
659 media_svc_error("Error in media_svc_set_item_validity");
660 return MS_MEDIA_ERR_INTERNAL;
663 return MS_MEDIA_ERR_NONE;
666 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
668 int ret = MS_MEDIA_ERR_NONE;
669 sqlite3 *db_handle = (sqlite3 *)handle;
670 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
672 media_svc_debug_fenter();
674 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
675 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
676 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
679 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
680 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
683 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
684 /*Get thumbnail path to delete*/
685 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
686 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
687 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
689 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
690 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
693 /*Get thumbnail path to delete*/
694 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
695 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
699 /*Get thumbnail path to delete*/
700 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
701 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
703 if (g_media_svc_insert_item_data_cnt == 1) {
705 /* Get notification info */
706 media_svc_noti_item *noti_item = NULL;
707 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
708 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
711 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
712 if (ret != MS_MEDIA_ERR_NONE) {
713 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
714 _media_svc_destroy_noti_item(noti_item);
719 /* Send notification */
720 media_svc_debug("Deletion is successful. Sending noti for this");
721 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
722 _media_svc_destroy_noti_item(noti_item);
724 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
725 if (ret != MS_MEDIA_ERR_NONE) {
726 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
733 if ((strlen(thumb_path) > 0) && (strncmp(thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
736 // Get count of media, which contains same thumbnail for music
737 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
738 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
739 if (ret != MS_MEDIA_ERR_NONE) {
740 media_svc_error("Failed to get thumbnail count : %d", ret);
744 if (thumb_count == 1) {
746 ret = _media_svc_remove_file(thumb_path);
747 if(ret != MS_MEDIA_ERR_NONE) {
748 media_svc_error("fail to remove thumbnail file.");
753 return MS_MEDIA_ERR_NONE;
756 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
758 int ret = MS_MEDIA_ERR_NONE;
759 sqlite3 *db_handle = (sqlite3 *)handle;
761 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
763 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
764 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
765 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
767 ret = _media_svc_truncate_table(db_handle, storage_id, storage_type, uid);
768 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
770 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
771 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? MEDIA_SVC_THUMB_INTERNAL_PATH : MEDIA_SVC_THUMB_EXTERNAL_PATH;
773 /* remove thumbnails */
774 ret = _media_svc_remove_all_files_in_dir(dirpath);
775 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
778 return MS_MEDIA_ERR_NONE;
781 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
783 sqlite3 *db_handle = (sqlite3 *)handle;
785 media_svc_debug_fenter();
787 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
788 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
789 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
791 /*Delete from DB and remove thumbnail files*/
792 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
795 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
797 sqlite3 *db_handle = (sqlite3 *)handle;
799 media_svc_debug_fenter();
801 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
802 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
804 /*Delete from DB and remove thumbnail files*/
805 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
808 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)
810 sqlite3 *db_handle = (sqlite3 *)handle;
812 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
813 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
814 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
816 return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
819 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
821 sqlite3 *db_handle = (sqlite3 *)handle;
823 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
824 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
825 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
828 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
830 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
833 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
835 int ret = MS_MEDIA_ERR_NONE;
836 sqlite3 *db_handle = (sqlite3 *)handle;
837 media_svc_media_type_e media_type;
838 int ini_val = _media_svc_get_ini_value();
840 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
841 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
842 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
843 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
845 media_svc_content_info_s content_info;
846 memset(&content_info, 0, sizeof(media_svc_content_info_s));
849 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
850 if (ret != MS_MEDIA_ERR_NONE) {
854 /* Initialize thumbnail information to remake thumbnail. */
856 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
857 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
858 if (ret != MS_MEDIA_ERR_NONE) {
859 _media_svc_destroy_content_info(&content_info);
863 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
864 ret = _media_svc_remove_file(thumb_path);
865 if (ret != MS_MEDIA_ERR_NONE) {
866 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
870 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
871 if (ret != MS_MEDIA_ERR_NONE) {
872 _media_svc_destroy_content_info(&content_info);
877 /* Get notification info */
878 media_svc_noti_item *noti_item = NULL;
879 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
880 if (ret != MS_MEDIA_ERR_NONE) {
881 _media_svc_destroy_content_info(&content_info);
885 media_type = noti_item->media_type;
886 content_info.media_type = media_type;
888 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
890 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
891 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
893 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
896 if (ret != MS_MEDIA_ERR_NONE) {
897 _media_svc_destroy_noti_item(noti_item);
898 _media_svc_destroy_content_info(&content_info);
902 /* Extracting thumbnail */
904 if (content_info.thumbnail_path == NULL) {
905 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
906 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
910 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
911 if (ret == MS_MEDIA_ERR_NONE) {
912 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
915 if (content_info.media_meta.width <= 0)
916 content_info.media_meta.width = width;
918 if (content_info.media_meta.height <= 0)
919 content_info.media_meta.height = height;
926 ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
928 if (ret == MS_MEDIA_ERR_NONE) {
929 media_svc_debug("Update is successful. Sending noti for this");
930 _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);
932 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
935 _media_svc_destroy_content_info(&content_info);
936 _media_svc_destroy_noti_item(noti_item);
941 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
943 sqlite3 *db_handle = (sqlite3 *)handle;
944 int ret = MS_MEDIA_ERR_NONE;
946 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
947 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
948 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
950 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
952 ret = _media_svc_sql_begin_trans(db_handle, uid);
953 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
955 /* Update all folder record's modified date, which are changed above */
956 char *update_folder_modified_time_sql = NULL;
960 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
962 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
963 //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
964 sqlite3_free(update_folder_modified_time_sql);
966 if (ret != SQLITE_OK) {
967 media_svc_error("failed to update folder modified time");
968 _media_svc_sql_rollback_trans(db_handle, uid);
970 return MS_MEDIA_ERR_DB_INTERNAL;
973 /* Update all items */
974 char *select_all_sql = NULL;
975 sqlite3_stmt *sql_stmt = NULL;
976 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
978 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
980 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);
982 media_svc_debug("[SQL query] : %s", select_all_sql);
984 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
985 if (ret != MS_MEDIA_ERR_NONE) {
986 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
987 _media_svc_sql_rollback_trans(db_handle, uid);
991 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
992 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
993 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
994 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
995 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
997 bool no_thumb = FALSE;
999 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1000 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1001 media_uuid[sizeof(media_uuid) - 1] = '\0';
1003 media_svc_error("media UUID is NULL");
1004 return MS_MEDIA_ERR_DB_INTERNAL;
1007 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1008 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1009 media_path[sizeof(media_path) - 1] = '\0';
1011 media_svc_error("media path is NULL");
1012 return MS_MEDIA_ERR_DB_INTERNAL;
1015 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1016 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1017 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1019 media_svc_debug("media thumb path doesn't exist in DB");
1023 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1025 /* Update path, thumbnail path of this item */
1026 char *replaced_path = NULL;
1027 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1028 if (replaced_path == NULL) {
1029 media_svc_error("_media_svc_replace_path failed");
1030 SQLITE3_FINALIZE(sql_stmt);
1031 _media_svc_sql_rollback_trans(db_handle, uid);
1032 return MS_MEDIA_ERR_INTERNAL;
1035 media_svc_debug("New media path : %s", replaced_path);
1036 media_svc_storage_type_e storage_type;
1039 /* If old thumb path is default or not */
1040 if (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
1041 strncpy(media_new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(media_new_thumb_path));
1043 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1044 if (ret != MS_MEDIA_ERR_NONE) {
1045 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1046 SAFE_FREE(replaced_path);
1047 _media_svc_sql_rollback_trans(db_handle, uid);
1051 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1052 if (ret != MS_MEDIA_ERR_NONE) {
1053 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1054 SAFE_FREE(replaced_path);
1055 SQLITE3_FINALIZE(sql_stmt);
1056 _media_svc_sql_rollback_trans(db_handle, uid);
1061 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1064 char *update_item_sql = NULL;
1067 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1070 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1071 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);
1073 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1076 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);
1080 ret = media_db_request_update_db_batch(update_item_sql, uid);
1081 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1082 sqlite3_free(update_item_sql);
1083 SAFE_FREE(replaced_path);
1085 if (ret != SQLITE_OK) {
1086 media_svc_error("failed to update item");
1087 SQLITE3_FINALIZE(sql_stmt);
1088 _media_svc_sql_rollback_trans(db_handle, uid);
1090 return MS_MEDIA_ERR_DB_INTERNAL;
1093 /* Rename thumbnail file of file system */
1094 /* if ((!no_thumb) && (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) */
1095 /* && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) { */
1096 if ((!no_thumb) && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
1097 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1098 if (ret != MS_MEDIA_ERR_NONE) {
1099 media_svc_error("_media_svc_rename_file failed : %d", ret);
1104 SQLITE3_FINALIZE(sql_stmt);
1106 ret = _media_svc_sql_end_trans(db_handle, uid);
1107 if (ret != MS_MEDIA_ERR_NONE) {
1108 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1109 _media_svc_sql_rollback_trans(db_handle, uid);
1113 media_svc_debug("Folder update is successful. Sending noti for this");
1114 /* Get notification info */
1115 media_svc_noti_item *noti_item = NULL;
1116 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1117 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1119 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1120 _media_svc_destroy_noti_item(noti_item);
1122 return MS_MEDIA_ERR_NONE;
1125 int media_svc_request_update_db(const char *db_query, uid_t uid)
1127 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1129 return _media_svc_sql_query(NULL, db_query, uid);
1132 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)
1134 int ret = MS_MEDIA_ERR_NONE;
1135 sqlite3 *db_handle = (sqlite3 *)handle;
1138 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1139 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1141 /* Get notification info */
1142 media_svc_noti_item *noti_item = NULL;
1143 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1144 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1146 if (folder_id != NULL) {
1149 uuid = noti_item->media_uuid;
1152 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1153 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1154 _media_svc_destroy_noti_item(noti_item);
1159 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1161 sqlite3 *db_handle = (sqlite3 *)handle;
1163 media_svc_debug_fenter();
1165 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1166 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1167 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1168 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1170 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1173 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, uid_t uid)
1175 sqlite3 *db_handle = (sqlite3 *)handle;
1177 media_svc_debug_fenter();
1179 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1181 return _media_svc_check_db_upgrade(db_handle, need_full_scan, uid);
1184 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1186 sqlite3 *db_handle = (sqlite3 *)handle;
1188 media_svc_debug_fenter();
1190 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1192 return _media_db_check_corrupt(db_handle);
1195 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)
1197 sqlite3 *db_handle = (sqlite3 *)handle;
1199 media_svc_debug_fenter();
1201 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1202 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1204 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1207 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1209 int ret = MS_MEDIA_ERR_NONE;
1210 sqlite3 *db_handle = (sqlite3 *)handle;
1211 time_t sto_time = 0;
1212 int cur_time = _media_svc_get_file_time(folder_path);
1213 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1215 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1217 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1218 if (ret == MS_MEDIA_ERR_NONE) {
1219 if (sto_time != cur_time) {
1220 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1227 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1229 sqlite3 *db_handle = (sqlite3 *)handle;
1231 media_svc_debug("Transaction data count : [%d]", data_cnt);
1233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1234 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1236 g_media_svc_update_item_data_cnt = data_cnt;
1237 g_media_svc_update_item_cur_data_cnt = 0;
1239 return MS_MEDIA_ERR_NONE;
1242 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1244 int ret = MS_MEDIA_ERR_NONE;
1245 sqlite3 *db_handle = (sqlite3 *)handle;
1247 media_svc_debug_fenter();
1249 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1251 if (g_media_svc_update_item_cur_data_cnt > 0) {
1253 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1256 g_media_svc_update_item_data_cnt = 1;
1257 g_media_svc_update_item_cur_data_cnt = 0;
1262 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1264 int ret = MS_MEDIA_ERR_NONE;
1265 sqlite3 *db_handle = (sqlite3 *)handle;
1267 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1269 media_svc_media_type_e media_type;
1271 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1272 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1273 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1275 media_svc_content_info_s content_info;
1276 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1279 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1280 if (ret != MS_MEDIA_ERR_NONE) {
1284 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1285 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1287 return MS_MEDIA_ERR_NONE;
1290 if (ret != MS_MEDIA_ERR_NONE) {
1291 _media_svc_destroy_content_info(&content_info);
1295 if (g_media_svc_update_item_data_cnt == 1) {
1297 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1298 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1300 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1302 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1303 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1305 g_media_svc_update_item_cur_data_cnt++;
1307 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1309 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1310 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1312 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1313 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1315 g_media_svc_update_item_cur_data_cnt = 0;
1318 media_svc_error("Error in media_svc_update_item_meta");
1319 _media_svc_destroy_content_info(&content_info);
1320 return MS_MEDIA_ERR_INTERNAL;
1323 _media_svc_destroy_content_info(&content_info);
1328 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)
1330 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1333 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1335 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1337 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1340 int media_svc_check_pinyin_support(bool *support)
1342 *support = _media_svc_check_pinyin_support();
1344 return MS_MEDIA_ERR_NONE;
1347 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1349 int ret = MS_MEDIA_ERR_NONE;
1350 sqlite3 * db_handle = (sqlite3 *)handle;
1352 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1354 ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1355 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1357 ret = _media_svc_update_media_view(db_handle, uid);
1358 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1363 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
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");
1369 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1371 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1376 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1378 int ret = MS_MEDIA_ERR_NONE;
1379 sqlite3 * db_handle = (sqlite3 *)handle;
1381 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1382 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1384 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1389 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1391 int ret = MS_MEDIA_ERR_NONE;
1392 sqlite3 * db_handle = (sqlite3 *)handle;
1394 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1395 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1397 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1402 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1404 int ret = MS_MEDIA_ERR_NONE;
1405 sqlite3 * db_handle = (sqlite3 *)handle;
1407 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1409 ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1414 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1416 sqlite3 * db_handle = (sqlite3 *)handle;
1418 media_svc_debug_fenter();
1420 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1421 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1423 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1427 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1429 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1430 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1433 initialize media content datas
1435 int ret = MS_MEDIA_ERR_NONE;
1436 char *media_uuid = NULL;
1437 char *file_name = NULL;
1439 ret = __media_svc_malloc_and_strncpy(&new_content_info->path, content_info->path);
1440 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1442 ret = __media_svc_malloc_and_strncpy(&new_content_info->storage_uuid, content_info->storage_uuid);
1443 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1445 /* Set default GPS value before extracting meta information */
1446 new_content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1447 new_content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1448 new_content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1450 /* Set default value before extracting meta information */
1451 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1452 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1454 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1455 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1457 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1458 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1460 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1461 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1463 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1464 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1466 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1467 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1469 /* ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN); */
1470 /* media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info); */
1472 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1473 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1475 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1476 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1478 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1479 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1481 new_content_info->storage_type = content_info->storage_type;
1482 time(&new_content_info->added_time);
1484 media_uuid = _media_info_generate_uuid();
1485 media_svc_retvm_if(media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Invalid UUID");
1487 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_uuid, media_uuid);
1488 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1490 file_name = g_path_get_basename(content_info->path);
1491 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, file_name);
1492 SAFE_FREE(file_name);
1493 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1495 new_content_info->played_count = 0;
1496 new_content_info->last_played_time = 0;
1497 new_content_info->last_played_position = 0;
1498 new_content_info->favourate = 0;
1499 new_content_info->media_meta.rating = 0;
1504 new_content_info->size = content_info->size;
1505 new_content_info->added_time = content_info->added_time;
1506 new_content_info->modified_time = content_info->modified_time;
1507 new_content_info->is_drm = content_info->is_drm;
1508 new_content_info->media_type = content_info->media_type;
1510 if (STRING_VALID(content_info->mime_type)) {
1511 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1512 if (ret != MS_MEDIA_ERR_NONE)
1513 media_svc_error("strcpy mime_type failed");
1516 if (STRING_VALID(content_info->thumbnail_path)) {
1517 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1518 if (ret != MS_MEDIA_ERR_NONE)
1519 media_svc_error("strcpy thumbnail_path failed");
1522 if (STRING_VALID(content_info->media_meta.title)) {
1523 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1524 if (ret != MS_MEDIA_ERR_NONE)
1525 media_svc_error("strcpy title failed");
1528 if (STRING_VALID(content_info->media_meta.album)) {
1529 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1530 if (ret != MS_MEDIA_ERR_NONE)
1531 media_svc_error("strcpy album failed");
1534 if (STRING_VALID(content_info->media_meta.artist)) {
1535 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1536 if (ret != MS_MEDIA_ERR_NONE)
1537 media_svc_error("strcpy artist failed");
1541 if(STRING_VALID(content_info->media_meta.album_artist))
1543 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album_artist, content_info->media_meta.album_artist);
1544 if(ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy album_artist failed");
1549 if (STRING_VALID(content_info->media_meta.genre)) {
1550 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1551 if (ret != MS_MEDIA_ERR_NONE)
1552 media_svc_error("strcpy genre failed");
1555 if (STRING_VALID(content_info->media_meta.composer)) {
1556 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1557 if (ret != MS_MEDIA_ERR_NONE)
1558 media_svc_error("strcpy composer failed");
1561 if (STRING_VALID(content_info->media_meta.year)) {
1562 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1563 if (ret != MS_MEDIA_ERR_NONE)
1564 media_svc_error("strcpy year failed");
1567 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1568 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1569 if (ret != MS_MEDIA_ERR_NONE)
1570 media_svc_error("strcpy recorded_date failed");
1573 if (STRING_VALID(content_info->media_meta.copyright)) {
1574 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1575 if (ret != MS_MEDIA_ERR_NONE)
1576 media_svc_error("strcpy copyright failed");
1579 if (STRING_VALID(content_info->media_meta.track_num)) {
1580 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1581 if (ret != MS_MEDIA_ERR_NONE)
1582 media_svc_error("strcpy track_num failed");
1585 if (STRING_VALID(content_info->media_meta.description)) {
1586 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1587 if (ret != MS_MEDIA_ERR_NONE)
1588 media_svc_error("strcpy description failed");
1591 if (STRING_VALID(content_info->media_meta.weather)) {
1592 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1593 if (ret != MS_MEDIA_ERR_NONE)
1594 media_svc_error("strcpy description failed");
1597 if (STRING_VALID(content_info->media_meta.datetaken)) {
1598 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1599 if (ret != MS_MEDIA_ERR_NONE)
1600 media_svc_error("strcpy datetaken failed");
1602 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1603 if (new_content_info->timeline == 0) {
1604 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1605 new_content_info->timeline = new_content_info->modified_time;
1607 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1611 new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1612 new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1613 new_content_info->media_meta.channel = content_info->media_meta.channel;
1614 new_content_info->media_meta.duration = content_info->media_meta.duration;
1615 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1616 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1617 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1618 new_content_info->media_meta.width = content_info->media_meta.width;
1619 new_content_info->media_meta.height = content_info->media_meta.height;
1620 new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1621 new_content_info->media_meta.rating = content_info->media_meta.rating;
1626 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1628 int ret = MS_MEDIA_ERR_NONE;
1629 sqlite3 *db_handle = (sqlite3 *)handle;
1630 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1631 bool append_album = FALSE;
1633 /* Checking parameters if they are valid */
1634 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1635 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1636 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1637 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1639 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1640 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1641 return MS_MEDIA_ERR_INVALID_PARAMETER;
1644 if (content_info->size <= 0) {
1645 media_svc_error("invalid size condition[%d]", content_info->size);
1646 return MS_MEDIA_ERR_INVALID_PARAMETER;
1649 if (content_info->modified_time <= 0) {
1650 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1651 return MS_MEDIA_ERR_INVALID_PARAMETER;
1653 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1655 media_svc_content_info_s _new_content_info;
1656 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1658 /* set othere data to the structure, which is passed as parameters */
1659 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1661 /* Set or Get folder id */
1662 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);
1663 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1665 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1666 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1668 /* register album table data */
1671 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1672 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1674 if (ret != MS_MEDIA_ERR_NONE) {
1675 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1676 media_svc_debug("album does not exist. So start to make album art");
1677 append_album = TRUE;
1679 media_svc_debug("other error");
1680 append_album = FALSE;
1683 _new_content_info.album_id = album_id;
1684 append_album = FALSE;
1686 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1687 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1689 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1692 media_svc_debug("album already exists. don't need to make album art");
1693 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1694 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1699 if (append_album == TRUE) {
1701 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1702 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1703 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);
1705 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1707 _new_content_info.album_id = album_id;
1710 /* Insert to db - calling _media_svc_insert_item_with_data */
1711 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1713 if (ret == MS_MEDIA_ERR_NONE) {
1714 media_svc_debug("Insertion is successful.");
1717 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1718 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1721 _media_svc_destroy_content_info(&_new_content_info);
1723 /* handling returned value - important */
1727 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1729 _media_svc_destroy_content_info(content_info);
1732 int media_svc_generate_uuid(char **uuid)
1734 char *gen_uuid = NULL;
1735 gen_uuid = _media_info_generate_uuid();
1736 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1738 *uuid = strdup(gen_uuid);
1740 return MS_MEDIA_ERR_NONE;
1743 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1745 sqlite3 * db_handle = (sqlite3 *)handle;
1747 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1749 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1752 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1754 sqlite3 * db_handle = (sqlite3 *)handle;
1756 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1757 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1758 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1759 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1761 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1764 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1766 sqlite3 * db_handle = (sqlite3 *)handle;
1768 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1769 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1770 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1772 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1775 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)
1777 int ret = MS_MEDIA_ERR_NONE;
1778 sqlite3 *db_handle = (sqlite3 *)handle;
1780 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1781 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1782 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1783 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1785 ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1786 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1788 if(strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1789 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1790 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1792 ret = _media_svc_update_media_view(db_handle, uid);
1793 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1799 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1801 int ret = MS_MEDIA_ERR_NONE;
1802 sqlite3 *db_handle = (sqlite3 *)handle;
1803 media_svc_storage_type_e storage_type;
1805 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1806 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1808 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1809 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1811 ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1812 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1814 ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1815 if (ret != MS_MEDIA_ERR_NONE) {
1816 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1819 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1820 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1821 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1823 ret = _media_svc_update_media_view(db_handle, uid);
1824 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1830 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1832 sqlite3 * db_handle = (sqlite3 *)handle;
1834 media_svc_debug("Transaction data count : [%d]", data_cnt);
1836 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1837 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1839 g_media_svc_insert_folder_data_cnt = data_cnt;
1840 g_media_svc_insert_folder_cur_data_cnt = 0;
1842 return MS_MEDIA_ERR_NONE;
1845 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1847 int ret = MS_MEDIA_ERR_NONE;
1848 sqlite3 * db_handle = (sqlite3 *)handle;
1850 media_svc_debug_fenter();
1852 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1854 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1856 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1859 g_media_svc_insert_folder_data_cnt = 1;
1860 g_media_svc_insert_folder_cur_data_cnt = 0;
1865 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1867 int ret = MS_MEDIA_ERR_NONE;
1868 sqlite3 * db_handle = (sqlite3 *)handle;
1869 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1871 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1872 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1873 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1874 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1876 if (g_media_svc_insert_folder_data_cnt == 1) {
1878 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1880 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1882 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1883 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1885 g_media_svc_insert_folder_cur_data_cnt ++;
1887 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1889 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1890 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1892 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1893 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1895 g_media_svc_insert_folder_cur_data_cnt = 0;
1898 media_svc_error("Error in media_svc_set_insert_folder");
1899 return MS_MEDIA_ERR_INTERNAL;
1905 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1907 int ret = MS_MEDIA_ERR_NONE;
1908 sqlite3 * db_handle = (sqlite3 *)handle;
1910 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1912 ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
1917 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1919 int ret = MS_MEDIA_ERR_NONE;
1920 sqlite3 * db_handle = (sqlite3 *)handle;
1922 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1924 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1929 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)
1931 int ret = MS_MEDIA_ERR_NONE;
1932 sqlite3 * db_handle = (sqlite3 *)handle;
1933 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1934 media_svc_media_type_e media_type;
1936 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1937 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1938 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1939 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1941 media_svc_content_info_s content_info;
1942 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1944 /*Set basic media info*/
1945 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1946 if (ret != MS_MEDIA_ERR_NONE) {
1947 media_svc_error("_media_svc_set_media_info fail %d", ret);
1951 //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);
1953 /*Set or Get folder id*/
1954 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1955 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1957 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1958 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1960 if (g_media_svc_insert_item_data_cnt == 1) {
1962 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1963 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1965 if (g_insert_with_noti)
1967 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1969 } else if(g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1971 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1972 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1974 if (g_insert_with_noti)
1976 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1978 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1979 g_media_svc_insert_item_cur_data_cnt ++;
1981 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1983 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1984 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1986 if (g_insert_with_noti)
1987 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1989 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1990 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1992 media_svc_debug("_media_svc_list_query_do over");
1994 if (g_insert_with_noti) {
1995 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1996 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1997 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1999 /* Recreate noti list */
2000 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2001 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2004 g_media_svc_insert_item_cur_data_cnt = 0;
2007 media_svc_error("Error in media_svc_insert_item_pass1");
2008 _media_svc_destroy_content_info(&content_info);
2009 return MS_MEDIA_ERR_INTERNAL;
2012 _media_svc_destroy_content_info(&content_info);
2014 return MS_MEDIA_ERR_NONE;
2017 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)
2019 int ret = MS_MEDIA_ERR_NONE;
2020 sqlite3 * db_handle = (sqlite3 *)handle;
2021 sqlite3_stmt *sql_stmt = NULL;
2022 media_svc_media_type_e media_type;
2023 media_svc_content_info_s content_info;
2024 GArray *db_data_array = NULL;
2025 media_svc_item_info_s *db_data = NULL;
2029 media_svc_debug_fenter();
2031 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2032 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2033 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2035 if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2036 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2037 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2038 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2039 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2040 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2042 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2043 } else { /*Storage Scanning*/
2044 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2047 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2048 if (ret != MS_MEDIA_ERR_NONE) {
2049 media_svc_error("error when get list. err = [%d]", ret);
2053 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2054 if (db_data_array == NULL) {
2055 media_svc_error("db_data_array is NULL. Out of memory");
2056 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2059 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2061 db_data = malloc(sizeof(media_svc_item_info_s));
2062 if(db_data == NULL) {
2063 media_svc_error("Out of memory");
2067 if(STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
2068 db_data->path = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2070 db_data->media_type = (int)sqlite3_column_int(sql_stmt,1);
2072 g_array_append_val(db_data_array, db_data);
2075 SQLITE3_FINALIZE(sql_stmt);
2077 while (db_data_array->len != 0) {
2079 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2080 g_array_remove_index (db_data_array, 0);
2082 if ((db_data == NULL) ||(db_data->path == NULL)) {
2083 media_svc_error("invalid db data");
2087 media_type = db_data->media_type;
2088 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2089 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2090 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2092 content_info.media_type = media_type;
2093 content_info.storage_type = storage_type;
2095 _media_svc_set_default_value(&content_info, FALSE);
2097 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2099 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2100 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2102 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2105 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2107 _media_svc_destroy_content_info(&content_info);
2108 SAFE_FREE(db_data->path);
2115 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2118 g_array_free(db_data_array, FALSE);
2119 db_data_array = NULL;
2121 media_svc_debug_fleave();
2123 return MS_MEDIA_ERR_NONE;
2126 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2128 int ret = MS_MEDIA_ERR_NONE;
2129 sqlite3 * db_handle = (sqlite3 *)handle;
2131 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2133 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2138 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2140 int ret = MS_MEDIA_ERR_NONE;
2141 sqlite3 * db_handle = (sqlite3 *)handle;
2144 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2145 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2146 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2148 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2149 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2152 media_svc_debug("item is exist in database");
2153 return MS_MEDIA_ERR_NONE;
2155 media_svc_debug("item is not exist in database");
2156 return MS_MEDIA_ERR_DB_NO_RECORD;
2159 return MS_MEDIA_ERR_NONE;
2162 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2164 int ret = MS_MEDIA_ERR_NONE;
2165 sqlite3 * db_handle = (sqlite3 *)handle;
2168 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2169 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2170 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2173 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2174 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2178 media_svc_debug("item is exist in database");
2179 return MS_MEDIA_ERR_NONE;
2181 media_svc_debug("item is not exist in database");
2182 return MS_MEDIA_ERR_DB_NO_RECORD;
2185 return MS_MEDIA_ERR_NONE;
2188 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2190 int ret = MS_MEDIA_ERR_NONE;
2191 sqlite3 * db_handle = (sqlite3 *)handle;
2193 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2194 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2196 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);