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