Code

filelist: removed filelist_prepend()
[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 static bool
178 same_song(const struct mpd_song *a, const struct mpd_song *b)
180         return strcmp(mpd_song_get_uri(a), mpd_song_get_uri(b)) == 0;
183 int
184 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
186         guint i;
188         assert(song != NULL);
190         for (i = 0; i < filelist_length(fl); ++i) {
191                 struct filelist_entry *entry = filelist_get(fl, i);
192                 const struct mpd_entity *entity  = entry->entity;
194                 if (entity != NULL &&
195                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
196                         const struct mpd_song *song2 =
197                                 mpd_entity_get_song(entity);
199                         if (same_song(song, song2))
200                                 return i;
201                 }
202         }
204         return -1;
207 int
208 filelist_find_directory(struct filelist *filelist, const char *name)
210         guint i;
212         assert(name != NULL);
214         for (i = 0; i < filelist_length(filelist); ++i) {
215                 struct filelist_entry *entry = filelist_get(filelist, i);
216                 const struct mpd_entity *entity  = entry->entity;
218                 if (entity != NULL &&
219                     mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY &&
220                     strcmp(mpd_directory_get_path(mpd_entity_get_directory(entity)),
221                            name) == 0)
222                         return i;
223         }
225         return -1;
228 void
229 filelist_recv(struct filelist *filelist, struct mpd_connection *connection)
231         struct mpd_entity *entity;
233         while ((entity = mpd_recv_entity(connection)) != NULL)
234                 filelist_append(filelist, entity);
237 struct filelist *
238 filelist_new_recv(struct mpd_connection *connection)
240         struct filelist *filelist = filelist_new();
241         filelist_recv(filelist, connection);
242         return filelist;