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