Code

46b9a1c8d82820d263be67aaee84a119d4b7a0fd
[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 void
70 filelist_move(struct filelist *filelist, struct filelist *from)
71 {
72         guint i;
74         for (i = 0; i < filelist_length(from); ++i)
75                 g_ptr_array_add(filelist->entries,
76                                 g_ptr_array_index(from->entries, i));
78         g_ptr_array_set_size(from->entries, 0);
79 }
81 static gint
82 filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
83 {
84         GCompareFunc compare_func = data;
85         gconstpointer a = *(const gconstpointer*)ap;
86         gconstpointer b = *(const gconstpointer*)bp;
88         return compare_func(a, b);
89 }
91 gint
92 compare_filelist_entry_path(gconstpointer filelist_entry1,
93                             gconstpointer filelist_entry2)
94 {
95         const struct mpd_entity *e1, *e2;
96         int n = 0;
98         e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
99         e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
101         if (e1 != NULL && e2 != NULL &&
102             mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
103                 switch (mpd_entity_get_type(e1)) {
104                 case MPD_ENTITY_TYPE_UNKNOWN:
105                         break;
106                 case MPD_ENTITY_TYPE_DIRECTORY:
107                         n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
108                                            mpd_directory_get_path(mpd_entity_get_directory(e2)));
109                         break;
110                 case MPD_ENTITY_TYPE_SONG:
111                         break;
112                 case MPD_ENTITY_TYPE_PLAYLIST:
113                         n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
114                                            mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
115                 }
116         }
117         return n;
120 /* Sorts the whole filelist, at the moment used by filelist_search */
121 void
122 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
124         g_ptr_array_sort_with_data(filelist->entries,
125                         filelist_compare_indirect,
126                         compare_func);
130 /* Only sorts the directories and playlist files.
131  * The songs stay in the order it came from mpd. */
132 void
133 filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
135         unsigned first, last;
136         const struct mpd_entity *iter;
138         assert(filelist && filelist->entries);
140         if (filelist->entries->len < 2)
141                 return;
142         iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
143         /* This can only happen at the beginning of the filelist,
144          * because NULL stands for "[..]" */
145         if (iter == NULL) {
146                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
147                 first = 1;
148         }
149         else
150                 first = 0;
151         /* find the last directory entry */
152         for (last = first+1; last < filelist->entries->len; last++) {
153                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
154                 if (mpd_entity_get_type(iter) != MPD_ENTITY_TYPE_DIRECTORY)
155                         break;
156         }
157         if (last == filelist->entries->len - 1)
158                 last++;
159         /* sort the directories */
160         if (last - first > 1)
161                 g_qsort_with_data(filelist->entries->pdata + first,
162                                 last - first, sizeof(gpointer),
163                                 filelist_compare_indirect, compare_func);
164         /* find the first playlist entry */
165         for (first = last; first < filelist->entries->len; first++) {
166                 iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, first))->entity;
167                 if (mpd_entity_get_type(iter) == MPD_ENTITY_TYPE_PLAYLIST)
168                         break;
169         }
170         /* sort the playlist entries */
171         if (filelist->entries->len - first > 1)
172                 g_qsort_with_data(filelist->entries->pdata + first,
173                                 filelist->entries->len - first, sizeof(gpointer),
174                                 filelist_compare_indirect, compare_func);
177 void
178 filelist_no_duplicates(struct filelist *filelist)
180         for (int i = filelist_length(filelist) - 1; i >= 0; --i) {
181                 struct filelist_entry *entry = filelist_get(filelist, i);
182                 const struct mpd_song *song;
184                 if (entry->entity == NULL ||
185                     mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
186                         continue;
188                 song = mpd_entity_get_song(entry->entity);
189                 if (filelist_find_song(filelist, song) < i) {
190                         g_ptr_array_remove_index(filelist->entries, i);
191                         mpd_entity_free(entry->entity);
192                         g_slice_free(struct filelist_entry, entry);
193                 }
194         }
197 static bool
198 same_song(const struct mpd_song *a, const struct mpd_song *b)
200         return strcmp(mpd_song_get_uri(a), mpd_song_get_uri(b)) == 0;
203 int
204 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
206         guint i;
208         assert(song != NULL);
210         for (i = 0; i < filelist_length(fl); ++i) {
211                 struct filelist_entry *entry = filelist_get(fl, i);
212                 const struct mpd_entity *entity  = entry->entity;
214                 if (entity != NULL &&
215                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
216                         const struct mpd_song *song2 =
217                                 mpd_entity_get_song(entity);
219                         if (same_song(song, song2))
220                                 return i;
221                 }
222         }
224         return -1;
227 int
228 filelist_find_directory(struct filelist *filelist, const char *name)
230         guint i;
232         assert(name != NULL);
234         for (i = 0; i < filelist_length(filelist); ++i) {
235                 struct filelist_entry *entry = filelist_get(filelist, i);
236                 const struct mpd_entity *entity  = entry->entity;
238                 if (entity != NULL &&
239                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
240                     strcmp(mpd_directory_get_path(mpd_entity_get_directory(entity)),
241                            name) == 0)
242                         return i;
243         }
245         return -1;
248 void
249 filelist_recv(struct filelist *filelist, struct mpd_connection *connection)
251         struct mpd_entity *entity;
253         while ((entity = mpd_recv_entity(connection)) != NULL)
254                 filelist_append(filelist, entity);
257 struct filelist *
258 filelist_new_recv(struct mpd_connection *connection)
260         struct filelist *filelist = filelist_new();
261         filelist_recv(filelist, connection);
262         return filelist;