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