2ce928cd4f73d7388bfb675a42d90b2896d41d3c
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "filelist.h"
22 #include <mpd/client.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include <assert.h>
28 struct filelist *
29 filelist_new(void)
30 {
31 struct filelist *filelist = g_malloc(sizeof(*filelist));
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_entity_free(entry->entity);
49 g_slice_free(struct filelist_entry, entry);
50 }
52 g_ptr_array_free(filelist->entries, TRUE);
53 g_free(filelist);
54 }
56 struct filelist_entry *
57 filelist_append(struct filelist *filelist, struct mpd_entity *entity)
58 {
59 struct filelist_entry *entry = g_slice_new(struct filelist_entry);
61 entry->flags = 0;
62 entry->entity = entity;
64 g_ptr_array_add(filelist->entries, entry);
66 return entry;
67 }
69 struct filelist_entry *
70 filelist_prepend(struct filelist *filelist, struct mpd_entity *entity)
71 {
72 struct filelist_entry *entry = filelist_append(filelist, entity);
74 /* this is very slow, but we should optimize screen_artist.c
75 later so that this function can be removed, so I'm not in
76 the mood to implement something better here */
78 if (!filelist_is_empty(filelist)) {
79 guint i;
81 for (i = filelist_length(filelist) - 1; i > 0; --i)
82 g_ptr_array_index(filelist->entries, i) =
83 filelist_get(filelist, i - 1);
85 g_ptr_array_index(filelist->entries, 0) = entry;
86 }
88 return entry;
89 }
91 void
92 filelist_move(struct filelist *filelist, struct filelist *from)
93 {
94 guint i;
96 for (i = 0; i < filelist_length(from); ++i)
97 g_ptr_array_add(filelist->entries,
98 g_ptr_array_index(from->entries, i));
100 g_ptr_array_set_size(from->entries, 0);
101 }
103 static gint
104 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
105 {
106 GCompareFunc compare_func = data;
107 gconstpointer a = *(const gconstpointer*)ap;
108 gconstpointer b = *(const gconstpointer*)bp;
110 return compare_func(a, b);
111 }
113 /* Sorts the whole filelist, at the moment used by filelist_search */
114 void
115 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
116 {
117 g_ptr_array_sort_with_data(filelist->entries,
118 filelist_compare_indirect,
119 compare_func);
120 }
123 /* Only sorts the directories and playlist files.
124 * The songs stay in the order it came from mpd. */
125 void
126 filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
127 {
128 unsigned first, last;
129 const struct mpd_entity *iter;
131 assert(filelist && filelist->entries);
133 if (filelist->entries->len < 2)
134 return;
135 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
136 /* This can only happen at the beginning of the filelist,
137 * because NULL stands for "[..]" */
138 if (iter == NULL) {
139 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
140 first = 1;
141 }
142 else
143 first = 0;
144 /* find the last directory entry */
145 for (last = first+1; last < filelist->entries->len; last++) {
146 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
147 if (mpd_entity_get_type(iter) != MPD_ENTITY_TYPE_DIRECTORY)
148 break;
149 }
150 if (last == filelist->entries->len - 1)
151 last++;
152 /* sort the directories */
153 if (last - first > 1)
154 g_qsort_with_data(filelist->entries->pdata + first,
155 last - first, sizeof(gpointer),
156 filelist_compare_indirect, compare_func);
157 /* find the first playlist entry */
158 for (first = last; first < filelist->entries->len; first++) {
159 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
160 if (mpd_entity_get_type(iter) == MPD_ENTITY_TYPE_PLAYLIST)
161 break;
162 }
163 /* sort the playlist entries */
164 if (filelist->entries->len - first > 1)
165 g_qsort_with_data(filelist->entries->pdata + first,
166 filelist->entries->len - first, sizeof(gpointer),
167 filelist_compare_indirect, compare_func);
168 }
170 static bool
171 same_song(const struct mpd_song *a, const struct mpd_song *b)
172 {
173 return strcmp(mpd_song_get_uri(a), mpd_song_get_uri(b)) == 0;
174 }
176 int
177 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
178 {
179 guint i;
181 assert(song != NULL);
183 for (i = 0; i < filelist_length(fl); ++i) {
184 struct filelist_entry *entry = filelist_get(fl, i);
185 const struct mpd_entity *entity = entry->entity;
187 if (entity != NULL &&
188 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
189 const struct mpd_song *song2 =
190 mpd_entity_get_song(entity);
192 if (same_song(song, song2))
193 return i;
194 }
195 }
197 return -1;
198 }
200 int
201 filelist_find_directory(struct filelist *filelist, const char *name)
202 {
203 guint i;
205 assert(name != NULL);
207 for (i = 0; i < filelist_length(filelist); ++i) {
208 struct filelist_entry *entry = filelist_get(filelist, i);
209 const struct mpd_entity *entity = entry->entity;
211 if (entity != NULL &&
212 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
213 strcmp(mpd_directory_get_path(mpd_entity_get_directory(entity)),
214 name) == 0)
215 return i;
216 }
218 return -1;
219 }
221 void
222 filelist_recv(struct filelist *filelist, struct mpd_connection *connection)
223 {
224 struct mpd_entity *entity;
226 while ((entity = mpd_recv_entity(connection)) != NULL)
227 filelist_append(filelist, entity);
228 }
230 struct filelist *
231 filelist_new_recv(struct mpd_connection *connection)
232 {
233 struct filelist *filelist = filelist_new();
234 filelist_recv(filelist, connection);
235 return filelist;
236 }