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 BATCH_REQUEST_MAX 300
58 } media_svc_item_info_s;
60 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type)
62 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
63 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
64 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
65 media_svc_error("storage type is incorrect[%d]", storage_type);
72 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
74 int ret = MS_MEDIA_ERR_NONE;
75 MediaDBHandle *db_handle = NULL;
77 media_svc_debug_fenter();
79 ret = media_db_connect(&db_handle, uid, need_write);
80 if (ret != MS_MEDIA_ERR_NONE)
84 return MS_MEDIA_ERR_NONE;
87 int media_svc_disconnect(MediaSvcHandle *handle)
89 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
91 media_svc_debug_fenter();
93 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
95 int ret = MS_MEDIA_ERR_NONE;
97 ret = media_db_disconnect(db_handle);
101 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
103 sqlite3 *db_handle = (sqlite3 *)handle;
105 return _media_svc_get_user_version(db_handle, user_version);
108 int media_svc_create_table(uid_t uid)
110 int ret = MS_MEDIA_ERR_NONE;
113 media_svc_debug_fenter();
115 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
116 if (ret != MS_MEDIA_ERR_NONE) {
117 media_svc_error("_media_svc_init_table_query fail.");
121 /*create media table*/
122 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
123 if (ret != MS_MEDIA_ERR_NONE) {
124 media_svc_error("_media_svc_make_table_query fail.");
128 /*create folder table*/
129 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
130 if (ret != MS_MEDIA_ERR_NONE) {
131 media_svc_error("_media_svc_make_table_query fail.");
135 /*create playlist_map table*/
136 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
137 if (ret != MS_MEDIA_ERR_NONE) {
138 media_svc_error("_media_svc_make_table_query fail.");
142 /*create playlist table*/
143 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
144 if (ret != MS_MEDIA_ERR_NONE) {
145 media_svc_error("_media_svc_make_table_query fail.");
149 /* create album table*/
150 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
151 if (ret != MS_MEDIA_ERR_NONE) {
152 media_svc_error("_media_svc_make_table_query fail.");
156 /*create tag_map table*/
157 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
158 if (ret != MS_MEDIA_ERR_NONE) {
159 media_svc_error("_media_svc_make_table_query fail.");
164 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
165 if (ret != MS_MEDIA_ERR_NONE) {
166 media_svc_error("_media_svc_make_table_query fail.");
170 /*create bookmark table*/
171 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
172 if (ret != MS_MEDIA_ERR_NONE) {
173 media_svc_error("_media_svc_make_table_query fail.");
177 /*create storage table from tizen 2.4 */
178 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
179 if (ret != MS_MEDIA_ERR_NONE) {
180 media_svc_error("_media_svc_make_table_query fail.");
184 /*create uhd table from tizen 3.0 */
185 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_UHD, MEDIA_SVC_DB_LIST_UHD, uid);
186 if (ret != MS_MEDIA_ERR_NONE) {
187 media_svc_error("_media_svc_make_table_query fail.");
191 /*create pvr table from tizen 3.0 */
192 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PVR, MEDIA_SVC_DB_LIST_PVR, uid);
193 if (ret != MS_MEDIA_ERR_NONE) {
194 media_svc_error("_media_svc_make_table_query fail.");
198 /*init storage table*/
199 ret = _media_svc_init_storage(db_handle, uid);
200 if (ret != MS_MEDIA_ERR_NONE) {
201 media_svc_error("_media_svc_make_table_query fail.");
206 /*create face table. from tizen 3.0*/
207 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
208 if (ret != MS_MEDIA_ERR_NONE) {
209 media_svc_error("_media_svc_make_table_query fail.");
213 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
214 if (ret != MS_MEDIA_ERR_NONE) {
215 media_svc_error("_media_svc_make_table_query fail.");
219 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
220 ret = _media_svc_sql_query(sql, uid);
221 if (ret != MS_MEDIA_ERR_NONE) {
222 media_svc_error("user_version update fail.");
226 _media_svc_destroy_table_query();
228 media_svc_debug_fleave();
230 return MS_MEDIA_ERR_NONE;
232 _media_svc_destroy_table_query();
234 media_svc_debug_fleave();
239 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
241 int ret = MS_MEDIA_ERR_NONE;
242 ms_user_storage_type_t type = -1;
244 ret = ms_user_get_storage_type(uid, path, &type);
245 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret);
247 *storage_type = type;
252 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
254 int ret = MS_MEDIA_ERR_NONE;
255 sqlite3 *db_handle = (sqlite3 *)handle;
257 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
258 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
260 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
265 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
267 int ret = MS_MEDIA_ERR_NONE;
268 sqlite3 *db_handle = (sqlite3 *)handle;
271 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
272 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
273 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
275 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
276 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
279 media_svc_debug("item is exist in database");
280 return MS_MEDIA_ERR_NONE;
282 media_svc_debug("item is not exist in database");
283 return MS_MEDIA_ERR_DB_NO_RECORD;
286 return MS_MEDIA_ERR_NONE;
289 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
291 media_svc_debug("Transaction data count : [%d]", data_cnt);
293 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
295 g_media_svc_insert_item_data_cnt = data_cnt;
296 g_media_svc_insert_item_cur_data_cnt = 0;
298 /* Prepare for making noti item list */
300 media_svc_debug("making noti list from pid[%d]", from_pid);
301 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
302 return MS_MEDIA_ERR_OUT_OF_MEMORY;
304 _media_svc_set_noti_from_pid(from_pid);
305 g_insert_with_noti = TRUE;
308 return MS_MEDIA_ERR_NONE;
311 int media_svc_insert_item_end(uid_t uid)
313 int ret = MS_MEDIA_ERR_NONE;
315 media_svc_debug_fenter();
317 if (g_media_svc_insert_item_cur_data_cnt > 0) {
319 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
320 if (g_insert_with_noti) {
321 media_svc_debug("sending noti list");
322 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
323 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
324 g_insert_with_noti = FALSE;
325 _media_svc_set_noti_from_pid(-1);
329 g_media_svc_insert_item_data_cnt = 1;
330 g_media_svc_insert_item_cur_data_cnt = 0;
335 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)
337 int ret = MS_MEDIA_ERR_NONE;
338 sqlite3 *db_handle = (sqlite3 *)handle;
339 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
340 media_svc_media_type_e media_type;
342 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
343 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
344 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
345 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
347 media_svc_content_info_s content_info;
348 memset(&content_info, 0, sizeof(media_svc_content_info_s));
351 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
352 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
353 if (ret != MS_MEDIA_ERR_NONE)
356 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
357 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
358 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
359 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
360 media_svc_debug("Do nothing[%d]", media_type);
361 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
362 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
364 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
365 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
367 /*Set or Get folder id*/
368 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
369 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
371 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
372 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
374 if (g_media_svc_insert_item_data_cnt == 1) {
376 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
377 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
379 if (g_insert_with_noti)
380 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
382 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
384 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
385 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
387 if (g_insert_with_noti)
388 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
390 g_media_svc_insert_item_cur_data_cnt++;
392 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
394 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
395 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
397 if (g_insert_with_noti)
398 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
400 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
401 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
403 if (g_insert_with_noti) {
404 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
405 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
407 /* Recreate noti list */
408 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
409 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
412 g_media_svc_insert_item_cur_data_cnt = 0;
415 media_svc_error("Error in media_svc_insert_item_bulk");
416 _media_svc_destroy_content_info(&content_info);
417 return MS_MEDIA_ERR_INTERNAL;
420 _media_svc_destroy_content_info(&content_info);
422 return MS_MEDIA_ERR_NONE;
425 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)
427 int ret = MS_MEDIA_ERR_NONE;
428 sqlite3 *db_handle = (sqlite3 *)handle;
429 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
430 media_svc_media_type_e media_type;
432 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
433 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
434 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
435 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
437 media_svc_content_info_s content_info;
438 memset(&content_info, 0, sizeof(media_svc_content_info_s));
441 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
442 if (ret != MS_MEDIA_ERR_NONE)
445 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
446 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
447 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
448 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
450 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
451 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
453 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
456 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
458 /*Set or Get folder id*/
459 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
460 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
462 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
463 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
465 /* Extracting thumbnail */
466 if (content_info.thumbnail_path == NULL) {
467 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
468 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
472 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
473 if (ret == MS_MEDIA_ERR_NONE)
474 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
476 if (content_info.media_meta.width <= 0)
477 content_info.media_meta.width = width;
479 if (content_info.media_meta.height <= 0)
480 content_info.media_meta.height = height;
484 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
486 if (ret == MS_MEDIA_ERR_NONE) {
487 media_svc_debug("Insertion is successful. Sending noti for this");
488 _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);
489 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
490 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
493 _media_svc_destroy_content_info(&content_info);
497 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
498 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
500 int ret = MS_MEDIA_ERR_NONE;
501 sqlite3 *db_handle = (sqlite3 *)handle;
502 char *file_name = NULL;
503 char *folder_path = NULL;
504 int modified_time = 0;
505 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
506 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
507 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
510 media_svc_debug_fenter();
512 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
513 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
514 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
515 media_svc_retvm_if(__media_svc_check_storage(src_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
516 media_svc_retvm_if(__media_svc_check_storage(dest_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
518 /*check and update folder*/
519 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
520 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
523 file_name = g_path_get_basename(dest_path);
525 /*get modified_time*/
526 modified_time = _media_svc_get_file_time(dest_path);
528 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
529 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
530 if (ret != MS_MEDIA_ERR_NONE) {
531 media_svc_error("_media_svc_get_media_type_by_path failed");
532 SAFE_FREE(file_name);
536 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
537 /*get old thumbnail_path*/
538 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
539 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
540 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
541 SAFE_FREE(file_name);
545 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
548 if (g_media_svc_move_item_data_cnt == 1) {
551 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
552 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
554 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
556 SAFE_FREE(file_name);
557 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
559 media_svc_debug("Move is successful. Sending noti for this");
561 /* Get notification info */
562 media_svc_noti_item *noti_item = NULL;
563 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
564 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
566 /* Send notification for move */
567 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
568 _media_svc_destroy_noti_item(noti_item);
570 /*update folder modified_time*/
571 folder_path = g_path_get_dirname(dest_path);
572 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
573 SAFE_FREE(folder_path);
574 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
576 ret = _media_svc_update_folder_table(storage_id, uid);
577 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
579 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
582 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
583 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
585 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
587 SAFE_FREE(file_name);
588 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
590 /*update folder modified_time*/
591 folder_path = g_path_get_dirname(dest_path);
592 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
593 SAFE_FREE(folder_path);
594 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
596 g_media_svc_move_item_cur_data_cnt++;
598 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
600 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
601 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
603 ret = _media_svc_update_item_by_path(storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
605 SAFE_FREE(file_name);
606 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
608 /*update folder modified_time*/
609 folder_path = g_path_get_dirname(dest_path);
610 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
611 SAFE_FREE(folder_path);
612 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
615 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
616 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
618 g_media_svc_move_item_cur_data_cnt = 0;
621 media_svc_error("Error in media_svc_move_item");
622 SAFE_FREE(file_name);
623 return MS_MEDIA_ERR_INTERNAL;
626 /*rename thumbnail file*/
627 if (STRING_VALID(old_thumb_path)) {
628 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
629 if (ret != MS_MEDIA_ERR_NONE)
630 media_svc_error("_media_svc_rename_file failed : %d", ret);
633 return MS_MEDIA_ERR_NONE;
636 int media_svc_set_item_validity_begin(int data_cnt)
638 media_svc_debug("Transaction data count : [%d]", data_cnt);
640 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
642 g_media_svc_item_validity_data_cnt = data_cnt;
643 g_media_svc_item_validity_cur_data_cnt = 0;
645 return MS_MEDIA_ERR_NONE;
648 int media_svc_set_item_validity_end(uid_t uid)
650 int ret = MS_MEDIA_ERR_NONE;
652 media_svc_debug_fenter();
654 if (g_media_svc_item_validity_cur_data_cnt > 0)
655 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
657 g_media_svc_item_validity_data_cnt = 1;
658 g_media_svc_item_validity_cur_data_cnt = 0;
663 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
665 int ret = MS_MEDIA_ERR_NONE;
667 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
668 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
670 media_svc_debug("path=[%s], validity=[%d]", path, validity);
672 if (g_media_svc_item_validity_data_cnt == 1) {
674 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
676 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
678 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
679 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
681 g_media_svc_item_validity_cur_data_cnt++;
683 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
685 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
686 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
688 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
689 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
691 g_media_svc_item_validity_cur_data_cnt = 0;
695 media_svc_error("Error in media_svc_set_item_validity");
696 return MS_MEDIA_ERR_INTERNAL;
699 return MS_MEDIA_ERR_NONE;
702 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
704 int ret = MS_MEDIA_ERR_NONE;
705 sqlite3 *db_handle = (sqlite3 *)handle;
706 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
708 media_svc_debug_fenter();
710 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
711 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
712 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
715 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
716 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
719 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
720 /*Get thumbnail path to delete*/
721 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
722 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
723 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
725 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
726 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
729 /*Get thumbnail path to delete*/
730 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
731 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
735 /*Get thumbnail path to delete*/
736 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
737 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
739 if (g_media_svc_insert_item_data_cnt == 1) {
741 /* Get notification info */
742 media_svc_noti_item *noti_item = NULL;
743 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
744 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
747 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
748 if (ret != MS_MEDIA_ERR_NONE) {
749 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
750 _media_svc_destroy_noti_item(noti_item);
755 /* Send notification */
756 media_svc_debug("Deletion is successful. Sending noti for this");
757 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
758 _media_svc_destroy_noti_item(noti_item);
760 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
761 if (ret != MS_MEDIA_ERR_NONE) {
762 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
769 if (STRING_VALID(thumb_path)) {
770 ret = _media_svc_remove_file(thumb_path);
771 if (ret != MS_MEDIA_ERR_NONE)
772 media_svc_error("fail to remove thumbnail file.");
775 return MS_MEDIA_ERR_NONE;
778 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
780 int ret = MS_MEDIA_ERR_NONE;
782 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
784 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
785 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
787 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
788 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
790 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
791 char *internal_thumb_path = NULL;
792 char *external_thumb_path = NULL;
794 ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
795 ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
797 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
798 media_svc_error("fail to get thumbnail path");
799 SAFE_FREE(internal_thumb_path);
800 SAFE_FREE(external_thumb_path);
801 return MS_MEDIA_ERR_INTERNAL;
803 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
805 /* remove thumbnails */
806 if (STRING_VALID(dirpath))
807 ret = _media_svc_remove_all_files_in_dir(dirpath);
808 SAFE_FREE(internal_thumb_path);
809 SAFE_FREE(external_thumb_path);
810 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
813 return MS_MEDIA_ERR_NONE;
816 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
818 sqlite3 *db_handle = (sqlite3 *)handle;
820 media_svc_debug_fenter();
822 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
823 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
824 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
826 /*Delete from DB and remove thumbnail files*/
827 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
830 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
832 sqlite3 *db_handle = (sqlite3 *)handle;
834 media_svc_debug_fenter();
836 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
837 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
839 /*Delete from DB and remove thumbnail files*/
840 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
843 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
845 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
846 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
848 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
851 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
853 sqlite3 *db_handle = (sqlite3 *)handle;
855 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
856 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
857 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
860 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
862 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
865 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
867 int ret = MS_MEDIA_ERR_NONE;
868 sqlite3 *db_handle = (sqlite3 *)handle;
869 media_svc_media_type_e media_type;
871 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
872 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
873 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
874 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
876 media_svc_content_info_s content_info;
877 memset(&content_info, 0, sizeof(media_svc_content_info_s));
880 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
881 if (ret != MS_MEDIA_ERR_NONE)
884 /* Initialize thumbnail information to remake thumbnail. */
885 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
886 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
887 if (ret != MS_MEDIA_ERR_NONE) {
888 _media_svc_destroy_content_info(&content_info);
892 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
893 ret = _media_svc_remove_file(thumb_path);
894 if (ret != MS_MEDIA_ERR_NONE)
895 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
898 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
899 if (ret != MS_MEDIA_ERR_NONE) {
900 _media_svc_destroy_content_info(&content_info);
904 /* Get notification info */
905 media_svc_noti_item *noti_item = NULL;
906 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
907 if (ret != MS_MEDIA_ERR_NONE) {
908 _media_svc_destroy_content_info(&content_info);
912 media_type = noti_item->media_type;
913 content_info.media_type = media_type;
915 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
916 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
917 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
918 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
919 media_svc_debug("Do nothing [%d]", media_type);
920 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
921 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
923 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
925 if (ret != MS_MEDIA_ERR_NONE) {
926 _media_svc_destroy_noti_item(noti_item);
927 _media_svc_destroy_content_info(&content_info);
931 /* Extracting thumbnail */
932 if (content_info.thumbnail_path == NULL) {
933 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
934 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
938 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
939 if (ret == MS_MEDIA_ERR_NONE)
940 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
942 if (content_info.media_meta.width <= 0)
943 content_info.media_meta.width = width;
945 if (content_info.media_meta.height <= 0)
946 content_info.media_meta.height = height;
950 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
952 if (ret == MS_MEDIA_ERR_NONE) {
953 media_svc_debug("Update is successful. Sending noti for this");
954 _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);
956 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
959 _media_svc_destroy_content_info(&content_info);
960 _media_svc_destroy_noti_item(noti_item);
965 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
967 sqlite3 *db_handle = (sqlite3 *)handle;
968 int ret = MS_MEDIA_ERR_NONE;
970 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
971 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
972 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
974 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
976 ret = _media_svc_sql_begin_trans(uid);
977 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
979 /* Update all folder record's modified date, which are changed above */
980 char *update_folder_modified_time_sql = NULL;
984 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
986 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
987 SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
989 if (ret != SQLITE_OK) {
990 media_svc_error("failed to update folder modified time");
991 _media_svc_sql_rollback_trans(uid);
993 return MS_MEDIA_ERR_DB_INTERNAL;
996 /* Update all items */
997 char *select_all_sql = NULL;
998 sqlite3_stmt *sql_stmt = NULL;
999 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1001 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1003 select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from '%q' where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", storage_id, dst_path, dst_child_path);
1005 media_svc_debug("[SQL query] : %s", select_all_sql);
1007 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1008 if (ret != MS_MEDIA_ERR_NONE) {
1009 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1010 _media_svc_sql_rollback_trans(uid);
1014 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1015 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1016 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1017 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1018 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1019 /*int media_type; */
1020 bool no_thumb = FALSE;
1022 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1023 _strncpy_safe(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1025 media_svc_error("media UUID is NULL");
1026 return MS_MEDIA_ERR_DB_INTERNAL;
1029 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1030 _strncpy_safe(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1032 media_svc_error("media path is NULL");
1033 return MS_MEDIA_ERR_DB_INTERNAL;
1036 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1037 _strncpy_safe(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1039 media_svc_debug("media thumb path doesn't exist in DB");
1043 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1045 /* Update path, thumbnail path of this item */
1046 char *replaced_path = NULL;
1047 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1048 if (replaced_path == NULL) {
1049 media_svc_error("_media_svc_replace_path failed");
1050 SQLITE3_FINALIZE(sql_stmt);
1051 _media_svc_sql_rollback_trans(uid);
1052 return MS_MEDIA_ERR_INTERNAL;
1055 media_svc_debug("New media path : %s", replaced_path);
1056 ms_user_storage_type_t storage_type = -1;
1059 ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
1060 if (ret != MS_MEDIA_ERR_NONE) {
1061 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
1062 SAFE_FREE(replaced_path);
1063 _media_svc_sql_rollback_trans(uid);
1067 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1068 if (ret != MS_MEDIA_ERR_NONE) {
1069 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1070 SAFE_FREE(replaced_path);
1071 SQLITE3_FINALIZE(sql_stmt);
1072 _media_svc_sql_rollback_trans(uid);
1076 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1079 char *update_item_sql = NULL;
1082 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1085 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1086 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
1088 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_thumb_path, media_uuid);
1090 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_new_thumb_path, media_uuid);
1094 ret = media_db_request_update_db_batch(update_item_sql, uid);
1095 SQLITE3_SAFE_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(uid);
1103 return MS_MEDIA_ERR_DB_INTERNAL;
1106 /* Rename thumbnail file of file system */
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);
1115 SQLITE3_FINALIZE(sql_stmt);
1117 ret = _media_svc_sql_end_trans(uid);
1118 if (ret != MS_MEDIA_ERR_NONE) {
1119 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1120 _media_svc_sql_rollback_trans(uid);
1124 media_svc_debug("Folder update is successful. Sending noti for this");
1125 /* Get notification info */
1126 media_svc_noti_item *noti_item = NULL;
1127 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1128 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1130 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1131 _media_svc_destroy_noti_item(noti_item);
1133 return MS_MEDIA_ERR_NONE;
1136 int media_svc_request_update_db(const char *db_query, uid_t uid)
1138 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1140 return _media_svc_sql_query(db_query, uid);
1143 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)
1145 int ret = MS_MEDIA_ERR_NONE;
1146 sqlite3 *db_handle = (sqlite3 *)handle;
1149 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1150 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1152 /* Get notification info */
1153 media_svc_noti_item *noti_item = NULL;
1154 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1155 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1157 if (folder_id != NULL) {
1158 uuid = strndup(folder_id, strlen(folder_id));
1160 if (noti_item->media_uuid != NULL) {
1161 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1163 _media_svc_destroy_noti_item(noti_item);
1164 media_svc_error("folder uuid is wrong");
1165 return MS_MEDIA_ERR_DB_INTERNAL;
1169 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1170 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1171 _media_svc_destroy_noti_item(noti_item);
1177 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1179 sqlite3 *db_handle = (sqlite3 *)handle;
1181 media_svc_debug_fenter();
1183 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1184 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1185 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1186 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1188 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1191 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1193 sqlite3 *db_handle = (sqlite3 *)handle;
1195 media_svc_debug_fenter();
1197 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1199 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1202 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1204 sqlite3 *db_handle = (sqlite3 *)handle;
1206 media_svc_debug_fenter();
1208 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1210 return _media_db_check_corrupt(db_handle);
1213 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)
1215 sqlite3 *db_handle = (sqlite3 *)handle;
1217 media_svc_debug_fenter();
1219 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1220 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1222 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1225 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1227 int ret = MS_MEDIA_ERR_NONE;
1228 sqlite3 *db_handle = (sqlite3 *)handle;
1229 time_t sto_time = 0;
1230 int cur_time = _media_svc_get_file_time(folder_path);
1231 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1233 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1235 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1236 if (ret == MS_MEDIA_ERR_NONE) {
1237 if (sto_time != cur_time)
1238 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1244 int media_svc_update_item_begin(int data_cnt)
1246 media_svc_debug("Transaction data count : [%d]", data_cnt);
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(uid_t uid)
1258 int ret = MS_MEDIA_ERR_NONE;
1260 media_svc_debug_fenter();
1262 if (g_media_svc_update_item_cur_data_cnt > 0)
1263 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1265 g_media_svc_update_item_data_cnt = 1;
1266 g_media_svc_update_item_cur_data_cnt = 0;
1271 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1273 int ret = MS_MEDIA_ERR_NONE;
1274 sqlite3 *db_handle = (sqlite3 *)handle;
1276 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1278 media_svc_media_type_e media_type;
1279 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1280 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1282 media_svc_content_info_s content_info;
1283 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1286 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1287 if (ret != MS_MEDIA_ERR_NONE)
1290 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1291 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1293 _media_svc_destroy_content_info(&content_info);
1294 return MS_MEDIA_ERR_NONE;
1297 if (ret != MS_MEDIA_ERR_NONE) {
1298 _media_svc_destroy_content_info(&content_info);
1302 if (g_media_svc_update_item_data_cnt == 1) {
1304 ret = _media_svc_update_meta_with_data(&content_info);
1305 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1307 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1309 ret = _media_svc_update_meta_with_data(&content_info);
1310 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1312 g_media_svc_update_item_cur_data_cnt++;
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(&content_info);
1317 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1319 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1320 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1322 g_media_svc_update_item_cur_data_cnt = 0;
1325 media_svc_error("Error in media_svc_update_item_meta");
1326 _media_svc_destroy_content_info(&content_info);
1327 return MS_MEDIA_ERR_INTERNAL;
1330 _media_svc_destroy_content_info(&content_info);
1335 int media_svc_publish_noti(media_item_type_e update_item, media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
1337 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1340 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1342 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1344 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1347 int media_svc_check_pinyin_support(bool *support)
1349 *support = _media_svc_check_pinyin_support();
1351 return MS_MEDIA_ERR_NONE;
1354 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1356 int ret = MS_MEDIA_ERR_NONE;
1357 sqlite3 * db_handle = (sqlite3 *)handle;
1359 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1361 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1362 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1364 ret = _media_svc_update_media_view(db_handle, uid);
1365 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1370 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1372 int ret = MS_MEDIA_ERR_NONE;
1373 sqlite3 * db_handle = (sqlite3 *)handle;
1375 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1376 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1378 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1383 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1385 int ret = MS_MEDIA_ERR_NONE;
1386 sqlite3 * db_handle = (sqlite3 *)handle;
1388 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1389 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1391 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1396 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1398 int ret = MS_MEDIA_ERR_NONE;
1399 sqlite3 * db_handle = (sqlite3 *)handle;
1401 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1402 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1404 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1409 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1411 int ret = MS_MEDIA_ERR_NONE;
1413 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1418 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1420 sqlite3 * db_handle = (sqlite3 *)handle;
1422 media_svc_debug_fenter();
1424 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1425 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1427 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1430 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1432 int ret = MS_MEDIA_ERR_NONE;
1434 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1435 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1437 if (content_info->added_time > 0)
1438 new_content_info->added_time = content_info->added_time;
1439 new_content_info->last_played_time = content_info->last_played_time;
1440 new_content_info->played_count = content_info->played_count;
1441 new_content_info->favourate = content_info->favourate;
1443 if (STRING_VALID(content_info->file_name)) {
1444 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1445 if (ret != MS_MEDIA_ERR_NONE)
1446 media_svc_error("strcpy file_name failed");
1449 if (STRING_VALID(content_info->media_meta.title)) {
1450 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1451 if (ret != MS_MEDIA_ERR_NONE)
1452 media_svc_error("strcpy title failed");
1455 if (STRING_VALID(content_info->media_meta.album)) {
1456 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1457 if (ret != MS_MEDIA_ERR_NONE)
1458 media_svc_error("strcpy album failed");
1461 if (STRING_VALID(content_info->media_meta.artist)) {
1462 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1463 if (ret != MS_MEDIA_ERR_NONE)
1464 media_svc_error("strcpy artist failed");
1467 if (STRING_VALID(content_info->media_meta.genre)) {
1468 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1469 if (ret != MS_MEDIA_ERR_NONE)
1470 media_svc_error("strcpy genre failed");
1473 if (STRING_VALID(content_info->media_meta.composer)) {
1474 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1475 if (ret != MS_MEDIA_ERR_NONE)
1476 media_svc_error("strcpy composer failed");
1479 if (STRING_VALID(content_info->media_meta.year)) {
1480 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1481 if (ret != MS_MEDIA_ERR_NONE)
1482 media_svc_error("strcpy year failed");
1485 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1486 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1487 if (ret != MS_MEDIA_ERR_NONE)
1488 media_svc_error("strcpy recorded_date failed");
1491 if (STRING_VALID(content_info->media_meta.copyright)) {
1492 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1493 if (ret != MS_MEDIA_ERR_NONE)
1494 media_svc_error("strcpy copyright failed");
1497 if (STRING_VALID(content_info->media_meta.track_num)) {
1498 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1499 if (ret != MS_MEDIA_ERR_NONE)
1500 media_svc_error("strcpy track_num failed");
1503 if (STRING_VALID(content_info->media_meta.description)) {
1504 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1505 if (ret != MS_MEDIA_ERR_NONE)
1506 media_svc_error("strcpy description failed");
1509 if (STRING_VALID(content_info->media_meta.weather)) {
1510 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1511 if (ret != MS_MEDIA_ERR_NONE)
1512 media_svc_error("strcpy weather failed");
1515 if (STRING_VALID(content_info->media_meta.category)) {
1516 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1517 if (ret != MS_MEDIA_ERR_NONE)
1518 media_svc_error("strcpy category failed");
1521 if (STRING_VALID(content_info->media_meta.keyword)) {
1522 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1523 if (ret != MS_MEDIA_ERR_NONE)
1524 media_svc_error("strcpy keyword failed");
1527 if (STRING_VALID(content_info->media_meta.location_tag)) {
1528 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1529 if (ret != MS_MEDIA_ERR_NONE)
1530 media_svc_error("strcpy location_tag failed");
1533 if (STRING_VALID(content_info->media_meta.content_name)) {
1534 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1535 if (ret != MS_MEDIA_ERR_NONE)
1536 media_svc_error("strcpy content_name failed");
1539 if (STRING_VALID(content_info->media_meta.age_rating)) {
1540 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1541 if (ret != MS_MEDIA_ERR_NONE)
1542 media_svc_error("strcpy age_rating failed");
1545 if (STRING_VALID(content_info->media_meta.author)) {
1546 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1547 if (ret != MS_MEDIA_ERR_NONE)
1548 media_svc_error("strcpy author failed");
1551 if (STRING_VALID(content_info->media_meta.provider)) {
1552 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1553 if (ret != MS_MEDIA_ERR_NONE)
1554 media_svc_error("strcpy provider failed");
1557 if (STRING_VALID(content_info->media_meta.datetaken)) {
1558 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1559 if (ret != MS_MEDIA_ERR_NONE)
1560 media_svc_error("strcpy datetaken failed");
1562 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1563 if (new_content_info->timeline == 0) {
1564 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1565 new_content_info->timeline = new_content_info->modified_time;
1567 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1571 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1572 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1573 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1574 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1575 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1577 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1578 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1579 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1580 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1581 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1582 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1584 new_content_info->media_meta.rating = content_info->media_meta.rating;
1589 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1591 int ret = MS_MEDIA_ERR_NONE;
1592 sqlite3 *db_handle = (sqlite3 *)handle;
1593 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1594 bool append_album = FALSE;
1596 /* Checking parameters if they are valid */
1597 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1598 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1599 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1601 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1603 media_svc_content_info_s _new_content_info;
1604 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1606 media_svc_media_type_e media_type;
1608 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1609 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1611 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1612 media_svc_debug("Do nothing[%d]", media_type);
1613 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1614 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1616 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1618 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1620 /* Extracting thumbnail */
1621 if (_new_content_info.thumbnail_path == NULL) {
1622 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1623 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1627 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1628 if (ret == MS_MEDIA_ERR_NONE)
1629 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1631 if (_new_content_info.media_meta.width <= 0)
1632 _new_content_info.media_meta.width = width;
1634 if (_new_content_info.media_meta.height <= 0)
1635 _new_content_info.media_meta.height = height;
1639 /* set othere data to the structure, which is passed as parameters */
1640 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1642 /* Set or Get folder id */
1643 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);
1644 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1646 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1647 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1649 /* register album table data */
1652 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1653 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1655 if (ret != MS_MEDIA_ERR_NONE) {
1656 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1657 media_svc_debug("album does not exist. So start to make album art");
1658 append_album = TRUE;
1660 media_svc_debug("other error");
1661 append_album = FALSE;
1664 _new_content_info.album_id = album_id;
1665 append_album = FALSE;
1667 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1668 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1669 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1672 media_svc_debug("album already exists. don't need to make album art");
1673 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1674 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1679 if (append_album == TRUE) {
1680 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1681 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1682 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);
1684 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1686 _new_content_info.album_id = album_id;
1689 /* Insert to db - calling _media_svc_insert_item_with_data */
1690 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1692 if (ret == MS_MEDIA_ERR_NONE)
1693 media_svc_debug("Insertion is successful.");
1695 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1696 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1698 _media_svc_destroy_content_info(&_new_content_info);
1700 /* handling returned value - important */
1704 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1706 _media_svc_destroy_content_info(content_info);
1709 int media_svc_generate_uuid(char **uuid)
1711 char *gen_uuid = NULL;
1712 gen_uuid = _media_info_generate_uuid();
1713 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1715 *uuid = strdup(gen_uuid);
1717 return MS_MEDIA_ERR_NONE;
1720 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1722 sqlite3 * db_handle = (sqlite3 *)handle;
1724 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1726 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1729 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1731 sqlite3 * db_handle = (sqlite3 *)handle;
1733 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1734 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1735 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1736 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1738 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1741 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1743 sqlite3 * db_handle = (sqlite3 *)handle;
1745 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1746 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1747 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1749 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1752 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, const char *storage_path, media_svc_storage_type_e storage_type, uid_t uid)
1754 int ret = MS_MEDIA_ERR_NONE;
1755 sqlite3 *db_handle = (sqlite3 *)handle;
1757 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1758 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1759 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1760 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1762 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1763 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1765 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1766 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1767 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1769 ret = _media_svc_update_media_view(db_handle, uid);
1770 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1776 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1778 int ret = MS_MEDIA_ERR_NONE;
1779 sqlite3 *db_handle = (sqlite3 *)handle;
1780 media_svc_storage_type_e storage_type;
1782 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1783 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1785 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1786 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1788 ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1789 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1791 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1792 if (ret != MS_MEDIA_ERR_NONE)
1793 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1795 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1796 ret = _media_svc_drop_media_table(storage_id, uid);
1797 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1799 ret = _media_svc_update_media_view(db_handle, uid);
1800 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1806 int media_svc_insert_folder_begin(int data_cnt)
1808 media_svc_debug("Transaction data count : [%d]", data_cnt);
1810 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1812 g_media_svc_insert_folder_data_cnt = data_cnt;
1813 g_media_svc_insert_folder_cur_data_cnt = 0;
1815 return MS_MEDIA_ERR_NONE;
1818 int media_svc_insert_folder_end(uid_t uid)
1820 int ret = MS_MEDIA_ERR_NONE;
1822 media_svc_debug_fenter();
1824 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1825 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1827 g_media_svc_insert_folder_data_cnt = 1;
1828 g_media_svc_insert_folder_cur_data_cnt = 0;
1833 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1835 int ret = MS_MEDIA_ERR_NONE;
1836 sqlite3 * db_handle = (sqlite3 *)handle;
1837 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1839 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1840 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1841 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1842 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1844 if (g_media_svc_insert_folder_data_cnt == 1) {
1846 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1848 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1850 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1851 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1853 g_media_svc_insert_folder_cur_data_cnt++;
1855 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1857 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1858 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1860 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1861 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1863 g_media_svc_insert_folder_cur_data_cnt = 0;
1866 media_svc_error("Error in media_svc_set_insert_folder");
1867 return MS_MEDIA_ERR_INTERNAL;
1873 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1875 int ret = MS_MEDIA_ERR_NONE;
1877 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1882 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1884 int ret = MS_MEDIA_ERR_NONE;
1885 sqlite3 * db_handle = (sqlite3 *)handle;
1887 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1889 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1894 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)
1896 int ret = MS_MEDIA_ERR_NONE;
1897 sqlite3 * db_handle = (sqlite3 *)handle;
1898 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1899 media_svc_media_type_e media_type;
1901 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1902 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1903 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1904 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1906 media_svc_content_info_s content_info;
1907 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1909 /*Set basic media info*/
1910 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1911 if (ret != MS_MEDIA_ERR_NONE) {
1912 media_svc_error("_media_svc_set_media_info fail %d", ret);
1916 /*Set or Get folder id*/
1917 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1918 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1920 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1921 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1923 if (g_media_svc_insert_item_data_cnt == 1) {
1925 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1926 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1928 if (g_insert_with_noti)
1929 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1930 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1932 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1933 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1935 if (g_insert_with_noti)
1936 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1938 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1939 g_media_svc_insert_item_cur_data_cnt++;
1941 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1943 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1944 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1946 if (g_insert_with_noti)
1947 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1949 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1950 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1952 media_svc_debug("_media_svc_list_query_do over");
1954 if (g_insert_with_noti) {
1955 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1956 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1957 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1959 /* Recreate noti list */
1960 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1961 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1964 g_media_svc_insert_item_cur_data_cnt = 0;
1967 media_svc_error("Error in media_svc_insert_item_pass1");
1968 _media_svc_destroy_content_info(&content_info);
1969 return MS_MEDIA_ERR_INTERNAL;
1972 _media_svc_destroy_content_info(&content_info);
1974 return MS_MEDIA_ERR_NONE;
1977 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)
1979 int ret = MS_MEDIA_ERR_NONE;
1980 sqlite3 * db_handle = (sqlite3 *)handle;
1981 sqlite3_stmt *sql_stmt = NULL;
1982 media_svc_media_type_e media_type;
1983 media_svc_content_info_s content_info;
1984 GArray *db_data_array = NULL;
1985 media_svc_item_info_s *db_data = NULL;
1988 media_svc_debug_fenter();
1990 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1991 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1993 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
1994 media_svc_error("storage type is incorrect[%d]", storage_type);
1995 return MS_MEDIA_ERR_INVALID_PARAMETER;
1998 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
1999 if (db_data_array == NULL) {
2000 media_svc_error("db_data_array is NULL. Out of memory");
2001 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2005 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2006 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2007 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2009 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2010 media_svc_error("folder no recursive extract");
2012 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2014 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' LIMIT %d", storage_id, folder_uuid, BATCH_REQUEST_MAX);
2015 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2016 media_svc_error("folder recursive extract");
2018 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2020 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' LIMIT %d", storage_id, extract_path, BATCH_REQUEST_MAX);
2023 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2025 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2028 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2029 if (ret != MS_MEDIA_ERR_NONE) {
2030 media_svc_error("error when get list. err = [%d]", ret);
2031 g_array_free(db_data_array, FALSE);
2035 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2037 db_data = malloc(sizeof(media_svc_item_info_s));
2038 if (db_data == NULL) {
2039 media_svc_error("Out of memory");
2043 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2044 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2046 g_array_append_val(db_data_array, db_data);
2049 SQLITE3_FINALIZE(sql_stmt);
2050 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2052 while (db_data_array->len != 0) {
2054 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2055 g_array_remove_index(db_data_array, 0);
2057 if ((db_data == NULL) || (db_data->path == NULL)) {
2058 media_svc_error("invalid db data");
2062 media_type = db_data->media_type;
2063 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2064 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2065 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2067 content_info.media_type = media_type;
2068 content_info.storage_type = storage_type;
2070 _media_svc_set_default_value(&content_info, FALSE);
2072 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2073 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2074 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2075 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2076 media_svc_debug("Do nothing[%d]", media_type);
2077 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2078 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2080 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2082 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2084 _media_svc_destroy_content_info(&content_info);
2085 SAFE_FREE(db_data->path);
2091 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2093 _media_svc_publish_noti_list(idx);
2094 _media_svc_destroy_noti_list(idx);
2096 /* Recreate noti list */
2097 ret = _media_svc_create_noti_list(idx);
2101 while (db_data_array->len != 0) {
2103 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2104 g_array_remove_index(db_data_array, 0);
2107 SAFE_FREE(db_data->path);
2113 g_array_free(db_data_array, FALSE);
2114 db_data_array = NULL;
2116 media_svc_debug_fleave();
2118 return MS_MEDIA_ERR_NONE;
2121 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2123 int ret = MS_MEDIA_ERR_NONE;
2124 sqlite3 * db_handle = (sqlite3 *)handle;
2126 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2127 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2129 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2134 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2136 int ret = MS_MEDIA_ERR_NONE;
2138 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2143 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2145 int ret = MS_MEDIA_ERR_NONE;
2146 sqlite3 * db_handle = (sqlite3 *)handle;
2147 time_t modified_time = 0;
2148 int system_time = 0;
2150 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2152 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2154 system_time = _media_svc_get_file_time(path);
2155 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
2157 if (system_time != modified_time && system_time != 0)
2165 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2167 sqlite3 * db_handle = (sqlite3 *)handle;
2169 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2170 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2172 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2175 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2177 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2178 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2180 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2183 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2185 int ret = MS_MEDIA_ERR_NONE;
2186 sqlite3 * db_handle = (sqlite3 *)handle;
2188 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2190 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2195 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2197 int ret = MS_MEDIA_ERR_NONE;
2198 sqlite3 * db_handle = (sqlite3 *)handle;
2201 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2202 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2203 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2205 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2206 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_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2221 int ret = MS_MEDIA_ERR_NONE;
2222 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(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2227 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2230 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2231 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2235 media_svc_debug("item is exist in database");
2236 return MS_MEDIA_ERR_NONE;
2238 media_svc_debug("item is not exist in database");
2239 return MS_MEDIA_ERR_DB_NO_RECORD;
2242 return MS_MEDIA_ERR_NONE;
2245 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2247 int ret = MS_MEDIA_ERR_NONE;
2248 sqlite3 * db_handle = (sqlite3 *)handle;
2250 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2251 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2253 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2258 int media_svc_append_query(const char *query, uid_t uid)
2260 int ret = MS_MEDIA_ERR_NONE;
2262 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2264 ret = _media_svc_append_query_list(query, uid);
2269 int media_svc_send_query(uid_t uid)
2271 int ret = MS_MEDIA_ERR_NONE;
2273 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2278 int media_svc_get_media_type(const char *path, int *mediatype)
2280 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2282 return _media_svc_get_media_type(path, mediatype);