e3e1cb1636668e49c26c0624180903eebdf90b47
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"
21 #include "libmpdclient.h"
23 #include <stdbool.h>
24 #include <string.h>
25 #include <assert.h>
27 struct filelist *
28 filelist_new(void)
29 {
30 struct filelist *filelist = g_malloc(sizeof(*filelist));
32 filelist->entries = g_ptr_array_new();
34 return filelist;
35 }
37 void
38 filelist_free(struct filelist *filelist)
39 {
40 guint i;
42 for (i = 0; i < filelist_length(filelist); ++i) {
43 struct filelist_entry *entry = filelist_get(filelist, i);
45 if (entry->entity)
46 mpd_freeInfoEntity(entry->entity);
48 g_slice_free(struct filelist_entry, entry);
49 }
51 g_ptr_array_free(filelist->entries, TRUE);
52 g_free(filelist);
53 }
55 struct filelist_entry *
56 filelist_append(struct filelist *filelist, struct mpd_InfoEntity *entity)
57 {
58 struct filelist_entry *entry = g_slice_new(struct filelist_entry);
60 entry->flags = 0;
61 entry->entity = entity;
63 g_ptr_array_add(filelist->entries, entry);
65 return entry;
66 }
68 struct filelist_entry *
69 filelist_prepend(struct filelist *filelist, struct mpd_InfoEntity *entity)
70 {
71 struct filelist_entry *entry = filelist_append(filelist, entity);
73 /* this is very slow, but we should optimize screen_artist.c
74 later so that this function can be removed, so I'm not in
75 the mood to implement something better here */
77 if (!filelist_is_empty(filelist)) {
78 guint i;
80 for (i = filelist_length(filelist) - 1; i > 0; --i)
81 g_ptr_array_index(filelist->entries, i) =
82 filelist_get(filelist, i - 1);
84 g_ptr_array_index(filelist->entries, 0) = entry;
85 }
87 return entry;
88 }
90 void
91 filelist_move(struct filelist *filelist, struct filelist *from)
92 {
93 guint i;
95 for (i = 0; i < filelist_length(from); ++i)
96 g_ptr_array_add(filelist->entries,
97 g_ptr_array_index(from->entries, i));
99 g_ptr_array_set_size(from->entries, 0);
100 }
102 static gint
103 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
104 {
105 GCompareFunc compare_func = data;
106 gconstpointer a = *(const gconstpointer*)ap;
107 gconstpointer b = *(const gconstpointer*)bp;
109 return compare_func(a, b);
110 }
112 /* Sorts the whole filelist, at the moment used by filelist_search */
113 void
114 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
115 {
116 g_ptr_array_sort_with_data(filelist->entries,
117 filelist_compare_indirect,
118 compare_func);
119 }
122 /* Only sorts the directories and playlist files.
123 * The songs stay in the order it came from mpd. */
124 void
125 filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
126 {
127 unsigned first, last;
128 const mpd_InfoEntity *iter;
130 assert(filelist && filelist->entries);
132 if (filelist->entries->len < 2)
133 return;
134 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
135 /* This can only happen at the beginning of the filelist,
136 * because NULL stands for "[..]" */
137 if (iter == NULL) {
138 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
139 first = 1;
140 }
141 else
142 first = 0;
143 /* find the last directory entry */
144 for (last = first+1; last < filelist->entries->len; last++) {
145 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
146 if (iter->type != MPD_INFO_ENTITY_TYPE_DIRECTORY)
147 break;
148 }
149 if (last == filelist->entries->len - 1)
150 last++;
151 /* sort the directories */
152 if (last - first > 1)
153 g_qsort_with_data(filelist->entries->pdata + first,
154 last - first, sizeof(gpointer),
155 filelist_compare_indirect, compare_func);
156 /* find the first playlist entry */
157 for (first = last; first < filelist->entries->len; first++) {
158 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
159 if (iter->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
160 break;
161 }
162 /* sort the playlist entries */
163 if (filelist->entries->len - first > 1)
164 g_qsort_with_data(filelist->entries->pdata + first,
165 filelist->entries->len - first, sizeof(gpointer),
166 filelist_compare_indirect, compare_func);
167 }
169 static bool
170 same_song(const struct mpd_song *a, const struct mpd_song *b)
171 {
172 return strcmp(a->file, b->file) == 0;
173 }
175 int
176 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
177 {
178 guint i;
180 assert(song != NULL);
182 for (i = 0; i < filelist_length(fl); ++i) {
183 struct filelist_entry *entry = filelist_get(fl, i);
184 mpd_InfoEntity *entity = entry->entity;
186 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
187 struct mpd_song *song2 = entity->info.song;
189 if (same_song(song, song2))
190 return i;
191 }
192 }
194 return -1;
195 }
197 int
198 filelist_find_directory(struct filelist *filelist, const char *name)
199 {
200 guint i;
202 assert(name != NULL);
204 for (i = 0; i < filelist_length(filelist); ++i) {
205 struct filelist_entry *entry = filelist_get(filelist, i);
206 mpd_InfoEntity *entity = entry->entity;
208 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
209 strcmp(entity->info.directory->path, name) == 0)
210 return i;
211 }
213 return -1;
214 }