[ACR-1163] Add synchronous API to create thumbnails
[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_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, 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, media_type, 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_create_thumbnail(content_info.path, thumb_path, sizeof(thumb_path), media_type, 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         int media_type = 0;
902         GArray *query_array = NULL;
903
904         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
905         media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
906         media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
907
908         media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
909
910         ret = _media_svc_sql_begin_trans(uid);
911         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
912
913         /*1. Rename directory */
914         /*Update Pinyin If Support Pinyin*/
915         name = g_path_get_basename(dst_path);
916         media_svc_check_pinyin_support(&pinyin_support);
917         if (pinyin_support) {
918                 media_svc_get_pinyin(name, &name_pinyin);
919                 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q', name_pinyin='%q' WHERE path = %Q",
920                                                 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, name_pinyin, src_path);
921         } else {
922                 sql = sqlite3_mprintf("UPDATE %Q SET path='%q', name='%q' WHERE path = %Q",
923                                                 MEDIA_SVC_DB_TABLE_FOLDER, dst_path, name, src_path);
924         }
925
926         ret = media_db_request_update_db_batch(sql, uid);
927         SQLITE3_SAFE_FREE(sql);
928         SAFE_FREE(name);
929         SAFE_FREE(name_pinyin);
930         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
931
932         /* 2. Update sub-dir, sub-file */
933         /* Sub folder */
934         sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q/', '%q/');", src_path, dst_path);
935         ret = media_db_request_update_db_batch(sql, uid);
936         SQLITE3_SAFE_FREE(sql);
937         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
938
939         /* Sub files */
940         sql = sqlite3_mprintf("UPDATE '%q' SET path = REPLACE( path, '%q/', '%q/');", storage_id, src_path, dst_path);
941         ret = media_db_request_update_db_batch(sql, uid);
942         SQLITE3_SAFE_FREE(sql);
943         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch update request");
944
945         ret = _media_svc_sql_end_trans(uid);
946         if (ret != MS_MEDIA_ERR_NONE) {
947                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
948                 return ret;
949         }
950
951         /*3. Get and update thumbnail_path if exists */
952         sqlite3_stmt *sql_stmt = NULL;
953
954         sql = sqlite3_mprintf("SELECT path, thumbnail_path, media_type from '%q' where path LIKE '%q/%%' AND thumbnail_path is not null", storage_id, dst_path);
955         media_svc_debug("[SQL query] : %s", sql);
956
957         ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
958         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "error when media_svc_rename_folder. err = [%d]", ret);
959
960
961         query_array = g_array_new(FALSE, FALSE, sizeof(char *));
962         media_svc_retvm_if(query_array == NULL, MS_MEDIA_ERR_INTERNAL, "g_array_new failed");
963
964         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
965                 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
966                 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
967                 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
968                 char *thumb_query = NULL;
969
970                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
971                         SAFE_STRLCPY(media_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_path));
972                 } else {
973                         media_svc_error("media path is NULL");
974                         SQLITE3_FINALIZE(sql_stmt);
975                         return MS_MEDIA_ERR_DB_INTERNAL;
976                 }
977
978                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
979                         SAFE_STRLCPY(media_thumb_path,  (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_thumb_path));
980                 } else {
981                         media_svc_error("thumbnail path is NULL");
982                         SQLITE3_FINALIZE(sql_stmt);
983                         return MS_MEDIA_ERR_DB_INTERNAL;
984                 }
985
986                 media_type = sqlite3_column_int(sql_stmt, 2);
987
988                 ms_user_storage_type_e storage_type = -1;
989
990                 ret = ms_user_get_storage_type(uid, media_path, &storage_type);
991                 if (ret != MS_MEDIA_ERR_NONE) {
992                         media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, media_path, storage_type);
993                         SQLITE3_FINALIZE(sql_stmt);
994                         return ret;
995                 }
996
997                 ret = _media_svc_get_thumbnail_path(storage_type, media_type, media_new_thumb_path, media_path, THUMB_EXT, uid);
998                 if (ret != MS_MEDIA_ERR_NONE) {
999                         media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1000                         SQLITE3_FINALIZE(sql_stmt);
1001                         return ret;
1002                 }
1003
1004                 thumb_query = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path='%q' WHERE path='%q'", storage_id, media_new_thumb_path, media_path);
1005                 g_array_append_val(query_array, thumb_query);
1006
1007                 ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1008                 if (ret != MS_MEDIA_ERR_NONE)
1009                         media_svc_error("_media_svc_rename_file failed : %d", ret);
1010         }
1011
1012         SQLITE3_FINALIZE(sql_stmt);
1013
1014         while (query_array->len != 0) {
1015                 sql = g_array_index(query_array , char*, 0);
1016                 g_array_remove_index(query_array, 0);
1017                 ret = media_db_request_update_db(sql, uid);
1018                 SQLITE3_SAFE_FREE(sql);
1019                 if (ret != MS_MEDIA_ERR_NONE)
1020                         media_svc_error("media_db_request_update_db failed : %d", ret);
1021         }
1022
1023         g_array_free(query_array, FALSE);
1024         query_array = NULL;
1025
1026         media_svc_debug("Folder update is successful. Sending noti for this");
1027         /* Get notification info */
1028         media_svc_noti_item *noti_item = NULL;
1029         ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1030         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1031
1032         _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1033         _media_svc_destroy_noti_item(noti_item);
1034
1035         return MS_MEDIA_ERR_NONE;
1036 }
1037
1038 int media_svc_request_update_db(const char *db_query, uid_t uid)
1039 {
1040         media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1041
1042         return _media_svc_sql_query(db_query, uid);
1043 }
1044
1045 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)
1046 {
1047         int ret = MS_MEDIA_ERR_NONE;
1048         sqlite3 *db_handle = (sqlite3 *)handle;
1049         char *uuid = NULL;
1050
1051         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1052         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1053
1054         /* Get notification info */
1055         media_svc_noti_item *noti_item = NULL;
1056         ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1057         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1058
1059         if (folder_id != NULL) {
1060                 uuid = strndup(folder_id, strlen(folder_id));
1061         } else {
1062                 if (noti_item->media_uuid != NULL) {
1063                         uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1064                 } else {
1065                         _media_svc_destroy_noti_item(noti_item);
1066                         media_svc_error("folder uuid is wrong");
1067                         return MS_MEDIA_ERR_DB_INTERNAL;
1068                 }
1069         }
1070
1071         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1072         _media_svc_destroy_noti_item(noti_item);
1073         SAFE_FREE(uuid);
1074
1075         return ret;
1076 }
1077
1078 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1079 {
1080         sqlite3 *db_handle = (sqlite3 *)handle;
1081
1082         media_svc_debug_fenter();
1083
1084         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1085
1086         return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1087 }
1088
1089 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1090 {
1091         int ret = MS_MEDIA_ERR_NONE;
1092         sqlite3 *db_handle = (sqlite3 *)handle;
1093         time_t sto_time = 0;
1094         int cur_time = _media_svc_get_file_time(folder_path);
1095         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1096
1097         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1098
1099         ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1100         if (ret == MS_MEDIA_ERR_NONE) {
1101                 if (sto_time != cur_time)
1102                         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1103         }
1104
1105         return ret;
1106 }
1107
1108 int media_svc_update_item_begin(int data_cnt)
1109 {
1110         media_svc_debug("Transaction data count : [%d]", data_cnt);
1111
1112         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1113
1114         g_media_svc_update_item_data_cnt = data_cnt;
1115         g_media_svc_update_item_cur_data_cnt = 0;
1116
1117         return MS_MEDIA_ERR_NONE;
1118 }
1119
1120 int media_svc_update_item_end(uid_t uid)
1121 {
1122         int ret = MS_MEDIA_ERR_NONE;
1123
1124         media_svc_debug_fenter();
1125
1126         if (g_media_svc_update_item_cur_data_cnt > 0)
1127                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1128
1129         g_media_svc_update_item_data_cnt = 1;
1130         g_media_svc_update_item_cur_data_cnt = 0;
1131
1132         return ret;
1133 }
1134
1135 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1136 {
1137         int ret = MS_MEDIA_ERR_NONE;
1138         sqlite3 *db_handle = (sqlite3 *)handle;
1139
1140         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1141
1142         media_svc_media_type_e media_type;
1143         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1144         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1145
1146         media_svc_content_info_s content_info;
1147         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1148
1149         /*Set media info*/
1150         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1151         if (ret != MS_MEDIA_ERR_NONE)
1152                 return ret;
1153
1154         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1155                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1156         else {
1157                 _media_svc_destroy_content_info(&content_info);
1158                 return MS_MEDIA_ERR_NONE;
1159         }
1160
1161         if (ret != MS_MEDIA_ERR_NONE) {
1162                 _media_svc_destroy_content_info(&content_info);
1163                 return ret;
1164         }
1165
1166         if (g_media_svc_update_item_data_cnt == 1) {
1167
1168                 ret = _media_svc_update_meta_with_data(&content_info);
1169                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1170
1171         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1172
1173                 ret = _media_svc_update_meta_with_data(&content_info);
1174                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1175
1176                 g_media_svc_update_item_cur_data_cnt++;
1177
1178         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1179
1180                 ret = _media_svc_update_meta_with_data(&content_info);
1181                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1182
1183                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1184                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1185
1186                 g_media_svc_update_item_cur_data_cnt = 0;
1187
1188         } else {
1189                 media_svc_error("Error in media_svc_update_item_meta");
1190                 _media_svc_destroy_content_info(&content_info);
1191                 return MS_MEDIA_ERR_INTERNAL;
1192         }
1193
1194         _media_svc_destroy_content_info(&content_info);
1195
1196         return ret;
1197 }
1198
1199 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)
1200 {
1201         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1202 }
1203
1204 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1205 {
1206         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1207
1208         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1209 }
1210
1211 int media_svc_check_pinyin_support(bool *support)
1212 {
1213         *support = _media_svc_check_pinyin_support();
1214
1215         return MS_MEDIA_ERR_NONE;
1216 }
1217
1218 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1219 {
1220         int ret = MS_MEDIA_ERR_NONE;
1221         sqlite3 * db_handle = (sqlite3 *)handle;
1222
1223         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1224
1225         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1226         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1227
1228         ret = _media_svc_update_media_view(db_handle, uid);
1229         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1230
1231         return ret;
1232 }
1233
1234 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1235 {
1236         int ret = MS_MEDIA_ERR_NONE;
1237         sqlite3 * db_handle = (sqlite3 *)handle;
1238
1239         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1240         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1241
1242         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1243
1244         return ret;
1245 }
1246
1247 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1248 {
1249         int ret = MS_MEDIA_ERR_NONE;
1250         sqlite3 * db_handle = (sqlite3 *)handle;
1251
1252         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1253         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1254
1255         ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1256
1257         return ret;
1258 }
1259
1260 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1261 {
1262         int ret = MS_MEDIA_ERR_NONE;
1263         sqlite3 * db_handle = (sqlite3 *)handle;
1264
1265         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1266         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1267
1268         ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1269
1270         return ret;
1271 }
1272
1273 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1274 {
1275         int ret = MS_MEDIA_ERR_NONE;
1276
1277         ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1278
1279         return ret;
1280 }
1281
1282 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1283 {
1284         sqlite3 * db_handle = (sqlite3 *)handle;
1285
1286         media_svc_debug_fenter();
1287
1288         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1289         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1290
1291         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1292 }
1293
1294 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1295 {
1296         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1297         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1298
1299         if (content_info->added_time > 0)
1300                 new_content_info->added_time = content_info->added_time;
1301         new_content_info->last_played_time = content_info->last_played_time;
1302         new_content_info->played_count = content_info->played_count;
1303         new_content_info->favourate = content_info->favourate;
1304
1305         /* Can be NULL if user not to set display_name using media_info_set_displayname().. */
1306         if (STRING_VALID(content_info->file_name)) {
1307                 /* Already filled in _media_svc_set_media_info() */
1308                 SAFE_FREE(new_content_info->file_name);
1309                 new_content_info->file_name = g_strdup(content_info->file_name);
1310         }
1311         new_content_info->media_meta.title = g_strdup(content_info->media_meta.title);
1312         new_content_info->media_meta.album = g_strdup(content_info->media_meta.album);
1313         new_content_info->media_meta.artist = g_strdup(content_info->media_meta.artist);
1314         new_content_info->media_meta.genre = g_strdup(content_info->media_meta.genre);
1315         new_content_info->media_meta.composer = g_strdup(content_info->media_meta.composer);
1316         new_content_info->media_meta.year = g_strdup(content_info->media_meta.year);
1317         new_content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.recorded_date);
1318         new_content_info->media_meta.copyright = g_strdup(content_info->media_meta.copyright);
1319         new_content_info->media_meta.track_num = g_strdup(content_info->media_meta.track_num);
1320         new_content_info->media_meta.description = g_strdup(content_info->media_meta.description);
1321         new_content_info->media_meta.weather = g_strdup(content_info->media_meta.weather);
1322         new_content_info->media_meta.category = g_strdup(content_info->media_meta.category);
1323         new_content_info->media_meta.keyword = g_strdup(content_info->media_meta.keyword);
1324         new_content_info->media_meta.location_tag = g_strdup(content_info->media_meta.location_tag);
1325         new_content_info->media_meta.content_name = g_strdup(content_info->media_meta.content_name);
1326         new_content_info->media_meta.age_rating = g_strdup(content_info->media_meta.age_rating);
1327         new_content_info->media_meta.author = g_strdup(content_info->media_meta.author);
1328         new_content_info->media_meta.provider = g_strdup(content_info->media_meta.provider);
1329
1330         if (STRING_VALID(content_info->media_meta.datetaken)) {
1331                 new_content_info->media_meta.datetaken = g_strdup(content_info->media_meta.datetaken);
1332
1333                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1334                 if (new_content_info->timeline == 0) {
1335                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1336                         new_content_info->timeline = new_content_info->modified_time;
1337                 } else {
1338                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1339                 }
1340         }
1341
1342         new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1343         /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1344         /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1345         /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1346         /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1347
1348         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1349                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1350         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1351                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1352         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1353                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1354
1355         new_content_info->media_meta.rating = content_info->media_meta.rating;
1356
1357         return MS_MEDIA_ERR_NONE;
1358 }
1359
1360 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1361 {
1362         int ret = MS_MEDIA_ERR_NONE;
1363         sqlite3 *db_handle = (sqlite3 *)handle;
1364         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1365         bool append_album = FALSE;
1366
1367         /* Checking parameters if they are valid */
1368         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1369         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1370         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1371
1372         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1373
1374         media_svc_content_info_s _new_content_info;
1375         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1376
1377         media_svc_media_type_e media_type;
1378
1379         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1380         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1381
1382         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1383                 media_svc_debug("Do nothing[%d]", media_type);
1384         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1385                 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1386         else
1387                 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1388
1389         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1390
1391         /* Extracting thumbnail */
1392         if (_new_content_info.thumbnail_path == NULL) {
1393                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1394                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1395
1396                         ret = _media_svc_create_thumbnail(_new_content_info.path, thumb_path, sizeof(thumb_path), media_type, uid);
1397                         if (ret == MS_MEDIA_ERR_NONE)
1398                                 _new_content_info.thumbnail_path = g_strdup(thumb_path);
1399                 }
1400         }
1401
1402         /* set othere data to the structure, which is passed as parameters */
1403         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1404
1405         /* Set or Get folder id */
1406         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);
1407         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1408
1409         _new_content_info.folder_uuid = g_strdup(folder_uuid);
1410         media_svc_retv_del_if(_new_content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &_new_content_info);
1411
1412         /* register album table data */
1413
1414         int album_id = 0;
1415         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1416                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1417
1418                 if (ret != MS_MEDIA_ERR_NONE) {
1419                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1420                                 media_svc_debug("album does not exist. So start to make album art");
1421                                 append_album = TRUE;
1422                         } else {
1423                                 media_svc_debug("other error");
1424                                 append_album = FALSE;
1425                         }
1426                 } else {
1427                         _new_content_info.album_id = album_id;
1428                         append_album = FALSE;
1429
1430                         if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1431                                 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1432                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1433                         } else {
1434
1435                                 media_svc_debug("album already exists. don't need to make album art");
1436                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1437                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1438                         }
1439                 }
1440         }
1441
1442         if (append_album == TRUE) {
1443                 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1444                         (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1445                         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);
1446                 else
1447                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1448
1449                 _new_content_info.album_id = album_id;
1450         }
1451
1452         /* Insert to db - calling _media_svc_insert_item_with_data */
1453         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1454
1455         if (ret == MS_MEDIA_ERR_NONE)
1456                 media_svc_debug("Insertion is successful.");
1457
1458         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1459                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1460
1461         _media_svc_destroy_content_info(&_new_content_info);
1462
1463         /* handling returned value - important */
1464         return ret;
1465 }
1466
1467 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1468 {
1469         _media_svc_destroy_content_info(content_info);
1470 }
1471
1472 int media_svc_generate_uuid(char **uuid)
1473 {
1474         char *gen_uuid = NULL;
1475         gen_uuid = _media_info_generate_uuid();
1476         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1477
1478         *uuid = strdup(gen_uuid);
1479
1480         return MS_MEDIA_ERR_NONE;
1481 }
1482
1483 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1484 {
1485         sqlite3 * db_handle = (sqlite3 *)handle;
1486
1487         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1488
1489         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1490 }
1491
1492 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
1493 {
1494         sqlite3 * db_handle = (sqlite3 *)handle;
1495
1496         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1497         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1498         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1499         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1500
1501         return _media_svc_check_storage(db_handle, storage_id, storage_path, validity, uid);
1502 }
1503
1504 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1505 {
1506         sqlite3 * db_handle = (sqlite3 *)handle;
1507
1508         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1509         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1510         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1511
1512         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1513 }
1514
1515 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)
1516 {
1517         int ret = MS_MEDIA_ERR_NONE;
1518         sqlite3 *db_handle = (sqlite3 *)handle;
1519
1520         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1521         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1522         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1523         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1524
1525         ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1526         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1527
1528         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1529                 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1530                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1531
1532                 ret = _media_svc_update_media_view(db_handle, uid);
1533                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1534         }
1535
1536         return ret;
1537 }
1538
1539 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1540 {
1541         int ret = MS_MEDIA_ERR_NONE;
1542         sqlite3 *db_handle = (sqlite3 *)handle;
1543         media_svc_storage_type_e storage_type;
1544
1545         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1546         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1547
1548         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1549         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1550
1551         ret = _media_svc_delete_storage(storage_id, uid);
1552         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1553
1554         ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1555         if (ret != MS_MEDIA_ERR_NONE)
1556                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1557
1558         if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1559                 ret = _media_svc_drop_media_table(storage_id, uid);
1560                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1561
1562                 ret = _media_svc_update_media_view(db_handle, uid);
1563                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1564         }
1565
1566         return ret;
1567 }
1568
1569 int media_svc_insert_folder_begin(int data_cnt)
1570 {
1571         media_svc_debug("Transaction data count : [%d]", data_cnt);
1572
1573         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1574
1575         g_media_svc_insert_folder_data_cnt = data_cnt;
1576         g_media_svc_insert_folder_cur_data_cnt = 0;
1577
1578         return MS_MEDIA_ERR_NONE;
1579 }
1580
1581 int media_svc_insert_folder_end(uid_t uid)
1582 {
1583         int ret = MS_MEDIA_ERR_NONE;
1584
1585         media_svc_debug_fenter();
1586
1587         if (g_media_svc_insert_folder_cur_data_cnt > 0)
1588                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1589
1590         g_media_svc_insert_folder_data_cnt = 1;
1591         g_media_svc_insert_folder_cur_data_cnt = 0;
1592
1593         return ret;
1594 }
1595
1596 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1597 {
1598         int ret = MS_MEDIA_ERR_NONE;
1599         sqlite3 * db_handle = (sqlite3 *)handle;
1600         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1601
1602         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1603         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1604         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1605         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1606
1607         if (g_media_svc_insert_folder_data_cnt == 1) {
1608
1609                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1610
1611         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1612
1613                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1614                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1615
1616                 g_media_svc_insert_folder_cur_data_cnt++;
1617
1618         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1619
1620                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1621                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1622
1623                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1624                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1625
1626                 g_media_svc_insert_folder_cur_data_cnt = 0;
1627
1628         } else {
1629                 media_svc_error("Error in media_svc_set_insert_folder");
1630                 return MS_MEDIA_ERR_INTERNAL;
1631         }
1632
1633         return ret;
1634 }
1635
1636 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1637 {
1638         int ret = MS_MEDIA_ERR_NONE;
1639
1640         ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1641
1642         return ret;
1643 }
1644
1645 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1646 {
1647         int ret = MS_MEDIA_ERR_NONE;
1648         sqlite3 * db_handle = (sqlite3 *)handle;
1649
1650         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1651
1652         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1653
1654         return ret;
1655 }
1656
1657 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)
1658 {
1659         int ret = MS_MEDIA_ERR_NONE;
1660         sqlite3 * db_handle = (sqlite3 *)handle;
1661         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1662         media_svc_media_type_e media_type;
1663
1664         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1665         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1666         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1667         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1668
1669         media_svc_content_info_s content_info;
1670         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1671
1672         /*Set basic media info*/
1673         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1674         if (ret != MS_MEDIA_ERR_NONE) {
1675                 media_svc_error("_media_svc_set_media_info fail %d", ret);
1676                 return ret;
1677         }
1678
1679         /*Set or Get folder id*/
1680         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1681         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1682
1683         content_info.folder_uuid = g_strdup(folder_uuid);
1684         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
1685
1686         if (g_media_svc_insert_item_data_cnt == 1) {
1687
1688                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1689                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1690
1691                 if (g_insert_with_noti)
1692                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1693         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1694
1695                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1696                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1697
1698                 if (g_insert_with_noti)
1699                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1700
1701                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1702                 g_media_svc_insert_item_cur_data_cnt++;
1703
1704         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1705
1706                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1707                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1708
1709                 if (g_insert_with_noti)
1710                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1711
1712                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1713                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1714
1715                 media_svc_debug("_media_svc_list_query_do over");
1716
1717                 if (g_insert_with_noti) {
1718                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1719                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1720                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1721
1722                         /* Recreate noti list */
1723                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1724                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1725                 }
1726
1727                 g_media_svc_insert_item_cur_data_cnt = 0;
1728
1729         } else {
1730                 media_svc_error("Error in media_svc_insert_item_pass1");
1731                 _media_svc_destroy_content_info(&content_info);
1732                 return MS_MEDIA_ERR_INTERNAL;
1733         }
1734
1735         _media_svc_destroy_content_info(&content_info);
1736
1737         return MS_MEDIA_ERR_NONE;
1738 }
1739
1740 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
1741 {
1742         int ret = MS_MEDIA_ERR_NONE;
1743         sqlite3 * db_handle = (sqlite3 *)handle;
1744
1745         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1746         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1747
1748         ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
1749
1750         return ret;
1751 }
1752
1753 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
1754 {
1755         int ret = MS_MEDIA_ERR_NONE;
1756
1757         ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
1758
1759         return ret;
1760 }
1761
1762 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
1763 {
1764         int ret = MS_MEDIA_ERR_NONE;
1765         sqlite3 * db_handle = (sqlite3 *)handle;
1766         time_t modified_time = 0;
1767         int system_time = 0;
1768
1769         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1770
1771         ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
1772
1773         system_time = _media_svc_get_file_time(path);
1774         media_svc_error("modified_time = [%ld], system_time = [%d], path = [%s]", modified_time, system_time, path);
1775
1776         if (system_time != modified_time && system_time != 0)
1777                 *modified = TRUE;
1778         else
1779                 *modified = FALSE;
1780
1781         return ret;
1782 }
1783
1784 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
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         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1790
1791         return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
1792 }
1793
1794 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
1795 {
1796         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1797         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1798
1799         return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
1800 }
1801
1802 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
1803 {
1804         int ret = MS_MEDIA_ERR_NONE;
1805         sqlite3 * db_handle = (sqlite3 *)handle;
1806         int count = -1;
1807
1808         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1809         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1810         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
1811
1812         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
1813         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1814
1815         if (count > 0) {
1816                 media_svc_debug("item is exist in database");
1817                 return MS_MEDIA_ERR_NONE;
1818         } else {
1819                 media_svc_debug("item is not exist in database");
1820                 return MS_MEDIA_ERR_DB_NO_RECORD;
1821         }
1822
1823         return MS_MEDIA_ERR_NONE;
1824 }
1825
1826 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
1827 {
1828         int ret = MS_MEDIA_ERR_NONE;
1829         sqlite3 * db_handle = (sqlite3 *)handle;
1830
1831         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1832         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1833
1834         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
1835
1836         return ret;
1837 }
1838
1839 int media_svc_append_query(const char *query, uid_t uid)
1840 {
1841         int ret = MS_MEDIA_ERR_NONE;
1842
1843         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
1844
1845         ret = _media_svc_append_query_list(query, uid);
1846
1847         return ret;
1848 }
1849
1850 int media_svc_send_query(uid_t uid)
1851 {
1852         int ret = MS_MEDIA_ERR_NONE;
1853
1854         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
1855
1856         return ret;
1857 }
1858
1859 int media_svc_get_media_type(const char *path, int *mediatype)
1860 {
1861         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1862
1863         return _media_svc_get_media_type(path, mediatype);
1864 }
1865
1866 int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
1867 {
1868         int ret = MS_MEDIA_ERR_NONE;
1869         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
1870         char *sql = NULL;
1871
1872         // 1. Check media type
1873         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
1874                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1875
1876         // 2. try to create thumbnail
1877         ret = _media_svc_create_thumbnail(file_path, thumb_path, MEDIA_SVC_PATHNAME_SIZE, media_type, uid);
1878         if (ret != MS_MEDIA_ERR_NONE)
1879                 media_svc_error("Failed to create thumbnail [%d]", ret);
1880
1881         // 3. Update creation result to media db
1882         sql = sqlite3_mprintf("UPDATE %q SET thumbnail_path = '%q' WHERE path='%q';", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, file_path);
1883
1884         ret = _media_svc_sql_query(sql, uid);
1885         SQLITE3_SAFE_FREE(sql);
1886         if (ret != MS_MEDIA_ERR_NONE) {
1887                 media_svc_error("Failed to update media db [%d]", ret);
1888                 *thumbnail_path = g_strdup("");
1889         } else {
1890                 *thumbnail_path = g_strdup(thumb_path);
1891         }
1892
1893         return ret;
1894 }