summaryrefslogtreecommitdiff
path: root/src/data/albumdata.c
blob: 8a6acc812842daea7271772c29a03ade4fa3e922 (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
/*
 * 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 "data/datamgr.h"

struct albumdata;

typedef bool (*get_album_list)(struct albumdata *ad, const char *cond);
typedef int (*media_compare)(struct group_info *gi, struct album_info *ai);
typedef void *(*media_compare_data_get)(struct album_info *ai);
typedef char *(*group_name_get)(struct album_info *ai);

static bool _get_name_list(struct albumdata *ad, const char *cond);
static int _compare_name(struct group_info *, struct album_info *);
static void *_get_data_name(struct album_info *);
static char *_get_name(struct album_info *);

static bool _get_artist_list(struct albumdata *ad, const char *cond);
static int _compare_artist(struct group_info *, struct album_info *);
static void *_get_data_artist(struct album_info *);
static char *_get_artist(struct album_info *);

enum _filter_type {
	E_FILTER_ALBUM = 0,
	E_FILTER_MEDIA
};

struct albumdata {
	Eina_List *album_list;
	Eina_List *media_list;

	int group_type;

	const char *media_type;
	int source_type;
};

struct _group_info {
	const char *sort_keyword;
	get_album_list get_list;
	media_compare media_cmp;
	media_compare_data_get data_get;
	group_name_get name_get;
};

static struct _group_info g_group_info[E_GROUP_ALBUM_MAX] = {
	[E_GROUP_ALBUM_NAME] = {
		.sort_keyword = MEDIA_ALBUM,
		.get_list = _get_name_list,
		.media_cmp = _compare_name,
		.data_get = _get_data_name,
		.name_get = _get_name,
	},
	[E_GROUP_ALBUM_ARTIST] = {
		.sort_keyword = MEDIA_ALBUM_ARTIST,
		.get_list = _get_artist_list,
		.media_cmp = _compare_artist,
		.data_get = _get_data_artist,
		.name_get = _get_artist
	},
};

static bool _create_filter(struct albumdata *ad, filter_h *filter,
			const char *cond, int filter_type)
{
	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 AND MEDIA_STORAGE_TYPE=%d",
				ad->media_type, ad->source_type);

	if (cond) {
		char s1[64];

		snprintf(s1, sizeof(s1), " AND %s", cond);
		strncat(buf, s1, strlen(s1));
	}

	media_filter_set_condition(tmp_filter, buf,
			MEDIA_CONTENT_COLLATE_DEFAULT);

	if (filter_type == E_FILTER_ALBUM) {
		media_filter_set_order(tmp_filter, MEDIA_CONTENT_ORDER_ASC,
				g_group_info[ad->group_type].sort_keyword,
				MEDIA_CONTENT_COLLATE_DEFAULT);
	} else if (filter_type == E_FILTER_MEDIA) {
		media_filter_set_order(tmp_filter, MEDIA_CONTENT_ORDER_ASC,
				MEDIA_TITLE, MEDIA_CONTENT_COLLATE_DEFAULT);
	}

	*filter = tmp_filter;

	return true;
}

static int _compare_name(struct group_info *gi, struct album_info *ai)
{
	if (!gi->data || !ai->name)
		return -1;

	return strncasecmp(gi->data, ai->name, 1);
}

static void *_get_data_name(struct album_info *ai)
{
	if (!ai->name)
		return NULL;

	return strdup(ai->name);
}

static char *_get_name(struct album_info *ai)
{
	if (!ai->name)
		return NULL;

	return strndup(ai->name, 1);
}

static int _compare_artist(struct group_info *gi, struct album_info *ai)
{
	if (!gi->data || !ai->artist)
		return -1;

	return strncasecmp(gi->data, ai->artist, 1);
}

static void *_get_data_artist(struct album_info *ai)
{
	if (!ai->artist)
		return NULL;

	return strdup(ai->artist);
}

static char *_get_artist(struct album_info *ai)
{
	if (!ai->artist)
		return NULL;

	return strndup(ai->artist, 1);
}

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

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

static void _destroy_album_info(struct album_info *ai)
{
	free(ai->name);
	free(ai->artist);
	free(ai->album_art);

	free(ai);
}

static void _destroy_album_list(Eina_List *list)
{
	struct album_info *ai;

	EINA_LIST_FREE(list, ai)
		_destroy_album_info(ai);
}

static bool _get_each_media_info(media_info_h media_h, void *data)
{
	app_media *am;
	struct albumdata *ad;

	if (!data)
		return false;

	ad = data;

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

	ad->media_list = eina_list_append(ad->media_list, am);

	return true;
}

static bool _get_each_album_info(media_album_h album, void *data)
{
	filter_h filter;
	struct albumdata *ad;
	struct album_info *ai;

	if (!data)
		return false;

	ad = data;

	ai = calloc(1, sizeof(*ai));
	if (!ai) {
		_ERR("failed to allocate memory");
		return false;
	}

	if (!_create_filter(ad, &filter, NULL, E_FILTER_ALBUM)) {
		_ERR("failed to create filter");
		free(ai);
		return false;
	}

	if (media_album_get_album_id(album, &(ai->id))
		!= MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to fetch album id");
		goto err;
	}

	if (media_album_get_name(album, &(ai->name))
		!= MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to fetch album name");
		goto err;
	}

	if (media_album_get_artist(album, &(ai->artist))
		!= MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to fetch album artist");
		goto err;
	}

	if (media_album_get_album_art(album, &(ai->album_art))
		!= MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to fetch album art");
		goto err;
	}

	if (media_album_get_media_count_from_db(ai->id, filter,
		&(ai->media_count)) != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get album media count");
		goto err;
	}

	ad->album_list = eina_list_append(ad->album_list, ai);

	media_filter_destroy(filter);

	return true;

err:
	media_filter_destroy(filter);
	_destroy_album_info(ai);
	return false;
}

static bool _get_each_group_info(const char *group_name, void *data)
{
	filter_h filter;
	struct albumdata *ad;
	int ret;
	char buf[1024];

	if (!data || !group_name)
		return false;

	ad = data;

	snprintf(buf, sizeof(buf), "MEDIA_ALBUM_ARTIST=\"%s\"", group_name);

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

	/* only one album info for the artist */
	media_filter_set_offset(filter, 0, 1);

	ret = media_album_foreach_album_from_db(filter,
				_get_each_album_info, ad);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get album info");
		media_filter_destroy(filter);
		return false;
	}

	media_filter_destroy(filter);

	return true;
}

