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_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1069 _media_svc_destroy_noti_item(noti_item);
1075 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1077 sqlite3 *db_handle = (sqlite3 *)handle;
1079 media_svc_debug_fenter();
1081 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1083 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1086 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1088 int ret = MS_MEDIA_ERR_NONE;
1089 sqlite3 *db_handle = (sqlite3 *)handle;
1090 time_t sto_time = 0;
1091 int cur_time = _media_svc_get_file_time(folder_path);
1092 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1094 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1096 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1097 if (ret == MS_MEDIA_ERR_NONE) {
1098 if (sto_time != cur_time)
1099 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1105 int media_svc_update_item_begin(int data_cnt)
1107 media_svc_debug("Transaction data count : [%d]", data_cnt);
1109 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1111 g_media_svc_update_item_data_cnt = data_cnt;
1112 g_media_svc_update_item_cur_data_cnt = 0;
1114 return MS_MEDIA_ERR_NONE;
1117 int media_svc_update_item_end(uid_t uid)
1119 int ret = MS_MEDIA_ERR_NONE;
1121 media_svc_debug_fenter();
1123 if (g_media_svc_update_item_cur_data_cnt > 0)
1124 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1126 g_media_svc_update_item_data_cnt = 1;
1127 g_media_svc_update_item_cur_data_cnt = 0;
1132 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1134 int ret = MS_MEDIA_ERR_NONE;
1135 sqlite3 *db_handle = (sqlite3 *)handle;
1137 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1139 media_svc_media_type_e media_type;
1140 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1141 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1143 media_svc_content_info_s content_info;
1144 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1147 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1148 if (ret != MS_MEDIA_ERR_NONE)
1151 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1152 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1154 _media_svc_destroy_content_info(&content_info);
1155 return MS_MEDIA_ERR_NONE;
1158 if (ret != MS_MEDIA_ERR_NONE) {
1159 _media_svc_destroy_content_info(&content_info);
1163 if (g_media_svc_update_item_data_cnt == 1) {
1165 ret = _media_svc_update_meta_with_data(&content_info);
1166 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1168 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1170 ret = _media_svc_update_meta_with_data(&content_info);
1171 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1173 g_media_svc_update_item_cur_data_cnt++;
1175 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1177 ret = _media_svc_update_meta_with_data(&content_info);
1178 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1180 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1181 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1183 g_media_svc_update_item_cur_data_cnt = 0;
1186 media_svc_error("Error in media_svc_update_item_meta");
1187 _media_svc_destroy_content_info(&content_info);
1188 return MS_MEDIA_ERR_INTERNAL;
1191 _media_svc_destroy_content_info(&content_info);
1196 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)
1198 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1201 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1203 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1205 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1208 int media_svc_check_pinyin_support(bool *support)
1210 *support = _media_svc_check_pinyin_support();
1212 return MS_MEDIA_ERR_NONE;
1215 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1217 int ret = MS_MEDIA_ERR_NONE;
1218 sqlite3 * db_handle = (sqlite3 *)handle;
1220 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1222 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1223 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1225 ret = _media_svc_update_media_view(db_handle, uid);
1226 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1231 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1233 int ret = MS_MEDIA_ERR_NONE;
1234 sqlite3 * db_handle = (sqlite3 *)handle;
1236 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1237 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1239 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1244 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1246 int ret = MS_MEDIA_ERR_NONE;
1247 sqlite3 * db_handle = (sqlite3 *)handle;
1249 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1250 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1252 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1257 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1259 int ret = MS_MEDIA_ERR_NONE;
1260 sqlite3 * db_handle = (sqlite3 *)handle;
1262 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1263 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1265 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1270 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1272 int ret = MS_MEDIA_ERR_NONE;
1274 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1279 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1281 sqlite3 * db_handle = (sqlite3 *)handle;
1283 media_svc_debug_fenter();
1285 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1286 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1288 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1291 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1293 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1294 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1296 if (content_info->added_time > 0)
1297 new_content_info->added_time = content_info->added_time;
1298 new_content_info->last_played_time = content_info->last_played_time;
1299 new_content_info->played_count = content_info->played_count;
1300 new_content_info->favourate = content_info->favourate;
1302 /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1303 if (STRING_VALID(content_info->file_name)) {
1304 /* Already filled in _media_svc_set_media_info() */
1305 SAFE_FREE(new_content_info->file_name);
1306 new_content_info->file_name = g_strdup(content_info->file_name);
1308 new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1309 new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1310 new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1311 new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1312 new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1313 new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1314 new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1315 new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1316 new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1317 new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1318 new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1319 new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1320 new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1321 new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1322 new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1323 new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1324 new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1325 new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1327 if (STRING_VALID(content_info->media_meta.datetaken)) {
1328 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1330 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1331 if (new_content_info->timeline == 0) {
1332 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1333 new_content_info->timeline = new_content_info->modified_time;
1335 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1339 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1340 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1341 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1342 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1343 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1345 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1346 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1347 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1348 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1349 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1350 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1352 new_content_info->media_meta.rating = content_info->media_meta.rating;
1354 return MS_MEDIA_ERR_NONE;
1357 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1359 int ret = MS_MEDIA_ERR_NONE;
1360 sqlite3 *db_handle = (sqlite3 *)handle;
1361 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1362 bool append_album = FALSE;
1364 /* Checking parameters if they are valid */
1365 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1366 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1367 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1369 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1371 media_svc_content_info_s _new_content_info;
1372 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1374 media_svc_media_type_e media_type;
1376 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1377 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1379 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1380 media_svc_debug("Do nothing[%d]", media_type);
1381 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1382 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1384 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1386 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1388 /* Extracting thumbnail */
1389 if (_new_content_info.thumbnail_path == NULL) {
1390 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1391 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1393 ret = _media_svc_request_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), uid);
1394 if (ret == MS_MEDIA_ERR_NONE)
1395 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1399 /* set othere data to the structure, which is passed as parameters */
1400 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1402 /* Set or Get folder id */
1403 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);
1404 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1406 _new_content_info.folder_uuid = g_strdup(folder_uuid);
1407 media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1409 /* register album table data */
1412 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1413 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1415 if (ret != MS_MEDIA_ERR_NONE) {
1416 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1417 media_svc_debug("album does not exist. So start to make album art");
1418 append_album = TRUE;
1420 media_svc_debug("other error");
1421 append_album = FALSE;
1424 _new_content_info.album_id = album_id;
1425 append_album = FALSE;
1427 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1428 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1429 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1432 media_svc_debug("album already exists. don't need to make album art");
1433 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1434 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1439 if (append_album == TRUE) {
1440 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1441 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1442 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);
1444 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1446 _new_content_info.album_id = album_id;
1449 /* Insert to db - calling _media_svc_insert_item_with_data */
1450 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1452 if (ret == MS_MEDIA_ERR_NONE)
1453 media_svc_debug("Insertion is successful.");
1455 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1456 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1458 _media_svc_destroy_content_info(&_new_content_info);
1460 /* handling returned value - important */
1464 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1466 _media_svc_destroy_content_info(content_info);
1469 int media_svc_generate_uuid(char **uuid)
1471 char *gen_uuid = NULL;
1472 gen_uuid = _media_info_generate_uuid();
1473 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1475 *uuid = strdup(gen_uuid);
1477 return MS_MEDIA_ERR_NONE;
1480 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1482 sqlite3 * db_handle = (sqlite3 *)handle;
1484 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1486 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1489 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1491 sqlite3 * db_handle = (sqlite3 *)handle;
1493 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1494 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1495 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1496 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1498 return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1501 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1503 sqlite3 * db_handle = (sqlite3 *)handle;
1505 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1506 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1507 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1509 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1512 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)
1514 int ret = MS_MEDIA_ERR_NONE;
1515 sqlite3 *db_handle = (sqlite3 *)handle;
1517 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1518 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1519 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1520 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1522 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1523 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1525 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1526 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1527 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1529 ret = _media_svc_update_media_view(db_handle, uid);
1530 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1536 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1538 int ret = MS_MEDIA_ERR_NONE;
1539 sqlite3 *db_handle = (sqlite3 *)handle;
1540 media_svc_storage_type_e storage_type;
1542 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1543 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1545 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1546 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1548 ret = _media_svc_delete_storage(storage_id, uid);
1549 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1551 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1552 if (ret != MS_MEDIA_ERR_NONE)
1553 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1555 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1556 ret = _media_svc_drop_media_table(storage_id, uid);
1557 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1559 ret = _media_svc_update_media_view(db_handle, uid);
1560 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1566 int media_svc_insert_folder_begin(int data_cnt)
1568 media_svc_debug("Transaction data count : [%d]", data_cnt);
1570 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1572 g_media_svc_insert_folder_data_cnt = data_cnt;
1573 g_media_svc_insert_folder_cur_data_cnt = 0;
1575 return MS_MEDIA_ERR_NONE;
1578 int media_svc_insert_folder_end(uid_t uid)
1580 int ret = MS_MEDIA_ERR_NONE;
1582 media_svc_debug_fenter();
1584 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1585 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1587 g_media_svc_insert_folder_data_cnt = 1;
1588 g_media_svc_insert_folder_cur_data_cnt = 0;
1593 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1595 int ret = MS_MEDIA_ERR_NONE;
1596 sqlite3 * db_handle = (sqlite3 *)handle;
1597 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1599 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1600 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1601 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1602 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1604 if (g_media_svc_insert_folder_data_cnt == 1) {
1606 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1608 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1610 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1611 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1613 g_media_svc_insert_folder_cur_data_cnt++;
1615 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1617 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1618 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1620 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1621 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1623 g_media_svc_insert_folder_cur_data_cnt = 0;
1626 media_svc_error("Error in media_svc_set_insert_folder");
1627 return MS_MEDIA_ERR_INTERNAL;
1633 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1635 int ret = MS_MEDIA_ERR_NONE;
1637 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1642 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1644 int ret = MS_MEDIA_ERR_NONE;
1645 sqlite3 * db_handle = (sqlite3 *)handle;
1647 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1649 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1654 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)
1656 int ret = MS_MEDIA_ERR_NONE;
1657 sqlite3 * db_handle = (sqlite3 *)handle;
1658 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1659 media_svc_media_type_e media_type;
1661 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1662 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1663 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1664 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1666 media_svc_content_info_s content_info;
1667 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1669 /*Set basic media info*/
1670 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1671 if (ret != MS_MEDIA_ERR_NONE) {
1672 media_svc_error("_media_svc_set_media_info fail %d", ret);
1676 /*Set or Get folder id*/
1677 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1678 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1680 content_info.folder_uuid = g_strdup(folder_uuid);
1681 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1683 if (g_media_svc_insert_item_data_cnt == 1) {
1685 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1686 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1688 if (g_insert_with_noti)
1689 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1690 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1692 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1693 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1695 if (g_insert_with_noti)
1696 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1698 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1699 g_media_svc_insert_item_cur_data_cnt++;
1701 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1703 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1704 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1706 if (g_insert_with_noti)
1707 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1709 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1710 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1712 media_svc_debug("_media_svc_list_query_do over");
1714 if (g_insert_with_noti) {
1715 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1716 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1717 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1719 /* Recreate noti list */
1720 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1721 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1724 g_media_svc_insert_item_cur_data_cnt = 0;
1727 media_svc_error("Error in media_svc_insert_item_pass1");
1728 _media_svc_destroy_content_info(&content_info);
1729 return MS_MEDIA_ERR_INTERNAL;
1732 _media_svc_destroy_content_info(&content_info);
1734 return MS_MEDIA_ERR_NONE;
1737 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1739 int ret = MS_MEDIA_ERR_NONE;
1740 sqlite3 * db_handle = (sqlite3 *)handle;
1742 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1743 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1745 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1750 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1752 int ret = MS_MEDIA_ERR_NONE;
1754 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1759 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1761 int ret = MS_MEDIA_ERR_NONE;
1762 sqlite3 * db_handle = (sqlite3 *)handle;
1763 time_t modified_time = 0;
1764 int system_time = 0;
1766 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1768 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1770 system_time = _media_svc_get_file_time(path);
1771 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1773 if (system_time != modified_time && system_time != 0)
1781 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
1783 sqlite3 * db_handle = (sqlite3 *)handle;
1785 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1786 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1788 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1791 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1793 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1794 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1796 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1799 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1801 int ret = MS_MEDIA_ERR_NONE;
1802 sqlite3 * db_handle = (sqlite3 *)handle;
1805 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1806 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1807 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1809 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1810 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1813 media_svc_debug("item is exist in database");
1814 return MS_MEDIA_ERR_NONE;
1816 media_svc_debug("item is not exist in database");
1817 return MS_MEDIA_ERR_DB_NO_RECORD;
1820 return MS_MEDIA_ERR_NONE;
1823 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1825 int ret = MS_MEDIA_ERR_NONE;
1826 sqlite3 * db_handle = (sqlite3 *)handle;
1828 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1829 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1831 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1836 int media_svc_append_query(const char *query, uid_t uid)
1838 int ret = MS_MEDIA_ERR_NONE;
1840 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1842 ret = _media_svc_append_query_list(query, uid);
1847 int media_svc_send_query(uid_t uid)
1849 int ret = MS_MEDIA_ERR_NONE;
1851 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1856 int media_svc_get_media_type(const char *path, int *mediatype)
1858 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1860 return _media_svc_get_media_type(path, mediatype);