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