playlist_list.c 8.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 22
#include "playlist_list.h"
#include "playlist_plugin.h"
23
#include "playlist/extm3u_playlist_plugin.h"
24
#include "playlist/m3u_playlist_plugin.h"
25
#include "playlist/xspf_playlist_plugin.h"
26
#include "playlist/lastfm_playlist_plugin.h"
27
#include "playlist/despotify_playlist_plugin.h"
28
#include "playlist/soundcloud_playlist_plugin.h"
Qball Cow's avatar
Qball Cow committed
29
#include "playlist/pls_playlist_plugin.h"
30
#include "playlist/asx_playlist_plugin.h"
31
#include "playlist/rss_playlist_plugin.h"
32
#include "playlist/cue_playlist_plugin.h"
33
#include "playlist/embcue_playlist_plugin.h"
34 35
#include "input_stream.h"
#include "uri.h"
36
#include "string_util.h"
37
#include "conf.h"
38
#include "mpd_error.h"
39 40

#include <assert.h>
41
#include <string.h>
42
#include <stdio.h>
43

44
const struct playlist_plugin *const playlist_plugins[] = {
45
	&extm3u_playlist_plugin,
46
	&m3u_playlist_plugin,
47
	&xspf_playlist_plugin,
Qball Cow's avatar
Qball Cow committed
48
	&pls_playlist_plugin,
49
	&asx_playlist_plugin,
50
	&rss_playlist_plugin,
51 52 53
#ifdef ENABLE_DESPOTIFY
	&despotify_playlist_plugin,
#endif
54 55
#ifdef ENABLE_LASTFM
	&lastfm_playlist_plugin,
56 57 58
#endif
#ifdef ENABLE_SOUNDCLOUD
	&soundcloud_playlist_plugin,
59 60
#endif
	&cue_playlist_plugin,
61
	&embcue_playlist_plugin,
62 63 64 65 66 67
	NULL
};

/** which plugins have been initialized successfully? */
static bool playlist_plugins_enabled[G_N_ELEMENTS(playlist_plugins)];

68 69 70 71
#define playlist_plugins_for_each_enabled(plugin) \
	playlist_plugins_for_each(plugin) \
		if (playlist_plugins_enabled[playlist_plugin_iterator - playlist_plugins])

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * Find the "playlist" configuration block for the specified plugin.
 *
 * @param plugin_name the name of the playlist plugin
 * @return the configuration block, or NULL if none was configured
 */
static const struct config_param *
playlist_plugin_config(const char *plugin_name)
{
	const struct config_param *param = NULL;

	assert(plugin_name != NULL);

	while ((param = config_get_next_param(CONF_PLAYLIST_PLUGIN, param)) != NULL) {
		const char *name =
			config_get_block_string(param, "name", NULL);
		if (name == NULL)
89
			MPD_ERROR("playlist configuration without 'plugin' name in line %d",
90 91 92 93 94 95 96 97 98
				param->line);

		if (strcmp(name, plugin_name) == 0)
			return param;
	}

	return NULL;
}

99 100 101
void
playlist_list_global_init(void)
{
102 103 104 105 106 107 108 109 110
	for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
		const struct playlist_plugin *plugin = playlist_plugins[i];
		const struct config_param *param =
			playlist_plugin_config(plugin->name);

		if (!config_get_block_bool(param, "enabled", true))
			/* the plugin is disabled in mpd.conf */
			continue;

111
		playlist_plugins_enabled[i] =
112 113
			playlist_plugin_init(playlist_plugins[i], param);
	}
114 115 116 117 118
}

void
playlist_list_global_finish(void)
{
119 120
	playlist_plugins_for_each_enabled(plugin)
		playlist_plugin_finish(plugin);
121 122
}

123
static struct playlist_provider *
124 125
playlist_list_open_uri_scheme(const char *uri, GMutex *mutex, GCond *cond,
			      bool *tried)
126 127
{
	char *scheme;
128
	struct playlist_provider *playlist = NULL;
129 130 131 132 133 134 135 136 137 138

	assert(uri != NULL);

	scheme = g_uri_parse_scheme(uri);
	if (scheme == NULL)
		return NULL;

	for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
		const struct playlist_plugin *plugin = playlist_plugins[i];

139 140
		assert(!tried[i]);

141 142
		if (playlist_plugins_enabled[i] && plugin->open_uri != NULL &&
		    plugin->schemes != NULL &&
143
		    string_array_contains(plugin->schemes, scheme)) {
144 145
			playlist = playlist_plugin_open_uri(plugin, uri,
							    mutex, cond);
146 147
			if (playlist != NULL)
				break;
148 149

			tried[i] = true;
150 151 152 153 154 155 156
		}
	}

	g_free(scheme);
	return playlist;
}

157
static struct playlist_provider *
158 159
playlist_list_open_uri_suffix(const char *uri, GMutex *mutex, GCond *cond,
			      const bool *tried)
160 161 162 163 164 165
{
	const char *suffix;
	struct playlist_provider *playlist = NULL;

	assert(uri != NULL);

166 167
	suffix = uri_get_suffix(uri);
	if (suffix == NULL)
168 169 170 171 172 173 174 175
		return NULL;

	for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
		const struct playlist_plugin *plugin = playlist_plugins[i];

		if (playlist_plugins_enabled[i] && !tried[i] &&
		    plugin->open_uri != NULL && plugin->suffixes != NULL &&
		    string_array_contains(plugin->suffixes, suffix)) {
176 177
			playlist = playlist_plugin_open_uri(plugin, uri,
							    mutex, cond);
178 179 180 181 182 183 184 185 186
			if (playlist != NULL)
				break;
		}
	}

	return playlist;
}

