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