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