Add face related tables
[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 /* Flag for items to be published by notification */
49 static __thread int g_insert_with_noti = FALSE;
50
51 #define DEFAULT_MEDIA_SVC_STORAGE_ID "media"
52
53 int media_svc_connect(MediaSvcHandle **handle, uid_t uid, bool need_write)
54 {
55         int ret = MS_MEDIA_ERR_NONE;
56         MediaDBHandle *db_handle = NULL;
57
58         media_svc_debug_func();
59
60         ret = media_db_connect(&db_handle, uid, need_write);
61         if (ret != MS_MEDIA_ERR_NONE)
62                 return ret;
63
64         *handle = db_handle;
65         return MS_MEDIA_ERR_NONE;
66 }
67
68 int media_svc_disconnect(MediaSvcHandle *handle)
69 {
70         MediaDBHandle *db_handle = (MediaDBHandle *)handle;
71
72         media_svc_debug_func();
73
74         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
75
76         int ret = MS_MEDIA_ERR_NONE;
77
78         ret = media_db_disconnect(db_handle);
79         return ret;
80 }
81
82 int media_svc_create_table(MediaSvcHandle *handle, uid_t uid)
83 {
84         int ret = MS_MEDIA_ERR_NONE;
85         sqlite3 *db_handle = (sqlite3 *)handle;
86
87         media_svc_debug_func();
88
89         ret = _media_svc_init_table_query();
90         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
91
92         /*create media table*/
93         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
94         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
95
96         /*create folder table*/
97         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
98         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
99
100         /*create playlist_map table*/
101         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
102         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
103
104         /*create playlist table*/
105         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
106         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
107
108         /* create album table*/
109         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
110         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
111
112         /*create tag_map table*/
113         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
114         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
115
116         /*create tag table*/
117         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
118         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
119
120         /*create bookmark table*/
121         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
122         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
123
124         /*create storage table. from tizen 2.4*/
125         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
126         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
127
128         /*init storage table*/
129         ret = _media_svc_init_storage(db_handle, uid);
130         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
131
132         /*create face table. from tizen 3.0*/
133         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
134         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
135
136         ret = _media_svc_make_table_query(db_handle, MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
137         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
138
139         _media_svc_destroy_table_query();
140
141         media_svc_debug_func();
142
143         return MS_MEDIA_ERR_NONE;
144 }
145
146 int media_svc_get_storage_type(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
147 {
148         int ret = MS_MEDIA_ERR_NONE;
149         media_svc_storage_type_e type;
150
151         ret = _media_svc_get_store_type_by_path(path, &type, uid);
152         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_store_type_by_path failed : %d", ret);
153
154         *storage_type = type;
155
156         return ret;
157 }
158
159 int media_svc_get_file_info(MediaSvcHandle *handle, const char *path, time_t *modified_time, unsigned long long *size)
160 {
161         int ret = MS_MEDIA_ERR_NONE;
162         sqlite3 *db_handle = (sqlite3 *)handle;
163
164         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
165
166         ret = _media_svc_get_fileinfo_by_path(db_handle, path, modified_time, size);
167
168         return ret;
169 }
170
171 int media_svc_check_item_exist_by_path(MediaSvcHandle *handle, const char *path)
172 {
173         int ret = MS_MEDIA_ERR_NONE;
174         sqlite3 *db_handle = (sqlite3 *)handle;
175         int count = -1;
176
177         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
178         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
179
180         ret = _media_svc_count_record_with_path(db_handle, path, &count);
181         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
182
183         if (count > 0) {
184                 media_svc_debug("item is exist in database");
185                 return MS_MEDIA_ERR_NONE;
186         } else {
187                 media_svc_debug("item is not exist in database");
188                 return MS_MEDIA_ERR_DB_NO_RECORD;
189         }
190
191         return MS_MEDIA_ERR_NONE;
192 }
193
194 int media_svc_insert_item_begin(MediaSvcHandle *handle, int data_cnt, int with_noti, int from_pid)
195 {
196         sqlite3 *db_handle = (sqlite3 *)handle;
197
198         media_svc_debug("Transaction data count : [%d]", data_cnt);
199
200         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
201         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
202
203         g_media_svc_insert_item_data_cnt  = data_cnt;
204         g_media_svc_insert_item_cur_data_cnt  = 0;
205
206         /* Prepare for making noti item list */
207         if (with_noti) {
208                 media_svc_debug("making noti list from pid[%d]", from_pid);
209                 if (_media_svc_create_noti_list(data_cnt) != MS_MEDIA_ERR_NONE) {
210                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
211                 }
212
213                 _media_svc_set_noti_from_pid(from_pid);
214                 g_insert_with_noti = TRUE;
215         }
216
217         return MS_MEDIA_ERR_NONE;
218 }
219
220 int media_svc_insert_item_end(MediaSvcHandle *handle, uid_t uid)
221 {
222         int ret = MS_MEDIA_ERR_NONE;
223         sqlite3 *db_handle = (sqlite3 *)handle;
224
225         media_svc_debug_func();
226
227         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
228
229         if (g_media_svc_insert_item_cur_data_cnt  > 0) {
230
231                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
232                 if (g_insert_with_noti) {
233                         media_svc_debug("sending noti list");
234                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt);
235                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt);
236                         g_insert_with_noti = FALSE;
237                         _media_svc_set_noti_from_pid(-1);
238                 }
239         }
240
241         g_media_svc_insert_item_data_cnt  = 1;
242         g_media_svc_insert_item_cur_data_cnt  = 0;
243
244         return ret;
245 }
246
247 int media_svc_insert_item_bulk(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path, int is_burst, uid_t uid)
248 {
249         int ret = MS_MEDIA_ERR_NONE;
250         sqlite3 *db_handle = (sqlite3 *)handle;
251         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
252         media_svc_media_type_e media_type;
253
254         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
255         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
256
257         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
258                 media_svc_error("storage type is incorrect[%d]", storage_type);
259                 return MS_MEDIA_ERR_INVALID_PARAMETER;
260         }
261
262         media_svc_content_info_s content_info;
263         memset(&content_info, 0, sizeof(media_svc_content_info_s));
264
265         /*Set media info*/
266         /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
267         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, path, &media_type, FALSE);
268         if (ret != MS_MEDIA_ERR_NONE) {
269                 return ret;
270         }
271
272         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
273                 /*Do nothing.*/
274         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
275                 ret = _media_svc_extract_image_metadata(handle, &content_info, media_type, uid);
276         } else {
277                 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, uid);
278         }
279         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
280
281         /*Set or Get folder id*/
282         ret = _media_svc_get_and_append_folder_id_by_path(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, path, storage_type, folder_uuid, uid);
283         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
284
285         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
286         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
287
288         if (g_media_svc_insert_item_data_cnt == 1) {
289
290                 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, FALSE, uid);
291                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
292
293                 if (g_insert_with_noti)
294                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt++);
295
296         } else if (g_media_svc_insert_item_cur_data_cnt  < (g_media_svc_insert_item_data_cnt  - 1)) {
297
298                 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, TRUE, uid);
299                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
300
301                 if (g_insert_with_noti)
302                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
303
304                 g_media_svc_insert_item_cur_data_cnt++;
305
306         } else if (g_media_svc_insert_item_cur_data_cnt  == (g_media_svc_insert_item_data_cnt  - 1)) {
307
308                 ret = _media_svc_insert_item_with_data(db_handle, &content_info, is_burst, TRUE, uid);
309                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
310
311                 if (g_insert_with_noti)
312                         _media_svc_insert_item_to_noti_list(&content_info, g_media_svc_insert_item_cur_data_cnt);
313
314                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_INSERT_ITEM, uid);
315                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
316
317                 if (g_insert_with_noti) {
318                         _media_svc_publish_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
319                         _media_svc_destroy_noti_list(g_media_svc_insert_item_cur_data_cnt + 1);
320
321                         /* Recreate noti list */
322                         ret = _media_svc_create_noti_list(g_media_svc_insert_item_data_cnt);
323                         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
324                 }
325
326                 g_media_svc_insert_item_cur_data_cnt = 0;
327
328         } else {
329                 media_svc_error("Error in media_svc_insert_item_bulk");
330                 _media_svc_destroy_content_info(&content_info);
331                 return MS_MEDIA_ERR_INTERNAL;
332         }
333
334         _media_svc_destroy_content_info(&content_info);
335
336         return MS_MEDIA_ERR_NONE;
337 }
338
339 int media_svc_insert_item_immediately(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
340 {
341         int ret = MS_MEDIA_ERR_NONE;
342         sqlite3 *db_handle = (sqlite3 *)handle;
343         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
344         media_svc_media_type_e media_type;
345
346         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
347         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
348
349 #if 0
350         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
351                 media_svc_error("storage type is incorrect[%d]", storage_type);
352                 return MS_MEDIA_ERR_INVALID_PARAMETER;
353         }
354 #endif
355
356         media_svc_content_info_s content_info;
357         memset(&content_info, 0, sizeof(media_svc_content_info_s));
358
359         /*Set media info*/
360         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, path, &media_type, FALSE);
361         if (ret != MS_MEDIA_ERR_NONE) {
362                 return ret;
363         }
364
365         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
366                 /*Do nothing.*/
367         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
368                 ret = _media_svc_extract_image_metadata(handle, &content_info, media_type, uid);
369         } else {
370                 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, uid);
371         }
372
373         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
374
375         /*Set or Get folder id*/
376         ret = _media_svc_get_and_append_folder_id_by_path(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, path, storage_type, folder_uuid, uid);
377         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
378
379         ret = __media_svc_malloc_and_strncpy(&content_info.folder_uuid, folder_uuid);
380         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
381 #if 1
382         /* Extracting thumbnail */
383         if (content_info.thumbnail_path == NULL) {
384                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
385                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
386                         int width = 0;
387                         int height = 0;
388
389                         ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
390                         if (ret == MS_MEDIA_ERR_NONE) {
391                                 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
392                         }
393
394                         if (content_info.media_meta.width <= 0)
395                                 content_info.media_meta.width = width;
396
397                         if (content_info.media_meta.height <= 0)
398                                 content_info.media_meta.height = height;
399                 }
400         }
401 #endif
402
403         ret = _media_svc_insert_item_with_data(db_handle, &content_info, FALSE, FALSE, uid);
404
405         if (ret == MS_MEDIA_ERR_NONE) {
406                 media_svc_debug("Insertion is successful. Sending noti for this");
407                 _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);
408         } else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
409                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
410         }
411
412         _media_svc_destroy_content_info(&content_info);
413         return ret;
414 }
415
416 int media_svc_insert_folder(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *path, uid_t uid)
417 {
418         int ret = MS_MEDIA_ERR_NONE;
419         sqlite3 *db_handle = (sqlite3 *)handle;
420         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
421         memset(folder_uuid, 0, sizeof(folder_uuid));
422
423         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
424         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
425
426         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
427                 media_svc_error("storage type is incorrect[%d]", storage_type);
428                 return MS_MEDIA_ERR_INVALID_PARAMETER;
429         }
430
431         media_svc_debug("storage[%d], folder_path[%s]", storage_type, path);
432
433         ret = _media_svc_get_and_append_folder(db_handle, DEFAULT_MEDIA_SVC_STORAGE_ID, path, storage_type, folder_uuid, uid);
434
435         return ret;
436 }
437
438 int media_svc_move_item_begin(MediaSvcHandle *handle, int data_cnt)
439 {
440         sqlite3 *db_handle = (sqlite3 *)handle;
441
442         media_svc_debug("Transaction data count : [%d]", data_cnt);
443
444         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
445         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
446
447         g_media_svc_move_item_data_cnt  = data_cnt;
448         g_media_svc_move_item_cur_data_cnt  = 0;
449
450         return MS_MEDIA_ERR_NONE;
451 }
452
453 int media_svc_move_item_end(MediaSvcHandle *handle, uid_t uid)
454 {
455         int ret = MS_MEDIA_ERR_NONE;
456         sqlite3 *db_handle = (sqlite3 *)handle;
457
458         media_svc_debug_func();
459
460         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
461
462         if (g_media_svc_move_item_cur_data_cnt  > 0) {
463
464                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
465         }
466
467         /*clean up old folder path*/
468         ret = _media_svc_update_folder_table(handle, uid);
469         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
470
471         g_media_svc_move_item_data_cnt  = 1;
472         g_media_svc_move_item_cur_data_cnt  = 0;
473
474         return ret;
475 }
476
477 int media_svc_move_item(MediaSvcHandle *handle, media_svc_storage_type_e src_storage, const char *src_path,
478                         media_svc_storage_type_e dest_storage, const char *dest_path, uid_t uid)
479 {
480         int ret = MS_MEDIA_ERR_NONE;
481         sqlite3 *db_handle = (sqlite3 *)handle;
482         char *file_name = NULL;
483         char *folder_path = NULL;
484         int modified_time = 0;
485         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
486         char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
487         char new_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
488         int media_type = -1;
489
490         media_svc_debug_func();
491
492         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
493         media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
494         media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
495
496         if ((src_storage != MEDIA_SVC_STORAGE_INTERNAL) && (src_storage != MEDIA_SVC_STORAGE_EXTERNAL)) {
497                 media_svc_error("src_storage type is incorrect[%d]", src_storage);
498                 return MS_MEDIA_ERR_INVALID_PARAMETER;
499         }
500         if ((dest_storage != MEDIA_SVC_STORAGE_INTERNAL) && (dest_storage != MEDIA_SVC_STORAGE_EXTERNAL)) {
501                 media_svc_error("dest_storage type is incorrect[%d]", dest_storage);
502                 return MS_MEDIA_ERR_INVALID_PARAMETER;
503         }
504
505         /*check and update folder*/
506         ret = _media_svc_get_and_append_folder_id_by_path(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, dest_path, dest_storage, folder_uuid, uid);
507         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
508
509         /*get filename*/
510         file_name = g_path_get_basename(dest_path);
511
512         /*get modified_time*/
513         modified_time = _media_svc_get_file_time(dest_path);
514
515         /*get thumbnail_path to update. only for Imgae and Video items. Audio share album_art(thumbnail)*/
516         ret = _media_svc_get_media_type_by_path(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, src_path, &media_type);
517         if (ret != MS_MEDIA_ERR_NONE) {
518                 media_svc_error("_media_svc_get_media_type_by_path failed");
519                 SAFE_FREE(file_name);
520                 return ret;
521         }
522
523         if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
524                 /*get old thumbnail_path*/
525                 ret = _media_svc_get_thumbnail_path_by_path(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, src_path, old_thumb_path);
526                 if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
527                         media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
528                         SAFE_FREE(file_name);
529                         return ret;
530                 }
531
532                 /* If old thumb path is default or not */
533                 if (strncmp(old_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
534                         strncpy(new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(new_thumb_path));
535                 } else {
536                         _media_svc_get_thumbnail_path(dest_storage, new_thumb_path, dest_path, THUMB_EXT, uid);
537                 }
538         }
539
540         if (g_media_svc_move_item_data_cnt == 1) {
541
542                 /*update item*/
543                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
544                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, FALSE, uid);
545                 } else {
546                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, FALSE, uid);
547                 }
548                 SAFE_FREE(file_name);
549                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
550
551                 media_svc_debug("Move is successful. Sending noti for this");
552
553                 /* Get notification info */
554                 media_svc_noti_item *noti_item = NULL;
555                 ret = _media_svc_get_noti_info(handle, DEFAULT_MEDIA_SVC_STORAGE_ID, dest_path, MS_MEDIA_ITEM_FILE, &noti_item);
556                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
557
558                 /* Send notification for move */
559                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_UPDATE, src_path, media_type, noti_item->media_uuid, noti_item->mime_type);
560                 _media_svc_destroy_noti_item(noti_item);
561
562                 /*update folder modified_time*/
563                 folder_path = g_path_get_dirname(dest_path);
564                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, FALSE, uid);
565                 SAFE_FREE(folder_path);
566                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
567
568                 ret = _media_svc_update_folder_table(handle, uid);
569                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
570
571         } else if (g_media_svc_move_item_cur_data_cnt  < (g_media_svc_move_item_data_cnt  - 1)) {
572
573                 /*update item*/
574                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
575                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
576                 } else {
577                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
578                 }
579                 SAFE_FREE(file_name);
580                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
581
582                 /*update folder modified_time*/
583                 folder_path = g_path_get_dirname(dest_path);
584                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, TRUE, uid);
585                 SAFE_FREE(folder_path);
586                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
587
588                 g_media_svc_move_item_cur_data_cnt++;
589
590         } else if (g_media_svc_move_item_cur_data_cnt  == (g_media_svc_move_item_data_cnt  - 1)) {
591
592                 /*update item*/
593                 if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
594                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, new_thumb_path, TRUE, uid);
595                 } else {
596                         ret = _media_svc_update_item_by_path(handle, src_path, dest_storage, dest_path, file_name, modified_time, folder_uuid, NULL, TRUE, uid);
597                 }
598                 SAFE_FREE(file_name);
599                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
600
601                 /*update folder modified_time*/
602                 folder_path = g_path_get_dirname(dest_path);
603                 ret = _media_svc_update_folder_modified_time_by_folder_uuid(handle, folder_uuid, folder_path, TRUE, uid);
604                 SAFE_FREE(folder_path);
605                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
606
607                 /*update db*/
608                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_MOVE_ITEM, uid);
609                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
610
611                 g_media_svc_move_item_cur_data_cnt = 0;
612
613         } else {
614                 media_svc_error("Error in media_svc_move_item");
615                 return MS_MEDIA_ERR_INTERNAL;
616         }
617
618         /*rename thumbnail file*/
619 /*      if((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) ||(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) { */
620         if ((strlen(old_thumb_path) > 0) && (strncmp(old_thumb_path, MEDIA_SVC_THUMB_DEFAULT_PATH, sizeof(MEDIA_SVC_THUMB_DEFAULT_PATH)) != 0)) {
621                 ret = _media_svc_rename_file(old_thumb_path, new_thumb_path);
622                 if (ret != MS_MEDIA_ERR_NONE)
623                         media_svc_error("_media_svc_rename_file failed : %d", ret);
624         }
625 /*      } */
626
627         return MS_MEDIA_ERR_NONE;
628 }
629
630 int media_svc_set_item_validity_begin(MediaSvcHandle *handle, int data_cnt)
631 {
632         sqlite3 *db_handle = (sqlite3 *)handle;
633
634         media_svc_debug("Transaction data count : [%d]", data_cnt);
635
636         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
637         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
638
639         g_media_svc_item_validity_data_cnt  = data_cnt;
640         g_media_svc_item_validity_cur_data_cnt  = 0;
641
642         return MS_MEDIA_ERR_NONE;
643 }
644
645 int media_svc_set_item_validity_end(MediaSvcHandle *handle, uid_t uid)
646 {
647         int ret = MS_MEDIA_ERR_NONE;
648         sqlite3 *db_handle = (sqlite3 *)handle;
649
650         media_svc_debug_func();
651
652         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
653
654         if (g_media_svc_item_validity_cur_data_cnt  > 0) {
655
656                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
657         }
658
659         g_media_svc_item_validity_data_cnt  = 1;
660         g_media_svc_item_validity_cur_data_cnt  = 0;
661
662         return ret;
663 }
664
665 int media_svc_set_item_validity(MediaSvcHandle *handle, const char *path, int validity, uid_t uid)
666 {
667         int ret = MS_MEDIA_ERR_NONE;
668         sqlite3 *db_handle = (sqlite3 *)handle;
669
670         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
671         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
672
673         media_svc_debug("path=[%s], validity=[%d]", path, validity);
674
675         if (g_media_svc_item_validity_data_cnt  == 1) {
676
677                 return _media_svc_update_item_validity(db_handle, path, validity, FALSE, uid);
678
679         } else if (g_media_svc_item_validity_cur_data_cnt  < (g_media_svc_item_validity_data_cnt  - 1)) {
680
681                 ret = _media_svc_update_item_validity(db_handle, path, validity, TRUE, uid);
682                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
683
684                 g_media_svc_item_validity_cur_data_cnt++;
685
686         } else if (g_media_svc_item_validity_cur_data_cnt  == (g_media_svc_item_validity_data_cnt  - 1)) {
687
688                 ret = _media_svc_update_item_validity(db_handle, path, validity, TRUE, uid);
689                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
690
691                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_SET_ITEM_VALIDITY, uid);
692                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
693
694                 g_media_svc_item_validity_cur_data_cnt  = 0;
695
696         } else {
697
698                 media_svc_error("Error in media_svc_set_item_validity");
699                 return MS_MEDIA_ERR_INTERNAL;
700         }
701
702         return MS_MEDIA_ERR_NONE;
703 }
704
705 int media_svc_delete_item_by_path(MediaSvcHandle *handle, const char *storage_id, const char *path, uid_t uid)
706 {
707         int ret = MS_MEDIA_ERR_NONE;
708         sqlite3 *db_handle = (sqlite3 *)handle;
709         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
710
711         media_svc_debug_func();
712
713         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
714         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
715         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
716
717         int media_type = -1;
718         ret = _media_svc_get_media_type_by_path(db_handle, storage_id, path, &media_type);
719         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
720
721 #if 0
722         if ((media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) || (media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)) {
723                 /*Get thumbnail path to delete*/
724                 ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
725                 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
726         } else if ((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
727                 int count = 0;
728                 ret = _media_svc_get_media_count_with_album_id_by_path(db_handle, path, &count);
729                 media_svc_retv_if((ret != MS_MEDIA_ERR_NONE), ret);
730
731                 if (count == 1) {
732                         /*Get thumbnail path to delete*/
733                         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
734                         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
735                 }
736         }
737 #endif
738         /*Get thumbnail path to delete*/
739         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
740         media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);
741
742         if (g_media_svc_insert_item_data_cnt == 1) {
743
744                 /* Get notification info */
745                 media_svc_noti_item *noti_item = NULL;
746                 ret = _media_svc_get_noti_info(db_handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
747                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
748
749                 /*Delete item*/
750                 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, FALSE, uid);
751                 if (ret != MS_MEDIA_ERR_NONE) {
752                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
753                         _media_svc_destroy_noti_item(noti_item);
754
755                         return ret;
756                 }
757
758                 /* Send notification */
759                 media_svc_debug("Deletion is successful. Sending noti for this");
760                 _media_svc_publish_noti(MS_MEDIA_ITEM_FILE, MS_MEDIA_ITEM_DELETE, path, media_type, noti_item->media_uuid, noti_item->mime_type);
761                 _media_svc_destroy_noti_item(noti_item);
762         } else {
763                 ret = _media_svc_delete_item_by_path(db_handle, storage_id, path, TRUE, uid);
764                 if (ret != MS_MEDIA_ERR_NONE) {
765                         media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
766                         return ret;
767                 }
768
769         }
770
771         /*Delete thumbnail*/
772         if ((strlen(thumb_path) > 0) && (strncmp(thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
773                 ret = _media_svc_remove_file(thumb_path);
774                 if (ret != MS_MEDIA_ERR_NONE) {
775                         media_svc_error("fail to remove thumbnail file.");
776                 }
777         }
778
779         return MS_MEDIA_ERR_NONE;
780 }
781
782 int media_svc_delete_all_items_in_storage(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, uid_t uid)
783 {
784         int ret = MS_MEDIA_ERR_NONE;
785         sqlite3 *db_handle = (sqlite3 *)handle;
786         char *dirpath = NULL;
787
788         media_svc_debug("media_svc_delete_all_items_in_storage [%d]", storage_type);
789
790         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
791
792         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
793                 media_svc_error("storage type is incorrect[%d]", storage_type);
794                 return MS_MEDIA_ERR_INVALID_PARAMETER;
795         }
796
797         ret = _media_svc_truncate_table(db_handle, storage_type, uid);
798         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
799
800         dirpath = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
801
802         /* remove thumbnails */
803         ret = _media_svc_remove_all_files_in_dir(dirpath);
804         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
805
806         return MS_MEDIA_ERR_NONE;
807 }
808
809 int media_svc_delete_invalid_items_in_storage(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, uid_t uid)
810 {
811         sqlite3 *db_handle = (sqlite3 *)handle;
812
813         media_svc_debug_func();
814
815         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
816
817         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
818                 media_svc_error("storage type is incorrect[%d]", storage_type);
819                 return MS_MEDIA_ERR_INVALID_PARAMETER;
820         }
821
822         /*Delete from DB and remove thumbnail files*/
823         return _media_svc_delete_invalid_items(db_handle, storage_type, uid);
824 }
825
826 int media_svc_delete_invalid_items_in_folder(MediaSvcHandle *handle, const char *folder_path, uid_t uid)
827 {
828         sqlite3 *db_handle = (sqlite3 *)handle;
829
830         media_svc_debug_func();
831
832         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
833
834         /*Delete from DB and remove thumbnail files*/
835         return _media_svc_delete_invalid_folder_items(db_handle, folder_path, uid);
836 }
837
838 int media_svc_set_all_storage_items_validity(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, int validity, uid_t uid)
839 {
840         sqlite3 *db_handle = (sqlite3 *)handle;
841
842         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
843
844         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
845                 media_svc_error("storage type is incorrect[%d]", storage_type);
846                 return MS_MEDIA_ERR_INVALID_PARAMETER;
847         }
848
849         return _media_svc_update_storage_item_validity(db_handle, storage_type, validity, uid);
850 }
851
852 int media_svc_set_folder_items_validity(MediaSvcHandle *handle, const char *folder_path, int validity, int recursive, uid_t uid)
853 {
854         sqlite3 *db_handle = (sqlite3 *)handle;
855
856         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
857         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
858
859         if (recursive)
860                 return _media_svc_update_recursive_folder_item_validity(db_handle, folder_path, validity, uid);
861         else
862                 return _media_svc_update_folder_item_validity(db_handle, folder_path, validity, uid);
863 }
864
865 int media_svc_refresh_item(MediaSvcHandle *handle, media_svc_storage_type_e storage_type, const char *storage_id, const char *path, uid_t uid)
866 {
867         int ret = MS_MEDIA_ERR_NONE;
868         sqlite3 *db_handle = (sqlite3 *)handle;
869         media_svc_media_type_e media_type;
870
871         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
872         media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
873         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
874
875         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL) && (storage_type != MEDIA_SVC_STORAGE_CLOUD)) {
876                 media_svc_error("storage type is incorrect[%d]", storage_type);
877                 return MS_MEDIA_ERR_INVALID_PARAMETER;
878         }
879
880         media_svc_content_info_s content_info;
881         memset(&content_info, 0, sizeof(media_svc_content_info_s));
882
883         /*Set media info*/
884         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, path, &media_type, TRUE);
885         if (ret != MS_MEDIA_ERR_NONE) {
886                 return ret;
887         }
888
889         /* Initialize thumbnail information to remake thumbnail. */
890         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1];
891         ret = _media_svc_get_thumbnail_path_by_path(db_handle, storage_id, path, thumb_path);
892         if (ret != MS_MEDIA_ERR_NONE) {
893                 _media_svc_destroy_content_info(&content_info);
894                 return ret;
895         }
896
897         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)) {
898                 ret = _media_svc_remove_file(thumb_path);
899                 if (ret != MS_MEDIA_ERR_NONE) {
900                         media_svc_error("_media_svc_remove_file failed : %s", thumb_path);
901                 }
902         }
903
904         ret = _media_svc_update_thumbnail_path(db_handle,  storage_id, path, NULL, uid);
905         if (ret != MS_MEDIA_ERR_NONE) {
906                 _media_svc_destroy_content_info(&content_info);
907                 return ret;
908         }
909
910         /* Get notification info */
911         media_svc_noti_item *noti_item = NULL;
912         ret = _media_svc_get_noti_info(handle, storage_id, path, MS_MEDIA_ITEM_FILE, &noti_item);
913         if (ret != MS_MEDIA_ERR_NONE) {
914                 _media_svc_destroy_content_info(&content_info);
915                 return ret;
916         }
917
918         media_type = noti_item->media_type;
919
920         if (media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
921                 /*Do nothing.*/
922         } else if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
923                 ret = _media_svc_extract_image_metadata(handle, &content_info, media_type, uid);
924         } else {
925                 ret = _media_svc_extract_media_metadata(handle, &content_info, media_type, uid);
926         }
927
928         if (ret != MS_MEDIA_ERR_NONE) {
929                 _media_svc_destroy_noti_item(noti_item);
930                 _media_svc_destroy_content_info(&content_info);
931                 return ret;
932         }
933 #if 1
934         /* Extracting thumbnail */
935         if (content_info.thumbnail_path == NULL) {
936                 if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
937                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
938                         int width = 0;
939                         int height = 0;
940
941                         ret = _media_svc_request_thumbnail_with_origin_size(content_info.path, thumb_path, sizeof(thumb_path), &width, &height, uid);
942                         if (ret == MS_MEDIA_ERR_NONE) {
943                                 ret = __media_svc_malloc_and_strncpy(&(content_info.thumbnail_path), thumb_path);
944                         }
945
946                         if (content_info.media_meta.width <= 0)
947                                 content_info.media_meta.width = width;
948
949                         if (content_info.media_meta.height <= 0)
950                                 content_info.media_meta.height = height;
951                 }
952         }
953
954 #endif
955
956         ret = _media_svc_update_item_with_data(db_handle, storage_id, &content_info, uid);
957
958         if (ret == MS_MEDIA_ERR_NONE) {
959                 media_svc_debug("Update is successful. Sending noti for this");
960                 _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);
961         } else {
962                 media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
963         }
964
965         _media_svc_destroy_content_info(&content_info);
966         _media_svc_destroy_noti_item(noti_item);
967
968         return ret;
969 }
970
971 int media_svc_rename_folder(MediaSvcHandle *handle, const char *storage_id, const char *src_path, const char *dst_path, uid_t uid)
972 {
973         sqlite3 *db_handle = (sqlite3 *)handle;
974         int ret = MS_MEDIA_ERR_NONE;
975
976         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
977         media_svc_retvm_if(src_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
978         media_svc_retvm_if(dst_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "dst_path is NULL");
979
980         media_svc_debug("Src path : %s,  Dst Path : %s", src_path, dst_path);
981
982         ret = _media_svc_sql_begin_trans(handle, uid);
983         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
984
985         /* Update all folder record's path, which are matched by old parent path */
986         char *update_folder_path_sql = NULL;
987         char src_path_slash[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
988         char dst_path_slash[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
989
990         snprintf(src_path_slash, sizeof(src_path_slash), "%s/", src_path);
991         snprintf(dst_path_slash, sizeof(dst_path_slash), "%s/", dst_path);
992
993         update_folder_path_sql = sqlite3_mprintf("UPDATE folder SET path = REPLACE( path, '%q', '%q');", src_path_slash, dst_path_slash);
994
995         /*ret = _media_svc_sql_query(handle, update_folder_path_sql, uid); */
996         ret = media_db_request_update_db_batch(update_folder_path_sql, uid);
997         sqlite3_free(update_folder_path_sql);
998
999         if (ret != SQLITE_OK) {
1000                 media_svc_error("failed to update folder path");
1001                 _media_svc_sql_rollback_trans(handle, uid);
1002
1003                 return MS_MEDIA_ERR_DB_INTERNAL;
1004         }
1005
1006         /* Update all folder record's modified date, which are changed above */
1007         char *update_folder_modified_time_sql = NULL;
1008         time_t date;
1009         time(&date);
1010
1011         update_folder_modified_time_sql = sqlite3_mprintf("UPDATE folder SET modified_time = %d where path like '%q';", date, dst_path);
1012
1013         ret = media_db_request_update_db_batch(update_folder_modified_time_sql, uid);
1014         /*ret = _media_svc_sql_query(handle, update_folder_modified_time_sql, uid); */
1015         sqlite3_free(update_folder_modified_time_sql);
1016
1017         if (ret != SQLITE_OK) {
1018                 media_svc_error("failed to update folder modified time");
1019                 _media_svc_sql_rollback_trans(handle, uid);
1020
1021                 return MS_MEDIA_ERR_DB_INTERNAL;
1022         }
1023
1024         /* Update all items */
1025         char *select_all_sql = NULL;
1026         sqlite3_stmt *sql_stmt = NULL;
1027         char dst_child_path[MEDIA_SVC_PATHNAME_SIZE + 1];
1028
1029         snprintf(dst_child_path, sizeof(dst_child_path), "%s/%%", dst_path);
1030
1031         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);
1032
1033         media_svc_debug("[SQL query] : %s", select_all_sql);
1034
1035         ret = _media_svc_sql_prepare_to_step(db_handle, select_all_sql, &sql_stmt);
1036         if (ret != MS_MEDIA_ERR_NONE) {
1037                 media_svc_error("error when media_svc_rename_folder. err = [%d]", ret);
1038                 _media_svc_sql_rollback_trans(db_handle, uid);
1039                 return ret;
1040         }
1041
1042         while (1) {
1043                 ret = sqlite3_step(sql_stmt);
1044                 if (ret != SQLITE_ROW) {
1045                         media_svc_debug("end of iteration");
1046                         break;
1047                 }
1048
1049                 char media_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1050                 char media_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1051                 char media_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1052                 char media_new_thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1053                 /*int media_type; */
1054                 bool no_thumb = FALSE;
1055
1056                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0))) {
1057                         strncpy(media_uuid,     (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(media_uuid));
1058                         media_uuid[sizeof(media_uuid) - 1] = '\0';
1059                 } else {
1060                         media_svc_error("media UUID is NULL");
1061                         return MS_MEDIA_ERR_DB_INTERNAL;
1062                 }
1063
1064                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) {
1065                         strncpy(media_path,     (const char *)sqlite3_column_text(sql_stmt, 1), sizeof(media_path));
1066                         media_path[sizeof(media_path) - 1] = '\0';
1067                 } else {
1068                         media_svc_error("media path is NULL");
1069                         return MS_MEDIA_ERR_DB_INTERNAL;
1070                 }
1071
1072                 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 2))) {
1073                         strncpy(media_thumb_path,       (const char *)sqlite3_column_text(sql_stmt, 2), sizeof(media_thumb_path));
1074                         media_thumb_path[sizeof(media_thumb_path) - 1] = '\0';
1075                 } else {
1076                         media_svc_debug("media thumb path doesn't exist in DB");
1077                         no_thumb = TRUE;
1078                 }
1079
1080                 /*media_type = sqlite3_column_int(sql_stmt, 3); */
1081
1082                 /* Update path, thumbnail path of this item */
1083                 char *replaced_path = NULL;
1084                 replaced_path = _media_svc_replace_path(media_path, src_path, dst_path);
1085                 if (replaced_path == NULL) {
1086                         media_svc_error("_media_svc_replace_path failed");
1087                         SQLITE3_FINALIZE(sql_stmt);
1088                         _media_svc_sql_rollback_trans(handle, uid);
1089                         return MS_MEDIA_ERR_INTERNAL;
1090                 }
1091
1092                 media_svc_debug("New media path : %s", replaced_path);
1093                 media_svc_storage_type_e storage_type;
1094
1095                 if (!no_thumb) {
1096                         /* If old thumb path is default or not */
1097                         if (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) == 0) {
1098                                 strncpy(media_new_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(media_new_thumb_path));
1099                         } else {
1100                                 ret = _media_svc_get_store_type_by_path(replaced_path, &storage_type, uid);
1101                                 if (ret != MS_MEDIA_ERR_NONE) {
1102                                         media_svc_error("_media_svc_get_store_type_by_path failed : %d", ret);
1103                                         SAFE_FREE(replaced_path);
1104                                         _media_svc_sql_rollback_trans(handle, uid);
1105                                         return ret;
1106                                 }
1107
1108                                 ret = _media_svc_get_thumbnail_path(storage_type, media_new_thumb_path, replaced_path, THUMB_EXT, uid);
1109                                 if (ret != MS_MEDIA_ERR_NONE) {
1110                                         media_svc_error("_media_svc_get_thumbnail_path failed : %d", ret);
1111                                         SAFE_FREE(replaced_path);
1112                                         SQLITE3_FINALIZE(sql_stmt);
1113                                         _media_svc_sql_rollback_trans(handle, uid);
1114                                         return ret;
1115                                 }
1116                         }
1117
1118                         /*media_svc_debug("New media thumbnail path : %s", media_new_thumb_path); */
1119                 }
1120
1121                 char *update_item_sql = NULL;
1122
1123                 if (no_thumb) {
1124                         update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q' WHERE media_uuid='%q'", replaced_path, media_uuid);
1125                 } else {
1126 #if 0
1127                         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1128                                 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);
1129                         } else {
1130                                 update_item_sql = sqlite3_mprintf("UPDATE media SET path='%q', thumbnail_path='%q' WHERE media_uuid='%q'", replaced_path, media_thumb_path, media_uuid);
1131                         }
1132 #else
1133                         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);
1134 #endif
1135                 }
1136
1137                 ret = media_db_request_update_db_batch(update_item_sql, uid);
1138                 /*ret = _media_svc_sql_query(handle, update_item_sql, uid); */
1139                 sqlite3_free(update_item_sql);
1140                 SAFE_FREE(replaced_path);
1141
1142                 if (ret != SQLITE_OK) {
1143                         media_svc_error("failed to update item");
1144                         SQLITE3_FINALIZE(sql_stmt);
1145                         _media_svc_sql_rollback_trans(handle, uid);
1146
1147                         return MS_MEDIA_ERR_DB_INTERNAL;
1148                 }
1149
1150                 /* Rename thumbnail file of file system */
1151 /*              if ((!no_thumb) && (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) */
1152 /*                              && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) { */
1153                 if ((!no_thumb) && (strncmp(media_thumb_path, _media_svc_get_thumb_default_path(uid), sizeof(_media_svc_get_thumb_default_path(uid))) != 0)) {
1154                         ret = _media_svc_rename_file(media_thumb_path, media_new_thumb_path);
1155                         if (ret != MS_MEDIA_ERR_NONE) {
1156                                 media_svc_error("_media_svc_rename_file failed : %d", ret);
1157                         }
1158                 }
1159         }
1160
1161         SQLITE3_FINALIZE(sql_stmt);
1162
1163         ret = _media_svc_sql_end_trans(handle, uid);
1164         if (ret != MS_MEDIA_ERR_NONE) {
1165                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
1166                 _media_svc_sql_rollback_trans(handle, uid);
1167                 return ret;
1168         }
1169
1170         media_svc_debug("Folder update is successful. Sending noti for this");
1171         /* Get notification info */
1172         media_svc_noti_item *noti_item = NULL;
1173         ret = _media_svc_get_noti_info(db_handle, storage_id, dst_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1174         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1175
1176         _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, src_path, -1, noti_item->media_uuid, NULL);
1177         _media_svc_destroy_noti_item(noti_item);
1178
1179         return MS_MEDIA_ERR_NONE;
1180 }
1181
1182 int media_svc_request_update_db(const char *db_query, uid_t uid)
1183 {
1184         media_svc_retvm_if(!STRING_VALID(db_query), MS_MEDIA_ERR_INVALID_PARAMETER, "db_query is NULL");
1185
1186         return _media_svc_sql_query(NULL, db_query, uid);
1187 }
1188
1189 int media_svc_send_dir_update_noti(MediaSvcHandle *handle, const char *dir_path)
1190 {
1191         int ret = MS_MEDIA_ERR_NONE;
1192         sqlite3 *db_handle = (sqlite3 *)handle;
1193
1194         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1195         media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");
1196
1197         /* Get notification info */
1198         media_svc_noti_item *noti_item = NULL;
1199         ret = _media_svc_get_noti_info(db_handle, DEFAULT_MEDIA_SVC_STORAGE_ID, dir_path, MS_MEDIA_ITEM_DIRECTORY, &noti_item);
1200         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1201
1202         ret = _media_svc_publish_noti(MS_MEDIA_ITEM_DIRECTORY, MS_MEDIA_ITEM_UPDATE, dir_path, -1, noti_item->media_uuid, NULL);
1203         _media_svc_destroy_noti_item(noti_item);
1204
1205         return ret;
1206 }
1207
1208 int media_svc_count_invalid_items_in_folder(MediaSvcHandle *handle, const char *folder_path, int *count)
1209 {
1210         sqlite3 *db_handle = (sqlite3 *)handle;
1211
1212         media_svc_debug_func();
1213
1214         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1215         media_svc_retvm_if(folder_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
1216         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1217
1218         return _media_svc_count_invalid_folder_items(db_handle, folder_path, count);
1219 }
1220
1221 int media_svc_check_db_upgrade(MediaSvcHandle *handle, uid_t uid)
1222 {
1223         sqlite3 *db_handle = (sqlite3 *)handle;
1224
1225         media_svc_debug_func();
1226         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1227
1228         return _media_svc_check_db_upgrade(db_handle, uid);
1229 }
1230
1231 int media_svc_check_db_corrupt(MediaSvcHandle *handle)
1232 {
1233         sqlite3 *db_handle = (sqlite3 *)handle;
1234
1235         media_svc_debug_func();
1236         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1237
1238         return _media_db_check_corrupt(db_handle);
1239 }
1240
1241 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)
1242 {
1243         sqlite3 *db_handle = (sqlite3 *)handle;
1244
1245         media_svc_debug_func();
1246
1247         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1248         media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
1249
1250         return _media_svc_get_all_folders(db_handle, start_path, folder_list, modified_time_list, item_num_list, count);
1251 }
1252
1253 int media_svc_update_folder_time(MediaSvcHandle *handle, const char *folder_path, uid_t uid)
1254 {
1255         int ret = MS_MEDIA_ERR_NONE;
1256         sqlite3 *db_handle = (sqlite3 *)handle;
1257         time_t sto_time = 0;
1258         int cur_time =  _media_svc_get_file_time(folder_path);
1259         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1260
1261         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1262
1263         ret = _media_svc_get_folder_info_by_foldername(db_handle, folder_path, folder_uuid, &sto_time);
1264         if (ret == MS_MEDIA_ERR_NONE) {
1265                 if (sto_time != cur_time) {
1266                         ret = _media_svc_update_folder_modified_time_by_folder_uuid(db_handle, folder_uuid, folder_path, FALSE, uid);
1267                 }
1268         }
1269
1270         return ret;
1271 }
1272
1273 int media_svc_update_item_begin(MediaSvcHandle *handle, int data_cnt)
1274 {
1275         sqlite3 *db_handle = (sqlite3 *)handle;
1276
1277         media_svc_debug("Transaction data count : [%d]", data_cnt);
1278
1279         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1280         media_svc_retvm_if(data_cnt < 1, MS_MEDIA_ERR_INVALID_PARAMETER, "data_cnt shuld be bigger than 1");
1281
1282         g_media_svc_update_item_data_cnt  = data_cnt;
1283         g_media_svc_update_item_cur_data_cnt  = 0;
1284
1285         return MS_MEDIA_ERR_NONE;
1286 }
1287
1288 int media_svc_update_item_end(MediaSvcHandle *handle, uid_t uid)
1289 {
1290         int ret = MS_MEDIA_ERR_NONE;
1291         sqlite3 *db_handle = (sqlite3 *)handle;
1292
1293         media_svc_debug_func();
1294
1295         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1296
1297         if (g_media_svc_update_item_cur_data_cnt  > 0) {
1298
1299                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1300         }
1301
1302         g_media_svc_update_item_data_cnt  = 1;
1303         g_media_svc_update_item_cur_data_cnt  = 0;
1304
1305         return ret;
1306 }
1307
1308
1309 int media_svc_update_item_meta(MediaSvcHandle *handle, const char *file_path, int storage_type, uid_t uid)
1310 {
1311         int ret = MS_MEDIA_ERR_NONE;
1312         sqlite3 *db_handle = (sqlite3 *)handle;
1313
1314         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1315
1316         media_svc_media_type_e media_type;
1317
1318         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1319         media_svc_retvm_if(!STRING_VALID(file_path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
1320
1321         if ((storage_type != MEDIA_SVC_STORAGE_INTERNAL) && (storage_type != MEDIA_SVC_STORAGE_EXTERNAL)) {
1322                 media_svc_error("storage type is incorrect[%d]", storage_type);
1323                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1324         }
1325
1326         media_svc_content_info_s content_info;
1327         memset(&content_info, 0, sizeof(media_svc_content_info_s));
1328
1329         /*Set media info*/
1330         ret = _media_svc_set_media_info(&content_info, DEFAULT_MEDIA_SVC_STORAGE_ID, storage_type, file_path, &media_type, FALSE);
1331         if (ret != MS_MEDIA_ERR_NONE) {
1332                 return ret;
1333         }
1334
1335         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1336                 ret = _media_svc_extract_music_metadata_for_update(db_handle, &content_info, media_type);
1337         } else {
1338                 return MS_MEDIA_ERR_NONE;
1339         }
1340         if (ret != MS_MEDIA_ERR_NONE) {
1341                 _media_svc_destroy_content_info(&content_info);
1342                 return ret;
1343         }
1344
1345         if (g_media_svc_update_item_data_cnt == 1) {
1346
1347                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1348                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1349
1350         } else if (g_media_svc_update_item_cur_data_cnt  < (g_media_svc_update_item_data_cnt  - 1)) {
1351
1352                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1353                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1354
1355                 g_media_svc_update_item_cur_data_cnt++;
1356
1357         } else if (g_media_svc_update_item_cur_data_cnt  == (g_media_svc_update_item_data_cnt  - 1)) {
1358
1359                 ret = _media_svc_update_meta_with_data(db_handle, &content_info);
1360                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1361
1362                 ret = _media_svc_list_query_do(db_handle, MEDIA_SVC_QUERY_UPDATE_ITEM, uid);
1363                 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);
1364
1365                 g_media_svc_update_item_cur_data_cnt = 0;
1366
1367         } else {
1368                 media_svc_error("Error in media_svc_update_item_meta");
1369                 _media_svc_destroy_content_info(&content_info);
1370                 return MS_MEDIA_ERR_INTERNAL;
1371         }
1372
1373         _media_svc_destroy_content_info(&content_info);
1374
1375         return ret;
1376 }
1377
1378 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)
1379 {
1380         return _media_svc_publish_noti(update_item, update_type, path, media_type, uuid, mime_type);
1381 }
1382
1383 int media_svc_get_pinyin(MediaSvcHandle *handle, const char *src_str, char **pinyin_str)
1384 {
1385         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1386
1387         return _media_svc_get_pinyin_str(src_str, pinyin_str);
1388 }
1389
1390 int media_svc_check_pinyin_support(bool *support)
1391 {
1392         *support = _media_svc_check_pinyin_support();
1393
1394         return MS_MEDIA_ERR_NONE;
1395 }
1396
1397 static int __media_svc_copy_para_to_content(media_svc_content_info_s *content_info, media_svc_content_info_s *new_content_info)
1398 {
1399         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1400         media_svc_retvm_if(new_content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1401
1402         /*
1403         initialize media content datas
1404         */
1405         int ret = MS_MEDIA_ERR_NONE;
1406         char *media_uuid = NULL;
1407         char *file_name = NULL;
1408
1409         ret = __media_svc_malloc_and_strncpy(&new_content_info->path, content_info->path);
1410         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1411
1412         ret = __media_svc_malloc_and_strncpy(&new_content_info->storage_uuid, content_info->storage_uuid);
1413         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1414
1415         /* Set default GPS value before extracting meta information */
1416         new_content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1417         new_content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1418         new_content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1419
1420         /* Set default value before extracting meta information */
1421         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1422         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1423
1424         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1425         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1426
1427         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
1428         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1429
1430         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
1431         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1432
1433         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1434         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1435
1436         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1437         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1438
1439 /*      ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN); */
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->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1443         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1444
1445         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1446         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1447
1448         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1449         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1450
1451         new_content_info->storage_type = content_info->storage_type;
1452         time(&new_content_info->added_time);
1453
1454         media_uuid = _media_info_generate_uuid();
1455         media_svc_retvm_if(media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Invalid UUID");
1456
1457         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_uuid, media_uuid);
1458         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1459
1460         file_name = g_path_get_basename(content_info->path);
1461         ret = __media_svc_malloc_and_strncpy(&new_content_info->file_name, file_name);
1462         SAFE_FREE(file_name);
1463         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, new_content_info);
1464
1465         new_content_info->played_count = 0;
1466         new_content_info->last_played_time = 0;
1467         new_content_info->last_played_position = 0;
1468         new_content_info->favourate = 0;
1469         new_content_info->media_meta.rating = 0;
1470         /*
1471         end of initializing
1472         */
1473
1474         new_content_info->size = content_info->size;
1475         new_content_info->added_time = content_info->added_time;
1476         new_content_info->modified_time = content_info->modified_time;
1477         new_content_info->is_drm = content_info->is_drm;
1478         new_content_info->media_type = content_info->media_type;
1479
1480         if (STRING_VALID(content_info->mime_type)) {
1481                 ret = __media_svc_malloc_and_strncpy(&new_content_info->mime_type, content_info->mime_type);
1482                 if (ret != MS_MEDIA_ERR_NONE)
1483                         media_svc_error("strcpy mime_type failed");
1484         }
1485
1486         if (STRING_VALID(content_info->thumbnail_path)) {
1487                 ret = __media_svc_malloc_and_strncpy(&new_content_info->thumbnail_path, content_info->thumbnail_path);
1488                 if (ret != MS_MEDIA_ERR_NONE)
1489                         media_svc_error("strcpy thumbnail_path failed");
1490         }
1491
1492         if (STRING_VALID(content_info->media_meta.title)) {
1493                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.title, content_info->media_meta.title);
1494                 if (ret != MS_MEDIA_ERR_NONE)
1495                         media_svc_error("strcpy title failed");
1496         }
1497
1498         if (STRING_VALID(content_info->media_meta.album)) {
1499                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album, content_info->media_meta.album);
1500                 if (ret != MS_MEDIA_ERR_NONE)
1501                         media_svc_error("strcpy album failed");
1502         }
1503
1504         if (STRING_VALID(content_info->media_meta.artist)) {
1505                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.artist, content_info->media_meta.artist);
1506                 if (ret != MS_MEDIA_ERR_NONE)
1507                         media_svc_error("strcpy artist failed");
1508         }
1509
1510         /*
1511                 if(STRING_VALID(content_info->media_meta.album_artist))
1512                 {
1513                         ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.album_artist, content_info->media_meta.album_artist);
1514                         if(ret != MS_MEDIA_ERR_NONE)
1515                                 media_svc_error("strcpy album_artist failed");
1516                 }
1517         */
1518
1519         if (STRING_VALID(content_info->media_meta.genre)) {
1520                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.genre, content_info->media_meta.genre);
1521                 if (ret != MS_MEDIA_ERR_NONE)
1522                         media_svc_error("strcpy genre failed");
1523         }
1524
1525         if (STRING_VALID(content_info->media_meta.composer)) {
1526                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.composer, content_info->media_meta.composer);
1527                 if (ret != MS_MEDIA_ERR_NONE)
1528                         media_svc_error("strcpy composer failed");
1529         }
1530
1531         if (STRING_VALID(content_info->media_meta.year)) {
1532                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.year, content_info->media_meta.year);
1533                 if (ret != MS_MEDIA_ERR_NONE)
1534                         media_svc_error("strcpy year failed");
1535         }
1536
1537         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1538                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.recorded_date, content_info->media_meta.recorded_date);
1539                 if (ret != MS_MEDIA_ERR_NONE)
1540                         media_svc_error("strcpy recorded_date failed");
1541         }
1542
1543         if (STRING_VALID(content_info->media_meta.copyright)) {
1544                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.copyright, content_info->media_meta.copyright);
1545                 if (ret != MS_MEDIA_ERR_NONE)
1546                         media_svc_error("strcpy copyright failed");
1547         }
1548
1549         if (STRING_VALID(content_info->media_meta.track_num)) {
1550                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.track_num, content_info->media_meta.track_num);
1551                 if (ret != MS_MEDIA_ERR_NONE)
1552                         media_svc_error("strcpy track_num failed");
1553         }
1554
1555         if (STRING_VALID(content_info->media_meta.description)) {
1556                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.description, content_info->media_meta.description);
1557                 if (ret != MS_MEDIA_ERR_NONE)
1558                         media_svc_error("strcpy description failed");
1559         }
1560
1561         if (STRING_VALID(content_info->media_meta.weather)) {
1562                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.weather, content_info->media_meta.weather);
1563                 if (ret != MS_MEDIA_ERR_NONE)
1564                         media_svc_error("strcpy description failed");
1565         }
1566
1567         if (STRING_VALID(content_info->media_meta.datetaken)) {
1568                 ret = __media_svc_malloc_and_strncpy(&new_content_info->media_meta.datetaken, content_info->media_meta.datetaken);
1569                 if (ret != MS_MEDIA_ERR_NONE)
1570                         media_svc_error("strcpy datetaken failed");
1571
1572                 new_content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1573                 if (new_content_info->timeline == 0) {
1574                         media_svc_error("Failed to get timeline : %s", new_content_info->media_meta.datetaken);
1575                         new_content_info->timeline = new_content_info->modified_time;
1576                 } else {
1577                         media_svc_debug("Timeline : %ld", new_content_info->timeline);
1578                 }
1579         }
1580
1581         new_content_info->media_meta.bitrate = content_info->media_meta.bitrate;
1582         new_content_info->media_meta.samplerate = content_info->media_meta.samplerate;
1583         new_content_info->media_meta.channel = content_info->media_meta.channel;
1584         new_content_info->media_meta.duration = content_info->media_meta.duration;
1585         new_content_info->media_meta.longitude = content_info->media_meta.longitude;
1586         new_content_info->media_meta.latitude = content_info->media_meta.latitude;
1587         new_content_info->media_meta.altitude = content_info->media_meta.altitude;
1588         new_content_info->media_meta.width = content_info->media_meta.width;
1589         new_content_info->media_meta.height = content_info->media_meta.height;
1590         new_content_info->media_meta.orientation = content_info->media_meta.orientation;
1591         new_content_info->media_meta.rating = content_info->media_meta.rating;
1592
1593         return 0;
1594 }
1595
1596 int media_svc_insert_item_immediately_with_data(MediaSvcHandle *handle, media_svc_content_info_s *content_info, uid_t uid)
1597 {
1598         int ret = MS_MEDIA_ERR_NONE;
1599         sqlite3 *db_handle = (sqlite3 *)handle;
1600         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
1601         bool append_album = FALSE;
1602
1603         /* Checking parameters if they are valid */
1604         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1605         media_svc_retvm_if(content_info == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "content_info is NULL");
1606         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "file_path is NULL");
1607         media_svc_retvm_if(!STRING_VALID(content_info->mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");
1608
1609         if ((content_info->media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (content_info->media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1610                 media_svc_error("invalid media_type condition[%d]", content_info->media_type);
1611                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1612         }
1613
1614         if (content_info->size <= 0) {
1615                 media_svc_error("invalid size condition[%d]", content_info->size);
1616                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1617         }
1618
1619         if (content_info->modified_time <= 0) {
1620                 media_svc_error("invalid modified_time condition[%d]", content_info->modified_time);
1621                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1622         }
1623         media_svc_debug("storage[%d], path[%s], media_type[%d]", content_info->storage_type, content_info->path, content_info->media_type);
1624
1625         media_svc_content_info_s _new_content_info;
1626         memset(&_new_content_info, 0, sizeof(media_svc_content_info_s));
1627
1628         /* set othere data to the structure, which is passed as parameters */
1629         __media_svc_copy_para_to_content(content_info, &_new_content_info);
1630
1631         /* Set or Get folder id */
1632         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);
1633         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1634
1635         ret = __media_svc_malloc_and_strncpy(&_new_content_info.folder_uuid, folder_uuid);
1636         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &_new_content_info);
1637
1638         /* register album table data */
1639
1640         int album_id = 0;
1641         if (_new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_SOUND || _new_content_info.media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
1642                 ret = _media_svc_get_album_id(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, &album_id);
1643
1644                 if (ret != MS_MEDIA_ERR_NONE) {
1645                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1646                                 media_svc_debug("album does not exist. So start to make album art");
1647                                 append_album = TRUE;
1648                         } else {
1649                                 media_svc_debug("other error");
1650                                 append_album = FALSE;
1651                         }
1652                 } else {
1653                         _new_content_info.album_id = album_id;
1654                         append_album = FALSE;
1655
1656                         if ((!strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1657                             (!strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1658
1659                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1660                         } else {
1661
1662                                 media_svc_debug("album already exists. don't need to make album art");
1663                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &_new_content_info.thumbnail_path);
1664                                 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, &_new_content_info);
1665                         }
1666                 }
1667         }
1668
1669         if (append_album == TRUE) {
1670
1671                 if ((strncmp(_new_content_info.media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1672                     (strncmp(_new_content_info.media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1673                         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);
1674                 else
1675                         ret = _media_svc_append_album(handle, _new_content_info.media_meta.album, _new_content_info.media_meta.artist, NULL, &album_id, uid);
1676
1677                 _new_content_info.album_id = album_id;
1678         }
1679
1680         /* Insert to db - calling _media_svc_insert_item_with_data */
1681         ret = _media_svc_insert_item_with_data(db_handle, &_new_content_info, FALSE, FALSE, uid);
1682
1683         if (ret == MS_MEDIA_ERR_NONE) {
1684                 media_svc_debug("Insertion is successful.");
1685         }
1686
1687         if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
1688                 media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
1689         }
1690
1691         _media_svc_destroy_content_info(&_new_content_info);
1692
1693         /* handling returned value - important */
1694         return ret;
1695 }
1696
1697 void media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1698 {
1699         _media_svc_destroy_content_info(content_info);
1700 }
1701
1702 char *media_info_generate_uuid(void)
1703 {
1704         return _media_info_generate_uuid();
1705 }
1706
1707 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)
1708 {
1709         int ret = MS_MEDIA_ERR_NONE;
1710         sqlite3 *db_handle = (sqlite3 *)handle;
1711
1712         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1713         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1714         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
1715
1716         ret = _media_svc_append_storage(db_handle, storage_id, storage_name, storage_path, storage_account, storage_type, uid);
1717         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);
1718
1719         ret = _media_svc_create_media_table_with_id(db_handle, storage_id, uid);
1720         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
1721
1722         if (storage_type == MEDIA_SVC_STORAGE_CLOUD) {
1723                 ret = _media_svc_update_media_view(db_handle, uid);
1724                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1725         }
1726
1727         return ret;
1728 }
1729
1730 int media_svc_delete_storage(MediaSvcHandle *handle, const char *storage_id, uid_t uid)
1731 {
1732         int ret = MS_MEDIA_ERR_NONE;
1733         sqlite3 *db_handle = (sqlite3 *)handle;
1734
1735         media_svc_retvm_if(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
1736         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
1737
1738         ret = _media_svc_delete_storage(db_handle, storage_id, uid);
1739         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "remove storage failed : %d", ret);
1740
1741         ret = _media_svc_drop_media_table(db_handle, storage_id, uid);
1742         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "drop table failed : %d", ret);
1743
1744         ret = _media_svc_update_media_view(db_handle, uid);
1745         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update media view failed : %d", ret);
1746
1747         return ret;
1748 }
1749
1750 int media_svc_request_extract_all_thumbs(uid_t uid)
1751 {
1752         int ret = MS_MEDIA_ERR_NONE;
1753
1754         ret =  _media_svc_request_extract_all_thumbs(uid);
1755         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_request_extract_all_thumbs failed : %d", ret);
1756
1757         return ret;
1758 }