aboutsummaryrefslogtreecommitdiffstats
path: root/src/filelist.c
blob: 46137cd3b00ca6f15df939b6e4003eea0b2d18f5 (plain) (blame)
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
/*
 * (c) 2004 by Kalle Wallin <kaw@linux.se>
 * (c) 2008 Max Kellermann <max@duempel.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "filelist.h"
#include "libmpdclient.h"

#include <string.h>
#include <assert.h>

struct filelist *
filelist_new(const char *path)
{
	struct filelist *filelist = g_malloc(sizeof(*filelist));

	filelist->path = g_strdup(path);
	filelist->entries = g_ptr_array_new();

	return filelist;
}

void
filelist_free(struct filelist *filelist)
{
	guint i;

	for (i = 0; i < filelist_length(filelist); ++i) {
		struct filelist_entry *entry = filelist_get(filelist, i);

		if (entry->entity)
			mpd_freeInfoEntity(entry->entity);

		g_slice_free(struct filelist_entry, entry);
	}

	g_ptr_array_free(filelist->entries, TRUE);
	g_free(filelist->path);
	g_free(filelist);
}

struct filelist_entry *
filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity)
{
	struct filelist_entry *entry = g_slice_new(struct filelist_entry);

	entry->flags = 0;
	entry->entity = entity;

	g_ptr_array_add(filelist->entries, entry);

	return entry;
}

struct filelist_entry *
filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity)
{
	struct filelist_entry *entry = filelist_append(filelist, entity);

	/* this is very slow, but we should optimize screen_artist.c
	   later so that this function can be removed, so I'm not in
	   the mood to implement something better here */

	if (!filelist_is_empty(filelist)) {
		guint i;

		for (i = filelist_length(filelist) - 1; i > 0; --i)
			g_ptr_array_index(filelist->entries, i) =
				filelist_get(filelist, i - 1);

		g_ptr_array_index(filelist->entries, 0) = entry;
	}

	return entry;
}

void
filelist_move(struct filelist *filelist, struct filelist *from)
{
	guint i;

	for (i = 0; i < filelist_length(from); ++i)
		g_ptr_array_add(filelist->entries,
				g_ptr_array_index(from->entries, i));

	g_ptr_array_set_size(from->entries, 0);
}

static gint
filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
{
	GCompareFunc compare_func = data;
	gconstpointer a = *(const gconstpointer*)ap;
	gconstpointer b = *(const gconstpointer*)bp;

	return compare_func(a, b);
}

void
filelist_sort(struct filelist *filelist, GCompareFunc compare_func)
{
	g_ptr_array_sort_with_data(filelist->entries,
				   filelist_compare_indirect,
				   compare_func);
}

struct filelist_entry *
filelist_find_song(struct filelist *fl, const struct mpd_song *song)
{
	guint i;

	assert(song != NULL);

	for (i = 0; i < filelist_length(fl); ++i) {
		struct filelist_entry *entry = filelist_get(fl, i);
		mpd_InfoEntity *entity  = entry->entity;

		if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
			struct mpd_song *song2 = entity->info.song;

			if (strcmp(song->file, song2->file) == 0)
				return entry;
		}
	}

	return NULL;
}