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