Folder related functions cleanup
[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 100
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 ERROR:
144         _media_svc_destroy_table_query();
145
146         media_svc_debug_fleave();
147
148         return ret;
149 }
150
151 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
152 {
153         return _media_svc_check_data_by_path(handle, path);
154 }
155
156 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
157 {
158         return _media_svc_get_modified_time(handle, path, modified_time);
159 }
160
161 int media_svc_insert_item_begin(bool with_noti, int from_pid)
162 {
163         g_media_svc_cur_data_cnt = 0;
164
165         /* Prepare for making noti item list */
166         if (with_noti) {
167                 media_svc_debug("making noti list from pid[%d]", from_pid);
168                 _media_svc_initialize_noti_list();
169                 _media_svc_set_noti_from_pid(from_pid);
170                 g_insert_with_noti = true;
171         }
172
173         return MS_MEDIA_ERR_NONE;
174 }
175
176 int media_svc_insert_item_end(uid_t uid)
177 {
178         int ret = MS_MEDIA_ERR_NONE;
179
180         media_svc_debug_fenter();
181
182         if (g_media_svc_cur_data_cnt > 0) {
183                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
184                 if (g_insert_with_noti) {
185                         media_svc_debug("sending noti list");
186                         _media_svc_publish_noti_list();
187                         g_insert_with_noti = false;
188                         _media_svc_set_noti_from_pid(-1);
189                 }
190         }
191
192         g_media_svc_cur_data_cnt = 0;
193
194         return ret;
195 }
196
197 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)
198 {
199         int ret = MS_MEDIA_ERR_NONE;
200         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
201
202         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
203         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
204         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
205         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
206
207         media_svc_content_info_s content_info;
208         memset(&content_info, 0, sizeof(media_svc_content_info_s));
209
210         /*Set media info*/
211         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
212         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
213         if (ret != MS_MEDIA_ERR_NONE)
214                 return ret;
215
216         switch (content_info.media_type) {
217         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
218                 ret = _media_svc_extract_image_metadata(&content_info);
219                 break;
220         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
221         case MEDIA_SVC_MEDIA_TYPE_SOUND:
222         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
223                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
224                 break;
225         default:
226                 /* The 'TITLE' should always be filled in */
227                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
228                 break;
229         }
230
231         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
232
233         /*Set or Get folder id*/
234         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
235         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
236
237         content_info.folder_uuid = g_strdup(folder_uuid);
238         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
239
240         ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
241         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
242
243         if (g_insert_with_noti)
244                 _media_svc_insert_item_to_noti_list(&content_info);
245
246         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
247         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
248                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
249                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
250
251                 if (g_insert_with_noti)
252                         _media_svc_publish_noti_list();
253
254                 g_media_svc_cur_data_cnt = 0;
255         }
256
257         _media_svc_destroy_content_info(&content_info);
258
259         return MS_MEDIA_ERR_NONE;
260 }
261
262 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)
263 {
264         int ret = MS_MEDIA_ERR_NONE;
265         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
266
267         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
268         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
269         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
270         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
271
272         media_svc_content_info_s content_info;
273         memset(&content_info, 0, sizeof(media_svc_content_info_s));
274
275         /*Set media info*/
276         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
277         if (ret != MS_MEDIA_ERR_NONE)
278                 return ret;
279
280         switch (content_info.media_type) {
281         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
282                 ret = _media_svc_extract_image_metadata(&content_info);
283                 break;
284         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
285         case MEDIA_SVC_MEDIA_TYPE_SOUND:
286         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
287                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
288                 break;
289         default:
290                 /* The 'TITLE' should always be filled in */
291                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
292                 break;
293         }
294
295         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
296
297         /*Set or Get folder id*/
298         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
299         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
300
301         content_info.folder_uuid = g_strdup(folder_uuid);
302         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
303
304         /* Extracting thumbnail */
305         if (content_info.thumbnail_path == NULL) {
306                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
307                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
308
309                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
310                         if (ret == MS_MEDIA_ERR_NONE)
311                                 content_info.thumbnail_path = g_strdup(thumb_path);
312                 }
313         }
314
315         ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);
316
317         if (ret == MS_MEDIA_ERR_NONE) {
318                 media_svc_debug("Insertion is successful. Sending noti for this");
319                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
320         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
321                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
322         }
323
324         _media_svc_destroy_content_info(&content_info);
325         return ret;
326 }
327
328 int media_svc_move_item(sqlite3 *handle,
329                                                                 const char *src_path,
330                                                                 const char *dest_path,
331                                                                 const char *media_id,
332                                                                 int media_type,
333                                                                 const char *mime_type,
334                                                                 uid_t uid)
335 {
336         int ret = MS_MEDIA_ERR_NONE;
337         char *file_name = NULL;
338         char *folder_path = NULL;
339         int modified_time = 0;
340         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
341         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
342         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
343         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
344         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
345
346         media_svc_debug_fenter();
347
348         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
349         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
350         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
351         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
352         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
353
354         /* Get storage_id */
355         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
356         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
357         /* Get storage_type */
358         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
359         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
360         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
361         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
362
363         /*check and update folder*/
364         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
365         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
366
367         /*get filename*/
368         file_name = g_path_get_basename(dest_path);
369
370         /*get modified_time*/
371         modified_time = _media_svc_get_file_time(dest_path);
372
373         /*get old thumbnail_path and remove thumbnail */
374         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
375         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
376                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
377                 SAFE_FREE(file_name);
378                 return ret;
379         }
380
381         _media_svc_remove_file(old_thumb_path);
382
383         /*move item*/
384         ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
385         SAFE_FREE(file_name);
386         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
387
388         media_svc_debug("Move is successful. Sending noti for this");
389         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
390
391         /*update folder modified_time*/
392         folder_path = g_path_get_dirname(dest_path);
393         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
394         SAFE_FREE(folder_path);
395         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
396
397         return MS_MEDIA_ERR_NONE;
398 }
399
400 int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
401 {
402         int ret = MS_MEDIA_ERR_NONE;
403
404         ret = _media_svc_update_item_validity(path, validity, true, uid);
405         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
406
407         /* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
408         if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
409                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
410                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
411
412                 g_media_svc_cur_data_cnt = 0;
413         }
414
415         return ret;
416 }
417
418 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
419 {
420         int ret = MS_MEDIA_ERR_NONE;
421         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
422         media_svc_noti_item *noti_item = NULL;
423
424         media_svc_debug_fenter();
425
426         /*Get thumbnail path to delete*/
427         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
428         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
429
430         /* Get notification info */
431         ret = _media_svc_get_noti_info(handle, path, &noti_item);
432         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
433
434         /*Delete item*/
435         ret = _media_svc_delete_item_by_path(path, uid);
436         if (ret != MS_MEDIA_ERR_NONE) {
437                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
438                 _media_svc_destroy_noti_item(noti_item);
439
440                 return ret;
441         }
442
443         /* Send notification */
444         media_svc_debug("Deletion is successful. Sending noti for this");
445         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
446         _media_svc_destroy_noti_item(noti_item);
447
448         /*Delete thumbnail*/
449         _media_svc_remove_file(thumb_path);
450
451         return MS_MEDIA_ERR_NONE;
452 }
453
454 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)
455 {
456         int ret = MS_MEDIA_ERR_NONE;
457         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
458
459         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
460         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
461         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
462         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
463
464         media_svc_content_info_s content_info;
465         memset(&content_info, 0, sizeof(media_svc_content_info_s));
466
467         /*Set media info*/
468         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
469         if (ret != MS_MEDIA_ERR_NONE)
470                 return ret;
471
472         /* Initialize thumbnail information to remake thumbnail. */
473         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
474         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
475                 _media_svc_destroy_content_info(&content_info);
476                 return ret;
477         }
478
479         if (STRING_VALID(thumb_path)) {
480                 _media_svc_remove_file(thumb_path);
481
482                 ret = _media_svc_update_thumbnail_path(path, NULL, uid);
483                 if (ret != MS_MEDIA_ERR_NONE) {
484                         _media_svc_destroy_content_info(&content_info);
485                         return ret;
486                 }
487         }
488
489         /* Get notification info */
490         media_svc_noti_item *noti_item = NULL;
491         ret = _media_svc_get_noti_info(handle, path, &noti_item);
492         if (ret != MS_MEDIA_ERR_NONE) {
493                 _media_svc_destroy_content_info(&content_info);
494                 return ret;
495         }
496
497         content_info.media_type = noti_item->media_type;
498         content_info.mime_type = g_strdup(noti_item->mime_type);
499
500         switch (content_info.media_type) {
501         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
502                 ret = _media_svc_extract_image_metadata(&content_info);
503                 break;
504         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
505         case MEDIA_SVC_MEDIA_TYPE_SOUND:
506         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
507                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
508                 break;
509         default:
510                 /* The 'TITLE' should always be filled in */
511                 content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
512                 break;
513         }
514
515         if (ret != MS_MEDIA_ERR_NONE) {
516                 _media_svc_destroy_noti_item(noti_item);
517                 _media_svc_destroy_content_info(&content_info);
518                 return ret;
519         }
520
521         /* Extracting thumbnail */
522         if (content_info.thumbnail_path == NULL) {
523                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
524                         memset(thumb_path, 0, sizeof(thumb_path));
525
526                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
527                         if (ret == MS_MEDIA_ERR_NONE)
528                                 content_info.thumbnail_path = g_strdup(thumb_path);
529                 }
530         }
531
532         ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);
533
534         if (ret == MS_MEDIA_ERR_NONE) {
535                 media_svc_debug("Update is successful. Sending noti for this");
536                 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
537         } else {
538                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
539         }
540
541         _media_svc_destroy_content_info(&content_info);
542         _media_svc_destroy_noti_item(noti_item);
543
544         return ret;
545 }
546
547 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
548 {
549         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
550
551         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
552 }
553
554 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
555 {
556         media_svc_debug_fenter();
557
558         return _media_svc_check_db_upgrade(handle, user_version, uid);
559 }
560
561 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
562 {
563         int ret = MS_MEDIA_ERR_NONE;
564         char *root_path = NULL;
565         GPtrArray *path_list = NULL;
566         int i = 0;
567
568         ret = ms_user_get_internal_root_path(uid, &root_path);
569         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
570
571         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
572         if (ret != MS_MEDIA_ERR_NONE)
573                 media_svc_error("Fail to send noti");
574
575         SAFE_FREE(root_path);
576
577         path_list = g_ptr_array_new_with_free_func(g_free);
578         _media_svc_get_storage_path(handle, &path_list);
579
580         for (i = 0; i < path_list->len; i++) {
581                 root_path = g_ptr_array_index(path_list, i);
582
583                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
584                 if (ret != MS_MEDIA_ERR_NONE)
585                         media_svc_error("Fail to send noti");
586         }
587
588         g_ptr_array_free(path_list, TRUE);
589 }
590
591 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
592 {
593         int ret = MS_MEDIA_ERR_NONE;
594         int i = 0;
595         char *sql = NULL;
596         char *file_path = NULL;
597         media_svc_content_info_s content_info;
598         GPtrArray *path_list = NULL;
599
600         path_list = g_ptr_array_new_with_free_func(g_free);
601         media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
602
603         sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", MEDIA_SVC_DB_TABLE_MEDIA);
604         ret = _media_svc_get_media(handle, sql, &path_list);
605         if (ret != MS_MEDIA_ERR_NONE) {
606                 media_svc_error("Fail to get media list");
607                 g_ptr_array_free(path_list, TRUE);
608                 return ret;
609         }
610
611         for (i = 0; i < path_list->len; i++) {
612                 file_path = g_ptr_array_index(path_list, i);
613
614                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
615                 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_path);
616                 if (ret != MS_MEDIA_ERR_NONE) {
617                         media_svc_error("Fail to extract metadata");
618                         _media_svc_destroy_content_info(&content_info);
619                         continue;
620                 }
621
622                 ret = _media_svc_update_meta_with_data(&content_info);
623                 if (ret != MS_MEDIA_ERR_NONE)
624                         media_svc_error("Fail to append item[%s]", content_info.path);
625
626                 _media_svc_destroy_content_info(&content_info);
627         }
628
629         g_ptr_array_free(path_list, TRUE);
630
631         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
632         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
633
634         /* Noti for this */
635         __media_svc_noti_all_storage(handle, uid);
636
637         return ret;
638 }
639
640 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)
641 {
642         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
643 }
644
645 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
646 {
647         return _media_svc_get_pinyin_str(src_str, pinyin_str);
648 }
649
650 int media_svc_check_pinyin_support(bool *support)
651 {
652         *support = _media_svc_check_pinyin_support();
653
654         return MS_MEDIA_ERR_NONE;
655 }
656
657 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
658 {
659         int ret = MS_MEDIA_ERR_NONE;
660
661         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
662
663         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
664         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
665
666         return ret;
667 }
668
669 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
670 {
671         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
672 }
673
674 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
675 {
676         return _media_svc_check_storage(handle, storage_id, storage_path, validity);
677 }
678
679 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
680 {
681         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
682 }
683
684 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)
685 {
686         int ret = MS_MEDIA_ERR_NONE;
687
688         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
689         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
690         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
691         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
692
693         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
694         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
695
696         /* Remove external storage that validity is 0 */
697         ret = _media_svc_delete_invalid_storage(handle, uid);
698         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
699
700         return ret;
701 }
702
703 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
704 {
705         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
706         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
707         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
708         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
709
710         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
711 }
712
713 int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
714 {
715         return _media_svc_set_folder_validity(true, storage_id, start_path, validity, is_recursive, uid);
716 }
717
718 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
719 {
720         return _media_svc_check_folder_by_path(handle, storage_id, folder_path);
721 }
722
723 int media_svc_append_query(const char *query, uid_t uid)
724 {
725         return _media_svc_append_query_list(query, uid);
726 }
727
728 int media_svc_send_query(uid_t uid)
729 {
730         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
731 }
732
733 int media_svc_get_media_type(const char *path, int *mediatype)
734 {
735         return _media_svc_get_media_type(path, mediatype);
736 }
737
738 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
739 {
740         int ret = MS_MEDIA_ERR_NONE;
741         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
742         char *sql = NULL;
743
744         // 1. Check media type
745         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
746                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
747
748         // 2. try to create thumbnail
749         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
750         if (ret != MS_MEDIA_ERR_NONE) {
751                 media_svc_error("Failed to create thumbnail [%d]", ret);
752                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
753                         return ret;
754         }
755
756         // 3. Update creation result to media db
757         sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
758
759         ret = _media_svc_sql_query(sql, uid);
760         SQLITE3_SAFE_FREE(sql);
761         if (ret != MS_MEDIA_ERR_NONE) {
762                 media_svc_error("Failed to update media db [%d]", ret);
763                 *thumbnail_path = g_strdup("");
764         } else {
765                 *thumbnail_path = g_strdup(thumb_path);
766         }
767
768         return ret;
769 }