Add to search ebooks with keywords
[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         case MEDIA_SVC_MEDIA_TYPE_BOOK:
224                 ret = _media_svc_extract_book_metadata(&content_info);
225                 /* The 'TITLE' should always be filled in */
226                 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
227                         g_free(content_info.media_meta.title);
228                         content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
229                 }
230                 break;
231         default:
232                 /* The 'TITLE' should always be filled in */
233                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
234                 break;
235         }
236
237         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
238
239         /*Set or Get folder id*/
240         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
241         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
242
243         content_info.folder_uuid = g_strdup(folder_uuid);
244         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
245
246         ret = _media_svc_insert_item_with_data(true, &content_info, true, 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_insert_item_to_noti_list(&content_info);
251
252         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
253         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
254                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
255                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
256
257                 if (g_insert_with_noti)
258                         _media_svc_publish_noti_list();
259
260                 g_media_svc_cur_data_cnt = 0;
261         }
262
263         _media_svc_destroy_content_info(&content_info);
264
265         return MS_MEDIA_ERR_NONE;
266 }
267
268 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)
269 {
270         int ret = MS_MEDIA_ERR_NONE;
271         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
272
273         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
274         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
275         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
276         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
277
278         media_svc_content_info_s content_info;
279         memset(&content_info, 0, sizeof(media_svc_content_info_s));
280
281         /*Set media info*/
282         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
283         if (ret != MS_MEDIA_ERR_NONE)
284                 return ret;
285
286         switch (content_info.media_type) {
287         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
288                 ret = _media_svc_extract_image_metadata(&content_info);
289                 break;
290         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
291         case MEDIA_SVC_MEDIA_TYPE_SOUND:
292         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
293                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
294                 break;
295         case MEDIA_SVC_MEDIA_TYPE_BOOK:
296                 ret = _media_svc_extract_book_metadata(&content_info);
297                 /* The 'TITLE' should always be filled in */
298                 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
299                         g_free(content_info.media_meta.title);
300                         content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
301                 }
302                 break;
303         default:
304                 /* The 'TITLE' should always be filled in */
305                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
306                 break;
307         }
308
309         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
310
311         /*Set or Get folder id*/
312         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
313         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
314
315         content_info.folder_uuid = g_strdup(folder_uuid);
316         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
317
318         /* Extracting thumbnail */
319         if (content_info.thumbnail_path == NULL) {
320                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
321                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
322
323                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
324                         if (ret == MS_MEDIA_ERR_NONE)
325                                 content_info.thumbnail_path = g_strdup(thumb_path);
326                 }
327         }
328
329         ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
330
331         if (ret == MS_MEDIA_ERR_NONE) {
332                 media_svc_debug("Insertion is successful. Sending noti for this");
333                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
334         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
335                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
336         }
337
338         _media_svc_destroy_content_info(&content_info);
339         return ret;
340 }
341
342 int media_svc_move_item(sqlite3 *handle,
343                                                 const char *src_path,
344                                                 const char *dest_path,
345                                                 const char *media_id,
346                                                 int media_type,
347                                                 const char *mime_type,
348                                                 uid_t uid)
349 {
350         int ret = MS_MEDIA_ERR_NONE;
351         char *file_name = NULL;
352         char *folder_path = NULL;
353         int modified_time = 0;
354         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
355         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
356         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
357         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
358         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
359
360         media_svc_debug_fenter();
361
362         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
363         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
364         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
365         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
366         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
367
368         /* Get storage_id */
369         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
370         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
371         /* Get storage_type */
372         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
373         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
374         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
375         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
376
377         /*check and update folder*/
378         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
379         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
380
381         /*get filename*/
382         file_name = g_path_get_basename(dest_path);
383
384         /*get modified_time*/
385         modified_time = _media_svc_get_file_time(dest_path);
386
387         /*get old thumbnail_path and remove thumbnail */
388         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
389         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
390                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
391                 g_free(file_name);
392                 return ret;
393         }
394
395         _media_svc_remove_file(old_thumb_path);
396
397         /*move item*/
398         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
399         g_free(file_name);
400         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
401
402         media_svc_debug("Move is successful. Sending noti for this");
403         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
404
405         /*update folder modified_time*/
406         folder_path = g_path_get_dirname(dest_path);
407         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
408         g_free(folder_path);
409         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
410
411         return MS_MEDIA_ERR_NONE;
412 }
413
414 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
415 {
416         int ret = MS_MEDIA_ERR_NONE;
417
418         ret = _media_svc_update_item_validity(path, validity, true, uid);
419         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
420
421         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
422         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
423                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
424                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
425
426                 g_media_svc_cur_data_cnt = 0;
427         }
428
429         return ret;
430 }
431
432 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
433 {
434         int ret = MS_MEDIA_ERR_NONE;
435         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
436         media_svc_noti_item *noti_item = NULL;
437
438         media_svc_debug_fenter();
439
440         /*Get thumbnail path to delete*/
441         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
442         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
443
444         /* Get notification info */
445         ret = _media_svc_get_noti_info(handle, path, &noti_item);
446         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
447
448         /*Delete item*/
449         ret = _media_svc_delete_item_by_path(path, uid);
450         if (ret != MS_MEDIA_ERR_NONE) {
451                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
452                 _media_svc_destroy_noti_item(noti_item);
453
454                 return ret;
455         }
456
457         /* Send notification */
458         media_svc_debug("Deletion is successful. Sending noti for this");
459         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
460         _media_svc_destroy_noti_item(noti_item);
461
462         /*Delete thumbnail*/
463         _media_svc_remove_file(thumb_path);
464
465         return MS_MEDIA_ERR_NONE;
466 }
467
468 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)
469 {
470         int ret = MS_MEDIA_ERR_NONE;
471         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
472         media_svc_content_info_s content_info = {0, };
473         media_svc_noti_item *noti_item = NULL;
474
475         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
476         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
477         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
478         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
479
480         /*Set media info*/
481         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
482         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
483
484         /* Initialize thumbnail information to remake thumbnail. */
485         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
486         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
487                 goto REFRESH_FINALIZE;
488
489         if (STRING_VALID(thumb_path)) {
490                 _media_svc_remove_file(thumb_path);
491
492                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
493                 if (ret != MS_MEDIA_ERR_NONE)
494                         goto REFRESH_FINALIZE;
495         }
496
497         /* Get notification info */
498         ret = _media_svc_get_noti_info(handle, path, &noti_item);
499         if (ret != MS_MEDIA_ERR_NONE)
500                 goto REFRESH_FINALIZE;
501
502         content_info.media_type = noti_item->media_type;
503         content_info.mime_type = g_strdup(noti_item->mime_type);
504
505         switch (content_info.media_type) {
506         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
507                 ret = _media_svc_extract_image_metadata(&content_info);
508                 break;
509         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
510         case MEDIA_SVC_MEDIA_TYPE_SOUND:
511         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
512                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
513                 break;
514         case MEDIA_SVC_MEDIA_TYPE_BOOK:
515                 ret = _media_svc_extract_book_metadata(&content_info);
516                 /* The 'TITLE' should always be filled in */
517                 if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
518                         g_free(content_info.media_meta.title);
519                         content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
520                 }
521                 break;
522         default:
523                 /* The 'TITLE' should always be filled in */
524                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
525                 break;
526         }
527
528         if (ret != MS_MEDIA_ERR_NONE)
529                 goto REFRESH_FINALIZE;
530
531         /* Extracting thumbnail */
532         if (content_info.thumbnail_path == NULL) {
533                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
534                         memset(thumb_path, 0, sizeof(thumb_path));
535
536                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
537                         if (ret == MS_MEDIA_ERR_NONE)
538                                 content_info.thumbnail_path = g_strdup(thumb_path);
539                 }
540         }
541
542         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
543
544         if (ret == MS_MEDIA_ERR_NONE) {
545                 if (is_direct) {
546                         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
547                         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
548                                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
549                                 if (ret != MS_MEDIA_ERR_NONE)
550                                         goto REFRESH_FINALIZE;
551
552                                 g_media_svc_cur_data_cnt = 0;
553                         }
554                 } else {
555                         /* Except scanner case */
556                         media_svc_debug("Update is successful. Sending noti for this");
557                         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
558                 }
559         } else {
560                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
561         }
562
563 REFRESH_FINALIZE:
564         _media_svc_destroy_content_info(&content_info);
565         _media_svc_destroy_noti_item(noti_item);
566
567         return ret;
568 }
569
570 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
571 {
572         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
573
574         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
575 }
576
577 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
578 {
579         media_svc_debug_fenter();
580
581         return _media_svc_check_db_upgrade(handle, user_version, uid);
582 }
583
584 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
585 {
586         int ret = MS_MEDIA_ERR_NONE;
587         char *root_path = NULL;
588         GPtrArray *path_list = NULL;
589         guint i = 0;
590
591         ret = ms_user_get_internal_root_path(uid, &root_path);
592         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
593
594         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
595         if (ret != MS_MEDIA_ERR_NONE)
596                 media_svc_error("Fail to send noti");
597
598         g_free(root_path);
599
600         path_list = g_ptr_array_new_with_free_func(g_free);
601         _media_svc_get_storage_path(handle, &path_list);
602
603         for (i = 0; i < path_list->len; i++) {
604                 root_path = g_ptr_array_index(path_list, i);
605
606                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
607                 if (ret != MS_MEDIA_ERR_NONE)
608                         media_svc_error("Fail to send noti");
609         }
610
611         g_ptr_array_free(path_list, TRUE);
612 }
613
614 static void __media_svc_foreach_update_media(gpointer data, gpointer user_data)
615 {
616         media_svc_content_info_s content_info = {0, };
617
618         if (_media_svc_extract_music_metadata_for_update(&content_info, (const char *)data) != MS_MEDIA_ERR_NONE) {
619                 media_svc_error("Fail to extract metadata");
620                 _media_svc_destroy_content_info(&content_info);
621                 return;
622         }
623
624         if (_media_svc_update_meta_with_data(&content_info) != MS_MEDIA_ERR_NONE)
625                 media_svc_error("Fail to append item[%s]", content_info.path);
626
627         _media_svc_destroy_content_info(&content_info);
628 }
629
630 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
631 {
632         int ret = MS_MEDIA_ERR_NONE;
633         char *sql = NULL;
634         GList *path_list = NULL;
635
636         sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", DB_TABLE_MEDIA);
637         ret = _media_svc_get_media(handle, sql, &path_list);
638         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get media list");
639
640         if (path_list) {
641                 g_list_foreach(path_list, __media_svc_foreach_update_media, NULL);
642                 g_list_free_full(path_list, g_free);
643
644                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
645                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
646         }
647
648         /* Noti for this */
649         __media_svc_noti_all_storage(handle, uid);
650
651         return ret;
652 }
653
654 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)
655 {
656         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
657 }
658
659 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
660 {
661         return _media_svc_get_pinyin_str(src_str, pinyin_str);
662 }
663
664 int media_svc_check_pinyin_support(bool *support)
665 {
666         *support = _media_svc_check_pinyin_support();
667
668         return MS_MEDIA_ERR_NONE;
669 }
670
671 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
672 {
673         int ret = MS_MEDIA_ERR_NONE;
674
675         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
676
677         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
678         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
679
680         return ret;
681 }
682
683 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
684 {
685         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
686 }
687
688 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
689 {
690         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
691 }
692
693 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
694 {
695         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
696 }
697
698 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)
699 {
700         int ret = MS_MEDIA_ERR_NONE;
701
702         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
703         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
704         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_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         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
708         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
709
710         /* Remove external storage that validity is 0 */
711         ret = _media_svc_delete_invalid_storage(handle, uid);
712         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
713
714         return ret;
715 }
716
717 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
718 {
719         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
720         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
721         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
722         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
723
724         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
725 }
726
727 int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
728 {
729         return _media_svc_set_folder_validity(true, storage_id, start_path, validity, is_recursive, uid);
730 }
731
732 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
733 {
734         return _media_svc_check_folder_by_path(handle, storage_id, folder_path);
735 }
736
737 int media_svc_append_query(const char *query, uid_t uid)
738 {
739         return _media_svc_append_query_list(query, uid);
740 }
741
742 int media_svc_send_query(uid_t uid)
743 {
744         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
745 }
746
747 int media_svc_get_media_type(const char *path, int *mediatype)
748 {
749         return _media_svc_get_media_type(path, mediatype);
750 }
751
752 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
753 {
754         int ret = MS_MEDIA_ERR_NONE;
755         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
756         char *sql = NULL;
757
758         // 1. Check media type
759         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
760                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
761
762         // 2. try to create thumbnail
763         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
764         if (ret != MS_MEDIA_ERR_NONE) {
765                 media_svc_error("Failed to create thumbnail [%d]", ret);
766                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
767                         return ret;
768         }
769
770         // 3. Update creation result to media db
771         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);
772
773         ret = _media_svc_sql_query(sql, uid);
774         SQLITE3_SAFE_FREE(sql);
775         if (ret != MS_MEDIA_ERR_NONE) {
776                 media_svc_error("Failed to update media db [%d]", ret);
777                 *thumbnail_path = g_strdup("");
778         } else {
779                 *thumbnail_path = g_strdup(thumb_path);
780         }
781
782         return ret;
783 }
784
785 int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, GList **result)
786 {
787         int ret = MS_MEDIA_ERR_NONE;
788         GList *item_list = NULL;
789         GList *iter = NULL;
790         char *query = NULL;
791         char *tmp_path = NULL;
792
793         media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
794         media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
795         media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");
796
797         query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;",
798                                                         DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK);
799
800         ret = _media_svc_get_media(handle, query, &item_list);
801         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
802
803         for (iter = item_list; iter; iter = g_list_next(iter)) {
804                 tmp_path = (char *)iter->data;
805
806                 if (_media_svc_is_keyword_included(tmp_path, keyword))
807                         *result = g_list_append(*result, g_strdup(tmp_path));
808         }
809
810         g_list_free_full(item_list, g_free);
811
812         return ret;
813 }