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 DEFAULT_MEDIA_SVC_STORAGE_ID "media"
59 } media_svc_item_info_s;
61 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type, bool check_all)
63 if (check_all == TRUE) {
64 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
65 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
66 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)
67 && (storage_type != MEDIA_SVC_STORAGE_CLOUD)) {
68 media_svc_error("storage type is incorrect[%d]", storage_type);
72 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
73 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
74 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
75 media_svc_error("storage type is incorrect[%d]", storage_type);
83 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
85 int ret = MS_MEDIA_ERR_NONE;
86 MediaDBHandle *db_handle = NULL;
88 media_svc_debug_fenter();
90 ret = media_db_connect(&db_handle, uid, need_write);
91 if (ret != MS_MEDIA_ERR_NONE)
95 return MS_MEDIA_ERR_NONE;
98 int media_svc_disconnect(MediaSvcHandle *handle)
100 MediaDBHandle *db_handle = (MediaDBHandle *)handle;
102 media_svc_debug_fenter();
104 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
106 int ret = MS_MEDIA_ERR_NONE;
108 ret = media_db_disconnect(db_handle);
112 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
114 sqlite3 *db_handle = (sqlite3 *)handle;
116 return _media_svc_get_user_version(db_handle, user_version);
119 int media_svc_create_table(MediaSvcHandle *handle, uid_t uid)
121 int ret = MS_MEDIA_ERR_NONE;
122 sqlite3 *db_handle = (sqlite3 *)handle;
125 media_svc_debug_fenter();
127 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
129 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
130 if (ret != MS_MEDIA_ERR_NONE) {
131 media_svc_error("_media_svc_init_table_query fail.");
135 /*create media table*/
136 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
137 if (ret != MS_MEDIA_ERR_NONE) {
138 media_svc_error("_media_svc_make_table_query fail.");
142 /*create folder table*/
143 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
144 if (ret != MS_MEDIA_ERR_NONE) {
145 media_svc_error("_media_svc_make_table_query fail.");
149 /*create playlist_map table*/
150 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
151 if (ret != MS_MEDIA_ERR_NONE) {
152 media_svc_error("_media_svc_make_table_query fail.");
156 /*create playlist table*/
157 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
158 if (ret != MS_MEDIA_ERR_NONE) {
159 media_svc_error("_media_svc_make_table_query fail.");
163 /* create album table*/
164 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
165 if (ret != MS_MEDIA_ERR_NONE) {
166 media_svc_error("_media_svc_make_table_query fail.");
170 /*create tag_map table*/
171 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
172 if (ret != MS_MEDIA_ERR_NONE) {
173 media_svc_error("_media_svc_make_table_query fail.");
178 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
179 if (ret != MS_MEDIA_ERR_NONE) {
180 media_svc_error("_media_svc_make_table_query fail.");
184 /*create bookmark table*/
185 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
186 if (ret != MS_MEDIA_ERR_NONE) {
187 media_svc_error("_media_svc_make_table_query fail.");
191 /*create storage table. from tizen 2.4*/
192 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
193 if (ret != MS_MEDIA_ERR_NONE) {
194 media_svc_error("_media_svc_make_table_query fail.");
199 /*init storage table*/
200 ret = _media_svc_init_storage(db_handle, uid);
201 if (ret != MS_MEDIA_ERR_NONE) {
202 media_svc_error("_media_svc_make_table_query fail.");
207 /*create face table. from tizen 3.0*/
208 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
209 if (ret != MS_MEDIA_ERR_NONE) {
210 media_svc_error("_media_svc_make_table_query fail.");
214 ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
215 if (ret != MS_MEDIA_ERR_NONE) {
216 media_svc_error("_media_svc_make_table_query fail.");
220 sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
221 ret = _media_svc_sql_query(db_handle, sql, uid);
222 if (ret != MS_MEDIA_ERR_NONE) {
223 media_svc_error("user_version update fail.");
227 _media_svc_destroy_table_query();
229 media_svc_debug_fleave();
231 return MS_MEDIA_ERR_NONE;
233 _media_svc_destroy_table_query();
235 media_svc_debug_fleave();
240 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
242 int ret = MS_MEDIA_ERR_NONE;
243 media_svc_storage_type_e type;
245 ret = _media_svc_get_storage_type_by_path(path, &type, uid);
246 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
248 *storage_type = type;
253 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
255 int ret = MS_MEDIA_ERR_NONE;
256 sqlite3 *db_handle = (sqlite3 *)handle;
258 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
259 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
261 ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
266 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
268 int ret = MS_MEDIA_ERR_NONE;
269 sqlite3 *db_handle = (sqlite3 *)handle;
272 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
273 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
274 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
276 ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
277 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
280 media_svc_debug("item is exist in database");
281 return MS_MEDIA_ERR_NONE;
283 media_svc_debug("item is not exist in database");
284 return MS_MEDIA_ERR_DB_NO_RECORD;
287 return MS_MEDIA_ERR_NONE;
290 int media_svc_insert_item_begin(MediaSvcHandle *handle, int data_cnt, int with_noti, int from_pid)
292 sqlite3 *db_handle = (sqlite3 *)handle;
294 media_svc_debug("Transaction data count : [%d]", data_cnt);
296 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
297 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
299 g_media_svc_insert_item_data_cnt = data_cnt;
300 g_media_svc_insert_item_cur_data_cnt = 0;
302 /* Prepare for making noti item list */
304 media_svc_debug("making noti list from pid[%d]", from_pid);
305 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
306 return MS_MEDIA_ERR_OUT_OF_MEMORY;
308 _media_svc_set_noti_from_pid(from_pid);
309 g_insert_with_noti = TRUE;
312 return MS_MEDIA_ERR_NONE;
315 int media_svc_insert_item_end(MediaSvcHandle *handle, uid_t uid)
317 int ret = MS_MEDIA_ERR_NONE;
318 sqlite3 *db_handle = (sqlite3 *)handle;
320 media_svc_debug_fenter();
322 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
324 if (g_media_svc_insert_item_cur_data_cnt > 0) {
326 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
327 if (g_insert_with_noti) {
328 media_svc_debug("sending noti list");
329 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
330 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
331 g_insert_with_noti = FALSE;
332 _media_svc_set_noti_from_pid(-1);
336 g_media_svc_insert_item_data_cnt = 1;
337 g_media_svc_insert_item_cur_data_cnt = 0;
342 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)
344 int ret = MS_MEDIA_ERR_NONE;
345 sqlite3 *db_handle = (sqlite3 *)handle;
346 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
347 media_svc_media_type_e media_type;
349 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
350 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
351 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
352 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
354 media_svc_content_info_s content_info;
355 memset(&content_info, 0, sizeof(media_svc_content_info_s));
358 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
359 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
360 if (ret != MS_MEDIA_ERR_NONE)
363 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
365 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
366 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
368 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
370 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
372 /*Set or Get folder id*/
373 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
374 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
376 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
377 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
379 if (g_media_svc_insert_item_data_cnt == 1) {
381 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
382 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
384 if (g_insert_with_noti)
385 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
387 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
389 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
390 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
392 if (g_insert_with_noti)
393 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
395 g_media_svc_insert_item_cur_data_cnt++;
397 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
399 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
400 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
402 if (g_insert_with_noti)
403 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
405 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
406 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
408 if (g_insert_with_noti) {
409 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
410 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
412 /* Recreate noti list */
413 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
414 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
417 g_media_svc_insert_item_cur_data_cnt = 0;
420 media_svc_error("Error in media_svc_insert_item_bulk");
421 _media_svc_destroy_content_info(&content_info);
422 return MS_MEDIA_ERR_INTERNAL;
425 _media_svc_destroy_content_info(&content_info);
427 return MS_MEDIA_ERR_NONE;
430 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)
432 int ret = MS_MEDIA_ERR_NONE;
433 sqlite3 *db_handle = (sqlite3 *)handle;
434 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
435 media_svc_media_type_e media_type;
436 int ini_val = _media_svc_get_ini_value();
438 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
439 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
440 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
441 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
443 media_svc_content_info_s content_info;
444 memset(&content_info, 0, sizeof(media_svc_content_info_s));
447 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
448 if (ret != MS_MEDIA_ERR_NONE) {
452 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
454 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
455 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
457 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
460 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
462 /*Set or Get folder id*/
463 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
464 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
466 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
467 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
469 /* Extracting thumbnail */
471 if (content_info.thumbnail_path == NULL) {
472 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
473 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
477 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
478 if (ret == MS_MEDIA_ERR_NONE)
479 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
481 if (content_info.media_meta.width <= 0)
482 content_info.media_meta.width = width;
484 if (content_info.media_meta.height <= 0)
485 content_info.media_meta.height = height;
491 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
493 if (ret == MS_MEDIA_ERR_NONE) {
494 media_svc_debug("Insertion is successful. Sending noti for this");
495 _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);
496 } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
497 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
500 _media_svc_destroy_content_info(&content_info);
504 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
505 media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
507 int ret = MS_MEDIA_ERR_NONE;
508 sqlite3 *db_handle = (sqlite3 *)handle;
509 char *file_name = NULL;
510 char *folder_path = NULL;
511 int modified_time = 0;
512 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
513 char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
514 char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
517 media_svc_debug_fenter();
519 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
520 media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
521 media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
522 media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
523 media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
525 /*check and update folder*/
526 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
527 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
530 file_name = g_path_get_basename(dest_path);
532 /*get modified_time*/
533 modified_time = _media_svc_get_file_time(dest_path);
535 /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
536 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
537 if (ret != MS_MEDIA_ERR_NONE) {
538 media_svc_error("_media_svc_get_media_type_by_path failed");
539 SAFE_FREE(file_name);
543 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
544 /*get old thumbnail_path*/
545 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
546 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
547 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
548 SAFE_FREE(file_name);
552 /* If old thumb path is default or not */
553 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
554 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
555 strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
557 _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
559 SAFE_FREE(default_thumbnail_path);
562 if (g_media_svc_move_item_data_cnt == 1) {
565 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
566 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
568 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
570 SAFE_FREE(file_name);
571 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
573 media_svc_debug("Move is successful. Sending noti for this");
575 /* Get notification info */
576 media_svc_noti_item *noti_item = NULL;
577 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, ¬i_item);
578 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
580 /* Send notification for move */
581 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
582 _media_svc_destroy_noti_item(noti_item);
584 /*update folder modified_time*/
585 folder_path = g_path_get_dirname(dest_path);
586 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
587 SAFE_FREE(folder_path);
588 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
590 ret = _media_svc_update_folder_table(db_handle, storage_id, uid);
591 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
593 } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
596 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
597 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
599 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
601 SAFE_FREE(file_name);
602 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
604 /*update folder modified_time*/
605 folder_path = g_path_get_dirname(dest_path);
606 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
607 SAFE_FREE(folder_path);
608 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
610 g_media_svc_move_item_cur_data_cnt++;
612 } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
615 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
616 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
618 ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
620 SAFE_FREE(file_name);
621 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
623 /*update folder modified_time*/
624 folder_path = g_path_get_dirname(dest_path);
625 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
626 SAFE_FREE(folder_path);
627 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
630 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
631 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
633 g_media_svc_move_item_cur_data_cnt = 0;
636 media_svc_error("Error in media_svc_move_item");
637 return MS_MEDIA_ERR_INTERNAL;
640 /*rename thumbnail file*/
641 /* if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
642 if ((strlen(old_thumb_path) > 0) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
643 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
644 if (ret != MS_MEDIA_ERR_NONE)
645 media_svc_error("_media_svc_rename_file failed : %d", ret);
649 return MS_MEDIA_ERR_NONE;
652 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
654 sqlite3 *db_handle = (sqlite3 *)handle;
656 media_svc_debug("Transaction data count : [%d]", data_cnt);
658 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
659 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
661 g_media_svc_item_validity_data_cnt = data_cnt;
662 g_media_svc_item_validity_cur_data_cnt = 0;
664 return MS_MEDIA_ERR_NONE;
667 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
669 int ret = MS_MEDIA_ERR_NONE;
670 sqlite3 *db_handle = (sqlite3 *)handle;
672 media_svc_debug_fenter();
674 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
676 if (g_media_svc_item_validity_cur_data_cnt > 0) {
678 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
681 g_media_svc_item_validity_data_cnt = 1;
682 g_media_svc_item_validity_cur_data_cnt = 0;
687 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *storage_id, const char *path, int validity, uid_t uid)
689 int ret = MS_MEDIA_ERR_NONE;
690 sqlite3 *db_handle = (sqlite3 *)handle;
692 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
693 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
694 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
696 media_svc_debug("path=[%s], validity=[%d]", path, validity);
698 if (g_media_svc_item_validity_data_cnt == 1) {
700 return _media_svc_update_item_validity(db_handle, storage_id, path, validity, FALSE, uid);
702 } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
704 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
705 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
707 g_media_svc_item_validity_cur_data_cnt++;
709 } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
711 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
712 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
714 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
715 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
717 g_media_svc_item_validity_cur_data_cnt = 0;
721 media_svc_error("Error in media_svc_set_item_validity");
722 return MS_MEDIA_ERR_INTERNAL;
725 return MS_MEDIA_ERR_NONE;
728 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
730 int ret = MS_MEDIA_ERR_NONE;
731 sqlite3 *db_handle = (sqlite3 *)handle;
732 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
734 media_svc_debug_fenter();
736 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
737 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
738 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
741 ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
742 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
745 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
746 /*Get thumbnail path to delete*/
747 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
748 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
749 } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
751 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
752 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
755 /*Get thumbnail path to delete*/
756 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
757 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
761 /*Get thumbnail path to delete*/
762 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
763 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
765 if (g_media_svc_insert_item_data_cnt == 1) {
767 /* Get notification info */
768 media_svc_noti_item *noti_item = NULL;
769 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
770 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
773 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
774 if (ret != MS_MEDIA_ERR_NONE) {
775 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
776 _media_svc_destroy_noti_item(noti_item);
781 /* Send notification */
782 media_svc_debug("Deletion is successful. Sending noti for this");
783 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
784 _media_svc_destroy_noti_item(noti_item);
786 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
787 if (ret != MS_MEDIA_ERR_NONE) {
788 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
795 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
796 if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
799 // Get count of media, which contains same thumbnail for music
800 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
801 ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
802 if (ret != MS_MEDIA_ERR_NONE) {
803 media_svc_error("Failed to get thumbnail count : %d", ret);
807 if (thumb_count == 1) {
809 ret = _media_svc_remove_file(thumb_path);
810 if (ret != MS_MEDIA_ERR_NONE) {
811 media_svc_error("fail to remove thumbnail file.");
816 SAFE_FREE(default_thumbnail_path);
818 return MS_MEDIA_ERR_NONE;
821 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
823 int ret = MS_MEDIA_ERR_NONE;
824 sqlite3 *db_handle = (sqlite3 *)handle;
826 media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
828 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
829 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
830 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
832 ret = _media_svc_truncate_table(db_handle, storage_id, storage_type, uid);
833 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
835 if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
836 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
837 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
839 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
841 /* remove thumbnails */
842 if (STRING_VALID(dirpath))
843 ret = _media_svc_remove_all_files_in_dir(dirpath);
844 SAFE_FREE(internal_thumb_path);
845 SAFE_FREE(external_thumb_path);
846 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
849 return MS_MEDIA_ERR_NONE;
852 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
854 sqlite3 *db_handle = (sqlite3 *)handle;
856 media_svc_debug_fenter();
858 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
859 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
860 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
862 /*Delete from DB and remove thumbnail files*/
863 return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
866 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
868 sqlite3 *db_handle = (sqlite3 *)handle;
870 media_svc_debug_fenter();
872 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
873 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
875 /*Delete from DB and remove thumbnail files*/
876 return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
879 int media_svc_set_all_storage_items_validity(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
881 sqlite3 *db_handle = (sqlite3 *)handle;
883 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
884 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
885 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
887 return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
890 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
892 sqlite3 *db_handle = (sqlite3 *)handle;
894 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
895 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
896 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
899 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
901 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
904 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
906 int ret = MS_MEDIA_ERR_NONE;
907 sqlite3 *db_handle = (sqlite3 *)handle;
908 media_svc_media_type_e media_type;
909 int ini_val = _media_svc_get_ini_value();
911 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
912 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
913 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
914 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
916 media_svc_content_info_s content_info;
917 memset(&content_info, 0, sizeof(media_svc_content_info_s));
920 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
921 if (ret != MS_MEDIA_ERR_NONE) {
925 /* Initialize thumbnail information to remake thumbnail. */
927 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
928 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
929 if (ret != MS_MEDIA_ERR_NONE) {
930 _media_svc_destroy_content_info(&content_info);
934 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
935 ret = _media_svc_remove_file(thumb_path);
936 if (ret != MS_MEDIA_ERR_NONE) {
937 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
941 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
942 if (ret != MS_MEDIA_ERR_NONE) {
943 _media_svc_destroy_content_info(&content_info);
948 /* Get notification info */
949 media_svc_noti_item *noti_item = NULL;
950 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, ¬i_item);
951 if (ret != MS_MEDIA_ERR_NONE) {
952 _media_svc_destroy_content_info(&content_info);
956 media_type = noti_item->media_type;
957 content_info.media_type = media_type;
959 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
961 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
962 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
964 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
967 if (ret != MS_MEDIA_ERR_NONE) {
968 _media_svc_destroy_noti_item(noti_item);
969 _media_svc_destroy_content_info(&content_info);
973 /* Extracting thumbnail */
975 if (content_info.thumbnail_path == NULL) {
976 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
977 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
981 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
982 if (ret == MS_MEDIA_ERR_NONE) {
983 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
986 if (content_info.media_meta.width <= 0)
987 content_info.media_meta.width = width;
989 if (content_info.media_meta.height <= 0)
990 content_info.media_meta.height = height;
997 ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
999 if (ret == MS_MEDIA_ERR_NONE) {
1000 media_svc_debug("Update is successful. Sending noti for this");
1001 _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);
1003 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
1006 _media_svc_destroy_content_info(&content_info);
1007 _media_svc_destroy_noti_item(noti_item);
1012 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
1014 sqlite3 *db_handle = (sqlite3 *)handle;
1015 int ret = MS_MEDIA_ERR_NONE;
1017 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1018 media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
1019 media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
1021 media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1023 ret = _media_svc_sql_begin_trans(db_handle, uid);
1024 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1026 /* Update all folder record's modified date, which are changed above */
1027 char *update_folder_modified_time_sql = NULL;
1031 update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1033 ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1034 //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
1035 sqlite3_free(update_folder_modified_time_sql);
1037 if (ret != SQLITE_OK) {
1038 media_svc_error("failed to update folder modified time");
1039 _media_svc_sql_rollback_trans(db_handle, uid);
1041 return MS_MEDIA_ERR_DB_INTERNAL;
1044 /* Update all items */
1045 char *select_all_sql = NULL;
1046 sqlite3_stmt *sql_stmt = NULL;
1047 char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1049 snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1051 select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from media where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", dst_path, dst_child_path);
1053 media_svc_debug("[SQL query] : %s", select_all_sql);
1055 ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1056 if (ret != MS_MEDIA_ERR_NONE) {
1057 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1058 _media_svc_sql_rollback_trans(db_handle, uid);
1062 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1063 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1064 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1065 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1066 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1067 /*int media_type; */
1068 bool no_thumb = FALSE;
1070 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1071 strncpy(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1072 media_uuid[sizeof(media_uuid) - 1] = '\0';
1074 media_svc_error("media UUID is NULL");
1075 return MS_MEDIA_ERR_DB_INTERNAL;
1078 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1079 strncpy(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1080 media_path[sizeof(media_path) - 1] = '\0';
1082 media_svc_error("media path is NULL");
1083 return MS_MEDIA_ERR_DB_INTERNAL;
1086 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1087 strncpy(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1088 media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1090 media_svc_debug("media thumb path doesn't exist in DB");
1094 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1096 /* Update path, thumbnail path of this item */
1097 char *replaced_path = NULL;
1098 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1099 if (replaced_path == NULL) {
1100 media_svc_error("_media_svc_replace_path failed");
1101 SQLITE3_FINALIZE(sql_stmt);
1102 _media_svc_sql_rollback_trans(db_handle, uid);
1103 return MS_MEDIA_ERR_INTERNAL;
1106 media_svc_debug("New media path : %s", replaced_path);
1107 media_svc_storage_type_e storage_type;
1110 /* If old thumb path is default or not */
1111 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1112 if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1113 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path) - 1);
1115 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1116 if (ret != MS_MEDIA_ERR_NONE) {
1117 media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1118 SAFE_FREE(replaced_path);
1119 SAFE_FREE(default_thumbnail_path);
1120 _media_svc_sql_rollback_trans(db_handle, uid);
1124 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1125 if (ret != MS_MEDIA_ERR_NONE) {
1126 media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1127 SAFE_FREE(replaced_path);
1128 SAFE_FREE(default_thumbnail_path);
1129 SQLITE3_FINALIZE(sql_stmt);
1130 _media_svc_sql_rollback_trans(db_handle, uid);
1135 SAFE_FREE(default_thumbnail_path);
1137 /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1140 char *update_item_sql = NULL;
1143 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1146 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1147 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1149 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1152 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1156 ret = media_db_request_update_db_batch(update_item_sql, uid);
1157 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1158 sqlite3_free(update_item_sql);
1159 SAFE_FREE(replaced_path);
1161 if (ret != SQLITE_OK) {
1162 media_svc_error("failed to update item");
1163 SQLITE3_FINALIZE(sql_stmt);
1164 _media_svc_sql_rollback_trans(db_handle, uid);
1166 return MS_MEDIA_ERR_DB_INTERNAL;
1169 /* Rename thumbnail file of file system */
1170 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1171 if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1172 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1173 if (ret != MS_MEDIA_ERR_NONE) {
1174 media_svc_error("_media_svc_rename_file failed : %d", ret);
1178 SAFE_FREE(default_thumbnail_path);
1181 SQLITE3_FINALIZE(sql_stmt);
1183 ret = _media_svc_sql_end_trans(db_handle, uid);
1184 if (ret != MS_MEDIA_ERR_NONE) {
1185 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1186 _media_svc_sql_rollback_trans(db_handle, uid);
1190 media_svc_debug("Folder update is successful. Sending noti for this");
1191 /* Get notification info */
1192 media_svc_noti_item *noti_item = NULL;
1193 ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1194 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1196 _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1197 _media_svc_destroy_noti_item(noti_item);
1199 return MS_MEDIA_ERR_NONE;
1202 int media_svc_request_update_db(const char *db_query, uid_t uid)
1204 media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1206 return _media_svc_sql_query(NULL, db_query, uid);
1209 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)
1211 int ret = MS_MEDIA_ERR_NONE;
1212 sqlite3 *db_handle = (sqlite3 *)handle;
1215 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1216 media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1218 /* Get notification info */
1219 media_svc_noti_item *noti_item = NULL;
1220 ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, ¬i_item);
1221 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1223 if (folder_id != NULL) {
1226 uuid = noti_item->media_uuid;
1229 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1230 ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1231 _media_svc_destroy_noti_item(noti_item);
1236 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1238 sqlite3 *db_handle = (sqlite3 *)handle;
1240 media_svc_debug_fenter();
1242 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1243 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1244 media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1245 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1247 return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1250 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, int user_version, uid_t uid)
1252 sqlite3 *db_handle = (sqlite3 *)handle;
1254 media_svc_debug_fenter();
1256 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1258 return _media_svc_check_db_upgrade(db_handle, need_full_scan, user_version, uid);
1261 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1263 sqlite3 *db_handle = (sqlite3 *)handle;
1265 media_svc_debug_fenter();
1267 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1269 return _media_db_check_corrupt(db_handle);
1272 int media_svc_get_folder_list(MediaSvcHandle *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
1274 sqlite3 *db_handle = (sqlite3 *)handle;
1276 media_svc_debug_fenter();
1278 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1279 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1281 return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1284 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1286 int ret = MS_MEDIA_ERR_NONE;
1287 sqlite3 *db_handle = (sqlite3 *)handle;
1288 time_t sto_time = 0;
1289 int cur_time = _media_svc_get_file_time(folder_path);
1290 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1292 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1294 ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1295 if (ret == MS_MEDIA_ERR_NONE) {
1296 if (sto_time != cur_time) {
1297 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1304 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1306 sqlite3 *db_handle = (sqlite3 *)handle;
1308 media_svc_debug("Transaction data count : [%d]", data_cnt);
1310 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1311 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1313 g_media_svc_update_item_data_cnt = data_cnt;
1314 g_media_svc_update_item_cur_data_cnt = 0;
1316 return MS_MEDIA_ERR_NONE;
1319 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1321 int ret = MS_MEDIA_ERR_NONE;
1322 sqlite3 *db_handle = (sqlite3 *)handle;
1324 media_svc_debug_fenter();
1326 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1328 if (g_media_svc_update_item_cur_data_cnt > 0) {
1330 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1333 g_media_svc_update_item_data_cnt = 1;
1334 g_media_svc_update_item_cur_data_cnt = 0;
1339 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1341 int ret = MS_MEDIA_ERR_NONE;
1342 sqlite3 *db_handle = (sqlite3 *)handle;
1344 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1346 media_svc_media_type_e media_type;
1348 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1349 media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1350 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1352 media_svc_content_info_s content_info;
1353 memset(&content_info, 0, sizeof(media_svc_content_info_s));
1356 ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1357 if (ret != MS_MEDIA_ERR_NONE) {
1361 if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1362 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1364 return MS_MEDIA_ERR_NONE;
1367 if (ret != MS_MEDIA_ERR_NONE) {
1368 _media_svc_destroy_content_info(&content_info);
1372 if (g_media_svc_update_item_data_cnt == 1) {
1374 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1375 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1377 } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1379 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1380 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1382 g_media_svc_update_item_cur_data_cnt++;
1384 } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1386 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1387 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1389 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1390 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1392 g_media_svc_update_item_cur_data_cnt = 0;
1395 media_svc_error("Error in media_svc_update_item_meta");
1396 _media_svc_destroy_content_info(&content_info);
1397 return MS_MEDIA_ERR_INTERNAL;
1400 _media_svc_destroy_content_info(&content_info);
1405 int media_svc_publish_noti(MediaSvcHandle *handle, 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)
1407 return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1410 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1412 media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1414 return _media_svc_get_pinyin_str(src_str, pinyin_str);
1417 int media_svc_check_pinyin_support(bool *support)
1419 *support = _media_svc_check_pinyin_support();
1421 return MS_MEDIA_ERR_NONE;
1424 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1426 int ret = MS_MEDIA_ERR_NONE;
1427 sqlite3 * db_handle = (sqlite3 *)handle;
1429 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1431 ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1432 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1434 ret = _media_svc_update_media_view(db_handle, uid);
1435 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1440 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1442 int ret = MS_MEDIA_ERR_NONE;
1443 sqlite3 * db_handle = (sqlite3 *)handle;
1445 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1446 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1448 ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1453 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1455 int ret = MS_MEDIA_ERR_NONE;
1456 sqlite3 * db_handle = (sqlite3 *)handle;
1458 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1459 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1461 ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1466 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1468 int ret = MS_MEDIA_ERR_NONE;
1469 sqlite3 * db_handle = (sqlite3 *)handle;
1471 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1472 media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1474 ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1479 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1481 int ret = MS_MEDIA_ERR_NONE;
1482 sqlite3 * db_handle = (sqlite3 *)handle;
1484 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1486 ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1491 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1493 sqlite3 * db_handle = (sqlite3 *)handle;
1495 media_svc_debug_fenter();
1497 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1498 media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1500 return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1503 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1505 int ret = MS_MEDIA_ERR_NONE;
1507 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1508 media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1510 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1511 new_content_info->size = content_info->size;
1512 new_content_info->modified_time = content_info->modified_time;
1513 new_content_info->is_drm = content_info->is_drm;
1514 new_content_info->media_type = content_info->media_type;
1516 if (STRING_VALID(content_info->mime_type)) {
1517 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1518 if (ret != MS_MEDIA_ERR_NONE)
1519 media_svc_error("strcpy mime_type failed");
1522 if (STRING_VALID(content_info->thumbnail_path)) {
1523 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1524 if (ret != MS_MEDIA_ERR_NONE)
1525 media_svc_error("strcpy thumbnail_path failed");
1528 new_content_info->media_meta.duration = content_info->media_meta.duration;
1529 new_content_info->media_meta.width = content_info->media_meta.width;
1530 new_content_info->media_meta.height = content_info->media_meta.height;
1533 if (content_info->added_time > 0)
1534 new_content_info->added_time = content_info->added_time;
1535 new_content_info->last_played_time = content_info->last_played_time;
1536 new_content_info->played_count = content_info->played_count;
1537 new_content_info->favourate = content_info->favourate;
1539 if (STRING_VALID(content_info->file_name)) {
1540 ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1541 if (ret != MS_MEDIA_ERR_NONE)
1542 media_svc_error("strcpy file_name failed");
1545 if (STRING_VALID(content_info->media_meta.title)) {
1546 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1547 if (ret != MS_MEDIA_ERR_NONE)
1548 media_svc_error("strcpy title failed");
1551 if (STRING_VALID(content_info->media_meta.album)) {
1552 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1553 if (ret != MS_MEDIA_ERR_NONE)
1554 media_svc_error("strcpy album failed");
1557 if (STRING_VALID(content_info->media_meta.artist)) {
1558 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1559 if (ret != MS_MEDIA_ERR_NONE)
1560 media_svc_error("strcpy artist failed");
1563 if (STRING_VALID(content_info->media_meta.genre)) {
1564 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1565 if (ret != MS_MEDIA_ERR_NONE)
1566 media_svc_error("strcpy genre failed");
1569 if (STRING_VALID(content_info->media_meta.composer)) {
1570 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1571 if (ret != MS_MEDIA_ERR_NONE)
1572 media_svc_error("strcpy composer failed");
1575 if (STRING_VALID(content_info->media_meta.year)) {
1576 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1577 if (ret != MS_MEDIA_ERR_NONE)
1578 media_svc_error("strcpy year failed");
1581 if (STRING_VALID(content_info->media_meta.recorded_date)) {
1582 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1583 if (ret != MS_MEDIA_ERR_NONE)
1584 media_svc_error("strcpy recorded_date failed");
1587 if (STRING_VALID(content_info->media_meta.copyright)) {
1588 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1589 if (ret != MS_MEDIA_ERR_NONE)
1590 media_svc_error("strcpy copyright failed");
1593 if (STRING_VALID(content_info->media_meta.track_num)) {
1594 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1595 if (ret != MS_MEDIA_ERR_NONE)
1596 media_svc_error("strcpy track_num failed");
1599 if (STRING_VALID(content_info->media_meta.description)) {
1600 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1601 if (ret != MS_MEDIA_ERR_NONE)
1602 media_svc_error("strcpy description failed");
1605 if (STRING_VALID(content_info->media_meta.weather)) {
1606 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1607 if (ret != MS_MEDIA_ERR_NONE)
1608 media_svc_error("strcpy weather failed");
1611 if (STRING_VALID(content_info->media_meta.category)) {
1612 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1613 if (ret != MS_MEDIA_ERR_NONE)
1614 media_svc_error("strcpy category failed");
1617 if (STRING_VALID(content_info->media_meta.keyword)) {
1618 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1619 if (ret != MS_MEDIA_ERR_NONE)
1620 media_svc_error("strcpy keyword failed");
1623 if (STRING_VALID(content_info->media_meta.location_tag)) {
1624 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1625 if (ret != MS_MEDIA_ERR_NONE)
1626 media_svc_error("strcpy location_tag failed");
1629 if (STRING_VALID(content_info->media_meta.content_name)) {
1630 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1631 if (ret != MS_MEDIA_ERR_NONE)
1632 media_svc_error("strcpy content_name failed");
1635 if (STRING_VALID(content_info->media_meta.age_rating)) {
1636 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1637 if (ret != MS_MEDIA_ERR_NONE)
1638 media_svc_error("strcpy age_rating failed");
1641 if (STRING_VALID(content_info->media_meta.author)) {
1642 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1643 if (ret != MS_MEDIA_ERR_NONE)
1644 media_svc_error("strcpy author failed");
1647 if (STRING_VALID(content_info->media_meta.provider)) {
1648 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1649 if (ret != MS_MEDIA_ERR_NONE)
1650 media_svc_error("strcpy provider failed");
1653 if (STRING_VALID(content_info->media_meta.datetaken)) {
1654 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1655 if (ret != MS_MEDIA_ERR_NONE)
1656 media_svc_error("strcpy datetaken failed");
1658 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1659 if (new_content_info->timeline == 0) {
1660 media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1661 new_content_info->timeline = new_content_info->modified_time;
1663 media_svc_debug("Timeline : %ld", new_content_info->timeline);
1667 //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1668 //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1669 //new_content_info->media_meta.channel = content_info->media_meta.channel;
1670 //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1672 if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1673 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1674 if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1675 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1676 if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1677 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1679 new_content_info->media_meta.rating = content_info->media_meta.rating;
1684 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1686 int ret = MS_MEDIA_ERR_NONE;
1687 sqlite3 *db_handle = (sqlite3 *)handle;
1688 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1689 bool append_album = FALSE;
1691 /* Checking parameters if they are valid */
1692 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1693 media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1694 media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1696 if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1697 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1699 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1700 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1701 return MS_MEDIA_ERR_INVALID_PARAMETER;
1704 if (content_info->size <= 0) {
1705 media_svc_error("invalid size condition[%d]", content_info->size);
1706 return MS_MEDIA_ERR_INVALID_PARAMETER;
1709 if (content_info->modified_time <= 0) {
1710 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1711 return MS_MEDIA_ERR_INVALID_PARAMETER;
1715 media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1717 media_svc_content_info_s _new_content_info;
1718 memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1720 media_svc_media_type_e media_type;
1722 ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1723 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1725 if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1726 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1728 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1729 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1731 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1734 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1736 /* Extracting thumbnail */
1737 int ini_val = _media_svc_get_ini_value();
1739 if (_new_content_info.thumbnail_path == NULL) {
1740 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1741 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1745 ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1746 if (ret == MS_MEDIA_ERR_NONE) {
1747 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1750 if (_new_content_info.media_meta.width <= 0)
1751 _new_content_info.media_meta.width = width;
1753 if (_new_content_info.media_meta.height <= 0)
1754 _new_content_info.media_meta.height = height;
1760 /* set othere data to the structure, which is passed as parameters */
1761 __media_svc_copy_para_to_content(content_info, &_new_content_info);
1763 /* Set or Get folder id */
1764 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);
1765 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1767 ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1768 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1770 /* register album table data */
1773 if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1774 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1776 if (ret != MS_MEDIA_ERR_NONE) {
1777 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1778 media_svc_debug("album does not exist. So start to make album art");
1779 append_album = TRUE;
1781 media_svc_debug("other error");
1782 append_album = FALSE;
1785 _new_content_info.album_id = album_id;
1786 append_album = FALSE;
1788 if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1789 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1791 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1794 media_svc_debug("album already exists. don't need to make album art");
1795 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1796 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1801 if (append_album == TRUE) {
1803 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1804 (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1805 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);
1807 ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1809 _new_content_info.album_id = album_id;
1812 /* Insert to db - calling _media_svc_insert_item_with_data */
1813 ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1815 if (ret == MS_MEDIA_ERR_NONE) {
1816 media_svc_debug("Insertion is successful.");
1819 if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1820 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1823 _media_svc_destroy_content_info(&_new_content_info);
1825 /* handling returned value - important */
1829 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1831 _media_svc_destroy_content_info(content_info);
1834 int media_svc_generate_uuid(char **uuid)
1836 char *gen_uuid = NULL;
1837 gen_uuid = _media_info_generate_uuid();
1838 media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1840 *uuid = strdup(gen_uuid);
1842 return MS_MEDIA_ERR_NONE;
1845 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1847 sqlite3 * db_handle = (sqlite3 *)handle;
1849 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1851 return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1854 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1856 sqlite3 * db_handle = (sqlite3 *)handle;
1858 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1859 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1860 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1861 media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1863 return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1866 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1868 sqlite3 * db_handle = (sqlite3 *)handle;
1870 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1871 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1872 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1874 return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1877 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, const char *storage_path, const char *storage_account, media_svc_storage_type_e storage_type, uid_t uid)
1879 int ret = MS_MEDIA_ERR_NONE;
1880 sqlite3 *db_handle = (sqlite3 *)handle;
1882 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1883 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1884 media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1885 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1887 ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1888 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1890 if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1891 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1892 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1894 ret = _media_svc_update_media_view(db_handle, uid);
1895 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1901 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1903 int ret = MS_MEDIA_ERR_NONE;
1904 sqlite3 *db_handle = (sqlite3 *)handle;
1905 media_svc_storage_type_e storage_type;
1907 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1908 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1910 ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1911 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1913 ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1914 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1916 ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1917 if (ret != MS_MEDIA_ERR_NONE) {
1918 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1921 if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1922 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1923 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1925 ret = _media_svc_update_media_view(db_handle, uid);
1926 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1932 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1934 sqlite3 * db_handle = (sqlite3 *)handle;
1936 media_svc_debug("Transaction data count : [%d]", data_cnt);
1938 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1939 media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1941 g_media_svc_insert_folder_data_cnt = data_cnt;
1942 g_media_svc_insert_folder_cur_data_cnt = 0;
1944 return MS_MEDIA_ERR_NONE;
1947 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1949 int ret = MS_MEDIA_ERR_NONE;
1950 sqlite3 * db_handle = (sqlite3 *)handle;
1952 media_svc_debug_fenter();
1954 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1956 if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1958 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1961 g_media_svc_insert_folder_data_cnt = 1;
1962 g_media_svc_insert_folder_cur_data_cnt = 0;
1967 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1969 int ret = MS_MEDIA_ERR_NONE;
1970 sqlite3 * db_handle = (sqlite3 *)handle;
1971 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1973 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1974 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1975 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1976 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1978 if (g_media_svc_insert_folder_data_cnt == 1) {
1980 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1982 } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1984 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1985 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1987 g_media_svc_insert_folder_cur_data_cnt++;
1989 } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1991 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1992 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1994 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1995 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1997 g_media_svc_insert_folder_cur_data_cnt = 0;
2000 media_svc_error("Error in media_svc_set_insert_folder");
2001 return MS_MEDIA_ERR_INTERNAL;
2007 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
2009 int ret = MS_MEDIA_ERR_NONE;
2010 sqlite3 * db_handle = (sqlite3 *)handle;
2012 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2014 ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
2019 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
2021 int ret = MS_MEDIA_ERR_NONE;
2022 sqlite3 * db_handle = (sqlite3 *)handle;
2024 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2026 ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
2031 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)
2033 int ret = MS_MEDIA_ERR_NONE;
2034 sqlite3 * db_handle = (sqlite3 *)handle;
2035 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2036 media_svc_media_type_e media_type;
2038 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2039 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2040 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2041 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2043 media_svc_content_info_s content_info;
2044 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2046 /*Set basic media info*/
2047 ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
2048 if (ret != MS_MEDIA_ERR_NONE) {
2049 media_svc_error("_media_svc_set_media_info fail %d", ret);
2053 //media_svc_debug("total %d, cur %d insert flag %d", g_media_svc_insert_item_data_cnt, g_media_svc_insert_item_cur_data_cnt, g_insert_with_noti);
2055 /*Set or Get folder id*/
2056 ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2057 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2059 ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2060 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2062 if (g_media_svc_insert_item_data_cnt == 1) {
2064 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2065 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2067 if (g_insert_with_noti)
2068 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2069 } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2071 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2072 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2074 if (g_insert_with_noti)
2075 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2077 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2078 g_media_svc_insert_item_cur_data_cnt++;
2080 } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2082 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2083 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2085 if (g_insert_with_noti)
2086 _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2088 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2089 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2091 media_svc_debug("_media_svc_list_query_do over");
2093 if (g_insert_with_noti) {
2094 media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2095 _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2096 _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2098 /* Recreate noti list */
2099 ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2100 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2103 g_media_svc_insert_item_cur_data_cnt = 0;
2106 media_svc_error("Error in media_svc_insert_item_pass1");
2107 _media_svc_destroy_content_info(&content_info);
2108 return MS_MEDIA_ERR_INTERNAL;
2111 _media_svc_destroy_content_info(&content_info);
2113 return MS_MEDIA_ERR_NONE;
2116 int media_svc_insert_item_pass2(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int scan_type, const char *extract_path, int is_burst, uid_t uid)
2118 int ret = MS_MEDIA_ERR_NONE;
2119 sqlite3 * db_handle = (sqlite3 *)handle;
2120 sqlite3_stmt *sql_stmt = NULL;
2121 media_svc_media_type_e media_type;
2122 media_svc_content_info_s content_info;
2123 GArray *db_data_array = NULL;
2124 media_svc_item_info_s *db_data = NULL;
2128 media_svc_debug_fenter();
2130 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2131 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2132 media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2134 if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2135 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2136 } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2137 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2138 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2139 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2141 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2142 } else { /*Storage Scanning*/
2143 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2146 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2147 if (ret != MS_MEDIA_ERR_NONE) {
2148 media_svc_error("error when get list. err = [%d]", ret);
2152 db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2153 if (db_data_array == NULL) {
2154 media_svc_error("db_data_array is NULL. Out of memory");
2155 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2158 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2160 db_data = malloc(sizeof(media_svc_item_info_s));
2161 if (db_data == NULL) {
2162 media_svc_error("Out of memory");
2166 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2167 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2169 g_array_append_val(db_data_array, db_data);
2172 SQLITE3_FINALIZE(sql_stmt);
2174 while (db_data_array->len != 0) {
2176 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2177 g_array_remove_index(db_data_array, 0);
2179 if ((db_data == NULL) || (db_data->path == NULL)) {
2180 media_svc_error("invalid db data");
2184 media_type = db_data->media_type;
2185 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2186 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2187 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2189 content_info.media_type = media_type;
2190 content_info.storage_type = storage_type;
2192 _media_svc_set_default_value(&content_info, FALSE);
2194 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2196 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2197 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2199 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2202 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2204 _media_svc_destroy_content_info(&content_info);
2205 SAFE_FREE(db_data->path);
2211 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2213 g_array_free(db_data_array, FALSE);
2214 db_data_array = NULL;
2216 media_svc_debug_fleave();
2218 return MS_MEDIA_ERR_NONE;
2221 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2223 int ret = MS_MEDIA_ERR_NONE;
2224 sqlite3 * db_handle = (sqlite3 *)handle;
2226 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2228 ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2233 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2235 int ret = MS_MEDIA_ERR_NONE;
2236 sqlite3 * db_handle = (sqlite3 *)handle;
2239 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2240 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2241 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2243 ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2244 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2247 media_svc_debug("item is exist in database");
2248 return MS_MEDIA_ERR_NONE;
2250 media_svc_debug("item is not exist in database");
2251 return MS_MEDIA_ERR_DB_NO_RECORD;
2254 return MS_MEDIA_ERR_NONE;
2257 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2259 int ret = MS_MEDIA_ERR_NONE;
2260 sqlite3 * db_handle = (sqlite3 *)handle;
2263 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2264 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2265 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2268 ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2269 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2273 media_svc_debug("item is exist in database");
2274 return MS_MEDIA_ERR_NONE;
2276 media_svc_debug("item is not exist in database");
2277 return MS_MEDIA_ERR_DB_NO_RECORD;
2280 return MS_MEDIA_ERR_NONE;
2283 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2285 int ret = MS_MEDIA_ERR_NONE;
2286 sqlite3 * db_handle = (sqlite3 *)handle;
2288 media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2289 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2291 ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);