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