Fix title being inserted as an empty string
[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  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "media-svc-media.h"
21 #include "media-svc-debug.h"
22 #include "media-svc-util.h"
23 #include "media-svc-db-utils.h"
24 #include "media-svc-env.h"
25 #include "media-svc-media-folder.h"
26 #include "media-svc-album.h"
27 #include "media-svc-noti.h"
28 #include "media-svc-storage.h"
29
30 #include <iniparser.h>
31
32 #define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
33
34 //static __thread int g_media_svc_data_cnt = 0;
35 static __thread int g_media_svc_cur_data_cnt = 0;
36
37 /* Flag for items to be published by notification */
38 static __thread bool g_insert_with_noti = false;
39
40 #define BATCH_ITEM_COUNT_MAX 100
41
42 int media_svc_check_table_exist(sqlite3 *handle, bool *exist)
43 {
44         return _media_svc_check_table_exist(handle, exist);
45 }
46
47 int media_svc_create_table(uid_t uid)
48 {
49         int ret = MS_MEDIA_ERR_NONE;
50         media_svc_debug_fenter();
51
52         ret = _media_svc_init_table_query();
53         if (ret != MS_MEDIA_ERR_NONE) {
54                 media_svc_error("_media_svc_init_table_query fail.");
55                 goto ERROR;
56         }
57
58         /*create media table*/
59         ret = _media_svc_make_table_query(DB_TABLE_MEDIA, DB_LIST_MEDIA, uid);
60         if (ret != MS_MEDIA_ERR_NONE) {
61                 media_svc_error("_media_svc_make_table_query fail.");
62                 goto ERROR;
63         }
64
65         /*create folder table*/
66         ret = _media_svc_make_table_query(DB_TABLE_FOLDER, DB_LIST_FOLDER, uid);
67         if (ret != MS_MEDIA_ERR_NONE) {
68                 media_svc_error("_media_svc_make_table_query fail.");
69                 goto ERROR;
70         }
71
72         /*create playlist_map table*/
73         ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST_MAP, DB_LIST_PLAYLIST_MAP, uid);
74         if (ret != MS_MEDIA_ERR_NONE) {
75                 media_svc_error("_media_svc_make_table_query fail.");
76                 goto ERROR;
77         }
78
79         /*create playlist table*/
80         ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST, DB_LIST_PLAYLIST, uid);
81         if (ret != MS_MEDIA_ERR_NONE) {
82                 media_svc_error("_media_svc_make_table_query fail.");
83                 goto ERROR;
84         }
85
86         /* create album table*/
87         ret = _media_svc_make_table_query(DB_TABLE_ALBUM, DB_LIST_ALBUM, uid);
88         if (ret != MS_MEDIA_ERR_NONE) {
89                 media_svc_error("_media_svc_make_table_query fail.");
90                 goto ERROR;
91         }
92
93         /*create tag_map table*/
94         ret = _media_svc_make_table_query(DB_TABLE_TAG_MAP, DB_LIST_TAG_MAP, uid);
95         if (ret != MS_MEDIA_ERR_NONE) {
96                 media_svc_error("_media_svc_make_table_query fail.");
97                 goto ERROR;
98         }
99
100         /*create tag table*/
101         ret = _media_svc_make_table_query(DB_TABLE_TAG, DB_LIST_TAG, uid);
102         if (ret != MS_MEDIA_ERR_NONE) {
103                 media_svc_error("_media_svc_make_table_query fail.");
104                 goto ERROR;
105         }
106
107         /*create bookmark table*/
108         ret = _media_svc_make_table_query(DB_TABLE_BOOKMARK, DB_LIST_BOOKMARK, uid);
109         if (ret != MS_MEDIA_ERR_NONE) {
110                 media_svc_error("_media_svc_make_table_query fail.");
111                 goto ERROR;
112         }
113
114         /*create storage table from tizen 2.4 */
115         ret = _media_svc_make_table_query(DB_TABLE_STORAGE, DB_LIST_STORAGE, uid);
116         if (ret != MS_MEDIA_ERR_NONE) {
117                 media_svc_error("_media_svc_make_table_query fail.");
118                 goto ERROR;
119         }
120
121         /*create face table. from tizen 3.0*/
122         ret = _media_svc_make_table_query(DB_TABLE_FACE_SCAN_LIST, DB_LIST_FACE_SCAN_LIST, uid);
123         if (ret != MS_MEDIA_ERR_NONE) {
124                 media_svc_error("_media_svc_make_table_query fail.");
125                 goto ERROR;
126         }
127
128         ret = _media_svc_make_table_query(DB_TABLE_FACE, DB_LIST_FACE, uid);
129         if (ret != MS_MEDIA_ERR_NONE) {
130                 media_svc_error("_media_svc_make_table_query fail.");
131                 goto ERROR;
132         }
133 ERROR:
134         _media_svc_destroy_table_query();
135
136         media_svc_debug_fleave();
137
138         return ret;
139 }
140
141 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
142 {
143         return _media_svc_check_data_by_path(handle, path);
144 }
145
146 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
147 {
148         return _media_svc_get_modified_time(handle, path, modified_time);
149 }
150
151 int media_svc_insert_item_begin(bool with_noti, int from_pid)
152 {
153         g_media_svc_cur_data_cnt = 0;
154
155         /* Prepare for making noti item list */
156         if (with_noti) {
157                 media_svc_debug("making noti list from pid[%d]", from_pid);
158                 _media_svc_initialize_noti_list();
159                 _media_svc_set_noti_from_pid(from_pid);
160                 g_insert_with_noti = true;
161         }
162
163         return MS_MEDIA_ERR_NONE;
164 }
165
166 int media_svc_insert_item_end(uid_t uid)
167 {
168         int ret = MS_MEDIA_ERR_NONE;
169
170         media_svc_debug_fenter();
171
172         if (g_media_svc_cur_data_cnt > 0) {
173                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
174                 if (g_insert_with_noti) {
175                         media_svc_debug("sending noti list");
176                         _media_svc_publish_noti_list();
177                         g_insert_with_noti = false;
178                         _media_svc_set_noti_from_pid(-1);
179                 }
180         }
181
182         g_media_svc_cur_data_cnt = 0;
183
184         return ret;
185 }
186
187 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
188 {
189         int ret = MS_MEDIA_ERR_NONE;
190         long long int folder_id = 0;
191
192         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
193         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
194         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
195
196         media_svc_content_info_s content_info = { 0, };
197
198         /*Set media info*/
199         ret = _media_svc_set_media_info(&content_info, storage_id, path, false);
200         if (ret != MS_MEDIA_ERR_NONE)
201                 return ret;
202
203         switch (content_info.media_type) {
204         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
205                 ret = _media_svc_extract_image_metadata(&content_info);
206                 break;
207         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
208                 ret = _media_svc_extract_video_metadata(&content_info);
209                 break;
210         case MEDIA_SVC_MEDIA_TYPE_SOUND:
211         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
212                 _media_svc_extract_audio_metadata(handle, true, &content_info, uid);
213                 break;
214         case MEDIA_SVC_MEDIA_TYPE_BOOK:
215                 ret = _media_svc_extract_book_metadata(&content_info);
216                 break;
217         default:
218                 /* The 'TITLE' should always be filled in */
219                 content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name);
220                 break;
221         }
222
223         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
224
225         /*Set or Get folder id*/
226         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, &folder_id, uid);
227         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
228
229         content_info.folder_id = folder_id;
230         media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);
231
232         ret = _media_svc_insert_item_stack(&content_info);
233         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
234
235         if (g_insert_with_noti)
236                 _media_svc_insert_item_to_noti_list(&content_info);
237
238         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
239         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
240                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, 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_publish_noti_list();
245
246                 g_media_svc_cur_data_cnt = 0;
247         }
248
249         _media_svc_destroy_content_info(&content_info);
250
251         return MS_MEDIA_ERR_NONE;
252 }
253
254 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
255 {
256         int ret = MS_MEDIA_ERR_NONE;
257         long long int folder_id = 0;
258
259         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
260         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
261         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
262
263         media_svc_content_info_s content_info = { 0, };
264
265         /*Set media info*/
266         ret = _media_svc_set_media_info(&content_info, storage_id, path, false);
267         if (ret != MS_MEDIA_ERR_NONE)
268                 return ret;
269
270         switch (content_info.media_type) {
271         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
272                 ret = _media_svc_extract_image_metadata(&content_info);
273                 break;
274         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
275                 ret = _media_svc_extract_video_metadata(&content_info);
276                 break;
277         case MEDIA_SVC_MEDIA_TYPE_SOUND:
278         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
279                 _media_svc_extract_audio_metadata(handle, false, &content_info, uid);
280                 break;
281         case MEDIA_SVC_MEDIA_TYPE_BOOK:
282                 ret = _media_svc_extract_book_metadata(&content_info);
283                 break;
284         default:
285                 /* The 'TITLE' should always be filled in */
286                 content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name);
287                 break;
288         }
289
290         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
291
292         /*Set or Get folder id*/
293         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, &folder_id, uid);
294         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
295
296         content_info.folder_id = folder_id;
297         media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);
298
299         /* Extracting thumbnail */
300         if (content_info.thumbnail_path == NULL) {
301                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
302                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
303
304                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
305                         if (ret == MS_MEDIA_ERR_NONE)
306                                 content_info.thumbnail_path = g_strdup(thumb_path);
307                 }
308         }
309
310         ret = _media_svc_insert_item(&content_info, uid);
311         if (ret == MS_MEDIA_ERR_NONE) {
312                 media_svc_debug("Insertion is successful. Sending noti for this");
313                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
314         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
315                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
316         }
317
318         _media_svc_destroy_content_info(&content_info);
319         return ret;
320 }
321
322 int media_svc_move_item(sqlite3 *handle,
323                                                 const char *src_path,
324                                                 const char *dest_path,
325                                                 const char *media_id,
326                                                 int media_type,
327                                                 const char *mime_type,
328                                                 uid_t uid)
329 {
330         int ret = MS_MEDIA_ERR_NONE;
331         char *file_name = NULL;
332         char *folder_path = NULL;
333         int modified_time = 0;
334         long long int folder_id = 0;
335         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
336         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
337
338         media_svc_debug_fenter();
339
340         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
341         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
342         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
343         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
344         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
345
346         /* Get storage_id */
347         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
348         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
349
350         /*check and update folder*/
351         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, &folder_id, uid);
352         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
353
354         /*get filename*/
355         file_name = g_path_get_basename(dest_path);
356
357         /*get modified_time*/
358         modified_time = _media_svc_get_file_time(dest_path);
359
360         /*get old thumbnail_path and remove thumbnail */
361         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
362         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
363                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
364                 g_free(file_name);
365                 return ret;
366         }
367
368         _media_svc_remove_file(old_thumb_path);
369
370         /*move item*/
371         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dest_path, file_name, modified_time, folder_id, uid);
372         g_free(file_name);
373         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
374
375         media_svc_debug("Move is successful. Sending noti for this");
376         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
377
378         /*update folder modified_time*/
379         folder_path = g_path_get_dirname(dest_path);
380         ret = _media_svc_update_folder_modified_time(folder_path, uid);
381         g_free(folder_path);
382         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
383
384         return MS_MEDIA_ERR_NONE;
385 }
386
387 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
388 {
389         int ret = MS_MEDIA_ERR_NONE;
390
391         ret = _media_svc_update_item_validity(path, validity, true, uid);
392         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
393
394         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
395         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
396                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
397                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
398
399                 g_media_svc_cur_data_cnt = 0;
400         }
401
402         return ret;
403 }
404
405 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
406 {
407         int ret = MS_MEDIA_ERR_NONE;
408         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
409         media_svc_noti_item *noti_item = NULL;
410
411         media_svc_debug_fenter();
412
413         /*Get thumbnail path to delete*/
414         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
415         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
416
417         /* Get notification info */
418         ret = _media_svc_get_noti_info(handle, path, &noti_item);
419         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
420
421         /*Delete item*/
422         ret = _media_svc_delete_item_by_path(path, uid);
423         if (ret != MS_MEDIA_ERR_NONE) {
424                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
425                 _media_svc_destroy_noti_item(noti_item);
426
427                 return ret;
428         }
429
430         /* Send notification */
431         media_svc_debug("Deletion is successful. Sending noti for this");
432         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
433         _media_svc_destroy_noti_item(noti_item);
434
435         /*Delete thumbnail*/
436         _media_svc_remove_file(thumb_path);
437
438         return MS_MEDIA_ERR_NONE;
439 }
440
441 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
442 {
443         int ret = MS_MEDIA_ERR_NONE;
444         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
445         media_svc_content_info_s content_info = {0, };
446         media_svc_noti_item *noti_item = NULL;
447
448         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
449         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
450
451         /*Set media info*/
452         ret = _media_svc_set_media_info(&content_info, NULL, path, true);
453         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
454
455         /* Initialize thumbnail information to remake thumbnail. */
456         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
457         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
458                 goto REFRESH_FINALIZE;
459
460         if (strlen(thumb_path) > 0) {
461                 _media_svc_remove_file(thumb_path);
462
463                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
464                 if (ret != MS_MEDIA_ERR_NONE)
465                         goto REFRESH_FINALIZE;
466         }
467
468         /* Get notification info */
469         ret = _media_svc_get_noti_info(handle, path, &noti_item);
470         if (ret != MS_MEDIA_ERR_NONE)
471                 goto REFRESH_FINALIZE;
472
473         content_info.media_type = noti_item->media_type;
474         content_info.mime_type = g_strdup(noti_item->mime_type);
475
476         switch (content_info.media_type) {
477         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
478                 ret = _media_svc_extract_image_metadata(&content_info);
479                 break;
480         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
481                 ret = _media_svc_extract_video_metadata(&content_info);
482                 break;
483         case MEDIA_SVC_MEDIA_TYPE_SOUND:
484         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
485                 _media_svc_extract_audio_metadata(handle, is_direct, &content_info, uid);
486                 break;
487         case MEDIA_SVC_MEDIA_TYPE_BOOK:
488                 ret = _media_svc_extract_book_metadata(&content_info);
489                 break;
490         default:
491                 /* The 'TITLE' should always be filled in */
492                 content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name);
493                 break;
494         }
495
496         if (ret != MS_MEDIA_ERR_NONE)
497                 goto REFRESH_FINALIZE;
498
499         /* Extracting thumbnail */
500         if (content_info.thumbnail_path == NULL) {
501                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
502                         memset(thumb_path, 0, sizeof(thumb_path));
503
504                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
505                         if (ret == MS_MEDIA_ERR_NONE)
506                                 content_info.thumbnail_path = g_strdup(thumb_path);
507                 }
508         }
509
510         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
511
512         if (ret == MS_MEDIA_ERR_NONE) {
513                 if (is_direct) {
514                         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
515                         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
516                                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
517                                 if (ret != MS_MEDIA_ERR_NONE)
518                                         goto REFRESH_FINALIZE;
519
520                                 g_media_svc_cur_data_cnt = 0;
521                         }
522                 } else {
523                         /* Except scanner case */
524                         media_svc_debug("Update is successful. Sending noti for this");
525                         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
526                 }
527         } else {
528                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
529         }
530
531 REFRESH_FINALIZE:
532         _media_svc_destroy_content_info(&content_info);
533         _media_svc_destroy_noti_item(noti_item);
534
535         return ret;
536 }
537
538 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, int update_type, int pid)
539 {
540         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
541
542         return _media_svc_publish_dir_noti((media_item_update_type_e)update_type, dir_path, folder_id, pid);
543 }
544
545 int media_svc_publish_update_noti(const char *path, int media_type, const char *uuid, const char *mime_type)
546 {
547         return _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, path, media_type, uuid, mime_type);
548 }
549
550 int media_svc_set_storage_validity(const char *storage_id, int validity, uid_t uid)
551 {
552         return _media_svc_update_storage_validity(storage_id, validity, uid);
553 }
554
555 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
556 {
557         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
558 }
559
560 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
561 {
562         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
563 }
564
565 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
566 {
567         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
568 }
569
570 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
571 {
572         int ret = MS_MEDIA_ERR_NONE;
573
574         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
575         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
576         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
577
578         ret = _media_svc_append_storage(storage_id, storage_path, uid);
579         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
580
581         /* Remove external storage that validity is 0 */
582         ret = _media_svc_delete_invalid_storage(handle, uid);
583         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
584
585         return ret;
586 }
587
588 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
589 {
590         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
591         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
592         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
593
594         return _media_svc_append_by_folder_path(handle, storage_id, path, uid);
595 }
596
597 int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
598 {
599         return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
600 }
601
602 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
603 {
604         return _media_svc_check_folder_by_path(handle, folder_path);
605 }
606
607 int media_svc_append_query(const char *query, uid_t uid)
608 {
609         return _media_svc_append_query_list(query, uid);
610 }
611
612 int media_svc_send_query(uid_t uid)
613 {
614         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
615 }
616
617 int media_svc_get_media_type(const char *path, int *mediatype)
618 {
619         return _media_svc_get_media_type(path, mediatype);
620 }
621
622 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
623 {
624         int ret = MS_MEDIA_ERR_NONE;
625         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
626         char *sql = NULL;
627
628         // 1. Check media type
629         if (media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE && media_type != MEDIA_SVC_MEDIA_TYPE_VIDEO)
630                 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
631
632         // 2. try to create thumbnail
633         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
634         if (ret != MS_MEDIA_ERR_NONE) {
635                 media_svc_error("Failed to create thumbnail [%d]", ret);
636                 if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
637                         return ret;
638         }
639
640         // 3. Update creation result to media db
641         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);
642
643         ret = _media_svc_sql_query(sql, uid);
644         SQLITE3_SAFE_FREE(sql);
645         if (ret != MS_MEDIA_ERR_NONE) {
646                 media_svc_error("Failed to update media db [%d]", ret);
647                 *thumbnail_path = g_strdup("");
648         } else {
649                 *thumbnail_path = g_strdup(thumb_path);
650         }
651
652         return ret;
653 }
654
655 static int __media_svc_get_ebook_search_type(void)
656 {
657         dictionary *dict = NULL;
658         static int _ebook_search_type = -1;
659
660         if (_ebook_search_type == -1) {
661                 dict = iniparser_load(CONTENT_INI_DEFAULT_PATH);
662                 if (!dict) {
663                         media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH);
664                         return MEDIA_SVC_SEARCH_TYPE_DIRECT;
665                 }
666
667                 _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
668                 media_svc_debug("ebook_search_type [%d]", _ebook_search_type);
669
670                 iniparser_freedict(dict);
671         }
672
673         return _ebook_search_type;
674 }
675
676 int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
677 {
678         int ret = MS_MEDIA_ERR_NONE;
679         GList *item_list = NULL;
680         GList *iter = NULL;
681         char *query = NULL;
682
683         media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
684         media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
685         media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");
686
687         query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;",
688                                                         DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK);
689
690         ret = _media_svc_get_media(handle, query, &item_list);
691         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
692
693         if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) {
694                 for (iter = item_list; iter; iter = g_list_next(iter))
695                         _media_svc_update_wordbook((char *)iter->data, uid);
696
697                 _media_svc_clean_wordbook(uid);
698
699                 if (!_media_svc_get_matched_list(keyword, uid, result))
700                         media_svc_error("_media_svc_get_matched_list failed");
701         } else {
702                 for (iter = item_list; iter; iter = g_list_next(iter)) {
703                         if (_media_svc_is_keyword_included((char *)iter->data, keyword))
704                                 *result = g_list_append(*result, g_strdup((gchar *)iter->data));
705                 }
706         }
707
708         g_list_free_full(item_list, g_free);
709
710         return ret;
711 }
712
713 int media_svc_check_db(sqlite3 *handle, uid_t uid)
714 {
715         int ret = MS_MEDIA_ERR_NONE;
716         bool exist = false;
717
718         ret = media_svc_check_table_exist(handle, &exist);
719         if (ret != MS_MEDIA_ERR_NONE)
720                 return ret;
721
722         if (!exist)
723                 ret = media_svc_create_table(uid);
724
725         return ret;
726 }