Apply coding rule
[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 = folder_id;
1201         } else {
1202                 uuid = 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
1209         return ret;
1210 }
1211
1212 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1213 {
1214         sqlite3 *db_handle = (sqlite3 *)handle;
1215
1216         media_svc_debug_fenter();
1217
1218         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1219         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1220         media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1221         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1222
1223         return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1224 }
1225
1226 int media_svc_check_db_upgrade(MediaSvcHandle *handle, bool *need_full_scan, int user_version, uid_t uid)
1227 {
1228         sqlite3 *db_handle = (sqlite3 *)handle;
1229
1230         media_svc_debug_fenter();
1231
1232         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1233
1234         return _media_svc_check_db_upgrade(db_handle, need_full_scan, user_version, uid);
1235 }
1236
1237 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1238 {
1239         sqlite3 *db_handle = (sqlite3 *)handle;
1240
1241         media_svc_debug_fenter();
1242
1243         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1244
1245         return _media_db_check_corrupt(db_handle);
1246 }
1247
1248 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)
1249 {
1250         sqlite3 *db_handle = (sqlite3 *)handle;
1251
1252         media_svc_debug_fenter();
1253
1254         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1255         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1256
1257         return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1258 }
1259
1260 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1261 {
1262         int ret = MS_MEDIA_ERR_NONE;
1263         sqlite3 *db_handle = (sqlite3 *)handle;
1264         time_t sto_time = 0;
1265         int cur_time = _media_svc_get_file_time(folder_path);
1266         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1267
1268         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1269
1270         ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1271         if (ret == MS_MEDIA_ERR_NONE) {
1272                 if (sto_time != cur_time) {
1273                         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1274                 }
1275         }
1276
1277         return ret;
1278 }
1279
1280 int media_svc_update_item_begin(int data_cnt)
1281 {
1282         media_svc_debug("Transaction data count : [%d]", data_cnt);
1283
1284         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1285
1286         g_media_svc_update_item_data_cnt = data_cnt;
1287         g_media_svc_update_item_cur_data_cnt = 0;
1288
1289         return MS_MEDIA_ERR_NONE;
1290 }
1291
1292 int media_svc_update_item_end(uid_t uid)
1293 {
1294         int ret = MS_MEDIA_ERR_NONE;
1295
1296         media_svc_debug_fenter();
1297
1298         if (g_media_svc_update_item_cur_data_cnt > 0) {
1299
1300                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1301         }
1302
1303         g_media_svc_update_item_data_cnt = 1;
1304         g_media_svc_update_item_cur_data_cnt = 0;
1305
1306         return ret;
1307 }
1308
1309 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1310 {
1311         int ret = MS_MEDIA_ERR_NONE;
1312         sqlite3 *db_handle = (sqlite3 *)handle;
1313
1314         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1315
1316         media_svc_media_type_e media_type;
1317         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1318         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1319
1320         media_svc_content_info_s content_info;
1321         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1322
1323         /*Set media info*/
1324         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1325         if (ret != MS_MEDIA_ERR_NONE) {
1326                 return ret;
1327         }
1328
1329         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1330                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1331         } else {
1332                 return MS_MEDIA_ERR_NONE;
1333         }
1334
1335         if (ret != MS_MEDIA_ERR_NONE) {
1336                 _media_svc_destroy_content_info(&content_info);
1337                 return ret;
1338         }
1339
1340         if (g_media_svc_update_item_data_cnt == 1) {
1341
1342                 ret = _media_svc_update_meta_with_data(&content_info);
1343                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1344
1345         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1346
1347                 ret = _media_svc_update_meta_with_data(&content_info);
1348                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1349
1350                 g_media_svc_update_item_cur_data_cnt++;
1351
1352         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1353
1354                 ret = _media_svc_update_meta_with_data(&content_info);
1355                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1356
1357                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1358                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1359
1360                 g_media_svc_update_item_cur_data_cnt = 0;
1361
1362         } else {
1363                 media_svc_error("Error in media_svc_update_item_meta");
1364                 _media_svc_destroy_content_info(&content_info);
1365                 return MS_MEDIA_ERR_INTERNAL;
1366         }
1367
1368         _media_svc_destroy_content_info(&content_info);
1369
1370         return ret;
1371 }
1372
1373 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)
1374 {
1375         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1376 }
1377
1378 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1379 {
1380         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1381
1382         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1383 }
1384
1385 int media_svc_check_pinyin_support(bool *support)
1386 {
1387         *support = _media_svc_check_pinyin_support();
1388
1389         return MS_MEDIA_ERR_NONE;
1390 }
1391
1392 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1393 {
1394         int ret = MS_MEDIA_ERR_NONE;
1395         sqlite3 * db_handle = (sqlite3 *)handle;
1396
1397         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1398
1399         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1400         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1401
1402         ret = _media_svc_update_media_view(db_handle, uid);
1403         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1404
1405         return ret;
1406 }
1407
1408 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id)
1409 {
1410         int ret = MS_MEDIA_ERR_NONE;
1411         sqlite3 * db_handle = (sqlite3 *)handle;
1412
1413         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1414         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1415
1416         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id);
1417
1418         return ret;
1419 }
1420
1421 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1422 {
1423         int ret = MS_MEDIA_ERR_NONE;
1424         sqlite3 * db_handle = (sqlite3 *)handle;
1425
1426         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1427         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1428
1429         ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1430
1431         return ret;
1432 }
1433
1434 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1435 {
1436         int ret = MS_MEDIA_ERR_NONE;
1437         sqlite3 * db_handle = (sqlite3 *)handle;
1438
1439         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1440         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1441
1442         ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1443
1444         return ret;
1445 }
1446
1447 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1448 {
1449         int ret = MS_MEDIA_ERR_NONE;
1450
1451         ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1452
1453         return ret;
1454 }
1455
1456 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1457 {
1458         sqlite3 * db_handle = (sqlite3 *)handle;
1459
1460         media_svc_debug_fenter();
1461
1462         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1463         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1464
1465         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1466 }
1467
1468 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1469 {
1470         int ret = MS_MEDIA_ERR_NONE;
1471
1472         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1473         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1474
1475         if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1476                 new_content_info->size = content_info->size;
1477                 new_content_info->modified_time = content_info->modified_time;
1478                 new_content_info->is_drm = content_info->is_drm;
1479                 new_content_info->media_type = content_info->media_type;
1480
1481                 if (STRING_VALID(content_info->mime_type)) {
1482                         ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1483                         if (ret != MS_MEDIA_ERR_NONE)
1484                                 media_svc_error("strcpy mime_type failed");
1485                 }
1486
1487                 if (STRING_VALID(content_info->thumbnail_path)) {
1488                         ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1489                         if (ret != MS_MEDIA_ERR_NONE)
1490                                 media_svc_error("strcpy thumbnail_path failed");
1491                 }
1492
1493                 new_content_info->media_meta.duration = content_info->media_meta.duration;
1494                 new_content_info->media_meta.width = content_info->media_meta.width;
1495                 new_content_info->media_meta.height = content_info->media_meta.height;
1496         }
1497
1498         if (content_info->added_time > 0)
1499                 new_content_info->added_time = content_info->added_time;
1500         new_content_info->last_played_time = content_info->last_played_time;
1501         new_content_info->played_count = content_info->played_count;
1502         new_content_info->favourate = content_info->favourate;
1503
1504         if (STRING_VALID(content_info->file_name)) {
1505                         ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1506                         if (ret != MS_MEDIA_ERR_NONE)
1507                                 media_svc_error("strcpy file_name failed");
1508         }
1509
1510         if (STRING_VALID(content_info->media_meta.title)) {
1511                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1512                 if (ret != MS_MEDIA_ERR_NONE)
1513                         media_svc_error("strcpy title failed");
1514         }
1515
1516         if (STRING_VALID(content_info->media_meta.album)) {
1517                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1518                 if (ret != MS_MEDIA_ERR_NONE)
1519                         media_svc_error("strcpy album failed");
1520         }
1521
1522         if (STRING_VALID(content_info->media_meta.artist)) {
1523                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1524                 if (ret != MS_MEDIA_ERR_NONE)
1525                         media_svc_error("strcpy artist failed");
1526         }
1527
1528         if (STRING_VALID(content_info->media_meta.genre)) {
1529                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1530                 if (ret != MS_MEDIA_ERR_NONE)
1531                         media_svc_error("strcpy genre failed");
1532         }
1533
1534         if (STRING_VALID(content_info->media_meta.composer)) {
1535                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1536                 if (ret != MS_MEDIA_ERR_NONE)
1537                         media_svc_error("strcpy composer failed");
1538         }
1539
1540         if (STRING_VALID(content_info->media_meta.year)) {
1541                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1542                 if (ret != MS_MEDIA_ERR_NONE)
1543                         media_svc_error("strcpy year failed");
1544         }
1545
1546         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1547                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1548                 if (ret != MS_MEDIA_ERR_NONE)
1549                         media_svc_error("strcpy recorded_date failed");
1550         }
1551
1552         if (STRING_VALID(content_info->media_meta.copyright)) {
1553                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1554                 if (ret != MS_MEDIA_ERR_NONE)
1555                         media_svc_error("strcpy copyright failed");
1556         }
1557
1558         if (STRING_VALID(content_info->media_meta.track_num)) {
1559                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1560                 if (ret != MS_MEDIA_ERR_NONE)
1561                         media_svc_error("strcpy track_num failed");
1562         }
1563
1564         if (STRING_VALID(content_info->media_meta.description)) {
1565                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1566                 if (ret != MS_MEDIA_ERR_NONE)
1567                         media_svc_error("strcpy description failed");
1568         }
1569
1570         if (STRING_VALID(content_info->media_meta.weather)) {
1571                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1572                 if (ret != MS_MEDIA_ERR_NONE)
1573                         media_svc_error("strcpy weather failed");
1574         }
1575
1576         if (STRING_VALID(content_info->media_meta.category)) {
1577                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1578                 if (ret != MS_MEDIA_ERR_NONE)
1579                         media_svc_error("strcpy category failed");
1580         }
1581
1582         if (STRING_VALID(content_info->media_meta.keyword)) {
1583                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1584                 if (ret != MS_MEDIA_ERR_NONE)
1585                         media_svc_error("strcpy keyword failed");
1586         }
1587
1588         if (STRING_VALID(content_info->media_meta.location_tag)) {
1589                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1590                 if (ret != MS_MEDIA_ERR_NONE)
1591                         media_svc_error("strcpy location_tag failed");
1592         }
1593
1594         if (STRING_VALID(content_info->media_meta.content_name)) {
1595                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1596                 if (ret != MS_MEDIA_ERR_NONE)
1597                         media_svc_error("strcpy content_name failed");
1598         }
1599
1600         if (STRING_VALID(content_info->media_meta.age_rating)) {
1601                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1602                 if (ret != MS_MEDIA_ERR_NONE)
1603                         media_svc_error("strcpy age_rating failed");
1604         }
1605
1606         if (STRING_VALID(content_info->media_meta.author)) {
1607                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1608                 if (ret != MS_MEDIA_ERR_NONE)
1609                         media_svc_error("strcpy author failed");
1610         }
1611
1612         if (STRING_VALID(content_info->media_meta.provider)) {
1613                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1614                 if (ret != MS_MEDIA_ERR_NONE)
1615                         media_svc_error("strcpy provider failed");
1616         }
1617
1618         if (STRING_VALID(content_info->media_meta.datetaken)) {
1619                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1620                 if (ret != MS_MEDIA_ERR_NONE)
1621                         media_svc_error("strcpy datetaken failed");
1622
1623                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1624                 if (new_content_info->timeline == 0) {
1625                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1626                         new_content_info->timeline = new_content_info->modified_time;
1627                 } else {
1628                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1629                 }
1630         }
1631
1632         //new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1633         //new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1634         //new_content_info->media_meta.channel = content_info->media_meta.channel;
1635         //new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1636
1637         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1638                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1639         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1640                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1641         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1642                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1643
1644         new_content_info->media_meta.rating = content_info->media_meta.rating;
1645
1646         return 0;
1647 }
1648
1649 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1650 {
1651         int ret = MS_MEDIA_ERR_NONE;
1652         sqlite3 *db_handle = (sqlite3 *)handle;
1653         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1654         bool append_album = FALSE;
1655
1656         /* Checking parameters if they are valid */
1657         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1658         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1659         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1660
1661         if (content_info->storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1662                 media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1663
1664                 if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1665                         media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1666                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1667                 }
1668
1669                 if (content_info->size <= 0) {
1670                         media_svc_error("invalid size condition[%d]", content_info->size);
1671                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1672                 }
1673
1674                 if (content_info->modified_time <= 0) {
1675                         media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1676                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1677                 }
1678         }
1679
1680         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1681
1682         media_svc_content_info_s _new_content_info;
1683         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1684
1685         media_svc_media_type_e media_type;
1686
1687         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1688         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1689
1690         if (content_info->storage_type != MEDIA_SVC_STORAGE_CLOUD) {
1691                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
1692                         /*Do nothing.*/
1693                 } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1694                         ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1695                 } else {
1696                         ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1697                 }
1698
1699                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1700
1701                 /* Extracting thumbnail */
1702                 int ini_val = _media_svc_get_ini_value();
1703                 if (ini_val == 1) {
1704                         if (_new_content_info.thumbnail_path == NULL) {
1705                                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1706                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1707                                         int width = 0;
1708                                         int height = 0;
1709
1710                                         ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1711                                         if (ret == MS_MEDIA_ERR_NONE) {
1712                                                 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1713                                         }
1714
1715                                         if (_new_content_info.media_meta.width <= 0)
1716                                                 _new_content_info.media_meta.width = width;
1717
1718                                         if (_new_content_info.media_meta.height <= 0)
1719                                                 _new_content_info.media_meta.height = height;
1720                                 }
1721                         }
1722                 }
1723         }
1724
1725         /* set othere data to the structure, which is passed as parameters */
1726         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1727
1728         /* Set or Get folder id */
1729         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);
1730         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1731
1732         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1733         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1734
1735         /* register album table data */
1736
1737         int album_id = 0;
1738         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1739                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1740
1741                 if (ret != MS_MEDIA_ERR_NONE) {
1742                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1743                                 media_svc_debug("album does not exist. So start to make album art");
1744                                 append_album = TRUE;
1745                         } else {
1746                                 media_svc_debug("other error");
1747                                 append_album = FALSE;
1748                         }
1749                 } else {
1750                         _new_content_info.album_id = album_id;
1751                         append_album = FALSE;
1752
1753                         if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1754                                 (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1755
1756                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1757                         } else {
1758
1759                                 media_svc_debug("album already exists. don't need to make album art");
1760                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1761                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1762                         }
1763                 }
1764         }
1765
1766         if (append_album == TRUE) {
1767
1768                 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1769                         (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1770                         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);
1771                 else
1772                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1773
1774                 _new_content_info.album_id = album_id;
1775         }
1776
1777         /* Insert to db - calling _media_svc_insert_item_with_data */
1778         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1779
1780         if (ret == MS_MEDIA_ERR_NONE) {
1781                 media_svc_debug("Insertion is successful.");
1782         }
1783
1784         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1785                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1786         }
1787
1788         _media_svc_destroy_content_info(&_new_content_info);
1789
1790         /* handling returned value - important */
1791         return ret;
1792 }
1793
1794 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1795 {
1796         _media_svc_destroy_content_info(content_info);
1797 }
1798
1799 int media_svc_generate_uuid(char **uuid)
1800 {
1801         char *gen_uuid = NULL;
1802         gen_uuid = _media_info_generate_uuid();
1803         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1804
1805         *uuid = strdup(gen_uuid);
1806
1807         return MS_MEDIA_ERR_NONE;
1808 }
1809
1810 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
1811 {
1812         sqlite3 * db_handle = (sqlite3 *)handle;
1813
1814         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1815
1816         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1817 }
1818
1819 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
1820 {
1821         sqlite3 * db_handle = (sqlite3 *)handle;
1822
1823         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1824         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1825         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1826         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1827
1828         return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity);
1829 }
1830
1831 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1832 {
1833         sqlite3 * db_handle = (sqlite3 *)handle;
1834
1835         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1836         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1837         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1838
1839         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1840 }
1841
1842 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)
1843 {
1844         int ret = MS_MEDIA_ERR_NONE;
1845         sqlite3 *db_handle = (sqlite3 *)handle;
1846
1847         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1848         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1849         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1850         media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1851
1852         ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1853         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1854
1855         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1856                 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1857                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1858
1859                 ret = _media_svc_update_media_view(db_handle, uid);
1860                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1861         }
1862
1863         return ret;
1864 }
1865
1866 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1867 {
1868         int ret = MS_MEDIA_ERR_NONE;
1869         sqlite3 *db_handle = (sqlite3 *)handle;
1870         media_svc_storage_type_e storage_type;
1871
1872         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1873         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1874
1875         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1876         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type_by_path failed : %d", ret);
1877
1878         ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1879         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1880
1881         ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1882         if (ret != MS_MEDIA_ERR_NONE) {
1883                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1884         }
1885
1886         if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) || (storage_type == MEDIA_SVC_STORAGE_CLOUD)) {
1887                 ret = _media_svc_drop_media_table(storage_id, uid);
1888                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1889
1890                 ret = _media_svc_update_media_view(db_handle, uid);
1891                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1892         }
1893
1894         return ret;
1895 }
1896
1897 int media_svc_insert_folder_begin(int data_cnt)
1898 {
1899         media_svc_debug("Transaction data count : [%d]", data_cnt);
1900
1901         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1902
1903         g_media_svc_insert_folder_data_cnt = data_cnt;
1904         g_media_svc_insert_folder_cur_data_cnt = 0;
1905
1906         return MS_MEDIA_ERR_NONE;
1907 }
1908
1909 int media_svc_insert_folder_end(uid_t uid)
1910 {
1911         int ret = MS_MEDIA_ERR_NONE;
1912
1913         media_svc_debug_fenter();
1914
1915         if (g_media_svc_insert_folder_cur_data_cnt > 0) {
1916
1917                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1918         }
1919
1920         g_media_svc_insert_folder_data_cnt = 1;
1921         g_media_svc_insert_folder_cur_data_cnt = 0;
1922
1923         return ret;
1924 }
1925
1926 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1927 {
1928         int ret = MS_MEDIA_ERR_NONE;
1929         sqlite3 * db_handle = (sqlite3 *)handle;
1930         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1931
1932         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1933         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1934         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1935         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1936
1937         if (g_media_svc_insert_folder_data_cnt == 1) {
1938
1939                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1940
1941         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1942
1943                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1944                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1945
1946                 g_media_svc_insert_folder_cur_data_cnt++;
1947
1948         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1949
1950                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1951                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1952
1953                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1954                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1955
1956                 g_media_svc_insert_folder_cur_data_cnt = 0;
1957
1958         } else {
1959                 media_svc_error("Error in media_svc_set_insert_folder");
1960                 return MS_MEDIA_ERR_INTERNAL;
1961         }
1962
1963         return ret;
1964 }
1965
1966 int media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
1967 {
1968         int ret = MS_MEDIA_ERR_NONE;
1969
1970         ret = _media_svc_delete_invalid_folder(storage_id, uid);
1971
1972         return ret;
1973 }
1974
1975 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1976 {
1977         int ret = MS_MEDIA_ERR_NONE;
1978         sqlite3 * db_handle = (sqlite3 *)handle;
1979
1980         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1981
1982         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1983
1984         return ret;
1985 }
1986
1987 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)
1988 {
1989         int ret = MS_MEDIA_ERR_NONE;
1990         sqlite3 * db_handle = (sqlite3 *)handle;
1991         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1992         media_svc_media_type_e media_type;
1993
1994         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1995         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1996         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1997         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1998
1999         media_svc_content_info_s content_info;
2000         memset(&content_info, 0, sizeof(media_svc_content_info_s));
2001
2002         /*Set basic media info*/
2003         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
2004         if (ret != MS_MEDIA_ERR_NONE) {
2005                 media_svc_error("_media_svc_set_media_info fail %d", ret);
2006                 return ret;
2007         }
2008
2009         //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);
2010
2011         /*Set or Get folder id*/
2012         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
2013         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2014
2015         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
2016         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2017
2018         if (g_media_svc_insert_item_data_cnt == 1) {
2019
2020                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
2021                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2022
2023                 if (g_insert_with_noti)
2024                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
2025         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
2026
2027                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2028                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2029
2030                 if (g_insert_with_noti)
2031                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2032
2033                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
2034                 g_media_svc_insert_item_cur_data_cnt++;
2035
2036         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
2037
2038                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
2039                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2040
2041                 if (g_insert_with_noti)
2042                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
2043
2044                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
2045                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2046
2047                 media_svc_debug("_media_svc_list_query_do over");
2048
2049                 if (g_insert_with_noti) {
2050                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2051                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2052                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2053
2054                         /* Recreate noti list */
2055                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2056                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2057                 }
2058
2059                 g_media_svc_insert_item_cur_data_cnt = 0;
2060
2061         } else {
2062                 media_svc_error("Error in media_svc_insert_item_pass1");
2063                 _media_svc_destroy_content_info(&content_info);
2064                 return MS_MEDIA_ERR_INTERNAL;
2065         }
2066
2067         _media_svc_destroy_content_info(&content_info);
2068
2069         return MS_MEDIA_ERR_NONE;
2070 }
2071
2072 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)
2073 {
2074         int ret = MS_MEDIA_ERR_NONE;
2075         sqlite3 * db_handle = (sqlite3 *)handle;
2076         sqlite3_stmt *sql_stmt = NULL;
2077         media_svc_media_type_e media_type;
2078         media_svc_content_info_s content_info;
2079         GArray *db_data_array = NULL;
2080         media_svc_item_info_s *db_data = NULL;
2081         int idx = 0;
2082
2083         media_svc_debug_fenter();
2084
2085         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2086         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2087
2088         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2089                 media_svc_error("storage type is incorrect[%d]", storage_type);
2090                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2091         }
2092
2093         db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2094         if (db_data_array == NULL) {
2095                 media_svc_error("db_data_array is NULL. Out of memory");
2096                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2097         }
2098
2099         char *sql;
2100         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2101         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2102         {
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, char *storage_id, 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