61eb839954bdffe001ae71b987a7f63464fe5418
[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         char *folder_path = NULL;
332         int modified_time = 0;
333         long long int folder_id = 0;
334         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
335         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
336
337         media_svc_debug_fenter();
338
339         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
340         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
341         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
342         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
343         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
344
345         /* Get storage_id */
346         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
347         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
348
349         /*check and update folder*/
350         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, &folder_id, uid);
351         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
352
353         /*get filename*/
354         file_name = g_path_get_basename(dest_path);
355
356         /*get modified_time*/
357         modified_time = _media_svc_get_file_time(dest_path);
358
359         /*get old thumbnail_path and remove thumbnail */
360         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
361         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
362                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
363                 g_free(file_name);
364                 return ret;
365         }
366
367         _media_svc_remove_file(old_thumb_path);
368
369         /*move item*/
370         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dest_path, file_name, modified_time, folder_id, uid);
371         g_free(file_name);
372         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
373
374         media_svc_debug("Move is successful. Sending noti for this");
375         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
376
377         /*update folder modified_time*/
378         folder_path = g_path_get_dirname(dest_path);
379         ret = _media_svc_update_folder_modified_time(folder_path, uid);
380         g_free(folder_path);
381         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
382
383         return MS_MEDIA_ERR_NONE;
384 }
385
386 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
387 {
388         int ret = MS_MEDIA_ERR_NONE;
389
390         ret = _media_svc_update_item_validity(path, validity, true, uid);
391         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
392
393         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
394         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
395                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
396                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
397
398                 g_media_svc_cur_data_cnt = 0;
399         }
400
401         return ret;
402 }
403
404 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
405 {
406         int ret = MS_MEDIA_ERR_NONE;
407         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
408         media_svc_noti_item *noti_item = NULL;
409
410         media_svc_debug_fenter();
411
412         /*Get thumbnail path to delete*/
413         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
414         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
415
416         /* Get notification info */
417         ret = _media_svc_get_noti_info(handle, path, &noti_item);
418         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
419
420         /*Delete item*/
421         ret = _media_svc_delete_item_by_path(path, uid);
422         if (ret != MS_MEDIA_ERR_NONE) {
423                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
424                 _media_svc_destroy_noti_item(noti_item);
425
426                 return ret;
427         }
428
429         /* Send notification */
430         media_svc_debug("Deletion is successful. Sending noti for this");
431         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
432         _media_svc_destroy_noti_item(noti_item);
433
434         /*Delete thumbnail*/
435         _media_svc_remove_file(thumb_path);
436
437         return MS_MEDIA_ERR_NONE;
438 }
439
440 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
441 {
442         int ret = MS_MEDIA_ERR_NONE;
443         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
444         media_svc_content_info_s content_info = {0, };
445         media_svc_noti_item *noti_item = NULL;
446
447         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
448         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
449
450         /*Set media info*/
451         ret = _media_svc_set_media_info(&content_info, NULL, path, true);
452         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
453
454         /* Initialize thumbnail information to remake thumbnail. */
455         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
456         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
457                 goto REFRESH_FINALIZE;
458
459         if (strlen(thumb_path) > 0) {
460                 _media_svc_remove_file(thumb_path);
461
462                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
463                 if (ret != MS_MEDIA_ERR_NONE)
464                         goto REFRESH_FINALIZE;
465         }
466
467         /* Get notification info */
468         ret = _media_svc_get_noti_info(handle, path, &noti_item);
469         if (ret != MS_MEDIA_ERR_NONE)
470                 goto REFRESH_FINALIZE;
471
472         content_info.media_type = noti_item->media_type;
473         content_info.mime_type = g_strdup(noti_item->mime_type);
474
475         switch (content_info.media_type) {
476         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
477                 ret = _media_svc_extract_image_metadata(&content_info);
478                 break;
479         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
480                 _media_svc_extract_video_metadata(&content_info);
481                 break;
482         case MEDIA_SVC_MEDIA_TYPE_SOUND:
483         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
484                 _media_svc_extract_audio_metadata(handle, is_direct, &content_info, uid);
485                 break;
486         case MEDIA_SVC_MEDIA_TYPE_BOOK:
487                 ret = _media_svc_extract_book_metadata(&content_info);
488                 break;
489         default:
490                 /* The 'TITLE' should always be filled in */
491                 content_info.media_meta.title = _media_svc_get_title_from_filename(content_info.file_name);
492                 break;
493         }
494
495         if (ret != MS_MEDIA_ERR_NONE)
496                 goto REFRESH_FINALIZE;
497
498         /* Extracting thumbnail */
499         if (content_info.thumbnail_path == NULL) {
500                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
501                         memset(thumb_path, 0, sizeof(thumb_path));
502
503                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
504                         if (ret == MS_MEDIA_ERR_NONE)
505                                 content_info.thumbnail_path = g_strdup(thumb_path);
506                 }
507         }
508
509         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
510
511         if (ret == MS_MEDIA_ERR_NONE) {
512                 if (is_direct) {
513                         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
514                         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
515                                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
516                                 if (ret != MS_MEDIA_ERR_NONE)
517                                         goto REFRESH_FINALIZE;
518
519                                 g_media_svc_cur_data_cnt = 0;
520                         }
521                 } else {
522                         /* Except scanner case */
523                         media_svc_debug("Update is successful. Sending noti for this");
524                         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
525                 }
526         } else {
527                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
528         }
529
530 REFRESH_FINALIZE:
531         _media_svc_destroy_content_info(&content_info);
532         _media_svc_destroy_noti_item(noti_item);
533
534         return ret;
535 }
536
537 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, int update_type, int pid)
538 {
539         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
540
541         return _media_svc_publish_dir_noti((media_item_update_type_e)update_type, dir_path, folder_id, pid);
542 }
543
544 int media_svc_publish_update_noti(const char *path, int media_type, const char *uuid, const char *mime_type)
545 {
546         return _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, path, media_type, uuid, mime_type);
547 }
548
549 int media_svc_set_storage_validity(const char *storage_id, int validity, uid_t uid)
550 {
551         return _media_svc_update_storage_validity(storage_id, validity, uid);
552 }
553
554 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
555 {
556         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
557 }
558
559 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
560 {
561         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
562 }
563
564 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
565 {
566         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
567 }
568
569 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
570 {
571         int ret = MS_MEDIA_ERR_NONE;
572
573         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
574         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
575         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
576
577         ret = _media_svc_append_storage(storage_id, storage_path, uid);
578         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
579
580         /* Remove external storage that validity is 0 */
581         ret = _media_svc_delete_invalid_storage(handle, uid);
582         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
583
584         return ret;
585 }
586
587 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
588 {
589         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
590         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
591         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
592
593         return _media_svc_append_by_folder_path(handle, storage_id, path, uid);
594 }
595
596 int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
597 {
598         return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
599 }
600
601 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
602 {
603         return _media_svc_check_folder_by_path(handle, folder_path);
604 }
605
606 int media_svc_append_query(const char *query, uid_t uid)
607 {
608         return _media_svc_append_query_list(query, uid);
609 }
610
611 int media_svc_send_query(uid_t uid)
612 {
613         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
614 }
615
616 int media_svc_get_media_type(const char *path, int *mediatype)
617 {
618         return _media_svc_get_media_type(path, mediatype);
619 }
620
621 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
622 {
623         int ret = MS_MEDIA_ERR_NONE;
624         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
625         char *sql = NULL;
626
627         // 1. Check media type
628         if (media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE && media_type != MEDIA_SVC_MEDIA_TYPE_VIDEO)
629                 return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
630
631         // 2. try to create thumbnail
632         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
633         if (ret != MS_MEDIA_ERR_NONE) {
634                 media_svc_error("Failed to create thumbnail [%d]", ret);
635                 if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
636                         return ret;
637         }
638
639         // 3. Update creation result to media db
640         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);
641
642         ret = _media_svc_sql_query(sql, uid);
643         SQLITE3_SAFE_FREE(sql);
644         if (ret != MS_MEDIA_ERR_NONE) {
645                 media_svc_error("Failed to update media db [%d]", ret);
646                 *thumbnail_path = g_strdup("");
647         } else {
648                 *thumbnail_path = g_strdup(thumb_path);
649         }
650
651         return ret;
652 }
653
654 static int __media_svc_get_ebook_search_type(void)
655 {
656         dictionary *dict = NULL;
657         static int _ebook_search_type = -1;
658
659         if (_ebook_search_type == -1) {
660                 dict = iniparser_load(CONTENT_INI_DEFAULT_PATH);
661                 if (!dict) {
662                         media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH);
663                         return MEDIA_SVC_SEARCH_TYPE_DIRECT;
664                 }
665
666                 _ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
667                 media_svc_debug("ebook_search_type [%d]", _ebook_search_type);
668
669                 iniparser_freedict(dict);
670         }
671
672         return _ebook_search_type;
673 }
674
675 int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
676 {
677         int ret = MS_MEDIA_ERR_NONE;
678         GList *item_list = NULL;
679         GList *iter = NULL;
680         const char *query = "SELECT media_path FROM media WHERE media_type=5 AND validity=1;";
681
682         media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
683         media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
684         media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");
685
686         ret = _media_svc_get_media(handle, query, &item_list);
687         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");
688
689         if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) {
690                 for (iter = item_list; iter; iter = g_list_next(iter))
691                         _media_svc_update_wordbook((char *)iter->data, uid);
692
693                 _media_svc_clean_wordbook(uid);
694
695                 if (!_media_svc_get_matched_list(keyword, uid, result))
696                         media_svc_error("_media_svc_get_matched_list failed");
697         } else {
698                 for (iter = item_list; iter; iter = g_list_next(iter)) {
699                         if (_media_svc_is_keyword_included((char *)iter->data, keyword))
700                                 *result = g_list_append(*result, g_strdup((gchar *)iter->data));
701                 }
702         }
703
704         g_list_free_full(item_list, g_free);
705
706         return ret;
707 }
708
709 int media_svc_check_db(sqlite3 *handle, uid_t uid)
710 {
711         int ret = MS_MEDIA_ERR_NONE;
712         bool exist = false;
713
714         ret = media_svc_check_table_exist(handle, &exist);
715         if (ret != MS_MEDIA_ERR_NONE)
716                 return ret;
717
718         if (!exist)
719                 ret = media_svc_create_table(uid);
720
721         return ret;
722 }