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