Use Common API in libmedia-util to get User path
[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         ms_user_storage_type_t type = -1;
253
254         ret = ms_user_get_storage_type(uid, path, &type);
255         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_storage_type 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 = NULL;
827                 char *external_thumb_path = NULL;
828
829                 ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
830                 ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
831
832                 if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
833                         media_svc_error("fail to get thumbnail path");
834                         SAFE_FREE(internal_thumb_path);
835                         SAFE_FREE(external_thumb_path);
836                         return MS_MEDIA_ERR_INTERNAL;
837                 }
838                 const char *dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
839
840                 /* remove thumbnails */
841                 if (STRING_VALID(dirpath))
842                         ret = _media_svc_remove_all_files_in_dir(dirpath);
843                 SAFE_FREE(internal_thumb_path);
844                 SAFE_FREE(external_thumb_path);
845                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
846         }
847
848         return MS_MEDIA_ERR_NONE;
849 }
850
851 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
852 {
853         sqlite3 *db_handle = (sqlite3 *)handle;
854
855         media_svc_debug_fenter();
856
857         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
858         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
859         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
860
861         /*Delete from DB and remove thumbnail files*/
862         return _media_svc_delete_invalid_items(db_handle, storage_id, storage_type, uid);
863 }
864
865 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
866 {
867         sqlite3 *db_handle = (sqlite3 *)handle;
868
869         media_svc_debug_fenter();
870
871         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
872         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
873
874         /*Delete from DB and remove thumbnail files*/
875         return _media_svc_delete_invalid_folder_items(db_handle, storage_id, folder_path, is_recursive, uid);
876 }
877
878 int media_svc_set_all_storage_items_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
879 {
880         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
881         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
882
883         return _media_svc_update_storage_item_validity(storage_id, storage_type, validity, uid);
884 }
885
886 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int validity, int recursive, uid_t uid)
887 {
888         sqlite3 *db_handle = (sqlite3 *)handle;
889
890         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
891         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
892         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
893
894         if (recursive)
895                 return _media_svc_update_recursive_folder_item_validity(storage_id, folder_path, validity, uid);
896         else
897                 return _media_svc_update_folder_item_validity(db_handle, storage_id, folder_path, validity, uid);
898 }
899
900 int media_svc_refresh_item(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
901 {
902         int ret = MS_MEDIA_ERR_NONE;
903         sqlite3 *db_handle = (sqlite3 *)handle;
904         media_svc_media_type_e media_type;
905
906         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
907         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
908         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
909         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
910
911         media_svc_content_info_s content_info;
912         memset(&content_info, 0, sizeof(media_svc_content_info_s));
913
914         /*Set media info*/
915         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, TRUE);
916         if (ret != MS_MEDIA_ERR_NONE)
917                 return ret;
918
919         /* Initialize thumbnail information to remake thumbnail. */
920         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
921         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
922         if (ret != MS_MEDIA_ERR_NONE) {
923                 _media_svc_destroy_content_info(&content_info);
924                 return ret;
925         }
926
927         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)) {
928                 ret = _media_svc_remove_file(thumb_path);
929                 if (ret != MS_MEDIA_ERR_NONE)
930                         media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
931         }
932
933         ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
934         if (ret != MS_MEDIA_ERR_NONE) {
935                 _media_svc_destroy_content_info(&content_info);
936                 return ret;
937         }
938
939         /* Get notification info */
940         media_svc_noti_item *noti_item = NULL;
941         ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
942         if (ret != MS_MEDIA_ERR_NONE) {
943                 _media_svc_destroy_content_info(&content_info);
944                 return ret;
945         }
946
947         media_type = noti_item->media_type;
948         content_info.media_type = media_type;
949
950         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
951         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
952         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
953         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
954                 media_svc_debug("Do nothing [%d]", media_type);
955         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
956                 ret = _media_svc_extract_image_metadata(db_handle, &content_info);
957         else
958                 ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
959
960         if (ret != MS_MEDIA_ERR_NONE) {
961                 _media_svc_destroy_noti_item(noti_item);
962                 _media_svc_destroy_content_info(&content_info);
963                 return ret;
964         }
965
966         /* Extracting thumbnail */
967         if (content_info.thumbnail_path == NULL) {
968                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
969                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
970                         int width = 0;
971                         int height = 0;
972
973                         ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
974                         if (ret == MS_MEDIA_ERR_NONE)
975                                 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
976
977                         if (content_info.media_meta.width <= 0)
978                                 content_info.media_meta.width = width;
979
980                         if (content_info.media_meta.height <= 0)
981                                 content_info.media_meta.height = height;
982                 }
983         }
984
985         ret = _media_svc_update_item_with_data(storage_id, &content_info, uid);
986
987         if (ret == MS_MEDIA_ERR_NONE) {
988                 media_svc_debug("Update is successful. Sending noti for this");
989                 _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);
990         } else {
991                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
992         }
993
994         _media_svc_destroy_content_info(&content_info);
995         _media_svc_destroy_noti_item(noti_item);
996
997         return ret;
998 }
999
1000 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
1001 {
1002         sqlite3 *db_handle = (sqlite3 *)handle;
1003         int ret = MS_MEDIA_ERR_NONE;
1004
1005         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1006         media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
1007         media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
1008
1009         media_svc_debug("Src path : %s, Dst Path : %s", src_path, dst_path);
1010
1011         ret = _media_svc_sql_begin_trans(uid);
1012         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1013
1014         /* Update all folder record's modified date, which are changed above */
1015         char *update_folder_modified_time_sql = NULL;
1016         time_t date;
1017         time(&date);
1018
1019         update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d WHERE path LIKE '%q';", date, dst_path);
1020
1021         ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1022         SQLITE3_SAFE_FREE(update_folder_modified_time_sql);
1023
1024         if (ret != SQLITE_OK) {
1025                 media_svc_error("failed to update folder modified time");
1026                 _media_svc_sql_rollback_trans(uid);
1027
1028                 return MS_MEDIA_ERR_DB_INTERNAL;
1029         }
1030
1031         /* Update all items */
1032         char *select_all_sql = NULL;
1033         sqlite3_stmt *sql_stmt = NULL;
1034         char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1035
1036         snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1037
1038         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);
1039
1040         media_svc_debug("[SQL query] : %s", select_all_sql);
1041
1042         ret = _media_svc_sql_prepare_to_step_simple(db_handle, select_all_sql, &sql_stmt);
1043         if (ret != MS_MEDIA_ERR_NONE) {
1044                 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1045                 _media_svc_sql_rollback_trans(uid);
1046                 return ret;
1047         }
1048
1049         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1050                 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1051                 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1052                 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1053                 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1054                 /*int media_type; */
1055                 bool no_thumb = FALSE;
1056
1057                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1058                         _strncpy_safe(media_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1059                 } else {
1060                         media_svc_error("media UUID is NULL");
1061                         return MS_MEDIA_ERR_DB_INTERNAL;
1062                 }
1063
1064                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1065                         _strncpy_safe(media_path, (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1066                 } else {
1067                         media_svc_error("media path is NULL");
1068                         return MS_MEDIA_ERR_DB_INTERNAL;
1069                 }
1070
1071                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1072                         _strncpy_safe(media_thumb_path, (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1073                 } else {
1074                         media_svc_debug("media thumb path doesn't exist in DB");
1075                         no_thumb = TRUE;
1076                 }
1077
1078                 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1079
1080                 /* Update path, thumbnail path of this item */
1081                 char *replaced_path = NULL;
1082                 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1083                 if (replaced_path == NULL) {
1084                         media_svc_error("_media_svc_replace_path failed");
1085                         SQLITE3_FINALIZE(sql_stmt);
1086                         _media_svc_sql_rollback_trans(uid);
1087                         return MS_MEDIA_ERR_INTERNAL;
1088                 }
1089
1090                 media_svc_debug("New media path : %s", replaced_path);
1091                 ms_user_storage_type_t storage_type = -1;
1092
1093                 if (!no_thumb) {
1094                         /* If old thumb path is default or not */
1095                         char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1096                         if (STRING_VALID(default_thumbnail_path) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) == 0)) {
1097                                 _strncpy_safe(media_new_thumb_path, default_thumbnail_path, sizeof(media_new_thumb_path));
1098                         } else {
1099                                 ret = ms_user_get_storage_type(uid, replaced_path, &storage_type);
1100                                 if (ret != MS_MEDIA_ERR_NONE) {
1101                                         media_svc_sec_error("ms_user_get_storage_type failed : [%d], path[%s] storage_type[%d]", ret, replaced_path, storage_type);
1102                                         SAFE_FREE(replaced_path);
1103                                         SAFE_FREE(default_thumbnail_path);
1104                                         _media_svc_sql_rollback_trans(uid);
1105                                         return ret;
1106                                 }
1107
1108                                 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1109                                 if (ret != MS_MEDIA_ERR_NONE) {
1110                                         media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1111                                         SAFE_FREE(replaced_path);
1112                                         SAFE_FREE(default_thumbnail_path);
1113                                         SQLITE3_FINALIZE(sql_stmt);
1114                                         _media_svc_sql_rollback_trans(uid);
1115                                         return ret;
1116                                 }
1117                         }
1118
1119                         SAFE_FREE(default_thumbnail_path);
1120
1121                         /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1122                 }
1123
1124                 char *update_item_sql = NULL;
1125
1126                 if (no_thumb) {
1127                         update_item_sql = sqlite3_mprintf("UPDATE '%q' SET path='%q' WHERE media_uuid='%q'", storage_id, replaced_path, media_uuid);
1128                 } else {
1129 #if 0
1130                         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)
1131                                 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);
1132                         else
1133                                 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);
1134 #else
1135                         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);
1136 #endif
1137                 }
1138
1139                 ret = media_db_request_update_db_batch(update_item_sql, uid);
1140                 SQLITE3_SAFE_FREE(update_item_sql);
1141                 SAFE_FREE(replaced_path);
1142
1143                 if (ret != SQLITE_OK) {
1144                         media_svc_error("failed to update item");
1145                         SQLITE3_FINALIZE(sql_stmt);
1146                         _media_svc_sql_rollback_trans(uid);
1147
1148                         return MS_MEDIA_ERR_DB_INTERNAL;
1149                 }
1150
1151                 /* Rename thumbnail file of file system */
1152                 char *default_thumbnail_path = _media_svc_get_thumb_default_path(uid);
1153                 if ((!no_thumb) && (default_thumbnail_path != NULL) && (strncmp(media_thumb_path, default_thumbnail_path, strlen(default_thumbnail_path)) != 0)) {
1154                         ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1155                         if (ret != MS_MEDIA_ERR_NONE)
1156                                 media_svc_error("_media_svc_rename_file failed : %d", ret);
1157                 }
1158
1159                 SAFE_FREE(default_thumbnail_path);
1160         }
1161
1162         SQLITE3_FINALIZE(sql_stmt);
1163
1164         ret = _media_svc_sql_end_trans(uid);
1165         if (ret != MS_MEDIA_ERR_NONE) {
1166                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
1167                 _media_svc_sql_rollback_trans(uid);
1168                 return ret;
1169         }
1170
1171         media_svc_debug("Folder update is successful. Sending noti for this");
1172         /* Get notification info */
1173         media_svc_noti_item *noti_item = NULL;
1174         ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1175         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1176
1177         _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1178         _media_svc_destroy_noti_item(noti_item);
1179
1180         return MS_MEDIA_ERR_NONE;
1181 }
1182
1183 int media_svc_request_update_db(const char *db_query, uid_t uid)
1184 {
1185         media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1186
1187         return _media_svc_sql_query(db_query, uid);
1188 }
1189
1190 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)
1191 {
1192         int ret = MS_MEDIA_ERR_NONE;
1193         sqlite3 *db_handle = (sqlite3 *)handle;
1194         char *uuid = NULL;
1195
1196         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1197         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1198
1199         /* Get notification info */
1200         media_svc_noti_item *noti_item = NULL;
1201         ret = _media_svc_get_noti_info(db_handle, storage_id, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1202         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1203
1204         if (folder_id != NULL) {
1205                 uuid = strndup(folder_id, strlen(folder_id));
1206         } else {
1207                 if (noti_item->media_uuid != NULL) {
1208                         uuid = strndup(noti_item->media_uuid, strlen(noti_item->media_uuid));
1209                 } else {
1210                         _media_svc_destroy_noti_item(noti_item);
1211                         media_svc_error("folder uuid is wrong");
1212                         return MS_MEDIA_ERR_DB_INTERNAL;
1213                 }
1214         }
1215
1216         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL, pid);
1217         ret = _media_svc_publish_dir_noti_v2(MS_MEDIA_ITEM_DIRECTORY, update_type, dir_path, -1, uuid, NULL, pid);
1218         _media_svc_destroy_noti_item(noti_item);
1219         SAFE_FREE(uuid);
1220
1221         return ret;
1222 }
1223
1224 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
1225 {
1226         sqlite3 *db_handle = (sqlite3 *)handle;
1227
1228         media_svc_debug_fenter();
1229
1230         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1231         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1232         media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1233         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1234
1235         return _media_svc_count_invalid_folder_items(db_handle, storage_id, folder_path, count);
1236 }
1237
1238 int media_svc_check_db_upgrade(MediaSvcHandle *handle, int user_version, uid_t uid)
1239 {
1240         sqlite3 *db_handle = (sqlite3 *)handle;
1241
1242         media_svc_debug_fenter();
1243
1244         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1245
1246         return _media_svc_check_db_upgrade(db_handle, user_version, uid);
1247 }
1248
1249 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1250 {
1251         sqlite3 *db_handle = (sqlite3 *)handle;
1252
1253         media_svc_debug_fenter();
1254
1255         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1256
1257         return _media_db_check_corrupt(db_handle);
1258 }
1259
1260 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)
1261 {
1262         sqlite3 *db_handle = (sqlite3 *)handle;
1263
1264         media_svc_debug_fenter();
1265
1266         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1267         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1268
1269         return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1270 }
1271
1272 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid)
1273 {
1274         int ret = MS_MEDIA_ERR_NONE;
1275         sqlite3 *db_handle = (sqlite3 *)handle;
1276         time_t sto_time = 0;
1277         int cur_time = _media_svc_get_file_time(folder_path);
1278         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1279
1280         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1281
1282         ret = _media_svc_get_folder_info_by_foldername(db_handle, storage_id, folder_path, folder_uuid, &sto_time);
1283         if (ret == MS_MEDIA_ERR_NONE) {
1284                 if (sto_time != cur_time)
1285                         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, FALSE, uid);
1286         }
1287
1288         return ret;
1289 }
1290
1291 int media_svc_update_item_begin(int data_cnt)
1292 {
1293         media_svc_debug("Transaction data count : [%d]", data_cnt);
1294
1295         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1296
1297         g_media_svc_update_item_data_cnt = data_cnt;
1298         g_media_svc_update_item_cur_data_cnt = 0;
1299
1300         return MS_MEDIA_ERR_NONE;
1301 }
1302
1303 int media_svc_update_item_end(uid_t uid)
1304 {
1305         int ret = MS_MEDIA_ERR_NONE;
1306
1307         media_svc_debug_fenter();
1308
1309         if (g_media_svc_update_item_cur_data_cnt > 0)
1310                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1311
1312         g_media_svc_update_item_data_cnt = 1;
1313         g_media_svc_update_item_cur_data_cnt = 0;
1314
1315         return ret;
1316 }
1317
1318 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, const char *storage_id, int storage_type, uid_t uid)
1319 {
1320         int ret = MS_MEDIA_ERR_NONE;
1321         sqlite3 *db_handle = (sqlite3 *)handle;
1322
1323         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1324
1325         media_svc_media_type_e media_type;
1326         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1327         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1328
1329         media_svc_content_info_s content_info;
1330         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1331
1332         /*Set media info*/
1333         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, file_path, &media_type, FALSE);
1334         if (ret != MS_MEDIA_ERR_NONE)
1335                 return ret;
1336
1337         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)
1338                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1339         else
1340                 return MS_MEDIA_ERR_NONE;
1341
1342         if (ret != MS_MEDIA_ERR_NONE) {
1343                 _media_svc_destroy_content_info(&content_info);
1344                 return ret;
1345         }
1346
1347         if (g_media_svc_update_item_data_cnt == 1) {
1348
1349                 ret = _media_svc_update_meta_with_data(&content_info);
1350                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1351
1352         } else if (g_media_svc_update_item_cur_data_cnt < (g_media_svc_update_item_data_cnt - 1)) {
1353
1354                 ret = _media_svc_update_meta_with_data(&content_info);
1355                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1356
1357                 g_media_svc_update_item_cur_data_cnt++;
1358
1359         } else if (g_media_svc_update_item_cur_data_cnt == (g_media_svc_update_item_data_cnt - 1)) {
1360
1361                 ret = _media_svc_update_meta_with_data(&content_info);
1362                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1363
1364                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1365                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1366
1367                 g_media_svc_update_item_cur_data_cnt = 0;
1368
1369         } else {
1370                 media_svc_error("Error in media_svc_update_item_meta");
1371                 _media_svc_destroy_content_info(&content_info);
1372                 return MS_MEDIA_ERR_INTERNAL;
1373         }
1374
1375         _media_svc_destroy_content_info(&content_info);
1376
1377         return ret;
1378 }
1379
1380 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)
1381 {
1382         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1383 }
1384
1385 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
1386 {
1387         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1388
1389         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1390 }
1391
1392 int media_svc_check_pinyin_support(bool *support)
1393 {
1394         *support = _media_svc_check_pinyin_support();
1395
1396         return MS_MEDIA_ERR_NONE;
1397 }
1398
1399 int media_svc_set_storage_validity(MediaSvcHandle *handle, const char *storage_id, int validity, uid_t uid)
1400 {
1401         int ret = MS_MEDIA_ERR_NONE;
1402         sqlite3 * db_handle = (sqlite3 *)handle;
1403
1404         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1405
1406         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
1407         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
1408
1409         ret = _media_svc_update_media_view(db_handle, uid);
1410         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1411
1412         return ret;
1413 }
1414
1415 int media_svc_get_storage_id(MediaSvcHandle *handle, const char *path, char *storage_id, uid_t uid)
1416 {
1417         int ret = MS_MEDIA_ERR_NONE;
1418         sqlite3 * db_handle = (sqlite3 *)handle;
1419
1420         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1421         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1422
1423         ret = _media_svc_get_storage_uuid(db_handle, path, storage_id, uid);
1424
1425         return ret;
1426 }
1427
1428 int media_svc_get_storage_path(MediaSvcHandle *handle, const char *storage_uuid, char **storage_path)
1429 {
1430         int ret = MS_MEDIA_ERR_NONE;
1431         sqlite3 * db_handle = (sqlite3 *)handle;
1432
1433         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1434         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1435
1436         ret = _media_svc_get_storage_path(db_handle, storage_uuid, storage_path);
1437
1438         return ret;
1439 }
1440
1441 int media_svc_get_storage_scan_status(MediaSvcHandle *handle, const char *storage_uuid, media_svc_scan_status_type_e *storage_status)
1442 {
1443         int ret = MS_MEDIA_ERR_NONE;
1444         sqlite3 * db_handle = (sqlite3 *)handle;
1445
1446         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1447         media_svc_retvm_if(storage_uuid == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_uuid is NULL");
1448
1449         ret = _media_svc_get_storage_scan_status(db_handle, storage_uuid, storage_status);
1450
1451         return ret;
1452 }
1453
1454 int media_svc_set_storage_scan_status(const char *storage_uuid, media_svc_scan_status_type_e storage_status, uid_t uid)
1455 {
1456         int ret = MS_MEDIA_ERR_NONE;
1457
1458         ret = _media_svc_set_storage_scan_status(storage_uuid, storage_status, uid);
1459
1460         return ret;
1461 }
1462
1463 int media_svc_get_storage_list(MediaSvcHandle *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
1464 {
1465         sqlite3 * db_handle = (sqlite3 *)handle;
1466
1467         media_svc_debug_fenter();
1468
1469         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1470         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1471
1472         return _media_svc_get_all_storage(db_handle, storage_list, storage_id_list, scan_status_list, count);
1473 }
1474
1475 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1476 {
1477         int ret = MS_MEDIA_ERR_NONE;
1478
1479         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1480         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1481
1482         if (content_info->added_time > 0)
1483                 new_content_info->added_time = content_info->added_time;
1484         new_content_info->last_played_time = content_info->last_played_time;
1485         new_content_info->played_count = content_info->played_count;
1486         new_content_info->favourate = content_info->favourate;
1487
1488         if (STRING_VALID(content_info->file_name)) {
1489                         ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, content_info->file_name);
1490                         if (ret != MS_MEDIA_ERR_NONE)
1491                                 media_svc_error("strcpy file_name failed");
1492         }
1493
1494         if (STRING_VALID(content_info->media_meta.title)) {
1495                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1496                 if (ret != MS_MEDIA_ERR_NONE)
1497                         media_svc_error("strcpy title failed");
1498         }
1499
1500         if (STRING_VALID(content_info->media_meta.album)) {
1501                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1502                 if (ret != MS_MEDIA_ERR_NONE)
1503                         media_svc_error("strcpy album failed");
1504         }
1505
1506         if (STRING_VALID(content_info->media_meta.artist)) {
1507                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1508                 if (ret != MS_MEDIA_ERR_NONE)
1509                         media_svc_error("strcpy artist failed");
1510         }
1511
1512         if (STRING_VALID(content_info->media_meta.genre)) {
1513                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1514                 if (ret != MS_MEDIA_ERR_NONE)
1515                         media_svc_error("strcpy genre failed");
1516         }
1517
1518         if (STRING_VALID(content_info->media_meta.composer)) {
1519                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1520                 if (ret != MS_MEDIA_ERR_NONE)
1521                         media_svc_error("strcpy composer failed");
1522         }
1523
1524         if (STRING_VALID(content_info->media_meta.year)) {
1525                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1526                 if (ret != MS_MEDIA_ERR_NONE)
1527                         media_svc_error("strcpy year failed");
1528         }
1529
1530         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1531                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1532                 if (ret != MS_MEDIA_ERR_NONE)
1533                         media_svc_error("strcpy recorded_date failed");
1534         }
1535
1536         if (STRING_VALID(content_info->media_meta.copyright)) {
1537                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1538                 if (ret != MS_MEDIA_ERR_NONE)
1539                         media_svc_error("strcpy copyright failed");
1540         }
1541
1542         if (STRING_VALID(content_info->media_meta.track_num)) {
1543                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1544                 if (ret != MS_MEDIA_ERR_NONE)
1545                         media_svc_error("strcpy track_num failed");
1546         }
1547
1548         if (STRING_VALID(content_info->media_meta.description)) {
1549                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1550                 if (ret != MS_MEDIA_ERR_NONE)
1551                         media_svc_error("strcpy description failed");
1552         }
1553
1554         if (STRING_VALID(content_info->media_meta.weather)) {
1555                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1556                 if (ret != MS_MEDIA_ERR_NONE)
1557                         media_svc_error("strcpy weather failed");
1558         }
1559
1560         if (STRING_VALID(content_info->media_meta.category)) {
1561                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.category, content_info->media_meta.category);
1562                 if (ret != MS_MEDIA_ERR_NONE)
1563                         media_svc_error("strcpy category failed");
1564         }
1565
1566         if (STRING_VALID(content_info->media_meta.keyword)) {
1567                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.keyword, content_info->media_meta.keyword);
1568                 if (ret != MS_MEDIA_ERR_NONE)
1569                         media_svc_error("strcpy keyword failed");
1570         }
1571
1572         if (STRING_VALID(content_info->media_meta.location_tag)) {
1573                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.location_tag, content_info->media_meta.location_tag);
1574                 if (ret != MS_MEDIA_ERR_NONE)
1575                         media_svc_error("strcpy location_tag failed");
1576         }
1577
1578         if (STRING_VALID(content_info->media_meta.content_name)) {
1579                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.content_name, content_info->media_meta.content_name);
1580                 if (ret != MS_MEDIA_ERR_NONE)
1581                         media_svc_error("strcpy content_name failed");
1582         }
1583
1584         if (STRING_VALID(content_info->media_meta.age_rating)) {
1585                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.age_rating, content_info->media_meta.age_rating);
1586                 if (ret != MS_MEDIA_ERR_NONE)
1587                         media_svc_error("strcpy age_rating failed");
1588         }
1589
1590         if (STRING_VALID(content_info->media_meta.author)) {
1591                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.author, content_info->media_meta.author);
1592                 if (ret != MS_MEDIA_ERR_NONE)
1593                         media_svc_error("strcpy author failed");
1594         }
1595
1596         if (STRING_VALID(content_info->media_meta.provider)) {
1597                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.provider, content_info->media_meta.provider);
1598                 if (ret != MS_MEDIA_ERR_NONE)
1599                         media_svc_error("strcpy provider failed");
1600         }
1601
1602         if (STRING_VALID(content_info->media_meta.datetaken)) {
1603                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1604                 if (ret != MS_MEDIA_ERR_NONE)
1605                         media_svc_error("strcpy datetaken failed");
1606
1607                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1608                 if (new_content_info->timeline == 0) {
1609                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1610                         new_content_info->timeline = new_content_info->modified_time;
1611                 } else {
1612                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1613                 }
1614         }
1615
1616         new_content_info->media_meta.is_360 = content_info->media_meta.is_360;
1617         /* new_content_info->media_meta.bitrate = content_info->media_meta.bitrate; */
1618         /* new_content_info->media_meta.samplerate = content_info->media_meta.samplerate; */
1619         /* new_content_info->media_meta.channel = content_info->media_meta.channel; */
1620         /* new_content_info->media_meta.orientation = content_info->media_meta.orientation; */
1621
1622         if (content_info->media_meta.longitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1623                 new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1624         if (content_info->media_meta.latitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1625                 new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1626         if (content_info->media_meta.altitude != MEDIA_SVC_DEFAULT_GPS_VALUE)
1627                 new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1628
1629         new_content_info->media_meta.rating = content_info->media_meta.rating;
1630
1631         return 0;
1632 }
1633
1634 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1635 {
1636         int ret = MS_MEDIA_ERR_NONE;
1637         sqlite3 *db_handle = (sqlite3 *)handle;
1638         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1639         bool append_album = FALSE;
1640
1641         /* Checking parameters if they are valid */
1642         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1643         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1644         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1645
1646         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1647
1648         media_svc_content_info_s _new_content_info;
1649         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1650
1651         media_svc_media_type_e media_type;
1652
1653         ret = _media_svc_set_media_info(&_new_content_info, content_info->storage_uuid, content_info->storage_type, content_info->path, &media_type, FALSE);
1654         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail _media_svc_set_media_info");
1655
1656         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER)
1657                 media_svc_debug("Do nothing[%d]", media_type);
1658         else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1659                 ret = _media_svc_extract_image_metadata(db_handle, &_new_content_info);
1660         else
1661                 ret = _media_svc_extract_media_metadata(db_handle, &_new_content_info, uid);
1662
1663         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1664
1665         /* Extracting thumbnail */
1666         if (_new_content_info.thumbnail_path == NULL) {
1667                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1668                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1669                         int width = 0;
1670                         int height = 0;
1671
1672                         ret = _media_svc_request_thumbnail_with_origin_size(_new_content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
1673                         if (ret == MS_MEDIA_ERR_NONE)
1674                                 ret = __media_svc_malloc_and_strncpy(&(_new_content_info.thumbnail_path), thumb_path);
1675
1676                         if (_new_content_info.media_meta.width <= 0)
1677                                 _new_content_info.media_meta.width = width;
1678
1679                         if (_new_content_info.media_meta.height <= 0)
1680                                 _new_content_info.media_meta.height = height;
1681                 }
1682         }
1683
1684         /* set othere data to the structure, which is passed as parameters */
1685         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1686
1687         /* Set or Get folder id */
1688         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);
1689         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1690
1691         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1692         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1693
1694         /* register album table data */
1695
1696         int album_id = 0;
1697         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1698                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1699
1700                 if (ret != MS_MEDIA_ERR_NONE) {
1701                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1702                                 media_svc_debug("album does not exist. So start to make album art");
1703                                 append_album = TRUE;
1704                         } else {
1705                                 media_svc_debug("other error");
1706                                 append_album = FALSE;
1707                         }
1708                 } else {
1709                         _new_content_info.album_id = album_id;
1710                         append_album = FALSE;
1711
1712                         if ((!g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1713                                 (!g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1714                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1715                         } else {
1716
1717                                 media_svc_debug("album already exists. don't need to make album art");
1718                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1719                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1720                         }
1721                 }
1722         }
1723
1724         if (append_album == TRUE) {
1725                 if ((g_strcmp0(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1726                         (g_strcmp0(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1727                         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);
1728                 else
1729                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1730
1731                 _new_content_info.album_id = album_id;
1732         }
1733
1734         /* Insert to db - calling _media_svc_insert_item_with_data */
1735         ret = _media_svc_insert_item_with_data(db_handle, _new_content_info.storage_uuid, &_new_content_info, FALSE, FALSE, uid);
1736
1737         if (ret == MS_MEDIA_ERR_NONE)
1738                 media_svc_debug("Insertion is successful.");
1739
1740         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL)
1741                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1742
1743         _media_svc_destroy_content_info(&_new_content_info);
1744
1745         /* handling returned value - important */
1746         return ret;
1747 }
1748
1749 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1750 {
1751         _media_svc_destroy_content_info(content_info);
1752 }
1753
1754 int media_svc_generate_uuid(char **uuid)
1755 {
1756         char *gen_uuid = NULL;
1757         gen_uuid = _media_info_generate_uuid();
1758         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
1759
1760         *uuid = strdup(gen_uuid);
1761
1762         return MS_MEDIA_ERR_NONE;
1763 }
1764
1765 int media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
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
1771         return _media_svc_get_mmc_info(db_handle, storage_name, storage_path, validity, info_exist);
1772 }
1773
1774 int media_svc_check_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, uid_t uid)
1775 {
1776         sqlite3 * db_handle = (sqlite3 *)handle;
1777
1778         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1779         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1780         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1781         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
1782
1783         return _media_svc_check_storage(db_handle, storage_id, storage_name, storage_path, validity, uid);
1784 }
1785
1786 int media_svc_update_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_path, uid_t uid)
1787 {
1788         sqlite3 * db_handle = (sqlite3 *)handle;
1789
1790         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1791         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1792         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1793
1794         return _media_svc_update_storage_path(db_handle, storage_id, storage_path, uid);
1795 }
1796
1797 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)
1798 {
1799         int ret = MS_MEDIA_ERR_NONE;
1800         sqlite3 *db_handle = (sqlite3 *)handle;
1801
1802         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1803         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1804         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1805         media_svc_retvm_if(__media_svc_check_storage(storage_type, TRUE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1806
1807         ret = _media_svc_append_storage(storage_id, storage_name, storage_path, storage_type, uid);
1808         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1809
1810         if (strcmp(storage_id, MEDIA_SVC_DB_TABLE_MEDIA)) {
1811                 ret = _media_svc_create_media_table_with_id(storage_id, uid);
1812                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1813
1814                 ret = _media_svc_update_media_view(db_handle, uid);
1815                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1816         }
1817
1818         return ret;
1819 }
1820
1821 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, const char *storage_name, uid_t uid)
1822 {
1823         int ret = MS_MEDIA_ERR_NONE;
1824         sqlite3 *db_handle = (sqlite3 *)handle;
1825         media_svc_storage_type_e storage_type;
1826
1827         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1828         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1829
1830         ret = _media_svc_get_storage_type(db_handle, storage_id, &storage_type);
1831         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_storage_type failed : %d", ret);
1832
1833         ret = _media_svc_delete_storage(storage_id, storage_name, uid);
1834         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1835
1836         ret = _media_svc_delete_folder_by_storage_id(storage_id, storage_type, uid);
1837         if (ret != MS_MEDIA_ERR_NONE)
1838                 media_svc_error("fail to _media_svc_delete_folder_by_storage_id. error : [%d]", ret);
1839
1840         if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
1841                 ret = _media_svc_drop_media_table(storage_id, uid);
1842                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1843
1844                 ret = _media_svc_update_media_view(db_handle, uid);
1845                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1846         }
1847
1848         return ret;
1849 }
1850
1851 int media_svc_insert_folder_begin(int data_cnt)
1852 {
1853         media_svc_debug("Transaction data count : [%d]", data_cnt);
1854
1855         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1856
1857         g_media_svc_insert_folder_data_cnt = data_cnt;
1858         g_media_svc_insert_folder_cur_data_cnt = 0;
1859
1860         return MS_MEDIA_ERR_NONE;
1861 }
1862
1863 int media_svc_insert_folder_end(uid_t uid)
1864 {
1865         int ret = MS_MEDIA_ERR_NONE;
1866
1867         media_svc_debug_fenter();
1868
1869         if (g_media_svc_insert_folder_cur_data_cnt > 0)
1870                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1871
1872         g_media_svc_insert_folder_data_cnt = 1;
1873         g_media_svc_insert_folder_cur_data_cnt = 0;
1874
1875         return ret;
1876 }
1877
1878 int media_svc_insert_folder(MediaSvcHandle *handle, const char *storage_id, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
1879 {
1880         int ret = MS_MEDIA_ERR_NONE;
1881         sqlite3 * db_handle = (sqlite3 *)handle;
1882         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1883
1884         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1885         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1886         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1887         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1888
1889         if (g_media_svc_insert_folder_data_cnt == 1) {
1890
1891                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, FALSE, uid);
1892
1893         } else if (g_media_svc_insert_folder_cur_data_cnt < (g_media_svc_insert_folder_data_cnt - 1)) {
1894
1895                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1896                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1897
1898                 g_media_svc_insert_folder_cur_data_cnt++;
1899
1900         } else if (g_media_svc_insert_folder_cur_data_cnt == (g_media_svc_insert_folder_data_cnt - 1)) {
1901
1902                 ret = _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, folder_uuid, TRUE, uid);
1903                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1904
1905                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_FOLDER, uid);
1906                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1907
1908                 g_media_svc_insert_folder_cur_data_cnt = 0;
1909
1910         } else {
1911                 media_svc_error("Error in media_svc_set_insert_folder");
1912                 return MS_MEDIA_ERR_INTERNAL;
1913         }
1914
1915         return ret;
1916 }
1917
1918 int media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
1919 {
1920         int ret = MS_MEDIA_ERR_NONE;
1921
1922         ret = _media_svc_delete_invalid_folder(storage_id, storage_type, uid);
1923
1924         return ret;
1925 }
1926
1927 int media_svc_set_folder_validity(MediaSvcHandle *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
1928 {
1929         int ret = MS_MEDIA_ERR_NONE;
1930         sqlite3 * db_handle = (sqlite3 *)handle;
1931
1932         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1933
1934         ret = _media_svc_set_folder_validity(db_handle, storage_id, start_path, validity, is_recursive, uid);
1935
1936         return ret;
1937 }
1938
1939 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)
1940 {
1941         int ret = MS_MEDIA_ERR_NONE;
1942         sqlite3 * db_handle = (sqlite3 *)handle;
1943         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
1944         media_svc_media_type_e media_type;
1945
1946         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1947         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1948         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1949         media_svc_retvm_if(__media_svc_check_storage(storage_type, FALSE) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
1950
1951         media_svc_content_info_s content_info;
1952         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1953
1954         /*Set basic media info*/
1955         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, &media_type, FALSE);
1956         if (ret != MS_MEDIA_ERR_NONE) {
1957                 media_svc_error("_media_svc_set_media_info fail %d", ret);
1958                 return ret;
1959         }
1960
1961         /*Set or Get folder id*/
1962         ret = _media_svc_get_and_append_folder_id_by_path(db_handle, storage_id, path, storage_type, folder_uuid, uid);
1963         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1964
1965         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
1966         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1967
1968         if (g_media_svc_insert_item_data_cnt == 1) {
1969
1970                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, FALSE, uid);
1971                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1972
1973                 if (g_insert_with_noti)
1974                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
1975         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
1976
1977                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1978                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1979
1980                 if (g_insert_with_noti)
1981                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1982
1983                 media_svc_debug("g_media_svc_insert_item_cur_data_cnt %d", g_media_svc_insert_item_cur_data_cnt);
1984                 g_media_svc_insert_item_cur_data_cnt++;
1985
1986         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
1987
1988                 ret = _media_svc_insert_item_pass1(db_handle, storage_id, &content_info, is_burst, TRUE, uid);
1989                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1990
1991                 if (g_insert_with_noti)
1992                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
1993
1994                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
1995                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1996
1997                 media_svc_debug("_media_svc_list_query_do over");
1998
1999                 if (g_insert_with_noti) {
2000                         media_svc_debug("publish noti list %d", g_media_svc_insert_item_cur_data_cnt);
2001                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2002                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
2003
2004                         /* Recreate noti list */
2005                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
2006                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
2007                 }
2008
2009                 g_media_svc_insert_item_cur_data_cnt = 0;
2010
2011         } else {
2012                 media_svc_error("Error in media_svc_insert_item_pass1");
2013                 _media_svc_destroy_content_info(&content_info);
2014                 return MS_MEDIA_ERR_INTERNAL;
2015         }
2016
2017         _media_svc_destroy_content_info(&content_info);
2018
2019         return MS_MEDIA_ERR_NONE;
2020 }
2021
2022 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)
2023 {
2024         int ret = MS_MEDIA_ERR_NONE;
2025         sqlite3 * db_handle = (sqlite3 *)handle;
2026         sqlite3_stmt *sql_stmt = NULL;
2027         media_svc_media_type_e media_type;
2028         media_svc_content_info_s content_info;
2029         GArray *db_data_array = NULL;
2030         media_svc_item_info_s *db_data = NULL;
2031         int idx = 0;
2032
2033         media_svc_debug_fenter();
2034
2035         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2036         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2037
2038         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB)) {
2039                 media_svc_error("storage type is incorrect[%d]", storage_type);
2040                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2041         }
2042
2043         db_data_array = g_array_new(FALSE, FALSE, sizeof(media_svc_item_info_s*));
2044         if (db_data_array == NULL) {
2045                 media_svc_error("db_data_array is NULL. Out of memory");
2046                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
2047         }
2048
2049         char *sql;
2050         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
2051         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
2052                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, extract_path, folder_uuid, uid);
2053
2054         if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && ret == MS_MEDIA_ERR_NONE) {
2055                 media_svc_error("folder no recursive extract");
2056                 if (is_burst == 1)
2057                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 AND title IS NULL and folder_uuid = '%q' ", storage_id, folder_uuid);
2058                 else
2059                         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);
2060         } else if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
2061                 media_svc_error("folder recursive extract");
2062                 if (is_burst == 1)
2063                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL and path LIKE '%q%%' ", storage_id, extract_path);
2064                 else
2065                         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);
2066         } else {
2067                 if (is_burst == 1)
2068                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL", storage_id);
2069                 else
2070                         sql = sqlite3_mprintf("SELECT path, media_type FROM '%s' WHERE validity = 1 and title IS NULL LIMIT %d", storage_id, BATCH_REQUEST_MAX);
2071         }
2072
2073         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
2074         if (ret != MS_MEDIA_ERR_NONE) {
2075                 media_svc_error("error when get list. err = [%d]", ret);
2076                 g_array_free(db_data_array, FALSE);
2077                 return ret;
2078         }
2079
2080         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2081                 db_data = NULL;
2082                 db_data = malloc(sizeof(media_svc_item_info_s));
2083                 if (db_data == NULL) {
2084                         media_svc_error("Out of memory");
2085                         continue;
2086                 }
2087
2088                 db_data->path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
2089                 db_data->media_type = (int)sqlite3_column_int(sql_stmt, 1);
2090
2091                 g_array_append_val(db_data_array, db_data);
2092         }
2093
2094         SQLITE3_FINALIZE(sql_stmt);
2095         media_svc_error("Insert Pass 2 get %d items!", db_data_array->len);
2096
2097         while (db_data_array->len != 0) {
2098                 db_data = NULL;
2099                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2100                 g_array_remove_index(db_data_array, 0);
2101
2102                 if ((db_data == NULL) || (db_data->path == NULL)) {
2103                         media_svc_error("invalid db data");
2104                         continue;
2105                 }
2106
2107                 media_type = db_data->media_type;
2108                 /* media_svc_debug("path is %s, media type %d", db_data->path, media_type); */
2109                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
2110                 __media_svc_malloc_and_strncpy(&content_info.path, db_data->path);
2111
2112                 content_info.media_type = media_type;
2113                 content_info.storage_type = storage_type;
2114
2115                 _media_svc_set_default_value(&content_info, FALSE);
2116
2117                 if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
2118                         || (media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
2119                         || (media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
2120                         || (media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
2121                         media_svc_debug("Do nothing[%d]", media_type);
2122                 else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
2123                         ret = _media_svc_extract_image_metadata(db_handle, &content_info);
2124                 else
2125                         ret = _media_svc_extract_media_metadata(db_handle, &content_info, uid);
2126
2127                 ret = _media_svc_insert_item_pass2(storage_id, &content_info, is_burst, TRUE, uid);
2128
2129                 _media_svc_destroy_content_info(&content_info);
2130                 SAFE_FREE(db_data->path);
2131                 SAFE_FREE(db_data);
2132                 idx++;
2133         }
2134
2135         if (idx > 0) {
2136                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
2137                 if (0) {
2138                         _media_svc_publish_noti_list(idx);
2139                         _media_svc_destroy_noti_list(idx);
2140
2141                         /* Recreate noti list */
2142                         ret = _media_svc_create_noti_list(idx);
2143                 }
2144         }
2145
2146         while (db_data_array->len != 0) {
2147                 db_data = NULL;
2148                 db_data = g_array_index(db_data_array, media_svc_item_info_s*, 0);
2149                 g_array_remove_index(db_data_array, 0);
2150
2151                 if (db_data) {
2152                         SAFE_FREE(db_data->path);
2153                         free(db_data);
2154                         db_data = NULL;
2155                 }
2156         }
2157
2158         g_array_free(db_data_array, FALSE);
2159         db_data_array = NULL;
2160
2161         media_svc_debug_fleave();
2162
2163         return MS_MEDIA_ERR_NONE;
2164 }
2165
2166 int media_svc_get_folder_scan_status(MediaSvcHandle *handle, const char *storage_id, const char *path, int *storage_status)
2167 {
2168         int ret = MS_MEDIA_ERR_NONE;
2169         sqlite3 * db_handle = (sqlite3 *)handle;
2170
2171         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2172         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2173
2174         ret = _media_svc_get_folder_scan_status(db_handle, storage_id, path, storage_status);
2175
2176         return ret;
2177 }
2178
2179 int media_svc_set_folder_scan_status(const char *storage_id, const char *path, int storage_status, uid_t uid)
2180 {
2181         int ret = MS_MEDIA_ERR_NONE;
2182
2183         ret = _media_svc_set_folder_scan_status(storage_id, path, storage_status, uid);
2184
2185         return ret;
2186 }
2187
2188 int media_svc_get_folder_modified_time(MediaSvcHandle *handle, const char *path, const char *storage_id, bool *modified)
2189 {
2190         int ret = MS_MEDIA_ERR_NONE;
2191         sqlite3 * db_handle = (sqlite3 *)handle;
2192         time_t modified_time = 0;
2193         int system_time = 0;
2194
2195         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2196
2197         ret = _media_svc_get_folder_modified_time_by_path(db_handle, path, storage_id, &modified_time);
2198
2199         system_time = _media_svc_get_file_time(path);
2200         media_svc_error("modified_time = [%d], system_time = [%d], path = [%s]", modified_time, system_time, path);
2201
2202         if (system_time != modified_time && system_time != 0)
2203                 *modified = TRUE;
2204         else
2205                 *modified = FALSE;
2206
2207         return ret;
2208 }
2209
2210 int media_svc_get_null_scan_folder_list(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, char ***folder_list, int *count)
2211 {
2212         sqlite3 * db_handle = (sqlite3 *)handle;
2213
2214         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2215         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
2216
2217         return _media_svc_get_null_scan_folder_list(db_handle, storage_id, folder_path, folder_list, count);
2218 }
2219
2220 int media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
2221 {
2222         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2223         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2224
2225         return _media_svc_change_validity_item_batch(storage_id, path, des_validity, src_validity, uid);
2226 }
2227
2228 int media_svc_delete_invalid_folder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
2229 {
2230         int ret = MS_MEDIA_ERR_NONE;
2231         sqlite3 * db_handle = (sqlite3 *)handle;
2232
2233         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2234
2235         ret = _media_svc_delete_invalid_folder_by_path(db_handle, storage_id, folder_path, uid, delete_count);
2236
2237         return ret;
2238 }
2239
2240 int media_svc_check_folder_exist_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path)
2241 {
2242         int ret = MS_MEDIA_ERR_NONE;
2243         sqlite3 * db_handle = (sqlite3 *)handle;
2244         int count = -1;
2245
2246         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2247         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2248         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2249
2250         ret = _media_svc_count_folder_with_path(db_handle, storage_id, folder_path, &count);
2251         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2252
2253         if (count > 0) {
2254                 media_svc_debug("item is exist in database");
2255                 return MS_MEDIA_ERR_NONE;
2256         } else {
2257                 media_svc_debug("item is not exist in database");
2258                 return MS_MEDIA_ERR_DB_NO_RECORD;
2259         }
2260
2261         return MS_MEDIA_ERR_NONE;
2262 }
2263
2264 int media_svc_check_subfolder_by_path(MediaSvcHandle *handle, const char *storage_id, const char *folder_path, int *count)
2265 {
2266         int ret = MS_MEDIA_ERR_NONE;
2267         sqlite3 * db_handle = (sqlite3 *)handle;
2268         int cnt = -1;
2269
2270         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2271         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
2272         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
2273
2274         *count = 0;
2275         ret = _media_svc_count_subfolder_with_path(db_handle, storage_id, folder_path, &cnt);
2276         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
2277         *count = cnt;
2278
2279         if (cnt > 0) {
2280                 media_svc_debug("item is exist in database");
2281                 return MS_MEDIA_ERR_NONE;
2282         } else {
2283                 media_svc_debug("item is not exist in database");
2284                 return MS_MEDIA_ERR_DB_NO_RECORD;
2285         }
2286
2287         return MS_MEDIA_ERR_NONE;
2288 }
2289
2290 int media_svc_get_folder_id(MediaSvcHandle *handle, const char *storage_id, const char *path, char *folder_id)
2291 {
2292         int ret = MS_MEDIA_ERR_NONE;
2293         sqlite3 * db_handle = (sqlite3 *)handle;
2294
2295         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
2296         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2297
2298         ret = _media_svc_get_folder_uuid(db_handle, storage_id, path, folder_id);
2299
2300         return ret;
2301 }
2302
2303 int media_svc_append_query(const char *query, uid_t uid)
2304 {
2305         int ret = MS_MEDIA_ERR_NONE;
2306
2307         media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
2308
2309         ret = _media_svc_append_query_list(query, uid);
2310
2311         return ret;
2312 }
2313
2314 int media_svc_send_query(uid_t uid)
2315 {
2316         int ret = MS_MEDIA_ERR_NONE;
2317
2318         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
2319
2320         return ret;
2321 }
2322
2323 int media_svc_get_media_type(const char *path, int *mediatype)
2324 {
2325         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
2326
2327         return _media_svc_get_media_type(path, mediatype);
2328 }
2329
2330