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