1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 * (c) 2008 Max Kellermann <max@duempel.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
20 #include "filelist.h"
21 #include "libmpdclient.h"
23 #include <string.h>
24 #include <assert.h>
26 struct filelist *
27 filelist_new(const char *path)
28 {
29 struct filelist *filelist = g_malloc(sizeof(*filelist));
31 filelist->path = g_strdup(path);
32 filelist->updated = FALSE;
33 filelist->entries = g_ptr_array_new();
35 return filelist;
36 }
38 void
39 filelist_free(struct filelist *filelist)
40 {
41 guint i;
43 for (i = 0; i < filelist_length(filelist); ++i) {
44 struct filelist_entry *entry = filelist_get(filelist, i);
46 if (entry->entity)
47 mpd_freeInfoEntity(entry->entity);
49 g_slice_free(struct filelist_entry, entry);
50 }
52 g_ptr_array_free(filelist->entries, TRUE);
53 g_free(filelist->path);
54 g_free(filelist);
55 }
57 struct filelist_entry *
58 filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity)
59 {
60 struct filelist_entry *entry = g_slice_new(struct filelist_entry);
62 entry->flags = 0;
63 entry->entity = entity;
65 g_ptr_array_add(filelist->entries, entry);
67 return entry;
68 }
70 struct filelist_entry *
71 filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity)
72 {
73 struct filelist_entry *entry = filelist_append(filelist, entity);
75 /* this is very slow, but we should optimize screen_artist.c
76 later so that this function can be removed, so I'm not in
77 the mood to implement something better here */
79 if (!filelist_is_empty(filelist)) {
80 guint i;
82 for (i = filelist_length(filelist) - 1; i > 0; --i)
83 g_ptr_array_index(filelist->entries, i) =
84 filelist_get(filelist, i - 1);
86 g_ptr_array_index(filelist->entries, 0) = entry;
87 }
89 return entry;
90 }
92 void
93 filelist_move(struct filelist *filelist, struct filelist *from)
94 {
95 guint i;
97 for (i = 0; i < filelist_length(from); ++i)
98 g_ptr_array_add(filelist->entries,
99 g_ptr_array_index(from->entries, i));
101 g_ptr_array_set_size(from->entries, 0);
102 }
104 static gint
105 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
106 {
107 GCompareFunc compare_func = data;
108 gconstpointer a = *(const gconstpointer*)ap;
109 gconstpointer b = *(const gconstpointer*)bp;
111 return compare_func(a, b);
112 }
114 void
115 filelist_sort(struct filelist *filelist, GCompareFunc compare_func)
116 {
117 g_ptr_array_sort_with_data(filelist->entries,
118 filelist_compare_indirect,
119 compare_func);
120 }
122 struct filelist_entry *
123 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
124 {
125 guint i;
127 assert(song != NULL);
129 for (i = 0; i < filelist_length(fl); ++i) {
130 struct filelist_entry *entry = filelist_get(fl, i);
131 mpd_InfoEntity *entity = entry->entity;
133 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
134 struct mpd_song *song2 = entity->info.song;
136 if (strcmp(song->file, song2->file) == 0)
137 return entry;
138 }
139 }
141 return NULL;
142 }