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