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