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;
245 _media_svc_set_noti_from_pid(from_pid);
246 g_insert_with_noti = TRUE;
249 return MS_MEDIA_ERR_NONE;
252 int media_svc_insert_item_end(MediaSvcHandle *handle, uid_t uid)
254 int ret = MS_MEDIA_ERR_NONE;
255 sqlite3 *db_handle = (sqlite3 *)handle;
257 media_svc_debug_fenter();
259 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
261 if (g_media_svc_insert_item_cur_data_cnt > 0) {
263 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
264 if (g_insert_with_noti) {
265 media_svc_debug("sending noti list");
266 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
267 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
268 g_insert_with_noti = FALSE;
269 _media_svc_set_noti_from_pid(-1);
273 g_media_svc_insert_item_data_cnt = 1;
274 g_media_svc_insert_item_cur_data_cnt = 0;
279 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)
281 int ret = MS_MEDIA_ERR_NONE;
282 sqlite3 *db_handle = (sqlite3 *)handle;
283 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
284 media_svc_media_type_e media_type;
286 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
287 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
288 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
289 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
291 media_svc_content_info_s content_info;
292 memset(&content_info, 0, sizeof(media_svc_content_info_s));
295 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
296 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
297 if (ret != MS_MEDIA_ERR_NONE)
300 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
302 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
303 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
305 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
307 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
309 /*Set or Get folder id*/
310 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
311 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
313 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
314 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
316 if (g_media_svc_insert_item_data_cnt == 1) {
318 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
319 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
321 if (g_insert_with_noti)
322 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
324 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
326 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
327 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
329 if (g_insert_with_noti)
330 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
332 g_media_svc_insert_item_cur_data_cnt++;
334 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
336 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
337 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
339 if (g_insert_with_noti)
340 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
342 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
343 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
345 if (g_insert_with_noti) {
346 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
347 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
349 /* Recreate noti list */
350 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
351 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
354 g_media_svc_insert_item_cur_data_cnt = 0;
357 media_svc_error("Error in media_svc_insert_item_bulk");
358 _media_svc_destroy_content_info(&content_info);
359 return MS_MEDIA_ERR_INTERNAL;
362 _media_svc_destroy_content_info(&content_info);
364 return MS_MEDIA_ERR_NONE;
367 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)
369 int ret = MS_MEDIA_ERR_NONE;
370 sqlite3 *db_handle = (sqlite3 *)handle;
371 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
372 media_svc_media_type_e media_type;
373 int ini_val = _media_svc_get_ini_value();
375 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
376 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
377 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
378 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
380 media_svc_content_info_s content_info;
381 memset(&content_info, 0, sizeof(media_svc_content_info_s));
384 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
385 if (ret != MS_MEDIA_ERR_NONE) {
389 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
391 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
392 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
394 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
397 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
399 /*Set or Get folder id*/
400 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
401 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
403 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
404 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
406 /* Extracting thumbnail */
408 if (content_info.thumbnail_path == NULL) {
409 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
410 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
414 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
415 if (ret == MS_MEDIA_ERR_NONE)
416 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
418 if (content_info.media_meta.width <= 0)
419 content_info.media_meta.width = width;
421 if (content_info.media_meta.height <= 0)
422 content_info.media_meta.height = height;
428 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
430 if (ret == MS_MEDIA_ERR_NONE) {
431 media_svc_debug("Insertion is successful. Sending noti for this");
432 _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);
433 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
434 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
437 _media_svc_destroy_content_info(&content_info);
441 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
442 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
444 int ret = MS_MEDIA_ERR_NONE;
445 sqlite3 *db_handle = (sqlite3 *)handle;
446 char *file_name = NULL;
447 char *folder_path = NULL;
448 int modified_time = 0;
449 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
450 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
451 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
454 media_svc_debug_fenter();
456 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
457 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
458 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
459 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
460 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
462 /*check and update folder*/
463 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
464 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
467 file_name = g_path_get_basename(dest_path);
469 /*get modified_time*/
470 modified_time = _media_svc_get_file_time(dest_path);
472 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
473 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
474 if (ret != MS_MEDIA_ERR_NONE) {
475 media_svc_error("_media_svc_get_media_type_by_path failed");
476 SAFE_FREE(file_name);
480 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
481 /*get old thumbnail_path*/
482 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
483 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
484 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
485 SAFE_FREE(file_name);
489 /* If old thumb path is default or not */
490 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
491 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
492 strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
494 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
496 SAFE_FREE(default_thumbnail_path);
499 if (g_media_svc_move_item_data_cnt == 1) {
502 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
503 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);
505 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);
507 SAFE_FREE(file_name);
508 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
510 media_svc_debug("Move is successful. Sending noti for this");
512 /* Get notification info */
513 media_svc_noti_item *noti_item = NULL;
514 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
515 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
517 /* Send notification for move */
518 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
519 _media_svc_destroy_noti_item(noti_item);
521 /*update folder modified_time*/
522 folder_path = g_path_get_dirname(dest_path);
523 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
524 SAFE_FREE(folder_path);
525 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
527 ret = _media_svc_update_folder_table(db_handle, storage_id, uid);
528 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
530 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
533 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
534 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);
536 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);
538 SAFE_FREE(file_name);
539 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
541 /*update folder modified_time*/
542 folder_path = g_path_get_dirname(dest_path);
543 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
544 SAFE_FREE(folder_path);
545 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
547 g_media_svc_move_item_cur_data_cnt++;
549 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
552 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
553 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);
555 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);
557 SAFE_FREE(file_name);
558 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
560 /*update folder modified_time*/
561 folder_path = g_path_get_dirname(dest_path);
562 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
563 SAFE_FREE(folder_path);
564 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
567 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
568 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
570 g_media_svc_move_item_cur_data_cnt = 0;
573 media_svc_error("Error in media_svc_move_item");
574 return MS_MEDIA_ERR_INTERNAL;
577 /*rename thumbnail file*/
578 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
579 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)) {
580 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
581 if (ret != MS_MEDIA_ERR_NONE)
582 media_svc_error("_media_svc_rename_file failed : %d", ret);
586 return MS_MEDIA_ERR_NONE;
589 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
591 sqlite3 *db_handle = (sqlite3 *)handle;
593 media_svc_debug("Transaction data count : [%d]", data_cnt);
595 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
596 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
598 g_media_svc_item_validity_data_cnt = data_cnt;
599 g_media_svc_item_validity_cur_data_cnt = 0;
601 return MS_MEDIA_ERR_NONE;
604 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
606 int ret = MS_MEDIA_ERR_NONE;
607 sqlite3 *db_handle = (sqlite3 *)handle;
609 media_svc_debug_fenter();
611 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
613 if (g_media_svc_item_validity_cur_data_cnt > 0) {
615 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
618 g_media_svc_item_validity_data_cnt = 1;
619 g_media_svc_item_validity_cur_data_cnt = 0;
624 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *storage_id, const char *path, int validity, uid_t uid)
626 int ret = MS_MEDIA_ERR_NONE;
627 sqlite3 *db_handle = (sqlite3 *)handle;
629 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
630 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
631 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
633 media_svc_debug("path=[%s], validity=[%d]", path, validity);
635 if (g_media_svc_item_validity_data_cnt == 1) {
637 return _media_svc_update_item_validity(db_handle, storage_id, path, validity, FALSE, uid);
639 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
641 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
642 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
644 g_media_svc_item_validity_cur_data_cnt++;
646 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
648 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
649 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
651 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
652 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
654 g_media_svc_item_validity_cur_data_cnt = 0;
658 media_svc_error("Error in media_svc_set_item_validity");
659 return MS_MEDIA_ERR_INTERNAL;
662 return MS_MEDIA_ERR_NONE;
665 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
667 int ret = MS_MEDIA_ERR_NONE;
668 sqlite3 *db_handle = (sqlite3 *)handle;
669 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
671 media_svc_debug_fenter();
673 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
674 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
675 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
678 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
679 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
682 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
683 /*Get thumbnail path to delete*/
684 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
685 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
686 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
688 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
689 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
692 /*Get thumbnail path to delete*/
693 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
694 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
698 /*Get thumbnail path to delete*/
699 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
700 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
702 if (g_media_svc_insert_item_data_cnt == 1) {
704 /* Get notification info */
705 media_svc_noti_item *noti_item = NULL;
706 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
707 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
710 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
711 if (ret != MS_MEDIA_ERR_NONE) {
712 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
713 _media_svc_destroy_noti_item(noti_item);
718 /* Send notification */
719 media_svc_debug("Deletion is successful. Sending noti for this");
720 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
721 _media_svc_destroy_noti_item(noti_item);
723 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
724 if (ret != MS_MEDIA_ERR_NONE) {
725 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
732 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
733 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 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 SAFE_FREE(default_thumbnail_path);
755 return MS_MEDIA_ERR_NONE;
758 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
760 int ret = MS_MEDIA_ERR_NONE;
761 sqlite3 *db_handle = (sqlite3 *)handle;
763 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
765 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
766 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
767 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
769 ret = _media_svc_truncate_table(db_handle, storage_id, storage_type, uid);
770 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
772 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
773 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
774 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
776 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
778 /* remove thumbnails */
779 ret = _media_svc_remove_all_files_in_dir(dirpath);
780 SAFE_FREE(internal_thumb_path);
781 SAFE_FREE(external_thumb_path);
782 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
785 return MS_MEDIA_ERR_NONE;
788 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
790 sqlite3 *db_handle = (sqlite3 *)handle;
792 media_svc_debug_fenter();
794 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
795 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
796 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
798 /*Delete from DB and remove thumbnail files*/
799 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
802 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
804 sqlite3 *db_handle = (sqlite3 *)handle;
806 media_svc_debug_fenter();
808 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
809 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
811 /*Delete from DB and remove thumbnail files*/
812 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
815 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)
817 sqlite3 *db_handle = (sqlite3 *)handle;
819 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
820 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
821 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
823 return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
826 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
828 sqlite3 *db_handle = (sqlite3 *)handle;
830 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
831 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
832 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
835 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
837 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
840 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
842 int ret = MS_MEDIA_ERR_NONE;
843 sqlite3 *db_handle = (sqlite3 *)handle;
844 media_svc_media_type_e media_type;
845 int ini_val = _media_svc_get_ini_value();
847 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
848 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
849 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
850 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
852 media_svc_content_info_s content_info;
853 memset(&content_info, 0, sizeof(media_svc_content_info_s));
856 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
857 if (ret != MS_MEDIA_ERR_NONE) {
861 /* Initialize thumbnail information to remake thumbnail. */
863 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
864 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
865 if (ret != MS_MEDIA_ERR_NONE) {
866 _media_svc_destroy_content_info(&content_info);
870 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)) {
871 ret = _media_svc_remove_file(thumb_path);
872 if (ret != MS_MEDIA_ERR_NONE) {
873 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
877 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
878 if (ret != MS_MEDIA_ERR_NONE) {
879 _media_svc_destroy_content_info(&content_info);
884 /* Get notification info */
885 media_svc_noti_item *noti_item = NULL;
886 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
887 if (ret != MS_MEDIA_ERR_NONE) {
888 _media_svc_destroy_content_info(&content_info);
892 media_type = noti_item->media_type;
893 content_info.media_type = media_type;
895 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
897 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
898 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
900 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
903 if (ret != MS_MEDIA_ERR_NONE) {
904 _media_svc_destroy_noti_item(noti_item);
905 _media_svc_destroy_content_info(&content_info);
909 /* Extracting thumbnail */
911 if (content_info.thumbnail_path == NULL) {
912 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
913 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
917 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
918 if (ret == MS_MEDIA_ERR_NONE) {
919 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
922 if (content_info.media_meta.width <= 0)
923 content_info.media_meta.width = width;
925 if (content_info.media_meta.height <= 0)
926 content_info.media_meta.height = height;
933 ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
935 if (ret == MS_MEDIA_ERR_NONE) {
936 media_svc_debug("Update is successful. Sending noti for this");
937 _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);
939 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
942 _media_svc_destroy_content_info(&content_info);
943 _media_svc_destroy_noti_item(noti_item);
948 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
950 sqlite3 *db_handle = (sqlite3 *)handle;
951 int ret = MS_MEDIA_ERR_NONE;
953 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
954 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
955 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
957 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
959 ret = _media_svc_sql_begin_trans(db_handle, uid);
960 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
962 /* Update all folder record's modified date, which are changed above */
963 char *update_folder_modified_time_sql = NULL;
967 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
969 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
970 //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
971 sqlite3_free(update_folder_modified_time_sql);
973 if (ret != SQLITE_OK) {
974 media_svc_error("failed to update folder modified time");
975 _media_svc_sql_rollback_trans(db_handle, uid);
977 return MS_MEDIA_ERR_DB_INTERNAL;
980 /* Update all items */
981 char *select_all_sql = NULL;
982 sqlite3_stmt *sql_stmt = NULL;
983 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
985 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
987 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);
989 media_svc_debug("[SQL query] : %s", select_all_sql);
991 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
992 if (ret != MS_MEDIA_ERR_NONE) {
993 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
994 _media_svc_sql_rollback_trans(db_handle, uid);
998 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
999 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1000 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1001 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1002 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1003 /*int media_type; */
1004 bool no_thumb = FALSE;
1006 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1007 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1008 media_uuid[sizeof(media_uuid) - 1] = '\0';
1010 media_svc_error("media UUID is NULL");
1011 return MS_MEDIA_ERR_DB_INTERNAL;
1014 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1015 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1016 media_path[sizeof(media_path) - 1] = '\0';
1018 media_svc_error("media path is NULL");
1019 return MS_MEDIA_ERR_DB_INTERNAL;
1022 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1023 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1024 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1026 media_svc_debug("media thumb path doesn't exist in DB");
1030 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1032 /* Update path, thumbnail path of this item */
1033 char *replaced_path = NULL;
1034 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1035 if (replaced_path == NULL) {
1036 media_svc_error("_media_svc_replace_path failed");
1037 SQLITE3_FINALIZE(sql_stmt);
1038 _media_svc_sql_rollback_trans(db_handle, uid);
1039 return MS_MEDIA_ERR_INTERNAL;
1042 media_svc_debug("New media path : %s", replaced_path);
1043 media_svc_storage_type_e storage_type;
1046 /* If old thumb path is default or not */
1047 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1048 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1049 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1051 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1052 if (ret != MS_MEDIA_ERR_NONE) {
1053 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1054 SAFE_FREE(replaced_path);
1055 SAFE_FREE(default_thumbnail_path);
1056 _media_svc_sql_rollback_trans(db_handle, uid);
1060 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1061 if (ret != MS_MEDIA_ERR_NONE) {
1062 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1063 SAFE_FREE(replaced_path);
1064 SAFE_FREE(default_thumbnail_path);
1065 SQLITE3_FINALIZE(sql_stmt);
1066 _media_svc_sql_rollback_trans(db_handle, uid);
1071 SAFE_FREE(default_thumbnail_path);
1073 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1076 char *update_item_sql = NULL;
1079 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1082 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1083 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);
1085 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_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_new_thumb_path, media_uuid);
1092 ret = media_db_request_update_db_batch(update_item_sql, uid);
1093 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1094 sqlite3_free(update_item_sql);
1095 SAFE_FREE(replaced_path);
1097 if (ret != SQLITE_OK) {
1098 media_svc_error("failed to update item");
1099 SQLITE3_FINALIZE(sql_stmt);
1100 _media_svc_sql_rollback_trans(db_handle, uid);
1102 return MS_MEDIA_ERR_DB_INTERNAL;
1105 /* Rename thumbnail file of file system */
1106 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1107 if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1108 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1109 if (ret != MS_MEDIA_ERR_NONE) {
1110 media_svc_error("_media_svc_rename_file failed : %d", ret);
1114 SAFE_FREE(default_thumbnail_path);
1117 SQLITE3_FINALIZE(sql_stmt);
1119 ret = _media_svc_sql_end_trans(db_handle, uid);
1120 if (ret != MS_MEDIA_ERR_NONE) {
1121 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1122 _media_svc_sql_rollback_trans(db_handle, uid);
1126 media_svc_debug("Folder update is successful. Sending noti for this");
1127 /* Get notification info */
1128 media_svc_noti_item *noti_item = NULL;
1129 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1130 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1132 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1133 _media_svc_destroy_noti_item(noti_item);
1135 return MS_MEDIA_ERR_NONE;
1138 int media_svc_request_update_db(const char *db_query, uid_t uid)
1140 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1142 return _media_svc_sql_query(NULL, db_query, uid);
1145 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)
1147 int ret = MS_MEDIA_ERR_NONE;
1148 sqlite3 *db_handle = (sqlite3 *)handle;
1151 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1152 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1154 /* Get notification info */
1155 media_svc_noti_item *noti_item = NULL;
1156 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1157 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1159 if (folder_id != NULL) {
1162 uuid = noti_item->media_uuid;
1165 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1166 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1167 _media_svc_destroy_noti_item(noti_item);
1172 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1174 sqlite3 *db_handle = (sqlite3 *)handle;
1176 media_svc_debug_fenter();
1178 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1179 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1180 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1181 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1183 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1186 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, uid_t uid)
1188 sqlite3 *db_handle = (sqlite3 *)handle;
1190 media_svc_debug_fenter();
1192 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1194 return _media_svc_check_db_upgrade(db_handle, need_full_scan, uid);
1197 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1199 sqlite3 *db_handle = (sqlite3 *)handle;
1201 media_svc_debug_fenter();
1203 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1205 return _media_db_check_corrupt(db_handle);
1208 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)
1210 sqlite3 *db_handle = (sqlite3 *)handle;
1212 media_svc_debug_fenter();
1214 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1215 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1217 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1220 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1222 int ret = MS_MEDIA_ERR_NONE;
1223 sqlite3 *db_handle = (sqlite3 *)handle;
1224 time_t sto_time = 0;
1225 int cur_time = _media_svc_get_file_time(folder_path);
1226 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1228 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1230 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1231 if (ret == MS_MEDIA_ERR_NONE) {
1232 if (sto_time != cur_time) {
1233 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1240 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1242 sqlite3 *db_handle = (sqlite3 *)handle;
1244 media_svc_debug("Transaction data count : [%d]", data_cnt);
1246 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1247 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1249 g_media_svc_update_item_data_cnt = data_cnt;
1250 g_media_svc_update_item_cur_data_cnt = 0;
1252 return MS_MEDIA_ERR_NONE;
1255 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1257 int ret = MS_MEDIA_ERR_NONE;
1258 sqlite3 *db_handle = (sqlite3 *)handle;
1260 media_svc_debug_fenter();
1262 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1264 if (g_media_svc_update_item_cur_data_cnt > 0) {
1266 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1269 g_media_svc_update_item_data_cnt = 1;
1270 g_media_svc_update_item_cur_data_cnt = 0;
1275 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1277 int ret = MS_MEDIA_ERR_NONE;
1278 sqlite3 *db_handle = (sqlite3 *)handle;
1280 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1282 media_svc_media_type_e media_type;
1284 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1285 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1286 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1288 media_svc_content_info_s content_info;
1289 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1292 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1293 if (ret != MS_MEDIA_ERR_NONE) {
1297 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1298 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1300 return MS_MEDIA_ERR_NONE;
1303 if (ret != MS_MEDIA_ERR_NONE) {
1304 _media_svc_destroy_content_info(&content_info);
1308 if (g_media_svc_update_item_data_cnt == 1) {
1310 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1311 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1313 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1315 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1316 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1318 g_media_svc_update_item_cur_data_cnt++;
1320 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1322 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1323 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1325 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1326 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1328 g_media_svc_update_item_cur_data_cnt = 0;
1331 media_svc_error("Error in media_svc_update_item_meta");
1332 _media_svc_destroy_content_info(&content_info);
1333 return MS_MEDIA_ERR_INTERNAL;
1336 _media_svc_destroy_content_info(&content_info);
1341 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)
1343 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1346 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1348 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1350 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1353 int media_svc_check_pinyin_support(bool *support)
1355 *support = _media_svc_check_pinyin_support();
1357 return MS_MEDIA_ERR_NONE;
1360 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1362 int ret = MS_MEDIA_ERR_NONE;
1363 sqlite3 * db_handle = (sqlite3 *)handle;
1365 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1367 ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1368 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1370 ret = _media_svc_update_media_view(db_handle, uid);
1371 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1376 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
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(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1384 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1389 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
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_path(db_handle, storage_uuid, storage_path);
1402 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
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");
1408 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1410 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1415 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1417 int ret = MS_MEDIA_ERR_NONE;
1418 sqlite3 * db_handle = (sqlite3 *)handle;
1420 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1422 ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1427 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1429 sqlite3 * db_handle = (sqlite3 *)handle;
1431 media_svc_debug_fenter();
1433 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1434 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1436 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1439 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1441 int ret = MS_MEDIA_ERR_NONE;
1443 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1444 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1446 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1447 new_content_info->size = content_info->size;
1448 new_content_info->modified_time = content_info->modified_time;
1449 new_content_info->is_drm = content_info->is_drm;
1450 new_content_info->media_type = content_info->media_type;
1452 if (STRING_VALID(content_info->mime_type)) {
1453 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1454 if (ret != MS_MEDIA_ERR_NONE)
1455 media_svc_error("strcpy mime_type failed");
1458 if (STRING_VALID(content_info->thumbnail_path)) {
1459 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1460 if (ret != MS_MEDIA_ERR_NONE)
1461 media_svc_error("strcpy thumbnail_path failed");
1464 new_content_info->media_meta.duration = content_info->media_meta.duration;
1465 new_content_info->media_meta.width = content_info->media_meta.width;
1466 new_content_info->media_meta.height = content_info->media_meta.height;
1469 if (content_info->added_time > 0)
1470 new_content_info->added_time = content_info->added_time;
1471 new_content_info->last_played_time = content_info->last_played_time;
1472 new_content_info->played_count = content_info->played_count;
1473 new_content_info->favourate = content_info->favourate;
1475 if (STRING_VALID(content_info->file_name)) {
1476 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1477 if (ret != MS_MEDIA_ERR_NONE)
1478 media_svc_error("strcpy file_name failed");
1481 if (STRING_VALID(content_info->media_meta.title)) {
1482 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1483 if (ret != MS_MEDIA_ERR_NONE)
1484 media_svc_error("strcpy title failed");
1487 if (STRING_VALID(content_info->media_meta.album)) {
1488 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1489 if (ret != MS_MEDIA_ERR_NONE)
1490 media_svc_error("strcpy album failed");
1493 if (STRING_VALID(content_info->media_meta.artist)) {
1494 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1495 if (ret != MS_MEDIA_ERR_NONE)
1496 media_svc_error("strcpy artist failed");
1499 if (STRING_VALID(content_info->media_meta.genre)) {
1500 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1501 if (ret != MS_MEDIA_ERR_NONE)
1502 media_svc_error("strcpy genre failed");
1505 if (STRING_VALID(content_info->media_meta.composer)) {
1506 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1507 if (ret != MS_MEDIA_ERR_NONE)
1508 media_svc_error("strcpy composer failed");
1511 if (STRING_VALID(content_info->media_meta.year)) {
1512 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1513 if (ret != MS_MEDIA_ERR_NONE)
1514 media_svc_error("strcpy year failed");
1517 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1518 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1519 if (ret != MS_MEDIA_ERR_NONE)
1520 media_svc_error("strcpy recorded_date failed");
1523 if (STRING_VALID(content_info->media_meta.copyright)) {
1524 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1525 if (ret != MS_MEDIA_ERR_NONE)
1526 media_svc_error("strcpy copyright failed");
1529 if (STRING_VALID(content_info->media_meta.track_num)) {
1530 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1531 if (ret != MS_MEDIA_ERR_NONE)
1532 media_svc_error("strcpy track_num failed");
1535 if (STRING_VALID(content_info->media_meta.description)) {
1536 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1537 if (ret != MS_MEDIA_ERR_NONE)
1538 media_svc_error("strcpy description failed");
1541 if (STRING_VALID(content_info->media_meta.weather)) {
1542 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1543 if (ret != MS_MEDIA_ERR_NONE)
1544 media_svc_error("strcpy weather failed");
1547 if (STRING_VALID(content_info->media_meta.category)) {
1548 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1549 if (ret != MS_MEDIA_ERR_NONE)
1550 media_svc_error("strcpy category failed");
1553 if (STRING_VALID(content_info->media_meta.keyword)) {
1554 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1555 if (ret != MS_MEDIA_ERR_NONE)
1556 media_svc_error("strcpy keyword failed");
1559 if (STRING_VALID(content_info->media_meta.location_tag)) {
1560 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1561 if (ret != MS_MEDIA_ERR_NONE)
1562 media_svc_error("strcpy location_tag failed");
1565 if (STRING_VALID(content_info->media_meta.content_name)) {
1566 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1567 if (ret != MS_MEDIA_ERR_NONE)
1568 media_svc_error("strcpy content_name failed");
1571 if (STRING_VALID(content_info->media_meta.age_rating)) {
1572 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1573 if (ret != MS_MEDIA_ERR_NONE)
1574 media_svc_error("strcpy age_rating failed");
1577 if (STRING_VALID(content_info->media_meta.author)) {
1578 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1579 if (ret != MS_MEDIA_ERR_NONE)
1580 media_svc_error("strcpy author failed");
1583 if (STRING_VALID(content_info->media_meta.provider)) {
1584 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1585 if (ret != MS_MEDIA_ERR_NONE)
1586 media_svc_error("strcpy provider failed");
1589 if (STRING_VALID(content_info->media_meta.datetaken)) {
1590 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1591 if (ret != MS_MEDIA_ERR_NONE)
1592 media_svc_error("strcpy datetaken failed");
1594 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1595 if (new_content_info->timeline == 0) {
1596 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1597 new_content_info->timeline = new_content_info->modified_time;
1599 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1603 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1604 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1605 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1606 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1608 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1609 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1610 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1611 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1612 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1613 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1615 new_content_info->media_meta.rating = content_info->media_meta.rating;
1620 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1622 int ret = MS_MEDIA_ERR_NONE;
1623 sqlite3 *db_handle = (sqlite3 *)handle;
1624 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1625 bool append_album = FALSE;
1627 /* Checking parameters if they are valid */
1628 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1629 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1630 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1632 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1633 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1635 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1636 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1637 return MS_MEDIA_ERR_INVALID_PARAMETER;
1640 if (content_info->size <= 0) {
1641 media_svc_error("invalid size condition[%d]", content_info->size);
1642 return MS_MEDIA_ERR_INVALID_PARAMETER;
1645 if (content_info->modified_time <= 0) {
1646 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1647 return MS_MEDIA_ERR_INVALID_PARAMETER;
1651 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1653 media_svc_content_info_s _new_content_info;
1654 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1656 media_svc_media_type_e media_type;
1658 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1659 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1661 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1662 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1664 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1665 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1667 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1670 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1672 /* Extracting thumbnail */
1673 int ini_val = _media_svc_get_ini_value();
1675 if (_new_content_info.thumbnail_path == NULL) {
1676 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1677 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1681 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1682 if (ret == MS_MEDIA_ERR_NONE) {
1683 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1686 if (_new_content_info.media_meta.width <= 0)
1687 _new_content_info.media_meta.width = width;
1689 if (_new_content_info.media_meta.height <= 0)
1690 _new_content_info.media_meta.height = height;
1696 /* set othere data to the structure, which is passed as parameters */
1697 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1699 /* Set or Get folder id */
1700 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);
1701 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1703 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1704 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1706 /* register album table data */
1709 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1710 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1712 if (ret != MS_MEDIA_ERR_NONE) {
1713 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1714 media_svc_debug("album does not exist. So start to make album art");
1715 append_album = TRUE;
1717 media_svc_debug("other error");
1718 append_album = FALSE;
1721 _new_content_info.album_id = album_id;
1722 append_album = FALSE;
1724 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1725 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1727 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1730 media_svc_debug("album already exists. don't need to make album art");
1731 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1732 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1737 if (append_album == TRUE) {
1739 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1740 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1741 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);
1743 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1745 _new_content_info.album_id = album_id;
1748 /* Insert to db - calling _media_svc_insert_item_with_data */
1749 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1751 if (ret == MS_MEDIA_ERR_NONE) {
1752 media_svc_debug("Insertion is successful.");
1755 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1756 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1759 _media_svc_destroy_content_info(&_new_content_info);
1761 /* handling returned value - important */
1765 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1767 _media_svc_destroy_content_info(content_info);
1770 int media_svc_generate_uuid(char **uuid)
1772 char *gen_uuid = NULL;
1773 gen_uuid = _media_info_generate_uuid();
1774 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1776 *uuid = strdup(gen_uuid);
1778 return MS_MEDIA_ERR_NONE;
1781 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1783 sqlite3 * db_handle = (sqlite3 *)handle;
1785 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1787 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1790 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1792 sqlite3 * db_handle = (sqlite3 *)handle;
1794 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1795 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1796 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1797 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1799 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1802 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1804 sqlite3 * db_handle = (sqlite3 *)handle;
1806 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1807 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1808 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1810 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1813 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)
1815 int ret = MS_MEDIA_ERR_NONE;
1816 sqlite3 *db_handle = (sqlite3 *)handle;
1818 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1819 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1820 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1821 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1823 ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1824 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1826 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1827 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1828 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1830 ret = _media_svc_update_media_view(db_handle, uid);
1831 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1837 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1839 int ret = MS_MEDIA_ERR_NONE;
1840 sqlite3 *db_handle = (sqlite3 *)handle;
1841 media_svc_storage_type_e storage_type;
1843 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1844 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1846 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1847 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1849 ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1850 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1852 ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1853 if (ret != MS_MEDIA_ERR_NONE) {
1854 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1857 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1858 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1859 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1861 ret = _media_svc_update_media_view(db_handle, uid);
1862 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1868 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1870 sqlite3 * db_handle = (sqlite3 *)handle;
1872 media_svc_debug("Transaction data count : [%d]", data_cnt);
1874 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1875 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1877 g_media_svc_insert_folder_data_cnt = data_cnt;
1878 g_media_svc_insert_folder_cur_data_cnt = 0;
1880 return MS_MEDIA_ERR_NONE;
1883 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1885 int ret = MS_MEDIA_ERR_NONE;
1886 sqlite3 * db_handle = (sqlite3 *)handle;
1888 media_svc_debug_fenter();
1890 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1892 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1894 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1897 g_media_svc_insert_folder_data_cnt = 1;
1898 g_media_svc_insert_folder_cur_data_cnt = 0;
1903 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1905 int ret = MS_MEDIA_ERR_NONE;
1906 sqlite3 * db_handle = (sqlite3 *)handle;
1907 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1909 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1910 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1911 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1912 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1914 if (g_media_svc_insert_folder_data_cnt == 1) {
1916 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1918 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1920 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1921 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1923 g_media_svc_insert_folder_cur_data_cnt++;
1925 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1927 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1928 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1930 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1931 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1933 g_media_svc_insert_folder_cur_data_cnt = 0;
1936 media_svc_error("Error in media_svc_set_insert_folder");
1937 return MS_MEDIA_ERR_INTERNAL;
1943 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1945 int ret = MS_MEDIA_ERR_NONE;
1946 sqlite3 * db_handle = (sqlite3 *)handle;
1948 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1950 ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
1955 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1957 int ret = MS_MEDIA_ERR_NONE;
1958 sqlite3 * db_handle = (sqlite3 *)handle;
1960 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1962 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1967 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)
1969 int ret = MS_MEDIA_ERR_NONE;
1970 sqlite3 * db_handle = (sqlite3 *)handle;
1971 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1972 media_svc_media_type_e media_type;
1974 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1975 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1976 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1977 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1979 media_svc_content_info_s content_info;
1980 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1982 /*Set basic media info*/
1983 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1984 if (ret != MS_MEDIA_ERR_NONE) {
1985 media_svc_error("_media_svc_set_media_info fail %d", ret);
1989 //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);
1991 /*Set or Get folder id*/
1992 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1993 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1995 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1996 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1998 if (g_media_svc_insert_item_data_cnt == 1) {
2000 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2001 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2003 if (g_insert_with_noti)
2004 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2005 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2007 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2008 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2010 if (g_insert_with_noti)
2011 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2013 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2014 g_media_svc_insert_item_cur_data_cnt++;
2016 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2018 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2019 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2021 if (g_insert_with_noti)
2022 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2024 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2025 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2027 media_svc_debug("_media_svc_list_query_do over");
2029 if (g_insert_with_noti) {
2030 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2031 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2032 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2034 /* Recreate noti list */
2035 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2036 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2039 g_media_svc_insert_item_cur_data_cnt = 0;
2042 media_svc_error("Error in media_svc_insert_item_pass1");
2043 _media_svc_destroy_content_info(&content_info);
2044 return MS_MEDIA_ERR_INTERNAL;
2047 _media_svc_destroy_content_info(&content_info);
2049 return MS_MEDIA_ERR_NONE;
2052 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)
2054 int ret = MS_MEDIA_ERR_NONE;
2055 sqlite3 * db_handle = (sqlite3 *)handle;
2056 sqlite3_stmt *sql_stmt = NULL;
2057 media_svc_media_type_e media_type;
2058 media_svc_content_info_s content_info;
2059 GArray *db_data_array = NULL;
2060 media_svc_item_info_s *db_data = NULL;
2064 media_svc_debug_fenter();
2066 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2067 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2068 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2070 if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2071 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2072 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2073 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2074 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2075 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2077 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2078 } else { /*Storage Scanning*/
2079 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2082 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2083 if (ret != MS_MEDIA_ERR_NONE) {
2084 media_svc_error("error when get list. err = [%d]", ret);
2088 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2089 if (db_data_array == NULL) {
2090 media_svc_error("db_data_array is NULL. Out of memory");
2091 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2094 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2096 db_data = malloc(sizeof(media_svc_item_info_s));
2097 if (db_data == NULL) {
2098 media_svc_error("Out of memory");
2102 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2103 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2105 g_array_append_val(db_data_array, db_data);
2108 SQLITE3_FINALIZE(sql_stmt);
2110 while (db_data_array->len != 0) {
2112 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2113 g_array_remove_index(db_data_array, 0);
2115 if ((db_data == NULL) || (db_data->path == NULL)) {
2116 media_svc_error("invalid db data");
2120 media_type = db_data->media_type;
2121 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2122 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2123 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2125 content_info.media_type = media_type;
2126 content_info.storage_type = storage_type;
2128 _media_svc_set_default_value(&content_info, FALSE);
2130 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2132 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2133 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2135 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2138 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2140 _media_svc_destroy_content_info(&content_info);
2141 SAFE_FREE(db_data->path);
2147 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2149 g_array_free(db_data_array, FALSE);
2150 db_data_array = NULL;
2152 media_svc_debug_fleave();
2154 return MS_MEDIA_ERR_NONE;
2157 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2159 int ret = MS_MEDIA_ERR_NONE;
2160 sqlite3 * db_handle = (sqlite3 *)handle;
2162 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2164 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2169 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2171 int ret = MS_MEDIA_ERR_NONE;
2172 sqlite3 * db_handle = (sqlite3 *)handle;
2175 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2176 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2177 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2179 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2180 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2183 media_svc_debug("item is exist in database");
2184 return MS_MEDIA_ERR_NONE;
2186 media_svc_debug("item is not exist in database");
2187 return MS_MEDIA_ERR_DB_NO_RECORD;
2190 return MS_MEDIA_ERR_NONE;
2193 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2195 int ret = MS_MEDIA_ERR_NONE;
2196 sqlite3 * db_handle = (sqlite3 *)handle;
2199 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2200 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2201 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2204 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2205 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2209 media_svc_debug("item is exist in database");
2210 return MS_MEDIA_ERR_NONE;
2212 media_svc_debug("item is not exist in database");
2213 return MS_MEDIA_ERR_DB_NO_RECORD;
2216 return MS_MEDIA_ERR_NONE;
2219 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2221 int ret = MS_MEDIA_ERR_NONE;
2222 sqlite3 * db_handle = (sqlite3 *)handle;
2224 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2225 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2227 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);