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_insert_item_data_cnt = 1;
40 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
42 static __thread int g_media_svc_update_item_data_cnt = 1;
43 static __thread int g_media_svc_update_item_cur_data_cnt = 0;
45 static __thread int g_media_svc_insert_folder_data_cnt = 1;
46 static __thread int g_media_svc_insert_folder_cur_data_cnt = 0;
48 /* Flag for items to be published by notification */
49 static __thread int g_insert_with_noti = FALSE;
51 #define BATCH_REQUEST_MAX 300
53 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type)
55 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
56 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
57 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
58 media_svc_error("storage type is incorrect[%d]", storage_type);
65 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
67 int ret = MS_MEDIA_ERR_NONE;
68 MediaDBHandle *db_handle = NULL;
70 media_svc_debug_fenter();
72 ret = media_db_connect(&db_handle, uid, need_write);
73 if (ret != MS_MEDIA_ERR_NONE)
77 return MS_MEDIA_ERR_NONE;
80 int media_svc_disconnect(MediaSvcHandle *handle)
82 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
84 media_svc_debug_fenter();
86 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
88 return media_db_disconnect(db_handle);
91 int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid)
93 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
95 media_svc_debug_fenter();
97 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
99 return _media_svc_do_cleanup(db_handle, uid);
102 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
104 sqlite3 *db_handle = (sqlite3 *)handle;
106 return _media_svc_get_user_version(db_handle, user_version);
109 int media_svc_create_table(uid_t uid)
111 int ret = MS_MEDIA_ERR_NONE;
114 media_svc_debug_fenter();
116 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
117 if (ret != MS_MEDIA_ERR_NONE) {
118 media_svc_error("_media_svc_init_table_query fail.");
122 /*create media table*/
123 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
124 if (ret != MS_MEDIA_ERR_NONE) {
125 media_svc_error("_media_svc_make_table_query fail.");
129 /*create folder table*/
130 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
131 if (ret != MS_MEDIA_ERR_NONE) {
132 media_svc_error("_media_svc_make_table_query fail.");
136 /*create playlist_map table*/
137 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
138 if (ret != MS_MEDIA_ERR_NONE) {
139 media_svc_error("_media_svc_make_table_query fail.");
143 /*create playlist table*/
144 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
145 if (ret != MS_MEDIA_ERR_NONE) {
146 media_svc_error("_media_svc_make_table_query fail.");
150 /* create album table*/
151 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
152 if (ret != MS_MEDIA_ERR_NONE) {
153 media_svc_error("_media_svc_make_table_query fail.");
157 /*create tag_map table*/
158 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
159 if (ret != MS_MEDIA_ERR_NONE) {
160 media_svc_error("_media_svc_make_table_query fail.");
165 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
166 if (ret != MS_MEDIA_ERR_NONE) {
167 media_svc_error("_media_svc_make_table_query fail.");
171 /*create bookmark table*/
172 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
173 if (ret != MS_MEDIA_ERR_NONE) {
174 media_svc_error("_media_svc_make_table_query fail.");
178 /*create storage table from tizen 2.4 */
179 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
180 if (ret != MS_MEDIA_ERR_NONE) {
181 media_svc_error("_media_svc_make_table_query fail.");
185 /*create face table. from tizen 3.0*/
186 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
187 if (ret != MS_MEDIA_ERR_NONE) {
188 media_svc_error("_media_svc_make_table_query fail.");
192 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
193 if (ret != MS_MEDIA_ERR_NONE) {
194 media_svc_error("_media_svc_make_table_query fail.");
198 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
199 ret = _media_svc_sql_query(sql, uid);
200 if (ret != MS_MEDIA_ERR_NONE) {
201 media_svc_error("user_version update fail.");
205 _media_svc_destroy_table_query();
207 media_svc_debug_fleave();
209 return MS_MEDIA_ERR_NONE;
211 _media_svc_destroy_table_query();
213 media_svc_debug_fleave();
218 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
220 int ret = MS_MEDIA_ERR_NONE;
221 ms_user_storage_type_e type = -1;
223 ret = ms_user_get_storage_type(uid, path, &type);
224 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret);
226 *storage_type = type;
231 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
233 int ret = MS_MEDIA_ERR_NONE;
234 sqlite3 *db_handle = (sqlite3 *)handle;
236 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
237 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
239 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
244 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
246 int ret = MS_MEDIA_ERR_NONE;
247 sqlite3 *db_handle = (sqlite3 *)handle;
250 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
251 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
252 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
254 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
255 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
258 media_svc_debug("item is exist in database");
259 return MS_MEDIA_ERR_NONE;
261 media_svc_debug("item is not exist in database");
262 return MS_MEDIA_ERR_DB_NO_RECORD;
265 return MS_MEDIA_ERR_NONE;
268 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
270 media_svc_debug("Transaction data count : [%d]", data_cnt);
272 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
274 g_media_svc_insert_item_data_cnt = data_cnt;
275 g_media_svc_insert_item_cur_data_cnt = 0;
277 /* Prepare for making noti item list */
279 media_svc_debug("making noti list from pid[%d]", from_pid);
280 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
281 return MS_MEDIA_ERR_OUT_OF_MEMORY;
283 _media_svc_set_noti_from_pid(from_pid);
284 g_insert_with_noti = TRUE;
287 return MS_MEDIA_ERR_NONE;
290 int media_svc_insert_item_end(uid_t uid)
292 int ret = MS_MEDIA_ERR_NONE;
294 media_svc_debug_fenter();
296 if (g_media_svc_insert_item_cur_data_cnt > 0) {
298 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
299 if (g_insert_with_noti) {
300 media_svc_debug("sending noti list");
301 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
302 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
303 g_insert_with_noti = FALSE;
304 _media_svc_set_noti_from_pid(-1);
308 g_media_svc_insert_item_data_cnt = 1;
309 g_media_svc_insert_item_cur_data_cnt = 0;
314 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)
316 int ret = MS_MEDIA_ERR_NONE;
317 sqlite3 *db_handle = (sqlite3 *)handle;
318 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
319 media_svc_media_type_e media_type;
321 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
322 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
323 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
324 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
326 media_svc_content_info_s content_info;
327 memset(&content_info, 0, sizeof(media_svc_content_info_s));
330 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
331 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
332 if (ret != MS_MEDIA_ERR_NONE)
335 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
336 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
337 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
338 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
339 media_svc_debug("Do nothing[%d]", media_type);
340 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
341 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
343 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
344 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
346 /*Set or Get folder id*/
347 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
348 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
350 content_info.folder_uuid = g_strdup(folder_uuid);
351 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
353 if (g_media_svc_insert_item_data_cnt == 1) {
355 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
356 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
358 if (g_insert_with_noti)
359 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
361 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
363 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
364 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
366 if (g_insert_with_noti)
367 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
369 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 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
380 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
382 if (g_insert_with_noti) {
383 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
384 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
386 /* Recreate noti list */
387 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
388 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
391 g_media_svc_insert_item_cur_data_cnt = 0;
394 media_svc_error("Error in media_svc_insert_item_bulk");
395 _media_svc_destroy_content_info(&content_info);
396 return MS_MEDIA_ERR_INTERNAL;
399 _media_svc_destroy_content_info(&content_info);
401 return MS_MEDIA_ERR_NONE;
404 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)
406 int ret = MS_MEDIA_ERR_NONE;
407 sqlite3 *db_handle = (sqlite3 *)handle;
408 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
409 media_svc_media_type_e media_type;
411 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
412 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
413 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
414 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
416 media_svc_content_info_s content_info;
417 memset(&content_info, 0, sizeof(media_svc_content_info_s));
420 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
421 if (ret != MS_MEDIA_ERR_NONE)
424 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
425 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
426 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
427 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
429 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
430 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
432 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
435 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
437 /*Set or Get folder id*/
438 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
439 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
441 content_info.folder_uuid = g_strdup(folder_uuid);
442 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
444 /* Extracting thumbnail */
445 if (content_info.thumbnail_path == NULL) {
446 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
447 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
449 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
450 if (ret == MS_MEDIA_ERR_NONE)
451 content_info.thumbnail_path = g_strdup(thumb_path);
455 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
457 if (ret == MS_MEDIA_ERR_NONE) {
458 media_svc_debug("Insertion is successful. Sending noti for this");
459 _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);
460 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
461 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
464 _media_svc_destroy_content_info(&content_info);
468 int media_svc_move_item(MediaSvcHandle *handle, const char *src_path, const char *dest_path, uid_t uid)
470 int ret = MS_MEDIA_ERR_NONE;
471 sqlite3 *db_handle = (sqlite3 *)handle;
472 char *file_name = NULL;
473 char *folder_path = NULL;
474 int modified_time = 0;
475 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
476 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
477 char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
478 char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
479 ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
480 ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
483 media_svc_debug_fenter();
485 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
486 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
487 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
490 ret = _media_svc_get_storage_uuid(db_handle, src_path, org_stg_id, uid);
491 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
492 ret = _media_svc_get_storage_uuid(db_handle, dest_path, dst_stg_id, uid);
493 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
494 /* Get storage_type */
495 ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
496 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
497 ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
498 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
500 /*check and update folder*/
501 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
502 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
505 file_name = g_path_get_basename(dest_path);
507 /*get modified_time*/
508 modified_time = _media_svc_get_file_time(dest_path);
510 ret = _media_svc_get_media_type_by_path(db_handle, org_stg_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 /*get old thumbnail_path and remove thumbnail */
518 ret = _media_svc_get_thumbnail_path_by_path(db_handle, src_path, old_thumb_path);
519 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
520 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
521 SAFE_FREE(file_name);
525 if (STRING_VALID(old_thumb_path)) {
526 ret = _media_svc_remove_file(old_thumb_path);
527 if (ret != MS_MEDIA_ERR_NONE)
528 media_svc_error("_media_svc_remove_file failed : %d", ret);
532 ret = _media_svc_update_item_by_path(org_stg_id, src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
533 SAFE_FREE(file_name);
534 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
536 media_svc_debug("Move is successful. Sending noti for this");
538 /* Get notification info */
539 media_svc_noti_item *noti_item = NULL;
540 ret = _media_svc_get_noti_info(db_handle, dst_stg_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
541 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
543 /* Send notification for move */
544 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
545 _media_svc_destroy_noti_item(noti_item);
547 /*update folder modified_time*/
548 folder_path = g_path_get_dirname(dest_path);
549 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
550 SAFE_FREE(folder_path);
551 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
553 return MS_MEDIA_ERR_NONE;
556 int media_svc_set_item_validity_begin(int data_cnt)
558 media_svc_debug("Transaction data count : [%d]", data_cnt);
560 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
562 g_media_svc_item_validity_data_cnt = data_cnt;
563 g_media_svc_item_validity_cur_data_cnt = 0;
565 return MS_MEDIA_ERR_NONE;
568 int media_svc_set_item_validity_end(uid_t uid)
570 int ret = MS_MEDIA_ERR_NONE;
572 media_svc_debug_fenter();
574 if (g_media_svc_item_validity_cur_data_cnt > 0)
575 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
577 g_media_svc_item_validity_data_cnt = 1;
578 g_media_svc_item_validity_cur_data_cnt = 0;
583 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
585 int ret = MS_MEDIA_ERR_NONE;
587 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
588 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
590 media_svc_debug("path=[%s], validity=[%d]", path, validity);
592 if (g_media_svc_item_validity_data_cnt == 1) {
594 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
596 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
598 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
599 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
601 g_media_svc_item_validity_cur_data_cnt++;
603 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
605 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
606 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
608 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
609 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
611 g_media_svc_item_validity_cur_data_cnt = 0;
615 media_svc_error("Error in media_svc_set_item_validity");
616 return MS_MEDIA_ERR_INTERNAL;
619 return MS_MEDIA_ERR_NONE;
622 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
624 int ret = MS_MEDIA_ERR_NONE;
625 sqlite3 *db_handle = (sqlite3 *)handle;
626 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
628 media_svc_debug_fenter();
630 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
631 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
632 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
635 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
636 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
638 /*Get thumbnail path to delete*/
639 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
640 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
642 if (g_media_svc_insert_item_data_cnt == 1) {
644 /* Get notification info */
645 media_svc_noti_item *noti_item = NULL;
646 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
647 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
650 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
651 if (ret != MS_MEDIA_ERR_NONE) {
652 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
653 _media_svc_destroy_noti_item(noti_item);
658 /* Send notification */
659 media_svc_debug("Deletion is successful. Sending noti for this");
660 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
661 _media_svc_destroy_noti_item(noti_item);
663 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
664 if (ret != MS_MEDIA_ERR_NONE) {
665 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
672 if (STRING_VALID(thumb_path)) {
673 ret = _media_svc_remove_file(thumb_path);
674 if (ret != MS_MEDIA_ERR_NONE)
675 media_svc_error("fail to remove thumbnail file.");
678 return MS_MEDIA_ERR_NONE;
681 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
683 int ret = MS_MEDIA_ERR_NONE;
684 char *thumb_path = NULL;
686 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
688 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
689 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
691 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
692 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
694 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
695 ret = ms_user_get_thumb_store_path(uid, (ms_user_storage_type_e)storage_type, &thumb_path);
696 if (!STRING_VALID(thumb_path)) {
697 media_svc_error("fail to get thumbnail path");
698 return MS_MEDIA_ERR_INTERNAL;
701 ret = _media_svc_remove_all_files_in_dir(thumb_path);
702 SAFE_FREE(thumb_path);
703 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
706 return MS_MEDIA_ERR_NONE;
709 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
711 sqlite3 *db_handle = (sqlite3 *)handle;
713 media_svc_debug_fenter();
715 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
716 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
717 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
719 /*Delete from DB and remove thumbnail files*/
720 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
723 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
725 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
726 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
728 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
731 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
733 sqlite3 *db_handle = (sqlite3 *)handle;
735 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
736 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
737 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
740 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
742 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
745 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
747 int ret = MS_MEDIA_ERR_NONE;
748 sqlite3 *db_handle = (sqlite3 *)handle;
749 media_svc_media_type_e media_type;
750 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
752 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
753 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
754 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
755 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
757 media_svc_content_info_s content_info;
758 memset(&content_info, 0, sizeof(media_svc_content_info_s));
761 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
762 if (ret != MS_MEDIA_ERR_NONE)
765 /* Initialize thumbnail information to remake thumbnail. */
766 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
767 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
768 _media_svc_destroy_content_info(&content_info);
772 if (STRING_VALID(thumb_path)) {
773 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
774 ret = _media_svc_remove_file(thumb_path);
775 if (ret != MS_MEDIA_ERR_NONE)
776 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
779 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
780 if (ret != MS_MEDIA_ERR_NONE) {
781 _media_svc_destroy_content_info(&content_info);
786 /* Get notification info */
787 media_svc_noti_item *noti_item = NULL;
788 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
789 if (ret != MS_MEDIA_ERR_NONE) {
790 _media_svc_destroy_content_info(&content_info);
794 media_type = noti_item->media_type;
795 content_info.media_type = media_type;
797 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
798 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
799 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
800 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
801 media_svc_debug("Do nothing [%d]", media_type);
802 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
803 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
805 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
807 if (ret != MS_MEDIA_ERR_NONE) {
808 _media_svc_destroy_noti_item(noti_item);
809 _media_svc_destroy_content_info(&content_info);
813 /* Extracting thumbnail */
814 if (content_info.thumbnail_path == NULL) {
815 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
816 memset(thumb_path, 0, sizeof(thumb_path));
818 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
819 if (ret == MS_MEDIA_ERR_NONE)
820 content_info.thumbnail_path = g_strdup(thumb_path);
824 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
826 if (ret == MS_MEDIA_ERR_NONE) {
827 media_svc_debug("Update is successful. Sending noti for this");
828 _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);
830 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
833 _media_svc_destroy_content_info(&content_info);
834 _media_svc_destroy_noti_item(noti_item);
839 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
841 int ret = MS_MEDIA_ERR_NONE;
842 sqlite3 *db_handle = (sqlite3 *)handle;
844 char *name_pinyin = NULL;
845 bool pinyin_support = FALSE;
848 GArray *query_array = NULL;
850 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
851 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
852 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
854 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
856 ret = _media_svc_sql_begin_trans(uid);
857 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
859 /*1. Rename directory */
860 /*Update Pinyin If Support Pinyin*/
861 name = g_path_get_basename(dst_path);
862 media_svc_check_pinyin_support(&pinyin_support);
863 if (pinyin_support) {
864 media_svc_get_pinyin(name, &name_pinyin);
865 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q",
866 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path);
868 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q",
869 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path);
872 ret = media_db_request_update_db_batch(sql, uid);
873 SQLITE3_SAFE_FREE(sql);
875 SAFE_FREE(name_pinyin);
876 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
878 /* 2. Update sub-dir, sub-file */
880 sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path);
881 ret = media_db_request_update_db_batch(sql, uid);
882 SQLITE3_SAFE_FREE(sql);
883 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
886 sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path);
887 ret = media_db_request_update_db_batch(sql, uid);
888 SQLITE3_SAFE_FREE(sql);
889 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
891 ret = _media_svc_sql_end_trans(uid);
892 if (ret != MS_MEDIA_ERR_NONE) {
893 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
897 /*3. Get and update thumbnail_path if exists */
898 sqlite3_stmt *sql_stmt = NULL;
900 sql = sqlite3_mprintf("SELECT path, thumbnail_path, media_type from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path);
901 media_svc_debug("[SQL query] : %s", sql);
903 ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
904 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret);
907 query_array = g_array_new(FALSE, FALSE, sizeof(char *));
908 media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed");
910 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
911 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
912 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
913 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
914 char *thumb_query = NULL;
916 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
917 SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path));
919 media_svc_error("media path is NULL");
920 SQLITE3_FINALIZE(sql_stmt);
921 return MS_MEDIA_ERR_DB_INTERNAL;
924 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
925 SAFE_STRLCPY(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path));
927 media_svc_error("thumbnail path is NULL");
928 SQLITE3_FINALIZE(sql_stmt);
929 return MS_MEDIA_ERR_DB_INTERNAL;
932 media_type = sqlite3_column_int(sql_stmt, 2);
934 ms_user_storage_type_e storage_type = -1;
936 ret = ms_user_get_storage_type(uid, media_path, &storage_type);
937 if (ret != MS_MEDIA_ERR_NONE) {
938 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, media_path, storage_type);
939 SQLITE3_FINALIZE(sql_stmt);
943 ret = _media_svc_get_thumbnail_path(storage_type, media_type, media_new_thumb_path, media_path, THUMB_EXT, uid);
944 if (ret != MS_MEDIA_ERR_NONE) {
945 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
946 SQLITE3_FINALIZE(sql_stmt);
950 thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path);
951 g_array_append_val(query_array, thumb_query);
953 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
954 if (ret != MS_MEDIA_ERR_NONE)
955 media_svc_error("_media_svc_rename_file failed : %d", ret);
958 SQLITE3_FINALIZE(sql_stmt);
960 while (query_array->len != 0) {
961 sql = g_array_index(query_array , char*, 0);
962 g_array_remove_index(query_array, 0);
963 ret = media_db_request_update_db(sql, uid);
964 SQLITE3_SAFE_FREE(sql);
965 if (ret != MS_MEDIA_ERR_NONE)
966 media_svc_error("media_db_request_update_db failed : %d", ret);
969 g_array_free(query_array, FALSE);
972 media_svc_debug("Folder update is successful. Sending noti for this");
973 /* Get notification info */
974 media_svc_noti_item *noti_item = NULL;
975 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
976 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
978 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
979 _media_svc_destroy_noti_item(noti_item);
981 return MS_MEDIA_ERR_NONE;
984 int media_svc_request_update_db(const char *db_query, uid_t uid)
986 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
988 return _media_svc_sql_query(db_query, uid);
991 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)
993 int ret = MS_MEDIA_ERR_NONE;
994 sqlite3 *db_handle = (sqlite3 *)handle;
997 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
998 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1000 /* Get notification info */
1001 media_svc_noti_item *noti_item = NULL;
1002 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1003 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1005 if (folder_id != NULL) {
1006 uuid = strndup(folder_id, strlen(folder_id));
1008 if (noti_item->media_uuid != NULL) {
1009 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1011 _media_svc_destroy_noti_item(noti_item);
1012 media_svc_error("folder uuid is wrong");
1013 return MS_MEDIA_ERR_DB_INTERNAL;
1017 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1018 _media_svc_destroy_noti_item(noti_item);
1024 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1026 sqlite3 *db_handle = (sqlite3 *)handle;
1028 media_svc_debug_fenter();
1030 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1032 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1035 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1037 int ret = MS_MEDIA_ERR_NONE;
1038 sqlite3 *db_handle = (sqlite3 *)handle;
1039 time_t sto_time = 0;
1040 int cur_time = _media_svc_get_file_time(folder_path);
1041 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1043 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1045 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1046 if (ret == MS_MEDIA_ERR_NONE) {
1047 if (sto_time != cur_time)
1048 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
1054 int media_svc_update_item_begin(int data_cnt)
1056 media_svc_debug("Transaction data count : [%d]", data_cnt);
1058 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1060 g_media_svc_update_item_data_cnt = data_cnt;
1061 g_media_svc_update_item_cur_data_cnt = 0;
1063 return MS_MEDIA_ERR_NONE;
1066 int media_svc_update_item_end(uid_t uid)
1068 int ret = MS_MEDIA_ERR_NONE;
1070 media_svc_debug_fenter();
1072 if (g_media_svc_update_item_cur_data_cnt > 0)
1073 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1075 g_media_svc_update_item_data_cnt = 1;
1076 g_media_svc_update_item_cur_data_cnt = 0;
1081 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1083 int ret = MS_MEDIA_ERR_NONE;
1084 sqlite3 *db_handle = (sqlite3 *)handle;
1086 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1088 media_svc_media_type_e media_type;
1089 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1090 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1092 media_svc_content_info_s content_info;
1093 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1096 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1097 if (ret != MS_MEDIA_ERR_NONE)
1100 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1101 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1103 _media_svc_destroy_content_info(&content_info);
1104 return MS_MEDIA_ERR_NONE;
1107 if (ret != MS_MEDIA_ERR_NONE) {
1108 _media_svc_destroy_content_info(&content_info);
1112 if (g_media_svc_update_item_data_cnt == 1) {
1114 ret = _media_svc_update_meta_with_data(&content_info);
1115 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1117 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1119 ret = _media_svc_update_meta_with_data(&content_info);
1120 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1122 g_media_svc_update_item_cur_data_cnt++;
1124 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1126 ret = _media_svc_update_meta_with_data(&content_info);
1127 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1129 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1130 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1132 g_media_svc_update_item_cur_data_cnt = 0;
1135 media_svc_error("Error in media_svc_update_item_meta");
1136 _media_svc_destroy_content_info(&content_info);
1137 return MS_MEDIA_ERR_INTERNAL;
1140 _media_svc_destroy_content_info(&content_info);
1145 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)
1147 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1150 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1152 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1154 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1157 int media_svc_check_pinyin_support(bool *support)
1159 *support = _media_svc_check_pinyin_support();
1161 return MS_MEDIA_ERR_NONE;
1164 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1166 int ret = MS_MEDIA_ERR_NONE;
1167 sqlite3 * db_handle = (sqlite3 *)handle;
1169 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1171 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1172 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1174 ret = _media_svc_update_media_view(db_handle, uid);
1175 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1180 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1182 int ret = MS_MEDIA_ERR_NONE;
1183 sqlite3 * db_handle = (sqlite3 *)handle;
1185 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1186 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1188 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1193 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1195 int ret = MS_MEDIA_ERR_NONE;
1196 sqlite3 * db_handle = (sqlite3 *)handle;
1198 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1199 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1201 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1206 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1208 int ret = MS_MEDIA_ERR_NONE;
1209 sqlite3 * db_handle = (sqlite3 *)handle;
1211 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1212 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1214 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1219 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1221 int ret = MS_MEDIA_ERR_NONE;
1223 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1228 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1230 sqlite3 * db_handle = (sqlite3 *)handle;
1232 media_svc_debug_fenter();
1234 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1235 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1237 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1240 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1242 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1243 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1245 if (content_info->added_time > 0)
1246 new_content_info->added_time = content_info->added_time;
1247 new_content_info->last_played_time = content_info->last_played_time;
1248 new_content_info->played_count = content_info->played_count;
1249 new_content_info->favourate = content_info->favourate;
1251 /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1252 if (STRING_VALID(content_info->file_name)) {
1253 /* Already filled in _media_svc_set_media_info() */
1254 SAFE_FREE(new_content_info->file_name);
1255 new_content_info->file_name = g_strdup(content_info->file_name);
1257 new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1258 new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1259 new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1260 new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1261 new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1262 new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1263 new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1264 new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1265 new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1266 new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1267 new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1268 new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1269 new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1270 new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1271 new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1272 new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1273 new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1274 new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1276 if (STRING_VALID(content_info->media_meta.datetaken)) {
1277 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1279 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1280 if (new_content_info->timeline == 0) {
1281 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1282 new_content_info->timeline = new_content_info->modified_time;
1284 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1288 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1289 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1290 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1291 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1292 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1294 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1295 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1296 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1297 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1298 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1299 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1301 new_content_info->media_meta.rating = content_info->media_meta.rating;
1303 return MS_MEDIA_ERR_NONE;
1306 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1308 int ret = MS_MEDIA_ERR_NONE;
1309 sqlite3 *db_handle = (sqlite3 *)handle;
1310 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1311 bool append_album = FALSE;
1313 /* Checking parameters if they are valid */
1314 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1315 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1316 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1318 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1320 media_svc_content_info_s _new_content_info;
1321 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1323 media_svc_media_type_e media_type;
1325 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1326 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1328 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1329 media_svc_debug("Do nothing[%d]", media_type);
1330 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1331 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1333 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1335 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1337 /* Extracting thumbnail */
1338 if (_new_content_info.thumbnail_path == NULL) {
1339 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1340 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1342 ret = _media_svc_create_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
1343 if (ret == MS_MEDIA_ERR_NONE)
1344 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1348 /* set othere data to the structure, which is passed as parameters */
1349 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1351 /* Set or Get folder id */
1352 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);
1353 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1355 _new_content_info.folder_uuid = g_strdup(folder_uuid);
1356 media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1358 /* register album table data */
1361 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1362 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1364 if (ret != MS_MEDIA_ERR_NONE) {
1365 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1366 media_svc_debug("album does not exist. So start to make album art");
1367 append_album = TRUE;
1369 media_svc_debug("other error");
1370 append_album = FALSE;
1373 _new_content_info.album_id = album_id;
1374 append_album = FALSE;
1376 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1377 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1378 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1381 media_svc_debug("album already exists. don't need to make album art");
1382 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1383 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1388 if (append_album == TRUE) {
1389 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1390 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1391 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);
1393 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1395 _new_content_info.album_id = album_id;
1398 /* Insert to db - calling _media_svc_insert_item_with_data */
1399 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1401 if (ret == MS_MEDIA_ERR_NONE)
1402 media_svc_debug("Insertion is successful.");
1404 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1405 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1407 _media_svc_destroy_content_info(&_new_content_info);
1409 /* handling returned value - important */
1413 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1415 _media_svc_destroy_content_info(content_info);
1418 int media_svc_generate_uuid(char **uuid)
1420 char *gen_uuid = NULL;
1421 gen_uuid = _media_info_generate_uuid();
1422 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1424 *uuid = strdup(gen_uuid);
1426 return MS_MEDIA_ERR_NONE;
1429 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1431 sqlite3 * db_handle = (sqlite3 *)handle;
1433 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1435 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1438 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1440 sqlite3 * db_handle = (sqlite3 *)handle;
1442 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1443 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1444 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1445 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1447 return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1450 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1452 sqlite3 * db_handle = (sqlite3 *)handle;
1454 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1455 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1456 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1458 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1461 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)
1463 int ret = MS_MEDIA_ERR_NONE;
1464 sqlite3 *db_handle = (sqlite3 *)handle;
1466 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1467 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1468 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1469 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1471 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1472 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1474 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1475 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1476 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1478 ret = _media_svc_update_media_view(db_handle, uid);
1479 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1485 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1487 int ret = MS_MEDIA_ERR_NONE;
1488 sqlite3 *db_handle = (sqlite3 *)handle;
1489 media_svc_storage_type_e storage_type;
1491 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1492 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1494 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1495 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1497 ret = _media_svc_delete_storage(storage_id, uid);
1498 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1500 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1501 if (ret != MS_MEDIA_ERR_NONE)
1502 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1504 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1505 ret = _media_svc_drop_media_table(storage_id, uid);
1506 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1508 ret = _media_svc_update_media_view(db_handle, uid);
1509 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1515 int media_svc_insert_folder_begin(int data_cnt)
1517 media_svc_debug("Transaction data count : [%d]", data_cnt);
1519 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1521 g_media_svc_insert_folder_data_cnt = data_cnt;
1522 g_media_svc_insert_folder_cur_data_cnt = 0;
1524 return MS_MEDIA_ERR_NONE;
1527 int media_svc_insert_folder_end(uid_t uid)
1529 int ret = MS_MEDIA_ERR_NONE;
1531 media_svc_debug_fenter();
1533 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1534 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1536 g_media_svc_insert_folder_data_cnt = 1;
1537 g_media_svc_insert_folder_cur_data_cnt = 0;
1542 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1544 int ret = MS_MEDIA_ERR_NONE;
1545 sqlite3 * db_handle = (sqlite3 *)handle;
1546 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1548 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1549 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1550 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1551 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1553 if (g_media_svc_insert_folder_data_cnt == 1) {
1555 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1557 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1559 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1560 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1562 g_media_svc_insert_folder_cur_data_cnt++;
1564 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1566 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1567 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1569 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1570 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1572 g_media_svc_insert_folder_cur_data_cnt = 0;
1575 media_svc_error("Error in media_svc_set_insert_folder");
1576 return MS_MEDIA_ERR_INTERNAL;
1582 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1584 int ret = MS_MEDIA_ERR_NONE;
1586 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1591 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1593 int ret = MS_MEDIA_ERR_NONE;
1594 sqlite3 * db_handle = (sqlite3 *)handle;
1596 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1598 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1603 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)
1605 int ret = MS_MEDIA_ERR_NONE;
1606 sqlite3 * db_handle = (sqlite3 *)handle;
1607 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1608 media_svc_media_type_e media_type;
1610 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1611 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1612 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1613 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1615 media_svc_content_info_s content_info;
1616 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1618 /*Set basic media info*/
1619 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1620 if (ret != MS_MEDIA_ERR_NONE) {
1621 media_svc_error("_media_svc_set_media_info fail %d", ret);
1625 /*Set or Get folder id*/
1626 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1627 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1629 content_info.folder_uuid = g_strdup(folder_uuid);
1630 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1632 if (g_media_svc_insert_item_data_cnt == 1) {
1634 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1635 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1637 if (g_insert_with_noti)
1638 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1639 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1641 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1642 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1644 if (g_insert_with_noti)
1645 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1647 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1648 g_media_svc_insert_item_cur_data_cnt++;
1650 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1652 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1653 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1655 if (g_insert_with_noti)
1656 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1658 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1659 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1661 media_svc_debug("_media_svc_list_query_do over");
1663 if (g_insert_with_noti) {
1664 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1665 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1666 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1668 /* Recreate noti list */
1669 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1670 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1673 g_media_svc_insert_item_cur_data_cnt = 0;
1676 media_svc_error("Error in media_svc_insert_item_pass1");
1677 _media_svc_destroy_content_info(&content_info);
1678 return MS_MEDIA_ERR_INTERNAL;
1681 _media_svc_destroy_content_info(&content_info);
1683 return MS_MEDIA_ERR_NONE;
1686 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1688 int ret = MS_MEDIA_ERR_NONE;
1689 sqlite3 * db_handle = (sqlite3 *)handle;
1691 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1692 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1694 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1699 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1701 int ret = MS_MEDIA_ERR_NONE;
1703 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1708 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1710 int ret = MS_MEDIA_ERR_NONE;
1711 sqlite3 * db_handle = (sqlite3 *)handle;
1712 time_t modified_time = 0;
1713 int system_time = 0;
1715 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1717 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1719 system_time = _media_svc_get_file_time(path);
1720 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1722 if (system_time != modified_time && system_time != 0)
1730 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
1732 sqlite3 * db_handle = (sqlite3 *)handle;
1734 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1735 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1737 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1740 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1742 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1743 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1745 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1748 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1750 int ret = MS_MEDIA_ERR_NONE;
1751 sqlite3 * db_handle = (sqlite3 *)handle;
1754 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1755 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1756 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1758 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1759 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1762 media_svc_debug("item is exist in database");
1763 return MS_MEDIA_ERR_NONE;
1765 media_svc_debug("item is not exist in database");
1766 return MS_MEDIA_ERR_DB_NO_RECORD;
1769 return MS_MEDIA_ERR_NONE;
1772 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1774 int ret = MS_MEDIA_ERR_NONE;
1775 sqlite3 * db_handle = (sqlite3 *)handle;
1777 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1778 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1780 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1785 int media_svc_append_query(const char *query, uid_t uid)
1787 int ret = MS_MEDIA_ERR_NONE;
1789 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1791 ret = _media_svc_append_query_list(query, uid);
1796 int media_svc_send_query(uid_t uid)
1798 int ret = MS_MEDIA_ERR_NONE;
1800 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1805 int media_svc_get_media_type(const char *path, int *mediatype)
1807 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1809 return _media_svc_get_media_type(path, mediatype);
1812 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
1814 int ret = MS_MEDIA_ERR_NONE;
1815 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
1818 // 1. Check media type
1819 if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1820 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1822 // 2. try to create thumbnail
1823 ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid);
1824 if (ret != MS_MEDIA_ERR_NONE) {
1825 media_svc_error("Failed to create thumbnail [%d]", ret);
1826 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
1830 // 3. Update creation result to media db
1831 sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q';", storage_id, thumb_path, file_path);
1833 ret = _media_svc_sql_query(sql, uid);
1834 SQLITE3_SAFE_FREE(sql);
1835 if (ret != MS_MEDIA_ERR_NONE) {
1836 media_svc_error("Failed to update media db [%d]", ret);
1837 *thumbnail_path = g_strdup("");
1839 *thumbnail_path = g_strdup(thumb_path);