Code

mpdclient: moved compare_filelistentry() to filelist.c
[ncmpc.git] / src / filelist.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 The Music Player Daemon Project
3  * Project homepage: http://musicpd.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.
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);
103 static gint
104 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
106         GCompareFunc compare_func = data;
107         gconstpointer a = *(const gconstpointer*)ap;
108         gconstpointer b = *(const gconstpointer*)bp;
110         return compare_func(a, b);
113 gint
114 compare_filelist_entry_path(gconstpointer filelist_entry1,
115                             gconstpointer filelist_entry2)
117         const struct mpd_entity *e1, *e2;
118         int n = 0;
120         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
121         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
123         if (e1 != NULL && e2 != NULL &&
124             mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
125                 switch (mpd_entity_get_type(e1)) {
126                 case MPD_ENTITY_TYPE_UNKNOWN:
127                         break;
128                 case MPD_ENTITY_TYPE_DIRECTORY:
129                         n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
130                                            mpd_directory_get_path(mpd_entity_get_directory(e2)));
131                         break;
132                 case MPD_ENTITY_TYPE_SONG:
133                         break;
134                 case MPD_ENTITY_TYPE_PLAYLIST:
135                         n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
136                                            mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
137                 }
138         }
139         return n;
142 /* Sorts the whole filelist, at the moment used by filelist_search */
143 void
144 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
146         g_ptr_array_sort_with_data(filelist->entries,
147                         filelist_compare_indirect,
148                         compare_func);
152 /* Only sorts the directories and playlist files.
153  * The songs stay in the order it came from mpd. */
154 void
155 filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
157         unsigned first, last;
158         const struct mpd_entity *iter;
160         assert(filelist && filelist->entries);
162         if (filelist->entries->len < 2)
163                 return;
164         iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
165         /* This can only happen at the beginning of the filelist,
166          * because NULL stands for "[..]" */
167         if (iter == NULL) {
168                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
169                 first = 1;
170         }
171         else
172                 first = 0;
173         /* find the last directory entry */
174         for (last = first+1; last < filelist->entries->len; last++) {
175                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
176                 if (mpd_entity_get_type(iter) != MPD_ENTITY_TYPE_DIRECTORY)
177                         break;
178         }
179         if (last == filelist->entries->len - 1)
180                 last++;
181         /* sort the directories */
182         if (last - first > 1)
183                 g_qsort_with_data(filelist->entries->pdata + first,
184                                 last - first, sizeof(gpointer),
185                                 filelist_compare_indirect, compare_func);
186         /* find the first playlist entry */
187         for (first = last; first < filelist->entries->len; first++) {
188                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
189                 if (mpd_entity_get_type(iter) == MPD_ENTITY_TYPE_PLAYLIST)
190                         break;
191         }
192         /* sort the playlist entries */
193         if (filelist->entries->len - first > 1)
194                 g_qsort_with_data(filelist->entries->pdata + first,
195                                 filelist->entries->len - first, sizeof(gpointer),
196                                 filelist_compare_indirect, compare_func);
199 static bool
200 same_song(const struct mpd_song *a, const struct mpd_song *b)
202         return strcmp(mpd_song_get_uri(a), mpd_song_get_uri(b)) == 0;
205 int
206 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
208         guint i;
210         assert(song != NULL);
212         for (i = 0; i < filelist_length(fl); ++i) {
213                 struct filelist_entry *entry = filelist_get(fl, i);
214                 const struct mpd_entity *entity  = entry->entity;
216                 if (entity != NULL &&
217                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
218                         const struct mpd_song *song2 =
219                                 mpd_entity_get_song(entity);
221                         if (same_song(song, song2))
222                                 return i;
223                 }
224         }
226         return -1;
229 int
230 filelist_find_directory(struct filelist *filelist, const char *name)
232         guint i;
234         assert(name != NULL);
236         for (i = 0; i < filelist_length(filelist); ++i) {
237                 struct filelist_entry *entry = filelist_get(filelist, i);
238                 const struct mpd_entity *entity  = entry->entity;
240                 if (entity != NULL &&
241                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
242                     strcmp(mpd_directory_get_path(mpd_entity_get_directory(entity)),
243                            name) == 0)
244                         return i;
245         }
247         return -1;
250 void
251 filelist_recv(struct filelist *filelist, struct mpd_connection *connection)
253         struct mpd_entity *entity;
255         while ((entity = mpd_recv_entity(connection)) != NULL)
256                 filelist_append(filelist, entity);
259 struct filelist *
260 filelist_new_recv(struct mpd_connection *connection)
262         struct filelist *filelist = filelist_new();
263         filelist_recv(filelist, connection);
264         return filelist;