summaryrefslogtreecommitdiff
path: root/src/data/mediadata.c
blob: 66d3d1f1bddb72f49df065527c6f7a9670b94c7e (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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 <Elementary.h>
#include <media_content.h>
#include <app_debug.h>
#include <app_media.h>
#include <unicode/ustring.h>
#include <unicode/uchar.h>

#include "data/datamgr.h"
#include "util/util.h"

#define STR_IMAGE_NAME "Photo"
#define STR_VIDEO_NAME "Video"

#define STR_PLACE_UNKNOWN "Unknown"

typedef int (*media_compare)(struct group_info *gi, app_media_info *info);
typedef void *(*media_compare_data_get)(app_media_info *info);
typedef char *(*group_name_get)(app_media_info *info);

static int _compare_cb_date(const void *, const void *);
static int _compare_cb_name(const void *, const void *);
static int _compare_cb_genre(const void *, const void *);
static int _compare_cb_place(const void *, const void *);
static int _compare_cb_type(const void *, const void *);

static int _compare_title(struct group_info *, app_media_info *);
static char *_get_title(app_media_info *);
static void *_get_data_title(app_media_info *info);
static int _compare_time(struct group_info *, app_media_info *);
static char *_get_time(app_media_info *);
static void *_get_data_time(app_media_info *info);
static int _compare_genre(struct group_info *, app_media_info *);
static char *_get_genre(app_media_info *);
static void *_get_data_genre(app_media_info *);
static int _compare_place(struct group_info *, app_media_info *);
static char *_get_place(app_media_info *);
static void *_get_data_place(app_media_info *);
static int _compare_type(struct group_info *, app_media_info *);
static char *_get_type(app_media_info *);
static void *_get_data_type(app_media_info *);

struct mediadata {
	Eina_List *media_list;
	int media_count[E_FILE_MAX];

	const char *media_type;
	int source_type;
};

struct _list_info {
	Eina_Compare_Cb sort_cmp_cb;

	media_compare media_cmp;
	media_compare_data_get data_get;
	group_name_get name_get;
};

static struct _list_info g_group_info[E_GROUP_MEDIA_MAX] = {
	[E_GROUP_MEDIA_DATE] = {
		.sort_cmp_cb = _compare_cb_date,
		.media_cmp = _compare_time,
		.name_get = _get_time,
		.data_get = _get_data_time,
	},
	[E_GROUP_MEDIA_NAME] = {
		.sort_cmp_cb = _compare_cb_name,
		.media_cmp = _compare_title,
		.name_get = _get_title,
		.data_get = _get_data_title,
	},
	[E_GROUP_MEDIA_GENRE] = {
		.sort_cmp_cb = _compare_cb_genre,
		.media_cmp = _compare_genre,
		.name_get = _get_genre,
		.data_get = _get_data_genre,
	},
	[E_GROUP_MEDIA_PLACE] = {
		.sort_cmp_cb = _compare_cb_place,
		.media_cmp = _compare_place,
		.name_get = _get_place,
		.data_get = _get_data_place,
	},
	[E_GROUP_MEDIA_TYPE] = {
		.sort_cmp_cb = _compare_cb_type,
		.media_cmp = _compare_type,
		.name_get = _get_type,
		.data_get = _get_data_type,
	},
};

static bool _create_filter(struct mediadata *md, filter_h *filter)
{
	filter_h tmp_filter;
	int ret;
	char buf[1024];

	ret = media_filter_create(&tmp_filter);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to create media filter");
		return false;
	}

	snprintf(buf, sizeof(buf), "%s", md->media_type);

	if (md->source_type != E_SOURCE_ALL) {
		char s[64];

		snprintf(s, sizeof(s), " AND MEDIA_STORAGE_TYPE=%d",
					md->source_type);

		strncat(buf, s, strlen(s));
	}

	media_filter_set_condition(tmp_filter, buf,
			MEDIA_CONTENT_COLLATE_DEFAULT);

	*filter = tmp_filter;

	return true;
}

static char *_get_date_string(struct tm *tm)
{
	char buf[32];

	strftime(buf, sizeof(buf), "%Y.%m.%d", tm);

	return strdup(buf);
}

static int _compare_title(struct group_info *gi, app_media_info *info)
{
	if (!gi->data || !info->title)
		return -1;

	int32_t result = -1;
	UChar *converted_group_name = NULL;
	UChar *converted_media_title = NULL;

	converted_group_name = util_convert_to_UTF16_string(gi->data);
	converted_media_title = util_convert_to_UTF16_string(info->title);

	if (converted_group_name && converted_media_title) {
		result = u_strncasecmp(converted_group_name, converted_media_title, 1, U_FOLD_CASE_DEFAULT);
	}

	if (converted_group_name) {
		free(converted_group_name);
	}

	if (converted_media_title) {
		free(converted_media_title);
	}

	return result;
}

