b84661d4ee7d7bf0f5c1f0a52c836b3f76fca56d
[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_move_item_data_cnt = 1;
40 static __thread int g_media_svc_move_item_cur_data_cnt = 0;
41
42 static __thread int g_media_svc_insert_item_data_cnt = 1;
43 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
44
45 static __thread int g_media_svc_update_item_data_cnt = 1;
46 static __thread int g_media_svc_update_item_cur_data_cnt = 0;
47
48 static __thread int g_media_svc_insert_folder_data_cnt = 1;
49 static __thread int g_media_svc_insert_folder_cur_data_cnt = 0;
50
51 /* Flag for items to be published by notification */
52 static __thread int g_insert_with_noti = FALSE;
53
54 #define DEFAULT_MEDIA_SVC_STORAGE_ID "media"
55
56 typedef struct {
57         int media_type;
58         char *path;
59 } media_svc_item_info_s;
60
61 static bool __media_svc_check_storage(media_svc_storage_type_e storage_type, bool check_all)
62 {
63         if (check_all == TRUE) {
64                 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
65                         && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
66                         && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)
67                         && (storage_type != MEDIA_SVC_STORAGE_CLOUD)) {
68                         media_svc_error("storage type is incorrect[%d]", storage_type);
69                         return FALSE;
70                 }
71         } else {
72                 if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL)
73                         && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)
74                         && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
75                         media_svc_error("storage type is incorrect[%d]", storage_type);
76                         return FALSE;
77                 }
78         }
79
80         return TRUE;
81 }
82
83 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
84 {
85         int ret = MS_MEDIA_ERR_NONE;
86         MediaDBHandle *db_handle = NULL;
87
88         media_svc_debug_fenter();
89
90         ret = media_db_connect(&db_handle, uid, need_write);
91         if (ret != MS_MEDIA_ERR_NONE)
92                 return ret;
93
94         *handle = db_handle;
95         return MS_MEDIA_ERR_NONE;
96 }
97
98 int media_svc_disconnect(MediaSvcHandle *handle)
99 {
100         MediaDBHandle *db_handle = (MediaDBHandle *)handle;
101
102         media_svc_debug_fenter();
103
104         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
105
106         int ret = MS_MEDIA_ERR_NONE;
107
108         ret = media_db_disconnect(db_handle);
109         return ret;
110 }
111
112 int media_svc_create_table(MediaSvcHandle *handle, uid_t uid)
113 {
114         int ret = MS_MEDIA_ERR_NONE;
115         sqlite3 *db_handle = (sqlite3 *)handle;
116
117         media_svc_debug_fenter();
118
119         ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
120         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
121
122         /*create media table*/
123         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
124         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
125
126         /*create folder table*/
127         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
128         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
129
130         /*create playlist_map table*/
131         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
132         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
133
134         /*create playlist table*/
135         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
136         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
137
138         /* create album table*/
139         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
140         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
141
142         /*create tag_map table*/
143         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
144         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
145
146         /*create tag table*/
147         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
148         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
149
150         /*create bookmark table*/
151         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
152         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
153
154         /*create storage table. from tizen 2.4*/
155         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
156         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
157
158 #if 0
159         /*init storage table*/
160         ret = _media_svc_init_storage(db_handle, uid);
161         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
162 #endif
163         /*create face table. from tizen 3.0*/
164         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
165         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
166
167         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
168         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
169
170         _media_svc_destroy_table_query();
171
172         media_svc_debug_fleave();
173
174         return MS_MEDIA_ERR_NONE;
175 }
176
177 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
178 {
179         int ret = MS_MEDIA_ERR_NONE;
180         media_svc_storage_type_e type;
181
182         ret = _media_svc_get_storage_type_by_path(path, &type, uid);
183         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
184
185         *storage_type = type;
186
187         return ret;
188 }
189
190 int media_svc_get_file_info(MediaSvcHandle *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
191 {
192         int ret = MS_MEDIA_ERR_NONE;
193         sqlite3 *db_handle = (sqlite3 *)handle;
194
195         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
196         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
197
198         ret = _media_svc_get_fileinfo_by_path(db_handle, storage_id, path, modified_time, size);
199
200         return ret;
201 }
202
203 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path)
204 {
205         int ret = MS_MEDIA_ERR_NONE;
206         sqlite3 *db_handle = (sqlite3 *)handle;
207         int count = -1;
208
209         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
210         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
211         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
212
213         ret = _media_svc_count_record_with_path(db_handle, storage_id, path, &count);
214         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
215
216         if (count > 0) {
217                 media_svc_debug("item is exist in database");
218                 return MS_MEDIA_ERR_NONE;
219         } else {
220                 media_svc_debug("item is not exist in database");
221                 return MS_MEDIA_ERR_DB_NO_RECORD;
222         }
223
224         return MS_MEDIA_ERR_NONE;
225 }
226
227 int media_svc_insert_item_begin(MediaSvcHandle *handle, int data_cnt, int with_noti, int from_pid)
228 {
229         sqlite3 *db_handle = (sqlite3 *)handle;
230
231         media_svc_debug("Transaction data count : [%d]", data_cnt);
232
233         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
234         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
235
236         g_media_svc_insert_item_data_cnt = data_cnt;
237         g_media_svc_insert_item_cur_data_cnt = 0;
238
239         /* Prepare for making noti item list */
240         if (with_noti) {
241                 media_svc_debug("making noti list from pid[%d]", from_pid);
242                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
243                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
244
245                 _media_svc_set_noti_from_pid(from_pid);
246                 g_insert_with_noti = TRUE;
247         }
248
249         return MS_MEDIA_ERR_NONE;
250 }
251
252 int media_svc_insert_item_end(MediaSvcHandle *handle, uid_t uid)
253 {
254         int ret = MS_MEDIA_ERR_NONE;
255         sqlite3 *db_handle = (sqlite3 *)handle;
256
257         media_svc_debug_fenter();
258
259         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
260
261         if (g_media_svc_insert_item_cur_data_cnt > 0) {
262
263                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
264                 if (g_insert_with_noti) {
265                         media_svc_debug("sending noti list");
266                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
267                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
268                         g_insert_with_noti = FALSE;
269                         _media_svc_set_noti_from_pid(-1);
270                 }
271         }
272
273         g_media_svc_insert_item_data_cnt = 1;
274         g_media_svc_insert_item_cur_data_cnt = 0;
275
276         return ret;
277 }
278
279 int media_svc_insert_item_bulk(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid)
280 {
281         int ret = MS_MEDIA_ERR_NONE;
282         sqlite3 *db_handle = (sqlite3 *)handle;
283         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
284         media_svc_media_type_e media_type;
285
286         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
287         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
288         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
289         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
290
291         media_svc_content_info_s content_info;
292         memset(&content_info, 0, sizeof(media_svc_content_info_s));
293
294         /*Set media info*/
295         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
296         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
297         if (ret != MS_MEDIA_ERR_NONE)
298                 return ret;
299
300         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
301                 /*Do nothing.*/
302         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
303                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
304         } else {
305                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
306         }
307         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
308
309         /*Set or Get folder id*/
310         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
311         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
312
313         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
314         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
315
316         if (g_media_svc_insert_item_data_cnt == 1) {
317
318                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
319                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
320
321                 if (g_insert_with_noti)
322                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
323
324         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
325
326                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
327                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
328
329                 if (g_insert_with_noti)
330                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
331
332                 g_media_svc_insert_item_cur_data_cnt++;
333
334         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
335
336                 ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
337                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
338
339                 if (g_insert_with_noti)
340                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
341
342                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
343                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
344
345                 if (g_insert_with_noti) {
346                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
347                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
348
349                         /* Recreate noti list */
350                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
351                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
352                 }
353
354                 g_media_svc_insert_item_cur_data_cnt = 0;
355
356         } else {
357                 media_svc_error("Error in media_svc_insert_item_bulk");
358                 _media_svc_destroy_content_info(&content_info);
359                 return MS_MEDIA_ERR_INTERNAL;
360         }
361
362         _media_svc_destroy_content_info(&content_info);
363
364         return MS_MEDIA_ERR_NONE;
365 }
366
367 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)
368 {
369         int ret = MS_MEDIA_ERR_NONE;
370         sqlite3 *db_handle = (sqlite3 *)handle;
371         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
372         media_svc_media_type_e media_type;
373         int ini_val = _media_svc_get_ini_value();
374
375         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
376         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
377         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
378         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
379
380         media_svc_content_info_s content_info;
381         memset(&content_info, 0, sizeof(media_svc_content_info_s));
382
383         /*Set media info*/
384         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
385         if (ret != MS_MEDIA_ERR_NONE) {
386                 return ret;
387         }
388
389         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
390                 /*Do nothing.*/
391         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
392                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
393         } else {
394                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
395         }
396
397         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
398
399         /*Set or Get folder id*/
400         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
401         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
402
403         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
404         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
405 #if 1
406         /* Extracting thumbnail */
407         if (ini_val == 1) {
408                 if (content_info.thumbnail_path == NULL) {
409                         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
410                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
411                                 int width = 0;
412                                 int height = 0;
413
414                                 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
415                                 if (ret == MS_MEDIA_ERR_NONE)
416                                         ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
417
418                                 if (content_info.media_meta.width <= 0)
419                                         content_info.media_meta.width = width;
420
421                                 if (content_info.media_meta.height <= 0)
422                                         content_info.media_meta.height = height;
423                         }
424                 }
425         }
426 #endif
427
428         ret = _media_svc_insert_item_with_data(db_handle, storage_id, &content_info, FALSE, FALSE, uid);
429
430         if (ret == MS_MEDIA_ERR_NONE) {
431                 media_svc_debug("Insertion is successful. Sending noti for this");
432                 _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);
433         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
434                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
435         }
436
437         _media_svc_destroy_content_info(&content_info);
438         return ret;
439 }
440
441 int media_svc_move_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e src_storage, const char *src_path,
442                         media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
443 {
444         int ret = MS_MEDIA_ERR_NONE;
445         sqlite3 *db_handle = (sqlite3 *)handle;
446         char *file_name = NULL;
447         char *folder_path = NULL;
448         int modified_time = 0;
449         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
450         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
451         char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
452         int media_type = -1;
453
454         media_svc_debug_fenter();
455
456         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
457         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
458         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
459         media_svc_retvm_if(__media_svc_check_storage(src_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid src_storage");
460         media_svc_retvm_if(__media_svc_check_storage(dest_storage, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid dest_storage");
461
462         /*check and update folder*/
463         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, dest_path, dest_storage, folder_uuid, uid);
464         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
465
466         /*get filename*/
467         file_name = g_path_get_basename(dest_path);
468
469         /*get modified_time*/
470         modified_time = _media_svc_get_file_time(dest_path);
471
472         /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
473         ret = _media_svc_get_media_type_by_path(db_handle, storage_id, src_path, &media_type);
474         if (ret != MS_MEDIA_ERR_NONE) {
475                 media_svc_error("_media_svc_get_media_type_by_path failed");
476                 SAFE_FREE(file_name);
477                 return ret;
478         }
479
480         if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
481                 /*get old thumbnail_path*/
482                 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, src_path, old_thumb_path);
483                 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
484                         media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
485                         SAFE_FREE(file_name);
486                         return ret;
487                 }
488
489                 /* If old thumb path is default or not */
490                 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
491                 if (STRING_VALID(default_thumbnail_path) && (strncmp(old_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0))
492                         strncpy(new_thumb_path, default_thumbnail_path, sizeof(new_thumb_path));
493                 else
494                         _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
495
496                 SAFE_FREE(default_thumbnail_path);
497         }
498
499         if (g_media_svc_move_item_data_cnt == 1) {
500
501                 /*update item*/
502                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
503                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
504                 } else {
505                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
506                 }
507                 SAFE_FREE(file_name);
508                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
509
510                 media_svc_debug("Move is successful. Sending noti for this");
511
512                 /* Get notification info */
513                 media_svc_noti_item *noti_item = NULL;
514                 ret = _media_svc_get_noti_info(db_handle, storage_id, dest_path, MS_MEDIA_ITEM_FILE, &noti_item);
515                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
516
517                 /* Send notification for move */
518                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
519                 _media_svc_destroy_noti_item(noti_item);
520
521                 /*update folder modified_time*/
522                 folder_path = g_path_get_dirname(dest_path);
523                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
524                 SAFE_FREE(folder_path);
525                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
526
527                 ret = _media_svc_update_folder_table(db_handle, storage_id, uid);
528                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
529
530         } else if (g_media_svc_move_item_cur_data_cnt < (g_media_svc_move_item_data_cnt - 1)) {
531
532                 /*update item*/
533                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
534                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
535                 } else {
536                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
537                 }
538                 SAFE_FREE(file_name);
539                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
540
541                 /*update folder modified_time*/
542                 folder_path = g_path_get_dirname(dest_path);
543                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
544                 SAFE_FREE(folder_path);
545                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
546
547                 g_media_svc_move_item_cur_data_cnt++;
548
549         } else if (g_media_svc_move_item_cur_data_cnt == (g_media_svc_move_item_data_cnt - 1)) {
550
551                 /*update item*/
552                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
553                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
554                 } else {
555                         ret = _media_svc_update_item_by_path(db_handle, storage_id, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
556                 }
557                 SAFE_FREE(file_name);
558                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
559
560                 /*update folder modified_time*/
561                 folder_path = g_path_get_dirname(dest_path);
562                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, TRUE, uid);
563                 SAFE_FREE(folder_path);
564                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
565
566                 /*update db*/
567                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
568                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
569
570                 g_media_svc_move_item_cur_data_cnt = 0;
571
572         } else {
573                 media_svc_error("Error in media_svc_move_item");
574                 return MS_MEDIA_ERR_INTERNAL;
575         }
576
577         /*rename thumbnail file*/
578 /*      if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
579         if ((strlen(old_thumb_path) > 0) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
580                 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
581                 if (ret != MS_MEDIA_ERR_NONE)
582                         media_svc_error("_media_svc_rename_file failed : %d", ret);
583         }
584 /*      } */
585
586         return MS_MEDIA_ERR_NONE;
587 }
588
589 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
590 {
591         sqlite3 *db_handle = (sqlite3 *)handle;
592
593         media_svc_debug("Transaction data count : [%d]", data_cnt);
594
595         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
596         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
597
598         g_media_svc_item_validity_data_cnt = data_cnt;
599         g_media_svc_item_validity_cur_data_cnt = 0;
600
601         return MS_MEDIA_ERR_NONE;
602 }
603
604 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
605 {
606         int ret = MS_MEDIA_ERR_NONE;
607         sqlite3 *db_handle = (sqlite3 *)handle;
608
609         media_svc_debug_fenter();
610
611         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
612
613         if (g_media_svc_item_validity_cur_data_cnt > 0) {
614
615                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
616         }
617
618         g_media_svc_item_validity_data_cnt = 1;
619         g_media_svc_item_validity_cur_data_cnt = 0;
620
621         return ret;
622 }
623
624 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *storage_id, const char *path, int validity, uid_t uid)
625 {
626         int ret = MS_MEDIA_ERR_NONE;
627         sqlite3 *db_handle = (sqlite3 *)handle;
628
629         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
630         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
631         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
632
633         media_svc_debug("path=[%s], validity=[%d]", path, validity);
634
635         if (g_media_svc_item_validity_data_cnt == 1) {
636
637                 return _media_svc_update_item_validity(db_handle, storage_id, path, validity, FALSE, uid);
638
639         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
640
641                 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
642                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
643
644                 g_media_svc_item_validity_cur_data_cnt++;
645
646         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
647
648                 ret = _media_svc_update_item_validity(db_handle, storage_id, path, validity, TRUE, uid);
649                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
650
651                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
652                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
653
654                 g_media_svc_item_validity_cur_data_cnt = 0;
655
656         } else {
657
658                 media_svc_error("Error in media_svc_set_item_validity");
659                 return MS_MEDIA_ERR_INTERNAL;
660         }
661
662         return MS_MEDIA_ERR_NONE;
663 }
664
665 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
666 {
667         int ret = MS_MEDIA_ERR_NONE;
668         sqlite3 *db_handle = (sqlite3 *)handle;
669         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
670
671         media_svc_debug_fenter();
672
673         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
674         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
675         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
676
677         int media_type = -1;
678         ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
679         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
680
681 #if 0
682         if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
683                 /*Get thumbnail path to delete*/
684                 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
685                 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
686         } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
687                 int count = 0;
688                 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
689                 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
690
691                 if (count == 1) {
692                         /*Get thumbnail path to delete*/
693                         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
694                         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
695                 }
696         }
697 #endif
698         /*Get thumbnail path to delete*/
699         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
700         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
701
702         if (g_media_svc_insert_item_data_cnt == 1) {
703
704                 /* Get notification info */
705                 media_svc_noti_item *noti_item = NULL;
706                 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
707                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
708
709                 /*Delete item*/
710                 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
711                 if (ret != MS_MEDIA_ERR_NONE) {
712                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
713                         _media_svc_destroy_noti_item(noti_item);
714
715                         return ret;
716                 }
717
718                 /* Send notification */
719                 media_svc_debug("Deletion is successful. Sending noti for this");
720                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
721                 _media_svc_destroy_noti_item(noti_item);
722         } else {
723                 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
724                 if (ret != MS_MEDIA_ERR_NONE) {
725                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
726                         return ret;
727                 }
728
729         }
730
731         /*Delete thumbnail*/
732         char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
733         if ((strlen(thumb_path) > 0) && ((STRING_VALID(default_thumbnail_path)) && (strncmp(thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0))) {
734 /*
735                 int thumb_count = 1;
736                 // Get count of media, which contains same thumbnail for music
737                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) ||(media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
738                         ret = _media_svc_get_thumbnail_count(db_handle, storage_id, thumb_path, &thumb_count);
739                         if (ret != MS_MEDIA_ERR_NONE) {
740                                 media_svc_error("Failed to get thumbnail count : %d", ret);
741                         }
742                 }
743
744                 if (thumb_count == 1) {
745 */
746                         ret = _media_svc_remove_file(thumb_path);
747                         if (ret != MS_MEDIA_ERR_NONE) {
748                                 media_svc_error("fail to remove thumbnail file.");
749                         }
750 //              }
751         }
752
753         SAFE_FREE(default_thumbnail_path);
754
755         return MS_MEDIA_ERR_NONE;
756 }
757
758 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
759 {
760         int ret = MS_MEDIA_ERR_NONE;
761         sqlite3 *db_handle = (sqlite3 *)handle;
762
763         media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
764
765         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
766         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
767         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
768
769         ret = _media_svc_truncate_table(db_handle, storage_id, storage_type, uid);
770         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
771
772         if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
773                 char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
774                 char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
775
776                 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
777
778                 /* remove thumbnails */
779                 ret = _media_svc_remove_all_files_in_dir(dirpath);
780                 SAFE_FREE(internal_thumb_path);
781                 SAFE_FREE(external_thumb_path);
782                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
783         }
784
785         return MS_MEDIA_ERR_NONE;
786 }
787
788 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
789 {
790         sqlite3 *db_handle = (sqlite3 *)handle;
791
792         media_svc_debug_fenter();
793
794         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
795         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
796         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
797
798         /*Delete from DB and remove thumbnail files*/
799         return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
800 }
801
802 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
803 {
804         sqlite3 *db_handle = (sqlite3 *)handle;
805
806         media_svc_debug_fenter();
807
808         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
809         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
810
811         /*Delete from DB and remove thumbnail files*/
812         return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
813 }
814
815 int media_svc_set_all_storage_items_validity(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
816 {
817         sqlite3 *db_handle = (sqlite3 *)handle;
818
819         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
820         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
821         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
822
823         return _media_svc_update_storage_item_validity(db_handle, storage_id, storage_type, validity, uid);
824 }
825
826 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
827 {
828         sqlite3 *db_handle = (sqlite3 *)handle;
829
830         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
831         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
832         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
833
834         if (recursive)
835                 return _media_svc_update_recursive_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
836         else
837                 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
838 }
839
840 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
841 {
842         int ret = MS_MEDIA_ERR_NONE;
843         sqlite3 *db_handle = (sqlite3 *)handle;
844         media_svc_media_type_e media_type;
845         int ini_val = _media_svc_get_ini_value();
846
847         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
848         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
849         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
850         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
851
852         media_svc_content_info_s content_info;
853         memset(&content_info, 0, sizeof(media_svc_content_info_s));
854
855         /*Set media info*/
856         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
857         if (ret != MS_MEDIA_ERR_NONE) {
858                 return ret;
859         }
860
861         /* Initialize thumbnail information to remake thumbnail. */
862         if (ini_val == 1) {
863                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
864                 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
865                 if (ret != MS_MEDIA_ERR_NONE) {
866                         _media_svc_destroy_content_info(&content_info);
867                         return ret;
868                 }
869
870                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS) && (STRING_VALID(MEDIA_SVC_THUMB_DEFAULT_PATH)) && (strncmp(thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, strlen(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
871                         ret = _media_svc_remove_file(thumb_path);
872                         if (ret != MS_MEDIA_ERR_NONE) {
873                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
874                         }
875                 }
876
877                 ret = _media_svc_update_thumbnail_path(db_handle, storage_id, path, NULL, uid);
878                 if (ret != MS_MEDIA_ERR_NONE) {
879                         _media_svc_destroy_content_info(&content_info);
880                         return ret;
881                 }
882         }
883
884         /* Get notification info */
885         media_svc_noti_item *noti_item = NULL;
886         ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
887         if (ret != MS_MEDIA_ERR_NONE) {
888                 _media_svc_destroy_content_info(&content_info);
889                 return ret;
890         }
891
892         media_type = noti_item->media_type;
893         content_info.media_type = media_type;
894
895         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
896                 /*Do nothing.*/
897         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
898                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
899         } else {
900                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
901         }
902
903         if (ret != MS_MEDIA_ERR_NONE) {
904                 _media_svc_destroy_noti_item(noti_item);
905                 _media_svc_destroy_content_info(&content_info);
906                 return ret;
907         }
908 #if 1
909         /* Extracting thumbnail */
910         if (ini_val == 1) {
911                 if (content_info.thumbnail_path == NULL) {
912                         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
913                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
914                                 int width = 0;
915                                 int height = 0;
916
917                                 ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
918                                 if (ret == MS_MEDIA_ERR_NONE) {
919                                         ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
920                                 }
921
922                                 if (content_info.media_meta.width <= 0)
923                                         content_info.media_meta.width = width;
924
925                                 if (content_info.media_meta.height <= 0)
926                                         content_info.media_meta.height = height;
927                         }
928                 }
929         }
930
931 #endif
932
933         ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
934
935         if (ret == MS_MEDIA_ERR_NONE) {
936                 media_svc_debug("Update is successful. Sending noti for this");
937                 _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);
938         } else {
939                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
940         }
941
942         _media_svc_destroy_content_info(&content_info);
943         _media_svc_destroy_noti_item(noti_item);
944
945         return ret;
946 }
947
948 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
949 {
950         sqlite3 *db_handle = (sqlite3 *)handle;
951         int ret = MS_MEDIA_ERR_NONE;
952
953         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
954         media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
955         media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
956
957         media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
958
959         ret = _media_svc_sql_begin_trans(db_handle, uid);
960         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
961
962         /* Update all folder record's modified date, which are changed above */
963         char *update_folder_modified_time_sql = NULL;
964         time_t date;
965         time(&date);
966
967         update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
968
969         ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
970         //ret = _media_svc_sql_query(db_handle, update_folder_modified_time_sql, uid);
971         sqlite3_free(update_folder_modified_time_sql);
972
973         if (ret != SQLITE_OK) {
974                 media_svc_error("failed to update folder modified time");
975                 _media_svc_sql_rollback_trans(db_handle, uid);
976
977                 return MS_MEDIA_ERR_DB_INTERNAL;
978         }
979
980         /* Update all items */
981         char *select_all_sql = NULL;
982         sqlite3_stmt *sql_stmt = NULL;
983         char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
984
985         snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
986
987         select_all_sql = sqlite3_mprintf("SELECT media_uuid, path, thumbnail_path, media_type from media where folder_uuid IN ( SELECT folder_uuid FROM folder where path='%q' or path like '%q');", dst_path, dst_child_path);
988
989         media_svc_debug("[SQL query] : %s", select_all_sql);
990
991         ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
992         if (ret != MS_MEDIA_ERR_NONE) {
993                 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
994                 _media_svc_sql_rollback_trans(db_handle, uid);
995                 return ret;
996         }
997
998         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
999                 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1000                 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1001                 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1002                 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1003                 /*int media_type; */
1004                 bool no_thumb = FALSE;
1005
1006                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1007                         strncpy(media_uuid,     (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1008                         media_uuid[sizeof(media_uuid) - 1] = '\0';
1009                 } else {
1010                         media_svc_error("media UUID is NULL");
1011                         return MS_MEDIA_ERR_DB_INTERNAL;
1012                 }
1013
1014                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1015                         strncpy(media_path,     (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1016                         media_path[sizeof(media_path) - 1] = '\0';
1017                 } else {
1018                         media_svc_error("media path is NULL");
1019                         return MS_MEDIA_ERR_DB_INTERNAL;
1020                 }
1021
1022                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1023                         strncpy(media_thumb_path,       (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1024                         media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1025                 } else {
1026                         media_svc_debug("media thumb path doesn't exist in DB");
1027                         no_thumb = TRUE;
1028                 }
1029
1030                 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1031
1032                 /* Update path, thumbnail path of this item */
1033                 char *replaced_path = NULL;
1034                 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1035                 if (replaced_path == NULL) {
1036                         media_svc_error("_media_svc_replace_path failed");
1037                         SQLITE3_FINALIZE(sql_stmt);
1038                         _media_svc_sql_rollback_trans(db_handle, uid);
1039                         return MS_MEDIA_ERR_INTERNAL;
1040                 }
1041
1042                 media_svc_debug("New media path : %s", replaced_path);
1043                 media_svc_storage_type_e storage_type;
1044
1045                 if (!no_thumb) {
1046                         /* If old thumb path is default or not */
1047                         char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1048                         if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1049                                 strncpy(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1050                         } else {
1051                                 ret = _media_svc_get_storage_type_by_path(replaced_path, &storage_type, uid);
1052                                 if (ret != MS_MEDIA_ERR_NONE) {
1053                                         media_svc_error("_media_svc_get_storage_type_by_path failed : %d", ret);
1054                                         SAFE_FREE(replaced_path);
1055                                         SAFE_FREE(default_thumbnail_path);
1056                                         _media_svc_sql_rollback_trans(db_handle, uid);
1057                                         return ret;
1058                                 }
1059
1060                                 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1061                                 if (ret != MS_MEDIA_ERR_NONE) {
1062                                         media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1063                                         SAFE_FREE(replaced_path);
1064                                         SAFE_FREE(default_thumbnail_path);
1065                                         SQLITE3_FINALIZE(sql_stmt);
1066                                         _media_svc_sql_rollback_trans(db_handle, uid);
1067                                         return ret;
1068                                 }
1069                         }
1070
1071                         SAFE_FREE(default_thumbnail_path);
1072
1073                         /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1074                 }
1075
1076                 char *update_item_sql = NULL;
1077
1078                 if (no_thumb) {
1079                         update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1080                 } else {
1081 #if 0
1082                         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1083                                 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1084                         } else {
1085                                 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1086                         }
1087 #else
1088                         update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_new_thumb_path, media_uuid);
1089 #endif
1090                 }
1091
1092                 ret = media_db_request_update_db_batch(update_item_sql, uid);
1093                 /*ret = _media_svc_sql_query(db_handle, update_item_sql, uid); */
1094                 sqlite3_free(update_item_sql);
1095                 SAFE_FREE(replaced_path);
1096
1097                 if (ret != SQLITE_OK) {
1098                         media_svc_error("failed to update item");
1099                         SQLITE3_FINALIZE(sql_stmt);
1100                         _media_svc_sql_rollback_trans(db_handle, uid);
1101
1102                         return MS_MEDIA_ERR_DB_INTERNAL;
1103                 }
1104
1105                 /* Rename thumbnail file of file system */
1106                 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1107                 if ((!no_thumb) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1108                         ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1109                         if (ret != MS_MEDIA_ERR_NONE) {
1110                                 media_svc_error("_media_svc_rename_file failed : %d", ret);
1111                         }
1112                 }
1113
1114                 SAFE_FREE(default_thumbnail_path);
1115         }
1116
1117         SQLITE3_FINALIZE(sql_stmt);
1118
1119         ret = _media_svc_sql_end_trans(db_handle, uid);
1120         if (ret != MS_MEDIA_ERR_NONE) {
1121                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1122                 _media_svc_sql_rollback_trans(db_handle, uid);
1123                 return ret;
1124         }
1125
1126         media_svc_debug("Folder update is successful. Sending noti for this");
1127         /* Get notification info */
1128         media_svc_noti_item *noti_item = NULL;
1129         ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1130         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1131
1132         _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1133         _media_svc_destroy_noti_item(noti_item);
1134
1135         return MS_MEDIA_ERR_NONE;
1136 }
1137
1138 int media_svc_request_update_db(const char *db_query, uid_t uid)
1139 {
1140         media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1141
1142         return _media_svc_sql_query(NULL, db_query, uid);
1143 }
1144
1145 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)
1146 {
1147         int ret = MS_MEDIA_ERR_NONE;
1148         sqlite3 *db_handle = (sqlite3 *)handle;
1149         char *uuid = NULL;
1150
1151         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1152         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1153
1154         /* Get notification info */
1155         media_svc_noti_item *noti_item = NULL;
1156         ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1157         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1158
1159         if (folder_id != NULL) {
1160                 uuid = folder_id;
1161         } else {
1162                 uuid = noti_item->media_uuid;
1163         }
1164
1165         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1166         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1167         _media_svc_destroy_noti_item(noti_item);
1168
1169         return ret;
1170 }
1171
1172 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1173 {
1174         sqlite3 *db_handle = (sqlite3 *)handle;
1175
1176         media_svc_debug_fenter();
1177
1178         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1179         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1180         media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1181         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1182
1183         return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1184 }
1185
1186 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, uid_t uid)
1187 {
1188         sqlite3 *db_handle = (sqlite3 *)handle;
1189
1190         media_svc_debug_fenter();
1191
1192         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1193
1194         return _media_svc_check_db_upgrade(db_handle, need_full_scan, uid);
1195 }
1196
1197 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1198 {
1199         sqlite3 *db_handle = (sqlite3 *)handle;
1200
1201         media_svc_debug_fenter();
1202
1203         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1204
1205         return _media_db_check_corrupt(db_handle);
1206 }
1207
1208 int media_svc_get_folder_list(MediaSvcHandle *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
1209 {
1210         sqlite3 *db_handle = (sqlite3 *)handle;
1211
1212         media_svc_debug_fenter();
1213
1214         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1215         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1216
1217         return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1218 }
1219
1220 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1221 {
1222         int ret = MS_MEDIA_ERR_NONE;
1223         sqlite3 *db_handle = (sqlite3 *)handle;
1224         time_t sto_time = 0;
1225         int cur_time = _media_svc_get_file_time(folder_path);
1226         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1227
1228         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1229
1230         ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1231         if (ret == MS_MEDIA_ERR_NONE) {
1232                 if (sto_time != cur_time) {
1233                         ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1234                 }
1235         }
1236
1237         return ret;
1238 }
1239
1240 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1241 {
1242         sqlite3 *db_handle = (sqlite3 *)handle;
1243
1244         media_svc_debug("Transaction data count : [%d]", data_cnt);
1245
1246         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1247         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1248
1249         g_media_svc_update_item_data_cnt = data_cnt;
1250         g_media_svc_update_item_cur_data_cnt = 0;
1251
1252         return MS_MEDIA_ERR_NONE;
1253 }
1254
1255 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1256 {
1257         int ret = MS_MEDIA_ERR_NONE;
1258         sqlite3 *db_handle = (sqlite3 *)handle;
1259
1260         media_svc_debug_fenter();
1261
1262         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1263
1264         if (g_media_svc_update_item_cur_data_cnt > 0) {
1265
1266                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1267         }
1268
1269         g_media_svc_update_item_data_cnt = 1;
1270         g_media_svc_update_item_cur_data_cnt = 0;
1271
1272         return ret;
1273 }
1274
1275 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1276 {
1277         int ret = MS_MEDIA_ERR_NONE;
1278         sqlite3 *db_handle = (sqlite3 *)handle;
1279
1280         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1281
1282         media_svc_media_type_e media_type;
1283
1284         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1285         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1286         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1287
1288         media_svc_content_info_s content_info;
1289         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1290
1291         /*Set media info*/
1292         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1293         if (ret != MS_MEDIA_ERR_NONE) {
1294                 return ret;
1295         }
1296
1297         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1298                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1299         } else {
1300                 return MS_MEDIA_ERR_NONE;
1301         }
1302
1303         if (ret != MS_MEDIA_ERR_NONE) {
1304                 _media_svc_destroy_content_info(&content_info);
1305                 return ret;
1306         }
1307
1308         if (g_media_svc_update_item_data_cnt == 1) {
1309
1310                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1311                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1312
1313         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1314
1315                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1316                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1317
1318                 g_media_svc_update_item_cur_data_cnt++;
1319
1320         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1321
1322                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1323                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1324
1325                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1326                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1327
1328                 g_media_svc_update_item_cur_data_cnt = 0;
1329
1330         } else {
1331                 media_svc_error("Error in media_svc_update_item_meta");
1332                 _media_svc_destroy_content_info(&content_info);
1333                 return MS_MEDIA_ERR_INTERNAL;
1334         }
1335
1336         _media_svc_destroy_content_info(&content_info);
1337
1338         return ret;
1339 }
1340
1341 int media_svc_publish_noti(MediaSvcHandle *handle, 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)
1342 {
1343         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1344 }
1345
1346 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1347 {
1348         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1349
1350         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1351 }
1352
1353 int media_svc_check_pinyin_support(bool *support)
1354 {
1355         *support = _media_svc_check_pinyin_support();
1356
1357         return MS_MEDIA_ERR_NONE;
1358 }
1359
1360 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1361 {
1362         int ret = MS_MEDIA_ERR_NONE;
1363         sqlite3 * db_handle = (sqlite3 *)handle;
1364
1365         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1366
1367         ret = _media_svc_update_storage_validity(db_handle, storage_id, validity, uid);
1368         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1369
1370         ret = _media_svc_update_media_view(db_handle, uid);
1371         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1372
1373         return ret;
1374 }
1375
1376 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1377 {
1378         int ret = MS_MEDIA_ERR_NONE;
1379         sqlite3 * db_handle = (sqlite3 *)handle;
1380
1381         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1382         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1383
1384         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1385
1386         return ret;
1387 }
1388
1389 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1390 {
1391         int ret = MS_MEDIA_ERR_NONE;
1392         sqlite3 * db_handle = (sqlite3 *)handle;
1393
1394         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1395         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1396
1397         ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1398
1399         return ret;
1400 }
1401
1402 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1403 {
1404         int ret = MS_MEDIA_ERR_NONE;
1405         sqlite3 * db_handle = (sqlite3 *)handle;
1406
1407         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1408         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1409
1410         ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1411
1412         return ret;
1413 }
1414
1415 int media_svc_set_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1416 {
1417         int ret = MS_MEDIA_ERR_NONE;
1418         sqlite3 * db_handle = (sqlite3 *)handle;
1419
1420         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1421
1422         ret = _media_svc_set_storage_scan_status(db_handle, storage_uuid, storage_status, uid);
1423
1424         return ret;
1425 }
1426
1427 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1428 {
1429         sqlite3 * db_handle = (sqlite3 *)handle;
1430
1431         media_svc_debug_fenter();
1432
1433         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1434         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1435
1436         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1437 }
1438
1439 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1440 {
1441         int ret = MS_MEDIA_ERR_NONE;
1442
1443         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1444         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1445
1446         if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1447                 new_content_info->size = content_info->size;
1448                 new_content_info->modified_time = content_info->modified_time;
1449                 new_content_info->is_drm = content_info->is_drm;
1450                 new_content_info->media_type = content_info->media_type;
1451
1452                 if (STRING_VALID(content_info->mime_type)) {
1453                         ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1454                         if (ret != MS_MEDIA_ERR_NONE)
1455                                 media_svc_error("strcpy mime_type failed");
1456                 }
1457
1458                 if (STRING_VALID(content_info->thumbnail_path)) {
1459                         ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1460                         if (ret != MS_MEDIA_ERR_NONE)
1461                                 media_svc_error("strcpy thumbnail_path failed");
1462                 }
1463
1464                 new_content_info->media_meta.duration = content_info->media_meta.duration;
1465                 new_content_info->media_meta.width = content_info->media_meta.width;
1466                 new_content_info->media_meta.height = content_info->media_meta.height;
1467         }
1468
1469         if (content_info->added_time > 0)
1470                 new_content_info->added_time = content_info->added_time;
1471         new_content_info->last_played_time = content_info->last_played_time;
1472         new_content_info->played_count = content_info->played_count;
1473         new_content_info->favourate = content_info->favourate;
1474
1475         if (STRING_VALID(content_info->file_name)) {
1476                         ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1477                         if (ret != MS_MEDIA_ERR_NONE)
1478                                 media_svc_error("strcpy file_name failed");
1479         }
1480
1481         if (STRING_VALID(content_info->media_meta.title)) {
1482                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1483                 if (ret != MS_MEDIA_ERR_NONE)
1484                         media_svc_error("strcpy title failed");
1485         }
1486
1487         if (STRING_VALID(content_info->media_meta.album)) {
1488                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1489                 if (ret != MS_MEDIA_ERR_NONE)
1490                         media_svc_error("strcpy album failed");
1491         }
1492
1493         if (STRING_VALID(content_info->media_meta.artist)) {
1494                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1495                 if (ret != MS_MEDIA_ERR_NONE)
1496                         media_svc_error("strcpy artist failed");
1497         }
1498
1499         if (STRING_VALID(content_info->media_meta.genre)) {
1500                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1501                 if (ret != MS_MEDIA_ERR_NONE)
1502                         media_svc_error("strcpy genre failed");
1503         }
1504
1505         if (STRING_VALID(content_info->media_meta.composer)) {
1506                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1507                 if (ret != MS_MEDIA_ERR_NONE)
1508                         media_svc_error("strcpy composer failed");
1509         }
1510
1511         if (STRING_VALID(content_info->media_meta.year)) {
1512                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1513                 if (ret != MS_MEDIA_ERR_NONE)
1514                         media_svc_error("strcpy year failed");
1515         }
1516
1517         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1518                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1519                 if (ret != MS_MEDIA_ERR_NONE)
1520                         media_svc_error("strcpy recorded_date failed");
1521         }
1522
1523         if (STRING_VALID(content_info->media_meta.copyright)) {
1524                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1525                 if (ret != MS_MEDIA_ERR_NONE)
1526                         media_svc_error("strcpy copyright failed");
1527         }
1528
1529         if (STRING_VALID(content_info->media_meta.track_num)) {
1530                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1531                 if (ret != MS_MEDIA_ERR_NONE)
1532                         media_svc_error("strcpy track_num failed");
1533         }
1534
1535         if (STRING_VALID(content_info->media_meta.description)) {
1536                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1537                 if (ret != MS_MEDIA_ERR_NONE)
1538                         media_svc_error("strcpy description failed");
1539         }
1540
1541         if (STRING_VALID(content_info->media_meta.weather)) {
1542                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1543                 if (ret != MS_MEDIA_ERR_NONE)
1544                         media_svc_error("strcpy weather failed");
1545         }
1546
1547         if (STRING_VALID(content_info->media_meta.category)) {
1548                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1549                 if (ret != MS_MEDIA_ERR_NONE)
1550                         media_svc_error("strcpy category failed");
1551         }
1552
1553         if (STRING_VALID(content_info->media_meta.keyword)) {
1554                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1555                 if (ret != MS_MEDIA_ERR_NONE)
1556                         media_svc_error("strcpy keyword failed");
1557         }
1558
1559         if (STRING_VALID(content_info->media_meta.location_tag)) {
1560                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1561                 if (ret != MS_MEDIA_ERR_NONE)
1562                         media_svc_error("strcpy location_tag failed");
1563         }
1564
1565         if (STRING_VALID(content_info->media_meta.content_name)) {
1566                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1567                 if (ret != MS_MEDIA_ERR_NONE)
1568                         media_svc_error("strcpy content_name failed");
1569         }
1570
1571         if (STRING_VALID(content_info->media_meta.age_rating)) {
1572                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1573                 if (ret != MS_MEDIA_ERR_NONE)
1574                         media_svc_error("strcpy age_rating failed");
1575         }
1576
1577         if (STRING_VALID(content_info->media_meta.author)) {
1578                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1579                 if (ret != MS_MEDIA_ERR_NONE)
1580                         media_svc_error("strcpy author failed");
1581         }
1582
1583         if (STRING_VALID(content_info->media_meta.provider)) {
1584                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1585                 if (ret != MS_MEDIA_ERR_NONE)
1586                         media_svc_error("strcpy provider failed");
1587         }
1588
1589         if (STRING_VALID(content_info->media_meta.datetaken)) {
1590                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1591                 if (ret != MS_MEDIA_ERR_NONE)
1592                         media_svc_error("strcpy datetaken failed");
1593
1594                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1595                 if (new_content_info->timeline == 0) {
1596                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1597                         new_content_info->timeline = new_content_info->modified_time;
1598                 } else {
1599                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1600                 }
1601         }
1602
1603         //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1604         //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1605         //new_content_info->media_meta.channel = content_info->media_meta.channel;
1606         //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1607
1608         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1609                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1610         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1611                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1612         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1613                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1614
1615         new_content_info->media_meta.rating = content_info->media_meta.rating;
1616
1617         return 0;
1618 }
1619
1620 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1621 {
1622         int ret = MS_MEDIA_ERR_NONE;
1623         sqlite3 *db_handle = (sqlite3 *)handle;
1624         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1625         bool append_album = FALSE;
1626
1627         /* Checking parameters if they are valid */
1628         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1629         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1630         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1631
1632         if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1633                 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1634
1635                 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1636                         media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1637                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1638                 }
1639
1640                 if (content_info->size <= 0) {
1641                         media_svc_error("invalid size condition[%d]", content_info->size);
1642                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1643                 }
1644
1645                 if (content_info->modified_time <= 0) {
1646                         media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1647                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1648                 }
1649         }
1650
1651         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1652
1653         media_svc_content_info_s _new_content_info;
1654         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1655
1656         media_svc_media_type_e media_type;
1657
1658         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1659         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1660
1661         if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1662                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1663                         /*Do nothing.*/
1664                 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1665                         ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1666                 } else {
1667                         ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1668                 }
1669
1670                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1671
1672                 /* Extracting thumbnail */
1673                 int ini_val = _media_svc_get_ini_value();
1674                 if (ini_val == 1) {
1675                         if (_new_content_info.thumbnail_path == NULL) {
1676                                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1677                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1678                                         int width = 0;
1679                                         int height = 0;
1680
1681                                         ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1682                                         if (ret == MS_MEDIA_ERR_NONE) {
1683                                                 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1684                                         }
1685
1686                                         if (_new_content_info.media_meta.width <= 0)
1687                                                 _new_content_info.media_meta.width = width;
1688
1689                                         if (_new_content_info.media_meta.height <= 0)
1690                                                 _new_content_info.media_meta.height = height;
1691                                 }
1692                         }
1693                 }
1694         }
1695
1696         /* set othere data to the structure, which is passed as parameters */
1697         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1698
1699         /* Set or Get folder id */
1700         ret = _media_svc_get_and_append_folder_id_by_path(handle, _new_content_info.storage_uuid, _new_content_info.path, _new_content_info.storage_type, folder_uuid, uid);
1701         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1702
1703         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1704         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1705
1706         /* register album table data */
1707
1708         int album_id = 0;
1709         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1710                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1711
1712                 if (ret != MS_MEDIA_ERR_NONE) {
1713                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1714                                 media_svc_debug("album does not exist. So start to make album art");
1715                                 append_album = TRUE;
1716                         } else {
1717                                 media_svc_debug("other error");
1718                                 append_album = FALSE;
1719                         }
1720                 } else {
1721                         _new_content_info.album_id = album_id;
1722                         append_album = FALSE;
1723
1724                         if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1725                                 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1726
1727                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1728                         } else {
1729
1730                                 media_svc_debug("album already exists. don't need to make album art");
1731                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1732                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1733                         }
1734                 }
1735         }
1736
1737         if (append_album == TRUE) {
1738
1739                 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1740                         (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1741                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, _new_content_info.thumbnail_path, &album_id, uid);
1742                 else
1743                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1744
1745                 _new_content_info.album_id = album_id;
1746         }
1747
1748         /* Insert to db - calling _media_svc_insert_item_with_data */
1749         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1750
1751         if (ret == MS_MEDIA_ERR_NONE) {
1752                 media_svc_debug("Insertion is successful.");
1753         }
1754
1755         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1756                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1757         }
1758
1759         _media_svc_destroy_content_info(&_new_content_info);
1760
1761         /* handling returned value - important */
1762         return ret;
1763 }
1764
1765 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1766 {
1767         _media_svc_destroy_content_info(content_info);
1768 }
1769
1770 int media_svc_generate_uuid(char **uuid)
1771 {
1772         char *gen_uuid = NULL;
1773         gen_uuid = _media_info_generate_uuid();
1774         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1775
1776         *uuid = strdup(gen_uuid);
1777
1778         return MS_MEDIA_ERR_NONE;
1779 }
1780
1781 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1782 {
1783         sqlite3 * db_handle = (sqlite3 *)handle;
1784
1785         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1786
1787         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1788 }
1789
1790 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1791 {
1792         sqlite3 * db_handle = (sqlite3 *)handle;
1793
1794         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1795         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1796         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1797         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1798
1799         return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1800 }
1801
1802 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1803 {
1804         sqlite3 * db_handle = (sqlite3 *)handle;
1805
1806         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1807         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1808         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1809
1810         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1811 }
1812
1813 int media_svc_insert_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, const char *storage_path, const char *storage_account, media_svc_storage_type_e storage_type, uid_t uid)
1814 {
1815         int ret = MS_MEDIA_ERR_NONE;
1816         sqlite3 *db_handle = (sqlite3 *)handle;
1817
1818         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1819         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1820         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1821         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1822
1823         ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1824         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1825
1826         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1827                 ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1828                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1829
1830                 ret = _media_svc_update_media_view(db_handle, uid);
1831                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1832         }
1833
1834         return ret;
1835 }
1836
1837 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1838 {
1839         int ret = MS_MEDIA_ERR_NONE;
1840         sqlite3 *db_handle = (sqlite3 *)handle;
1841         media_svc_storage_type_e storage_type;
1842
1843         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1844         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1845
1846         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1847         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1848
1849         ret = _media_svc_delete_storage(db_handle, storage_id, storage_name, uid);
1850         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1851
1852         ret = _media_svc_delete_folder_by_storage_id(db_handle, storage_id, storage_type, uid);
1853         if (ret != MS_MEDIA_ERR_NONE) {
1854                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1855         }
1856
1857         if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1858                 ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1859                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1860
1861                 ret = _media_svc_update_media_view(db_handle, uid);
1862                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1863         }
1864
1865         return ret;
1866 }
1867
1868 int media_svc_insert_folder_begin(MediaSvcHandle *handle, int data_cnt)
1869 {
1870         sqlite3 * db_handle = (sqlite3 *)handle;
1871
1872         media_svc_debug("Transaction data count : [%d]", data_cnt);
1873
1874         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1875         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1876
1877         g_media_svc_insert_folder_data_cnt = data_cnt;
1878         g_media_svc_insert_folder_cur_data_cnt = 0;
1879
1880         return MS_MEDIA_ERR_NONE;
1881 }
1882
1883 int media_svc_insert_folder_end(MediaSvcHandle *handle, uid_t uid)
1884 {
1885         int ret = MS_MEDIA_ERR_NONE;
1886         sqlite3 * db_handle = (sqlite3 *)handle;
1887
1888         media_svc_debug_fenter();
1889
1890         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1891
1892         if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1893
1894                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1895         }
1896
1897         g_media_svc_insert_folder_data_cnt = 1;
1898         g_media_svc_insert_folder_cur_data_cnt = 0;
1899
1900         return ret;
1901 }
1902
1903 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1904 {
1905         int ret = MS_MEDIA_ERR_NONE;
1906         sqlite3 * db_handle = (sqlite3 *)handle;
1907         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1908
1909         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1910         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1911         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1912         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1913
1914         if (g_media_svc_insert_folder_data_cnt == 1) {
1915
1916                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1917
1918         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1919
1920                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1921                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1922
1923                 g_media_svc_insert_folder_cur_data_cnt++;
1924
1925         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1926
1927                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1928                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1929
1930                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1931                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1932
1933                 g_media_svc_insert_folder_cur_data_cnt = 0;
1934
1935         } else {
1936                 media_svc_error("Error in media_svc_set_insert_folder");
1937                 return MS_MEDIA_ERR_INTERNAL;
1938         }
1939
1940         return ret;
1941 }
1942
1943 int media_svc_delete_invalid_folder(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1944 {
1945         int ret = MS_MEDIA_ERR_NONE;
1946         sqlite3 * db_handle = (sqlite3 *)handle;
1947
1948         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1949
1950         ret = _media_svc_delete_invalid_folder(db_handle, storage_id, uid);
1951
1952         return ret;
1953 }
1954
1955 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1956 {
1957         int ret = MS_MEDIA_ERR_NONE;
1958         sqlite3 * db_handle = (sqlite3 *)handle;
1959
1960         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1961
1962         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1963
1964         return ret;
1965 }
1966
1967 int media_svc_insert_item_pass1(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid)
1968 {
1969         int ret = MS_MEDIA_ERR_NONE;
1970         sqlite3 * db_handle = (sqlite3 *)handle;
1971         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1972         media_svc_media_type_e media_type;
1973
1974         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1975         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1976         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1977         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1978
1979         media_svc_content_info_s content_info;
1980         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1981
1982         /*Set basic media info*/
1983         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1984         if (ret != MS_MEDIA_ERR_NONE) {
1985                 media_svc_error("_media_svc_set_media_info fail %d", ret);
1986                 return ret;
1987         }
1988
1989         //media_svc_debug("total %d, cur %d insert flag %d", g_media_svc_insert_item_data_cnt, g_media_svc_insert_item_cur_data_cnt, g_insert_with_noti);
1990
1991         /*Set or Get folder id*/
1992         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1993         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1994
1995         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1996         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1997
1998         if (g_media_svc_insert_item_data_cnt == 1) {
1999
2000                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2001                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2002
2003                 if (g_insert_with_noti)
2004                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2005         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2006
2007                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2008                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2009
2010                 if (g_insert_with_noti)
2011                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2012
2013                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2014                 g_media_svc_insert_item_cur_data_cnt++;
2015
2016         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2017
2018                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2019                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2020
2021                 if (g_insert_with_noti)
2022                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2023
2024                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2025                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2026
2027                 media_svc_debug("_media_svc_list_query_do over");
2028
2029                 if (g_insert_with_noti) {
2030                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2031                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2032                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2033
2034                         /* Recreate noti list */
2035                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2036                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2037                 }
2038
2039                 g_media_svc_insert_item_cur_data_cnt = 0;
2040
2041         } else {
2042                 media_svc_error("Error in media_svc_insert_item_pass1");
2043                 _media_svc_destroy_content_info(&content_info);
2044                 return MS_MEDIA_ERR_INTERNAL;
2045         }
2046
2047         _media_svc_destroy_content_info(&content_info);
2048
2049         return MS_MEDIA_ERR_NONE;
2050 }
2051
2052 int media_svc_insert_item_pass2(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, int scan_type, const char *extract_path, int is_burst, uid_t uid)
2053 {
2054         int ret = MS_MEDIA_ERR_NONE;
2055         sqlite3 * db_handle = (sqlite3 *)handle;
2056         sqlite3_stmt *sql_stmt = NULL;
2057         media_svc_media_type_e media_type;
2058         media_svc_content_info_s content_info;
2059         GArray *db_data_array = NULL;
2060         media_svc_item_info_s *db_data = NULL;
2061         int idx = 0;
2062         char *sql = NULL;
2063
2064         media_svc_debug_fenter();
2065
2066         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2067         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2068         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
2069
2070         if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2071                 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND path LIKE '%q/%%'", storage_id, extract_path);
2072         } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2073                 char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
2074                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2075                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2076
2077                 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL AND folder_uuid = '%q'", storage_id, folder_uuid);
2078         } else { /*Storage Scanning*/
2079                 sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL", storage_id);
2080         }
2081
2082         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2083         if (ret != MS_MEDIA_ERR_NONE) {
2084                 media_svc_error("error when get list. err = [%d]", ret);
2085                 return ret;
2086         }
2087
2088         db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2089         if (db_data_array == NULL) {
2090                 media_svc_error("db_data_array is NULL. Out of memory");
2091                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2092         }
2093
2094         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2095                 db_data = NULL;
2096                 db_data = malloc(sizeof(media_svc_item_info_s));
2097                 if (db_data == NULL) {
2098                         media_svc_error("Out of memory");
2099                         continue;
2100                 }
2101
2102                 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2103                 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2104
2105                 g_array_append_val(db_data_array, db_data);
2106         }
2107
2108         SQLITE3_FINALIZE(sql_stmt);
2109
2110         while (db_data_array->len != 0) {
2111                 db_data = NULL;
2112                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2113                 g_array_remove_index(db_data_array, 0);
2114
2115                 if ((db_data == NULL) || (db_data->path == NULL)) {
2116                         media_svc_error("invalid db data");
2117                         continue;
2118                 }
2119
2120                 media_type = db_data->media_type;
2121                 media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2122                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2123                 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2124
2125                 content_info.media_type = media_type;
2126                 content_info.storage_type = storage_type;
2127
2128                 _media_svc_set_default_value(&content_info, FALSE);
2129
2130                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2131                         /*Do nothing.*/
2132                 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2133                         ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2134                 } else {
2135                         ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2136                 }
2137
2138                 ret = _media_svc_insert_item_pass2(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2139
2140                 _media_svc_destroy_content_info(&content_info);
2141                 SAFE_FREE(db_data->path);
2142                 SAFE_FREE(db_data);
2143                 idx++;
2144         }
2145
2146         if (idx > 0)
2147                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2148
2149         g_array_free(db_data_array, FALSE);
2150         db_data_array = NULL;
2151
2152         media_svc_debug_fleave();
2153
2154         return MS_MEDIA_ERR_NONE;
2155 }
2156
2157 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2158 {
2159         int ret = MS_MEDIA_ERR_NONE;
2160         sqlite3 * db_handle = (sqlite3 *)handle;
2161
2162         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2163
2164         ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2165
2166         return ret;
2167 }
2168
2169 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2170 {
2171         int ret = MS_MEDIA_ERR_NONE;
2172         sqlite3 * db_handle = (sqlite3 *)handle;
2173         int count = -1;
2174
2175         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2176         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2177         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2178
2179         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2180         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2181
2182         if (count > 0) {
2183                 media_svc_debug("item is exist in database");
2184                 return MS_MEDIA_ERR_NONE;
2185         } else {
2186                 media_svc_debug("item is not exist in database");
2187                 return MS_MEDIA_ERR_DB_NO_RECORD;
2188         }
2189
2190         return MS_MEDIA_ERR_NONE;
2191 }
2192
2193 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2194 {
2195         int ret = MS_MEDIA_ERR_NONE;
2196         sqlite3 * db_handle = (sqlite3 *)handle;
2197         int cnt = -1;
2198
2199         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2200         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2201         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2202
2203         *count = 0;
2204         ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2205         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2206         *count = cnt;
2207
2208         if (cnt > 0) {
2209                 media_svc_debug("item is exist in database");
2210                 return MS_MEDIA_ERR_NONE;
2211         } else {
2212                 media_svc_debug("item is not exist in database");
2213                 return MS_MEDIA_ERR_DB_NO_RECORD;
2214         }
2215
2216         return MS_MEDIA_ERR_NONE;
2217 }
2218
2219 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2220 {
2221         int ret = MS_MEDIA_ERR_NONE;
2222         sqlite3 * db_handle = (sqlite3 *)handle;
2223
2224         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2225         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2226
2227         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2228
2229         return ret;
2230 }
2231