Merge "Version up (0.4.14 to 0.4.15)" into tizen
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include "media-svc-media.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-util.h"
25 #include "media-svc-db-utils.h"
26 #include "media-svc-env.h"
27 #include "media-svc-media-folder.h"
28 #include "media-svc-album.h"
29 #include "media-svc-noti.h"
30 #include "media-svc-storage.h"
31
32 //static __thread int g_media_svc_data_cnt = 0;
33 static __thread int g_media_svc_cur_data_cnt = 0;
34
35 /* Flag for items to be published by notification */
36 static __thread bool g_insert_with_noti = false;
37
38 #define BATCH_ITEM_COUNT_MAX 100
39
40 int media_svc_get_user_version(sqlite3 *handle, int *user_version)
41 {
42         return _media_svc_get_user_version(handle, user_version);
43 }
44
45 int media_svc_create_table(uid_t uid)
46 {
47         int ret = MS_MEDIA_ERR_NONE;
48         char *sql = NULL;
49
50         media_svc_debug_fenter();
51
52         ret = _media_svc_init_table_query();
53         if (ret != MS_MEDIA_ERR_NONE) {
54                 media_svc_error("_media_svc_init_table_query fail.");
55                 goto ERROR;
56         }
57
58         /*create media table*/
59         ret = _media_svc_make_table_query(DB_TABLE_MEDIA, DB_LIST_MEDIA, uid);
60         if (ret != MS_MEDIA_ERR_NONE) {
61                 media_svc_error("_media_svc_make_table_query fail.");
62                 goto ERROR;
63         }
64
65         /*create folder table*/
66         ret = _media_svc_make_table_query(DB_TABLE_FOLDER, DB_LIST_FOLDER, uid);
67         if (ret != MS_MEDIA_ERR_NONE) {
68                 media_svc_error("_media_svc_make_table_query fail.");
69                 goto ERROR;
70         }
71
72         /*create playlist_map table*/
73         ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST_MAP, DB_LIST_PLAYLIST_MAP, uid);
74         if (ret != MS_MEDIA_ERR_NONE) {
75                 media_svc_error("_media_svc_make_table_query fail.");
76                 goto ERROR;
77         }
78
79         /*create playlist table*/
80         ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST, DB_LIST_PLAYLIST, uid);
81         if (ret != MS_MEDIA_ERR_NONE) {
82                 media_svc_error("_media_svc_make_table_query fail.");
83                 goto ERROR;
84         }
85
86         /* create album table*/
87         ret = _media_svc_make_table_query(DB_TABLE_ALBUM, DB_LIST_ALBUM, uid);
88         if (ret != MS_MEDIA_ERR_NONE) {
89                 media_svc_error("_media_svc_make_table_query fail.");
90                 goto ERROR;
91         }
92
93         /*create tag_map table*/
94         ret = _media_svc_make_table_query(DB_TABLE_TAG_MAP, DB_LIST_TAG_MAP, uid);
95         if (ret != MS_MEDIA_ERR_NONE) {
96                 media_svc_error("_media_svc_make_table_query fail.");
97                 goto ERROR;
98         }
99
100         /*create tag table*/
101         ret = _media_svc_make_table_query(DB_TABLE_TAG, DB_LIST_TAG, uid);
102         if (ret != MS_MEDIA_ERR_NONE) {
103                 media_svc_error("_media_svc_make_table_query fail.");
104                 goto ERROR;
105         }
106
107         /*create bookmark table*/
108         ret = _media_svc_make_table_query(DB_TABLE_BOOKMARK, DB_LIST_BOOKMARK, uid);
109         if (ret != MS_MEDIA_ERR_NONE) {
110                 media_svc_error("_media_svc_make_table_query fail.");
111                 goto ERROR;
112         }
113
114         /*create storage table from tizen 2.4 */
115         ret = _media_svc_make_table_query(DB_TABLE_STORAGE, DB_LIST_STORAGE, uid);
116         if (ret != MS_MEDIA_ERR_NONE) {
117                 media_svc_error("_media_svc_make_table_query fail.");
118                 goto ERROR;
119         }
120
121         /*create face table. from tizen 3.0*/
122         ret = _media_svc_make_table_query(DB_TABLE_FACE_SCAN_LIST, DB_LIST_FACE_SCAN_LIST, uid);
123         if (ret != MS_MEDIA_ERR_NONE) {
124                 media_svc_error("_media_svc_make_table_query fail.");
125                 goto ERROR;
126         }
127
128         ret = _media_svc_make_table_query(DB_TABLE_FACE, DB_LIST_FACE, uid);
129         if (ret != MS_MEDIA_ERR_NONE) {
130                 media_svc_error("_media_svc_make_table_query fail.");
131                 goto ERROR;
132         }
133
134         sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_DB_VERSION);
135         ret = _media_svc_sql_query(sql, uid);
136         SQLITE3_SAFE_FREE(sql);
137         if (ret != MS_MEDIA_ERR_NONE) {
138                 media_svc_error("user_version update fail.");
139                 goto ERROR;
140         }
141 ERROR:
142         _media_svc_destroy_table_query();
143
144         media_svc_debug_fleave();
145
146         return ret;
147 }
148
149 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
150 {
151         return _media_svc_check_data_by_path(handle, path);
152 }
153
154 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
155 {
156         return _media_svc_get_modified_time(handle, path, modified_time);
157 }
158
159 int media_svc_insert_item_begin(bool with_noti, int from_pid)
160 {
161         g_media_svc_cur_data_cnt = 0;
162
163         /* Prepare for making noti item list */
164         if (with_noti) {
165                 media_svc_debug("making noti list from pid[%d]", from_pid);
166                 _media_svc_initialize_noti_list();
167                 _media_svc_set_noti_from_pid(from_pid);
168                 g_insert_with_noti = true;
169         }
170
171         return MS_MEDIA_ERR_NONE;
172 }
173
174 int media_svc_insert_item_end(uid_t uid)
175 {
176         int ret = MS_MEDIA_ERR_NONE;
177
178         media_svc_debug_fenter();
179
180         if (g_media_svc_cur_data_cnt > 0) {
181                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
182                 if (g_insert_with_noti) {
183                         media_svc_debug("sending noti list");
184                         _media_svc_publish_noti_list();
185                         g_insert_with_noti = false;
186                         _media_svc_set_noti_from_pid(-1);
187                 }
188         }
189
190         g_media_svc_cur_data_cnt = 0;
191
192         return ret;
193 }
194
195 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)
196 {
197         int ret = MS_MEDIA_ERR_NONE;
198         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
199
200         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
201         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
202         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
203         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
204
205         media_svc_content_info_s content_info;
206         memset(&content_info, 0, sizeof(media_svc_content_info_s));
207
208         /*Set media info*/
209         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
210         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
211         if (ret != MS_MEDIA_ERR_NONE)
212                 return ret;
213
214         switch (content_info.media_type) {
215         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
216                 ret = _media_svc_extract_image_metadata(&content_info);
217                 break;
218         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
219         case MEDIA_SVC_MEDIA_TYPE_SOUND:
220         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
221                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
222                 break;
223         default:
224                 /* The 'TITLE' should always be filled in */
225                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
226                 break;
227         }
228
229         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
230
231         /*Set or Get folder id*/
232         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
233         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
234
235         content_info.folder_uuid = g_strdup(folder_uuid);
236         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
237
238         ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
239         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
240
241         if (g_insert_with_noti)
242                 _media_svc_insert_item_to_noti_list(&content_info);
243
244         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
245         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
246                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
247                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
248
249                 if (g_insert_with_noti)
250                         _media_svc_publish_noti_list();
251
252                 g_media_svc_cur_data_cnt = 0;
253         }
254
255         _media_svc_destroy_content_info(&content_info);
256
257         return MS_MEDIA_ERR_NONE;
258 }
259
260 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)
261 {
262         int ret = MS_MEDIA_ERR_NONE;
263         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
264
265         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
266         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
267         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
268         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
269
270         media_svc_content_info_s content_info;
271         memset(&content_info, 0, sizeof(media_svc_content_info_s));
272
273         /*Set media info*/
274         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
275         if (ret != MS_MEDIA_ERR_NONE)
276                 return ret;
277
278         switch (content_info.media_type) {
279         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
280                 ret = _media_svc_extract_image_metadata(&content_info);
281                 break;
282         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
283         case MEDIA_SVC_MEDIA_TYPE_SOUND:
284         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
285                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
286                 break;
287         default:
288                 /* The 'TITLE' should always be filled in */
289                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
290                 break;
291         }
292
293         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
294
295         /*Set or Get folder id*/
296         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
297         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
298
299         content_info.folder_uuid = g_strdup(folder_uuid);
300         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
301
302         /* Extracting thumbnail */
303         if (content_info.thumbnail_path == NULL) {
304                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
305                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
306
307                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
308                         if (ret == MS_MEDIA_ERR_NONE)
309                                 content_info.thumbnail_path = g_strdup(thumb_path);
310                 }
311         }
312
313         ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
314
315         if (ret == MS_MEDIA_ERR_NONE) {
316                 media_svc_debug("Insertion is successful. Sending noti for this");
317                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
318         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
319                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
320         }
321
322         _media_svc_destroy_content_info(&content_info);
323         return ret;
324 }
325
326 int media_svc_move_item(sqlite3 *handle,
327                                                 const char *src_path,
328                                                 const char *dest_path,
329                                                 const char *media_id,
330                                                 int media_type,
331                                                 const char *mime_type,
332                                                 uid_t uid)
333 {
334         int ret = MS_MEDIA_ERR_NONE;
335         char *file_name = NULL;
336         char *folder_path = NULL;
337         int modified_time = 0;
338         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
339         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
340         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
341         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
342         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
343
344         media_svc_debug_fenter();
345
346         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
347         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
348         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
349         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
350         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
351
352         /* Get storage_id */
353         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
354         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
355         /* Get storage_type */
356         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
357         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
358         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
359         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
360
361         /*check and update folder*/
362         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
363         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
364
365         /*get filename*/
366         file_name = g_path_get_basename(dest_path);
367
368         /*get modified_time*/
369         modified_time = _media_svc_get_file_time(dest_path);
370
371         /*get old thumbnail_path and remove thumbnail */
372         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
373         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
374                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
375                 g_free(file_name);
376                 return ret;
377         }
378
379         _media_svc_remove_file(old_thumb_path);
380
381         /*move item*/
382         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
383         g_free(file_name);
384         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
385
386         media_svc_debug("Move is successful. Sending noti for this");
387         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
388
389         /*update folder modified_time*/
390         folder_path = g_path_get_dirname(dest_path);
391         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
392         g_free(folder_path);
393         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
394
395         return MS_MEDIA_ERR_NONE;
396 }
397
398 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
399 {
400         int ret = MS_MEDIA_ERR_NONE;
401
402         ret = _media_svc_update_item_validity(path, validity, true, uid);
403         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
404
405         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
406         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
407                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
408                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
409
410                 g_media_svc_cur_data_cnt = 0;
411         }
412
413         return ret;
414 }
415
416 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
417 {
418         int ret = MS_MEDIA_ERR_NONE;
419         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
420         media_svc_noti_item *noti_item = NULL;
421
422         media_svc_debug_fenter();
423
424         /*Get thumbnail path to delete*/
425         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
426         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
427
428         /* Get notification info */
429         ret = _media_svc_get_noti_info(handle, path, &noti_item);
430         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
431
432         /*Delete item*/
433         ret = _media_svc_delete_item_by_path(path, uid);
434         if (ret != MS_MEDIA_ERR_NONE) {
435                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
436                 _media_svc_destroy_noti_item(noti_item);
437
438                 return ret;
439         }
440
441         /* Send notification */
442         media_svc_debug("Deletion is successful. Sending noti for this");
443         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
444         _media_svc_destroy_noti_item(noti_item);
445
446         /*Delete thumbnail*/
447         _media_svc_remove_file(thumb_path);
448
449         return MS_MEDIA_ERR_NONE;
450 }
451
452 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)
453 {
454         int ret = MS_MEDIA_ERR_NONE;
455         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
456         media_svc_content_info_s content_info = {0, };
457         media_svc_noti_item *noti_item = NULL;
458
459         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
460         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
461         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
462         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
463
464         /*Set media info*/
465         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
466         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
467
468         /* Initialize thumbnail information to remake thumbnail. */
469         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
470         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
471                 goto REFRESH_FINALIZE;
472
473         if (STRING_VALID(thumb_path)) {
474                 _media_svc_remove_file(thumb_path);
475
476                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
477                 if (ret != MS_MEDIA_ERR_NONE)
478                         goto REFRESH_FINALIZE;
479         }
480
481         /* Get notification info */
482         ret = _media_svc_get_noti_info(handle, path, &noti_item);
483         if (ret != MS_MEDIA_ERR_NONE)
484                 goto REFRESH_FINALIZE;
485
486         content_info.media_type = noti_item->media_type;
487         content_info.mime_type = g_strdup(noti_item->mime_type);
488
489         switch (content_info.media_type) {
490         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
491                 ret = _media_svc_extract_image_metadata(&content_info);
492                 break;
493         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
494         case MEDIA_SVC_MEDIA_TYPE_SOUND:
495         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
496                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
497                 break;
498         default:
499                 /* The 'TITLE' should always be filled in */
500                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
501                 break;
502         }
503
504         if (ret != MS_MEDIA_ERR_NONE)
505                 goto REFRESH_FINALIZE;
506
507         /* Extracting thumbnail */
508         if (content_info.thumbnail_path == NULL) {
509                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
510                         memset(thumb_path, 0, sizeof(thumb_path));
511
512                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
513                         if (ret == MS_MEDIA_ERR_NONE)
514                                 content_info.thumbnail_path = g_strdup(thumb_path);
515                 }
516         }
517
518         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
519
520         if (ret == MS_MEDIA_ERR_NONE) {
521                 if (is_direct) {
522                         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
523                         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
524                                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
525                                 if (ret != MS_MEDIA_ERR_NONE)
526                                         goto REFRESH_FINALIZE;
527
528                                 g_media_svc_cur_data_cnt = 0;
529                         }
530                 } else {
531                         /* Except scanner case */
532                         media_svc_debug("Update is successful. Sending noti for this");
533                         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
534                 }
535         } else {
536                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
537         }
538
539 REFRESH_FINALIZE:
540         _media_svc_destroy_content_info(&content_info);
541         _media_svc_destroy_noti_item(noti_item);
542
543         return ret;
544 }
545
546 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
547 {
548         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
549
550         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
551 }
552
553 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
554 {
555         media_svc_debug_fenter();
556
557         return _media_svc_check_db_upgrade(handle, user_version, uid);
558 }
559
560 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
561 {
562         int ret = MS_MEDIA_ERR_NONE;
563         char *root_path = NULL;
564         GPtrArray *path_list = NULL;
565         int i = 0;
566
567         ret = ms_user_get_internal_root_path(uid, &root_path);
568         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
569
570         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
571         if (ret != MS_MEDIA_ERR_NONE)
572                 media_svc_error("Fail to send noti");
573
574         g_free(root_path);
575
576         path_list = g_ptr_array_new_with_free_func(g_free);
577         _media_svc_get_storage_path(handle, &path_list);
578
579         for (i = 0; i < path_list->len; i++) {
580                 root_path = g_ptr_array_index(path_list, i);
581
582                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
583                 if (ret != MS_MEDIA_ERR_NONE)
584                         media_svc_error("Fail to send noti");
585         }
586
587         g_ptr_array_free(path_list, TRUE);
588 }
589
590 static void __media_svc_foreach_update_media(gpointer data, gpointer user_data)
591 {
592         media_svc_content_info_s content_info = {0, };
593
594         if (_media_svc_extract_music_metadata_for_update(&content_info, (const char *)data) != MS_MEDIA_ERR_NONE) {
595                 media_svc_error("Fail to extract metadata");
596                 _media_svc_destroy_content_info(&content_info);
597                 return;
598         }
599
600         if (_media_svc_update_meta_with_data(&content_info) != MS_MEDIA_ERR_NONE)
601                 media_svc_error("Fail to append item[%s]", content_info.path);
602
603         _media_svc_destroy_content_info(&content_info);
604 }
605
606 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
607 {
608         int ret = MS_MEDIA_ERR_NONE;
609         char *sql = NULL;
610         GList *path_list = NULL;
611
612         sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", DB_TABLE_MEDIA);
613         ret = _media_svc_get_media(handle, sql, &path_list);
614         SQLITE3_SAFE_FREE(sql);
615         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get media list");
616
617         if (path_list) {
618                 g_list_foreach(path_list, __media_svc_foreach_update_media, NULL);
619                 g_list_free_full(path_list, g_free);
620
621                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
622                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
623         }
624
625         /* Noti for this */
626         __media_svc_noti_all_storage(handle, uid);
627
628         return ret;
629 }
630
631 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)
632 {
633         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
634 }
635
636 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
637 {
638         return _media_svc_get_pinyin_str(src_str, pinyin_str);
639 }
640
641 int media_svc_check_pinyin_support(bool *support)
642 {
643         *support = _media_svc_check_pinyin_support();
644
645         return MS_MEDIA_ERR_NONE;
646 }
647
648 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
649 {
650         int ret = MS_MEDIA_ERR_NONE;
651
652         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
653
654         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
655         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
656
657         return ret;
658 }
659
660 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
661 {
662         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
663 }
664
665 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
666 {
667         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
668 }
669
670 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
671 {
672         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
673 }
674
675 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)
676 {
677         int ret = MS_MEDIA_ERR_NONE;
678
679         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
680         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
681         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
682         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
683
684         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
685         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
686
687         /* Remove external storage that validity is 0 */
688         ret = _media_svc_delete_invalid_storage(handle, uid);
689         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
690
691         return ret;
692 }
693
694 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
695 {
696         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
697         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
698         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
699         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
700
701         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
702 }
703
704 int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
705 {
706         return _media_svc_set_folder_validity(true, storage_id, start_path, validity, is_recursive, uid);
707 }
708
709 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
710 {
711         return _media_svc_check_folder_by_path(handle, storage_id, folder_path);
712 }
713
714 int media_svc_append_query(const char *query, uid_t uid)
715 {
716         return _media_svc_append_query_list(query, uid);
717 }
718
719 int media_svc_send_query(uid_t uid)
720 {
721         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
722 }
723
724 int media_svc_get_media_type(const char *path, int *mediatype)
725 {
726         return _media_svc_get_media_type(path, mediatype);
727 }
728
729 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
730 {
731         int ret = MS_MEDIA_ERR_NONE;
732         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
733         char *sql = NULL;
734
735         // 1. Check media type
736         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
737                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
738
739         // 2. try to create thumbnail
740         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
741         if (ret != MS_MEDIA_ERR_NONE) {
742                 media_svc_error("Failed to create thumbnail [%d]", ret);
743                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
744                         return ret;
745         }
746
747         // 3. Update creation result to media db
748         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);
749
750         ret = _media_svc_sql_query(sql, uid);
751         SQLITE3_SAFE_FREE(sql);
752         if (ret != MS_MEDIA_ERR_NONE) {
753                 media_svc_error("Failed to update media db [%d]", ret);
754                 *thumbnail_path = g_strdup("");
755         } else {
756                 *thumbnail_path = g_strdup(thumb_path);
757         }
758
759         return ret;
760 }