Fix build warning
[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.bitrate = content_info->media_meta.bitrate;
1634         //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1635         //new_content_info->media_meta.channel = content_info->media_meta.channel;
1636         //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1637
1638         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1639                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1640         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1641                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1642         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1643                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1644
1645         new_content_info->media_meta.rating = content_info->media_meta.rating;
1646
1647         return 0;
1648 }
1649
1650 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1651 {
1652         int ret = MS_MEDIA_ERR_NONE;
1653         sqlite3 *db_handle = (sqlite3 *)handle;
1654         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1655         bool append_album = FALSE;
1656
1657         /* Checking parameters if they are valid */
1658         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1659         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1660         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1661
1662         if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1663                 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1664
1665                 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1666                         media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1667                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1668                 }
1669
1670                 if (content_info->size <= 0) {
1671                         media_svc_error("invalid size condition[%d]", content_info->size);
1672                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1673                 }
1674
1675                 if (content_info->modified_time <= 0) {
1676                         media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1677                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1678                 }
1679         }
1680
1681         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1682
1683         media_svc_content_info_s _new_content_info;
1684         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1685
1686         media_svc_media_type_e media_type;
1687
1688         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1689         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1690
1691         if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1692                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1693                         /*Do nothing.*/
1694                 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1695                         ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1696                 } else {
1697                         ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1698                 }
1699
1700                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1701
1702                 /* Extracting thumbnail */
1703                 int ini_val = _media_svc_get_ini_value();
1704                 if (ini_val == 1) {
1705                         if (_new_content_info.thumbnail_path == NULL) {
1706                                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1707                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1708                                         int width = 0;
1709                                         int height = 0;
1710
1711                                         ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1712                                         if (ret == MS_MEDIA_ERR_NONE) {
1713                                                 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1714                                         }
1715
1716                                         if (_new_content_info.media_meta.width <= 0)
1717                                                 _new_content_info.media_meta.width = width;
1718
1719                                         if (_new_content_info.media_meta.height <= 0)
1720                                                 _new_content_info.media_meta.height = height;
1721                                 }
1722                         }
1723                 }
1724         }
1725
1726         /* set othere data to the structure, which is passed as parameters */
1727         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1728
1729         /* Set or Get folder id */
1730         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);
1731         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1732
1733         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1734         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1735
1736         /* register album table data */
1737
1738         int album_id = 0;
1739         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1740                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1741
1742                 if (ret != MS_MEDIA_ERR_NONE) {
1743                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1744                                 media_svc_debug("album does not exist. So start to make album art");
1745                                 append_album = TRUE;
1746                         } else {
1747                                 media_svc_debug("other error");
1748                                 append_album = FALSE;
1749                         }
1750                 } else {
1751                         _new_content_info.album_id = album_id;
1752                         append_album = FALSE;
1753
1754                         if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1755                                 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1756
1757                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1758                         } else {
1759
1760                                 media_svc_debug("album already exists. don't need to make album art");
1761                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1762                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1763                         }
1764                 }
1765         }
1766
1767         if (append_album == TRUE) {
1768
1769                 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1770                         (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1771                         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);
1772                 else
1773                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1774
1775                 _new_content_info.album_id = album_id;
1776         }
1777
1778         /* Insert to db - calling _media_svc_insert_item_with_data */
1779         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1780
1781         if (ret == MS_MEDIA_ERR_NONE) {
1782                 media_svc_debug("Insertion is successful.");
1783         }
1784
1785         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1786                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1787         }
1788
1789         _media_svc_destroy_content_info(&_new_content_info);
1790
1791         /* handling returned value - important */
1792         return ret;
1793 }
1794
1795 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1796 {
1797         _media_svc_destroy_content_info(content_info);
1798 }
1799
1800 int media_svc_generate_uuid(char **uuid)
1801 {
1802         char *gen_uuid = NULL;
1803         gen_uuid = _media_info_generate_uuid();
1804         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1805
1806         *uuid = strdup(gen_uuid);
1807
1808         return MS_MEDIA_ERR_NONE;
1809 }
1810
1811 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1812 {
1813         sqlite3 * db_handle = (sqlite3 *)handle;
1814
1815         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1816
1817         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1818 }
1819
1820 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1821 {
1822         sqlite3 * db_handle = (sqlite3 *)handle;
1823
1824         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1825         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1826         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1827         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1828
1829         return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1830 }
1831
1832 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1833 {
1834         sqlite3 * db_handle = (sqlite3 *)handle;
1835
1836         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1837         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1838         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1839
1840         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1841 }
1842
1843 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)
1844 {
1845         int ret = MS_MEDIA_ERR_NONE;
1846         sqlite3 *db_handle = (sqlite3 *)handle;
1847
1848         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1849         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1850         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1851         media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1852
1853         ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1854         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1855
1856         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1857                 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1858                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1859
1860                 ret = _media_svc_update_media_view(db_handle, uid);
1861                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1862         }
1863
1864         return ret;
1865 }
1866
1867 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1868 {
1869         int ret = MS_MEDIA_ERR_NONE;
1870         sqlite3 *db_handle = (sqlite3 *)handle;
1871         media_svc_storage_type_e storage_type;
1872
1873         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1874         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1875
1876         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1877         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1878
1879         ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1880         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1881
1882         ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1883         if (ret != MS_MEDIA_ERR_NONE) {
1884                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1885         }
1886
1887         if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) || (storage_type == MEDIA_SVC_STORAGE_CLOUD)) {
1888                 ret = _media_svc_drop_media_table(storage_id, uid);
1889                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1890
1891                 ret = _media_svc_update_media_view(db_handle, uid);
1892                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1893         }
1894
1895         return ret;
1896 }
1897
1898 int media_svc_insert_folder_begin(int data_cnt)
1899 {
1900         media_svc_debug("Transaction data count : [%d]", data_cnt);
1901
1902         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1903
1904         g_media_svc_insert_folder_data_cnt = data_cnt;
1905         g_media_svc_insert_folder_cur_data_cnt = 0;
1906
1907         return MS_MEDIA_ERR_NONE;
1908 }
1909
1910 int media_svc_insert_folder_end(uid_t uid)
1911 {
1912         int ret = MS_MEDIA_ERR_NONE;
1913
1914         media_svc_debug_fenter();
1915
1916         if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1917
1918                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1919         }
1920
1921         g_media_svc_insert_folder_data_cnt = 1;
1922         g_media_svc_insert_folder_cur_data_cnt = 0;
1923
1924         return ret;
1925 }
1926
1927 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1928 {
1929         int ret = MS_MEDIA_ERR_NONE;
1930         sqlite3 * db_handle = (sqlite3 *)handle;
1931         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1932
1933         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1934         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1935         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1936         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1937
1938         if (g_media_svc_insert_folder_data_cnt == 1) {
1939
1940                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1941
1942         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1943
1944                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1945                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1946
1947                 g_media_svc_insert_folder_cur_data_cnt++;
1948
1949         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1950
1951                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1952                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1953
1954                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1955                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1956
1957                 g_media_svc_insert_folder_cur_data_cnt = 0;
1958
1959         } else {
1960                 media_svc_error("Error in media_svc_set_insert_folder");
1961                 return MS_MEDIA_ERR_INTERNAL;
1962         }
1963
1964         return ret;
1965 }
1966
1967 int media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
1968 {
1969         int ret = MS_MEDIA_ERR_NONE;
1970
1971         ret = _media_svc_delete_invalid_folder(storage_id, uid);
1972
1973         return ret;
1974 }
1975
1976 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1977 {
1978         int ret = MS_MEDIA_ERR_NONE;
1979         sqlite3 * db_handle = (sqlite3 *)handle;
1980
1981         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1982
1983         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1984
1985         return ret;
1986 }
1987
1988 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)
1989 {
1990         int ret = MS_MEDIA_ERR_NONE;
1991         sqlite3 * db_handle = (sqlite3 *)handle;
1992         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1993         media_svc_media_type_e media_type;
1994
1995         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1996         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1997         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1998         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1999
2000         media_svc_content_info_s content_info;
2001         memset(&content_info, 0, sizeof(media_svc_content_info_s));
2002
2003         /*Set basic media info*/
2004         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
2005         if (ret != MS_MEDIA_ERR_NONE) {
2006                 media_svc_error("_media_svc_set_media_info fail %d", ret);
2007                 return ret;
2008         }
2009
2010         //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);
2011
2012         /*Set or Get folder id*/
2013         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2014         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2015
2016         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2017         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2018
2019         if (g_media_svc_insert_item_data_cnt == 1) {
2020
2021                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2022                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2023
2024                 if (g_insert_with_noti)
2025                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2026         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2027
2028                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2029                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2030
2031                 if (g_insert_with_noti)
2032                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2033
2034                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2035                 g_media_svc_insert_item_cur_data_cnt++;
2036
2037         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2038
2039                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2040                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2041
2042                 if (g_insert_with_noti)
2043                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2044
2045                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2046                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2047
2048                 media_svc_debug("_media_svc_list_query_do over");
2049
2050                 if (g_insert_with_noti) {
2051                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2052                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2053                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2054
2055                         /* Recreate noti list */
2056                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2057                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2058                 }
2059
2060                 g_media_svc_insert_item_cur_data_cnt = 0;
2061
2062         } else {
2063                 media_svc_error("Error in media_svc_insert_item_pass1");
2064                 _media_svc_destroy_content_info(&content_info);
2065                 return MS_MEDIA_ERR_INTERNAL;
2066         }
2067
2068         _media_svc_destroy_content_info(&content_info);
2069
2070         return MS_MEDIA_ERR_NONE;
2071 }
2072
2073 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)
2074 {
2075         int ret = MS_MEDIA_ERR_NONE;
2076         sqlite3 * db_handle = (sqlite3 *)handle;
2077         sqlite3_stmt *sql_stmt = NULL;
2078         media_svc_media_type_e media_type;
2079         media_svc_content_info_s content_info;
2080         GArray *db_data_array = NULL;
2081         media_svc_item_info_s *db_data = NULL;
2082         int idx = 0;
2083
2084         media_svc_debug_fenter();
2085
2086         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2087         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2088
2089         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2090                 media_svc_error("storage type is incorrect[%d]", storage_type);
2091                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2092         }
2093
2094         db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2095         if (db_data_array == NULL) {
2096                 media_svc_error("db_data_array is NULL. Out of memory");
2097                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2098         }
2099
2100         char *sql;
2101         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2102         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
2103                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2104         }
2105
2106         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2107                 media_svc_error("folder no recursive extract");
2108                 if (is_burst == 1) {
2109                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2110                 } else {
2111                         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);
2112                 }
2113         } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2114                 media_svc_error("folder recursive extract");
2115                 if (is_burst == 1) {
2116                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2117                 } else {
2118                         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);
2119                 }
2120         } else {
2121                 if (is_burst == 1) {
2122                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2123                 } else {
2124                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2125                 }
2126         }
2127
2128         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2129         if (ret != MS_MEDIA_ERR_NONE) {
2130                 media_svc_error("error when get list. err = [%d]", ret);
2131                 g_array_free(db_data_array, FALSE);
2132                 return ret;
2133         }
2134
2135         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2136                 db_data = NULL;
2137                 db_data = malloc(sizeof(media_svc_item_info_s));
2138                 if (db_data == NULL) {
2139                         media_svc_error("Out of memory");
2140                         continue;
2141                 }
2142
2143                 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2144                 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2145
2146                 g_array_append_val(db_data_array, db_data);
2147         }
2148
2149         SQLITE3_FINALIZE(sql_stmt);
2150         media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2151
2152         while (db_data_array->len != 0) {
2153                 db_data = NULL;
2154                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2155                 g_array_remove_index(db_data_array, 0);
2156
2157                 if ((db_data == NULL) || (db_data->path == NULL)) {
2158                         media_svc_error("invalid db data");
2159                         continue;
2160                 }
2161
2162                 media_type = db_data->media_type;
2163                 //media_svc_debug("path is %s, media type %d", db_data->path, media_type);
2164                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2165                 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2166
2167                 content_info.media_type = media_type;
2168                 content_info.storage_type = storage_type;
2169
2170                 _media_svc_set_default_value(&content_info, FALSE);
2171
2172                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
2173                         /*Do nothing.*/
2174                 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
2175                         ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2176                 } else {
2177                         ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2178                 }
2179
2180                 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2181
2182                 _media_svc_destroy_content_info(&content_info);
2183                 SAFE_FREE(db_data->path);
2184                 SAFE_FREE(db_data);
2185                 idx++;
2186         }
2187
2188         if (idx > 0) {
2189                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2190                 if (0) {
2191                         _media_svc_publish_noti_list(idx);
2192                         _media_svc_destroy_noti_list(idx);
2193
2194                         /* Recreate noti list */
2195                         ret = _media_svc_create_noti_list(idx);
2196                 }
2197         }
2198         g_array_free(db_data_array, FALSE);
2199         db_data_array = NULL;
2200
2201         media_svc_debug_fleave();
2202
2203         return MS_MEDIA_ERR_NONE;
2204 }
2205
2206 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2207 {
2208         int ret = MS_MEDIA_ERR_NONE;
2209         sqlite3 * db_handle = (sqlite3 *)handle;
2210
2211         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2212         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2213
2214         ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2215
2216         return ret;
2217 }
2218
2219 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2220 {
2221         int ret = MS_MEDIA_ERR_NONE;
2222
2223         ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2224
2225         return ret;
2226 }
2227
2228 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2229 {
2230         int ret = MS_MEDIA_ERR_NONE;
2231         sqlite3 * db_handle = (sqlite3 *)handle;
2232         time_t modified_time = 0;
2233         int system_time = 0;
2234
2235         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2236
2237         ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2238
2239         system_time = _media_svc_get_file_time(path);
2240         media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2241
2242         if (system_time != modified_time && system_time != 0) {
2243                 *modified = TRUE;
2244         } else {
2245                 *modified = FALSE;
2246         }
2247
2248         return ret;
2249 }
2250
2251 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2252 {
2253         sqlite3 * db_handle = (sqlite3 *)handle;
2254
2255         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2256         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2257
2258         return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2259 }
2260
2261 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2262 {
2263         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2264         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2265
2266         return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2267 }
2268
2269 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2270 {
2271         int ret = MS_MEDIA_ERR_NONE;
2272         sqlite3 * db_handle = (sqlite3 *)handle;
2273
2274         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2275
2276         ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2277
2278         return ret;
2279 }
2280
2281 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2282 {
2283         int ret = MS_MEDIA_ERR_NONE;
2284         sqlite3 * db_handle = (sqlite3 *)handle;
2285         int count = -1;
2286
2287         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2288         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2289         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2290
2291         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2292         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2293
2294         if (count > 0) {
2295                 media_svc_debug("item is exist in database");
2296                 return MS_MEDIA_ERR_NONE;
2297         } else {
2298                 media_svc_debug("item is not exist in database");
2299                 return MS_MEDIA_ERR_DB_NO_RECORD;
2300         }
2301
2302         return MS_MEDIA_ERR_NONE;
2303 }
2304
2305 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2306 {
2307         int ret = MS_MEDIA_ERR_NONE;
2308         sqlite3 * db_handle = (sqlite3 *)handle;
2309         int cnt = -1;
2310
2311         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2312         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2313         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2314
2315         *count = 0;
2316         ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2317         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2318         *count = cnt;
2319
2320         if (cnt > 0) {
2321                 media_svc_debug("item is exist in database");
2322                 return MS_MEDIA_ERR_NONE;
2323         } else {
2324                 media_svc_debug("item is not exist in database");
2325                 return MS_MEDIA_ERR_DB_NO_RECORD;
2326         }
2327
2328         return MS_MEDIA_ERR_NONE;
2329 }
2330
2331 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2332 {
2333         int ret = MS_MEDIA_ERR_NONE;
2334         sqlite3 * db_handle = (sqlite3 *)handle;
2335
2336         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2337         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2338
2339         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2340
2341         return ret;
2342 }
2343
2344 int media_svc_append_query(const char *query, uid_t uid)
2345 {
2346         int ret = MS_MEDIA_ERR_NONE;
2347
2348         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2349
2350         ret = _media_svc_append_query_list(query, uid);
2351
2352         return ret;
2353 }
2354
2355 int media_svc_send_query(uid_t uid)
2356 {
2357         int ret = MS_MEDIA_ERR_NONE;
2358
2359         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2360
2361         return ret;
2362 }
2363