static bool _get_media_list(struct albumdata *ad, int album_id)
{
	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 (!_create_filter(ad, &filter, NULL, E_FILTER_MEDIA)) {
		_ERR("failed to create filter");
		media_content_disconnect();
		return false;
	}

	ret = media_album_foreach_media_from_db(album_id, filter,
				_get_each_media_info, ad);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get media info");
		_destroy_media_list(ad->media_list);
		media_filter_destroy(filter);
		media_content_disconnect();
		return false;
	}

	media_filter_destroy(filter);
	media_content_disconnect();

	return true;
}

static bool _get_name_list(struct albumdata *ad, const char *cond)
{
	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 (!_create_filter(ad, &filter, cond, E_FILTER_ALBUM)) {
		_ERR("failed to create filter");
		media_content_disconnect();
		return false;
	}

	ret = media_album_foreach_album_from_db(filter,
				_get_each_album_info, ad);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get album info");
		_destroy_album_list(ad->album_list);
		media_filter_destroy(filter);
		media_content_disconnect();
		return false;
	}

	media_filter_destroy(filter);
	media_content_disconnect();

	return true;
}

static bool _get_artist_list(struct albumdata *ad, const char *cond)
{
	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 (!_create_filter(ad, &filter, NULL, E_FILTER_ALBUM)) {
		_ERR("failed to create filter");
		media_content_disconnect();
		return false;
	}

	ret = media_group_foreach_group_from_db(filter,
				MEDIA_CONTENT_GROUP_ALBUM_ARTIST,
				_get_each_group_info, ad);
	if (ret != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to get group info");
		_destroy_album_list(ad->album_list);
		media_filter_destroy(filter);
		media_content_disconnect();
		return false;
	}

	media_filter_destroy(filter);
	media_content_disconnect();

	return true;
}

static void *_create(const char *media_type, int source_type)
{
	struct albumdata *ad;

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

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

	ad->media_type = media_type;
	ad->source_type = source_type;

	return (void *)ad;
}

static void _destroy(void *handle)
{
	struct albumdata *ad;

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

	ad = handle;

	_destroy_album_list(ad->album_list);
	_destroy_media_list(ad->media_list);

	free(ad);
}

static Eina_List *_get_list(void *handle, int type, void *data)
{
	struct albumdata *ad;
	int *album_id;
	char *artist;
	char buf[64];

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

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

	ad = handle;

	switch (type) {
	case E_LIST_ALBUM_MEDIA:
		if (ad->media_list) {
			_destroy_media_list(ad->media_list);
			ad->media_list = NULL;
		}

		album_id = data;
		_get_media_list(ad, *album_id);

		return ad->media_list;
	case E_LIST_ARTIST_ALBUM:
		if (ad->album_list) {
			_destroy_album_list(ad->album_list);
			ad->album_list = NULL;
		}

		ad->group_type = E_GROUP_ALBUM_NAME;

		artist = data;

		snprintf(buf, sizeof(buf), "MEDIA_ALBUM_ARTIST=\"%s\"", artist);
		_get_name_list(ad, buf);

		return ad->album_list;
	default:
		break;
	}

	return NULL;
}

static int _get_count(void *handle, int type)
{
	struct albumdata *ad;

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

	ad = handle;

	return eina_list_count(ad->album_list);
}

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

	EINA_LIST_FREE(list, gi) {
		free(gi->name);
		free(gi->data);
		_destroy_album_list(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 albumdata *ad;
	struct group_info *gi;
	struct album_info *ai;

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

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

	ad = handle;

	ad->group_type = type;

	if (ad->album_list) {
		_destroy_album_list(ad->album_list);
		ad->album_list = NULL;
	}

	if (!g_group_info[type].get_list(ad, NULL)) {
		_ERR("failed to get album list");
		return NULL;
	}

	gi = NULL;
	list = NULL;
	EINA_LIST_FOREACH(ad->album_list, l, ai) {
		if (!gi || g_group_info[type].media_cmp(gi, ai)) {
			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(ai);
			gi->data = g_group_info[type].data_get(ai);

			list = eina_list_append(list, gi);
		}

		gi->list = eina_list_append(gi->list, ai);
	}

	eina_list_free(ad->album_list);
	ad->album_list = NULL;

	return list;
}

static void _set_source(void *handle, int source_type)
{
	struct albumdata *ad;

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

	ad = handle;

	ad->source_type = source_type;
}

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 *albumdata_get_ops(void)
{
	return &_ops;
};