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