Code

Update copyright notices
[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 void
114 filelist_sort(struct filelist *filelist, GCompareFunc compare_func)
116         g_ptr_array_sort_with_data(filelist->entries,
117                                    filelist_compare_indirect,
118                                    compare_func);
121 int
122 filelist_find_song(struct filelist *fl, const struct mpd_song *song)
124         guint i;
126         assert(song != NULL);
128         for (i = 0; i < filelist_length(fl); ++i) {
129                 struct filelist_entry *entry = filelist_get(fl, i);
130                 mpd_InfoEntity *entity  = entry->entity;
132                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
133                         struct mpd_song *song2 = entity->info.song;
135                         if (strcmp(song->file, song2->file) == 0)
136                                 return i;
137                 }
138         }
140         return -1;
143 int
144 filelist_find_directory(struct filelist *filelist, const char *name)
146         guint i;
148         assert(name != NULL);
150         for (i = 0; i < filelist_length(filelist); ++i) {
151                 struct filelist_entry *entry = filelist_get(filelist, i);
152                 mpd_InfoEntity *entity  = entry->entity;
154                 if (entity && entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY &&
155                     strcmp(entity->info.directory->path, name) == 0)
156                         return i;
157         }
159         return -1;