Rearrange included header files
[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(MEDIA_SVC_DB_TABLE_MEDIA);
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(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_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(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_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         if (ret != MS_MEDIA_ERR_NONE) {
137                 media_svc_error("user_version update fail.");
138                 goto ERROR;
139         }
140 ERROR:
141         _media_svc_destroy_table_query();
142
143         media_svc_debug_fleave();
144
145         return ret;
146 }
147
148 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
149 {
150         return _media_svc_check_data_by_path(handle, path);
151 }
152
153 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
154 {
155         return _media_svc_get_modified_time(handle, path, modified_time);
156 }
157
158 int media_svc_insert_item_begin(bool with_noti, int from_pid)
159 {
160         g_media_svc_cur_data_cnt = 0;
161
162         /* Prepare for making noti item list */
163         if (with_noti) {
164                 media_svc_debug("making noti list from pid[%d]", from_pid);
165                 _media_svc_initialize_noti_list();
166                 _media_svc_set_noti_from_pid(from_pid);
167                 g_insert_with_noti = true;
168         }
169
170         return MS_MEDIA_ERR_NONE;
171 }
172
173 int media_svc_insert_item_end(uid_t uid)
174 {
175         int ret = MS_MEDIA_ERR_NONE;
176
177         media_svc_debug_fenter();
178
179         if (g_media_svc_cur_data_cnt > 0) {
180                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
181                 if (g_insert_with_noti) {
182                         media_svc_debug("sending noti list");
183                         _media_svc_publish_noti_list();
184                         g_insert_with_noti = false;
185                         _media_svc_set_noti_from_pid(-1);
186                 }
187         }
188
189         g_media_svc_cur_data_cnt = 0;
190
191         return ret;
192 }
193
194 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)
195 {
196         int ret = MS_MEDIA_ERR_NONE;
197         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
198
199         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
200         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
201         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
202         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
203
204         media_svc_content_info_s content_info;
205         memset(&content_info, 0, sizeof(media_svc_content_info_s));
206
207         /*Set media info*/
208         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
209         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
210         if (ret != MS_MEDIA_ERR_NONE)
211                 return ret;
212
213         switch (content_info.media_type) {
214         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
215                 ret = _media_svc_extract_image_metadata(&content_info);
216                 break;
217         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
218         case MEDIA_SVC_MEDIA_TYPE_SOUND:
219         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
220                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
221                 break;
222         default:
223                 /* The 'TITLE' should always be filled in */
224                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
225                 break;
226         }
227
228         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
229
230         /*Set or Get folder id*/
231         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
232         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
233
234         content_info.folder_uuid = g_strdup(folder_uuid);
235         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
236
237         ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
238         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
239
240         if (g_insert_with_noti)
241                 _media_svc_insert_item_to_noti_list(&content_info);
242
243         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
244         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
245                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
246                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
247
248                 if (g_insert_with_noti)
249                         _media_svc_publish_noti_list();
250
251                 g_media_svc_cur_data_cnt = 0;
252         }
253
254         _media_svc_destroy_content_info(&content_info);
255
256         return MS_MEDIA_ERR_NONE;
257 }
258
259 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)
260 {
261         int ret = MS_MEDIA_ERR_NONE;
262         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
263
264         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
265         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
266         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
267         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
268
269         media_svc_content_info_s content_info;
270         memset(&content_info, 0, sizeof(media_svc_content_info_s));
271
272         /*Set media info*/
273         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
274         if (ret != MS_MEDIA_ERR_NONE)
275                 return ret;
276
277         switch (content_info.media_type) {
278         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
279                 ret = _media_svc_extract_image_metadata(&content_info);
280                 break;
281         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
282         case MEDIA_SVC_MEDIA_TYPE_SOUND:
283         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
284                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
285                 break;
286         default:
287                 /* The 'TITLE' should always be filled in */
288                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
289                 break;
290         }
291
292         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
293
294         /*Set or Get folder id*/
295         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
296         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
297
298         content_info.folder_uuid = g_strdup(folder_uuid);
299         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
300
301         /* Extracting thumbnail */
302         if (content_info.thumbnail_path == NULL) {
303                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
304                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
305
306                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
307                         if (ret == MS_MEDIA_ERR_NONE)
308                                 content_info.thumbnail_path = g_strdup(thumb_path);
309                 }
310         }
311
312         ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
313
314         if (ret == MS_MEDIA_ERR_NONE) {
315                 media_svc_debug("Insertion is successful. Sending noti for this");
316                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
317         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
318                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
319         }
320
321         _media_svc_destroy_content_info(&content_info);
322         return ret;
323 }
324
325 int media_svc_move_item(sqlite3 *handle,
326                                                                 const char *src_path,
327                                                                 const char *dest_path,
328                                                                 const char *media_id,
329                                                                 int media_type,
330                                                                 const char *mime_type,
331                                                                 uid_t uid)
332 {
333         int ret = MS_MEDIA_ERR_NONE;
334         char *file_name = NULL;
335         char *folder_path = NULL;
336         int modified_time = 0;
337         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
338         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
339         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
340         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
341         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
342
343         media_svc_debug_fenter();
344
345         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
346         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
347         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
348         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
349         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
350
351         /* Get storage_id */
352         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
353         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
354         /* Get storage_type */
355         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
356         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
357         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
358         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
359
360         /*check and update folder*/
361         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
362         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
363
364         /*get filename*/
365         file_name = g_path_get_basename(dest_path);
366
367         /*get modified_time*/
368         modified_time = _media_svc_get_file_time(dest_path);
369
370         /*get old thumbnail_path and remove thumbnail */
371         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
372         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
373                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
374                 SAFE_FREE(file_name);
375                 return ret;
376         }
377
378         _media_svc_remove_file(old_thumb_path);
379
380         /*move item*/
381         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
382         SAFE_FREE(file_name);
383         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
384
385         media_svc_debug("Move is successful. Sending noti for this");
386         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
387
388         /*update folder modified_time*/
389         folder_path = g_path_get_dirname(dest_path);
390         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
391         SAFE_FREE(folder_path);
392         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
393
394         return MS_MEDIA_ERR_NONE;
395 }
396
397 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
398 {
399         int ret = MS_MEDIA_ERR_NONE;
400
401         ret = _media_svc_update_item_validity(path, validity, true, uid);
402         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
403
404         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
405         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
406                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
407                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
408
409                 g_media_svc_cur_data_cnt = 0;
410         }
411
412         return ret;
413 }
414
415 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
416 {
417         int ret = MS_MEDIA_ERR_NONE;
418         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
419         media_svc_noti_item *noti_item = NULL;
420
421         media_svc_debug_fenter();
422
423         /*Get thumbnail path to delete*/
424         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
425         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
426
427         /* Get notification info */
428         ret = _media_svc_get_noti_info(handle, path, &noti_item);
429         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
430
431         /*Delete item*/
432         ret = _media_svc_delete_item_by_path(path, uid);
433         if (ret != MS_MEDIA_ERR_NONE) {
434                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
435                 _media_svc_destroy_noti_item(noti_item);
436
437                 return ret;
438         }
439
440         /* Send notification */
441         media_svc_debug("Deletion is successful. Sending noti for this");
442         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
443         _media_svc_destroy_noti_item(noti_item);
444
445         /*Delete thumbnail*/
446         _media_svc_remove_file(thumb_path);
447
448         return MS_MEDIA_ERR_NONE;
449 }
450
451 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)
452 {
453         int ret = MS_MEDIA_ERR_NONE;
454         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
455
456         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
457         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
458         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
459         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
460
461         media_svc_content_info_s content_info;
462         memset(&content_info, 0, sizeof(media_svc_content_info_s));
463
464         /*Set media info*/
465         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
466         if (ret != MS_MEDIA_ERR_NONE)
467                 return ret;
468
469         /* Initialize thumbnail information to remake thumbnail. */
470         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
471         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
472                 _media_svc_destroy_content_info(&content_info);
473                 return ret;
474         }
475
476         if (STRING_VALID(thumb_path)) {
477                 _media_svc_remove_file(thumb_path);
478
479                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
480                 if (ret != MS_MEDIA_ERR_NONE) {
481                         _media_svc_destroy_content_info(&content_info);
482                         return ret;
483                 }
484         }
485
486         /* Get notification info */
487         media_svc_noti_item *noti_item = NULL;
488         ret = _media_svc_get_noti_info(handle, path, &noti_item);
489         if (ret != MS_MEDIA_ERR_NONE) {
490                 _media_svc_destroy_content_info(&content_info);
491                 return ret;
492         }
493
494         content_info.media_type = noti_item->media_type;
495         content_info.mime_type = g_strdup(noti_item->mime_type);
496
497         switch (content_info.media_type) {
498         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
499                 ret = _media_svc_extract_image_metadata(&content_info);
500                 break;
501         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
502         case MEDIA_SVC_MEDIA_TYPE_SOUND:
503         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
504                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
505                 break;
506         default:
507                 /* The 'TITLE' should always be filled in */
508                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
509                 break;
510         }
511
512         if (ret != MS_MEDIA_ERR_NONE) {
513                 _media_svc_destroy_noti_item(noti_item);
514                 _media_svc_destroy_content_info(&content_info);
515                 return ret;
516         }
517
518         /* Extracting thumbnail */
519         if (content_info.thumbnail_path == NULL) {
520                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
521                         memset(thumb_path, 0, sizeof(thumb_path));
522
523                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
524                         if (ret == MS_MEDIA_ERR_NONE)
525                                 content_info.thumbnail_path = g_strdup(thumb_path);
526                 }
527         }
528
529         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
530
531         if (ret == MS_MEDIA_ERR_NONE) {
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         } else {
535                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
536         }
537
538         _media_svc_destroy_content_info(&content_info);
539         _media_svc_destroy_noti_item(noti_item);
540
541         return ret;
542 }
543
544 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
545 {
546         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
547
548         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
549 }
550
551 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
552 {
553         media_svc_debug_fenter();
554
555         return _media_svc_check_db_upgrade(handle, user_version, uid);
556 }
557
558 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
559 {
560         int ret = MS_MEDIA_ERR_NONE;
561         char *root_path = NULL;
562         GPtrArray *path_list = NULL;
563         int i = 0;
564
565         ret = ms_user_get_internal_root_path(uid, &root_path);
566         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
567
568         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
569         if (ret != MS_MEDIA_ERR_NONE)
570                 media_svc_error("Fail to send noti");
571
572         SAFE_FREE(root_path);
573
574         path_list = g_ptr_array_new_with_free_func(g_free);
575         _media_svc_get_storage_path(handle, &path_list);
576
577         for (i = 0; i < path_list->len; i++) {
578                 root_path = g_ptr_array_index(path_list, i);
579
580                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
581                 if (ret != MS_MEDIA_ERR_NONE)
582                         media_svc_error("Fail to send noti");
583         }
584
585         g_ptr_array_free(path_list, TRUE);
586 }
587
588 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
589 {
590         int ret = MS_MEDIA_ERR_NONE;
591         int i = 0;
592         char *sql = NULL;
593         char *file_path = NULL;
594         media_svc_content_info_s content_info;
595         GPtrArray *path_list = NULL;
596
597         path_list = g_ptr_array_new_with_free_func(g_free);
598         media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
599
600         sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", MEDIA_SVC_DB_TABLE_MEDIA);
601         ret = _media_svc_get_media(handle, sql, &path_list);
602         if (ret != MS_MEDIA_ERR_NONE) {
603                 media_svc_error("Fail to get media list");
604                 g_ptr_array_free(path_list, TRUE);
605                 return ret;
606         }
607
608         for (i = 0; i < path_list->len; i++) {
609                 file_path = g_ptr_array_index(path_list, i);
610
611                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
612                 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_path);
613                 if (ret != MS_MEDIA_ERR_NONE) {
614                         media_svc_error("Fail to extract metadata");
615                         _media_svc_destroy_content_info(&content_info);
616                         continue;
617                 }
618
619                 ret = _media_svc_update_meta_with_data(&content_info);
620                 if (ret != MS_MEDIA_ERR_NONE)
621                         media_svc_error("Fail to append item[%s]", content_info.path);
622
623                 _media_svc_destroy_content_info(&content_info);
624         }
625
626         g_ptr_array_free(path_list, TRUE);
627
628         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
629         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
630
631         /* Noti for this */
632         __media_svc_noti_all_storage(handle, uid);
633
634         return ret;
635 }
636
637 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)
638 {
639         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
640 }
641
642 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
643 {
644         return _media_svc_get_pinyin_str(src_str, pinyin_str);
645 }
646
647 int media_svc_check_pinyin_support(bool *support)
648 {
649         *support = _media_svc_check_pinyin_support();
650
651         return MS_MEDIA_ERR_NONE;
652 }
653
654 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
655 {
656         int ret = MS_MEDIA_ERR_NONE;
657
658         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
659
660         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
661         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
662
663         return ret;
664 }
665
666 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
667 {
668         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
669 }
670
671 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
672 {
673         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
674 }
675
676 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
677 {
678         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
679 }
680
681 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)
682 {
683         int ret = MS_MEDIA_ERR_NONE;
684
685         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
686         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
687         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
688         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
689
690         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
691         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
692
693         /* Remove external storage that validity is 0 */
694         ret = _media_svc_delete_invalid_storage(handle, uid);
695         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
696
697         return ret;
698 }
699
700 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
701 {
702         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
703         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
704         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
705         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
706
707         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
708 }
709
710 int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
711 {
712         return _media_svc_set_folder_validity(true, storage_id, start_path, validity, is_recursive, uid);
713 }
714
715 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
716 {
717         return _media_svc_check_folder_by_path(handle, storage_id, folder_path);
718 }
719
720 int media_svc_append_query(const char *query, uid_t uid)
721 {
722         return _media_svc_append_query_list(query, uid);
723 }
724
725 int media_svc_send_query(uid_t uid)
726 {
727         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
728 }
729
730 int media_svc_get_media_type(const char *path, int *mediatype)
731 {
732         return _media_svc_get_media_type(path, mediatype);
733 }
734
735 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
736 {
737         int ret = MS_MEDIA_ERR_NONE;
738         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
739         char *sql = NULL;
740
741         // 1. Check media type
742         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
743                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
744
745         // 2. try to create thumbnail
746         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
747         if (ret != MS_MEDIA_ERR_NONE) {
748                 media_svc_error("Failed to create thumbnail [%d]", ret);
749                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
750                         return ret;
751         }
752
753         // 3. Update creation result to media db
754         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
755
756         ret = _media_svc_sql_query(sql, uid);
757         SQLITE3_SAFE_FREE(sql);
758         if (ret != MS_MEDIA_ERR_NONE) {
759                 media_svc_error("Failed to update media db [%d]", ret);
760                 *thumbnail_path = g_strdup("");
761         } else {
762                 *thumbnail_path = g_strdup(thumb_path);
763         }
764
765         return ret;
766 }