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