Code

mpdclient: moved code to playlist.c
[ncmpc.git] / src / playlist.c
1 /*
2  * $Id$
3  *
4  * (c) 2004 by Kalle Wallin <kaw@linux.se>
5  * (c) 2008 Max Kellermann <max@duempel.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
22 #include "playlist.h"
23 #include "mpdclient.h"
24 #include "ncmpc.h"
26 #include <string.h>
28 #define ENABLE_PLCHANGES
30 #define MPD_ERROR(c) (c==NULL || c->connection==NULL || c->connection->error)
32 gint
33 mpdclient_playlist_free(mpdclient_playlist_t *playlist)
34 {
35         guint i;
37         for (i = 0; i < playlist->list->len; ++i) {
38                 struct mpd_song *song = g_array_index(playlist->list, struct mpd_song *, i);
39                 mpd_freeSong(song);
40         }
42         g_array_free(playlist->list, TRUE);
43         memset(playlist, 0, sizeof(mpdclient_playlist_t));
44         return 0;
45 }
47 struct mpd_song *
48 playlist_get_song(mpdclient_t *c, gint idx)
49 {
50         if (idx < 0 || (guint)idx >= c->playlist.list->len)
51                 return NULL;
53         return g_array_index(c->playlist.list, struct mpd_song *, idx);
54 }
56 struct mpd_song *
57 playlist_lookup_song(mpdclient_t *c, gint id)
58 {
59         guint i;
61         for (i = 0; i < c->playlist.list->len; ++i) {
62                 struct mpd_song *song = g_array_index(c->playlist.list,
63                                                       struct mpd_song *, i);
64                 if (song->id == id)
65                         return song;
66         }
68         return NULL;
69 }
71 gint
72 playlist_get_index(mpdclient_t *c, struct mpd_song *song)
73 {
74         guint i;
76         for (i = 0; i < c->playlist.list->len; ++i) {
77                 if (g_array_index(c->playlist.list, struct mpd_song *, i)
78                     == song)
79                         return (gint)i;
80         }
82         return -1;
83 }
85 gint
86 playlist_get_index_from_id(mpdclient_t *c, gint id)
87 {
88         guint i;
90         for (i = 0; i < c->playlist.list->len; ++i) {
91                 struct mpd_song *song = g_array_index(c->playlist.list,
92                                                       struct mpd_song *, i);
93                 if (song->id == id)
94                         return (gint)i;
95         }
97         return -1;
98 }
100 gint
101 playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
103         guint i;
105         for (i = 0; i < c->playlist.list->len; ++i) {
106                 struct mpd_song *song = g_array_index(c->playlist.list,
107                                                       struct mpd_song *, i);
108                 if(strcmp(song->file, filename) == 0)
109                         return (gint)i;
110         }
112         return -1;