Merge "Adding initial structure for unittest" into tizen
[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-util.h>
25 #include "media-svc.h"
26 #include "media-svc-media.h"
27 #include "media-svc-debug.h"
28 #include "media-svc-util.h"
29 #include "media-svc-db-utils.h"
30 #include "media-svc-env.h"
31 #include "media-svc-media-folder.h"
32 #include "media-svc-album.h"
33 #include "media-svc-noti.h"
34 #include "media-svc-storage.h"
35
36 static __thread int g_media_svc_item_validity_data_cnt = 1;
37 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
38
39 static __thread int g_media_svc_insert_item_data_cnt = 1;
40 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
41
42 static __thread int g_media_svc_update_item_data_cnt = 1;
43 static __thread int g_media_svc_update_item_cur_data_cnt = 0;
44
45 static __thread int g_media_svc_insert_folder_data_cnt = 1;
46 static __thread int g_media_svc_insert_folder_cur_data_cnt = 0;
47
48 /* Flag for items to be published by notification */
49 static __thread int g_insert_with_noti = FALSE;
50
51 #define BATCH_REQUEST_MAX 300
52
53 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type)
54 {
55         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
56                 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
57                 && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
58                 media_svc_error("storage type is incorrect[%d]", storage_type);
59                 return FALSE;
60         }
61
62         return TRUE;
63 }
64
65 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
66 {
67         int ret = MS_MEDIA_ERR_NONE;
68         MediaDBHandle *db_handle = NULL;
69
70         media_svc_debug_fenter();
71
72         ret = media_db_connect(&db_handle, uid, need_write);
73         if (ret != MS_MEDIA_ERR_NONE)
74                 return ret;
75
76         *handle = db_handle;
77         return MS_MEDIA_ERR_NONE;
78 }
79
80 int media_svc_disconnect(MediaSvcHandle *handle)
81 {
82         MediaDBHandle *db_handle = (MediaDBHandle *)handle;
83
84         media_svc_debug_fenter();
85
86         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
87
88         return media_db_disconnect(db_handle);
89 }
90
91 int media_svc_cleanup_db(MediaSvcHandle *handle, uid_t uid)
92 {
93         MediaDBHandle *db_handle = (MediaDBHandle *)handle;
94
95         media_svc_debug_fenter();
96
97         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
98
99         return _media_svc_do_cleanup(db_handle, uid);
100 }
101
102 int media_svc_get_user_version(MediaSvcHandle *handle, int *user_version)
103 {
104         sqlite3 *db_handle = (sqlite3 *)handle;
105
106         return _media_svc_get_user_version(db_handle, user_version);
107 }
108
109 int media_svc_create_table(uid_t uid)
110 {
111         int ret = MS_MEDIA_ERR_NONE;
112         char *sql = NULL;
113
114         media_svc_debug_fenter();
115
116         ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
117         if (ret != MS_MEDIA_ERR_NONE) {
118                 media_svc_error("_media_svc_init_table_query fail.");
119                 goto ERROR;
120         }
121
122         /*create media table*/
123         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
124         if (ret != MS_MEDIA_ERR_NONE) {
125                 media_svc_error("_media_svc_make_table_query fail.");
126                 goto ERROR;
127         }
128
129         /*create folder table*/
130         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
131         if (ret != MS_MEDIA_ERR_NONE) {
132                 media_svc_error("_media_svc_make_table_query fail.");
133                 goto ERROR;
134         }
135
136         /*create playlist_map table*/
137         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
138         if (ret != MS_MEDIA_ERR_NONE) {
139                 media_svc_error("_media_svc_make_table_query fail.");
140                 goto ERROR;
141         }
142
143         /*create playlist table*/
144         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
145         if (ret != MS_MEDIA_ERR_NONE) {
146                 media_svc_error("_media_svc_make_table_query fail.");
147                 goto ERROR;
148         }
149
150         /* create album table*/
151         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
152         if (ret != MS_MEDIA_ERR_NONE) {
153                 media_svc_error("_media_svc_make_table_query fail.");
154                 goto ERROR;
155         }
156
157         /*create tag_map table*/
158         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
159         if (ret != MS_MEDIA_ERR_NONE) {
160                 media_svc_error("_media_svc_make_table_query fail.");
161                 goto ERROR;
162         }
163
164         /*create tag table*/
165         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
166         if (ret != MS_MEDIA_ERR_NONE) {
167                 media_svc_error("_media_svc_make_table_query fail.");
168                 goto ERROR;
169         }
170
171         /*create bookmark table*/
172         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
173         if (ret != MS_MEDIA_ERR_NONE) {
174                 media_svc_error("_media_svc_make_table_query fail.");
175                 goto ERROR;
176         }
177
178         /*create storage table from tizen 2.4 */
179         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
180         if (ret != MS_MEDIA_ERR_NONE) {
181                 media_svc_error("_media_svc_make_table_query fail.");
182                 goto ERROR;
183         }
184
185         /*create face table. from tizen 3.0*/
186         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
187         if (ret != MS_MEDIA_ERR_NONE) {
188                 media_svc_error("_media_svc_make_table_query fail.");
189                 goto ERROR;
190         }
191
192         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
193         if (ret != MS_MEDIA_ERR_NONE) {
194                 media_svc_error("_media_svc_make_table_query fail.");
195                 goto ERROR;
196         }
197
198         sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
199         ret = _media_svc_sql_query(sql, uid);
200         if (ret != MS_MEDIA_ERR_NONE) {
201                 media_svc_error("user_version update fail.");
202                 goto ERROR;
203         }
204
205         _media_svc_destroy_table_query();
206
207         media_svc_debug_fleave();
208
209         return MS_MEDIA_ERR_NONE;
210 ERROR:
211         _media_svc_destroy_table_query();
212
213         media_svc_debug_fleave();
214
215         return ret;
216 }
217
218 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
219 {
220         int ret = MS_MEDIA_ERR_NONE;
221         ms_user_storage_type_e type = -1;
222
223         ret = ms_user_get_storage_type(uid, path, &type);
224         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type failed : %d", ret);
225
226         *storage_type = type;
227
228         return ret;
229 }
230
231 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
232 {
233         int ret = MS_MEDIA_ERR_NONE;
234         sqlite3 *db_handle = (sqlite3 *)handle;
235
236         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
237         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
238
239         ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
240
241         return ret;
242 }
243
244 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
245 {
246         int ret = MS_MEDIA_ERR_NONE;
247         sqlite3 *db_handle = (sqlite3 *)handle;
248         int count = -1;
249
250         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
251         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
252         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
253
254         ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
255         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
256
257         if (count > 0) {
258                 media_svc_debug("item is exist in database");
259                 return MS_MEDIA_ERR_NONE;
260         } else {
261                 media_svc_debug("item is not exist in database");
262                 return MS_MEDIA_ERR_DB_NO_RECORD;
263         }
264
265         return MS_MEDIA_ERR_NONE;
266 }
267
268 int media_svc_insert_item_begin(int data_cnt, int with_noti, int from_pid)
269 {
270         media_svc_debug("Transaction data count : [%d]", data_cnt);
271
272         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
273
274         g_media_svc_insert_item_data_cnt = data_cnt;
275         g_media_svc_insert_item_cur_data_cnt = 0;
276
277         /* Prepare for making noti item list */
278         if (with_noti) {
279                 media_svc_debug("making noti list from pid[%d]", from_pid);
280                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
281                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
282
283                 _media_svc_set_noti_from_pid(from_pid);
284                 g_insert_with_noti = TRUE;
285         }
286
287         return MS_MEDIA_ERR_NONE;
288 }
289
290 int media_svc_insert_item_end(uid_t uid)
291 {
292         int ret = MS_MEDIA_ERR_NONE;
293
294         media_svc_debug_fenter();
295
296         if (g_media_svc_insert_item_cur_data_cnt > 0) {
297
298                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
299                 if (g_insert_with_noti) {
300                         media_svc_debug("sending noti list");
301                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
302                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
303                         g_insert_with_noti = FALSE;
304                         _media_svc_set_noti_from_pid(-1);
305                 }
306         }
307
308         g_media_svc_insert_item_data_cnt = 1;
309         g_media_svc_insert_item_cur_data_cnt = 0;
310
311         return ret;
312 }
313
314 int media_svc_insert_item_bulk(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
315 {
316         int ret = MS_MEDIA_ERR_NONE;
317         sqlite3 *db_handle = (sqlite3 *)handle;
318         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
319         media_svc_media_type_e media_type;
320
321         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
322         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
323         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
324         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
325
326         media_svc_content_info_s content_info;
327         memset(&content_info, 0, sizeof(media_svc_content_info_s));
328
329         /*Set media info*/
330         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
331         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
332         if (ret != MS_MEDIA_ERR_NONE)
333                 return ret;
334
335         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
336         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
337         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
338         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
339                 media_svc_debug("Do nothing[%d]", media_type);
340         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
341                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
342         else
343                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
344         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
345
346         /*Set or Get folder id*/
347         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
348         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
349
350         content_info.folder_uuid = g_strdup(folder_uuid);
351         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
352
353         if (g_media_svc_insert_item_data_cnt == 1) {
354
355                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, uid);
356                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
357
358                 if (g_insert_with_noti)
359                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
360
361         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
362
363                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, TRUE, uid);
364                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
365
366                 if (g_insert_with_noti)
367                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
368
369                 g_media_svc_insert_item_cur_data_cnt++;
370
371         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
372
373                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, TRUE, uid);
374                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
375
376                 if (g_insert_with_noti)
377                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
378
379                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
380                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
381
382                 if (g_insert_with_noti) {
383                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
384                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
385
386                         /* Recreate noti list */
387                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
388                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
389                 }
390
391                 g_media_svc_insert_item_cur_data_cnt = 0;
392
393         } else {
394                 media_svc_error("Error in media_svc_insert_item_bulk");
395                 _media_svc_destroy_content_info(&content_info);
396                 return MS_MEDIA_ERR_INTERNAL;
397         }
398
399         _media_svc_destroy_content_info(&content_info);
400
401         return MS_MEDIA_ERR_NONE;
402 }
403
404 int media_svc_insert_item_immediately(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
405 {
406         int ret = MS_MEDIA_ERR_NONE;
407         sqlite3 *db_handle = (sqlite3 *)handle;
408         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
409         media_svc_media_type_e media_type;
410
411         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
412         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
413         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
414         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
415
416         media_svc_content_info_s content_info;
417         memset(&content_info, 0, sizeof(media_svc_content_info_s));
418
419         /*Set media info*/
420         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
421         if (ret != MS_MEDIA_ERR_NONE)
422                 return ret;
423
424         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
425         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
426         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
427         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
428                 /*Do nothing.*/
429         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
430                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
431         } else {
432                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
433         }
434
435         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
436
437         /*Set or Get folder id*/
438         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
439         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
440
441         content_info.folder_uuid = g_strdup(folder_uuid);
442         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
443
444         /* Extracting thumbnail */
445         if (content_info.thumbnail_path == NULL) {
446                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
447                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
448
449                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
450                         if (ret == MS_MEDIA_ERR_NONE)
451                                 content_info.thumbnail_path = g_strdup(thumb_path);
452                 }
453         }
454
455         ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, uid);
456
457         if (ret == MS_MEDIA_ERR_NONE) {
458                 media_svc_debug("Insertion is successful. Sending noti for this");
459                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
460         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
461                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
462         }
463
464         _media_svc_destroy_content_info(&content_info);
465         return ret;
466 }
467
468 int media_svc_move_item(MediaSvcHandle *handle, const char *src_path, const char *dest_path, uid_t uid)
469 {
470         int ret = MS_MEDIA_ERR_NONE;
471         sqlite3 *db_handle = (sqlite3 *)handle;
472         char *file_name = NULL;
473         char *folder_path = NULL;
474         int modified_time = 0;
475         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
476         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
477         char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
478         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
479         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
480         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
481         int media_type = -1;
482
483         media_svc_debug_fenter();
484
485         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
486         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
487         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
488
489         /* Get storage_id */
490         ret = _media_svc_get_storage_uuid(db_handle, src_path, org_stg_id, uid);
491         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
492         ret = _media_svc_get_storage_uuid(db_handle, dest_path, dst_stg_id, uid);
493         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
494         /* Get storage_type */
495         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
496         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
497         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
498         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
499
500         /*check and update folder*/
501         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
502         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
503
504         /*get filename*/
505         file_name = g_path_get_basename(dest_path);
506
507         /*get modified_time*/
508         modified_time = _media_svc_get_file_time(dest_path);
509
510         ret = _media_svc_get_media_type_by_path(db_handle, org_stg_id, src_path, &media_type);
511         if (ret != MS_MEDIA_ERR_NONE) {
512                 media_svc_error("_media_svc_get_media_type_by_path failed");
513                 SAFE_FREE(file_name);
514                 return ret;
515         }
516
517         /*get old thumbnail_path and remove thumbnail */
518         ret = _media_svc_get_thumbnail_path_by_path(db_handle, src_path, old_thumb_path);
519         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
520                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
521                 SAFE_FREE(file_name);
522                 return ret;
523         }
524
525         if (STRING_VALID(old_thumb_path)) {
526                 ret = _media_svc_remove_file(old_thumb_path);
527                 if (ret != MS_MEDIA_ERR_NONE)
528                         media_svc_error("_media_svc_remove_file failed : %d", ret);
529         }
530
531         /*move item*/
532         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);
533         SAFE_FREE(file_name);
534         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
535
536         media_svc_debug("Move is successful. Sending noti for this");
537
538         /* Get notification info */
539         media_svc_noti_item *noti_item = NULL;
540         ret = _media_svc_get_noti_info(db_handle, dst_stg_id, dest_path, MS_MEDIA_ITEM_FILE, &noti_item);
541         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
542
543         /* Send notification for move */
544         _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
545         _media_svc_destroy_noti_item(noti_item);
546
547         /*update folder modified_time*/
548         folder_path = g_path_get_dirname(dest_path);
549         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
550         SAFE_FREE(folder_path);
551         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
552
553         return MS_MEDIA_ERR_NONE;
554 }
555
556 int media_svc_set_item_validity_begin(int data_cnt)
557 {
558         media_svc_debug("Transaction data count : [%d]", data_cnt);
559
560         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
561
562         g_media_svc_item_validity_data_cnt = data_cnt;
563         g_media_svc_item_validity_cur_data_cnt = 0;
564
565         return MS_MEDIA_ERR_NONE;
566 }
567
568 int media_svc_set_item_validity_end(uid_t uid)
569 {
570         int ret = MS_MEDIA_ERR_NONE;
571
572         media_svc_debug_fenter();
573
574         if (g_media_svc_item_validity_cur_data_cnt > 0)
575                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
576
577         g_media_svc_item_validity_data_cnt = 1;
578         g_media_svc_item_validity_cur_data_cnt = 0;
579
580         return ret;
581 }
582
583 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
584 {
585         int ret = MS_MEDIA_ERR_NONE;
586
587         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
588         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
589
590         media_svc_debug("path=[%s], validity=[%d]", path, validity);
591
592         if (g_media_svc_item_validity_data_cnt == 1) {
593
594                 return _media_svc_update_item_validity(storage_id, path, validity, FALSE, uid);
595
596         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
597
598                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
599                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
600
601                 g_media_svc_item_validity_cur_data_cnt++;
602
603         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
604
605                 ret = _media_svc_update_item_validity(storage_id, path, validity, TRUE, uid);
606                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
607
608                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
609                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
610
611                 g_media_svc_item_validity_cur_data_cnt = 0;
612
613         } else {
614
615                 media_svc_error("Error in media_svc_set_item_validity");
616                 return MS_MEDIA_ERR_INTERNAL;
617         }
618
619         return MS_MEDIA_ERR_NONE;
620 }
621
622 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
623 {
624         int ret = MS_MEDIA_ERR_NONE;
625         sqlite3 *db_handle = (sqlite3 *)handle;
626         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
627
628         media_svc_debug_fenter();
629
630         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
631         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
632         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
633
634         int media_type = -1;
635         ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
636         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
637
638         /*Get thumbnail path to delete*/
639         ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
640         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
641
642         if (g_media_svc_insert_item_data_cnt == 1) {
643
644                 /* Get notification info */
645                 media_svc_noti_item *noti_item = NULL;
646                 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
647                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
648
649                 /*Delete item*/
650                 ret = _media_svc_delete_item_by_path(storage_id, path, FALSE, uid);
651                 if (ret != MS_MEDIA_ERR_NONE) {
652                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
653                         _media_svc_destroy_noti_item(noti_item);
654
655                         return ret;
656                 }
657
658                 /* Send notification */
659                 media_svc_debug("Deletion is successful. Sending noti for this");
660                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
661                 _media_svc_destroy_noti_item(noti_item);
662         } else {
663                 ret = _media_svc_delete_item_by_path(storage_id, path, TRUE, uid);
664                 if (ret != MS_MEDIA_ERR_NONE) {
665                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
666                         return ret;
667                 }
668
669         }
670
671         /*Delete thumbnail*/
672         if (STRING_VALID(thumb_path)) {
673                 ret = _media_svc_remove_file(thumb_path);
674                 if (ret != MS_MEDIA_ERR_NONE)
675                         media_svc_error("fail to remove thumbnail file.");
676         }
677
678         return MS_MEDIA_ERR_NONE;
679 }
680
681 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
682 {
683         sqlite3 *db_handle = (sqlite3 *)handle;
684
685         media_svc_debug_fenter();
686
687         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
688         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
689         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
690
691         /*Delete from DB and remove thumbnail files*/
692         return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
693 }
694
695 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
696 {
697         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
698         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
699
700         return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
701 }
702
703 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
704 {
705         sqlite3 *db_handle = (sqlite3 *)handle;
706
707         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
708         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
709         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
710
711         if (recursive)
712                 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
713         else
714                 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
715 }
716
717 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
718 {
719         int ret = MS_MEDIA_ERR_NONE;
720         sqlite3 *db_handle = (sqlite3 *)handle;
721         media_svc_media_type_e media_type;
722         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
723
724         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
725         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
726         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
727         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
728
729         media_svc_content_info_s content_info;
730         memset(&content_info, 0, sizeof(media_svc_content_info_s));
731
732         /*Set media info*/
733         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
734         if (ret != MS_MEDIA_ERR_NONE)
735                 return ret;
736
737         /* Initialize thumbnail information to remake thumbnail. */
738         ret = _media_svc_get_thumbnail_path_by_path(db_handle, path, thumb_path);
739         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
740                 _media_svc_destroy_content_info(&content_info);
741                 return ret;
742         }
743
744         if (STRING_VALID(thumb_path)) {
745                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
746                         ret = _media_svc_remove_file(thumb_path);
747                         if (ret != MS_MEDIA_ERR_NONE)
748                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
749                 }
750
751                 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
752                 if (ret != MS_MEDIA_ERR_NONE) {
753                         _media_svc_destroy_content_info(&content_info);
754                         return ret;
755                 }
756         }
757
758         /* Get notification info */
759         media_svc_noti_item *noti_item = NULL;
760         ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
761         if (ret != MS_MEDIA_ERR_NONE) {
762                 _media_svc_destroy_content_info(&content_info);
763                 return ret;
764         }
765
766         media_type = noti_item->media_type;
767         content_info.media_type = media_type;
768
769         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
770         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
771         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
772         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
773                 media_svc_debug("Do nothing [%d]", media_type);
774         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
775                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
776         else
777                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
778
779         if (ret != MS_MEDIA_ERR_NONE) {
780                 _media_svc_destroy_noti_item(noti_item);
781                 _media_svc_destroy_content_info(&content_info);
782                 return ret;
783         }
784
785         /* Extracting thumbnail */
786         if (content_info.thumbnail_path == NULL) {
787                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
788                         memset(thumb_path, 0, sizeof(thumb_path));
789
790                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
791                         if (ret == MS_MEDIA_ERR_NONE)
792                                 content_info.thumbnail_path = g_strdup(thumb_path);
793                 }
794         }
795
796         ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
797
798         if (ret == MS_MEDIA_ERR_NONE) {
799                 media_svc_debug("Update is successful. Sending noti for this");
800                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
801         } else {
802                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
803         }
804
805         _media_svc_destroy_content_info(&content_info);
806         _media_svc_destroy_noti_item(noti_item);
807
808         return ret;
809 }
810
811 int media_svc_request_update_db(const char *db_query, uid_t uid)
812 {
813         media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
814
815         return _media_svc_sql_query(db_query, uid);
816 }
817
818 int media_svc_send_dir_update_noti(MediaSvcHandle *handle, const char *storage_id, const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
819 {
820         int ret = MS_MEDIA_ERR_NONE;
821         sqlite3 *db_handle = (sqlite3 *)handle;
822         char *uuid = NULL;
823
824         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
825         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
826
827         /* Get notification info */
828         media_svc_noti_item *noti_item = NULL;
829         ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
830         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
831
832         if (folder_id != NULL) {
833                 uuid = strndup(folder_id, strlen(folder_id));
834         } else {
835                 if (noti_item->media_uuid != NULL) {
836                         uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
837                 } else {
838                         _media_svc_destroy_noti_item(noti_item);
839                         media_svc_error("folder uuid is wrong");
840                         return MS_MEDIA_ERR_DB_INTERNAL;
841                 }
842         }
843
844         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
845         _media_svc_destroy_noti_item(noti_item);
846         SAFE_FREE(uuid);
847
848         return ret;
849 }
850
851 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
852 {
853         sqlite3 *db_handle = (sqlite3 *)handle;
854
855         media_svc_debug_fenter();
856
857         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
858
859         return _media_svc_check_db_upgrade(db_handle, user_version, uid);
860 }
861
862 int media_svc_update_item_begin(int data_cnt)
863 {
864         media_svc_debug("Transaction data count : [%d]", data_cnt);
865
866         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
867
868         g_media_svc_update_item_data_cnt = data_cnt;
869         g_media_svc_update_item_cur_data_cnt = 0;
870
871         return MS_MEDIA_ERR_NONE;
872 }
873
874 int media_svc_update_item_end(uid_t uid)
875 {
876         int ret = MS_MEDIA_ERR_NONE;
877
878         media_svc_debug_fenter();
879
880         if (g_media_svc_update_item_cur_data_cnt > 0)
881                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
882
883         g_media_svc_update_item_data_cnt = 1;
884         g_media_svc_update_item_cur_data_cnt = 0;
885
886         return ret;
887 }
888
889 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
890 {
891         int ret = MS_MEDIA_ERR_NONE;
892         sqlite3 *db_handle = (sqlite3 *)handle;
893
894         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
895
896         media_svc_media_type_e media_type;
897         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
898         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
899
900         media_svc_content_info_s content_info;
901         memset(&content_info, 0, sizeof(media_svc_content_info_s));
902
903         /*Set media info*/
904         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
905         if (ret != MS_MEDIA_ERR_NONE)
906                 return ret;
907
908         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
909                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
910         else {
911                 _media_svc_destroy_content_info(&content_info);
912                 return MS_MEDIA_ERR_NONE;
913         }
914
915         if (ret != MS_MEDIA_ERR_NONE) {
916                 _media_svc_destroy_content_info(&content_info);
917                 return ret;
918         }
919
920         if (g_media_svc_update_item_data_cnt == 1) {
921
922                 ret = _media_svc_update_meta_with_data(&content_info);
923                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
924
925         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
926
927                 ret = _media_svc_update_meta_with_data(&content_info);
928                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
929
930                 g_media_svc_update_item_cur_data_cnt++;
931
932         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
933
934                 ret = _media_svc_update_meta_with_data(&content_info);
935                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
936
937                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
938                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
939
940                 g_media_svc_update_item_cur_data_cnt = 0;
941
942         } else {
943                 media_svc_error("Error in media_svc_update_item_meta");
944                 _media_svc_destroy_content_info(&content_info);
945                 return MS_MEDIA_ERR_INTERNAL;
946         }
947
948         _media_svc_destroy_content_info(&content_info);
949
950         return ret;
951 }
952
953 int media_svc_publish_noti(media_item_type_e update_item, media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
954 {
955         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
956 }
957
958 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
959 {
960         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
961
962         return _media_svc_get_pinyin_str(src_str, pinyin_str);
963 }
964
965 int media_svc_check_pinyin_support(bool *support)
966 {
967         *support = _media_svc_check_pinyin_support();
968
969         return MS_MEDIA_ERR_NONE;
970 }
971
972 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
973 {
974         int ret = MS_MEDIA_ERR_NONE;
975         sqlite3 * db_handle = (sqlite3 *)handle;
976
977         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
978
979         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
980         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
981
982         ret = _media_svc_update_media_view(db_handle, uid);
983         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
984
985         return ret;
986 }
987
988 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
989 {
990         int ret = MS_MEDIA_ERR_NONE;
991         sqlite3 * db_handle = (sqlite3 *)handle;
992
993         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
994         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
995
996         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
997
998         return ret;
999 }
1000
1001 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int *count)
1002 {
1003         sqlite3 * db_handle = (sqlite3 *)handle;
1004
1005         media_svc_debug_fenter();
1006
1007         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1008         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1009
1010         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, count);
1011 }
1012
1013 int media_svc_generate_uuid(char **uuid)
1014 {
1015         char *gen_uuid = NULL;
1016         gen_uuid = _media_info_generate_uuid();
1017         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1018
1019         *uuid = strdup(gen_uuid);
1020
1021         return MS_MEDIA_ERR_NONE;
1022 }
1023
1024 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1025 {
1026         sqlite3 * db_handle = (sqlite3 *)handle;
1027
1028         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1029         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1030         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1031         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1032
1033         return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1034 }
1035
1036 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1037 {
1038         sqlite3 * db_handle = (sqlite3 *)handle;
1039
1040         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1041         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1042         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1043
1044         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1045 }
1046
1047 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, media_svc_storage_type_e storage_type, uid_t uid)
1048 {
1049         int ret = MS_MEDIA_ERR_NONE;
1050         sqlite3 *db_handle = (sqlite3 *)handle;
1051
1052         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1053         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1054         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1055         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1056
1057         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
1058         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1059
1060         ret = _media_svc_create_media_table_with_id(storage_id, uid);
1061         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1062
1063         ret = _media_svc_update_media_view(db_handle, uid);
1064         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1065
1066         /* Remove external storage that validity is 0 */
1067         ret = _media_svc_delete_invalid_storage(db_handle, storage_type, uid);
1068         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
1069
1070         return ret;
1071 }
1072
1073 int media_svc_insert_folder_begin(int data_cnt)
1074 {
1075         media_svc_debug("Transaction data count : [%d]", data_cnt);
1076
1077         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1078
1079         g_media_svc_insert_folder_data_cnt = data_cnt;
1080         g_media_svc_insert_folder_cur_data_cnt = 0;
1081
1082         return MS_MEDIA_ERR_NONE;
1083 }
1084
1085 int media_svc_insert_folder_end(uid_t uid)
1086 {
1087         int ret = MS_MEDIA_ERR_NONE;
1088
1089         media_svc_debug_fenter();
1090
1091         if (g_media_svc_insert_folder_cur_data_cnt > 0)
1092                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1093
1094         g_media_svc_insert_folder_data_cnt = 1;
1095         g_media_svc_insert_folder_cur_data_cnt = 0;
1096
1097         return ret;
1098 }
1099
1100 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1101 {
1102         int ret = MS_MEDIA_ERR_NONE;
1103         sqlite3 * db_handle = (sqlite3 *)handle;
1104         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1105
1106         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1107         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1108         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1109         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1110
1111         if (g_media_svc_insert_folder_data_cnt == 1) {
1112
1113                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1114
1115         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1116
1117                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1118                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1119
1120                 g_media_svc_insert_folder_cur_data_cnt++;
1121
1122         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1123
1124                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1125                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1126
1127                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1128                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1129
1130                 g_media_svc_insert_folder_cur_data_cnt = 0;
1131
1132         } else {
1133                 media_svc_error("Error in media_svc_set_insert_folder");
1134                 return MS_MEDIA_ERR_INTERNAL;
1135         }
1136
1137         return ret;
1138 }
1139
1140 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1141 {
1142         int ret = MS_MEDIA_ERR_NONE;
1143
1144         ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1145
1146         return ret;
1147 }
1148
1149 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1150 {
1151         int ret = MS_MEDIA_ERR_NONE;
1152         sqlite3 * db_handle = (sqlite3 *)handle;
1153
1154         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1155
1156         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1157
1158         return ret;
1159 }
1160
1161 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1162 {
1163         int ret = MS_MEDIA_ERR_NONE;
1164         sqlite3 * db_handle = (sqlite3 *)handle;
1165         int count = -1;
1166
1167         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1168         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1169         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1170
1171         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1172         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1173
1174         if (count > 0) {
1175                 media_svc_debug("item is exist in database");
1176                 return MS_MEDIA_ERR_NONE;
1177         } else {
1178                 media_svc_debug("item is not exist in database");
1179                 return MS_MEDIA_ERR_DB_NO_RECORD;
1180         }
1181
1182         return MS_MEDIA_ERR_NONE;
1183 }
1184
1185 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1186 {
1187         int ret = MS_MEDIA_ERR_NONE;
1188         sqlite3 * db_handle = (sqlite3 *)handle;
1189
1190         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1191         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1192
1193         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1194
1195         return ret;
1196 }
1197
1198 int media_svc_append_query(const char *query, uid_t uid)
1199 {
1200         int ret = MS_MEDIA_ERR_NONE;
1201
1202         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1203
1204         ret = _media_svc_append_query_list(query, uid);
1205
1206         return ret;
1207 }
1208
1209 int media_svc_send_query(uid_t uid)
1210 {
1211         int ret = MS_MEDIA_ERR_NONE;
1212
1213         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1214
1215         return ret;
1216 }
1217
1218 int media_svc_get_media_type(const char *path, int *mediatype)
1219 {
1220         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1221
1222         return _media_svc_get_media_type(path, mediatype);
1223 }
1224
1225 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
1226 {
1227         int ret = MS_MEDIA_ERR_NONE;
1228         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
1229         char *sql = NULL;
1230
1231         // 1. Check media type
1232         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1233                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1234
1235         // 2. try to create thumbnail
1236         ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid);
1237         if (ret != MS_MEDIA_ERR_NONE) {
1238                 media_svc_error("Failed to create thumbnail [%d]", ret);
1239                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
1240                         return ret;
1241         }
1242
1243         // 3. Update creation result to media db
1244         sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q';", storage_id, thumb_path, file_path);
1245
1246         ret = _media_svc_sql_query(sql, uid);
1247         SQLITE3_SAFE_FREE(sql);
1248         if (ret != MS_MEDIA_ERR_NONE) {
1249                 media_svc_error("Failed to update media db [%d]", ret);
1250                 *thumbnail_path = g_strdup("");
1251         } else {
1252                 *thumbnail_path = g_strdup(thumb_path);
1253         }
1254
1255         return ret;
1256 }