summaryrefslogtreecommitdiff
path: root/src/common/media-svc.c
blob: 5f28f29056697d131a1345c5e6b1dbf5fbb06fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
 * libmedia-service
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-svc-media.h"
#include "media-svc-debug.h"
#include "media-svc-util.h"
#include "media-svc-db-utils.h"
#include "media-svc-env.h"
#include "media-svc-media-folder.h"
#include "media-svc-album.h"
#include "media-svc-noti.h"
#include "media-svc-storage.h"

#include <iniparser.h>

#define CONTENT_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"

//static __thread int g_media_svc_data_cnt = 0;
static __thread int g_media_svc_cur_data_cnt = 0;

/* Flag for items to be published by notification */
static __thread bool g_insert_with_noti = false;

#define BATCH_ITEM_COUNT_MAX 100

int media_svc_check_table_exist(sqlite3 *handle, bool *exist)
{
	return _media_svc_check_table_exist(handle, exist);
}

int media_svc_create_table(uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	media_svc_debug_fenter();

	ret = _media_svc_init_table_query();
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_init_table_query fail.");
		goto ERROR;
	}

	/*create media table*/
	ret = _media_svc_make_table_query(DB_TABLE_MEDIA, DB_LIST_MEDIA, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create folder table*/
	ret = _media_svc_make_table_query(DB_TABLE_FOLDER, DB_LIST_FOLDER, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create playlist_map table*/
	ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST_MAP, DB_LIST_PLAYLIST_MAP, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create playlist table*/
	ret = _media_svc_make_table_query(DB_TABLE_PLAYLIST, DB_LIST_PLAYLIST, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/* create album table*/
	ret = _media_svc_make_table_query(DB_TABLE_ALBUM, DB_LIST_ALBUM, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create tag_map table*/
	ret = _media_svc_make_table_query(DB_TABLE_TAG_MAP, DB_LIST_TAG_MAP, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create tag table*/
	ret = _media_svc_make_table_query(DB_TABLE_TAG, DB_LIST_TAG, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create bookmark table*/
	ret = _media_svc_make_table_query(DB_TABLE_BOOKMARK, DB_LIST_BOOKMARK, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create storage table from tizen 2.4 */
	ret = _media_svc_make_table_query(DB_TABLE_STORAGE, DB_LIST_STORAGE, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	/*create face table. from tizen 3.0*/
	ret = _media_svc_make_table_query(DB_TABLE_FACE_SCAN_LIST, DB_LIST_FACE_SCAN_LIST, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}

	ret = _media_svc_make_table_query(DB_TABLE_FACE, DB_LIST_FACE, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_make_table_query fail.");
		goto ERROR;
	}
ERROR:
	_media_svc_destroy_table_query();

	media_svc_debug_fleave();

	return ret;
}

int media_svc_check_item_exist_by_path(sqlite3 *handle, const char *storage_id, const char *path)
{
	return _media_svc_check_data_by_path(handle, path);
}

int media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
{
	return _media_svc_get_modified_time(handle, path, modified_time);
}

int media_svc_insert_item_begin(bool with_noti, int from_pid)
{
	g_media_svc_cur_data_cnt = 0;

	/* Prepare for making noti item list */
	if (with_noti) {
		media_svc_debug("making noti list from pid[%d]", from_pid);
		_media_svc_initialize_noti_list();
		_media_svc_set_noti_from_pid(from_pid);
		g_insert_with_noti = true;
	}

	return MS_MEDIA_ERR_NONE;
}

int media_svc_insert_item_end(uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;

	media_svc_debug_fenter();

	if (g_media_svc_cur_data_cnt > 0) {
		ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
		if (g_insert_with_noti) {
			media_svc_debug("sending noti list");
			_media_svc_publish_noti_list();
			g_insert_with_noti = false;
			_media_svc_set_noti_from_pid(-1);
		}
	}

	g_media_svc_cur_data_cnt = 0;

	return ret;
}

int media_svc_insert_item_bulk(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	long long int folder_id = 0;

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
	media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");

	media_svc_content_info_s content_info;
	memset(&content_info, 0, sizeof(media_svc_content_info_s));

	/*Set media info*/
	/* if drm_contentinfo is not NULL, the file is OMA DRM.*/
	ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
	if (ret != MS_MEDIA_ERR_NONE)
		return ret;

	switch (content_info.media_type) {
	case MEDIA_SVC_MEDIA_TYPE_IMAGE:
		ret = _media_svc_extract_image_metadata(&content_info);
		break;
	case MEDIA_SVC_MEDIA_TYPE_VIDEO:
	case MEDIA_SVC_MEDIA_TYPE_SOUND:
	case MEDIA_SVC_MEDIA_TYPE_MUSIC:
		ret = _media_svc_extract_media_metadata(handle, true, &content_info, uid);
		break;
	case MEDIA_SVC_MEDIA_TYPE_BOOK:
		ret = _media_svc_extract_book_metadata(&content_info);
		/* The 'TITLE' should always be filled in */
		if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
			g_free(content_info.media_meta.title);
			content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		}
		break;
	default:
		/* The 'TITLE' should always be filled in */
		content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		break;
	}

	media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

	/*Set or Get folder id*/
	ret = _media_svc_get_and_append_folder_id_by_path(handle, true, storage_id, path, storage_type, &folder_id, uid);
	media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

	content_info.folder_id = folder_id;
	media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);

	ret = _media_svc_insert_item_with_data(true, &content_info, true, uid);
	media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

	if (g_insert_with_noti)
		_media_svc_insert_item_to_noti_list(&content_info);

	/* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
	if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
		ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
		media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

		if (g_insert_with_noti)
			_media_svc_publish_noti_list();

		g_media_svc_cur_data_cnt = 0;
	}

	_media_svc_destroy_content_info(&content_info);

	return MS_MEDIA_ERR_NONE;
}

int media_svc_insert_item_immediately(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	long long int folder_id = 0;

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
	media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");

	media_svc_content_info_s content_info;
	memset(&content_info, 0, sizeof(media_svc_content_info_s));

	/*Set media info*/
	ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, false);
	if (ret != MS_MEDIA_ERR_NONE)
		return ret;

	switch (content_info.media_type) {
	case MEDIA_SVC_MEDIA_TYPE_IMAGE:
		ret = _media_svc_extract_image_metadata(&content_info);
		break;
	case MEDIA_SVC_MEDIA_TYPE_VIDEO:
	case MEDIA_SVC_MEDIA_TYPE_SOUND:
	case MEDIA_SVC_MEDIA_TYPE_MUSIC:
		ret = _media_svc_extract_media_metadata(handle, false, &content_info, uid);
		break;
	case MEDIA_SVC_MEDIA_TYPE_BOOK:
		ret = _media_svc_extract_book_metadata(&content_info);
		/* The 'TITLE' should always be filled in */
		if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
			g_free(content_info.media_meta.title);
			content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		}
		break;
	default:
		/* The 'TITLE' should always be filled in */
		content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		break;
	}

	media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

	/*Set or Get folder id*/
	ret = _media_svc_get_and_append_folder_id_by_path(handle, false, storage_id, path, storage_type, &folder_id, uid);
	media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, &content_info);

	content_info.folder_id = folder_id;
	media_svc_retv_del_if(content_info.folder_id <= 0, MS_MEDIA_ERR_INTERNAL, &content_info);

	/* Extracting thumbnail */
	if (content_info.thumbnail_path == NULL) {
		if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
			char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };

			ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
			if (ret == MS_MEDIA_ERR_NONE)
				content_info.thumbnail_path = g_strdup(thumb_path);
		}
	}

	ret = _media_svc_insert_item_with_data(false, &content_info, false, uid);

	if (ret == MS_MEDIA_ERR_NONE) {
		media_svc_debug("Insertion is successful. Sending noti for this");
		_media_svc_publish_noti(MS_MEDIA_ITEM_INSERT, content_info.path, content_info.media_type, content_info.media_uuid, content_info.mime_type);
	} else if (ret == MS_MEDIA_ERR_DB_CONSTRAINT_FAIL) {
		media_svc_error("This item is already inserted. This may be normal operation because other process already did this");
	}

	_media_svc_destroy_content_info(&content_info);
	return ret;
}

