File: filelist.c

package info (click to toggle)
ncmpc 0.25-0.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,616 kB
  • sloc: ansic: 12,229; sh: 4,157; makefile: 282; ruby: 28
file content (250 lines) | stat: -rw-r--r-- 6,902 bytes parent folder | download | duplicates (2)
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
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2010 The Music Player Daemon Project
 * Project homepage: http://musicpd.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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "filelist.h"

#include <mpd/client.h>

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

struct filelist *
filelist_new(void)
{
	struct filelist *filelist = g_malloc(sizeof(*filelist));

	filelist->entries = g_ptr_array_new();

	return filelist;
}

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

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

		g_slice_free(struct filelist_entry, entry);
	}

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

struct filelist_entry *
filelist_append(struct filelist *filelist, struct mpd_entity *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;
}

void
filelist_move(struct filelist *filelist, struct filelist *from)
{
	for (unsigned 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);
}

gint
compare_filelist_entry_path(gconstpointer filelist_entry1,
			    gconstpointer filelist_entry2)
{
	const struct mpd_entity *e1, *e2;

	e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
	e2 = ((const struct filelist_entry *)filelist_entry2)->entity;

	int n = 0;
	if (e1 != NULL && e2 != NULL &&
	    mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
		switch (mpd_entity_get_type(e1)) {
		case MPD_ENTITY_TYPE_UNKNOWN:
			break;
		case MPD_ENTITY_TYPE_DIRECTORY:
			n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
					   mpd_directory_get_path(mpd_entity_get_directory(e2)));
			break;
		case MPD_ENTITY_TYPE_SONG:
			break;
		case MPD_ENTITY_TYPE_PLAYLIST:
			n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
					   mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
		}
	}
	return n;
}

/* Sorts the whole filelist, at the moment used by filelist_search */
void
filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
{
	g_ptr_array_sort_with_data(filelist->entries,
			filelist_compare_indirect,
			compare_func);
}


/* Only sorts the directories and playlist files.
 * The songs stay in the order it came from mpd. */
void
filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
{
	const struct mpd_entity *iter;

	assert(filelist && filelist->entries);

	if (filelist->entries->len < 2)
		return;

	/* If the first entry is NULL, skip it, because NULL stands for "[..]" */
	iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
	unsigned first = iter == NULL ? 1 : 0, last;

	/* find the last directory entry */
	for (last = first+1; last < filelist->entries->len; last++) {
		iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
		if (mpd_entity_get_type(iter) != MPD_ENTITY_TYPE_DIRECTORY)
			break;
	}
	if (last == filelist->entries->len - 1)
		last++;
	/* sort the directories */
	if (last - first > 1)
		g_qsort_with_data(filelist->entries->pdata + first,
				last - first, sizeof(gpointer),
				filelist_compare_indirect, compare_func);
	/* find the first playlist entry */
	for (first = last; first < filelist->entries->len; first++) {
		iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
		if (mpd_entity_get_type(iter) == MPD_ENTITY_TYPE_PLAYLIST)
			break;
	}
	/* sort the playlist entries */
	if (filelist->entries->len - first > 1)
		g_qsort_with_data(filelist->entries->pdata + first,
				filelist->entries->len - first, sizeof(gpointer),
				filelist_compare_indirect, compare_func);
}

void
filelist_no_duplicates(struct filelist *filelist)
{
	for (int i = filelist_length(filelist) - 1; i >= 0; --i) {
		struct filelist_entry *entry = filelist_get(filelist, i);
		const struct mpd_song *song;

		if (entry->entity == NULL ||
		    mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
			continue;

		song = mpd_entity_get_song(entry->entity);
		if (filelist_find_song(filelist, song) < i) {
			g_ptr_array_remove_index(filelist->entries, i);
			mpd_entity_free(entry->entity);
			g_slice_free(struct filelist_entry, entry);
		}
	}
}

static bool
same_song(const struct mpd_song *a, const struct mpd_song *b)
{
	return strcmp(mpd_song_get_uri(a), mpd_song_get_uri(b)) == 0;
}

int
filelist_find_song(const struct filelist *fl, const struct mpd_song *song)
{
	assert(song != NULL);

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

		if (entity != NULL &&
		    mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
			const struct mpd_song *song2 =
				mpd_entity_get_song(entity);

			if (same_song(song, song2))
				return i;
		}
	}

	return -1;
}

int
filelist_find_directory(const struct filelist *filelist, const char *name)
{
	assert(name != NULL);

	for (unsigned i = 0; i < filelist_length(filelist); ++i) {
		struct filelist_entry *entry = filelist_get(filelist, i);
		const struct mpd_entity *entity  = entry->entity;

		if (entity != NULL &&
		    mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
		    strcmp(mpd_directory_get_path(mpd_entity_get_directory(entity)),
			   name) == 0)
			return i;
	}

	return -1;
}

void
filelist_recv(struct filelist *filelist, struct mpd_connection *connection)
{
	struct mpd_entity *entity;

	while ((entity = mpd_recv_entity(connection)) != NULL)
		filelist_append(filelist, entity);
}

struct filelist *
filelist_new_recv(struct mpd_connection *connection)
{
	struct filelist *filelist = filelist_new();
	filelist_recv(filelist, connection);
	return filelist;
}