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