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_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, 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, media_type, 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_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, 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;
902 GArray *query_array = NULL;
904 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
905 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
906 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
908 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
910 ret = _media_svc_sql_begin_trans(uid);
911 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
913 /*1. Rename directory */
914 /*Update Pinyin If Support Pinyin*/
915 name = g_path_get_basename(dst_path);
916 media_svc_check_pinyin_support(&pinyin_support);
917 if (pinyin_support) {
918 media_svc_get_pinyin(name, &name_pinyin);
919 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q",
920 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path);
922 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q",
923 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path);
926 ret = media_db_request_update_db_batch(sql, uid);
927 SQLITE3_SAFE_FREE(sql);
929 SAFE_FREE(name_pinyin);
930 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
932 /* 2. Update sub-dir, sub-file */
934 sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path);
935 ret = media_db_request_update_db_batch(sql, uid);
936 SQLITE3_SAFE_FREE(sql);
937 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
940 sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path);
941 ret = media_db_request_update_db_batch(sql, uid);
942 SQLITE3_SAFE_FREE(sql);
943 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
945 ret = _media_svc_sql_end_trans(uid);
946 if (ret != MS_MEDIA_ERR_NONE) {
947 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
951 /*3. Get and update thumbnail_path if exists */
952 sqlite3_stmt *sql_stmt = NULL;
954 sql = sqlite3_mprintf("SELECT path, thumbnail_path, media_type from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path);
955 media_svc_debug("[SQL query] : %s", sql);
957 ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
958 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret);
961 query_array = g_array_new(FALSE, FALSE, sizeof(char *));
962 media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed");
964 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
965 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
966 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
967 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
968 char *thumb_query = NULL;
970 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
971 SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path));
973 media_svc_error("media path is NULL");
974 SQLITE3_FINALIZE(sql_stmt);
975 return MS_MEDIA_ERR_DB_INTERNAL;
978 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
979 SAFE_STRLCPY(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path));
981 media_svc_error("thumbnail path is NULL");
982 SQLITE3_FINALIZE(sql_stmt);
983 return MS_MEDIA_ERR_DB_INTERNAL;
986 media_type = sqlite3_column_int(sql_stmt, 2);
988 ms_user_storage_type_e storage_type = -1;
990 ret = ms_user_get_storage_type(uid, media_path, &storage_type);
991 if (ret != MS_MEDIA_ERR_NONE) {
992 media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, media_path, storage_type);
993 SQLITE3_FINALIZE(sql_stmt);
997 ret = _media_svc_get_thumbnail_path(storage_type, media_type, media_new_thumb_path, media_path, THUMB_EXT, uid);
998 if (ret != MS_MEDIA_ERR_NONE) {
999 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1000 SQLITE3_FINALIZE(sql_stmt);
1004 thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path);
1005 g_array_append_val(query_array, thumb_query);
1007 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1008 if (ret != MS_MEDIA_ERR_NONE)
1009 media_svc_error("_media_svc_rename_file failed : %d", ret);
1012 SQLITE3_FINALIZE(sql_stmt);
1014 while (query_array->len != 0) {
1015 sql = g_array_index(query_array , char*, 0);
1016 g_array_remove_index(query_array, 0);
1017 ret = media_db_request_update_db(sql, uid);
1018 SQLITE3_SAFE_FREE(sql);
1019 if (ret != MS_MEDIA_ERR_NONE)
1020 media_svc_error("media_db_request_update_db failed : %d", ret);
1023 g_array_free(query_array, FALSE);
1026 media_svc_debug("Folder update is successful. Sending noti for this");
1027 /* Get notification info */
1028 media_svc_noti_item *noti_item = NULL;
1029 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1030 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1032 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1033 _media_svc_destroy_noti_item(noti_item);
1035 return MS_MEDIA_ERR_NONE;
1038 int media_svc_request_update_db(const char *db_query, uid_t uid)
1040 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1042 return _media_svc_sql_query(db_query, uid);
1045 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)
1047 int ret = MS_MEDIA_ERR_NONE;
1048 sqlite3 *db_handle = (sqlite3 *)handle;
1051 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1052 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1054 /* Get notification info */
1055 media_svc_noti_item *noti_item = NULL;
1056 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1057 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1059 if (folder_id != NULL) {
1060 uuid = strndup(folder_id, strlen(folder_id));
1062 if (noti_item->media_uuid != NULL) {
1063 uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1065 _media_svc_destroy_noti_item(noti_item);
1066 media_svc_error("folder uuid is wrong");
1067 return MS_MEDIA_ERR_DB_INTERNAL;
1071 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1072 _media_svc_destroy_noti_item(noti_item);
1078 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1080 sqlite3 *db_handle = (sqlite3 *)handle;
1082 media_svc_debug_fenter();
1084 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1086 return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1089 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1091 int ret = MS_MEDIA_ERR_NONE;
1092 sqlite3 *db_handle = (sqlite3 *)handle;
1093 time_t sto_time = 0;
1094 int cur_time = _media_svc_get_file_time(folder_path);
1095 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1097 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1099 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1100 if (ret == MS_MEDIA_ERR_NONE) {
1101 if (sto_time != cur_time)
1102 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1108 int media_svc_update_item_begin(int data_cnt)
1110 media_svc_debug("Transaction data count : [%d]", data_cnt);
1112 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1114 g_media_svc_update_item_data_cnt = data_cnt;
1115 g_media_svc_update_item_cur_data_cnt = 0;
1117 return MS_MEDIA_ERR_NONE;
1120 int media_svc_update_item_end(uid_t uid)
1122 int ret = MS_MEDIA_ERR_NONE;
1124 media_svc_debug_fenter();
1126 if (g_media_svc_update_item_cur_data_cnt > 0)
1127 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1129 g_media_svc_update_item_data_cnt = 1;
1130 g_media_svc_update_item_cur_data_cnt = 0;
1135 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1137 int ret = MS_MEDIA_ERR_NONE;
1138 sqlite3 *db_handle = (sqlite3 *)handle;
1140 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1142 media_svc_media_type_e media_type;
1143 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1144 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1146 media_svc_content_info_s content_info;
1147 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1150 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1151 if (ret != MS_MEDIA_ERR_NONE)
1154 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1155 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1157 _media_svc_destroy_content_info(&content_info);
1158 return MS_MEDIA_ERR_NONE;
1161 if (ret != MS_MEDIA_ERR_NONE) {
1162 _media_svc_destroy_content_info(&content_info);
1166 if (g_media_svc_update_item_data_cnt == 1) {
1168 ret = _media_svc_update_meta_with_data(&content_info);
1169 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1171 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1173 ret = _media_svc_update_meta_with_data(&content_info);
1174 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1176 g_media_svc_update_item_cur_data_cnt++;
1178 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1180 ret = _media_svc_update_meta_with_data(&content_info);
1181 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1183 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1184 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1186 g_media_svc_update_item_cur_data_cnt = 0;
1189 media_svc_error("Error in media_svc_update_item_meta");
1190 _media_svc_destroy_content_info(&content_info);
1191 return MS_MEDIA_ERR_INTERNAL;
1194 _media_svc_destroy_content_info(&content_info);
1199 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)
1201 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1204 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1206 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1208 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1211 int media_svc_check_pinyin_support(bool *support)
1213 *support = _media_svc_check_pinyin_support();
1215 return MS_MEDIA_ERR_NONE;
1218 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1220 int ret = MS_MEDIA_ERR_NONE;
1221 sqlite3 * db_handle = (sqlite3 *)handle;
1223 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1225 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1226 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1228 ret = _media_svc_update_media_view(db_handle, uid);
1229 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1234 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1236 int ret = MS_MEDIA_ERR_NONE;
1237 sqlite3 * db_handle = (sqlite3 *)handle;
1239 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1240 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1242 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1247 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1249 int ret = MS_MEDIA_ERR_NONE;
1250 sqlite3 * db_handle = (sqlite3 *)handle;
1252 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1253 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1255 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1260 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1262 int ret = MS_MEDIA_ERR_NONE;
1263 sqlite3 * db_handle = (sqlite3 *)handle;
1265 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1266 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1268 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1273 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1275 int ret = MS_MEDIA_ERR_NONE;
1277 ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1282 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1284 sqlite3 * db_handle = (sqlite3 *)handle;
1286 media_svc_debug_fenter();
1288 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1289 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1291 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1294 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1296 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1297 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1299 if (content_info->added_time > 0)
1300 new_content_info->added_time = content_info->added_time;
1301 new_content_info->last_played_time = content_info->last_played_time;
1302 new_content_info->played_count = content_info->played_count;
1303 new_content_info->favourate = content_info->favourate;
1305 /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1306 if (STRING_VALID(content_info->file_name)) {
1307 /* Already filled in _media_svc_set_media_info() */
1308 SAFE_FREE(new_content_info->file_name);
1309 new_content_info->file_name = g_strdup(content_info->file_name);
1311 new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1312 new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1313 new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1314 new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1315 new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1316 new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1317 new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1318 new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1319 new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1320 new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1321 new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1322 new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1323 new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1324 new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1325 new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1326 new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1327 new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1328 new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1330 if (STRING_VALID(content_info->media_meta.datetaken)) {
1331 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1333 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1334 if (new_content_info->timeline == 0) {
1335 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1336 new_content_info->timeline = new_content_info->modified_time;
1338 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1342 new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1343 /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1344 /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1345 /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1346 /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1348 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1349 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1350 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1351 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1352 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1353 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1355 new_content_info->media_meta.rating = content_info->media_meta.rating;
1357 return MS_MEDIA_ERR_NONE;
1360 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1362 int ret = MS_MEDIA_ERR_NONE;
1363 sqlite3 *db_handle = (sqlite3 *)handle;
1364 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1365 bool append_album = FALSE;
1367 /* Checking parameters if they are valid */
1368 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1369 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1370 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1372 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1374 media_svc_content_info_s _new_content_info;
1375 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1377 media_svc_media_type_e media_type;
1379 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1380 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1382 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1383 media_svc_debug("Do nothing[%d]", media_type);
1384 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1385 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1387 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1389 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1391 /* Extracting thumbnail */
1392 if (_new_content_info.thumbnail_path == NULL) {
1393 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1394 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1396 ret = _media_svc_create_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
1397 if (ret == MS_MEDIA_ERR_NONE)
1398 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1402 /* set othere data to the structure, which is passed as parameters */
1403 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1405 /* Set or Get folder id */
1406 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);
1407 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1409 _new_content_info.folder_uuid = g_strdup(folder_uuid);
1410 media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1412 /* register album table data */
1415 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1416 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1418 if (ret != MS_MEDIA_ERR_NONE) {
1419 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1420 media_svc_debug("album does not exist. So start to make album art");
1421 append_album = TRUE;
1423 media_svc_debug("other error");
1424 append_album = FALSE;
1427 _new_content_info.album_id = album_id;
1428 append_album = FALSE;
1430 if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1431 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1432 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1435 media_svc_debug("album already exists. don't need to make album art");
1436 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1437 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1442 if (append_album == TRUE) {
1443 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1444 (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1445 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);
1447 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1449 _new_content_info.album_id = album_id;
1452 /* Insert to db - calling _media_svc_insert_item_with_data */
1453 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1455 if (ret == MS_MEDIA_ERR_NONE)
1456 media_svc_debug("Insertion is successful.");
1458 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1459 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1461 _media_svc_destroy_content_info(&_new_content_info);
1463 /* handling returned value - important */
1467 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1469 _media_svc_destroy_content_info(content_info);
1472 int media_svc_generate_uuid(char **uuid)
1474 char *gen_uuid = NULL;
1475 gen_uuid = _media_info_generate_uuid();
1476 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1478 *uuid = strdup(gen_uuid);
1480 return MS_MEDIA_ERR_NONE;
1483 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1485 sqlite3 * db_handle = (sqlite3 *)handle;
1487 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1489 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1492 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1494 sqlite3 * db_handle = (sqlite3 *)handle;
1496 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1497 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1498 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1499 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1501 return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1504 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1506 sqlite3 * db_handle = (sqlite3 *)handle;
1508 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1509 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1510 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1512 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1515 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)
1517 int ret = MS_MEDIA_ERR_NONE;
1518 sqlite3 *db_handle = (sqlite3 *)handle;
1520 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1521 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1522 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1523 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1525 ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1526 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1528 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1529 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1530 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1532 ret = _media_svc_update_media_view(db_handle, uid);
1533 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1539 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1541 int ret = MS_MEDIA_ERR_NONE;
1542 sqlite3 *db_handle = (sqlite3 *)handle;
1543 media_svc_storage_type_e storage_type;
1545 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1546 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1548 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1549 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1551 ret = _media_svc_delete_storage(storage_id, uid);
1552 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1554 ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1555 if (ret != MS_MEDIA_ERR_NONE)
1556 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1558 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1559 ret = _media_svc_drop_media_table(storage_id, uid);
1560 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1562 ret = _media_svc_update_media_view(db_handle, uid);
1563 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1569 int media_svc_insert_folder_begin(int data_cnt)
1571 media_svc_debug("Transaction data count : [%d]", data_cnt);
1573 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1575 g_media_svc_insert_folder_data_cnt = data_cnt;
1576 g_media_svc_insert_folder_cur_data_cnt = 0;
1578 return MS_MEDIA_ERR_NONE;
1581 int media_svc_insert_folder_end(uid_t uid)
1583 int ret = MS_MEDIA_ERR_NONE;
1585 media_svc_debug_fenter();
1587 if (g_media_svc_insert_folder_cur_data_cnt > 0)
1588 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1590 g_media_svc_insert_folder_data_cnt = 1;
1591 g_media_svc_insert_folder_cur_data_cnt = 0;
1596 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1598 int ret = MS_MEDIA_ERR_NONE;
1599 sqlite3 * db_handle = (sqlite3 *)handle;
1600 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1602 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1603 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1604 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1605 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1607 if (g_media_svc_insert_folder_data_cnt == 1) {
1609 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1611 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1613 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1614 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1616 g_media_svc_insert_folder_cur_data_cnt++;
1618 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1620 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1621 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1623 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1624 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1626 g_media_svc_insert_folder_cur_data_cnt = 0;
1629 media_svc_error("Error in media_svc_set_insert_folder");
1630 return MS_MEDIA_ERR_INTERNAL;
1636 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1638 int ret = MS_MEDIA_ERR_NONE;
1640 ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1645 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1647 int ret = MS_MEDIA_ERR_NONE;
1648 sqlite3 * db_handle = (sqlite3 *)handle;
1650 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1652 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1657 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)
1659 int ret = MS_MEDIA_ERR_NONE;
1660 sqlite3 * db_handle = (sqlite3 *)handle;
1661 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1662 media_svc_media_type_e media_type;
1664 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1665 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1666 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1667 media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1669 media_svc_content_info_s content_info;
1670 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1672 /*Set basic media info*/
1673 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1674 if (ret != MS_MEDIA_ERR_NONE) {
1675 media_svc_error("_media_svc_set_media_info fail %d", ret);
1679 /*Set or Get folder id*/
1680 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1681 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1683 content_info.folder_uuid = g_strdup(folder_uuid);
1684 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1686 if (g_media_svc_insert_item_data_cnt == 1) {
1688 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1689 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1691 if (g_insert_with_noti)
1692 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1693 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1695 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1696 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1698 if (g_insert_with_noti)
1699 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1701 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1702 g_media_svc_insert_item_cur_data_cnt++;
1704 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1706 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1707 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1709 if (g_insert_with_noti)
1710 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1712 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1713 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1715 media_svc_debug("_media_svc_list_query_do over");
1717 if (g_insert_with_noti) {
1718 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1719 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1720 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1722 /* Recreate noti list */
1723 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1724 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1727 g_media_svc_insert_item_cur_data_cnt = 0;
1730 media_svc_error("Error in media_svc_insert_item_pass1");
1731 _media_svc_destroy_content_info(&content_info);
1732 return MS_MEDIA_ERR_INTERNAL;
1735 _media_svc_destroy_content_info(&content_info);
1737 return MS_MEDIA_ERR_NONE;
1740 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1742 int ret = MS_MEDIA_ERR_NONE;
1743 sqlite3 * db_handle = (sqlite3 *)handle;
1745 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1746 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1748 ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1753 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1755 int ret = MS_MEDIA_ERR_NONE;
1757 ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1762 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1764 int ret = MS_MEDIA_ERR_NONE;
1765 sqlite3 * db_handle = (sqlite3 *)handle;
1766 time_t modified_time = 0;
1767 int system_time = 0;
1769 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1771 ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1773 system_time = _media_svc_get_file_time(path);
1774 media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1776 if (system_time != modified_time && system_time != 0)
1784 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
1786 sqlite3 * db_handle = (sqlite3 *)handle;
1788 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1789 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1791 return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1794 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1796 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1797 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1799 return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1802 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1804 int ret = MS_MEDIA_ERR_NONE;
1805 sqlite3 * db_handle = (sqlite3 *)handle;
1808 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1809 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1810 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1812 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1813 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1816 media_svc_debug("item is exist in database");
1817 return MS_MEDIA_ERR_NONE;
1819 media_svc_debug("item is not exist in database");
1820 return MS_MEDIA_ERR_DB_NO_RECORD;
1823 return MS_MEDIA_ERR_NONE;
1826 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1828 int ret = MS_MEDIA_ERR_NONE;
1829 sqlite3 * db_handle = (sqlite3 *)handle;
1831 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1832 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1834 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1839 int media_svc_append_query(const char *query, uid_t uid)
1841 int ret = MS_MEDIA_ERR_NONE;
1843 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1845 ret = _media_svc_append_query_list(query, uid);
1850 int media_svc_send_query(uid_t uid)
1852 int ret = MS_MEDIA_ERR_NONE;
1854 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1859 int media_svc_get_media_type(const char *path, int *mediatype)
1861 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1863 return _media_svc_get_media_type(path, mediatype);
1866 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
1868 int ret = MS_MEDIA_ERR_NONE;
1869 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
1872 // 1. Check media type
1873 if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1874 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1876 // 2. try to create thumbnail
1877 ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid);
1878 if (ret != MS_MEDIA_ERR_NONE)
1879 media_svc_error("Failed to create thumbnail [%d]", ret);
1881 // 3. Update creation result to media db
1882 sql = sqlite3_mprintf("UPDATE %q SET thumbnail_path = '%q' WHERE path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
1884 ret = _media_svc_sql_query(sql, uid);
1885 SQLITE3_SAFE_FREE(sql);
1886 if (ret != MS_MEDIA_ERR_NONE) {
1887 media_svc_error("Failed to update media db [%d]", ret);
1888 *thumbnail_path = g_strdup("");
1890 *thumbnail_path = g_strdup(thumb_path);