Improve msc_metadata_update
[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-svc.h"
25 #include "media-svc-media.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-svc-env.h"
30 #include "media-svc-media-folder.h"
31 #include "media-svc-album.h"
32 #include "media-svc-noti.h"
33 #include "media-svc-storage.h"
34
35 static __thread int g_media_svc_item_validity_data_cnt = 1;
36 static __thread int g_media_svc_item_validity_cur_data_cnt = 0;
37
38 static __thread int g_media_svc_insert_item_data_cnt = 1;
39 static __thread int g_media_svc_insert_item_cur_data_cnt = 0;
40
41 /* Flag for items to be published by notification */
42 static __thread bool g_insert_with_noti = false;
43
44 #define BATCH_REQUEST_MAX 300
45
46 int media_svc_get_user_version(sqlite3 *handle, int *user_version)
47 {
48         return _media_svc_get_user_version(handle, user_version);
49 }
50
51 int media_svc_create_table(uid_t uid)
52 {
53         int ret = MS_MEDIA_ERR_NONE;
54         char *sql = NULL;
55
56         media_svc_debug_fenter();
57
58         ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
59         if (ret != MS_MEDIA_ERR_NONE) {
60                 media_svc_error("_media_svc_init_table_query fail.");
61                 goto ERROR;
62         }
63
64         /*create media table*/
65         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
66         if (ret != MS_MEDIA_ERR_NONE) {
67                 media_svc_error("_media_svc_make_table_query fail.");
68                 goto ERROR;
69         }
70
71         /*create folder table*/
72         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
73         if (ret != MS_MEDIA_ERR_NONE) {
74                 media_svc_error("_media_svc_make_table_query fail.");
75                 goto ERROR;
76         }
77
78         /*create playlist_map table*/
79         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
80         if (ret != MS_MEDIA_ERR_NONE) {
81                 media_svc_error("_media_svc_make_table_query fail.");
82                 goto ERROR;
83         }
84
85         /*create playlist table*/
86         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
87         if (ret != MS_MEDIA_ERR_NONE) {
88                 media_svc_error("_media_svc_make_table_query fail.");
89                 goto ERROR;
90         }
91
92         /* create album table*/
93         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
94         if (ret != MS_MEDIA_ERR_NONE) {
95                 media_svc_error("_media_svc_make_table_query fail.");
96                 goto ERROR;
97         }
98
99         /*create tag_map table*/
100         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
101         if (ret != MS_MEDIA_ERR_NONE) {
102                 media_svc_error("_media_svc_make_table_query fail.");
103                 goto ERROR;
104         }
105
106         /*create tag table*/
107         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
108         if (ret != MS_MEDIA_ERR_NONE) {
109                 media_svc_error("_media_svc_make_table_query fail.");
110                 goto ERROR;
111         }
112
113         /*create bookmark table*/
114         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
115         if (ret != MS_MEDIA_ERR_NONE) {
116                 media_svc_error("_media_svc_make_table_query fail.");
117                 goto ERROR;
118         }
119
120         /*create storage table from tizen 2.4 */
121         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
122         if (ret != MS_MEDIA_ERR_NONE) {
123                 media_svc_error("_media_svc_make_table_query fail.");
124                 goto ERROR;
125         }
126
127         /*create face table. from tizen 3.0*/
128         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
129         if (ret != MS_MEDIA_ERR_NONE) {
130                 media_svc_error("_media_svc_make_table_query fail.");
131                 goto ERROR;
132         }
133
134         ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
135         if (ret != MS_MEDIA_ERR_NONE) {
136                 media_svc_error("_media_svc_make_table_query fail.");
137                 goto ERROR;
138         }
139
140         sql = sqlite3_mprintf("pragma user_version = %d;", LATEST_VERSION_NUMBER);
141         ret = _media_svc_sql_query(sql, uid);
142         if (ret != MS_MEDIA_ERR_NONE) {
143                 media_svc_error("user_version update fail.");
144                 goto ERROR;
145         }
146
147         _media_svc_destroy_table_query();
148
149         media_svc_debug_fleave();
150
151         return MS_MEDIA_ERR_NONE;
152 ERROR:
153         _media_svc_destroy_table_query();
154
155         media_svc_debug_fleave();
156
157         return ret;
158 }
159
160 int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
161 {
162         int ret = MS_MEDIA_ERR_NONE;
163         int count = -1;
164
165         ret = _media_svc_count_record_with_path(handle, storage_id, path, &count);
166         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
167
168         if (count > 0) {
169                 media_svc_debug("item is exist in database");
170                 return MS_MEDIA_ERR_NONE;
171         } else {
172                 media_svc_debug("item is not exist in database");
173                 return MS_MEDIA_ERR_DB_NO_RECORD;
174         }
175
176         return MS_MEDIA_ERR_NONE;
177 }
178
179 int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
180 {
181         return _media_svc_get_modified_time(handle, storage_id, path, modified_time);
182 }
183
184 int media_svc_insert_item_begin(int data_cnt, bool with_noti, int from_pid)
185 {
186         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
187
188         g_media_svc_insert_item_data_cnt = data_cnt;
189         g_media_svc_insert_item_cur_data_cnt = 0;
190
191         /* Prepare for making noti item list */
192         if (with_noti) {
193                 media_svc_debug("making noti list from pid[%d]", from_pid);
194                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE)
195                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
196
197                 _media_svc_set_noti_from_pid(from_pid);
198                 g_insert_with_noti = true;
199         }
200
201         return MS_MEDIA_ERR_NONE;
202 }
203
204 int media_svc_insert_item_end(uid_t uid)
205 {
206         int ret = MS_MEDIA_ERR_NONE;
207
208         media_svc_debug_fenter();
209
210         if (g_media_svc_insert_item_cur_data_cnt > 0) {
211
212                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
213                 if (g_insert_with_noti) {
214                         media_svc_debug("sending noti list");
215                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
216                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
217                         g_insert_with_noti = false;
218                         _media_svc_set_noti_from_pid(-1);
219                 }
220         }
221
222         g_media_svc_insert_item_data_cnt = 1;
223         g_media_svc_insert_item_cur_data_cnt = 0;
224
225         return ret;
226 }
227
228 int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
229 {
230         int ret = MS_MEDIA_ERR_NONE;
231         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
232
233         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
234         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
235         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
236         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
237
238         media_svc_content_info_s content_info;
239         memset(&content_info, 0, sizeof(media_svc_content_info_s));
240
241         /*Set media info*/
242         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
243         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
244         if (ret != MS_MEDIA_ERR_NONE)
245                 return ret;
246
247         if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
248         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
249         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
250         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
251                 media_svc_debug("Do nothing[%d]", content_info.media_type);
252         else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
253                 ret = _media_svc_extract_image_metadata(&content_info);
254         else
255                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
256         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
257
258         /*Set or Get folder id*/
259         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
260         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
261
262         content_info.folder_uuid = g_strdup(folder_uuid);
263         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
264
265         if (g_media_svc_insert_item_data_cnt == 1) {
266
267                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, false, uid);
268                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
269
270                 if (g_insert_with_noti)
271                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
272
273         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
274
275                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
276                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
277
278                 if (g_insert_with_noti)
279                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
280
281                 g_media_svc_insert_item_cur_data_cnt++;
282
283         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
284
285                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
286                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
287
288                 if (g_insert_with_noti)
289                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
290
291                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
292                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
293
294                 if (g_insert_with_noti) {
295                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
296                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
297
298                         /* Recreate noti list */
299                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
300                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
301                 }
302
303                 g_media_svc_insert_item_cur_data_cnt = 0;
304
305         } else {
306                 media_svc_error("Error in media_svc_insert_item_bulk");
307                 _media_svc_destroy_content_info(&content_info);
308                 return MS_MEDIA_ERR_INTERNAL;
309         }
310
311         _media_svc_destroy_content_info(&content_info);
312
313         return MS_MEDIA_ERR_NONE;
314 }
315
316 int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
317 {
318         int ret = MS_MEDIA_ERR_NONE;
319         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
320
321         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
322         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
323         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
324         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
325
326         media_svc_content_info_s content_info;
327         memset(&content_info, 0, sizeof(media_svc_content_info_s));
328
329         /*Set media info*/
330         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
331         if (ret != MS_MEDIA_ERR_NONE)
332                 return ret;
333
334         if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
335         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
336         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
337         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA)) {
338                 /*Do nothing.*/
339         } else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
340                 ret = _media_svc_extract_image_metadata(&content_info);
341         } else {
342                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
343         }
344
345         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
346
347         /*Set or Get folder id*/
348         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
349         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
350
351         content_info.folder_uuid = g_strdup(folder_uuid);
352         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
353
354         /* Extracting thumbnail */
355         if (content_info.thumbnail_path == NULL) {
356                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
357                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
358
359                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
360                         if (ret == MS_MEDIA_ERR_NONE)
361                                 content_info.thumbnail_path = g_strdup(thumb_path);
362                 }
363         }
364
365         ret = _media_svc_insert_item_with_data(false, storage_id, &content_info, false, uid);
366
367         if (ret == MS_MEDIA_ERR_NONE) {
368                 media_svc_debug("Insertion is successful. Sending noti for this");
369                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
370         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
371                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
372         }
373
374         _media_svc_destroy_content_info(&content_info);
375         return ret;
376 }
377
378 int media_svc_move_item(sqlite3 *handle,
379                                                                 const char *src_path,
380                                                                 const char *dest_path,
381                                                                 const char *media_id,
382                                                                 int media_type,
383                                                                 const char *mime_type,
384                                                                 uid_t uid)
385 {
386         int ret = MS_MEDIA_ERR_NONE;
387         char *file_name = NULL;
388         char *folder_path = NULL;
389         int modified_time = 0;
390         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
391         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
392         char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
393         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
394         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
395         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
396
397         media_svc_debug_fenter();
398
399         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
400         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
401         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
402         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
403         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
404
405         /* Get storage_id */
406         ret = _media_svc_get_storage_uuid(handle, src_path, org_stg_id, uid);
407         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
408         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
409         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
410         /* Get storage_type */
411         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
412         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
413         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
414         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
415
416         /*check and update folder*/
417         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
418         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
419
420         /*get filename*/
421         file_name = g_path_get_basename(dest_path);
422
423         /*get modified_time*/
424         modified_time = _media_svc_get_file_time(dest_path);
425
426         /*get old thumbnail_path and remove thumbnail */
427         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
428         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
429                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
430                 SAFE_FREE(file_name);
431                 return ret;
432         }
433
434         if (STRING_VALID(old_thumb_path)) {
435                 ret = _media_svc_remove_file(old_thumb_path);
436                 if (ret != MS_MEDIA_ERR_NONE)
437                         media_svc_error("_media_svc_remove_file failed : %d", ret);
438         }
439
440         /*move item*/
441         ret = _media_svc_update_item_by_path(org_stg_id, src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_uuid, uid);
442         SAFE_FREE(file_name);
443         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
444
445         media_svc_debug("Move is successful. Sending noti for this");
446         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
447
448         /*update folder modified_time*/
449         folder_path = g_path_get_dirname(dest_path);
450         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
451         SAFE_FREE(folder_path);
452         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
453
454         return MS_MEDIA_ERR_NONE;
455 }
456
457 int media_svc_set_item_validity_begin(int data_cnt)
458 {
459         media_svc_debug("Transaction data count : [%d]", data_cnt);
460
461         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
462
463         g_media_svc_item_validity_data_cnt = data_cnt;
464         g_media_svc_item_validity_cur_data_cnt = 0;
465
466         return MS_MEDIA_ERR_NONE;
467 }
468
469 int media_svc_set_item_validity_end(uid_t uid)
470 {
471         int ret = MS_MEDIA_ERR_NONE;
472
473         media_svc_debug_fenter();
474
475         if (g_media_svc_item_validity_cur_data_cnt > 0)
476                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
477
478         g_media_svc_item_validity_data_cnt = 1;
479         g_media_svc_item_validity_cur_data_cnt = 0;
480
481         return ret;
482 }
483
484 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
485 {
486         int ret = MS_MEDIA_ERR_NONE;
487
488         if (g_media_svc_item_validity_data_cnt == 1) {
489
490                 return _media_svc_update_item_validity(storage_id, path, validity, false, uid);
491
492         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
493
494                 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
495                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
496
497                 g_media_svc_item_validity_cur_data_cnt++;
498
499         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
500
501                 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
502                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
503
504                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
505                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
506
507                 g_media_svc_item_validity_cur_data_cnt = 0;
508
509         } else {
510
511                 media_svc_error("Error in media_svc_set_item_validity");
512                 return MS_MEDIA_ERR_INTERNAL;
513         }
514
515         return MS_MEDIA_ERR_NONE;
516 }
517
518 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
519 {
520         int ret = MS_MEDIA_ERR_NONE;
521         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
522         media_svc_noti_item *noti_item = NULL;
523
524         media_svc_debug_fenter();
525
526         /*Get thumbnail path to delete*/
527         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
528         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
529
530         /* Get notification info */
531         ret = _media_svc_get_noti_info(handle, storage_id, path, &noti_item);
532         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
533
534         /*Delete item*/
535         ret = _media_svc_delete_item_by_path(storage_id, path, uid);
536         if (ret != MS_MEDIA_ERR_NONE) {
537                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
538                 _media_svc_destroy_noti_item(noti_item);
539
540                 return ret;
541         }
542
543         /* Send notification */
544         media_svc_debug("Deletion is successful. Sending noti for this");
545         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
546         _media_svc_destroy_noti_item(noti_item);
547
548         /*Delete thumbnail*/
549         if (STRING_VALID(thumb_path)) {
550                 ret = _media_svc_remove_file(thumb_path);
551                 if (ret != MS_MEDIA_ERR_NONE)
552                         media_svc_error("fail to remove thumbnail file.");
553         }
554
555         return MS_MEDIA_ERR_NONE;
556 }
557
558 int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
559 {
560         int ret = MS_MEDIA_ERR_NONE;
561         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
562
563         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
564         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
565         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
566         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
567
568         media_svc_content_info_s content_info;
569         memset(&content_info, 0, sizeof(media_svc_content_info_s));
570
571         /*Set media info*/
572         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
573         if (ret != MS_MEDIA_ERR_NONE)
574                 return ret;
575
576         /* Initialize thumbnail information to remake thumbnail. */
577         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
578         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
579                 _media_svc_destroy_content_info(&content_info);
580                 return ret;
581         }
582
583         if (STRING_VALID(thumb_path)) {
584                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
585                         ret = _media_svc_remove_file(thumb_path);
586                         if (ret != MS_MEDIA_ERR_NONE)
587                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
588                 }
589
590                 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
591                 if (ret != MS_MEDIA_ERR_NONE) {
592                         _media_svc_destroy_content_info(&content_info);
593                         return ret;
594                 }
595         }
596
597         /* Get notification info */
598         media_svc_noti_item *noti_item = NULL;
599         ret = _media_svc_get_noti_info(handle, storage_id, path, &noti_item);
600         if (ret != MS_MEDIA_ERR_NONE) {
601                 _media_svc_destroy_content_info(&content_info);
602                 return ret;
603         }
604
605         content_info.media_type = noti_item->media_type;
606
607         if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
608         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
609         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
610         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
611                 media_svc_debug("Do nothing [%d]", content_info.media_type);
612         else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
613                 ret = _media_svc_extract_image_metadata(&content_info);
614         else
615                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
616
617         if (ret != MS_MEDIA_ERR_NONE) {
618                 _media_svc_destroy_noti_item(noti_item);
619                 _media_svc_destroy_content_info(&content_info);
620                 return ret;
621         }
622
623         /* Extracting thumbnail */
624         if (content_info.thumbnail_path == NULL) {
625                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
626                         memset(thumb_path, 0, sizeof(thumb_path));
627
628                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
629                         if (ret == MS_MEDIA_ERR_NONE)
630                                 content_info.thumbnail_path = g_strdup(thumb_path);
631                 }
632         }
633
634         ret = _media_svc_update_item_with_data(is_direct, storage_id, &content_info, uid);
635
636         if (ret == MS_MEDIA_ERR_NONE) {
637                 media_svc_debug("Update is successful. Sending noti for this");
638                 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
639         } else {
640                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
641         }
642
643         _media_svc_destroy_content_info(&content_info);
644         _media_svc_destroy_noti_item(noti_item);
645
646         return ret;
647 }
648
649 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
650 {
651         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
652
653         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
654 }
655
656 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
657 {
658         media_svc_debug_fenter();
659
660         return _media_svc_check_db_upgrade(handle, user_version, uid);
661 }
662
663 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
664 {
665         int ret = MS_MEDIA_ERR_NONE;
666         char *root_path = NULL;
667         GArray *path_list = NULL;
668
669         ms_user_get_internal_root_path(uid, &root_path);
670         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
671         if (ret != MS_MEDIA_ERR_NONE)
672                 media_svc_error("Fail to send noti");
673
674         SAFE_FREE(root_path);
675
676         path_list = g_array_new(FALSE, FALSE, sizeof(char *));
677         _media_svc_get_storage_path(handle, &path_list);
678
679         while (path_list->len != 0) {
680                 root_path = g_array_index(path_list , char *, 0);
681                 g_array_remove_index(path_list, 0);
682
683                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
684                 if (ret != MS_MEDIA_ERR_NONE)
685                         media_svc_error("Fail to send noti");
686
687                 SAFE_FREE(root_path);
688         }
689
690         g_array_free(path_list, false);
691 }
692
693 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
694 {
695         int ret = MS_MEDIA_ERR_NONE;
696         /* NOTICE : After 6.0, change 'media_view' to 'media', add 'AND validity=1', and no need storage id */
697         char *sql = NULL;
698         media_svc_content_info_s content_info;
699         media_svc_file_s *file_info = NULL;
700         GArray *path_list = NULL;
701
702         path_list = g_array_new(FALSE, FALSE, sizeof(media_svc_file_s *));
703         media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
704
705         sql = sqlite3_mprintf("SELECT media_path, storage_uuid FROM %q WHERE media_type=3", MEDIA_SVC_DB_VIEW_MEDIA);
706         ret = _media_svc_get_media(handle, sql, &path_list);
707         if (ret != MS_MEDIA_ERR_NONE) {
708                 media_svc_error("Fail to get media list");
709                 g_array_free(path_list, false);
710                 return ret;
711         }
712
713         while (path_list->len != 0) {
714                 file_info = g_array_index(path_list, media_svc_file_s *, 0);
715                 g_array_remove_index(path_list, 0);
716
717                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
718                 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_info->storage_id, file_info->path);
719                 if (ret != MS_MEDIA_ERR_NONE) {
720                         media_svc_error("Fail to extract metadata");
721                         _media_svc_destroy_content_info(&content_info);
722                         SAFE_FREE(file_info);
723                         continue;
724                 }
725
726                 ret = _media_svc_update_meta_with_data(&content_info);
727                 if (ret != MS_MEDIA_ERR_NONE)
728                         media_svc_error("Fail to append item[%s]", content_info.path);
729
730                 _media_svc_destroy_content_info(&content_info);
731                 SAFE_FREE(file_info);
732         }
733
734         g_array_free(path_list, false);
735
736         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
737         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
738
739         /* Noti for this */
740         __media_svc_noti_all_storage(handle, uid);
741
742         return ret;
743 }
744
745 int media_svc_publish_noti(media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
746 {
747         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
748 }
749
750 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
751 {
752         return _media_svc_get_pinyin_str(src_str, pinyin_str);
753 }
754
755 int media_svc_check_pinyin_support(bool *support)
756 {
757         *support = _media_svc_check_pinyin_support();
758
759         return MS_MEDIA_ERR_NONE;
760 }
761
762 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
763 {
764         int ret = MS_MEDIA_ERR_NONE;
765
766         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
767
768         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
769         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
770
771         ret = _media_svc_update_media_view(handle, uid);
772         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
773
774         return ret;
775 }
776
777 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
778 {
779         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
780 }
781
782 int media_svc_generate_uuid(char **uuid)
783 {
784         char *gen_uuid = NULL;
785         gen_uuid = _media_info_generate_uuid();
786         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
787
788         *uuid = strdup(gen_uuid);
789
790         return MS_MEDIA_ERR_NONE;
791 }
792
793 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
794 {
795         return _media_svc_check_storage(handle, storage_id, storage_path, validity, uid);
796 }
797
798 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
799 {
800         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
801 }
802
803 int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
804 {
805         int ret = MS_MEDIA_ERR_NONE;
806
807         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
808         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
809         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
810         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
811
812         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
813         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
814
815         ret = _media_svc_create_media_table_with_id(storage_id, uid);
816         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
817
818         ret = _media_svc_update_media_view(handle, uid);
819         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
820
821         /* Remove external storage that validity is 0 */
822         ret = _media_svc_delete_invalid_storage(handle, uid);
823         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
824
825         return ret;
826 }
827
828 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
829 {
830         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
831         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
832         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
833         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
834
835         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
836 }
837
838 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
839 {
840         return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
841 }
842
843 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
844 {
845         int ret = MS_MEDIA_ERR_NONE;
846         int count = -1;
847
848         ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
849         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
850
851         if (count > 0) {
852                 media_svc_debug("item is exist in database");
853                 return MS_MEDIA_ERR_NONE;
854         } else {
855                 media_svc_debug("item is not exist in database");
856                 return MS_MEDIA_ERR_DB_NO_RECORD;
857         }
858
859         return MS_MEDIA_ERR_NONE;
860 }
861
862 int media_svc_append_query(const char *query, uid_t uid)
863 {
864         return _media_svc_append_query_list(query, uid);
865 }
866
867 int media_svc_send_query(uid_t uid)
868 {
869         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
870 }
871
872 int media_svc_get_media_type(const char *path, int *mediatype)
873 {
874         return _media_svc_get_media_type(path, mediatype);
875 }
876
877 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
878 {
879         int ret = MS_MEDIA_ERR_NONE;
880         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
881         char *sql = NULL;
882
883         // 1. Check media type
884         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
885                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
886
887         // 2. try to create thumbnail
888         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
889         if (ret != MS_MEDIA_ERR_NONE) {
890                 media_svc_error("Failed to create thumbnail [%d]", ret);
891                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
892                         return ret;
893         }
894
895         // 3. Update creation result to media db
896         sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path='%q' WHERE media_path='%q';", storage_id, thumb_path, file_path);
897
898         ret = _media_svc_sql_query(sql, uid);
899         SQLITE3_SAFE_FREE(sql);
900         if (ret != MS_MEDIA_ERR_NONE) {
901                 media_svc_error("Failed to update media db [%d]", ret);
902                 *thumbnail_path = g_strdup("");
903         } else {
904                 *thumbnail_path = g_strdup(thumb_path);
905         }
906
907         return ret;
908 }