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