int media_svc_move_item(sqlite3 *handle,
						const char *src_path,
						const char *dest_path,
						const char *media_id,
						int media_type,
						const char *mime_type,
						uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *file_name = NULL;
	char *folder_path = NULL;
	int modified_time = 0;
	long long int folder_id = 0;
	char old_thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
	char dst_stg_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
	ms_user_storage_type_e org_stg_type = MS_USER_STORAGE_INTERNAL;
	ms_user_storage_type_e dst_stg_type = MS_USER_STORAGE_INTERNAL;

	media_svc_debug_fenter();

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(!STRING_VALID(src_path), MS_MEDIA_ERR_INVALID_PARAMETER, "src_path is NULL");
	media_svc_retvm_if(!STRING_VALID(dest_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dest_path is NULL");
	media_svc_retvm_if(!STRING_VALID(media_id), MS_MEDIA_ERR_INVALID_PARAMETER, "media_id is NULL");
	media_svc_retvm_if(!STRING_VALID(mime_type), MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is NULL");

	/* Get storage_id */
	ret = _media_svc_get_storage_uuid(handle, dest_path, dst_stg_id, uid);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
	/* Get storage_type */
	ret = ms_user_get_storage_type(uid, src_path, &org_stg_type);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
	ret = ms_user_get_storage_type(uid, dest_path, &dst_stg_type);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/*check and update folder*/
	ret = _media_svc_get_and_append_folder_id_by_path(handle, false, dst_stg_id, dest_path, dst_stg_type, &folder_id, uid);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/*get filename*/
	file_name = g_path_get_basename(dest_path);

	/*get modified_time*/
	modified_time = _media_svc_get_file_time(dest_path);

	/*get old thumbnail_path and remove thumbnail */
	ret = _media_svc_get_thumbnail_path_by_path(handle, src_path, old_thumb_path);
	if ((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD)) {
		media_svc_error("_media_svc_get_thumbnail_path_by_path failed");
		g_free(file_name);
		return ret;
	}

	_media_svc_remove_file(old_thumb_path);

	/*move item*/
	ret = _media_svc_update_item_by_path(src_path, dst_stg_id, dst_stg_type, dest_path, file_name, modified_time, folder_id, uid);
	g_free(file_name);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	media_svc_debug("Move is successful. Sending noti for this");
	_media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, src_path, media_type, media_id, mime_type);

	/*update folder modified_time*/
	folder_path = g_path_get_dirname(dest_path);
	ret = _media_svc_update_folder_modified_time(folder_path, uid);
	g_free(folder_path);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	return MS_MEDIA_ERR_NONE;
}

int media_svc_set_item_validity(const char *path, int validity, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;

	ret = _media_svc_update_item_validity(path, validity, true, uid);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
	if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
		ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
		media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

		g_media_svc_cur_data_cnt = 0;
	}

	return ret;
}

int media_svc_delete_item_by_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = {0, };
	media_svc_noti_item *noti_item = NULL;

	media_svc_debug_fenter();

	/*Get thumbnail path to delete*/
	ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
	media_svc_retv_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret);

	/* Get notification info */
	ret = _media_svc_get_noti_info(handle, path, &noti_item);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/*Delete item*/
	ret = _media_svc_delete_item_by_path(path, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("_media_svc_delete_item_by_path failed : %d", ret);
		_media_svc_destroy_noti_item(noti_item);

		return ret;
	}

	/* Send notification */
	media_svc_debug("Deletion is successful. Sending noti for this");
	_media_svc_publish_noti(MS_MEDIA_ITEM_DELETE, path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
	_media_svc_destroy_noti_item(noti_item);

	/*Delete thumbnail*/
	_media_svc_remove_file(thumb_path);

	return MS_MEDIA_ERR_NONE;
}

