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