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