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