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_cleanup_db(MediaSvcHandle *handle, uid_t uid)
103 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
105 media_svc_debug_fenter();
107 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
109 return _media_svc_do_cleanup(db_handle, uid);
112 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
114 sqlite3 *db_handle = (sqlite3 *)handle;
116 return _media_svc_get_user_version(db_handle, user_version);
119 int media_svc_create_table(uid_t uid)
121 int ret = MS_MEDIA_ERR_NONE;
124 media_svc_debug_fenter();
126 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
127 if (ret != MS_MEDIA_ERR_NONE) {
128 media_svc_error("_media_svc_init_table_query fail.");
132 /*create media table*/
133 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
134 if (ret != MS_MEDIA_ERR_NONE) {
135 media_svc_error("_media_svc_make_table_query fail.");
139 /*create folder table*/
140 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
141 if (ret != MS_MEDIA_ERR_NONE) {
142 media_svc_error("_media_svc_make_table_query fail.");
146 /*create playlist_map table*/
147 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
148 if (ret != MS_MEDIA_ERR_NONE) {
149 media_svc_error("_media_svc_make_table_query fail.");
153 /*create playlist table*/
154 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
155 if (ret != MS_MEDIA_ERR_NONE) {
156 media_svc_error("_media_svc_make_table_query fail.");
160 /* create album table*/
161 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
162 if (ret != MS_MEDIA_ERR_NONE) {
163 media_svc_error("_media_svc_make_table_query fail.");
167 /*create tag_map table*/
168 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
169 if (ret != MS_MEDIA_ERR_NONE) {
170 media_svc_error("_media_svc_make_table_query fail.");
175 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
176 if (ret != MS_MEDIA_ERR_NONE) {
177 media_svc_error("_media_svc_make_table_query fail.");
181 /*create bookmark table*/
182 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
183 if (ret != MS_MEDIA_ERR_NONE) {
184 media_svc_error("_media_svc_make_table_query fail.");
188 /*create storage table from tizen 2.4 */
189 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
190 if (ret != MS_MEDIA_ERR_NONE) {
191 media_svc_error("_media_svc_make_table_query fail.");
195 /*create face table. from tizen 3.0*/
196 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
197 if (ret != MS_MEDIA_ERR_NONE) {
198 media_svc_error("_media_svc_make_table_query fail.");
202 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
203 if (ret != MS_MEDIA_ERR_NONE) {
204 media_svc_error("_media_svc_make_table_query fail.");
208 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
209 ret = _media_svc_sql_query(sql, uid);
210 if (ret != MS_MEDIA_ERR_NONE) {
211 media_svc_error("user_version update fail.");
215 _media_svc_destroy_table_query();
217 media_svc_debug_fleave();
219 return MS_MEDIA_ERR_NONE;
221 _media_svc_destroy_table_query();
223 media_svc_debug_fleave();
228 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
230 int ret = MS_MEDIA_ERR_NONE;
231 ms_user_storage_type_e type = -1;
233 ret = ms_user_get_storage_type(uid, path, &type);
234 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret);
236 *storage_type = type;
241 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
243 int ret = MS_MEDIA_ERR_NONE;
244 sqlite3 *db_handle = (sqlite3 *)handle;
246 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
247 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
249 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
254 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
256 int ret = MS_MEDIA_ERR_NONE;
257 sqlite3 *db_handle = (sqlite3 *)handle;
260 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
261 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
262 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
264 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
265 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
268 media_svc_debug("item is exist in database");
269 return MS_MEDIA_ERR_NONE;
271 media_svc_debug("item is not exist in database");
272 return MS_MEDIA_ERR_DB_NO_RECORD;
275 return MS_MEDIA_ERR_NONE;
278 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
280 media_svc_debug("Transaction data count : [%d]", data_cnt);
282 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
284 g_media_svc_insert_item_data_cnt = data_cnt;
285 g_media_svc_insert_item_cur_data_cnt = 0;
287 /* Prepare for making noti item list */
289 media_svc_debug("making noti list from pid[%d]", from_pid);
290 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
291 return MS_MEDIA_ERR_OUT_OF_MEMORY;
293 _media_svc_set_noti_from_pid(from_pid);
294 g_insert_with_noti = TRUE;
297 return MS_MEDIA_ERR_NONE;
300 int media_svc_insert_item_end(uid_t uid)
302 int ret = MS_MEDIA_ERR_NONE;
304 media_svc_debug_fenter();
306 if (g_media_svc_insert_item_cur_data_cnt > 0) {
308 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
309 if (g_insert_with_noti) {
310 media_svc_debug("sending noti list");
311 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
312 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
313 g_insert_with_noti = FALSE;
314 _media_svc_set_noti_from_pid(-1);
318 g_media_svc_insert_item_data_cnt = 1;
319 g_media_svc_insert_item_cur_data_cnt = 0;
324 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)
326 int ret = MS_MEDIA_ERR_NONE;
327 sqlite3 *db_handle = (sqlite3 *)handle;
328 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
329 media_svc_media_type_e media_type;
331 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
332 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
333 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
334 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
336 media_svc_content_info_s content_info;
337 memset(&content_info, 0, sizeof(media_svc_content_info_s));
340 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
341 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
342 if (ret != MS_MEDIA_ERR_NONE)
345 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
346 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
347 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
348 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
349 media_svc_debug("Do nothing[%d]", media_type);
350 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
351 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
353 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
354 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
356 /*Set or Get folder id*/
357 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
358 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
360 content_info.folder_uuid = g_strdup(folder_uuid);
361 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
363 if (g_media_svc_insert_item_data_cnt == 1) {
365 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
366 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
368 if (g_insert_with_noti)
369 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
371 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
373 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
374 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
376 if (g_insert_with_noti)
377 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
379 g_media_svc_insert_item_cur_data_cnt++;
381 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
383 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
384 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
386 if (g_insert_with_noti)
387 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
389 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
390 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
392 if (g_insert_with_noti) {
393 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
394 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
396 /* Recreate noti list */
397 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
398 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
401 g_media_svc_insert_item_cur_data_cnt = 0;
404 media_svc_error("Error in media_svc_insert_item_bulk");
405 _media_svc_destroy_content_info(&content_info);
406 return MS_MEDIA_ERR_INTERNAL;
409 _media_svc_destroy_content_info(&content_info);
411 return MS_MEDIA_ERR_NONE;
414 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)
416 int ret = MS_MEDIA_ERR_NONE;
417 sqlite3 *db_handle = (sqlite3 *)handle;
418 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
419 media_svc_media_type_e media_type;
421 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
422 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
423 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
424 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
426 media_svc_content_info_s content_info;
427 memset(&content_info, 0, sizeof(media_svc_content_info_s));
430 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
431 if (ret != MS_MEDIA_ERR_NONE)
434 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
435 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
436 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
437 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
439 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
440 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
442 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
445 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
447 /*Set or Get folder id*/
448 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
449 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
451 content_info.folder_uuid = g_strdup(folder_uuid);
452 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
454 /* Extracting thumbnail */
455 if (content_info.thumbnail_path == NULL) {
456 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
457 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
459 ret = _media_svc_request_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), uid);
460 if (ret == MS_MEDIA_ERR_NONE)
461 content_info.thumbnail_path = g_strdup(thumb_path);
465 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
467 if (ret == MS_MEDIA_ERR_NONE) {
468 media_svc_debug("Insertion is successful. Sending noti for this");
469 _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);
470 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
471 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
474 _media_svc_destroy_content_info(&content_info);
478 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
479 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
481 int ret = MS_MEDIA_ERR_NONE;
482 sqlite3 *db_handle = (sqlite3 *)handle;
483 char *file_name = NULL;
484 char *folder_path = NULL;
485 int modified_time = 0;
486 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
487 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
488 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
491 media_svc_debug_fenter();
493 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
494 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
495 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
496 media_svc_retvm_if(__media_svc_check_storage(src_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
497 media_svc_retvm_if(__media_svc_check_storage(dest_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
499 /*check and update folder*/
500 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
501 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
504 file_name = g_path_get_basename(dest_path);
506 /*get modified_time*/
507 modified_time = _media_svc_get_file_time(dest_path);
509 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
510 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
511 if (ret != MS_MEDIA_ERR_NONE) {
512 media_svc_error("_media_svc_get_media_type_by_path failed");
513 SAFE_FREE(file_name);
517 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
518 /*get old thumbnail_path*/
519 ret = _media_svc_get_thumbnail_path_by_path(db_handle, src_path, old_thumb_path);
520 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
521 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
522 SAFE_FREE(file_name);
526 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
529 if (g_media_svc_move_item_data_cnt == 1) {
532 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
533 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
535 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
537 SAFE_FREE(file_name);
538 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
540 media_svc_debug("Move is successful. Sending noti for this");
542 /* Get notification info */
543 media_svc_noti_item *noti_item = NULL;
544 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
545 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
547 /* Send notification for move */
548 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
549 _media_svc_destroy_noti_item(noti_item);
551 /*update folder modified_time*/
552 folder_path = g_path_get_dirname(dest_path);
553 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
554 SAFE_FREE(folder_path);
555 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
557 ret = _media_svc_update_folder_table(storage_id, uid);
558 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
560 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
563 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
564 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
566 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
568 SAFE_FREE(file_name);
569 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
571 /*update folder modified_time*/
572 folder_path = g_path_get_dirname(dest_path);
573 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
574 SAFE_FREE(folder_path);
575 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
577 g_media_svc_move_item_cur_data_cnt++;
579 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
581 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
582 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
584 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
586 SAFE_FREE(file_name);
587 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
589 /*update folder modified_time*/
590 folder_path = g_path_get_dirname(dest_path);
591 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
592 SAFE_FREE(folder_path);
593 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
596 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
597 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
599 g_media_svc_move_item_cur_data_cnt = 0;
602 media_svc_error("Error in media_svc_move_item");
603 SAFE_FREE(file_name);
604 return MS_MEDIA_ERR_INTERNAL;
607 /*rename thumbnail file*/
608 if (STRING_VALID(old_thumb_path)) {
609 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
610 if (ret != MS_MEDIA_ERR_NONE)
611 media_svc_error("_media_svc_rename_file failed : %d", ret);
614 return MS_MEDIA_ERR_NONE;
617 int media_svc_set_item_validity_begin(int data_cnt)
619 media_svc_debug("Transaction data count : [%d]", data_cnt);
621 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
623 g_media_svc_item_validity_data_cnt = data_cnt;
624 g_media_svc_item_validity_cur_data_cnt = 0;
626 return MS_MEDIA_ERR_NONE;
629 int media_svc_set_item_validity_end(uid_t uid)
631 int ret = MS_MEDIA_ERR_NONE;
633 media_svc_debug_fenter();
635 if (g_media_svc_item_validity_cur_data_cnt > 0)
636 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
638 g_media_svc_item_validity_data_cnt = 1;
639 g_media_svc_item_validity_cur_data_cnt = 0;
644 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
646 int ret = MS_MEDIA_ERR_NONE;
648 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
649 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
651 media_svc_debug("path=[%s], validity=[%d]", path, validity);
653 if (g_media_svc_item_validity_data_cnt == 1) {
655 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
657 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
659 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
660 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
662 g_media_svc_item_validity_cur_data_cnt++;
664 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
666 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
667 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
669 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
670 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
672 g_media_svc_item_validity_cur_data_cnt = 0;
676 media_svc_error("Error in media_svc_set_item_validity");
677 return MS_MEDIA_ERR_INTERNAL;
680 return MS_MEDIA_ERR_NONE;
683 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
685 int ret = MS_MEDIA_ERR_NONE;
686 sqlite3 *db_handle = (sqlite3 *)handle;
687 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
689 media_svc_debug_fenter();
691 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
692 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
693 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
696 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
697 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
699 /*Get thumbnail path to delete*/
700 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
701 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
703 if (g_media_svc_insert_item_data_cnt == 1) {
705 /* Get notification info */
706 media_svc_noti_item *noti_item = NULL;
707 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
708 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
711 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
712 if (ret != MS_MEDIA_ERR_NONE) {
713 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
714 _media_svc_destroy_noti_item(noti_item);
719 /* Send notification */
720 media_svc_debug("Deletion is successful. Sending noti for this");
721 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
722 _media_svc_destroy_noti_item(noti_item);
724 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
725 if (ret != MS_MEDIA_ERR_NONE) {
726 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
733 if (STRING_VALID(thumb_path)) {
734 ret = _media_svc_remove_file(thumb_path);
735 if (ret != MS_MEDIA_ERR_NONE)
736 media_svc_error("fail to remove thumbnail file.");
739 return MS_MEDIA_ERR_NONE;
742 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
744 int ret = MS_MEDIA_ERR_NONE;
745 char *thumb_path = NULL;
747 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
749 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
750 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
752 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
753 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
755 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
756 ret = ms_user_get_thumb_store_path(uid, (ms_user_storage_type_e)storage_type, &thumb_path);
757 if (!STRING_VALID(thumb_path)) {
758 media_svc_error("fail to get thumbnail path");
759 return MS_MEDIA_ERR_INTERNAL;
762 ret = _media_svc_remove_all_files_in_dir(thumb_path);
763 SAFE_FREE(thumb_path);
764 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
767 return MS_MEDIA_ERR_NONE;
770 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
772 sqlite3 *db_handle = (sqlite3 *)handle;
774 media_svc_debug_fenter();
776 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
777 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
778 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
780 /*Delete from DB and remove thumbnail files*/
781 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
784 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
786 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
787 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
789 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
792 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
794 sqlite3 *db_handle = (sqlite3 *)handle;
796 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
797 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
798 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
801 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
803 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
806 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
808 int ret = MS_MEDIA_ERR_NONE;
809 sqlite3 *db_handle = (sqlite3 *)handle;
810 media_svc_media_type_e media_type;
812 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
813 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
814 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
815 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
817 media_svc_content_info_s content_info;
818 memset(&content_info, 0, sizeof(media_svc_content_info_s));
821 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
822 if (ret != MS_MEDIA_ERR_NONE)
825 /* Initialize thumbnail information to remake thumbnail. */
826 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
827 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
828 if (ret != MS_MEDIA_ERR_NONE) {
829 _media_svc_destroy_content_info(&content_info);
833 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
834 ret = _media_svc_remove_file(thumb_path);
835 if (ret != MS_MEDIA_ERR_NONE)
836 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
839 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
840 if (ret != MS_MEDIA_ERR_NONE) {
841 _media_svc_destroy_content_info(&content_info);
845 /* Get notification info */
846 media_svc_noti_item *noti_item = NULL;
847 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
848 if (ret != MS_MEDIA_ERR_NONE) {
849 _media_svc_destroy_content_info(&content_info);
853 media_type = noti_item->media_type;
854 content_info.media_type = media_type;
856 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
857 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
858 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
859 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
860 media_svc_debug("Do nothing [%d]", media_type);
861 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
862 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
864 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
866 if (ret != MS_MEDIA_ERR_NONE) {
867 _media_svc_destroy_noti_item(noti_item);
868 _media_svc_destroy_content_info(&content_info);
872 /* Extracting thumbnail */
873 if (content_info.thumbnail_path == NULL) {
874 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
875 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
877 ret = _media_svc_request_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), uid);
878 if (ret == MS_MEDIA_ERR_NONE)
879 content_info.thumbnail_path = g_strdup(thumb_path);
883 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
885 if (ret == MS_MEDIA_ERR_NONE) {
886 media_svc_debug("Update is successful. Sending noti for this");
887 _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);
889 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
892 _media_svc_destroy_content_info(&content_info);
893 _media_svc_destroy_noti_item(noti_item);
898 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
900 sqlite3 *db_handle = (sqlite3 *)handle;
901 int ret = MS_MEDIA_ERR_NONE;
903 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
904 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
905 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
907 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
909 ret = _media_svc_sql_begin_trans(uid);
910 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
912 /* Update all folder record's modified date, which are changed above */
913 char *update_folder_modified_time_sql = NULL;
917 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
919 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
920 SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
922 if (ret != SQLITE_OK) {
923 media_svc_error("failed to update folder modified time");
925 return MS_MEDIA_ERR_DB_INTERNAL;
928 /* Update all items */
929 char *select_all_sql = NULL;
930 sqlite3_stmt *sql_stmt = NULL;
931 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
933 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
935 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);
937 media_svc_debug("[SQL query] : %s", select_all_sql);
939 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
940 if (ret != MS_MEDIA_ERR_NONE) {
941 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
945 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
946 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
947 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
948 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
949 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
951 bool no_thumb = FALSE;
953 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
954 SAFE_STRLCPY(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
956 media_svc_error("media UUID is NULL");
957 return MS_MEDIA_ERR_DB_INTERNAL;
960 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
961 SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
963 media_svc_error("media path is NULL");
964 return MS_MEDIA_ERR_DB_INTERNAL;
967 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
968 SAFE_STRLCPY(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
970 media_svc_debug("media thumb path doesn't exist in DB");
974 /*media_type = sqlite3_column_int(sql_stmt, 3); */
976 /* Update path, thumbnail path of this item */
977 char *replaced_path = NULL;
978 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
979 if (replaced_path == NULL) {
980 media_svc_error("_media_svc_replace_path failed");
981 SQLITE3_FINALIZE(sql_stmt);
982 return MS_MEDIA_ERR_INTERNAL;
985 media_svc_debug("New media path : %s", replaced_path);
986 ms_user_storage_type_e storage_type = -1;
989 ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
990 if (ret != MS_MEDIA_ERR_NONE) {
991 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
992 SAFE_FREE(replaced_path);
996 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
997 if (ret != MS_MEDIA_ERR_NONE) {
998 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
999 SAFE_FREE(replaced_path);
1000 SQLITE3_FINALIZE(sql_stmt);
1004 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1007 char *update_item_sql = NULL;
1010 update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1012 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);
1014 ret = media_db_request_update_db_batch(update_item_sql, uid);
1015 SQLITE3_SAFE_FREE(update_item_sql);
1016 SAFE_FREE(replaced_path);
1018 if (ret != SQLITE_OK) {
1019 media_svc_error("failed to update item");
1020 SQLITE3_FINALIZE(sql_stmt);
1022 return MS_MEDIA_ERR_DB_INTERNAL;
1025 /* Rename thumbnail file of file system */
1027 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1028 if (ret != MS_MEDIA_ERR_NONE)
1029 media_svc_error("_media_svc_rename_file failed : %d", ret);
1034 SQLITE3_FINALIZE(sql_stmt);
1036 ret = _media_svc_sql_end_trans(uid);
1037 if (ret != MS_MEDIA_ERR_NONE) {
1038 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1042 media_svc_debug("Folder update is successful. Sending noti for this");
1043 /* Get notification info */
1044 media_svc_noti_item *noti_item = NULL;
1045 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1046 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1048 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1049 _media_svc_destroy_noti_item(noti_item);
1051 return MS_MEDIA_ERR_NONE;
1054 int media_svc_request_update_db(const char *db_query, uid_t uid)
1056 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1058 return _media_svc_sql_query(db_query, uid);
1061 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)
1063 int ret = MS_MEDIA_ERR_NONE;
1064 sqlite3 *db_handle = (sqlite3 *)handle;
1067 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1068 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1070 /* Get notification info */
1071 media_svc_noti_item *noti_item = NULL;
1072 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1073 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1075 if (folder_id != NULL) {
1076 uuid = strndup(folder_id, strlen(folder_id));
1078 if (noti_item->media_uuid != NULL) {
1079 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1081 _media_svc_destroy_noti_item(noti_item);
1082 media_svc_error("folder uuid is wrong");
1083 return MS_MEDIA_ERR_DB_INTERNAL;
1087 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1088 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1089 _media_svc_destroy_noti_item(noti_item);
1095 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1097 sqlite3 *db_handle = (sqlite3 *)handle;
1099 media_svc_debug_fenter();
1101 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1103 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1106 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1108 int ret = MS_MEDIA_ERR_NONE;
1109 sqlite3 *db_handle = (sqlite3 *)handle;
1110 time_t sto_time = 0;
1111 int cur_time = _media_svc_get_file_time(folder_path);
1112 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1114 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1116 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1117 if (ret == MS_MEDIA_ERR_NONE) {
1118 if (sto_time != cur_time)
1119 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1125 int media_svc_update_item_begin(int data_cnt)
1127 media_svc_debug("Transaction data count : [%d]", data_cnt);
1129 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1131 g_media_svc_update_item_data_cnt = data_cnt;
1132 g_media_svc_update_item_cur_data_cnt = 0;
1134 return MS_MEDIA_ERR_NONE;
1137 int media_svc_update_item_end(uid_t uid)
1139 int ret = MS_MEDIA_ERR_NONE;
1141 media_svc_debug_fenter();
1143 if (g_media_svc_update_item_cur_data_cnt > 0)
1144 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1146 g_media_svc_update_item_data_cnt = 1;
1147 g_media_svc_update_item_cur_data_cnt = 0;
1152 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1154 int ret = MS_MEDIA_ERR_NONE;
1155 sqlite3 *db_handle = (sqlite3 *)handle;
1157 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1159 media_svc_media_type_e media_type;
1160 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1161 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1163 media_svc_content_info_s content_info;
1164 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1167 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1168 if (ret != MS_MEDIA_ERR_NONE)
1171 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1172 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1174 _media_svc_destroy_content_info(&content_info);
1175 return MS_MEDIA_ERR_NONE;
1178 if (ret != MS_MEDIA_ERR_NONE) {
1179 _media_svc_destroy_content_info(&content_info);
1183 if (g_media_svc_update_item_data_cnt == 1) {
1185 ret = _media_svc_update_meta_with_data(&content_info);
1186 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1188 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1190 ret = _media_svc_update_meta_with_data(&content_info);
1191 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1193 g_media_svc_update_item_cur_data_cnt++;
1195 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1197 ret = _media_svc_update_meta_with_data(&content_info);
1198 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1200 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1201 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1203 g_media_svc_update_item_cur_data_cnt = 0;
1206 media_svc_error("Error in media_svc_update_item_meta");
1207 _media_svc_destroy_content_info(&content_info);
1208 return MS_MEDIA_ERR_INTERNAL;
1211 _media_svc_destroy_content_info(&content_info);
1216 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)
1218 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1221 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1223 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1225 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1228 int media_svc_check_pinyin_support(bool *support)
1230 *support = _media_svc_check_pinyin_support();
1232 return MS_MEDIA_ERR_NONE;
1235 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1237 int ret = MS_MEDIA_ERR_NONE;
1238 sqlite3 * db_handle = (sqlite3 *)handle;
1240 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1242 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1243 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1245 ret = _media_svc_update_media_view(db_handle, uid);
1246 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1251 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1253 int ret = MS_MEDIA_ERR_NONE;
1254 sqlite3 * db_handle = (sqlite3 *)handle;
1256 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1257 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1259 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1264 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1266 int ret = MS_MEDIA_ERR_NONE;
1267 sqlite3 * db_handle = (sqlite3 *)handle;
1269 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1270 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1272 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1277 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1279 int ret = MS_MEDIA_ERR_NONE;
1280 sqlite3 * db_handle = (sqlite3 *)handle;
1282 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1283 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1285 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1290 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1292 int ret = MS_MEDIA_ERR_NONE;
1294 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1299 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1301 sqlite3 * db_handle = (sqlite3 *)handle;
1303 media_svc_debug_fenter();
1305 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1306 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1308 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1311 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1313 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1314 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1316 if (content_info->added_time > 0)
1317 new_content_info->added_time = content_info->added_time;
1318 new_content_info->last_played_time = content_info->last_played_time;
1319 new_content_info->played_count = content_info->played_count;
1320 new_content_info->favourate = content_info->favourate;
1322 /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1323 if (STRING_VALID(content_info->file_name)) {
1324 /* Already filled in _media_svc_set_media_info() */
1325 SAFE_FREE(new_content_info->file_name);
1326 new_content_info->file_name = g_strdup(content_info->file_name);
1328 new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1329 new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1330 new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1331 new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1332 new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1333 new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1334 new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1335 new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1336 new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1337 new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1338 new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1339 new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1340 new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1341 new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1342 new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1343 new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1344 new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1345 new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1347 if (STRING_VALID(content_info->media_meta.datetaken)) {
1348 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1350 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1351 if (new_content_info->timeline == 0) {
1352 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1353 new_content_info->timeline = new_content_info->modified_time;
1355 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1359 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1360 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1361 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1362 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1363 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1365 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1366 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1367 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1368 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1369 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1370 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1372 new_content_info->media_meta.rating = content_info->media_meta.rating;
1374 return MS_MEDIA_ERR_NONE;
1377 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1379 int ret = MS_MEDIA_ERR_NONE;
1380 sqlite3 *db_handle = (sqlite3 *)handle;
1381 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1382 bool append_album = FALSE;
1384 /* Checking parameters if they are valid */
1385 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1386 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1387 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1389 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1391 media_svc_content_info_s _new_content_info;
1392 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1394 media_svc_media_type_e media_type;
1396 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1397 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1399 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1400 media_svc_debug("Do nothing[%d]", media_type);
1401 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1402 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1404 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1406 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1408 /* Extracting thumbnail */
1409 if (_new_content_info.thumbnail_path == NULL) {
1410 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1411 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1413 ret = _media_svc_request_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), uid);
1414 if (ret == MS_MEDIA_ERR_NONE)
1415 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1419 /* set othere data to the structure, which is passed as parameters */
1420 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1422 /* Set or Get folder id */
1423 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);
1424 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1426 _new_content_info.folder_uuid = g_strdup(folder_uuid);
1427 media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1429 /* register album table data */
1432 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1433 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1435 if (ret != MS_MEDIA_ERR_NONE) {
1436 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1437 media_svc_debug("album does not exist. So start to make album art");
1438 append_album = TRUE;
1440 media_svc_debug("other error");
1441 append_album = FALSE;
1444 _new_content_info.album_id = album_id;
1445 append_album = FALSE;
1447 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1448 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1449 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1452 media_svc_debug("album already exists. don't need to make album art");
1453 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1454 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1459 if (append_album == TRUE) {
1460 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1461 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1462 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);
1464 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1466 _new_content_info.album_id = album_id;
1469 /* Insert to db - calling _media_svc_insert_item_with_data */
1470 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1472 if (ret == MS_MEDIA_ERR_NONE)
1473 media_svc_debug("Insertion is successful.");
1475 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1476 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1478 _media_svc_destroy_content_info(&_new_content_info);
1480 /* handling returned value - important */
1484 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1486 _media_svc_destroy_content_info(content_info);
1489 int media_svc_generate_uuid(char **uuid)
1491 char *gen_uuid = NULL;
1492 gen_uuid = _media_info_generate_uuid();
1493 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1495 *uuid = strdup(gen_uuid);
1497 return MS_MEDIA_ERR_NONE;
1500 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1502 sqlite3 * db_handle = (sqlite3 *)handle;
1504 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1506 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1509 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1511 sqlite3 * db_handle = (sqlite3 *)handle;
1513 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1514 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1515 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1516 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1518 return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1521 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1523 sqlite3 * db_handle = (sqlite3 *)handle;
1525 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1526 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1527 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1529 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1532 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)
1534 int ret = MS_MEDIA_ERR_NONE;
1535 sqlite3 *db_handle = (sqlite3 *)handle;
1537 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1538 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1539 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1540 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1542 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1543 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1545 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1546 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1547 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1549 ret = _media_svc_update_media_view(db_handle, uid);
1550 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1556 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1558 int ret = MS_MEDIA_ERR_NONE;
1559 sqlite3 *db_handle = (sqlite3 *)handle;
1560 media_svc_storage_type_e storage_type;
1562 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1563 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1565 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1566 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1568 ret = _media_svc_delete_storage(storage_id, uid);
1569 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1571 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1572 if (ret != MS_MEDIA_ERR_NONE)
1573 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1575 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1576 ret = _media_svc_drop_media_table(storage_id, uid);
1577 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1579 ret = _media_svc_update_media_view(db_handle, uid);
1580 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1586 int media_svc_insert_folder_begin(int data_cnt)
1588 media_svc_debug("Transaction data count : [%d]", data_cnt);
1590 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1592 g_media_svc_insert_folder_data_cnt = data_cnt;
1593 g_media_svc_insert_folder_cur_data_cnt = 0;
1595 return MS_MEDIA_ERR_NONE;
1598 int media_svc_insert_folder_end(uid_t uid)
1600 int ret = MS_MEDIA_ERR_NONE;
1602 media_svc_debug_fenter();
1604 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1605 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1607 g_media_svc_insert_folder_data_cnt = 1;
1608 g_media_svc_insert_folder_cur_data_cnt = 0;
1613 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1615 int ret = MS_MEDIA_ERR_NONE;
1616 sqlite3 * db_handle = (sqlite3 *)handle;
1617 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1619 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1620 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1621 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1622 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1624 if (g_media_svc_insert_folder_data_cnt == 1) {
1626 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1628 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1630 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1631 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1633 g_media_svc_insert_folder_cur_data_cnt++;
1635 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1637 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1638 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1640 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1641 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1643 g_media_svc_insert_folder_cur_data_cnt = 0;
1646 media_svc_error("Error in media_svc_set_insert_folder");
1647 return MS_MEDIA_ERR_INTERNAL;
1653 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1655 int ret = MS_MEDIA_ERR_NONE;
1657 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1662 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1664 int ret = MS_MEDIA_ERR_NONE;
1665 sqlite3 * db_handle = (sqlite3 *)handle;
1667 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1669 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1674 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)
1676 int ret = MS_MEDIA_ERR_NONE;
1677 sqlite3 * db_handle = (sqlite3 *)handle;
1678 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1679 media_svc_media_type_e media_type;
1681 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1682 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1683 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1684 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1686 media_svc_content_info_s content_info;
1687 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1689 /*Set basic media info*/
1690 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1691 if (ret != MS_MEDIA_ERR_NONE) {
1692 media_svc_error("_media_svc_set_media_info fail %d", ret);
1696 /*Set or Get folder id*/
1697 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1698 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1700 content_info.folder_uuid = g_strdup(folder_uuid);
1701 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1703 if (g_media_svc_insert_item_data_cnt == 1) {
1705 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1706 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1708 if (g_insert_with_noti)
1709 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1710 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1712 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1713 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1715 if (g_insert_with_noti)
1716 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1718 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1719 g_media_svc_insert_item_cur_data_cnt++;
1721 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1723 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1724 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1726 if (g_insert_with_noti)
1727 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1729 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1730 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1732 media_svc_debug("_media_svc_list_query_do over");
1734 if (g_insert_with_noti) {
1735 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1736 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1737 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1739 /* Recreate noti list */
1740 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1741 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1744 g_media_svc_insert_item_cur_data_cnt = 0;
1747 media_svc_error("Error in media_svc_insert_item_pass1");
1748 _media_svc_destroy_content_info(&content_info);
1749 return MS_MEDIA_ERR_INTERNAL;
1752 _media_svc_destroy_content_info(&content_info);
1754 return MS_MEDIA_ERR_NONE;
1757 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)
1759 int ret = MS_MEDIA_ERR_NONE;
1760 sqlite3 * db_handle = (sqlite3 *)handle;
1761 sqlite3_stmt *sql_stmt = NULL;
1762 media_svc_media_type_e media_type;
1763 media_svc_content_info_s content_info;
1764 GArray *db_data_array = NULL;
1765 media_svc_item_info_s *db_data = NULL;
1768 media_svc_debug_fenter();
1770 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1771 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1773 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
1774 media_svc_error("storage type is incorrect[%d]", storage_type);
1775 return MS_MEDIA_ERR_INVALID_PARAMETER;
1778 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
1779 if (db_data_array == NULL) {
1780 media_svc_error("db_data_array is NULL. Out of memory");
1781 return MS_MEDIA_ERR_OUT_OF_MEMORY;
1785 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1786 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
1787 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
1789 if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
1790 media_svc_error("folder no recursive extract");
1792 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
1794 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);
1795 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
1796 media_svc_error("folder recursive extract");
1798 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
1800 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);
1803 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
1805 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
1808 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
1809 if (ret != MS_MEDIA_ERR_NONE) {
1810 media_svc_error("error when get list. err = [%d]", ret);
1811 g_array_free(db_data_array, FALSE);
1815 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1817 db_data = malloc(sizeof(media_svc_item_info_s));
1818 if (db_data == NULL) {
1819 media_svc_error("Out of memory");
1823 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
1824 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
1826 g_array_append_val(db_data_array, db_data);
1829 SQLITE3_FINALIZE(sql_stmt);
1830 media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
1832 while (db_data_array->len != 0) {
1834 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
1835 g_array_remove_index(db_data_array, 0);
1837 if ((db_data == NULL) || (db_data->path == NULL)) {
1838 media_svc_error("invalid db data");
1842 media_type = db_data->media_type;
1843 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
1844 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1845 content_info.path = g_strdup(db_data->path);
1847 content_info.media_type = media_type;
1848 content_info.storage_type = storage_type;
1850 _media_svc_set_default_value(&content_info, FALSE);
1852 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
1853 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
1854 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
1855 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
1856 media_svc_debug("Do nothing[%d]", media_type);
1857 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1858 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
1860 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
1862 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
1864 _media_svc_destroy_content_info(&content_info);
1865 SAFE_FREE(db_data->path);
1871 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1873 _media_svc_publish_noti_list(idx);
1874 _media_svc_destroy_noti_list(idx);
1876 /* Recreate noti list */
1877 ret = _media_svc_create_noti_list(idx);
1881 while (db_data_array->len != 0) {
1883 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
1884 g_array_remove_index(db_data_array, 0);
1887 SAFE_FREE(db_data->path);
1892 g_array_free(db_data_array, FALSE);
1893 db_data_array = NULL;
1895 media_svc_debug_fleave();
1897 return MS_MEDIA_ERR_NONE;
1900 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1902 int ret = MS_MEDIA_ERR_NONE;
1903 sqlite3 * db_handle = (sqlite3 *)handle;
1905 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1906 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1908 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1913 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1915 int ret = MS_MEDIA_ERR_NONE;
1917 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1922 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1924 int ret = MS_MEDIA_ERR_NONE;
1925 sqlite3 * db_handle = (sqlite3 *)handle;
1926 time_t modified_time = 0;
1927 int system_time = 0;
1929 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1931 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1933 system_time = _media_svc_get_file_time(path);
1934 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1936 if (system_time != modified_time && system_time != 0)
1944 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
1946 sqlite3 * db_handle = (sqlite3 *)handle;
1948 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1949 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1951 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1954 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1956 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1957 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1959 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1962 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1964 int ret = MS_MEDIA_ERR_NONE;
1965 sqlite3 * db_handle = (sqlite3 *)handle;
1968 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1969 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1970 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1972 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1973 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1976 media_svc_debug("item is exist in database");
1977 return MS_MEDIA_ERR_NONE;
1979 media_svc_debug("item is not exist in database");
1980 return MS_MEDIA_ERR_DB_NO_RECORD;
1983 return MS_MEDIA_ERR_NONE;
1986 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1988 int ret = MS_MEDIA_ERR_NONE;
1989 sqlite3 * db_handle = (sqlite3 *)handle;
1991 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1992 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1994 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1999 int media_svc_append_query(const char *query, uid_t uid)
2001 int ret = MS_MEDIA_ERR_NONE;
2003 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2005 ret = _media_svc_append_query_list(query, uid);
2010 int media_svc_send_query(uid_t uid)
2012 int ret = MS_MEDIA_ERR_NONE;
2014 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2019 int media_svc_get_media_type(const char *path, int *mediatype)
2021 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2023 return _media_svc_get_media_type(path, mediatype);