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/asx_playlist_plugin.h"
22
#include "playlist_plugin.h"
23
#include "input_stream.h"
33
#define G_LOG_DOMAIN "asx"
36
* This is the state object for the GLib XML parser.
40
* The list of songs (in reverse order because that's faster
46
* The current position in the XML file.
53
* The current tag within the "entry" element. This is only
54
* valid if state==ENTRY. TAG_NUM_OF_ITEM_TYPES means there
60
* The current song. It is allocated after the "location"
67
get_attribute(const gchar **attribute_names, const gchar **attribute_values,
70
for (unsigned i = 0; attribute_names[i] != NULL; ++i)
71
if (g_ascii_strcasecmp(attribute_names[i], name) == 0)
72
return attribute_values[i];
78
asx_start_element(G_GNUC_UNUSED GMarkupParseContext *context,
79
const gchar *element_name,
80
const gchar **attribute_names,
81
const gchar **attribute_values,
82
gpointer user_data, G_GNUC_UNUSED GError **error)
84
struct asx_parser *parser = user_data;
86
switch (parser->state) {
88
if (g_ascii_strcasecmp(element_name, "entry") == 0) {
89
parser->state = ENTRY;
90
parser->song = song_remote_new("asx:");
91
parser->tag = TAG_NUM_OF_ITEM_TYPES;
97
if (g_ascii_strcasecmp(element_name, "ref") == 0) {
98
const gchar *href = get_attribute(attribute_names,
102
/* create new song object, and copy
103
the existing tag over; we cannot
104
replace the existing song's URI,
105
because that attribute is
107
struct song *song = song_remote_new(href);
109
if (parser->song != NULL) {
110
song->tag = parser->song->tag;
111
parser->song->tag = NULL;
112
song_free(parser->song);
117
} else if (g_ascii_strcasecmp(element_name, "author") == 0)
118
/* is that correct? or should it be COMPOSER
120
parser->tag = TAG_ARTIST;
121
else if (g_ascii_strcasecmp(element_name, "title") == 0)
122
parser->tag = TAG_TITLE;
129
asx_end_element(G_GNUC_UNUSED GMarkupParseContext *context,
130
const gchar *element_name,
131
gpointer user_data, G_GNUC_UNUSED GError **error)
133
struct asx_parser *parser = user_data;
135
switch (parser->state) {
140
if (g_ascii_strcasecmp(element_name, "entry") == 0) {
141
if (strcmp(parser->song->uri, "asx:") != 0)
142
parser->songs = g_slist_prepend(parser->songs,
145
song_free(parser->song);
147
parser->state = ROOT;
149
parser->tag = TAG_NUM_OF_ITEM_TYPES;
156
asx_text(G_GNUC_UNUSED GMarkupParseContext *context,
157
const gchar *text, gsize text_len,
158
gpointer user_data, G_GNUC_UNUSED GError **error)
160
struct asx_parser *parser = user_data;
162
switch (parser->state) {
167
if (parser->tag != TAG_NUM_OF_ITEM_TYPES) {
168
if (parser->song->tag == NULL)
169
parser->song->tag = tag_new();
170
tag_add_item_n(parser->song->tag, parser->tag,
178
static const GMarkupParser asx_parser = {
179
.start_element = asx_start_element,
180
.end_element = asx_end_element,
185
song_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data)
187
struct song *song = data;
193
asx_parser_destroy(gpointer data)
195
struct asx_parser *parser = data;
197
if (parser->state >= ENTRY)
198
song_free(parser->song);
200
g_slist_foreach(parser->songs, song_free_callback, NULL);
201
g_slist_free(parser->songs);
205
* The playlist object
209
struct asx_playlist {
210
struct playlist_provider base;
215
static struct playlist_provider *
216
asx_open_stream(struct input_stream *is)
218
struct asx_parser parser = {
222
struct asx_playlist *playlist;
223
GMarkupParseContext *context;
227
GError *error = NULL;
229
/* parse the ASX XML file */
231
context = g_markup_parse_context_new(&asx_parser,
232
G_MARKUP_TREAT_CDATA_AS_TEXT,
233
&parser, asx_parser_destroy);
236
nbytes = input_stream_read(is, buffer, sizeof(buffer), &error);
239
g_markup_parse_context_free(context);
240
g_warning("%s", error->message);
248
success = g_markup_parse_context_parse(context, buffer, nbytes,
251
g_warning("XML parser failed: %s", error->message);
253
g_markup_parse_context_free(context);
258
success = g_markup_parse_context_end_parse(context, &error);
260
g_warning("XML parser failed: %s", error->message);
262
g_markup_parse_context_free(context);
266
/* create a #asx_playlist object from the parsed song list */
268
playlist = g_new(struct asx_playlist, 1);
269
playlist_provider_init(&playlist->base, &asx_playlist_plugin);
270
playlist->songs = g_slist_reverse(parser.songs);
273
g_markup_parse_context_free(context);
275
return &playlist->base;
279
asx_close(struct playlist_provider *_playlist)
281
struct asx_playlist *playlist = (struct asx_playlist *)_playlist;
283
g_slist_foreach(playlist->songs, song_free_callback, NULL);
284
g_slist_free(playlist->songs);
289
asx_read(struct playlist_provider *_playlist)
291
struct asx_playlist *playlist = (struct asx_playlist *)_playlist;
294
if (playlist->songs == NULL)
297
song = playlist->songs->data;
298
playlist->songs = g_slist_remove(playlist->songs, song);
303
static const char *const asx_suffixes[] = {
308
static const char *const asx_mime_types[] = {
313
const struct playlist_plugin asx_playlist_plugin = {
316
.open_stream = asx_open_stream,
320
.suffixes = asx_suffixes,
321
.mime_types = asx_mime_types,