static void *_get_data_title(app_media_info *info)
{
	if (!info->title)
		return NULL;

	char *result_str = NULL;
	UChar *converted_string = NULL;

	converted_string = util_convert_to_UTF16_string(info->title);

	if (converted_string == NULL) {
		_ERR("_convert_to_UTF16_string failed");
		goto OUT;
	}
	result_str = util_convert_to_UTF8_string(converted_string);

OUT:
	if (converted_string)
		free(converted_string);

	return result_str;
}

static char *_get_title(app_media_info *info)
{
	if (!info->title)
		return NULL;

	char *result_str = NULL;
	UChar *converted_string = NULL;
	UChar sub_string[10] = { 0, };

	converted_string = util_convert_to_UTF16_string(info->title);

	if (converted_string == NULL) {
		_ERR("util_convert_to_UTF16_string failed");
		goto OUT;
	}

	/* Get a character (not a byte) from left */
	u_strncpy(sub_string, converted_string, 1);

	result_str = util_convert_to_UTF8_string(sub_string);

OUT:

	if (converted_string)
		free(converted_string);

	return result_str;
}

static int _compare_time(struct group_info *gi, app_media_info *info)
{
	struct tm *tm;

	if (!gi->data)
		return -1;

	tm = gi->data;

	if (tm->tm_year == info->content_time->tm_year &&
			tm->tm_yday == info->content_time->tm_yday)
		return 0;

	return 1;
}

static void *_get_data_time(app_media_info *info)
{
	struct tm *tm;

	tm = calloc(1, sizeof(*tm));
	if (!tm)
		return NULL;

	memcpy(tm, info->content_time, sizeof(*tm));

	return tm;
}

static char *_get_time(app_media_info *info)
{
	return  _get_date_string(info->content_time);
}

static const char *_get_genre_string(app_media_info *info)
{
	const char *genre;

	if (info->media_type == MEDIA_CONTENT_TYPE_VIDEO)
		genre = info->video->genre;
	else if (info->media_type == MEDIA_CONTENT_TYPE_MUSIC)
		genre = info->audio->genre;
	else
		genre = NULL;

	return genre;
}

static int _compare_genre(struct group_info *gi, app_media_info *info)
{
	const char *genre;

	genre = _get_genre_string(info);

	if (!gi->data || !genre)
		return -1;

	return strcmp(gi->data, genre);
}

static void *_get_data_genre(app_media_info *info)
{
	const char *genre;

	genre = _get_genre_string(info);

	if (!genre)
		return NULL;

	return strdup(genre);
}

static char *_get_genre(app_media_info *info)
{
	const char *genre;

	genre = _get_genre_string(info);

	if (!genre)
		return NULL;

	return strdup(genre);
}

static void _get_place_string(const char *src, char **city, char **country)
{
	char str[64];
	char *p, *tmp;

	if (!src) {
		*city = strdup(STR_PLACE_UNKNOWN);
		*country = strdup(STR_PLACE_UNKNOWN);
		return;
	}

	snprintf(str, sizeof(str), "%s", src);

	p = strtok_r(str, "/", &tmp);
	if (!p)
		p = STR_PLACE_UNKNOWN;

	*city = strdup(p);

	p = strtok_r(tmp, "", &tmp);
	if (!p)
		p = STR_PLACE_UNKNOWN;

	*country = strdup(p);
}

static int _compare_place(struct group_info *gi, app_media_info *info)
{
	if (!gi->data)
		return -1;

	if (!info->location_tag)
		return strcmp(gi->data, STR_PLACE_UNKNOWN);

	return strcmp(gi->data, info->location_tag);
}

static void *_get_data_place(app_media_info *info)
{
	if (!info->location_tag)
		return strdup(STR_PLACE_UNKNOWN);

	return strdup(info->location_tag);
}

static char *_get_place(app_media_info *info)
{
	if (!info->location_tag)
		return strdup(STR_PLACE_UNKNOWN);

	return strdup(info->location_tag);
}

static int _compare_type(struct group_info *gi, app_media_info *info)
{
	int *type;

	if (!gi->data)
		return -1;

	type = gi->data;

	if (*type == info->media_type)
		return 0;

	return 1;
}

static void *_get_data_type(app_media_info *info)
{
	int *type;

	type = calloc(1, sizeof(*type));
	if (!type)
		return NULL;

	*type = info->media_type;

	return type;
}

static char *_get_type(app_media_info *info)
{
	const char *type;

	if (info->media_type == MEDIA_CONTENT_TYPE_IMAGE)
		type = STR_IMAGE_NAME;
	else if (info->media_type == MEDIA_CONTENT_TYPE_VIDEO)
		type = STR_VIDEO_NAME;
	else
		type = NULL;

	if (!type)
		return NULL;

	return strdup(type);
}

