XspfPlaylistPlugin.cxx 5.15 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21
#include "XspfPlaylistPlugin.hxx"
22 23
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
24
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "input/InputStream.hxx"
26
#include "tag/TagBuilder.hxx"
27
#include "util/Error.hxx"
28
#include "util/Domain.hxx"
29
#include "lib/expat/ExpatParser.hxx"
30
#include "Log.hxx"
31 32 33

#include <string.h>

34
static constexpr Domain xspf_domain("xspf");
35 36

/**
37
 * This is the state object for our XML parser.
38
 */
39
struct XspfParser {
40 41 42 43
	/**
	 * The list of songs (in reverse order because that's faster
	 * while adding).
	 */
44
	std::forward_list<DetachedSong> songs;
45 46 47 48 49 50 51 52 53 54 55 56 57 58

	/**
	 * The current position in the XML file.
	 */
	enum {
		ROOT, PLAYLIST, TRACKLIST, TRACK,
		LOCATION,
	} state;

	/**
	 * The current tag within the "track" element.  This is only
	 * valid if state==TRACK.  TAG_NUM_OF_ITEM_TYPES means there
	 * is no (known) tag.
	 */
59
	TagType tag_type;
60 61

	/**
62
	 * The current song URI.  It is set by the "location" element.
63
	 */
64
	std::string location;
65

66 67
	TagBuilder tag_builder;

68
	XspfParser()
69
		:state(ROOT) {}
70 71
};

72 73 74
static void XMLCALL
xspf_start_element(void *user_data, const XML_Char *element_name,
		   gcc_unused const XML_Char **atts)
75
{
76
	XspfParser *parser = (XspfParser *)user_data;
77 78

	switch (parser->state) {
79
	case XspfParser::ROOT:
80
		if (strcmp(element_name, "playlist") == 0)
81
			parser->state = XspfParser::PLAYLIST;
82 83 84

		break;

85
	case XspfParser::PLAYLIST:
86
		if (strcmp(element_name, "trackList") == 0)
87
			parser->state = XspfParser::TRACKLIST;
88 89 90

		break;

91
	case XspfParser::TRACKLIST:
92
		if (strcmp(element_name, "track") == 0) {
93
			parser->state = XspfParser::TRACK;
94
			parser->location.clear();
95
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
96 97 98 99
		}

		break;

100
	case XspfParser::TRACK:
101
		if (strcmp(element_name, "location") == 0)
102
			parser->state = XspfParser::LOCATION;
103
		else if (strcmp(element_name, "title") == 0)
104
			parser->tag_type = TAG_TITLE;
105 106 107
		else if (strcmp(element_name, "creator") == 0)
			/* TAG_COMPOSER would be more correct
			   according to the XSPF spec */
108
			parser->tag_type = TAG_ARTIST;
109
		else if (strcmp(element_name, "annotation") == 0)
110
			parser->tag_type = TAG_COMMENT;
111
		else if (strcmp(element_name, "album") == 0)
112
			parser->tag_type = TAG_ALBUM;
113
		else if (strcmp(element_name, "trackNum") == 0)
114
			parser->tag_type = TAG_TRACK;
115 116 117

		break;

118
	case XspfParser::LOCATION:
119 120 121 122
		break;
	}
}

123 124
static void XMLCALL
xspf_end_element(void *user_data, const XML_Char *element_name)
125
{
126
	XspfParser *parser = (XspfParser *)user_data;
127 128

	switch (parser->state) {
129
	case XspfParser::ROOT:
130 131
		break;

132
	case XspfParser::PLAYLIST:
133
		if (strcmp(element_name, "playlist") == 0)
134
			parser->state = XspfParser::ROOT;
135 136 137

		break;

138
	case XspfParser::TRACKLIST:
139
		if (strcmp(element_name, "tracklist") == 0)
140
			parser->state = XspfParser::PLAYLIST;
141 142 143

		break;

144
	case XspfParser::TRACK:
145
		if (strcmp(element_name, "track") == 0) {
146 147 148
			if (!parser->location.empty())
				parser->songs.emplace_front(std::move(parser->location),
							    parser->tag_builder.Commit());
149

150
			parser->state = XspfParser::TRACKLIST;
151
		} else
152
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
153 154 155

		break;

156 157
	case XspfParser::LOCATION:
		parser->state = XspfParser::TRACK;
158 159 160 161
		break;
	}
}

162 163
static void XMLCALL
xspf_char_data(void *user_data, const XML_Char *s, int len)
164
{
165
	XspfParser *parser = (XspfParser *)user_data;
166 167

	switch (parser->state) {
168 169 170
	case XspfParser::ROOT:
	case XspfParser::PLAYLIST:
	case XspfParser::TRACKLIST:
171 172
		break;

173
	case XspfParser::TRACK:
174
		if (!parser->location.empty() &&
175
		    parser->tag_type != TAG_NUM_OF_ITEM_TYPES)
176
			parser->tag_builder.AddItem(parser->tag_type, s, len);
177 178 179

		break;

180
	case XspfParser::LOCATION:
181
		parser->location.assign(s, len);
182 183 184 185 186 187 188 189 190 191

		break;
	}
}

/*
 * The playlist object
 *
 */

192
static SongEnumerator *
193
xspf_open_stream(InputStream &is)
194
{
195
	XspfParser parser;
196

197 198 199 200 201 202 203 204
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(xspf_start_element, xspf_end_element);
		expat.SetCharacterDataHandler(xspf_char_data);

		Error error;
		if (!expat.Parse(is, error)) {
			LogError(error);
205
			return nullptr;
206 207 208
		}
	}

209
	parser.songs.reverse();
210
	return new MemorySongEnumerator(std::move(parser.songs));
211 212 213 214
}

static const char *const xspf_suffixes[] = {
	"xspf",
215
	nullptr
216 217 218 219
};

static const char *const xspf_mime_types[] = {
	"application/xspf+xml",
220
	nullptr
221 222 223
};

const struct playlist_plugin xspf_playlist_plugin = {
224
	"xspf",
225

226 227 228 229
	nullptr,
	nullptr,
	nullptr,
	xspf_open_stream,
230

231 232 233
	nullptr,
	xspf_suffixes,
	xspf_mime_types,
234
};