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
56 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type)
58 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
59 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
60 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
61 media_svc_error("storage type is incorrect[%d]", storage_type);
68 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
70 int ret = MS_MEDIA_ERR_NONE;
71 MediaDBHandle *db_handle = NULL;
73 media_svc_debug_fenter();
75 ret = media_db_connect(&db_handle, uid, need_write);
76 if (ret != MS_MEDIA_ERR_NONE)
80 return MS_MEDIA_ERR_NONE;
83 int media_svc_disconnect(MediaSvcHandle *handle)
85 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
87 media_svc_debug_fenter();
89 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
91 return media_db_disconnect(db_handle);
94 int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid)
96 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
98 media_svc_debug_fenter();
100 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
102 return _media_svc_do_cleanup(db_handle, uid);
105 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
107 sqlite3 *db_handle = (sqlite3 *)handle;
109 return _media_svc_get_user_version(db_handle, user_version);
112 int media_svc_create_table(uid_t uid)
114 int ret = MS_MEDIA_ERR_NONE;
117 media_svc_debug_fenter();
119 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
120 if (ret != MS_MEDIA_ERR_NONE) {
121 media_svc_error("_media_svc_init_table_query fail.");
125 /*create media table*/
126 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
127 if (ret != MS_MEDIA_ERR_NONE) {
128 media_svc_error("_media_svc_make_table_query fail.");
132 /*create folder table*/
133 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
134 if (ret != MS_MEDIA_ERR_NONE) {
135 media_svc_error("_media_svc_make_table_query fail.");
139 /*create playlist_map table*/
140 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
141 if (ret != MS_MEDIA_ERR_NONE) {
142 media_svc_error("_media_svc_make_table_query fail.");
146 /*create playlist table*/
147 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
148 if (ret != MS_MEDIA_ERR_NONE) {
149 media_svc_error("_media_svc_make_table_query fail.");
153 /* create album table*/
154 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
155 if (ret != MS_MEDIA_ERR_NONE) {
156 media_svc_error("_media_svc_make_table_query fail.");
160 /*create tag_map table*/
161 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
162 if (ret != MS_MEDIA_ERR_NONE) {
163 media_svc_error("_media_svc_make_table_query fail.");
168 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
169 if (ret != MS_MEDIA_ERR_NONE) {
170 media_svc_error("_media_svc_make_table_query fail.");
174 /*create bookmark table*/
175 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
176 if (ret != MS_MEDIA_ERR_NONE) {
177 media_svc_error("_media_svc_make_table_query fail.");
181 /*create storage table from tizen 2.4 */
182 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
183 if (ret != MS_MEDIA_ERR_NONE) {
184 media_svc_error("_media_svc_make_table_query fail.");
188 /*create face table. from tizen 3.0*/
189 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
190 if (ret != MS_MEDIA_ERR_NONE) {
191 media_svc_error("_media_svc_make_table_query fail.");
195 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
196 if (ret != MS_MEDIA_ERR_NONE) {
197 media_svc_error("_media_svc_make_table_query fail.");
201 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
202 ret = _media_svc_sql_query(sql, uid);
203 if (ret != MS_MEDIA_ERR_NONE) {
204 media_svc_error("user_version update fail.");
208 _media_svc_destroy_table_query();
210 media_svc_debug_fleave();
212 return MS_MEDIA_ERR_NONE;
214 _media_svc_destroy_table_query();
216 media_svc_debug_fleave();
221 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
223 int ret = MS_MEDIA_ERR_NONE;
224 ms_user_storage_type_e type = -1;
226 ret = ms_user_get_storage_type(uid, path, &type);
227 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret);
229 *storage_type = type;
234 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
236 int ret = MS_MEDIA_ERR_NONE;
237 sqlite3 *db_handle = (sqlite3 *)handle;
239 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
240 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
242 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
247 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
249 int ret = MS_MEDIA_ERR_NONE;
250 sqlite3 *db_handle = (sqlite3 *)handle;
253 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
254 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
255 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
257 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
258 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
261 media_svc_debug("item is exist in database");
262 return MS_MEDIA_ERR_NONE;
264 media_svc_debug("item is not exist in database");
265 return MS_MEDIA_ERR_DB_NO_RECORD;
268 return MS_MEDIA_ERR_NONE;
271 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
273 media_svc_debug("Transaction data count : [%d]", data_cnt);
275 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
277 g_media_svc_insert_item_data_cnt = data_cnt;
278 g_media_svc_insert_item_cur_data_cnt = 0;
280 /* Prepare for making noti item list */
282 media_svc_debug("making noti list from pid[%d]", from_pid);
283 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
284 return MS_MEDIA_ERR_OUT_OF_MEMORY;
286 _media_svc_set_noti_from_pid(from_pid);
287 g_insert_with_noti = TRUE;
290 return MS_MEDIA_ERR_NONE;
293 int media_svc_insert_item_end(uid_t uid)
295 int ret = MS_MEDIA_ERR_NONE;
297 media_svc_debug_fenter();
299 if (g_media_svc_insert_item_cur_data_cnt > 0) {
301 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
302 if (g_insert_with_noti) {
303 media_svc_debug("sending noti list");
304 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
305 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
306 g_insert_with_noti = FALSE;
307 _media_svc_set_noti_from_pid(-1);
311 g_media_svc_insert_item_data_cnt = 1;
312 g_media_svc_insert_item_cur_data_cnt = 0;
317 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)
319 int ret = MS_MEDIA_ERR_NONE;
320 sqlite3 *db_handle = (sqlite3 *)handle;
321 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
322 media_svc_media_type_e media_type;
324 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
325 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
326 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
327 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
329 media_svc_content_info_s content_info;
330 memset(&content_info, 0, sizeof(media_svc_content_info_s));
333 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
334 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
335 if (ret != MS_MEDIA_ERR_NONE)
338 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
339 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
340 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
341 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
342 media_svc_debug("Do nothing[%d]", media_type);
343 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
344 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
346 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
347 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
349 /*Set or Get folder id*/
350 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
351 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
353 content_info.folder_uuid = g_strdup(folder_uuid);
354 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
356 if (g_media_svc_insert_item_data_cnt == 1) {
358 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
359 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
361 if (g_insert_with_noti)
362 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
364 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
366 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
367 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
369 if (g_insert_with_noti)
370 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
372 g_media_svc_insert_item_cur_data_cnt++;
374 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
376 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
377 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
379 if (g_insert_with_noti)
380 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
382 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
383 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
385 if (g_insert_with_noti) {
386 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
387 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
389 /* Recreate noti list */
390 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
391 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
394 g_media_svc_insert_item_cur_data_cnt = 0;
397 media_svc_error("Error in media_svc_insert_item_bulk");
398 _media_svc_destroy_content_info(&content_info);
399 return MS_MEDIA_ERR_INTERNAL;
402 _media_svc_destroy_content_info(&content_info);
404 return MS_MEDIA_ERR_NONE;
407 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)
409 int ret = MS_MEDIA_ERR_NONE;
410 sqlite3 *db_handle = (sqlite3 *)handle;
411 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
412 media_svc_media_type_e media_type;
414 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
415 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
416 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
417 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
419 media_svc_content_info_s content_info;
420 memset(&content_info, 0, sizeof(media_svc_content_info_s));
423 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
424 if (ret != MS_MEDIA_ERR_NONE)
427 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
428 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
429 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
430 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
432 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
433 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
435 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
438 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
440 /*Set or Get folder id*/
441 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
442 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
444 content_info.folder_uuid = g_strdup(folder_uuid);
445 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
447 /* Extracting thumbnail */
448 if (content_info.thumbnail_path == NULL) {
449 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
450 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
452 ret = _media_svc_request_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), uid);
453 if (ret == MS_MEDIA_ERR_NONE)
454 content_info.thumbnail_path = g_strdup(thumb_path);
458 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
460 if (ret == MS_MEDIA_ERR_NONE) {
461 media_svc_debug("Insertion is successful. Sending noti for this");
462 _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);
463 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
464 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
467 _media_svc_destroy_content_info(&content_info);
471 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
472 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
474 int ret = MS_MEDIA_ERR_NONE;
475 sqlite3 *db_handle = (sqlite3 *)handle;
476 char *file_name = NULL;
477 char *folder_path = NULL;
478 int modified_time = 0;
479 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
480 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
481 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
484 media_svc_debug_fenter();
486 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
487 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
488 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
489 media_svc_retvm_if(__media_svc_check_storage(src_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
490 media_svc_retvm_if(__media_svc_check_storage(dest_storage) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
492 /*check and update folder*/
493 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
494 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
497 file_name = g_path_get_basename(dest_path);
499 /*get modified_time*/
500 modified_time = _media_svc_get_file_time(dest_path);
502 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
503 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
504 if (ret != MS_MEDIA_ERR_NONE) {
505 media_svc_error("_media_svc_get_media_type_by_path failed");
506 SAFE_FREE(file_name);
510 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
511 /*get old thumbnail_path*/
512 ret = _media_svc_get_thumbnail_path_by_path(db_handle, src_path, old_thumb_path);
513 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
514 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
515 SAFE_FREE(file_name);
519 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
522 if (g_media_svc_move_item_data_cnt == 1) {
525 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
526 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
528 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
530 SAFE_FREE(file_name);
531 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
533 media_svc_debug("Move is successful. Sending noti for this");
535 /* Get notification info */
536 media_svc_noti_item *noti_item = NULL;
537 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
538 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
540 /* Send notification for move */
541 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
542 _media_svc_destroy_noti_item(noti_item);
544 /*update folder modified_time*/
545 folder_path = g_path_get_dirname(dest_path);
546 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
547 SAFE_FREE(folder_path);
548 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
550 ret = _media_svc_update_folder_table(storage_id, uid);
551 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
553 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
556 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
557 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
559 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
561 SAFE_FREE(file_name);
562 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
564 /*update folder modified_time*/
565 folder_path = g_path_get_dirname(dest_path);
566 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
567 SAFE_FREE(folder_path);
568 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
570 g_media_svc_move_item_cur_data_cnt++;
572 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
574 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO))
575 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
577 ret = _media_svc_update_item_by_path(src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
579 SAFE_FREE(file_name);
580 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
582 /*update folder modified_time*/
583 folder_path = g_path_get_dirname(dest_path);
584 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, TRUE, uid);
585 SAFE_FREE(folder_path);
586 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
589 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_MOVE_ITEM, uid);
590 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
592 g_media_svc_move_item_cur_data_cnt = 0;
595 media_svc_error("Error in media_svc_move_item");
596 SAFE_FREE(file_name);
597 return MS_MEDIA_ERR_INTERNAL;
600 /*rename thumbnail file*/
601 if (STRING_VALID(old_thumb_path)) {
602 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
603 if (ret != MS_MEDIA_ERR_NONE)
604 media_svc_error("_media_svc_rename_file failed : %d", ret);
607 return MS_MEDIA_ERR_NONE;
610 int media_svc_set_item_validity_begin(int data_cnt)
612 media_svc_debug("Transaction data count : [%d]", data_cnt);
614 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
616 g_media_svc_item_validity_data_cnt = data_cnt;
617 g_media_svc_item_validity_cur_data_cnt = 0;
619 return MS_MEDIA_ERR_NONE;
622 int media_svc_set_item_validity_end(uid_t uid)
624 int ret = MS_MEDIA_ERR_NONE;
626 media_svc_debug_fenter();
628 if (g_media_svc_item_validity_cur_data_cnt > 0)
629 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
631 g_media_svc_item_validity_data_cnt = 1;
632 g_media_svc_item_validity_cur_data_cnt = 0;
637 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
639 int ret = MS_MEDIA_ERR_NONE;
641 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
642 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
644 media_svc_debug("path=[%s], validity=[%d]", path, validity);
646 if (g_media_svc_item_validity_data_cnt == 1) {
648 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
650 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
652 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
653 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
655 g_media_svc_item_validity_cur_data_cnt++;
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 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
663 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
665 g_media_svc_item_validity_cur_data_cnt = 0;
669 media_svc_error("Error in media_svc_set_item_validity");
670 return MS_MEDIA_ERR_INTERNAL;
673 return MS_MEDIA_ERR_NONE;
676 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
678 int ret = MS_MEDIA_ERR_NONE;
679 sqlite3 *db_handle = (sqlite3 *)handle;
680 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
682 media_svc_debug_fenter();
684 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
685 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
686 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
689 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
690 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
692 /*Get thumbnail path to delete*/
693 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
694 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
696 if (g_media_svc_insert_item_data_cnt == 1) {
698 /* Get notification info */
699 media_svc_noti_item *noti_item = NULL;
700 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
701 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
704 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
705 if (ret != MS_MEDIA_ERR_NONE) {
706 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
707 _media_svc_destroy_noti_item(noti_item);
712 /* Send notification */
713 media_svc_debug("Deletion is successful. Sending noti for this");
714 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
715 _media_svc_destroy_noti_item(noti_item);
717 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
718 if (ret != MS_MEDIA_ERR_NONE) {
719 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
726 if (STRING_VALID(thumb_path)) {
727 ret = _media_svc_remove_file(thumb_path);
728 if (ret != MS_MEDIA_ERR_NONE)
729 media_svc_error("fail to remove thumbnail file.");
732 return MS_MEDIA_ERR_NONE;
735 int media_svc_delete_all_items_in_storage(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
737 int ret = MS_MEDIA_ERR_NONE;
738 char *thumb_path = NULL;
740 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
742 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
743 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
745 ret = _media_svc_truncate_table(storage_id, storage_type, uid);
746 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
748 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
749 ret = ms_user_get_thumb_store_path(uid, (ms_user_storage_type_e)storage_type, &thumb_path);
750 if (!STRING_VALID(thumb_path)) {
751 media_svc_error("fail to get thumbnail path");
752 return MS_MEDIA_ERR_INTERNAL;
755 ret = _media_svc_remove_all_files_in_dir(thumb_path);
756 SAFE_FREE(thumb_path);
757 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
760 return MS_MEDIA_ERR_NONE;
763 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
765 sqlite3 *db_handle = (sqlite3 *)handle;
767 media_svc_debug_fenter();
769 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
770 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
771 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
773 /*Delete from DB and remove thumbnail files*/
774 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
777 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
779 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
780 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
782 return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
785 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
787 sqlite3 *db_handle = (sqlite3 *)handle;
789 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
790 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
791 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
794 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
796 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
799 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
801 int ret = MS_MEDIA_ERR_NONE;
802 sqlite3 *db_handle = (sqlite3 *)handle;
803 media_svc_media_type_e media_type;
804 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
806 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
807 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
808 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
809 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
811 media_svc_content_info_s content_info;
812 memset(&content_info, 0, sizeof(media_svc_content_info_s));
815 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
816 if (ret != MS_MEDIA_ERR_NONE)
819 /* Initialize thumbnail information to remake thumbnail. */
820 ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
821 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
822 _media_svc_destroy_content_info(&content_info);
826 if (STRING_VALID(thumb_path)) {
827 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
828 ret = _media_svc_remove_file(thumb_path);
829 if (ret != MS_MEDIA_ERR_NONE)
830 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
833 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
834 if (ret != MS_MEDIA_ERR_NONE) {
835 _media_svc_destroy_content_info(&content_info);
840 /* Get notification info */
841 media_svc_noti_item *noti_item = NULL;
842 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
843 if (ret != MS_MEDIA_ERR_NONE) {
844 _media_svc_destroy_content_info(&content_info);
848 media_type = noti_item->media_type;
849 content_info.media_type = media_type;
851 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
852 || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
853 || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
854 || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
855 media_svc_debug("Do nothing [%d]", media_type);
856 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
857 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
859 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
861 if (ret != MS_MEDIA_ERR_NONE) {
862 _media_svc_destroy_noti_item(noti_item);
863 _media_svc_destroy_content_info(&content_info);
867 /* Extracting thumbnail */
868 if (content_info.thumbnail_path == NULL) {
869 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
870 memset(thumb_path, 0, sizeof(thumb_path));
872 ret = _media_svc_request_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), uid);
873 if (ret == MS_MEDIA_ERR_NONE)
874 content_info.thumbnail_path = g_strdup(thumb_path);
878 ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
880 if (ret == MS_MEDIA_ERR_NONE) {
881 media_svc_debug("Update is successful. Sending noti for this");
882 _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);
884 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
887 _media_svc_destroy_content_info(&content_info);
888 _media_svc_destroy_noti_item(noti_item);
893 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
895 int ret = MS_MEDIA_ERR_NONE;
896 sqlite3 *db_handle = (sqlite3 *)handle;
898 char *name_pinyin = NULL;
899 bool pinyin_support = FALSE;
901 GArray *query_array = NULL;
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 /*1. Rename directory */
913 /*Update Pinyin If Support Pinyin*/
914 name = g_path_get_basename(dst_path);
915 media_svc_check_pinyin_support(&pinyin_support);
916 if (pinyin_support) {
917 media_svc_get_pinyin(name, &name_pinyin);
918 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q",
919 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path);
921 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q",
922 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path);
925 ret = media_db_request_update_db_batch(sql, uid);
926 SQLITE3_SAFE_FREE(sql);
928 SAFE_FREE(name_pinyin);
929 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
931 /* 2. Update sub-dir, sub-file */
933 sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path);
934 ret = media_db_request_update_db_batch(sql, uid);
935 SQLITE3_SAFE_FREE(sql);
936 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
939 sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path);
940 ret = media_db_request_update_db_batch(sql, uid);
941 SQLITE3_SAFE_FREE(sql);
942 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
944 ret = _media_svc_sql_end_trans(uid);
945 if (ret != MS_MEDIA_ERR_NONE) {
946 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
950 /*3. Get and update thumbnail_path if exists */
951 sqlite3_stmt *sql_stmt = NULL;
953 sql = sqlite3_mprintf("SELECT path, thumbnail_path from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path);
954 media_svc_debug("[SQL query] : %s", sql);
956 ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
957 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret);
960 query_array = g_array_new(FALSE, FALSE, sizeof(char *));
961 media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed");
963 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
964 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
965 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
966 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
967 char *thumb_query = NULL;
969 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
970 SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path));
972 media_svc_error("media path is NULL");
973 SQLITE3_FINALIZE(sql_stmt);
974 return MS_MEDIA_ERR_DB_INTERNAL;
977 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
978 SAFE_STRLCPY(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path));
980 media_svc_error("thumbnail path is NULL");
981 SQLITE3_FINALIZE(sql_stmt);
982 return MS_MEDIA_ERR_DB_INTERNAL;
985 ms_user_storage_type_e storage_type = -1;
987 ret = ms_user_get_storage_type(uid, media_path, &storage_type);
988 if (ret != MS_MEDIA_ERR_NONE) {
989 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, media_path, storage_type);
990 SQLITE3_FINALIZE(sql_stmt);
994 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, media_path, THUMB_EXT, uid);
995 if (ret != MS_MEDIA_ERR_NONE) {
996 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
997 SQLITE3_FINALIZE(sql_stmt);
1001 thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path);
1002 g_array_append_val(query_array, thumb_query);
1004 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1005 if (ret != MS_MEDIA_ERR_NONE)
1006 media_svc_error("_media_svc_rename_file failed : %d", ret);
1009 SQLITE3_FINALIZE(sql_stmt);
1011 while (query_array->len != 0) {
1012 sql = g_array_index(query_array , char*, 0);
1013 g_array_remove_index(query_array, 0);
1014 ret = media_db_request_update_db(sql, uid);
1015 SQLITE3_SAFE_FREE(sql);
1016 if (ret != MS_MEDIA_ERR_NONE)
1017 media_svc_error("media_db_request_update_db failed : %d", ret);
1020 g_array_free(query_array, FALSE);
1023 media_svc_debug("Folder update is successful. Sending noti for this");
1024 /* Get notification info */
1025 media_svc_noti_item *noti_item = NULL;
1026 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1027 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1029 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1030 _media_svc_destroy_noti_item(noti_item);
1032 return MS_MEDIA_ERR_NONE;
1035 int media_svc_request_update_db(const char *db_query, uid_t uid)
1037 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1039 return _media_svc_sql_query(db_query, uid);
1042 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)
1044 int ret = MS_MEDIA_ERR_NONE;
1045 sqlite3 *db_handle = (sqlite3 *)handle;
1048 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1049 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1051 /* Get notification info */
1052 media_svc_noti_item *noti_item = NULL;
1053 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1054 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1056 if (folder_id != NULL) {
1057 uuid = strndup(folder_id, strlen(folder_id));
1059 if (noti_item->media_uuid != NULL) {
1060 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1062 _media_svc_destroy_noti_item(noti_item);
1063 media_svc_error("folder uuid is wrong");
1064 return MS_MEDIA_ERR_DB_INTERNAL;
1068 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1069 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1070 _media_svc_destroy_noti_item(noti_item);
1076 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1078 sqlite3 *db_handle = (sqlite3 *)handle;
1080 media_svc_debug_fenter();
1082 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1084 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1087 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1089 int ret = MS_MEDIA_ERR_NONE;
1090 sqlite3 *db_handle = (sqlite3 *)handle;
1091 time_t sto_time = 0;
1092 int cur_time = _media_svc_get_file_time(folder_path);
1093 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1095 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1097 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1098 if (ret == MS_MEDIA_ERR_NONE) {
1099 if (sto_time != cur_time)
1100 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1106 int media_svc_update_item_begin(int data_cnt)
1108 media_svc_debug("Transaction data count : [%d]", data_cnt);
1110 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1112 g_media_svc_update_item_data_cnt = data_cnt;
1113 g_media_svc_update_item_cur_data_cnt = 0;
1115 return MS_MEDIA_ERR_NONE;
1118 int media_svc_update_item_end(uid_t uid)
1120 int ret = MS_MEDIA_ERR_NONE;
1122 media_svc_debug_fenter();
1124 if (g_media_svc_update_item_cur_data_cnt > 0)
1125 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1127 g_media_svc_update_item_data_cnt = 1;
1128 g_media_svc_update_item_cur_data_cnt = 0;
1133 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1135 int ret = MS_MEDIA_ERR_NONE;
1136 sqlite3 *db_handle = (sqlite3 *)handle;
1138 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1140 media_svc_media_type_e media_type;
1141 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1142 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1144 media_svc_content_info_s content_info;
1145 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1148 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1149 if (ret != MS_MEDIA_ERR_NONE)
1152 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1153 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1155 _media_svc_destroy_content_info(&content_info);
1156 return MS_MEDIA_ERR_NONE;
1159 if (ret != MS_MEDIA_ERR_NONE) {
1160 _media_svc_destroy_content_info(&content_info);
1164 if (g_media_svc_update_item_data_cnt == 1) {
1166 ret = _media_svc_update_meta_with_data(&content_info);
1167 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1169 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1171 ret = _media_svc_update_meta_with_data(&content_info);
1172 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1174 g_media_svc_update_item_cur_data_cnt++;
1176 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1178 ret = _media_svc_update_meta_with_data(&content_info);
1179 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1181 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1182 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1184 g_media_svc_update_item_cur_data_cnt = 0;
1187 media_svc_error("Error in media_svc_update_item_meta");
1188 _media_svc_destroy_content_info(&content_info);
1189 return MS_MEDIA_ERR_INTERNAL;
1192 _media_svc_destroy_content_info(&content_info);
1197 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)
1199 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1202 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1204 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1206 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1209 int media_svc_check_pinyin_support(bool *support)
1211 *support = _media_svc_check_pinyin_support();
1213 return MS_MEDIA_ERR_NONE;
1216 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1218 int ret = MS_MEDIA_ERR_NONE;
1219 sqlite3 * db_handle = (sqlite3 *)handle;
1221 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1223 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1224 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1226 ret = _media_svc_update_media_view(db_handle, uid);
1227 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1232 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1234 int ret = MS_MEDIA_ERR_NONE;
1235 sqlite3 * db_handle = (sqlite3 *)handle;
1237 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1238 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1240 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1245 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1247 int ret = MS_MEDIA_ERR_NONE;
1248 sqlite3 * db_handle = (sqlite3 *)handle;
1250 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1251 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1253 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1258 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1260 int ret = MS_MEDIA_ERR_NONE;
1261 sqlite3 * db_handle = (sqlite3 *)handle;
1263 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1264 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1266 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1271 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1273 int ret = MS_MEDIA_ERR_NONE;
1275 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1280 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1282 sqlite3 * db_handle = (sqlite3 *)handle;
1284 media_svc_debug_fenter();
1286 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1287 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1289 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1292 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1294 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1295 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1297 if (content_info->added_time > 0)
1298 new_content_info->added_time = content_info->added_time;
1299 new_content_info->last_played_time = content_info->last_played_time;
1300 new_content_info->played_count = content_info->played_count;
1301 new_content_info->favourate = content_info->favourate;
1303 /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1304 if (STRING_VALID(content_info->file_name)) {
1305 /* Already filled in _media_svc_set_media_info() */
1306 SAFE_FREE(new_content_info->file_name);
1307 new_content_info->file_name = g_strdup(content_info->file_name);
1309 new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1310 new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1311 new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1312 new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1313 new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1314 new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1315 new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1316 new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1317 new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1318 new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1319 new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1320 new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1321 new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1322 new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1323 new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1324 new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1325 new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1326 new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1328 if (STRING_VALID(content_info->media_meta.datetaken)) {
1329 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1331 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1332 if (new_content_info->timeline == 0) {
1333 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1334 new_content_info->timeline = new_content_info->modified_time;
1336 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1340 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1341 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1342 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1343 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1344 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1346 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1347 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1348 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1349 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1350 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1351 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1353 new_content_info->media_meta.rating = content_info->media_meta.rating;
1355 return MS_MEDIA_ERR_NONE;
1358 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1360 int ret = MS_MEDIA_ERR_NONE;
1361 sqlite3 *db_handle = (sqlite3 *)handle;
1362 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1363 bool append_album = FALSE;
1365 /* Checking parameters if they are valid */
1366 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1367 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1368 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1370 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1372 media_svc_content_info_s _new_content_info;
1373 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1375 media_svc_media_type_e media_type;
1377 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1378 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1380 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1381 media_svc_debug("Do nothing[%d]", media_type);
1382 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1383 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1385 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1387 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1389 /* Extracting thumbnail */
1390 if (_new_content_info.thumbnail_path == NULL) {
1391 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1392 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1394 ret = _media_svc_request_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), uid);
1395 if (ret == MS_MEDIA_ERR_NONE)
1396 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1400 /* set othere data to the structure, which is passed as parameters */
1401 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1403 /* Set or Get folder id */
1404 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);
1405 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1407 _new_content_info.folder_uuid = g_strdup(folder_uuid);
1408 media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1410 /* register album table data */
1413 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1414 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1416 if (ret != MS_MEDIA_ERR_NONE) {
1417 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1418 media_svc_debug("album does not exist. So start to make album art");
1419 append_album = TRUE;
1421 media_svc_debug("other error");
1422 append_album = FALSE;
1425 _new_content_info.album_id = album_id;
1426 append_album = FALSE;
1428 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1429 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1430 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1433 media_svc_debug("album already exists. don't need to make album art");
1434 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1435 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1440 if (append_album == TRUE) {
1441 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1442 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1443 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);
1445 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1447 _new_content_info.album_id = album_id;
1450 /* Insert to db - calling _media_svc_insert_item_with_data */
1451 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1453 if (ret == MS_MEDIA_ERR_NONE)
1454 media_svc_debug("Insertion is successful.");
1456 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1457 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1459 _media_svc_destroy_content_info(&_new_content_info);
1461 /* handling returned value - important */
1465 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1467 _media_svc_destroy_content_info(content_info);
1470 int media_svc_generate_uuid(char **uuid)
1472 char *gen_uuid = NULL;
1473 gen_uuid = _media_info_generate_uuid();
1474 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1476 *uuid = strdup(gen_uuid);
1478 return MS_MEDIA_ERR_NONE;
1481 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1483 sqlite3 * db_handle = (sqlite3 *)handle;
1485 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1487 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1490 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1492 sqlite3 * db_handle = (sqlite3 *)handle;
1494 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1495 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1496 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1497 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1499 return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1502 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1504 sqlite3 * db_handle = (sqlite3 *)handle;
1506 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1507 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1508 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1510 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1513 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)
1515 int ret = MS_MEDIA_ERR_NONE;
1516 sqlite3 *db_handle = (sqlite3 *)handle;
1518 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1519 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1520 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1521 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1523 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1524 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1526 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1527 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1528 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1530 ret = _media_svc_update_media_view(db_handle, uid);
1531 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1537 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1539 int ret = MS_MEDIA_ERR_NONE;
1540 sqlite3 *db_handle = (sqlite3 *)handle;
1541 media_svc_storage_type_e storage_type;
1543 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1544 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1546 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1547 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1549 ret = _media_svc_delete_storage(storage_id, uid);
1550 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1552 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1553 if (ret != MS_MEDIA_ERR_NONE)
1554 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1556 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1557 ret = _media_svc_drop_media_table(storage_id, uid);
1558 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1560 ret = _media_svc_update_media_view(db_handle, uid);
1561 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1567 int media_svc_insert_folder_begin(int data_cnt)
1569 media_svc_debug("Transaction data count : [%d]", data_cnt);
1571 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1573 g_media_svc_insert_folder_data_cnt = data_cnt;
1574 g_media_svc_insert_folder_cur_data_cnt = 0;
1576 return MS_MEDIA_ERR_NONE;
1579 int media_svc_insert_folder_end(uid_t uid)
1581 int ret = MS_MEDIA_ERR_NONE;
1583 media_svc_debug_fenter();
1585 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1586 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1588 g_media_svc_insert_folder_data_cnt = 1;
1589 g_media_svc_insert_folder_cur_data_cnt = 0;
1594 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1596 int ret = MS_MEDIA_ERR_NONE;
1597 sqlite3 * db_handle = (sqlite3 *)handle;
1598 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1600 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1601 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1602 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1603 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1605 if (g_media_svc_insert_folder_data_cnt == 1) {
1607 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1609 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1611 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1612 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1614 g_media_svc_insert_folder_cur_data_cnt++;
1616 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1618 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1619 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1621 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1622 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1624 g_media_svc_insert_folder_cur_data_cnt = 0;
1627 media_svc_error("Error in media_svc_set_insert_folder");
1628 return MS_MEDIA_ERR_INTERNAL;
1634 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1636 int ret = MS_MEDIA_ERR_NONE;
1638 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1643 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1645 int ret = MS_MEDIA_ERR_NONE;
1646 sqlite3 * db_handle = (sqlite3 *)handle;
1648 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1650 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1655 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)
1657 int ret = MS_MEDIA_ERR_NONE;
1658 sqlite3 * db_handle = (sqlite3 *)handle;
1659 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1660 media_svc_media_type_e media_type;
1662 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1663 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1664 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1665 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1667 media_svc_content_info_s content_info;
1668 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1670 /*Set basic media info*/
1671 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1672 if (ret != MS_MEDIA_ERR_NONE) {
1673 media_svc_error("_media_svc_set_media_info fail %d", ret);
1677 /*Set or Get folder id*/
1678 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1679 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1681 content_info.folder_uuid = g_strdup(folder_uuid);
1682 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1684 if (g_media_svc_insert_item_data_cnt == 1) {
1686 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1687 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1689 if (g_insert_with_noti)
1690 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1691 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1693 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1694 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1696 if (g_insert_with_noti)
1697 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1699 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1700 g_media_svc_insert_item_cur_data_cnt++;
1702 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1704 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1705 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1707 if (g_insert_with_noti)
1708 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1710 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1711 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1713 media_svc_debug("_media_svc_list_query_do over");
1715 if (g_insert_with_noti) {
1716 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1717 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1718 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1720 /* Recreate noti list */
1721 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1722 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1725 g_media_svc_insert_item_cur_data_cnt = 0;
1728 media_svc_error("Error in media_svc_insert_item_pass1");
1729 _media_svc_destroy_content_info(&content_info);
1730 return MS_MEDIA_ERR_INTERNAL;
1733 _media_svc_destroy_content_info(&content_info);
1735 return MS_MEDIA_ERR_NONE;
1738 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1740 int ret = MS_MEDIA_ERR_NONE;
1741 sqlite3 * db_handle = (sqlite3 *)handle;
1743 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1744 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1746 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1751 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1753 int ret = MS_MEDIA_ERR_NONE;
1755 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1760 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1762 int ret = MS_MEDIA_ERR_NONE;
1763 sqlite3 * db_handle = (sqlite3 *)handle;
1764 time_t modified_time = 0;
1765 int system_time = 0;
1767 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1769 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1771 system_time = _media_svc_get_file_time(path);
1772 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1774 if (system_time != modified_time && system_time != 0)
1782 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
1784 sqlite3 * db_handle = (sqlite3 *)handle;
1786 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1787 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1789 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1792 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1794 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1795 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1797 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1800 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1802 int ret = MS_MEDIA_ERR_NONE;
1803 sqlite3 * db_handle = (sqlite3 *)handle;
1806 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1807 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1808 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1810 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1811 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1814 media_svc_debug("item is exist in database");
1815 return MS_MEDIA_ERR_NONE;
1817 media_svc_debug("item is not exist in database");
1818 return MS_MEDIA_ERR_DB_NO_RECORD;
1821 return MS_MEDIA_ERR_NONE;
1824 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1826 int ret = MS_MEDIA_ERR_NONE;
1827 sqlite3 * db_handle = (sqlite3 *)handle;
1829 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1830 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1832 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1837 int media_svc_append_query(const char *query, uid_t uid)
1839 int ret = MS_MEDIA_ERR_NONE;
1841 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1843 ret = _media_svc_append_query_list(query, uid);
1848 int media_svc_send_query(uid_t uid)
1850 int ret = MS_MEDIA_ERR_NONE;
1852 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1857 int media_svc_get_media_type(const char *path, int *mediatype)
1859 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1861 return _media_svc_get_media_type(path, mediatype);