Code

mpdclient: Fixes sorting of the filelist.
[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"
21 #include "libmpdclient.h"
23 #include <string.h>
24 #include <assert.h>
26 struct filelist *
27 filelist_new(const char *path)
28 {
29         struct filelist *filelist = g_malloc(sizeof(*filelist));
31         filelist->path = g_strdup(path);
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->path);
53         g_free(filelist);
54 }
56 struct filelist_entry *
57 filelist_append(struct filelist *filelist, struct mpd_InfoEntity *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_InfoEntity *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 /* Sorts the whole filelist, at the moment used by filelist_search */
114 void
115 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
117         g_ptr_array_sort_with_data(filelist->entries,
118                         filelist_compare_indirect,
119                         compare_func);
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)
128         unsigned first, last;
129         const mpd_InfoEntity *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 (iter->type != MPD_INFO_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 (iter->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
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);
170 int
171 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
173         guint i;
175         assert(song != NULL);
177         for (i = 0; i < filelist_length(fl); ++i) {
178                 struct filelist_entry *entry = filelist_get(fl, i);
179                 mpd_InfoEntity *entity  = entry->entity;
181                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
182                         struct mpd_song *song2 = entity->info.song;
184                         if (strcmp(song->file, song2->file) == 0)
185                                 return i;
186                 }
187         }
189         return -1;
192 int
193 filelist_find_directory(struct filelist *filelist, const char *name)
195         guint i;
197         assert(name != NULL);
199         for (i = 0; i < filelist_length(filelist); ++i) {
200                 struct filelist_entry *entry = filelist_get(filelist, i);
201                 mpd_InfoEntity *entity  = entry->entity;
203                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
204                     strcmp(entity->info.directory->path, name) == 0)
205                         return i;
206         }
208         return -1;