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 if (STRING_VALID(dirpath))
780 ret = _media_svc_remove_all_files_in_dir(dirpath);
781 SAFE_FREE(internal_thumb_path);
782 SAFE_FREE(external_thumb_path);
783 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
786 return MS_MEDIA_ERR_NONE;
789 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
791 sqlite3 *db_handle = (sqlite3 *)handle;
793 media_svc_debug_fenter();
795 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
796 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
797 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
799 /*Delete from DB and remove thumbnail files*/
800 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
803 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
805 sqlite3 *db_handle = (sqlite3 *)handle;
807 media_svc_debug_fenter();
809 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
810 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
812 /*Delete from DB and remove thumbnail files*/
813 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
816 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)
818 sqlite3 *db_handle = (sqlite3 *)handle;
820 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
821 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
822 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
824 return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
827 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
829 sqlite3 *db_handle = (sqlite3 *)handle;
831 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
832 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
833 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
836 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
838 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
841 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
843 int ret = MS_MEDIA_ERR_NONE;
844 sqlite3 *db_handle = (sqlite3 *)handle;
845 media_svc_media_type_e media_type;
846 int ini_val = _media_svc_get_ini_value();
848 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
849 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
850 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
851 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
853 media_svc_content_info_s content_info;
854 memset(&content_info, 0, sizeof(media_svc_content_info_s));
857 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
858 if (ret != MS_MEDIA_ERR_NONE) {
862 /* Initialize thumbnail information to remake thumbnail. */
864 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
865 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
866 if (ret != MS_MEDIA_ERR_NONE) {
867 _media_svc_destroy_content_info(&content_info);
871 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)) {
872 ret = _media_svc_remove_file(thumb_path);
873 if (ret != MS_MEDIA_ERR_NONE) {
874 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
878 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
879 if (ret != MS_MEDIA_ERR_NONE) {
880 _media_svc_destroy_content_info(&content_info);
885 /* Get notification info */
886 media_svc_noti_item *noti_item = NULL;
887 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
888 if (ret != MS_MEDIA_ERR_NONE) {
889 _media_svc_destroy_content_info(&content_info);
893 media_type = noti_item->media_type;
894 content_info.media_type = media_type;
896 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
898 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
899 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
901 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
904 if (ret != MS_MEDIA_ERR_NONE) {
905 _media_svc_destroy_noti_item(noti_item);
906 _media_svc_destroy_content_info(&content_info);
910 /* Extracting thumbnail */
912 if (content_info.thumbnail_path == NULL) {
913 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
914 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
918 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
919 if (ret == MS_MEDIA_ERR_NONE) {
920 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
923 if (content_info.media_meta.width <= 0)
924 content_info.media_meta.width = width;
926 if (content_info.media_meta.height <= 0)
927 content_info.media_meta.height = height;
934 ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
936 if (ret == MS_MEDIA_ERR_NONE) {
937 media_svc_debug("Update is successful. Sending noti for this");
938 _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);
940 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
943 _media_svc_destroy_content_info(&content_info);
944 _media_svc_destroy_noti_item(noti_item);
949 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
951 sqlite3 *db_handle = (sqlite3 *)handle;
952 int ret = MS_MEDIA_ERR_NONE;
954 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
955 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
956 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
958 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
960 ret = _media_svc_sql_begin_trans(db_handle, uid);
961 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
963 /* Update all folder record's modified date, which are changed above */
964 char *update_folder_modified_time_sql = NULL;
968 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
970 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
971 //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
972 sqlite3_free(update_folder_modified_time_sql);
974 if (ret != SQLITE_OK) {
975 media_svc_error("failed to update folder modified time");
976 _media_svc_sql_rollback_trans(db_handle, uid);
978 return MS_MEDIA_ERR_DB_INTERNAL;
981 /* Update all items */
982 char *select_all_sql = NULL;
983 sqlite3_stmt *sql_stmt = NULL;
984 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
986 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
988 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);
990 media_svc_debug("[SQL query] : %s", select_all_sql);
992 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
993 if (ret != MS_MEDIA_ERR_NONE) {
994 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
995 _media_svc_sql_rollback_trans(db_handle, uid);
999 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1000 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1001 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1002 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1003 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1004 /*int media_type; */
1005 bool no_thumb = FALSE;
1007 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1008 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1009 media_uuid[sizeof(media_uuid) - 1] = '\0';
1011 media_svc_error("media UUID is NULL");
1012 return MS_MEDIA_ERR_DB_INTERNAL;
1015 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1016 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1017 media_path[sizeof(media_path) - 1] = '\0';
1019 media_svc_error("media path is NULL");
1020 return MS_MEDIA_ERR_DB_INTERNAL;
1023 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1024 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1025 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1027 media_svc_debug("media thumb path doesn't exist in DB");
1031 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1033 /* Update path, thumbnail path of this item */
1034 char *replaced_path = NULL;
1035 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1036 if (replaced_path == NULL) {
1037 media_svc_error("_media_svc_replace_path failed");
1038 SQLITE3_FINALIZE(sql_stmt);
1039 _media_svc_sql_rollback_trans(db_handle, uid);
1040 return MS_MEDIA_ERR_INTERNAL;
1043 media_svc_debug("New media path : %s", replaced_path);
1044 media_svc_storage_type_e storage_type;
1047 /* If old thumb path is default or not */
1048 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1049 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1050 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path) - 1);
1052 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1053 if (ret != MS_MEDIA_ERR_NONE) {
1054 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1055 SAFE_FREE(replaced_path);
1056 SAFE_FREE(default_thumbnail_path);
1057 _media_svc_sql_rollback_trans(db_handle, uid);
1061 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1062 if (ret != MS_MEDIA_ERR_NONE) {
1063 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1064 SAFE_FREE(replaced_path);
1065 SAFE_FREE(default_thumbnail_path);
1066 SQLITE3_FINALIZE(sql_stmt);
1067 _media_svc_sql_rollback_trans(db_handle, uid);
1072 SAFE_FREE(default_thumbnail_path);
1074 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1077 char *update_item_sql = NULL;
1080 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1083 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1084 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);
1086 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1089 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);
1093 ret = media_db_request_update_db_batch(update_item_sql, uid);
1094 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1095 sqlite3_free(update_item_sql);
1096 SAFE_FREE(replaced_path);
1098 if (ret != SQLITE_OK) {
1099 media_svc_error("failed to update item");
1100 SQLITE3_FINALIZE(sql_stmt);
1101 _media_svc_sql_rollback_trans(db_handle, uid);
1103 return MS_MEDIA_ERR_DB_INTERNAL;
1106 /* Rename thumbnail file of file system */
1107 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1108 if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1109 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1110 if (ret != MS_MEDIA_ERR_NONE) {
1111 media_svc_error("_media_svc_rename_file failed : %d", ret);
1115 SAFE_FREE(default_thumbnail_path);
1118 SQLITE3_FINALIZE(sql_stmt);
1120 ret = _media_svc_sql_end_trans(db_handle, uid);
1121 if (ret != MS_MEDIA_ERR_NONE) {
1122 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1123 _media_svc_sql_rollback_trans(db_handle, uid);
1127 media_svc_debug("Folder update is successful. Sending noti for this");
1128 /* Get notification info */
1129 media_svc_noti_item *noti_item = NULL;
1130 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1131 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1133 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1134 _media_svc_destroy_noti_item(noti_item);
1136 return MS_MEDIA_ERR_NONE;
1139 int media_svc_request_update_db(const char *db_query, uid_t uid)
1141 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1143 return _media_svc_sql_query(NULL, db_query, uid);
1146 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)
1148 int ret = MS_MEDIA_ERR_NONE;
1149 sqlite3 *db_handle = (sqlite3 *)handle;
1152 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1153 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1155 /* Get notification info */
1156 media_svc_noti_item *noti_item = NULL;
1157 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1158 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1160 if (folder_id != NULL) {
1163 uuid = noti_item->media_uuid;
1166 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1167 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1168 _media_svc_destroy_noti_item(noti_item);
1173 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
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");
1180 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1181 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1182 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1184 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1187 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, uid_t uid)
1189 sqlite3 *db_handle = (sqlite3 *)handle;
1191 media_svc_debug_fenter();
1193 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1195 return _media_svc_check_db_upgrade(db_handle, need_full_scan, uid);
1198 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1200 sqlite3 *db_handle = (sqlite3 *)handle;
1202 media_svc_debug_fenter();
1204 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1206 return _media_db_check_corrupt(db_handle);
1209 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)
1211 sqlite3 *db_handle = (sqlite3 *)handle;
1213 media_svc_debug_fenter();
1215 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1216 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1218 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1221 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1223 int ret = MS_MEDIA_ERR_NONE;
1224 sqlite3 *db_handle = (sqlite3 *)handle;
1225 time_t sto_time = 0;
1226 int cur_time = _media_svc_get_file_time(folder_path);
1227 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1229 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1231 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1232 if (ret == MS_MEDIA_ERR_NONE) {
1233 if (sto_time != cur_time) {
1234 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1241 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1243 sqlite3 *db_handle = (sqlite3 *)handle;
1245 media_svc_debug("Transaction data count : [%d]", data_cnt);
1247 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1248 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1250 g_media_svc_update_item_data_cnt = data_cnt;
1251 g_media_svc_update_item_cur_data_cnt = 0;
1253 return MS_MEDIA_ERR_NONE;
1256 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1258 int ret = MS_MEDIA_ERR_NONE;
1259 sqlite3 *db_handle = (sqlite3 *)handle;
1261 media_svc_debug_fenter();
1263 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1265 if (g_media_svc_update_item_cur_data_cnt > 0) {
1267 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1270 g_media_svc_update_item_data_cnt = 1;
1271 g_media_svc_update_item_cur_data_cnt = 0;
1276 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1278 int ret = MS_MEDIA_ERR_NONE;
1279 sqlite3 *db_handle = (sqlite3 *)handle;
1281 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1283 media_svc_media_type_e media_type;
1285 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1286 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1287 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1289 media_svc_content_info_s content_info;
1290 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1293 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1294 if (ret != MS_MEDIA_ERR_NONE) {
1298 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1299 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1301 return MS_MEDIA_ERR_NONE;
1304 if (ret != MS_MEDIA_ERR_NONE) {
1305 _media_svc_destroy_content_info(&content_info);
1309 if (g_media_svc_update_item_data_cnt == 1) {
1311 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1312 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1314 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1316 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1317 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1319 g_media_svc_update_item_cur_data_cnt++;
1321 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1323 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1324 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1326 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1327 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1329 g_media_svc_update_item_cur_data_cnt = 0;
1332 media_svc_error("Error in media_svc_update_item_meta");
1333 _media_svc_destroy_content_info(&content_info);
1334 return MS_MEDIA_ERR_INTERNAL;
1337 _media_svc_destroy_content_info(&content_info);
1342 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)
1344 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1347 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1349 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1351 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1354 int media_svc_check_pinyin_support(bool *support)
1356 *support = _media_svc_check_pinyin_support();
1358 return MS_MEDIA_ERR_NONE;
1361 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1363 int ret = MS_MEDIA_ERR_NONE;
1364 sqlite3 * db_handle = (sqlite3 *)handle;
1366 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1368 ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1369 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1371 ret = _media_svc_update_media_view(db_handle, uid);
1372 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1377 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1379 int ret = MS_MEDIA_ERR_NONE;
1380 sqlite3 * db_handle = (sqlite3 *)handle;
1382 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1383 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1385 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1390 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1392 int ret = MS_MEDIA_ERR_NONE;
1393 sqlite3 * db_handle = (sqlite3 *)handle;
1395 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1396 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1398 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1403 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1405 int ret = MS_MEDIA_ERR_NONE;
1406 sqlite3 * db_handle = (sqlite3 *)handle;
1408 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1409 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1411 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1416 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1418 int ret = MS_MEDIA_ERR_NONE;
1419 sqlite3 * db_handle = (sqlite3 *)handle;
1421 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1423 ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1428 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1430 sqlite3 * db_handle = (sqlite3 *)handle;
1432 media_svc_debug_fenter();
1434 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1435 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1437 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1440 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1442 int ret = MS_MEDIA_ERR_NONE;
1444 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1445 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1447 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1448 new_content_info->size = content_info->size;
1449 new_content_info->modified_time = content_info->modified_time;
1450 new_content_info->is_drm = content_info->is_drm;
1451 new_content_info->media_type = content_info->media_type;
1453 if (STRING_VALID(content_info->mime_type)) {
1454 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1455 if (ret != MS_MEDIA_ERR_NONE)
1456 media_svc_error("strcpy mime_type failed");
1459 if (STRING_VALID(content_info->thumbnail_path)) {
1460 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1461 if (ret != MS_MEDIA_ERR_NONE)
1462 media_svc_error("strcpy thumbnail_path failed");
1465 new_content_info->media_meta.duration = content_info->media_meta.duration;
1466 new_content_info->media_meta.width = content_info->media_meta.width;
1467 new_content_info->media_meta.height = content_info->media_meta.height;
1470 if (content_info->added_time > 0)
1471 new_content_info->added_time = content_info->added_time;
1472 new_content_info->last_played_time = content_info->last_played_time;
1473 new_content_info->played_count = content_info->played_count;
1474 new_content_info->favourate = content_info->favourate;
1476 if (STRING_VALID(content_info->file_name)) {
1477 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1478 if (ret != MS_MEDIA_ERR_NONE)
1479 media_svc_error("strcpy file_name failed");
1482 if (STRING_VALID(content_info->media_meta.title)) {
1483 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1484 if (ret != MS_MEDIA_ERR_NONE)
1485 media_svc_error("strcpy title failed");
1488 if (STRING_VALID(content_info->media_meta.album)) {
1489 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1490 if (ret != MS_MEDIA_ERR_NONE)
1491 media_svc_error("strcpy album failed");
1494 if (STRING_VALID(content_info->media_meta.artist)) {
1495 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1496 if (ret != MS_MEDIA_ERR_NONE)
1497 media_svc_error("strcpy artist failed");
1500 if (STRING_VALID(content_info->media_meta.genre)) {
1501 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1502 if (ret != MS_MEDIA_ERR_NONE)
1503 media_svc_error("strcpy genre failed");
1506 if (STRING_VALID(content_info->media_meta.composer)) {
1507 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1508 if (ret != MS_MEDIA_ERR_NONE)
1509 media_svc_error("strcpy composer failed");
1512 if (STRING_VALID(content_info->media_meta.year)) {
1513 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1514 if (ret != MS_MEDIA_ERR_NONE)
1515 media_svc_error("strcpy year failed");
1518 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1519 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1520 if (ret != MS_MEDIA_ERR_NONE)
1521 media_svc_error("strcpy recorded_date failed");
1524 if (STRING_VALID(content_info->media_meta.copyright)) {
1525 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1526 if (ret != MS_MEDIA_ERR_NONE)
1527 media_svc_error("strcpy copyright failed");
1530 if (STRING_VALID(content_info->media_meta.track_num)) {
1531 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1532 if (ret != MS_MEDIA_ERR_NONE)
1533 media_svc_error("strcpy track_num failed");
1536 if (STRING_VALID(content_info->media_meta.description)) {
1537 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1538 if (ret != MS_MEDIA_ERR_NONE)
1539 media_svc_error("strcpy description failed");
1542 if (STRING_VALID(content_info->media_meta.weather)) {
1543 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1544 if (ret != MS_MEDIA_ERR_NONE)
1545 media_svc_error("strcpy weather failed");
1548 if (STRING_VALID(content_info->media_meta.category)) {
1549 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1550 if (ret != MS_MEDIA_ERR_NONE)
1551 media_svc_error("strcpy category failed");
1554 if (STRING_VALID(content_info->media_meta.keyword)) {
1555 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1556 if (ret != MS_MEDIA_ERR_NONE)
1557 media_svc_error("strcpy keyword failed");
1560 if (STRING_VALID(content_info->media_meta.location_tag)) {
1561 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1562 if (ret != MS_MEDIA_ERR_NONE)
1563 media_svc_error("strcpy location_tag failed");
1566 if (STRING_VALID(content_info->media_meta.content_name)) {
1567 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1568 if (ret != MS_MEDIA_ERR_NONE)
1569 media_svc_error("strcpy content_name failed");
1572 if (STRING_VALID(content_info->media_meta.age_rating)) {
1573 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1574 if (ret != MS_MEDIA_ERR_NONE)
1575 media_svc_error("strcpy age_rating failed");
1578 if (STRING_VALID(content_info->media_meta.author)) {
1579 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1580 if (ret != MS_MEDIA_ERR_NONE)
1581 media_svc_error("strcpy author failed");
1584 if (STRING_VALID(content_info->media_meta.provider)) {
1585 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1586 if (ret != MS_MEDIA_ERR_NONE)
1587 media_svc_error("strcpy provider failed");
1590 if (STRING_VALID(content_info->media_meta.datetaken)) {
1591 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1592 if (ret != MS_MEDIA_ERR_NONE)
1593 media_svc_error("strcpy datetaken failed");
1595 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1596 if (new_content_info->timeline == 0) {
1597 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1598 new_content_info->timeline = new_content_info->modified_time;
1600 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1604 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1605 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1606 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1607 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1609 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1610 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1611 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1612 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1613 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1614 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1616 new_content_info->media_meta.rating = content_info->media_meta.rating;
1621 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1623 int ret = MS_MEDIA_ERR_NONE;
1624 sqlite3 *db_handle = (sqlite3 *)handle;
1625 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1626 bool append_album = FALSE;
1628 /* Checking parameters if they are valid */
1629 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1630 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1631 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1633 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1634 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1636 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1637 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1638 return MS_MEDIA_ERR_INVALID_PARAMETER;
1641 if (content_info->size <= 0) {
1642 media_svc_error("invalid size condition[%d]", content_info->size);
1643 return MS_MEDIA_ERR_INVALID_PARAMETER;
1646 if (content_info->modified_time <= 0) {
1647 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1648 return MS_MEDIA_ERR_INVALID_PARAMETER;
1652 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1654 media_svc_content_info_s _new_content_info;
1655 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1657 media_svc_media_type_e media_type;
1659 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1660 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1662 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1663 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1665 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1666 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1668 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1671 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1673 /* Extracting thumbnail */
1674 int ini_val = _media_svc_get_ini_value();
1676 if (_new_content_info.thumbnail_path == NULL) {
1677 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1678 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1682 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1683 if (ret == MS_MEDIA_ERR_NONE) {
1684 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1687 if (_new_content_info.media_meta.width <= 0)
1688 _new_content_info.media_meta.width = width;
1690 if (_new_content_info.media_meta.height <= 0)
1691 _new_content_info.media_meta.height = height;
1697 /* set othere data to the structure, which is passed as parameters */
1698 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1700 /* Set or Get folder id */
1701 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);
1702 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1704 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1705 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1707 /* register album table data */
1710 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1711 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1713 if (ret != MS_MEDIA_ERR_NONE) {
1714 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1715 media_svc_debug("album does not exist. So start to make album art");
1716 append_album = TRUE;
1718 media_svc_debug("other error");
1719 append_album = FALSE;
1722 _new_content_info.album_id = album_id;
1723 append_album = FALSE;
1725 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1726 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1728 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1731 media_svc_debug("album already exists. don't need to make album art");
1732 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1733 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1738 if (append_album == TRUE) {
1740 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1741 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1742 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);
1744 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1746 _new_content_info.album_id = album_id;
1749 /* Insert to db - calling _media_svc_insert_item_with_data */
1750 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1752 if (ret == MS_MEDIA_ERR_NONE) {
1753 media_svc_debug("Insertion is successful.");
1756 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1757 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1760 _media_svc_destroy_content_info(&_new_content_info);
1762 /* handling returned value - important */
1766 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1768 _media_svc_destroy_content_info(content_info);
1771 int media_svc_generate_uuid(char **uuid)
1773 char *gen_uuid = NULL;
1774 gen_uuid = _media_info_generate_uuid();
1775 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1777 *uuid = strdup(gen_uuid);
1779 return MS_MEDIA_ERR_NONE;
1782 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1784 sqlite3 * db_handle = (sqlite3 *)handle;
1786 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1788 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1791 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1793 sqlite3 * db_handle = (sqlite3 *)handle;
1795 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1796 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1797 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1798 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1800 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1803 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1805 sqlite3 * db_handle = (sqlite3 *)handle;
1807 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1808 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1809 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1811 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1814 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)
1816 int ret = MS_MEDIA_ERR_NONE;
1817 sqlite3 *db_handle = (sqlite3 *)handle;
1819 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1820 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1821 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1822 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1824 ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1825 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1827 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1828 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1829 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1831 ret = _media_svc_update_media_view(db_handle, uid);
1832 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1838 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1840 int ret = MS_MEDIA_ERR_NONE;
1841 sqlite3 *db_handle = (sqlite3 *)handle;
1842 media_svc_storage_type_e storage_type;
1844 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1845 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1847 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1848 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1850 ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1851 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1853 ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1854 if (ret != MS_MEDIA_ERR_NONE) {
1855 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1858 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1859 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1860 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1862 ret = _media_svc_update_media_view(db_handle, uid);
1863 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1869 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1871 sqlite3 * db_handle = (sqlite3 *)handle;
1873 media_svc_debug("Transaction data count : [%d]", data_cnt);
1875 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1876 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1878 g_media_svc_insert_folder_data_cnt = data_cnt;
1879 g_media_svc_insert_folder_cur_data_cnt = 0;
1881 return MS_MEDIA_ERR_NONE;
1884 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1886 int ret = MS_MEDIA_ERR_NONE;
1887 sqlite3 * db_handle = (sqlite3 *)handle;
1889 media_svc_debug_fenter();
1891 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1893 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1895 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1898 g_media_svc_insert_folder_data_cnt = 1;
1899 g_media_svc_insert_folder_cur_data_cnt = 0;
1904 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1906 int ret = MS_MEDIA_ERR_NONE;
1907 sqlite3 * db_handle = (sqlite3 *)handle;
1908 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1910 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1911 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1912 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1913 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1915 if (g_media_svc_insert_folder_data_cnt == 1) {
1917 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1919 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1921 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1922 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1924 g_media_svc_insert_folder_cur_data_cnt++;
1926 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1928 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1929 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1931 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1932 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1934 g_media_svc_insert_folder_cur_data_cnt = 0;
1937 media_svc_error("Error in media_svc_set_insert_folder");
1938 return MS_MEDIA_ERR_INTERNAL;
1944 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1946 int ret = MS_MEDIA_ERR_NONE;
1947 sqlite3 * db_handle = (sqlite3 *)handle;
1949 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1951 ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
1956 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1958 int ret = MS_MEDIA_ERR_NONE;
1959 sqlite3 * db_handle = (sqlite3 *)handle;
1961 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1963 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1968 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)
1970 int ret = MS_MEDIA_ERR_NONE;
1971 sqlite3 * db_handle = (sqlite3 *)handle;
1972 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1973 media_svc_media_type_e media_type;
1975 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1976 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1977 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1978 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1980 media_svc_content_info_s content_info;
1981 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1983 /*Set basic media info*/
1984 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1985 if (ret != MS_MEDIA_ERR_NONE) {
1986 media_svc_error("_media_svc_set_media_info fail %d", ret);
1990 //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);
1992 /*Set or Get folder id*/
1993 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1994 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1996 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1997 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1999 if (g_media_svc_insert_item_data_cnt == 1) {
2001 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2002 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2004 if (g_insert_with_noti)
2005 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2006 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2008 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2009 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2011 if (g_insert_with_noti)
2012 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2014 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2015 g_media_svc_insert_item_cur_data_cnt++;
2017 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2019 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2020 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2022 if (g_insert_with_noti)
2023 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2025 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2026 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2028 media_svc_debug("_media_svc_list_query_do over");
2030 if (g_insert_with_noti) {
2031 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2032 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2033 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2035 /* Recreate noti list */
2036 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2037 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2040 g_media_svc_insert_item_cur_data_cnt = 0;
2043 media_svc_error("Error in media_svc_insert_item_pass1");
2044 _media_svc_destroy_content_info(&content_info);
2045 return MS_MEDIA_ERR_INTERNAL;
2048 _media_svc_destroy_content_info(&content_info);
2050 return MS_MEDIA_ERR_NONE;
2053 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)
2055 int ret = MS_MEDIA_ERR_NONE;
2056 sqlite3 * db_handle = (sqlite3 *)handle;
2057 sqlite3_stmt *sql_stmt = NULL;
2058 media_svc_media_type_e media_type;
2059 media_svc_content_info_s content_info;
2060 GArray *db_data_array = NULL;
2061 media_svc_item_info_s *db_data = NULL;
2065 media_svc_debug_fenter();
2067 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2068 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2069 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2071 if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2072 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2073 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2074 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2075 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2076 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2078 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2079 } else { /*Storage Scanning*/
2080 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2083 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2084 if (ret != MS_MEDIA_ERR_NONE) {
2085 media_svc_error("error when get list. err = [%d]", ret);
2089 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2090 if (db_data_array == NULL) {
2091 media_svc_error("db_data_array is NULL. Out of memory");
2092 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2095 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2097 db_data = malloc(sizeof(media_svc_item_info_s));
2098 if (db_data == NULL) {
2099 media_svc_error("Out of memory");
2103 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2104 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2106 g_array_append_val(db_data_array, db_data);
2109 SQLITE3_FINALIZE(sql_stmt);
2111 while (db_data_array->len != 0) {
2113 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2114 g_array_remove_index(db_data_array, 0);
2116 if ((db_data == NULL) || (db_data->path == NULL)) {
2117 media_svc_error("invalid db data");
2121 media_type = db_data->media_type;
2122 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2123 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2124 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2126 content_info.media_type = media_type;
2127 content_info.storage_type = storage_type;
2129 _media_svc_set_default_value(&content_info, FALSE);
2131 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2133 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2134 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2136 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2139 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2141 _media_svc_destroy_content_info(&content_info);
2142 SAFE_FREE(db_data->path);
2148 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2150 g_array_free(db_data_array, FALSE);
2151 db_data_array = NULL;
2153 media_svc_debug_fleave();
2155 return MS_MEDIA_ERR_NONE;
2158 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2160 int ret = MS_MEDIA_ERR_NONE;
2161 sqlite3 * db_handle = (sqlite3 *)handle;
2163 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2165 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2170 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2172 int ret = MS_MEDIA_ERR_NONE;
2173 sqlite3 * db_handle = (sqlite3 *)handle;
2176 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2177 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2178 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2180 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2181 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2184 media_svc_debug("item is exist in database");
2185 return MS_MEDIA_ERR_NONE;
2187 media_svc_debug("item is not exist in database");
2188 return MS_MEDIA_ERR_DB_NO_RECORD;
2191 return MS_MEDIA_ERR_NONE;
2194 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2196 int ret = MS_MEDIA_ERR_NONE;
2197 sqlite3 * db_handle = (sqlite3 *)handle;
2200 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2201 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2202 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2205 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2206 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2210 media_svc_debug("item is exist in database");
2211 return MS_MEDIA_ERR_NONE;
2213 media_svc_debug("item is not exist in database");
2214 return MS_MEDIA_ERR_DB_NO_RECORD;
2217 return MS_MEDIA_ERR_NONE;
2220 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2222 int ret = MS_MEDIA_ERR_NONE;
2223 sqlite3 * db_handle = (sqlite3 *)handle;
2225 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2226 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2228 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);