int media_svc_refresh_item(sqlite3 *handle, bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
	media_svc_content_info_s content_info = {0, };
	media_svc_noti_item *noti_item = NULL;

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
	media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");

	/*Set media info*/
	ret = _media_svc_set_media_info(&content_info, storage_id, storage_type, path, true);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	/* Initialize thumbnail information to remake thumbnail. */
	ret = _media_svc_get_thumbnail_path_by_path(handle, path, thumb_path);
	if (ret != MS_MEDIA_ERR_NONE && ret != MS_MEDIA_ERR_DB_NO_RECORD)
		goto REFRESH_FINALIZE;

	if (STRING_VALID(thumb_path)) {
		_media_svc_remove_file(thumb_path);

		ret = _media_svc_update_thumbnail_path(path, NULL, uid);
		if (ret != MS_MEDIA_ERR_NONE)
			goto REFRESH_FINALIZE;
	}

	/* Get notification info */
	ret = _media_svc_get_noti_info(handle, path, &noti_item);
	if (ret != MS_MEDIA_ERR_NONE)
		goto REFRESH_FINALIZE;

	content_info.media_type = noti_item->media_type;
	content_info.mime_type = g_strdup(noti_item->mime_type);

	switch (content_info.media_type) {
	case MEDIA_SVC_MEDIA_TYPE_IMAGE:
		ret = _media_svc_extract_image_metadata(&content_info);
		break;
	case MEDIA_SVC_MEDIA_TYPE_VIDEO:
	case MEDIA_SVC_MEDIA_TYPE_SOUND:
	case MEDIA_SVC_MEDIA_TYPE_MUSIC:
		ret = _media_svc_extract_media_metadata(handle, is_direct, &content_info, uid);
		break;
	case MEDIA_SVC_MEDIA_TYPE_BOOK:
		ret = _media_svc_extract_book_metadata(&content_info);
		/* The 'TITLE' should always be filled in */
		if (!content_info.media_meta.title || strlen(content_info.media_meta.title) == 0) {
			g_free(content_info.media_meta.title);
			content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		}
		break;
	default:
		/* The 'TITLE' should always be filled in */
		content_info.media_meta.title = _media_svc_get_title_by_path(content_info.path);
		break;
	}

	if (ret != MS_MEDIA_ERR_NONE)
		goto REFRESH_FINALIZE;

	/* Extracting thumbnail */
	if (content_info.thumbnail_path == NULL) {
		if (content_info.media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE || content_info.media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
			memset(thumb_path, 0, sizeof(thumb_path));

			ret = _media_svc_create_thumbnail(content_info.path, thumb_path, content_info.media_type, uid);
			if (ret == MS_MEDIA_ERR_NONE)
				content_info.thumbnail_path = g_strdup(thumb_path);
		}
	}

	ret = _media_svc_update_item_with_data(is_direct, &content_info, uid);

	if (ret == MS_MEDIA_ERR_NONE) {
		if (is_direct) {
			/* To avoid over-occupying memory, update per BATCH_ITEM_COUNT_MAX. */
			if (++g_media_svc_cur_data_cnt == BATCH_ITEM_COUNT_MAX) {
				ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
				if (ret != MS_MEDIA_ERR_NONE)
					goto REFRESH_FINALIZE;

				g_media_svc_cur_data_cnt = 0;
			}
		} else {
			/* Except scanner case */
			media_svc_debug("Update is successful. Sending noti for this");
			_media_svc_publish_noti(MS_MEDIA_ITEM_UPDATE, content_info.path, noti_item->media_type, noti_item->media_uuid, noti_item->mime_type);
		}
	} else {
		media_svc_error("_media_svc_update_item_with_data failed : %d", ret);
	}

REFRESH_FINALIZE:
	_media_svc_destroy_content_info(&content_info);
	_media_svc_destroy_noti_item(noti_item);

	return ret;
}

