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