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