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-svc.h"
25 #include "media-svc-media.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-env.h"
30 #include "media-svc-media-folder.h"
31 #include "media-svc-album.h"
32 #include "media-svc-noti.h"
33 #include "media-svc-storage.h"
35 static __thread int g_media_svc_item_validity_data_cnt = 1;
36 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
38 static __thread int g_media_svc_insert_item_data_cnt = 1;
39 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
41 /* Flag for items to be published by notification */
42 static __thread bool g_insert_with_noti = false;
44 #define BATCH_REQUEST_MAX 300
46 int media_svc_get_user_version(sqlite3 *handle, int *user_version)
48 return _media_svc_get_user_version(handle, user_version);
51 int media_svc_create_table(uid_t uid)
53 int ret = MS_MEDIA_ERR_NONE;
56 media_svc_debug_fenter();
58 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
59 if (ret != MS_MEDIA_ERR_NONE) {
60 media_svc_error("_media_svc_init_table_query fail.");
64 /*create media table*/
65 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
66 if (ret != MS_MEDIA_ERR_NONE) {
67 media_svc_error("_media_svc_make_table_query fail.");
71 /*create folder table*/
72 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
73 if (ret != MS_MEDIA_ERR_NONE) {
74 media_svc_error("_media_svc_make_table_query fail.");
78 /*create playlist_map table*/
79 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
80 if (ret != MS_MEDIA_ERR_NONE) {
81 media_svc_error("_media_svc_make_table_query fail.");
85 /*create playlist table*/
86 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
87 if (ret != MS_MEDIA_ERR_NONE) {
88 media_svc_error("_media_svc_make_table_query fail.");
92 /* create album table*/
93 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
94 if (ret != MS_MEDIA_ERR_NONE) {
95 media_svc_error("_media_svc_make_table_query fail.");
99 /*create tag_map table*/
100 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
101 if (ret != MS_MEDIA_ERR_NONE) {
102 media_svc_error("_media_svc_make_table_query fail.");
107 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
108 if (ret != MS_MEDIA_ERR_NONE) {
109 media_svc_error("_media_svc_make_table_query fail.");
113 /*create bookmark table*/
114 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
115 if (ret != MS_MEDIA_ERR_NONE) {
116 media_svc_error("_media_svc_make_table_query fail.");
120 /*create storage table from tizen 2.4 */
121 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
122 if (ret != MS_MEDIA_ERR_NONE) {
123 media_svc_error("_media_svc_make_table_query fail.");
127 /*create face table. from tizen 3.0*/
128 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
129 if (ret != MS_MEDIA_ERR_NONE) {
130 media_svc_error("_media_svc_make_table_query fail.");
134 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
135 if (ret != MS_MEDIA_ERR_NONE) {
136 media_svc_error("_media_svc_make_table_query fail.");
140 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
141 ret = _media_svc_sql_query(sql, uid);
142 if (ret != MS_MEDIA_ERR_NONE) {
143 media_svc_error("user_version update fail.");
147 _media_svc_destroy_table_query();
149 media_svc_debug_fleave();
151 return MS_MEDIA_ERR_NONE;
153 _media_svc_destroy_table_query();
155 media_svc_debug_fleave();
160 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
162 int ret = MS_MEDIA_ERR_NONE;
165 ret = _media_svc_count_record_with_path(handle, storage_id, path, &count);
166 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
169 media_svc_debug("item is exist in database");
170 return MS_MEDIA_ERR_NONE;
172 media_svc_debug("item is not exist in database");
173 return MS_MEDIA_ERR_DB_NO_RECORD;
176 return MS_MEDIA_ERR_NONE;
179 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
181 return _media_svc_get_modified_time(handle, storage_id, path, modified_time);
184 int media_svc_insert_item_begin(int data_cnt, bool with_noti, int from_pid)
186 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
188 g_media_svc_insert_item_data_cnt = data_cnt;
189 g_media_svc_insert_item_cur_data_cnt = 0;
191 /* Prepare for making noti item list */
193 media_svc_debug("making noti list from pid[%d]", from_pid);
194 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
195 return MS_MEDIA_ERR_OUT_OF_MEMORY;
197 _media_svc_set_noti_from_pid(from_pid);
198 g_insert_with_noti = true;
201 return MS_MEDIA_ERR_NONE;
204 int media_svc_insert_item_end(uid_t uid)
206 int ret = MS_MEDIA_ERR_NONE;
208 media_svc_debug_fenter();
210 if (g_media_svc_insert_item_cur_data_cnt > 0) {
212 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
213 if (g_insert_with_noti) {
214 media_svc_debug("sending noti list");
215 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
216 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
217 g_insert_with_noti = false;
218 _media_svc_set_noti_from_pid(-1);
222 g_media_svc_insert_item_data_cnt = 1;
223 g_media_svc_insert_item_cur_data_cnt = 0;
228 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
230 int ret = MS_MEDIA_ERR_NONE;
231 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
233 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
234 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
235 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
236 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
238 media_svc_content_info_s content_info;
239 memset(&content_info, 0, sizeof(media_svc_content_info_s));
242 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
243 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
244 if (ret != MS_MEDIA_ERR_NONE)
247 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
248 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
249 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
250 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
251 media_svc_debug("Do nothing[%d]", content_info.media_type);
252 else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
253 ret = _media_svc_extract_image_metadata(&content_info);
255 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
256 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
258 /*Set or Get folder id*/
259 ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
260 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
262 content_info.folder_uuid = g_strdup(folder_uuid);
263 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
265 if (g_media_svc_insert_item_data_cnt == 1) {
267 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, false, uid);
268 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
270 if (g_insert_with_noti)
271 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
273 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
275 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
276 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
278 if (g_insert_with_noti)
279 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
281 g_media_svc_insert_item_cur_data_cnt++;
283 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
285 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
286 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
288 if (g_insert_with_noti)
289 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
291 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
292 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
294 if (g_insert_with_noti) {
295 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
296 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
298 /* Recreate noti list */
299 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
300 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
303 g_media_svc_insert_item_cur_data_cnt = 0;
306 media_svc_error("Error in media_svc_insert_item_bulk");
307 _media_svc_destroy_content_info(&content_info);
308 return MS_MEDIA_ERR_INTERNAL;
311 _media_svc_destroy_content_info(&content_info);
313 return MS_MEDIA_ERR_NONE;
316 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
318 int ret = MS_MEDIA_ERR_NONE;
319 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
321 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
322 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
323 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
324 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
326 media_svc_content_info_s content_info;
327 memset(&content_info, 0, sizeof(media_svc_content_info_s));
330 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
331 if (ret != MS_MEDIA_ERR_NONE)
334 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
335 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
336 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
337 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
339 } else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
340 ret = _media_svc_extract_image_metadata(&content_info);
342 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
345 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
347 /*Set or Get folder id*/
348 ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
349 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
351 content_info.folder_uuid = g_strdup(folder_uuid);
352 media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
354 /* Extracting thumbnail */
355 if (content_info.thumbnail_path == NULL) {
356 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
357 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
359 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
360 if (ret == MS_MEDIA_ERR_NONE)
361 content_info.thumbnail_path = g_strdup(thumb_path);
365 ret = _media_svc_insert_item_with_data(false, storage_id, &content_info, false, uid);
367 if (ret == MS_MEDIA_ERR_NONE) {
368 media_svc_debug("Insertion is successful. Sending noti for this");
369 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
370 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
371 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
374 _media_svc_destroy_content_info(&content_info);
378 int media_svc_move_item(sqlite3 *handle,
379 const char *src_path,
380 const char *dest_path,
381 const char *media_id,
383 const char *mime_type,
386 int ret = MS_MEDIA_ERR_NONE;
387 char *file_name = NULL;
388 char *folder_path = NULL;
389 int modified_time = 0;
390 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
391 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
392 char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
393 char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
394 ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
395 ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
397 media_svc_debug_fenter();
399 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
400 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
401 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
402 media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
403 media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
406 ret = _media_svc_get_storage_uuid(handle, src_path, org_stg_id, uid);
407 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
408 ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
409 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
410 /* Get storage_type */
411 ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
412 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
413 ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
414 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
416 /*check and update folder*/
417 ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
418 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
421 file_name = g_path_get_basename(dest_path);
423 /*get modified_time*/
424 modified_time = _media_svc_get_file_time(dest_path);
426 /*get old thumbnail_path and remove thumbnail */
427 ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
428 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
429 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
430 SAFE_FREE(file_name);
434 if (STRING_VALID(old_thumb_path)) {
435 ret = _media_svc_remove_file(old_thumb_path);
436 if (ret != MS_MEDIA_ERR_NONE)
437 media_svc_error("_media_svc_remove_file failed : %d", ret);
441 ret = _media_svc_update_item_by_path(org_stg_id, src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
442 SAFE_FREE(file_name);
443 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
445 media_svc_debug("Move is successful. Sending noti for this");
446 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
448 /*update folder modified_time*/
449 folder_path = g_path_get_dirname(dest_path);
450 ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
451 SAFE_FREE(folder_path);
452 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
454 return MS_MEDIA_ERR_NONE;
457 int media_svc_set_item_validity_begin(int data_cnt)
459 media_svc_debug("Transaction data count : [%d]", data_cnt);
461 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
463 g_media_svc_item_validity_data_cnt = data_cnt;
464 g_media_svc_item_validity_cur_data_cnt = 0;
466 return MS_MEDIA_ERR_NONE;
469 int media_svc_set_item_validity_end(uid_t uid)
471 int ret = MS_MEDIA_ERR_NONE;
473 media_svc_debug_fenter();
475 if (g_media_svc_item_validity_cur_data_cnt > 0)
476 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
478 g_media_svc_item_validity_data_cnt = 1;
479 g_media_svc_item_validity_cur_data_cnt = 0;
484 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
486 int ret = MS_MEDIA_ERR_NONE;
488 if (g_media_svc_item_validity_data_cnt == 1) {
490 return _media_svc_update_item_validity(storage_id, path, validity, false, uid);
492 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
494 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
495 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
497 g_media_svc_item_validity_cur_data_cnt++;
499 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
501 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
502 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
504 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
505 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
507 g_media_svc_item_validity_cur_data_cnt = 0;
511 media_svc_error("Error in media_svc_set_item_validity");
512 return MS_MEDIA_ERR_INTERNAL;
515 return MS_MEDIA_ERR_NONE;
518 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
520 int ret = MS_MEDIA_ERR_NONE;
521 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
522 media_svc_noti_item *noti_item = NULL;
524 media_svc_debug_fenter();
526 /*Get thumbnail path to delete*/
527 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
528 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
530 /* Get notification info */
531 ret = _media_svc_get_noti_info(handle, storage_id, path, ¬i_item);
532 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
535 ret = _media_svc_delete_item_by_path(storage_id, path, uid);
536 if (ret != MS_MEDIA_ERR_NONE) {
537 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
538 _media_svc_destroy_noti_item(noti_item);
543 /* Send notification */
544 media_svc_debug("Deletion is successful. Sending noti for this");
545 _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
546 _media_svc_destroy_noti_item(noti_item);
549 if (STRING_VALID(thumb_path)) {
550 ret = _media_svc_remove_file(thumb_path);
551 if (ret != MS_MEDIA_ERR_NONE)
552 media_svc_error("fail to remove thumbnail file.");
555 return MS_MEDIA_ERR_NONE;
558 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
560 int ret = MS_MEDIA_ERR_NONE;
561 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
563 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
564 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
565 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
566 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
568 media_svc_content_info_s content_info;
569 memset(&content_info, 0, sizeof(media_svc_content_info_s));
572 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
573 if (ret != MS_MEDIA_ERR_NONE)
576 /* Initialize thumbnail information to remake thumbnail. */
577 ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
578 if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
579 _media_svc_destroy_content_info(&content_info);
583 if (STRING_VALID(thumb_path)) {
584 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
585 ret = _media_svc_remove_file(thumb_path);
586 if (ret != MS_MEDIA_ERR_NONE)
587 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
590 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
591 if (ret != MS_MEDIA_ERR_NONE) {
592 _media_svc_destroy_content_info(&content_info);
597 /* Get notification info */
598 media_svc_noti_item *noti_item = NULL;
599 ret = _media_svc_get_noti_info(handle, storage_id, path, ¬i_item);
600 if (ret != MS_MEDIA_ERR_NONE) {
601 _media_svc_destroy_content_info(&content_info);
605 content_info.media_type = noti_item->media_type;
607 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
608 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
609 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
610 || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
611 media_svc_debug("Do nothing [%d]", content_info.media_type);
612 else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
613 ret = _media_svc_extract_image_metadata(&content_info);
615 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
617 if (ret != MS_MEDIA_ERR_NONE) {
618 _media_svc_destroy_noti_item(noti_item);
619 _media_svc_destroy_content_info(&content_info);
623 /* Extracting thumbnail */
624 if (content_info.thumbnail_path == NULL) {
625 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
626 memset(thumb_path, 0, sizeof(thumb_path));
628 ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
629 if (ret == MS_MEDIA_ERR_NONE)
630 content_info.thumbnail_path = g_strdup(thumb_path);
634 ret = _media_svc_update_item_with_data(is_direct, storage_id, &content_info, uid);
636 if (ret == MS_MEDIA_ERR_NONE) {
637 media_svc_debug("Update is successful. Sending noti for this");
638 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
640 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
643 _media_svc_destroy_content_info(&content_info);
644 _media_svc_destroy_noti_item(noti_item);
649 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
651 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
653 return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
656 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
658 media_svc_debug_fenter();
660 return _media_svc_check_db_upgrade(handle, user_version, uid);
663 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
665 int ret = MS_MEDIA_ERR_NONE;
666 char *root_path = NULL;
667 GArray *path_list = NULL;
669 ms_user_get_internal_root_path(uid, &root_path);
670 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
671 if (ret != MS_MEDIA_ERR_NONE)
672 media_svc_error("Fail to send noti");
674 SAFE_FREE(root_path);
676 path_list = g_array_new(FALSE, FALSE, sizeof(char *));
677 _media_svc_get_storage_path(handle, &path_list);
679 while (path_list->len != 0) {
680 root_path = g_array_index(path_list , char *, 0);
681 g_array_remove_index(path_list, 0);
683 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
684 if (ret != MS_MEDIA_ERR_NONE)
685 media_svc_error("Fail to send noti");
687 SAFE_FREE(root_path);
690 g_array_free(path_list, false);
693 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
695 int ret = MS_MEDIA_ERR_NONE;
696 /* NOTICE : After 6.0, change 'media_view' to 'media', add 'AND validity=1', and no need storage id */
698 media_svc_content_info_s content_info;
699 media_svc_file_s *file_info = NULL;
700 GArray *path_list = NULL;
702 path_list = g_array_new(FALSE, FALSE, sizeof(media_svc_file_s *));
703 media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
705 sql = sqlite3_mprintf("SELECT media_path, storage_uuid FROM %q WHERE media_type=3", MEDIA_SVC_DB_VIEW_MEDIA);
706 ret = _media_svc_get_media(handle, sql, &path_list);
707 if (ret != MS_MEDIA_ERR_NONE) {
708 media_svc_error("Fail to get media list");
709 g_array_free(path_list, false);
713 while (path_list->len != 0) {
714 file_info = g_array_index(path_list, media_svc_file_s *, 0);
715 g_array_remove_index(path_list, 0);
717 memset(&content_info, 0, sizeof(media_svc_content_info_s));
718 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_info->storage_id, file_info->path);
719 if (ret != MS_MEDIA_ERR_NONE) {
720 media_svc_error("Fail to extract metadata");
721 _media_svc_destroy_content_info(&content_info);
722 SAFE_FREE(file_info);
726 ret = _media_svc_update_meta_with_data(&content_info);
727 if (ret != MS_MEDIA_ERR_NONE)
728 media_svc_error("Fail to append item[%s]", content_info.path);
730 _media_svc_destroy_content_info(&content_info);
731 SAFE_FREE(file_info);
734 g_array_free(path_list, false);
736 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
737 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
740 __media_svc_noti_all_storage(handle, uid);
745 int media_svc_publish_noti(media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
747 return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
750 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
752 return _media_svc_get_pinyin_str(src_str, pinyin_str);
755 int media_svc_check_pinyin_support(bool *support)
757 *support = _media_svc_check_pinyin_support();
759 return MS_MEDIA_ERR_NONE;
762 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
764 int ret = MS_MEDIA_ERR_NONE;
766 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
768 ret = _media_svc_update_storage_validity(storage_id, validity, uid);
769 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
771 ret = _media_svc_update_media_view(handle, uid);
772 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
777 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
779 return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
782 int media_svc_generate_uuid(char **uuid)
784 char *gen_uuid = NULL;
785 gen_uuid = _media_info_generate_uuid();
786 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
788 *uuid = strdup(gen_uuid);
790 return MS_MEDIA_ERR_NONE;
793 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
795 return _media_svc_check_storage(handle, storage_id, storage_path, validity, uid);
798 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
800 return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
803 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
805 int ret = MS_MEDIA_ERR_NONE;
807 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
808 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
809 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
810 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
812 ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
813 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
815 ret = _media_svc_create_media_table_with_id(storage_id, uid);
816 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
818 ret = _media_svc_update_media_view(handle, uid);
819 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
821 /* Remove external storage that validity is 0 */
822 ret = _media_svc_delete_invalid_storage(handle, uid);
823 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
828 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
830 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
831 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
832 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
833 media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
835 return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
838 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
840 return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
843 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
845 int ret = MS_MEDIA_ERR_NONE;
848 ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
849 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
852 media_svc_debug("item is exist in database");
853 return MS_MEDIA_ERR_NONE;
855 media_svc_debug("item is not exist in database");
856 return MS_MEDIA_ERR_DB_NO_RECORD;
859 return MS_MEDIA_ERR_NONE;
862 int media_svc_append_query(const char *query, uid_t uid)
864 return _media_svc_append_query_list(query, uid);
867 int media_svc_send_query(uid_t uid)
869 return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
872 int media_svc_get_media_type(const char *path, int *mediatype)
874 return _media_svc_get_media_type(path, mediatype);
877 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
879 int ret = MS_MEDIA_ERR_NONE;
880 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
883 // 1. Check media type
884 if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
885 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
887 // 2. try to create thumbnail
888 ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
889 if (ret != MS_MEDIA_ERR_NONE) {
890 media_svc_error("Failed to create thumbnail [%d]", ret);
891 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
895 // 3. Update creation result to media db
896 sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path='%q' WHERE media_path='%q';", storage_id, thumb_path, file_path);
898 ret = _media_svc_sql_query(sql, uid);
899 SQLITE3_SAFE_FREE(sql);
900 if (ret != MS_MEDIA_ERR_NONE) {
901 media_svc_error("Failed to update media db [%d]", ret);
902 *thumbnail_path = g_strdup("");
904 *thumbnail_path = g_strdup(thumb_path);