Remove unnecessary parameter
[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                 return MS_MEDIA_ERR_NONE;
1294
1295         if (ret != MS_MEDIA_ERR_NONE) {
1296                 _media_svc_destroy_content_info(&content_info);
1297                 return ret;
1298         }
1299
1300         if (g_media_svc_update_item_data_cnt == 1) {
1301
1302                 ret = _media_svc_update_meta_with_data(&content_info);
1303                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1304
1305         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1306
1307                 ret = _media_svc_update_meta_with_data(&content_info);
1308                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1309
1310                 g_media_svc_update_item_cur_data_cnt++;
1311
1312         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1313
1314                 ret = _media_svc_update_meta_with_data(&content_info);
1315                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1316
1317                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1318                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1319
1320                 g_media_svc_update_item_cur_data_cnt = 0;
1321
1322         } else {
1323                 media_svc_error("Error in media_svc_update_item_meta");
1324                 _media_svc_destroy_content_info(&content_info);
1325                 return MS_MEDIA_ERR_INTERNAL;
1326         }
1327
1328         _media_svc_destroy_content_info(&content_info);
1329
1330         return ret;
1331 }
1332
1333 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)
1334 {
1335         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1336 }
1337
1338 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1339 {
1340         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1341
1342         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1343 }
1344
1345 int media_svc_check_pinyin_support(bool *support)
1346 {
1347         *support = _media_svc_check_pinyin_support();
1348
1349         return MS_MEDIA_ERR_NONE;
1350 }
1351
1352 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1353 {
1354         int ret = MS_MEDIA_ERR_NONE;
1355         sqlite3 * db_handle = (sqlite3 *)handle;
1356
1357         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1358
1359         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1360         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1361
1362         ret = _media_svc_update_media_view(db_handle, uid);
1363         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1364
1365         return ret;
1366 }
1367
1368 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1369 {
1370         int ret = MS_MEDIA_ERR_NONE;
1371         sqlite3 * db_handle = (sqlite3 *)handle;
1372
1373         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1374         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1375
1376         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1377
1378         return ret;
1379 }
1380
1381 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1382 {
1383         int ret = MS_MEDIA_ERR_NONE;
1384         sqlite3 * db_handle = (sqlite3 *)handle;
1385
1386         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1387         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1388
1389         ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1390
1391         return ret;
1392 }
1393
1394 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1395 {
1396         int ret = MS_MEDIA_ERR_NONE;
1397         sqlite3 * db_handle = (sqlite3 *)handle;
1398
1399         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1400         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1401
1402         ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1403
1404         return ret;
1405 }
1406
1407 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1408 {
1409         int ret = MS_MEDIA_ERR_NONE;
1410
1411         ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1412
1413         return ret;
1414 }
1415
1416 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1417 {
1418         sqlite3 * db_handle = (sqlite3 *)handle;
1419
1420         media_svc_debug_fenter();
1421
1422         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1423         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1424
1425         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1426 }
1427
1428 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1429 {
1430         int ret = MS_MEDIA_ERR_NONE;
1431
1432         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1433         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1434
1435         if (content_info->added_time > 0)
1436                 new_content_info->added_time = content_info->added_time;
1437         new_content_info->last_played_time = content_info->last_played_time;
1438         new_content_info->played_count = content_info->played_count;
1439         new_content_info->favourate = content_info->favourate;
1440
1441         if (STRING_VALID(content_info->file_name)) {
1442                         ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1443                         if (ret != MS_MEDIA_ERR_NONE)
1444                                 media_svc_error("strcpy file_name failed");
1445         }
1446
1447         if (STRING_VALID(content_info->media_meta.title)) {
1448                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1449                 if (ret != MS_MEDIA_ERR_NONE)
1450                         media_svc_error("strcpy title failed");
1451         }
1452
1453         if (STRING_VALID(content_info->media_meta.album)) {
1454                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1455                 if (ret != MS_MEDIA_ERR_NONE)
1456                         media_svc_error("strcpy album failed");
1457         }
1458
1459         if (STRING_VALID(content_info->media_meta.artist)) {
1460                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1461                 if (ret != MS_MEDIA_ERR_NONE)
1462                         media_svc_error("strcpy artist failed");
1463         }
1464
1465         if (STRING_VALID(content_info->media_meta.genre)) {
1466                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1467                 if (ret != MS_MEDIA_ERR_NONE)
1468                         media_svc_error("strcpy genre failed");
1469         }
1470
1471         if (STRING_VALID(content_info->media_meta.composer)) {
1472                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1473                 if (ret != MS_MEDIA_ERR_NONE)
1474                         media_svc_error("strcpy composer failed");
1475         }
1476
1477         if (STRING_VALID(content_info->media_meta.year)) {
1478                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1479                 if (ret != MS_MEDIA_ERR_NONE)
1480                         media_svc_error("strcpy year failed");
1481         }
1482
1483         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1484                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1485                 if (ret != MS_MEDIA_ERR_NONE)
1486                         media_svc_error("strcpy recorded_date failed");
1487         }
1488
1489         if (STRING_VALID(content_info->media_meta.copyright)) {
1490                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1491                 if (ret != MS_MEDIA_ERR_NONE)
1492                         media_svc_error("strcpy copyright failed");
1493         }
1494
1495         if (STRING_VALID(content_info->media_meta.track_num)) {
1496                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1497                 if (ret != MS_MEDIA_ERR_NONE)
1498                         media_svc_error("strcpy track_num failed");
1499         }
1500
1501         if (STRING_VALID(content_info->media_meta.description)) {
1502                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1503                 if (ret != MS_MEDIA_ERR_NONE)
1504                         media_svc_error("strcpy description failed");
1505         }
1506
1507         if (STRING_VALID(content_info->media_meta.weather)) {
1508                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1509                 if (ret != MS_MEDIA_ERR_NONE)
1510                         media_svc_error("strcpy weather failed");
1511         }
1512
1513         if (STRING_VALID(content_info->media_meta.category)) {
1514                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1515                 if (ret != MS_MEDIA_ERR_NONE)
1516                         media_svc_error("strcpy category failed");
1517         }
1518
1519         if (STRING_VALID(content_info->media_meta.keyword)) {
1520                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1521                 if (ret != MS_MEDIA_ERR_NONE)
1522                         media_svc_error("strcpy keyword failed");
1523         }
1524
1525         if (STRING_VALID(content_info->media_meta.location_tag)) {
1526                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1527                 if (ret != MS_MEDIA_ERR_NONE)
1528                         media_svc_error("strcpy location_tag failed");
1529         }
1530
1531         if (STRING_VALID(content_info->media_meta.content_name)) {
1532                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1533                 if (ret != MS_MEDIA_ERR_NONE)
1534                         media_svc_error("strcpy content_name failed");
1535         }
1536
1537         if (STRING_VALID(content_info->media_meta.age_rating)) {
1538                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1539                 if (ret != MS_MEDIA_ERR_NONE)
1540                         media_svc_error("strcpy age_rating failed");
1541         }
1542
1543         if (STRING_VALID(content_info->media_meta.author)) {
1544                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1545                 if (ret != MS_MEDIA_ERR_NONE)
1546                         media_svc_error("strcpy author failed");
1547         }
1548
1549         if (STRING_VALID(content_info->media_meta.provider)) {
1550                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1551                 if (ret != MS_MEDIA_ERR_NONE)
1552                         media_svc_error("strcpy provider failed");
1553         }
1554
1555         if (STRING_VALID(content_info->media_meta.datetaken)) {
1556                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1557                 if (ret != MS_MEDIA_ERR_NONE)
1558                         media_svc_error("strcpy datetaken failed");
1559
1560                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1561                 if (new_content_info->timeline == 0) {
1562                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1563                         new_content_info->timeline = new_content_info->modified_time;
1564                 } else {
1565                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1566                 }
1567         }
1568
1569         new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1570         /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1571         /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1572         /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1573         /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1574
1575         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1576                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1577         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1578                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1579         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1580                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1581
1582         new_content_info->media_meta.rating = content_info->media_meta.rating;
1583
1584         return 0;
1585 }
1586
1587 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1588 {
1589         int ret = MS_MEDIA_ERR_NONE;
1590         sqlite3 *db_handle = (sqlite3 *)handle;
1591         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1592         bool append_album = FALSE;
1593
1594         /* Checking parameters if they are valid */
1595         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1596         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1597         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1598
1599         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1600
1601         media_svc_content_info_s _new_content_info;
1602         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1603
1604         media_svc_media_type_e media_type;
1605
1606         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1607         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1608
1609         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1610                 media_svc_debug("Do nothing[%d]", media_type);
1611         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1612                 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1613         else
1614                 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1615
1616         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1617
1618         /* Extracting thumbnail */
1619         if (_new_content_info.thumbnail_path == NULL) {
1620                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1621                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1622                         int width = 0;
1623                         int height = 0;
1624
1625                         ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1626                         if (ret == MS_MEDIA_ERR_NONE)
1627                                 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1628
1629                         if (_new_content_info.media_meta.width <= 0)
1630                                 _new_content_info.media_meta.width = width;
1631
1632                         if (_new_content_info.media_meta.height <= 0)
1633                                 _new_content_info.media_meta.height = height;
1634                 }
1635         }
1636
1637         /* set othere data to the structure, which is passed as parameters */
1638         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1639
1640         /* Set or Get folder id */
1641         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);
1642         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1643
1644         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1645         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1646
1647         /* register album table data */
1648
1649         int album_id = 0;
1650         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1651                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1652
1653                 if (ret != MS_MEDIA_ERR_NONE) {
1654                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1655                                 media_svc_debug("album does not exist. So start to make album art");
1656                                 append_album = TRUE;
1657                         } else {
1658                                 media_svc_debug("other error");
1659                                 append_album = FALSE;
1660                         }
1661                 } else {
1662                         _new_content_info.album_id = album_id;
1663                         append_album = FALSE;
1664
1665                         if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1666                                 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1667                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1668                         } else {
1669
1670                                 media_svc_debug("album already exists. don't need to make album art");
1671                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1672                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1673                         }
1674                 }
1675         }
1676
1677         if (append_album == TRUE) {
1678                 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1679                         (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1680                         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);
1681                 else
1682                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1683
1684                 _new_content_info.album_id = album_id;
1685         }
1686
1687         /* Insert to db - calling _media_svc_insert_item_with_data */
1688         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1689
1690         if (ret == MS_MEDIA_ERR_NONE)
1691                 media_svc_debug("Insertion is successful.");
1692
1693         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1694                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1695
1696         _media_svc_destroy_content_info(&_new_content_info);
1697
1698         /* handling returned value - important */
1699         return ret;
1700 }
1701
1702 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1703 {
1704         _media_svc_destroy_content_info(content_info);
1705 }
1706
1707 int media_svc_generate_uuid(char **uuid)
1708 {
1709         char *gen_uuid = NULL;
1710         gen_uuid = _media_info_generate_uuid();
1711         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1712
1713         *uuid = strdup(gen_uuid);
1714
1715         return MS_MEDIA_ERR_NONE;
1716 }
1717
1718 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1719 {
1720         sqlite3 * db_handle = (sqlite3 *)handle;
1721
1722         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1723
1724         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1725 }
1726
1727 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1728 {
1729         sqlite3 * db_handle = (sqlite3 *)handle;
1730
1731         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1732         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1733         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1734         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1735
1736         return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1737 }
1738
1739 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1740 {
1741         sqlite3 * db_handle = (sqlite3 *)handle;
1742
1743         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1744         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1745         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1746
1747         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1748 }
1749
1750 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)
1751 {
1752         int ret = MS_MEDIA_ERR_NONE;
1753         sqlite3 *db_handle = (sqlite3 *)handle;
1754
1755         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1756         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1757         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1758         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1759
1760         ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1761         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1762
1763         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1764                 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1765                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1766
1767                 ret = _media_svc_update_media_view(db_handle, uid);
1768                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1769         }
1770
1771         return ret;
1772 }
1773
1774 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1775 {
1776         int ret = MS_MEDIA_ERR_NONE;
1777         sqlite3 *db_handle = (sqlite3 *)handle;
1778         media_svc_storage_type_e storage_type;
1779
1780         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1781         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1782
1783         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1784         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1785
1786         ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1787         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1788
1789         ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1790         if (ret != MS_MEDIA_ERR_NONE)
1791                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1792
1793         if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1794                 ret = _media_svc_drop_media_table(storage_id, uid);
1795                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1796
1797                 ret = _media_svc_update_media_view(db_handle, uid);
1798                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1799         }
1800
1801         return ret;
1802 }
1803
1804 int media_svc_insert_folder_begin(int data_cnt)
1805 {
1806         media_svc_debug("Transaction data count : [%d]", data_cnt);
1807
1808         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1809
1810         g_media_svc_insert_folder_data_cnt = data_cnt;
1811         g_media_svc_insert_folder_cur_data_cnt = 0;
1812
1813         return MS_MEDIA_ERR_NONE;
1814 }
1815
1816 int media_svc_insert_folder_end(uid_t uid)
1817 {
1818         int ret = MS_MEDIA_ERR_NONE;
1819
1820         media_svc_debug_fenter();
1821
1822         if (g_media_svc_insert_folder_cur_data_cnt > 0)
1823                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1824
1825         g_media_svc_insert_folder_data_cnt = 1;
1826         g_media_svc_insert_folder_cur_data_cnt = 0;
1827
1828         return ret;
1829 }
1830
1831 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1832 {
1833         int ret = MS_MEDIA_ERR_NONE;
1834         sqlite3 * db_handle = (sqlite3 *)handle;
1835         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1836
1837         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1838         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1839         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1840         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1841
1842         if (g_media_svc_insert_folder_data_cnt == 1) {
1843
1844                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1845
1846         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1847
1848                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1849                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1850
1851                 g_media_svc_insert_folder_cur_data_cnt++;
1852
1853         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1854
1855                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1856                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1857
1858                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1859                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1860
1861                 g_media_svc_insert_folder_cur_data_cnt = 0;
1862
1863         } else {
1864                 media_svc_error("Error in media_svc_set_insert_folder");
1865                 return MS_MEDIA_ERR_INTERNAL;
1866         }
1867
1868         return ret;
1869 }
1870
1871 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1872 {
1873         int ret = MS_MEDIA_ERR_NONE;
1874
1875         ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1876
1877         return ret;
1878 }
1879
1880 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1881 {
1882         int ret = MS_MEDIA_ERR_NONE;
1883         sqlite3 * db_handle = (sqlite3 *)handle;
1884
1885         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1886
1887         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1888
1889         return ret;
1890 }
1891
1892 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)
1893 {
1894         int ret = MS_MEDIA_ERR_NONE;
1895         sqlite3 * db_handle = (sqlite3 *)handle;
1896         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1897         media_svc_media_type_e media_type;
1898
1899         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1900         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1901         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1902         media_svc_retvm_if(__media_svc_check_storage(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1903
1904         media_svc_content_info_s content_info;
1905         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1906
1907         /*Set basic media info*/
1908         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1909         if (ret != MS_MEDIA_ERR_NONE) {
1910                 media_svc_error("_media_svc_set_media_info fail %d", ret);
1911                 return ret;
1912         }
1913
1914         /*Set or Get folder id*/
1915         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1916         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1917
1918         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1919         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1920
1921         if (g_media_svc_insert_item_data_cnt == 1) {
1922
1923                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1924                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1925
1926                 if (g_insert_with_noti)
1927                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1928         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1929
1930                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1931                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1932
1933                 if (g_insert_with_noti)
1934                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1935
1936                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1937                 g_media_svc_insert_item_cur_data_cnt++;
1938
1939         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1940
1941                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1942                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1943
1944                 if (g_insert_with_noti)
1945                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1946
1947                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1948                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1949
1950                 media_svc_debug("_media_svc_list_query_do over");
1951
1952                 if (g_insert_with_noti) {
1953                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
1954                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1955                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
1956
1957                         /* Recreate noti list */
1958                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
1959                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1960                 }
1961
1962                 g_media_svc_insert_item_cur_data_cnt = 0;
1963
1964         } else {
1965                 media_svc_error("Error in media_svc_insert_item_pass1");
1966                 _media_svc_destroy_content_info(&content_info);
1967                 return MS_MEDIA_ERR_INTERNAL;
1968         }
1969
1970         _media_svc_destroy_content_info(&content_info);
1971
1972         return MS_MEDIA_ERR_NONE;
1973 }
1974
1975 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)
1976 {
1977         int ret = MS_MEDIA_ERR_NONE;
1978         sqlite3 * db_handle = (sqlite3 *)handle;
1979         sqlite3_stmt *sql_stmt = NULL;
1980         media_svc_media_type_e media_type;
1981         media_svc_content_info_s content_info;
1982         GArray *db_data_array = NULL;
1983         media_svc_item_info_s *db_data = NULL;
1984         int idx = 0;
1985
1986         media_svc_debug_fenter();
1987
1988         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1989         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1990
1991         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
1992                 media_svc_error("storage type is incorrect[%d]", storage_type);
1993                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1994         }
1995
1996         db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
1997         if (db_data_array == NULL) {
1998                 media_svc_error("db_data_array is NULL. Out of memory");
1999                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2000         }
2001
2002         char *sql;
2003         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2004         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2005                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2006
2007         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2008                 media_svc_error("folder no recursive extract");
2009                 if (is_burst == 1)
2010                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2011                 else
2012                         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);
2013         } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2014                 media_svc_error("folder recursive extract");
2015                 if (is_burst == 1)
2016                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2017                 else
2018                         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);
2019         } else {
2020                 if (is_burst == 1)
2021                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2022                 else
2023                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2024         }
2025
2026         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2027         if (ret != MS_MEDIA_ERR_NONE) {
2028                 media_svc_error("error when get list. err = [%d]", ret);
2029                 g_array_free(db_data_array, FALSE);
2030                 return ret;
2031         }
2032
2033         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2034                 db_data = NULL;
2035                 db_data = malloc(sizeof(media_svc_item_info_s));
2036                 if (db_data == NULL) {
2037                         media_svc_error("Out of memory");
2038                         continue;
2039                 }
2040
2041                 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2042                 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2043
2044                 g_array_append_val(db_data_array, db_data);
2045         }
2046
2047         SQLITE3_FINALIZE(sql_stmt);
2048         media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2049
2050         while (db_data_array->len != 0) {
2051                 db_data = NULL;
2052                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2053                 g_array_remove_index(db_data_array, 0);
2054
2055                 if ((db_data == NULL) || (db_data->path == NULL)) {
2056                         media_svc_error("invalid db data");
2057                         continue;
2058                 }
2059
2060                 media_type = db_data->media_type;
2061                 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2062                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2063                 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2064
2065                 content_info.media_type = media_type;
2066                 content_info.storage_type = storage_type;
2067
2068                 _media_svc_set_default_value(&content_info, FALSE);
2069
2070                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2071                         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2072                         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2073                         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2074                         media_svc_debug("Do nothing[%d]", media_type);
2075                 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2076                         ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2077                 else
2078                         ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2079
2080                 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2081
2082                 _media_svc_destroy_content_info(&content_info);
2083                 SAFE_FREE(db_data->path);
2084                 SAFE_FREE(db_data);
2085                 idx++;
2086         }
2087
2088         if (idx > 0) {
2089                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2090                 if (0) {
2091                         _media_svc_publish_noti_list(idx);
2092                         _media_svc_destroy_noti_list(idx);
2093
2094                         /* Recreate noti list */
2095                         ret = _media_svc_create_noti_list(idx);
2096                 }
2097         }
2098
2099         while (db_data_array->len != 0) {
2100                 db_data = NULL;
2101                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2102                 g_array_remove_index(db_data_array, 0);
2103
2104                 if (db_data) {
2105                         SAFE_FREE(db_data->path);
2106                         free(db_data);
2107                         db_data = NULL;
2108                 }
2109         }
2110
2111         g_array_free(db_data_array, FALSE);
2112         db_data_array = NULL;
2113
2114         media_svc_debug_fleave();
2115
2116         return MS_MEDIA_ERR_NONE;
2117 }
2118
2119 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2120 {
2121         int ret = MS_MEDIA_ERR_NONE;
2122         sqlite3 * db_handle = (sqlite3 *)handle;
2123
2124         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2125         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2126
2127         ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2128
2129         return ret;
2130 }
2131
2132 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2133 {
2134         int ret = MS_MEDIA_ERR_NONE;
2135
2136         ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2137
2138         return ret;
2139 }
2140
2141 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2142 {
2143         int ret = MS_MEDIA_ERR_NONE;
2144         sqlite3 * db_handle = (sqlite3 *)handle;
2145         time_t modified_time = 0;
2146         int system_time = 0;
2147
2148         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2149
2150         ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2151
2152         system_time = _media_svc_get_file_time(path);
2153         media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2154
2155         if (system_time != modified_time && system_time != 0)
2156                 *modified = TRUE;
2157         else
2158                 *modified = FALSE;
2159
2160         return ret;
2161 }
2162
2163 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2164 {
2165         sqlite3 * db_handle = (sqlite3 *)handle;
2166
2167         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2168         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2169
2170         return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2171 }
2172
2173 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2174 {
2175         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2176         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2177
2178         return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2179 }
2180
2181 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2182 {
2183         int ret = MS_MEDIA_ERR_NONE;
2184         sqlite3 * db_handle = (sqlite3 *)handle;
2185
2186         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2187
2188         ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2189
2190         return ret;
2191 }
2192
2193 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2194 {
2195         int ret = MS_MEDIA_ERR_NONE;
2196         sqlite3 * db_handle = (sqlite3 *)handle;
2197         int count = -1;
2198
2199         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2200         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2201         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2202
2203         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2204         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2205
2206         if (count > 0) {
2207                 media_svc_debug("item is exist in database");
2208                 return MS_MEDIA_ERR_NONE;
2209         } else {
2210                 media_svc_debug("item is not exist in database");
2211                 return MS_MEDIA_ERR_DB_NO_RECORD;
2212         }
2213
2214         return MS_MEDIA_ERR_NONE;
2215 }
2216
2217 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2218 {
2219         int ret = MS_MEDIA_ERR_NONE;
2220         sqlite3 * db_handle = (sqlite3 *)handle;
2221         int cnt = -1;
2222
2223         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2224         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2225         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2226
2227         *count = 0;
2228         ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2229         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2230         *count = cnt;
2231
2232         if (cnt > 0) {
2233                 media_svc_debug("item is exist in database");
2234                 return MS_MEDIA_ERR_NONE;
2235         } else {
2236                 media_svc_debug("item is not exist in database");
2237                 return MS_MEDIA_ERR_DB_NO_RECORD;
2238         }
2239
2240         return MS_MEDIA_ERR_NONE;
2241 }
2242
2243 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2244 {
2245         int ret = MS_MEDIA_ERR_NONE;
2246         sqlite3 * db_handle = (sqlite3 *)handle;
2247
2248         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2249         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2250
2251         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2252
2253         return ret;
2254 }
2255
2256 int media_svc_append_query(const char *query, uid_t uid)
2257 {
2258         int ret = MS_MEDIA_ERR_NONE;
2259
2260         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2261
2262         ret = _media_svc_append_query_list(query, uid);
2263
2264         return ret;
2265 }
2266
2267 int media_svc_send_query(uid_t uid)
2268 {
2269         int ret = MS_MEDIA_ERR_NONE;
2270
2271         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2272
2273         return ret;
2274 }
2275
2276 int media_svc_get_media_type(const char *path, int *mediatype)
2277 {
2278         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2279
2280         return _media_svc_get_media_type(path, mediatype);
2281 }
2282
2283