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