Fix Coverity issues
[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         switch (content_info.media_type) {
248         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
249                 ret = _media_svc_extract_image_metadata(&content_info);
250                 break;
251         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
252         case MEDIA_SVC_MEDIA_TYPE_SOUND:
253         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
254                 ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
255                 break;
256         default:
257                 media_svc_debug("Do nothing[%d]", content_info.media_type);
258                 break;
259         }
260
261         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
262
263         /*Set or Get folder id*/
264         ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, folder_uuid, uid);
265         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
266
267         content_info.folder_uuid = g_strdup(folder_uuid);
268         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
269
270         if (g_media_svc_insert_item_data_cnt == 1) {
271
272                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, false, uid);
273                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
274
275                 if (g_insert_with_noti)
276                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
277
278         } else if (g_media_svc_insert_item_cur_data_cnt < (g_media_svc_insert_item_data_cnt - 1)) {
279
280                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
281                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
282
283                 if (g_insert_with_noti)
284                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
285
286                 g_media_svc_insert_item_cur_data_cnt++;
287
288         } else if (g_media_svc_insert_item_cur_data_cnt == (g_media_svc_insert_item_data_cnt - 1)) {
289
290                 ret = _media_svc_insert_item_with_data(true, storage_id, &content_info, true, uid);
291                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
292
293                 if (g_insert_with_noti)
294                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
295
296                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_INSERT_ITEM, uid);
297                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
298
299                 if (g_insert_with_noti) {
300                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
301                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
302
303                         /* Recreate noti list */
304                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
305                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
306                 }
307
308                 g_media_svc_insert_item_cur_data_cnt = 0;
309
310         } else {
311                 media_svc_error("Error in media_svc_insert_item_bulk");
312                 _media_svc_destroy_content_info(&content_info);
313                 return MS_MEDIA_ERR_INTERNAL;
314         }
315
316         _media_svc_destroy_content_info(&content_info);
317
318         return MS_MEDIA_ERR_NONE;
319 }
320
321 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)
322 {
323         int ret = MS_MEDIA_ERR_NONE;
324         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
325
326         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
327         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
328         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
329         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
330
331         media_svc_content_info_s content_info;
332         memset(&content_info, 0, sizeof(media_svc_content_info_s));
333
334         /*Set media info*/
335         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
336         if (ret != MS_MEDIA_ERR_NONE)
337                 return ret;
338
339         switch (content_info.media_type) {
340         case MEDIA_SVC_MEDIA_TYPE_IMAGE:
341                 ret = _media_svc_extract_image_metadata(&content_info);
342                 break;
343         case MEDIA_SVC_MEDIA_TYPE_VIDEO:
344         case MEDIA_SVC_MEDIA_TYPE_SOUND:
345         case MEDIA_SVC_MEDIA_TYPE_MUSIC:
346                 ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
347                 break;
348         default:
349                 media_svc_debug("Do nothing[%d]", content_info.media_type);
350                 break;
351         }
352
353         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
354
355         /*Set or Get folder id*/
356         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, folder_uuid, uid);
357         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
358
359         content_info.folder_uuid = g_strdup(folder_uuid);
360         media_svc_retv_del_if(content_info.folder_uuid == NULL, MS_MEDIA_ERR_INTERNAL, &content_info);
361
362         /* Extracting thumbnail */
363         if (content_info.thumbnail_path == NULL) {
364                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
365                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
366
367                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
368                         if (ret == MS_MEDIA_ERR_NONE)
369                                 content_info.thumbnail_path = g_strdup(thumb_path);
370                 }
371         }
372
373         ret = _media_svc_insert_item_with_data(false, storage_id, &content_info, false, uid);
374
375         if (ret == MS_MEDIA_ERR_NONE) {
376                 media_svc_debug("Insertion is successful. Sending noti for this");
377                 _media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
378         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
379                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
380         }
381
382         _media_svc_destroy_content_info(&content_info);
383         return ret;
384 }
385
386 int media_svc_move_item(sqlite3 *handle,
387                                                                 const char *src_path,
388                                                                 const char *dest_path,
389                                                                 const char *media_id,
390                                                                 int media_type,
391                                                                 const char *mime_type,
392                                                                 uid_t uid)
393 {
394         int ret = MS_MEDIA_ERR_NONE;
395         char *file_name = NULL;
396         char *folder_path = NULL;
397         int modified_time = 0;
398         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
399         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
400         char org_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
401         char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
402         ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
403         ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;
404
405         media_svc_debug_fenter();
406
407         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
408         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
409         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
410         media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
411         media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
412
413         /* Get storage_id */
414         ret = _media_svc_get_storage_uuid(handle, src_path, org_stg_id, uid);
415         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
416         ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
417         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
418         /* Get storage_type */
419         ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
420         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
421         ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
422         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
423
424         /*check and update folder*/
425         ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, folder_uuid, uid);
426         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
427
428         /*get filename*/
429         file_name = g_path_get_basename(dest_path);
430
431         /*get modified_time*/
432         modified_time = _media_svc_get_file_time(dest_path);
433
434         /*get old thumbnail_path and remove thumbnail */
435         ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
436         if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
437                 media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
438                 SAFE_FREE(file_name);
439                 return ret;
440         }
441
442         if (STRING_VALID(old_thumb_path)) {
443                 ret = _media_svc_remove_file(old_thumb_path);
444                 if (ret != MS_MEDIA_ERR_NONE)
445                         media_svc_error("_media_svc_remove_file failed : %d", ret);
446         }
447
448         /*move item*/
449         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);
450         SAFE_FREE(file_name);
451         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
452
453         media_svc_debug("Move is successful. Sending noti for this");
454         _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);
455
456         /*update folder modified_time*/
457         folder_path = g_path_get_dirname(dest_path);
458         ret = _media_svc_update_folder_modified_time_by_folder_uuid(folder_uuid, folder_path, uid);
459         SAFE_FREE(folder_path);
460         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
461
462         return MS_MEDIA_ERR_NONE;
463 }
464
465 int media_svc_set_item_validity_begin(int data_cnt)
466 {
467         media_svc_debug("Transaction data count : [%d]", data_cnt);
468
469         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
470
471         g_media_svc_item_validity_data_cnt = data_cnt;
472         g_media_svc_item_validity_cur_data_cnt = 0;
473
474         return MS_MEDIA_ERR_NONE;
475 }
476
477 int media_svc_set_item_validity_end(uid_t uid)
478 {
479         int ret = MS_MEDIA_ERR_NONE;
480
481         media_svc_debug_fenter();
482
483         if (g_media_svc_item_validity_cur_data_cnt > 0)
484                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
485
486         g_media_svc_item_validity_data_cnt = 1;
487         g_media_svc_item_validity_cur_data_cnt = 0;
488
489         return ret;
490 }
491
492 int media_svc_set_item_validity(const char *storage_id, const char *path, int validity, uid_t uid)
493 {
494         int ret = MS_MEDIA_ERR_NONE;
495
496         if (g_media_svc_item_validity_data_cnt == 1) {
497
498                 return _media_svc_update_item_validity(storage_id, path, validity, false, uid);
499
500         } else if (g_media_svc_item_validity_cur_data_cnt < (g_media_svc_item_validity_data_cnt - 1)) {
501
502                 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
503                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
504
505                 g_media_svc_item_validity_cur_data_cnt++;
506
507         } else if (g_media_svc_item_validity_cur_data_cnt == (g_media_svc_item_validity_data_cnt - 1)) {
508
509                 ret = _media_svc_update_item_validity(storage_id, path, validity, true, uid);
510                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
511
512                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
513                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
514
515                 g_media_svc_item_validity_cur_data_cnt = 0;
516
517         } else {
518
519                 media_svc_error("Error in media_svc_set_item_validity");
520                 return MS_MEDIA_ERR_INTERNAL;
521         }
522
523         return MS_MEDIA_ERR_NONE;
524 }
525
526 int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
527 {
528         int ret = MS_MEDIA_ERR_NONE;
529         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
530         media_svc_noti_item *noti_item = NULL;
531
532         media_svc_debug_fenter();
533
534         /*Get thumbnail path to delete*/
535         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
536         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
537
538         /* Get notification info */
539         ret = _media_svc_get_noti_info(handle, storage_id, path, &noti_item);
540         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
541
542         /*Delete item*/
543         ret = _media_svc_delete_item_by_path(storage_id, path, uid);
544         if (ret != MS_MEDIA_ERR_NONE) {
545                 media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
546                 _media_svc_destroy_noti_item(noti_item);
547
548                 return ret;
549         }
550
551         /* Send notification */
552         media_svc_debug("Deletion is successful. Sending noti for this");
553         _media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
554         _media_svc_destroy_noti_item(noti_item);
555
556         /*Delete thumbnail*/
557         if (STRING_VALID(thumb_path)) {
558                 ret = _media_svc_remove_file(thumb_path);
559                 if (ret != MS_MEDIA_ERR_NONE)
560                         media_svc_error("fail to remove thumbnail file.");
561         }
562
563         return MS_MEDIA_ERR_NONE;
564 }
565
566 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)
567 {
568         int ret = MS_MEDIA_ERR_NONE;
569         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
570
571         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
572         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
573         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
574         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
575
576         media_svc_content_info_s content_info;
577         memset(&content_info, 0, sizeof(media_svc_content_info_s));
578
579         /*Set media info*/
580         ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
581         if (ret != MS_MEDIA_ERR_NONE)
582                 return ret;
583
584         /* Initialize thumbnail information to remake thumbnail. */
585         ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
586         if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD) {
587                 _media_svc_destroy_content_info(&content_info);
588                 return ret;
589         }
590
591         if (STRING_VALID(thumb_path)) {
592                 if (g_file_test(thumb_path, G_FILE_TEST_EXISTS)) {
593                         ret = _media_svc_remove_file(thumb_path);
594                         if (ret != MS_MEDIA_ERR_NONE)
595                                 media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
596                 }
597
598                 ret = _media_svc_update_thumbnail_path(storage_id, path, NULL, uid);
599                 if (ret != MS_MEDIA_ERR_NONE) {
600                         _media_svc_destroy_content_info(&content_info);
601                         return ret;
602                 }
603         }
604
605         /* Get notification info */
606         media_svc_noti_item *noti_item = NULL;
607         ret = _media_svc_get_noti_info(handle, storage_id, path, &noti_item);
608         if (ret != MS_MEDIA_ERR_NONE) {
609                 _media_svc_destroy_content_info(&content_info);
610                 return ret;
611         }
612
613         content_info.media_type = noti_item->media_type;
614
615         if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_OTHER
616         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_PVR)
617         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_UHD)
618         || (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SCSA))
619                 media_svc_debug("Do nothing [%d]", content_info.media_type);
620         else if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
621                 ret = _media_svc_extract_image_metadata(&content_info);
622         else
623                 ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
624
625         if (ret != MS_MEDIA_ERR_NONE) {
626                 _media_svc_destroy_noti_item(noti_item);
627                 _media_svc_destroy_content_info(&content_info);
628                 return ret;
629         }
630
631         /* Extracting thumbnail */
632         if (content_info.thumbnail_path == NULL) {
633                 if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
634                         memset(thumb_path, 0, sizeof(thumb_path));
635
636                         ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
637                         if (ret == MS_MEDIA_ERR_NONE)
638                                 content_info.thumbnail_path = g_strdup(thumb_path);
639                 }
640         }
641
642         ret = _media_svc_update_item_with_data(is_direct, storage_id, &content_info, uid);
643
644         if (ret == MS_MEDIA_ERR_NONE) {
645                 media_svc_debug("Update is successful. Sending noti for this");
646                 _media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
647         } else {
648                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
649         }
650
651         _media_svc_destroy_content_info(&content_info);
652         _media_svc_destroy_noti_item(noti_item);
653
654         return ret;
655 }
656
657 int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
658 {
659         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
660
661         return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
662 }
663
664 int media_svc_check_db_upgrade(sqlite3 *handle, int user_version, uid_t uid)
665 {
666         media_svc_debug_fenter();
667
668         return _media_svc_check_db_upgrade(handle, user_version, uid);
669 }
670
671 static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
672 {
673         int ret = MS_MEDIA_ERR_NONE;
674         char *root_path = NULL;
675         GPtrArray *path_list = NULL;
676         int i = 0;
677
678         ret = ms_user_get_internal_root_path(uid, &root_path);
679         media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");
680
681         ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
682         if (ret != MS_MEDIA_ERR_NONE)
683                 media_svc_error("Fail to send noti");
684
685         SAFE_FREE(root_path);
686
687         path_list = g_ptr_array_new_with_free_func(g_free);
688         _media_svc_get_storage_path(handle, &path_list);
689
690         for (i = 0; i < path_list->len; i++) {
691                 root_path = g_ptr_array_index(path_list, i);
692
693                 ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
694                 if (ret != MS_MEDIA_ERR_NONE)
695                         media_svc_error("Fail to send noti");
696         }
697
698         g_ptr_array_free(path_list, TRUE);
699 }
700
701 static void __file_info_free(gpointer data)
702 {
703         SAFE_FREE(*(media_svc_file_s **)data);
704 }
705
706 int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
707 {
708         int ret = MS_MEDIA_ERR_NONE;
709         int i = 0;
710         /* NOTICE : After 6.0, change 'media_view' to 'media', add 'AND validity=1', and no need storage id */
711         char *sql = NULL;
712         media_svc_content_info_s content_info;
713         media_svc_file_s *file_info = NULL;
714         GArray *path_list = NULL;
715
716         path_list = g_array_new(FALSE, FALSE, sizeof(media_svc_file_s *));
717         media_svc_retvm_if(!path_list, MS_MEDIA_ERR_OUT_OF_MEMORY, "Allocation failed");
718         g_array_set_clear_func(path_list, __file_info_free);
719
720         sql = sqlite3_mprintf("SELECT media_path, storage_uuid FROM %q WHERE media_type=3", MEDIA_SVC_DB_VIEW_MEDIA);
721         ret = _media_svc_get_media(handle, sql, &path_list);
722         if (ret != MS_MEDIA_ERR_NONE) {
723                 media_svc_error("Fail to get media list");
724                 g_array_free(path_list, TRUE);
725                 return ret;
726         }
727
728         for (i = 0; i < path_list->len; i++) {
729                 file_info = g_array_index(path_list, media_svc_file_s *, i);
730
731                 memset(&content_info, 0, sizeof(media_svc_content_info_s));
732                 ret = _media_svc_extract_music_metadata_for_update(&content_info, file_info->storage_id, file_info->path);
733                 if (ret != MS_MEDIA_ERR_NONE) {
734                         media_svc_error("Fail to extract metadata");
735                         _media_svc_destroy_content_info(&content_info);
736                         continue;
737                 }
738
739                 ret = _media_svc_update_meta_with_data(&content_info);
740                 if (ret != MS_MEDIA_ERR_NONE)
741                         media_svc_error("Fail to append item[%s]", content_info.path);
742
743                 _media_svc_destroy_content_info(&content_info);
744         }
745
746         g_array_free(path_list, TRUE);
747
748         ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
749         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
750
751         /* Noti for this */
752         __media_svc_noti_all_storage(handle, uid);
753
754         return ret;
755 }
756
757 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)
758 {
759         return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
760 }
761
762 int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
763 {
764         return _media_svc_get_pinyin_str(src_str, pinyin_str);
765 }
766
767 int media_svc_check_pinyin_support(bool *support)
768 {
769         *support = _media_svc_check_pinyin_support();
770
771         return MS_MEDIA_ERR_NONE;
772 }
773
774 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
775 {
776         int ret = MS_MEDIA_ERR_NONE;
777
778         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
779
780         ret = _media_svc_update_storage_validity(storage_id, validity, uid);
781         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);
782
783         ret = _media_svc_update_media_view(handle, uid);
784         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
785
786         return ret;
787 }
788
789 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
790 {
791         return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
792 }
793
794 int media_svc_generate_uuid(char **uuid)
795 {
796         char *gen_uuid = NULL;
797         gen_uuid = _media_info_generate_uuid();
798         media_svc_retvm_if(gen_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Fail to generate uuid");
799
800         *uuid = strdup(gen_uuid);
801
802         return MS_MEDIA_ERR_NONE;
803 }
804
805 int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
806 {
807         return _media_svc_check_storage(handle, storage_id, storage_path, validity, uid);
808 }
809
810 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
811 {
812         return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
813 }
814
815 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)
816 {
817         int ret = MS_MEDIA_ERR_NONE;
818
819         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
820         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
821         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
822         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
823
824         ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
825         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
826
827         ret = _media_svc_create_media_table_with_id(storage_id, uid);
828         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
829
830         ret = _media_svc_update_media_view(handle, uid);
831         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
832
833         /* Remove external storage that validity is 0 */
834         ret = _media_svc_delete_invalid_storage(handle, uid);
835         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);
836
837         return ret;
838 }
839
840 int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
841 {
842         media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
843         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
844         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
845         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
846
847         return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
848 }
849
850 int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
851 {
852         return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
853 }
854
855 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
856 {
857         int ret = MS_MEDIA_ERR_NONE;
858         int count = -1;
859
860         ret = _media_svc_count_folder_with_path(handle, storage_id, folder_path, &count);
861         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
862
863         if (count > 0) {
864                 media_svc_debug("item is exist in database");
865                 return MS_MEDIA_ERR_NONE;
866         } else {
867                 media_svc_debug("item is not exist in database");
868                 return MS_MEDIA_ERR_DB_NO_RECORD;
869         }
870
871         return MS_MEDIA_ERR_NONE;
872 }
873
874 int media_svc_append_query(const char *query, uid_t uid)
875 {
876         return _media_svc_append_query_list(query, uid);
877 }
878
879 int media_svc_send_query(uid_t uid)
880 {
881         return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
882 }
883
884 int media_svc_get_media_type(const char *path, int *mediatype)
885 {
886         return _media_svc_get_media_type(path, mediatype);
887 }
888
889 int media_svc_create_thumbnail(const char *storage_id, const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
890 {
891         int ret = MS_MEDIA_ERR_NONE;
892         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
893         char *sql = NULL;
894
895         // 1. Check media type
896         if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
897                 return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
898
899         // 2. try to create thumbnail
900         ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
901         if (ret != MS_MEDIA_ERR_NONE) {
902                 media_svc_error("Failed to create thumbnail [%d]", ret);
903                 if (ret == MS_MEDIA_ERR_UNSUPPORTED_CONTENT)
904                         return ret;
905         }
906
907         // 3. Update creation result to media db
908         sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path='%q' WHERE media_path='%q';", storage_id, thumb_path, file_path);
909
910         ret = _media_svc_sql_query(sql, uid);
911         SQLITE3_SAFE_FREE(sql);
912         if (ret != MS_MEDIA_ERR_NONE) {
913                 media_svc_error("Failed to update media db [%d]", ret);
914                 *thumbnail_path = g_strdup("");
915         } else {
916                 *thumbnail_path = g_strdup(thumb_path);
917         }
918
919         return ret;
920 }