Fix to limit the length of query list
[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  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <string.h>
23 #include <errno.h>
24 #include "media-svc.h"
25 #include "media-svc-media.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-env.h"
30 #include "media-svc-media-folder.h"
31 #include "media-svc-album.h"
32 #include "media-svc-noti.h"
33 #include "media-svc-storage.h"
34
35 //static __thread int g_media_svc_data_cnt = 0;
36 static __thread int g_media_svc_cur_data_cnt = 0;
37
38 /* Flag for items to be published by notification */
39 static __thread bool g_insert_with_noti = false;
40
41 #define BATCH_ITEM_COUNT_MAX 30
42
43 int media_svc_get_user_version(sqlite3 *handle, int *user_version)
44 {
45         return _media_svc_get_user_version(handle, user_version);
46 }
47
48 int media_svc_create_table(uid_t uid)
49 {
50         int ret = MS_MEDIA_ERR_NONE;
51         char *sql = NULL;
52
53         media_svc_debug_fenter();
54
55         ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
56         if (ret != MS_MEDIA_ERR_NONE) {
57                 media_svc_error("_media_svc_init_table_query fail.");
58                 goto ERROR;
59         }
60
61         /*create media table*/
62         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
63         if (ret != MS_MEDIA_ERR_NONE) {
64                 media_svc_error("_media_svc_make_table_query fail.");
65                 goto ERROR;
66         }
67
68         /*create folder table*/
69         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
70         if (ret != MS_MEDIA_ERR_NONE) {
71                 media_svc_error("_media_svc_make_table_query fail.");
72                 goto ERROR;
73         }
74
75         /*create playlist_map table*/
76         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
77         if (ret != MS_MEDIA_ERR_NONE) {
78                 media_svc_error("_media_svc_make_table_query fail.");
79                 goto ERROR;
80         }
81
82         /*create playlist table*/
83         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
84         if (ret != MS_MEDIA_ERR_NONE) {
85                 media_svc_error("_media_svc_make_table_query fail.");
86                 goto ERROR;
87         }
88
89         /* create album table*/
90         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
91         if (ret != MS_MEDIA_ERR_NONE) {
92                 media_svc_error("_media_svc_make_table_query fail.");
93                 goto ERROR;
94         }
95
96         /*create tag_map table*/
97         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
98         if (ret != MS_MEDIA_ERR_NONE) {
99                 media_svc_error("_media_svc_make_table_query fail.");
100                 goto ERROR;
101         }
102
103         /*create tag table*/
104         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
105         if (ret != MS_MEDIA_ERR_NONE) {
106                 media_svc_error("_media_svc_make_table_query fail.");
107                 goto ERROR;
108         }
109
110         /*create bookmark table*/
111         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
112         if (ret != MS_MEDIA_ERR_NONE) {
113                 media_svc_error("_media_svc_make_table_query fail.");
114                 goto ERROR;
115         }
116
117         /*create storage table from tizen 2.4 */
118         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
119         if (ret != MS_MEDIA_ERR_NONE) {
120                 media_svc_error("_media_svc_make_table_query fail.");
121                 goto ERROR;
122         }
123
124         /*create face table. from tizen 3.0*/
125         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
126         if (ret != MS_MEDIA_ERR_NONE) {
127                 media_svc_error("_media_svc_make_table_query fail.");
128                 goto ERROR;
129         }
130
131         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
132         if (ret != MS_MEDIA_ERR_NONE) {
133                 media_svc_error("_media_svc_make_table_query fail.");
134                 goto ERROR;
135         }
136
137         sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_DB_VERSION);
138         ret = _media_svc_sql_query(sql, uid);
139         if (ret != MS_MEDIA_ERR_NONE) {
140                 media_svc_error("user_version update fail.");
141                 goto ERROR;
142         }
143
144         _media_svc_destroy_table_query();
145
146         media_svc_debug_fleave();
147
148         return MS_MEDIA_ERR_NONE;
149 ERROR:
150         _media_svc_destroy_table_query();
151
152         media_svc_debug_fleave();
153
154         return ret;
155 }
156
157 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
158 {
159         int ret = MS_MEDIA_ERR_NONE;
160         int count = -1;
161
162         ret = _media_svc_count_record_with_path(handle, path, &count);
163         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
164
165         if (count > 0) {
166                 media_svc_debug("item is exist in database");
167                 return MS_MEDIA_ERR_NONE;
168         } else {
169                 media_svc_debug("item is not exist in database");
170                 return MS_MEDIA_ERR_DB_NO_RECORD;
171         }
172
173         return MS_MEDIA_ERR_NONE;
174 }
175
176 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
177 {
178         return _media_svc_get_modified_time(handle, path, modified_time);
179 }
180
181 int media_svc_insert_item_begin(bool with_noti, int from_pid)
182 {
183         g_media_svc_cur_data_cnt = 0;
184
185         /* Prepare for making noti item list */
186         if (with_noti) {
187                 media_svc_debug("making noti list from pid[%d]", from_pid);
188                 _media_svc_initialize_noti_list();
189                 _media_svc_set_noti_from_pid(from_pid);
190                 g_insert_with_noti = true;
191         }
192
193         return MS_MEDIA_ERR_NONE;
194 }
195
196 int media_svc_insert_item_end(uid_t uid)
197 {
198         int ret = MS_MEDIA_ERR_NONE;
199
200         media_svc_debug_fenter();
201
202         if (g_media_svc_cur_data_cnt > 0) {
203                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
204                 if (g_insert_with_noti) {
205                         media_svc_debug("sending noti list");
206                         _media_svc_publish_noti_list();
207                         _media_svc_initialize_noti_list();
208                         g_insert_with_noti = false;
209                         _media_svc_set_noti_from_pid(-1);
210                 }
211         }
212
213         g_media_svc_cur_data_cnt = 0;
214
215         return ret;
216 }
217
218 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
219 {
220         int ret = MS_MEDIA_ERR_NONE;
221         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
222
223         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
224         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
225         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
226         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
227
228         media_svc_content_info_s content_info;
229         memset(&content_info, 0, sizeof(media_svc_content_info_s));
230
231         /*Set media info*/
232         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
233         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
234         if (ret != MS_MEDIA_ERR_NONE)
235                 return ret;
236
237         switch (content_info.media_type) {
238         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
239                 ret = _media_svc_extract_image_metadata(&content_info);
240                 break;
241         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
242         case MEDIA_SVC_MEDIA_TYPE_SOUND:
243         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
244                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
245                 break;
246         default:
247                 media_svc_debug("Do nothing[%d]", content_info.media_type);
248                 break;
249         }
250
251         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
252
253         /*Set or Get folder id*/
254         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
255         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
256
257         content_info.folder_uuid = g_strdup(folder_uuid);
258         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
259
260         ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
261         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
262
263         if (g_insert_with_noti)
264                 _media_svc_insert_item_to_noti_list(&content_info);
265
266         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
267         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
268                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
269                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
270
271                 if (g_insert_with_noti) {
272                         _media_svc_publish_noti_list();
273                         _media_svc_initialize_noti_list();
274                 }
275
276                 g_media_svc_cur_data_cnt = 0;
277         }
278
279         _media_svc_destroy_content_info(&content_info);
280
281         return MS_MEDIA_ERR_NONE;
282 }
283
284 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
285 {
286         int ret = MS_MEDIA_ERR_NONE;
287         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
288
289         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
290         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
291         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
292         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
293
294         media_svc_content_info_s content_info;
295         memset(&content_info, 0, sizeof(media_svc_content_info_s));
296
297         /*Set media info*/
298         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
299         if (ret != MS_MEDIA_ERR_NONE)
300                 return ret;
301
302         switch (content_info.media_type) {
303         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
304                 ret = _media_svc_extract_image_metadata(&content_info);
305                 break;
306         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
307         case MEDIA_SVC_MEDIA_TYPE_SOUND:
308         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
309                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
310                 break;
311         default:
312                 media_svc_debug("Do nothing[%d]", content_info.media_type);
313                 break;
314         }
315
316         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
317
318         /*Set or Get folder id*/
319         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
320         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
321
322         content_info.folder_uuid = g_strdup(folder_uuid);
323         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
324
325         /* Extracting thumbnail */
326         if (content_info.thumbnail_path == NULL) {
327                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
328                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
329
330                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
331                         if (ret == MS_MEDIA_ERR_NONE)
332                                 content_info.thumbnail_path = g_strdup(thumb_path);
333                 }
334         }
335
336         ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
337
338         if (ret == MS_MEDIA_ERR_NONE) {
339                 media_svc_debug("Insertion is successful. Sending noti for this");
340                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
341         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
342                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
343         }
344
345         _media_svc_destroy_content_info(&content_info);
346         return ret;
347 }
348
349 int media_svc_move_item(sqlite3 *handle,
350                                                                 const char *src_path,
351                                                                 const char *dest_path,
352                                                                 const char *media_id,
353                                                                 int media_type,
354                                                                 const char *mime_type,
355                                                                 uid_t uid)
356 {
357         int ret = MS_MEDIA_ERR_NONE;
358         char *file_name = NULL;
359         char *folder_path = NULL;
360         int modified_time = 0;
361         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
362         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
363         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
364         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
365         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
366
367         media_svc_debug_fenter();
368
369         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
370         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
371         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
372         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
373         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
374
375         /* Get storage_id */
376         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
377         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
378         /* Get storage_type */
379         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
380         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
381         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
382         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
383
384         /*check and update folder*/
385         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
386         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
387
388         /*get filename*/
389         file_name = g_path_get_basename(dest_path);
390
391         /*get modified_time*/
392         modified_time = _media_svc_get_file_time(dest_path);
393
394         /*get old thumbnail_path and remove thumbnail */
395         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
396         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
397                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
398                 SAFE_FREE(file_name);
399                 return ret;
400         }
401
402         _media_svc_remove_file(old_thumb_path);
403
404         /*move item*/
405         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
406         SAFE_FREE(file_name);
407         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
408
409         media_svc_debug("Move is successful. Sending noti for this");
410         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
411
412         /*update folder modified_time*/
413         folder_path = g_path_get_dirname(dest_path);
414         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
415         SAFE_FREE(folder_path);
416         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
417
418         return MS_MEDIA_ERR_NONE;
419 }
420
421 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
422 {
423         int ret = MS_MEDIA_ERR_NONE;
424
425         ret = _media_svc_update_item_validity(path, validity, true, uid);
426         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
427
428         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
429         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
430                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
431                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
432
433                 g_media_svc_cur_data_cnt = 0;
434         }
435
436         return ret;
437 }
438
439 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
440 {
441         int ret = MS_MEDIA_ERR_NONE;
442         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
443         media_svc_noti_item *noti_item = NULL;
444
445         media_svc_debug_fenter();
446
447         /*Get thumbnail path to delete*/
448         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
449         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
450
451         /* Get notification info */
452         ret = _media_svc_get_noti_info(handle, path, &noti_item);
453         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
454
455         /*Delete item*/
456         ret = _media_svc_delete_item_by_path(path, uid);
457         if (ret != MS_MEDIA_ERR_NONE) {
458                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
459                 _media_svc_destroy_noti_item(noti_item);
460
461                 return ret;
462         }
463
464         /* Send notification */
465         media_svc_debug("Deletion is successful. Sending noti for this");
466         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
467         _media_svc_destroy_noti_item(noti_item);
468
469         /*Delete thumbnail*/
470         _media_svc_remove_file(thumb_path);
471
472         return MS_MEDIA_ERR_NONE;
473 }
474
475 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
476 {
477         int ret = MS_MEDIA_ERR_NONE;
478         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
479
480         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
481         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
482         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
483         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
484
485         media_svc_content_info_s content_info;
486         memset(&content_info, 0, sizeof(media_svc_content_info_s));
487
488         /*Set media info*/
489         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
490         if (ret != MS_MEDIA_ERR_NONE)
491                 return ret;
492
493         /* Initialize thumbnail information to remake thumbnail. */
494         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
495         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
496                 _media_svc_destroy_content_info(&content_info);
497                 return ret;
498         }
499
500         if (STRING_VALID(thumb_path)) {
501                 _media_svc_remove_file(thumb_path);
502
503                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
504                 if (ret != MS_MEDIA_ERR_NONE) {
505                         _media_svc_destroy_content_info(&content_info);
506                         return ret;
507                 }
508         }
509
510         /* Get notification info */
511         media_svc_noti_item *noti_item = NULL;
512         ret = _media_svc_get_noti_info(handle, path, &noti_item);
513         if (ret != MS_MEDIA_ERR_NONE) {
514                 _media_svc_destroy_content_info(&content_info);
515                 return ret;
516         }
517
518         content_info.media_type = noti_item->media_type;
519         content_info.mime_type = g_strdup(noti_item->mime_type);
520
521         switch (content_info.media_type) {
522         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
523                 ret = _media_svc_extract_image_metadata(&content_info);
524                 break;
525         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
526         case MEDIA_SVC_MEDIA_TYPE_SOUND:
527         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
528                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
529                 break;
530         default:
531                 media_svc_debug("Do nothing[%d]", content_info.media_type);
532                 break;
533         }
534
535         if (ret != MS_MEDIA_ERR_NONE) {
536                 _media_svc_destroy_noti_item(noti_item);
537                 _media_svc_destroy_content_info(&content_info);
538                 return ret;
539         }
540
541         /* Extracting thumbnail */
542         if (content_info.thumbnail_path == NULL) {
543                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
544                         memset(thumb_path, 0, sizeof(thumb_path));
545
546                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
547                         if (ret == MS_MEDIA_ERR_NONE)
548                                 content_info.thumbnail_path = g_strdup(thumb_path);
549                 }
550         }
551
552         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
553
554         if (ret == MS_MEDIA_ERR_NONE) {
555                 media_svc_debug("Update is successful. Sending noti for this");
556                 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
557         } else {
558                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
559         }
560
561         _media_svc_destroy_content_info(&content_info);
562         _media_svc_destroy_noti_item(noti_item);
563
564         return ret;
565 }
566
567 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
568 {
569         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
570
571         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
572 }
573
574 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
575 {
576         media_svc_debug_fenter();
577
578         return _media_svc_check_db_upgrade(handle, user_version, uid);
579 }
580
581 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
582 {
583         int ret = MS_MEDIA_ERR_NONE;
584         char *root_path = NULL;
585         GPtrArray *path_list = NULL;
586         int i = 0;
587
588         ret = ms_user_get_internal_root_path(uid, &root_path);
589         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
590
591         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
592         if (ret != MS_MEDIA_ERR_NONE)
593                 media_svc_error("Fail to send noti");
594
595         SAFE_FREE(root_path);
596
597         path_list = g_ptr_array_new_with_free_func(g_free);
598         _media_svc_get_storage_path(handle, &path_list);
599
600         for (i = 0; i < path_list->len; i++) {
601                 root_path = g_ptr_array_index(path_list, i);
602
603                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
604                 if (ret != MS_MEDIA_ERR_NONE)
605                         media_svc_error("Fail to send noti");
606         }
607
608         g_ptr_array_free(path_list, TRUE);
609 }
610
611 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
612 {
613         int ret = MS_MEDIA_ERR_NONE;
614         int i = 0;
615         char *sql = NULL;
616         char *file_path = NULL;
617         media_svc_content_info_s content_info;
618         GPtrArray *path_list = NULL;
619
620         path_list = g_ptr_array_new_with_free_func(g_free);
621         media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
622
623         sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", MEDIA_SVC_DB_TABLE_MEDIA);
624         ret = _media_svc_get_media(handle, sql, &path_list);
625         if (ret != MS_MEDIA_ERR_NONE) {
626                 media_svc_error("Fail to get media list");
627                 g_ptr_array_free(path_list, TRUE);
628                 return ret;
629         }
630
631         for (i = 0; i < path_list->len; i++) {
632                 file_path = g_ptr_array_index(path_list, i);
633
634                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
635                 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_path);
636                 if (ret != MS_MEDIA_ERR_NONE) {
637                         media_svc_error("Fail to extract metadata");
638                         _media_svc_destroy_content_info(&content_info);
639                         continue;
640                 }
641
642                 ret = _media_svc_update_meta_with_data(&content_info);
643                 if (ret != MS_MEDIA_ERR_NONE)
644                         media_svc_error("Fail to append item[%s]", content_info.path);
645
646                 _media_svc_destroy_content_info(&content_info);
647         }
648
649         g_ptr_array_free(path_list, TRUE);
650
651         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
652         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
653
654         /* Noti for this */
655         __media_svc_noti_all_storage(handle, uid);
656
657         return ret;
658 }
659
660 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)
661 {
662         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
663 }
664
665 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
666 {
667         return _media_svc_get_pinyin_str(src_str, pinyin_str);
668 }
669
670 int media_svc_check_pinyin_support(bool *support)
671 {
672         *support = _media_svc_check_pinyin_support();
673
674         return MS_MEDIA_ERR_NONE;
675 }
676
677 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
678 {
679         int ret = MS_MEDIA_ERR_NONE;
680
681         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
682
683         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
684         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
685
686         return ret;
687 }
688
689 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
690 {
691         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
692 }
693
694 int media_svc_generate_uuid(char **uuid)
695 {
696         char *gen_uuid = NULL;
697         gen_uuid = _media_info_generate_uuid();
698         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
699
700         *uuid = strdup(gen_uuid);
701
702         return MS_MEDIA_ERR_NONE;
703 }
704
705 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
706 {
707         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
708 }
709
710 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
711 {
712         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
713 }
714
715 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
716 {
717         int ret = MS_MEDIA_ERR_NONE;
718
719         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
720         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
721         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
722         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
723
724         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
725         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
726
727         /* Remove external storage that validity is 0 */
728         ret = _media_svc_delete_invalid_storage(handle, uid);
729         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
730
731         return ret;
732 }
733
734 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
735 {
736         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
737         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
738         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
739         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
740
741         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
742 }
743
744 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
745 {
746         return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
747 }
748
749 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
750 {
751         int ret = MS_MEDIA_ERR_NONE;
752         int count = -1;
753
754         ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
755         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
756
757         if (count > 0) {
758                 media_svc_debug("item is exist in database");
759                 return MS_MEDIA_ERR_NONE;
760         } else {
761                 media_svc_debug("item is not exist in database");
762                 return MS_MEDIA_ERR_DB_NO_RECORD;
763         }
764
765         return MS_MEDIA_ERR_NONE;
766 }
767
768 int media_svc_append_query(const char *query, uid_t uid)
769 {
770         return _media_svc_append_query_list(query, uid);
771 }
772
773 int media_svc_send_query(uid_t uid)
774 {
775         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
776 }
777
778 int media_svc_get_media_type(const char *path, int *mediatype)
779 {
780         return _media_svc_get_media_type(path, mediatype);
781 }
782
783 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
784 {
785         int ret = MS_MEDIA_ERR_NONE;
786         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
787         char *sql = NULL;
788
789         // 1. Check media type
790         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
791                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
792
793         // 2. try to create thumbnail
794         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
795         if (ret != MS_MEDIA_ERR_NONE) {
796                 media_svc_error("Failed to create thumbnail [%d]", ret);
797                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
798                         return ret;
799         }
800
801         // 3. Update creation result to media db
802         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
803
804         ret = _media_svc_sql_query(sql, uid);
805         SQLITE3_SAFE_FREE(sql);
806         if (ret != MS_MEDIA_ERR_NONE) {
807                 media_svc_error("Failed to update media db [%d]", ret);
808                 *thumbnail_path = g_strdup("");
809         } else {
810                 *thumbnail_path = g_strdup(thumb_path);
811         }
812
813         return ret;
814 }