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_free(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_alloc(sizeof(*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 void
105 filelist_sort(struct filelist *filelist, GCompareFunc compare_func)
106 {
107 g_ptr_array_sort(filelist->entries, compare_func);
108 }
110 struct filelist_entry *
111 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
112 {
113 guint i;
115 assert(song != NULL);
117 for (i = 0; i < filelist_length(fl); ++i) {
118 struct filelist_entry *entry = filelist_get(fl, i);
119 mpd_InfoEntity *entity = entry->entity;
121 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
122 struct mpd_song *song2 = entity->info.song;
124 if (strcmp(song->file, song2->file) == 0)
125 return entry;
126 }
127 }
129 return NULL;
130 }