static int _compare_cb_date(const void *data1, const void *data2)
{
	app_media *am1, *am2;
	app_media_info *info1, *info2;
	time_t tm1, tm2;

	am1 = (app_media *)data1;
	am2 = (app_media *)data2;

	info1 = app_media_get_info(am1);
	info2 = app_media_get_info(am2);

	if (!info1 || !info2)
		return -1;

	tm1 = mktime(info1->content_time);
	tm2 = mktime(info2->content_time);

	if (tm1 < tm2)
		return 1;

	return -1;
}

static int _compare_cb_name(const void *data1, const void *data2)
{
	app_media *am1, *am2;
	app_media_info *info1, *info2;

	am1 = (app_media *)data1;
	am2 = (app_media *)data2;

	info1 = app_media_get_info(am1);
	info2 = app_media_get_info(am2);

	if (!info1 || !info2 || !info1->title || !info2->title)
		return -1;

	return strcasecmp(info1->title, info2->title);
}

static int _compare_cb_genre(const void *data1, const void *data2)
{
	app_media *am1, *am2;
	app_media_info *info1, *info2;
	const char *genre1, *genre2;

	am1 = (app_media *)data1;
	am2 = (app_media *)data2;

	info1 = app_media_get_info(am1);
	info2 = app_media_get_info(am2);

	if (!info1 || !info2)
		return -1;

	genre1 = _get_genre_string(info1);
	genre2 = _get_genre_string(info2);

	if (!genre1 || !genre2)
		return -1;

	return strcmp(genre1, genre2);
}

static int _compare_cb_place(const void *data1, const void *data2)
{
	app_media *am1, *am2;
	app_media_info *info1, *info2;
	char *city1, *city2;
	char *country1, *country2;
	int r;

	am1 = (app_media *)data1;
	am2 = (app_media *)data2;

	info1 = app_media_get_info(am1);
	info2 = app_media_get_info(am2);

	if (!info1 || !info2)
		return -1;

	_get_place_string(info1->location_tag, &city1, &country1);
	_get_place_string(info2->location_tag, &city2, &country2);

	if (!city1 || !city2 || !country1 || !country2) {
		r = -1;
		goto ret;
	}

	r = strcmp(country1, country2);
	if (!r)
		r = strcmp(city1, city2);
	else {
		/* NOTE: to set "Unknown" as the last item */
		if (r < 0 && !strcmp(country1, STR_PLACE_UNKNOWN))
			r = 1;
		else if (r > 0 && !strcmp(country2, STR_PLACE_UNKNOWN))
			r = -1;
	}

ret:
	free(city1);
	free(city2);
	free(country1);
	free(country2);

	return r;
}

static int _compare_cb_type(const void *data1, const void *data2)
{
	app_media *am1, *am2;
	app_media_info *info1, *info2;

	am1 = (app_media *)data1;
	am2 = (app_media *)data2;

	info1 = app_media_get_info(am1);
	info2 = app_media_get_info(am2);

	if (!info1 || !info2)
		return -1;

	if (info1->media_type < info2->media_type)
		return 1;

	return -1;
}

static Eina_List *_sort_list(Eina_List *list, int sort)
{
	Eina_List *sorted_list;

	sorted_list = eina_list_sort(list, 0, g_group_info[sort].sort_cmp_cb);

	return sorted_list;
}

static void _destroy_media_list(Eina_List *list)
{
	app_media *am;

	EINA_LIST_FREE(list, am)
		app_media_destroy(am);
}

static bool _get_each_media_info(media_info_h media_h, void *data)
{
	Eina_List **list;
	app_media *am;

	if (!data)
		return false;

	list = data;

	am = app_media_create(media_h);
	if (!am) {
		_ERR("failed to create app media");
		return false;
	}

	*list = eina_list_append(*list, am);

	return true;
}

static bool _get_medialist(struct mediadata *md)
{
	Eina_List *list;
	filter_h filter;
	int ret;

	ret = media_content_connect();
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to connect to media content");
		return false;
	}

	if (md->media_list) {
		_destroy_media_list(md->media_list);
		md->media_list = NULL;
	}

	if (!_create_filter(md, &filter)) {
		_ERR("failed to create filter");
		return false;
	}

	list = NULL;
	ret = media_info_foreach_media_from_db(filter,
			_get_each_media_info, &list);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get media info");
		media_filter_destroy(filter);
		return false;
	}

	md->media_list = list;

	media_filter_destroy(filter);

	media_content_disconnect();

	return true;
}

