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