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