int media_svc_send_dir_update_noti(const char *dir_path, const char *folder_id, media_item_update_type_e update_type, int pid)
{
	media_svc_retvm_if(!STRING_VALID(dir_path), MS_MEDIA_ERR_INVALID_PARAMETER, "dir_path is NULL");

	return _media_svc_publish_dir_noti(update_type, dir_path, folder_id, pid);
}

static void __media_svc_noti_all_storage(sqlite3 *handle, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *root_path = NULL;
	GPtrArray *path_list = NULL;
	guint i = 0;

	ret = ms_user_get_internal_root_path(uid, &root_path);
	media_svc_retm_if(ret != MS_MEDIA_ERR_NONE, "Fail to get root path");

	ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
	if (ret != MS_MEDIA_ERR_NONE)
		media_svc_error("Fail to send noti");

	g_free(root_path);

	path_list = g_ptr_array_new_with_free_func(g_free);
	_media_svc_get_storage_path(handle, &path_list);

	for (i = 0; i < path_list->len; i++) {
		root_path = g_ptr_array_index(path_list, i);

		ret = _media_svc_publish_dir_noti(MS_MEDIA_ITEM_UPDATE, root_path, NULL, 0);
		if (ret != MS_MEDIA_ERR_NONE)
			media_svc_error("Fail to send noti");
	}

	g_ptr_array_free(path_list, TRUE);
}

