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