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