2
* Copyright (C) 2003-2010 The Music Player Daemon Project
3
* http://www.musicpd.org
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.
21
#include "playlist/xspf_playlist_plugin.h"
22
#include "playlist_plugin.h"
23
#include "input_stream.h"
34
#define G_LOG_DOMAIN "xspf"
37
* This is the state object for the GLib XML parser.
41
* The list of songs (in reverse order because that's faster
47
* The current position in the XML file.
50
ROOT, PLAYLIST, TRACKLIST, TRACK,
55
* The current tag within the "track" element. This is only
56
* valid if state==TRACK. TAG_NUM_OF_ITEM_TYPES means there
62
* The current song. It is allocated after the "location"
69
xspf_start_element(G_GNUC_UNUSED GMarkupParseContext *context,
70
const gchar *element_name,
71
G_GNUC_UNUSED const gchar **attribute_names,
72
G_GNUC_UNUSED const gchar **attribute_values,
73
gpointer user_data, G_GNUC_UNUSED GError **error)
75
struct xspf_parser *parser = user_data;
77
switch (parser->state) {
79
if (strcmp(element_name, "playlist") == 0)
80
parser->state = PLAYLIST;
85
if (strcmp(element_name, "trackList") == 0)
86
parser->state = TRACKLIST;
91
if (strcmp(element_name, "track") == 0) {
92
parser->state = TRACK;
94
parser->tag = TAG_NUM_OF_ITEM_TYPES;
100
if (strcmp(element_name, "location") == 0)
101
parser->state = LOCATION;
102
else if (strcmp(element_name, "title") == 0)
103
parser->tag = TAG_TITLE;
104
else if (strcmp(element_name, "creator") == 0)
105
/* TAG_COMPOSER would be more correct
106
according to the XSPF spec */
107
parser->tag = TAG_ARTIST;
108
else if (strcmp(element_name, "annotation") == 0)
109
parser->tag = TAG_COMMENT;
110
else if (strcmp(element_name, "album") == 0)
111
parser->tag = TAG_ALBUM;
112
else if (strcmp(element_name, "trackNum") == 0)
113
parser->tag = TAG_TRACK;
123
xspf_end_element(G_GNUC_UNUSED GMarkupParseContext *context,
124
const gchar *element_name,
125
gpointer user_data, G_GNUC_UNUSED GError **error)
127
struct xspf_parser *parser = user_data;
129
switch (parser->state) {
134
if (strcmp(element_name, "playlist") == 0)
135
parser->state = ROOT;
140
if (strcmp(element_name, "tracklist") == 0)
141
parser->state = PLAYLIST;
146
if (strcmp(element_name, "track") == 0) {
147
if (parser->song != NULL)
148
parser->songs = g_slist_prepend(parser->songs,
151
parser->state = TRACKLIST;
153
parser->tag = TAG_NUM_OF_ITEM_TYPES;
158
parser->state = TRACK;
164
xspf_text(G_GNUC_UNUSED GMarkupParseContext *context,
165
const gchar *text, gsize text_len,
166
gpointer user_data, G_GNUC_UNUSED GError **error)
168
struct xspf_parser *parser = user_data;
170
switch (parser->state) {
177
if (parser->song != NULL &&
178
parser->tag != TAG_NUM_OF_ITEM_TYPES) {
179
if (parser->song->tag == NULL)
180
parser->song->tag = tag_new();
181
tag_add_item_n(parser->song->tag, parser->tag,
188
if (parser->song == NULL) {
189
char *uri = g_strndup(text, text_len);
190
parser->song = song_remote_new(uri);
198
static const GMarkupParser xspf_parser = {
199
.start_element = xspf_start_element,
200
.end_element = xspf_end_element,
205
song_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
207
struct song *song = data;
213
xspf_parser_destroy(gpointer data)
215
struct xspf_parser *parser = data;
217
if (parser->state >= TRACK && parser->song != NULL)
218
song_free(parser->song);
220
g_slist_foreach(parser->songs, song_free_callback, NULL);
221
g_slist_free(parser->songs);
225
* The playlist object
229
struct xspf_playlist {
230
struct playlist_provider base;
235
static struct playlist_provider *
236
xspf_open_stream(struct input_stream *is)
238
struct xspf_parser parser = {
242
struct xspf_playlist *playlist;
243
GMarkupParseContext *context;
247
GError *error = NULL;
249
/* parse the XSPF XML file */
251
context = g_markup_parse_context_new(&xspf_parser,
252
G_MARKUP_TREAT_CDATA_AS_TEXT,
253
&parser, xspf_parser_destroy);
256
nbytes = input_stream_read(is, buffer, sizeof(buffer), &error);
259
g_markup_parse_context_free(context);
260
g_warning("%s", error->message);
268
success = g_markup_parse_context_parse(context, buffer, nbytes,
271
g_warning("XML parser failed: %s", error->message);
273
g_markup_parse_context_free(context);
278
success = g_markup_parse_context_end_parse(context, &error);
280
g_warning("XML parser failed: %s", error->message);
282
g_markup_parse_context_free(context);
286
/* create a #xspf_playlist object from the parsed song list */
288
playlist = g_new(struct xspf_playlist, 1);
289
playlist_provider_init(&playlist->base, &xspf_playlist_plugin);
290
playlist->songs = g_slist_reverse(parser.songs);
293
g_markup_parse_context_free(context);
295
return &playlist->base;
299
xspf_close(struct playlist_provider *_playlist)
301
struct xspf_playlist *playlist = (struct xspf_playlist *)_playlist;
303
g_slist_foreach(playlist->songs, song_free_callback, NULL);
304
g_slist_free(playlist->songs);
309
xspf_read(struct playlist_provider *_playlist)
311
struct xspf_playlist *playlist = (struct xspf_playlist *)_playlist;
314
if (playlist->songs == NULL)
317
song = playlist->songs->data;
318
playlist->songs = g_slist_remove(playlist->songs, song);
323
static const char *const xspf_suffixes[] = {
328
static const char *const xspf_mime_types[] = {
329
"application/xspf+xml",
333
const struct playlist_plugin xspf_playlist_plugin = {
336
.open_stream = xspf_open_stream,
340
.suffixes = xspf_suffixes,
341
.mime_types = xspf_mime_types,