static void __media_svc_foreach_update_media(gpointer data, gpointer user_data)
{
	media_svc_content_info_s content_info = {0, };

	if (_media_svc_extract_music_metadata_for_update(&content_info, (const char *)data) != MS_MEDIA_ERR_NONE) {
		media_svc_error("Fail to extract metadata");
		_media_svc_destroy_content_info(&content_info);
		return;
	}

	if (_media_svc_update_meta_with_data(&content_info) != MS_MEDIA_ERR_NONE)
		media_svc_error("Fail to append item[%s]", content_info.path);

	_media_svc_destroy_content_info(&content_info);
}

int media_svc_update_item_meta(sqlite3 *handle, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;
	char *sql = NULL;
	GList *path_list = NULL;

	sql = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=3 AND validity=1", DB_TABLE_MEDIA);
	ret = _media_svc_get_media(handle, sql, &path_list);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get media list");

	if (path_list) {
		g_list_foreach(path_list, __media_svc_foreach_update_media, NULL);
		g_list_free_full(path_list, g_free);

		ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_SCANNER, uid);
		media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_list_query_do failed");
	}

	/* Noti for this */
	__media_svc_noti_all_storage(handle, uid);

	return ret;
}

int media_svc_publish_noti(media_item_update_type_e update_type, const char *path, media_type_e media_type, const char *uuid, const char *mime_type)
{
	return _media_svc_publish_noti(update_type, path, media_type, uuid, mime_type);
}

int media_svc_get_pinyin(const char *src_str, char **pinyin_str)
{
	return _media_svc_get_pinyin_str(src_str, pinyin_str);
}

int media_svc_check_pinyin_support(bool *support)
{
	*support = _media_svc_check_pinyin_support();

	return MS_MEDIA_ERR_NONE;
}

int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");

	ret = _media_svc_update_storage_validity(storage_id, validity, uid);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "update storage validity failed: %d", ret);

	return ret;
}

int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
{
	return _media_svc_get_storage_uuid(handle, path, storage_id, uid);
}

int media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
{
	return _media_svc_check_storage(handle, storage_id, storage_path, validity);
}

int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid)
{
	return _media_svc_update_storage_path(handle, storage_id, storage_path, uid);
}

int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
{
	int ret = MS_MEDIA_ERR_NONE;

	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
	media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");

	ret = _media_svc_append_storage(storage_id, storage_path, storage_type, uid);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "append storage failed : %d", ret);

	/* Remove external storage that validity is 0 */
	ret = _media_svc_delete_invalid_storage(handle, uid);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Delete invalid storage failed : %d", ret);

	return ret;
}

int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, uid_t uid)
{
	media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL");
	media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
	media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
	media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");

	return _media_svc_append_by_folder_path(handle, storage_id, path, storage_type, uid);
}

int media_svc_set_folder_validity(const char *start_path, int validity, bool is_recursive, uid_t uid)
{
	return _media_svc_set_folder_validity(true, start_path, validity, is_recursive, uid);
}