struct playlist_provider *
187
playlist_list_open_uri(const char *uri, GMutex *mutex, GCond *cond)
188 189 190 191 192 193 194 195 196 197
{
	struct playlist_provider *playlist;
	/** this array tracks which plugins have already been tried by
	    playlist_list_open_uri_scheme() */
	bool tried[G_N_ELEMENTS(playlist_plugins) - 1];

	assert(uri != NULL);

	memset(tried, false, sizeof(tried));

198
	playlist = playlist_list_open_uri_scheme(uri, mutex, cond, tried);
199
	if (playlist == NULL)
200 201
		playlist = playlist_list_open_uri_suffix(uri, mutex, cond,
							 tried);
202 203 204 205

	return playlist;
}

206
static struct playlist_provider *
207
playlist_list_open_stream_mime2(struct input_stream *is, const char *mime)
208 209 210 211
{
	struct playlist_provider *playlist;

	assert(is != NULL);
212
	assert(mime != NULL);
213

214 215
	playlist_plugins_for_each_enabled(plugin) {
		if (plugin->open_stream != NULL &&
216
		    plugin->mime_types != NULL &&
217
		    string_array_contains(plugin->mime_types, mime)) {
218 219
			/* rewind the stream, so each plugin gets a
			   fresh start */
220
			input_stream_seek(is, 0, SEEK_SET, NULL);
221

222 223 224 225 226 227 228 229 230
			playlist = playlist_plugin_open_stream(plugin, is);
			if (playlist != NULL)
				return playlist;
		}
	}

	return NULL;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static struct playlist_provider *
playlist_list_open_stream_mime(struct input_stream *is)
{
	assert(is->mime != NULL);

	const char *semicolon = strchr(is->mime, ';');
	if (semicolon == NULL)
		return playlist_list_open_stream_mime2(is, is->mime);

	if (semicolon == is->mime)
		return NULL;

	/* probe only the portion before the semicolon*/
	char *mime = g_strndup(is->mime, semicolon - is->mime);
	struct playlist_provider *playlist =
		playlist_list_open_stream_mime2(is, mime);
	g_free(mime);
	return playlist;
}

251 252 253 254 255 256 257 258
static struct playlist_provider *
playlist_list_open_stream_suffix(struct input_stream *is, const char *suffix)
{
	struct playlist_provider *playlist;

	assert(is != NULL);
	assert(suffix != NULL);

259 260
	playlist_plugins_for_each_enabled(plugin) {
		if (plugin->open_stream != NULL &&
261
		    plugin->suffixes != NULL &&
262
		    string_array_contains(plugin->suffixes, suffix)) {
263 264
			/* rewind the stream, so each plugin gets a
			   fresh start */
265
			input_stream_seek(is, 0, SEEK_SET, NULL);
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			playlist = playlist_plugin_open_stream(plugin, is);
			if (playlist != NULL)
				return playlist;
		}
	}

	return NULL;
}

struct playlist_provider *
playlist_list_open_stream(struct input_stream *is, const char *uri)
{
	const char *suffix;
	struct playlist_provider *playlist;

282
	input_stream_lock_wait_ready(is);
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	if (is->mime != NULL) {
		playlist = playlist_list_open_stream_mime(is);
		if (playlist != NULL)
			return playlist;
	}

	suffix = uri != NULL ? uri_get_suffix(uri) : NULL;
	if (suffix != NULL) {
		playlist = playlist_list_open_stream_suffix(is, suffix);
		if (playlist != NULL)
			return playlist;
	}

	return NULL;
}
299

300
bool
301 302 303 304
playlist_suffix_supported(const char *suffix)
{
	assert(suffix != NULL);

305 306
	playlist_plugins_for_each_enabled(plugin) {
		if (plugin->suffixes != NULL &&
307
		    string_array_contains(plugin->suffixes, suffix))
308 309 310 311 312 313 314
			return true;
	}

	return false;
}

struct playlist_provider *
315 316
playlist_list_open_path(const char *path_fs, GMutex *mutex, GCond *cond,
			struct input_stream **is_r)
317
{
318
	GError *error = NULL;
319
	const char *suffix;
320
	struct input_stream *is;
321 322 323 324 325
	struct playlist_provider *playlist;

	assert(path_fs != NULL);

	suffix = uri_get_suffix(path_fs);
326
	if (suffix == NULL || !playlist_suffix_supported(suffix))
327 328
		return NULL;

329
	is = input_stream_open(path_fs, mutex, cond, &error);
330
	if (is == NULL) {
331 332 333 334 335 336 337 338
		if (error != NULL) {
			g_warning("%s", error->message);
			g_error_free(error);
		}

		return NULL;
	}

339
	input_stream_lock_wait_ready(is);
340

341
	playlist = playlist_list_open_stream_suffix(is, suffix);
342 343 344
	if (playlist != NULL)
		*is_r = is;
	else
345 346 347 348
		input_stream_close(is);

	return playlist;
}