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