static void *_create(const char *media_type, int source_type)
{
	struct mediadata *md;

	if (!media_type) {
		_ERR("invalid argument");
		return NULL;
	}

	md = calloc(1, sizeof(*md));
	if (!md) {
		_ERR("failed to allocate md");
		return NULL;
	}

	md->media_type = media_type;
	md->source_type = source_type;

	if (!_get_medialist(md)) {
		_ERR("failed to get medialist");
		free(md);
		return NULL;
	}

	return (void *)md;
}

static void _destroy(void *handle)
{
	struct mediadata *md;

	if (!handle) {
		_ERR("failed to get mediadata handle");
		return;
	}

	md = handle;

	_destroy_media_list(md->media_list);

	free(md);
}

static Eina_List *_get_list(void *handle, int type, void *data)
{
	struct mediadata *md;

	if (!handle) {
		_ERR("failed to get mediadata handle");
		return NULL;
	}

	md = handle;

	return md->media_list;
}

static int _get_count(void *handle, int type)
{
	struct mediadata *md;

	if (!handle) {
		_ERR("failed to get mediadata handle");
		return -1;
	}

	if (type < 0 || type >= E_FILE_MAX) {
		_ERR("invalid argument");
		return -1;
	}

	md = handle;

	return md->media_count[type];
}

static void _free_group_list(Eina_List *list)
{
	struct group_info *gi;

	EINA_LIST_FREE(list, gi) {
		free(gi->name);
		free(gi->data);
		eina_list_free(gi->list);

		free(gi);
	}
}

static void _free_group(Eina_List *list)
{
	_free_group_list(list);
}

static Eina_List *_get_group(void *handle, int type, void *data)
{
	Eina_List *list, *l;
	struct mediadata *md;
	struct group_info *gi;
	app_media *am;
	app_media_info *info;
	int i;

	if (!handle) {
		_ERR("failed to get mediadata handle");
		return NULL;
	}

	if (type < 0 || type >= E_GROUP_MEDIA_MAX) {
		_ERR("invalid argument");
		return NULL;
	}

	md = handle;

	if (!_get_medialist(md)) {
		_ERR("failed to get media list");
		return NULL;
	}

	md->media_list = _sort_list(md->media_list, type);

	for (i = 0; i < E_FILE_MAX; i++)
		md->media_count[i] = 0;

	gi = NULL;
	list = NULL;
	EINA_LIST_FOREACH(md->media_list, l, am) {
		info = app_media_get_info(am);
		if (!info) {
			_ERR("failed to get media info");
			_free_group_list(list);
			return NULL;
		}

		if (!gi || g_group_info[type].media_cmp(gi, info)) {
			gi = calloc(1, sizeof(*gi));
			if (!gi) {
				_ERR("failed to create group info");
				_free_group_list(list);
				return NULL;
			}

			gi->name = g_group_info[type].name_get(info);
			gi->data = g_group_info[type].data_get(info);

			list = eina_list_append(list, gi);
		}

		gi->list = eina_list_append(gi->list, am);

		md->media_count[info->media_type]++;
	}

	return list;
}

static void _set_source(void *handle, int source_type)
{
	struct mediadata *md;

	if (!handle) {
		_ERR("failed to get mediadata handle");
		return;
	}

	md = handle;

	md->source_type = source_type;

	_get_medialist(md);
}

static struct data_ops _ops = {
	.create = _create,
	.destroy = _destroy,
	.get_list = _get_list,
	.get_count = _get_count,
	.get_group = _get_group,
	.free_group = _free_group,
	.set_source = _set_source,
};

struct data_ops *mediadata_get_ops(void)
{
	return &_ops;
};

bool _media_item_cb(media_info_h item, void *user_data)
{
    app_media **am = (app_media**)user_data;

    *am = app_media_create(item);

    return false;   //only 1 item
}


app_media* mediadata_get_app_media_by_file_path(char *file_path)
{
	int ret = MEDIA_CONTENT_ERROR_NONE;
	filter_h media_filter = NULL;
	app_media *am = NULL;
	char condition[PATH_MAX + 256] = {0,};

	if (file_path == NULL) {
		_ERR("filepath is NULL");
		goto out;
	}

	media_content_connect();

	snprintf(condition, sizeof(condition), "%s = '%s'", MEDIA_PATH, file_path);


	if ((ret = media_filter_create(&media_filter)) != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("media_filter_create returns [%d]", ret);
		goto out;
	}

	if ((ret = media_filter_set_condition(media_filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT)) != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("media_filter_create returns [%d]", ret);
		goto out;
	}

	ret = media_filter_set_order(media_filter, MEDIA_CONTENT_ORDER_DESC, MEDIA_DISPLAY_NAME, MEDIA_CONTENT_COLLATE_NOCASE);

	if ((ret = media_info_foreach_media_from_db(media_filter, _media_item_cb, &am)) != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("media_info_foreach_media_from_db is failed [%d]", ret);
	}

out:
	if (media_filter)
		media_filter_destroy(media_filter);

	return am;
}