int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *folder_path)
{
	return _media_svc_check_folder_by_path(handle, folder_path);
}

int media_svc_append_query(const char *query, uid_t uid)
{
	return _media_svc_append_query_list(query, uid);
}

int media_svc_send_query(uid_t uid)
{
	return _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
}

int media_svc_get_media_type(const char *path, int *mediatype)
{
	return _media_svc_get_media_type(path, mediatype);
}

int media_svc_create_thumbnail(const char *file_path, int media_type, uid_t uid, char **thumbnail_path)
{
	int ret = MS_MEDIA_ERR_NONE;
	char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = { 0, };
	char *sql = NULL;

	// 1. Check media type
	if (media_type != MS_MEDIA_IMAGE && media_type != MS_MEDIA_VIDEO)
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;

	// 2. try to create thumbnail
	ret = _media_svc_create_thumbnail(file_path, thumb_path, media_type, uid);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("Failed to create thumbnail [%d]", ret);
		if (ret == MS_MEDIA_ERR_THUMB_UNSUPPORTED)
			return ret;
	}

	// 3. Update creation result to media db
	sql = sqlite3_mprintf("UPDATE %q SET media_thumbnail_path='%q' WHERE media_path='%q';", DB_TABLE_MEDIA, thumb_path, file_path);

	ret = _media_svc_sql_query(sql, uid);
	SQLITE3_SAFE_FREE(sql);
	if (ret != MS_MEDIA_ERR_NONE) {
		media_svc_error("Failed to update media db [%d]", ret);
		*thumbnail_path = g_strdup("");
	} else {
		*thumbnail_path = g_strdup(thumb_path);
	}

	return ret;
}

static int __media_svc_get_ebook_search_type(void)
{
	dictionary *dict = NULL;
	static int _ebook_search_type = -1;

	if (_ebook_search_type == -1) {
		dict = iniparser_load(CONTENT_INI_DEFAULT_PATH);
		if (!dict) {
			media_svc_error("%s load failed. Use direct search.", CONTENT_INI_DEFAULT_PATH);
			return MEDIA_SVC_SEARCH_TYPE_DIRECT;
		}

		_ebook_search_type = iniparser_getint(dict, "media-content-config:ebook_search_type", 0);
		media_svc_debug("ebook_search_type [%d]", _ebook_search_type);

		iniparser_freedict(dict);
	}

	return _ebook_search_type;
}

int media_svc_get_book_by_keyword(sqlite3 *handle, const char *keyword, uid_t uid, GList **result)
{
	int ret = MS_MEDIA_ERR_NONE;
	GList *item_list = NULL;
	GList *iter = NULL;
	char *query = NULL;

	media_svc_retvm_if(!handle, MS_MEDIA_ERR_INVALID_PARAMETER, "db handle is NULL");
	media_svc_retvm_if(!keyword, MS_MEDIA_ERR_INVALID_PARAMETER, "keyword is NULL");
	media_svc_retvm_if(!result, MS_MEDIA_ERR_INVALID_PARAMETER, "result is NULL");

	query = sqlite3_mprintf("SELECT media_path FROM %q WHERE media_type=%d AND validity=1;",
							DB_TABLE_MEDIA, MEDIA_SVC_MEDIA_TYPE_BOOK);

	ret = _media_svc_get_media(handle, query, &item_list);
	media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_media failed");

	if (__media_svc_get_ebook_search_type() == MEDIA_SVC_SEARCH_TYPE_DB) {
		for (iter = item_list; iter; iter = g_list_next(iter))
			_media_svc_update_wordbook((char *)iter->data, uid);

		_media_svc_clean_wordbook(uid);

		if (!_media_svc_get_matched_list(keyword, uid, result))
			media_svc_error("_media_svc_get_matched_list failed");
	} else {
		for (iter = item_list; iter; iter = g_list_next(iter)) {
			if (_media_svc_is_keyword_included((char *)iter->data, keyword))
				*result = g_list_append(*result, g_strdup((gchar *)iter->data));
		}
	}

	g_list_free_full(item_list, g_free);

	return ret;
}