PlaylistMapper.cxx 2.72 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
21
#include "PlaylistMapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "PlaylistFile.hxx"
23
#include "PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Mapper.hxx"
25
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriUtil.hxx"
27 28 29 30

#include <assert.h>

static struct playlist_provider *
31
playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
32
		   struct input_stream **is_r)
33 34 35
{
	struct playlist_provider *playlist;

36
	playlist = playlist_list_open_uri(path_fs, mutex, cond);
37 38 39
	if (playlist != NULL)
		*is_r = NULL;
	else
40
		playlist = playlist_list_open_path(path_fs, mutex, cond, is_r);
41 42 43 44 45 46 47 48

	return playlist;
}

/**
 * Load a playlist from the configured playlist directory.
 */
static struct playlist_provider *
49
playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
50
			      struct input_stream **is_r)
51 52 53 54 55
{
	char *path_fs;

	assert(spl_valid_name(uri));

56 57
	const Path &playlist_directory_fs = map_spl_path();
	if (playlist_directory_fs.IsNull())
58 59
		return NULL;

60
	path_fs = g_build_filename(playlist_directory_fs.c_str(), uri, NULL);
61

62 63
	struct playlist_provider *playlist =
		playlist_open_path(path_fs, mutex, cond, is_r);
64 65 66 67 68 69 70 71 72
	g_free(path_fs);

	return playlist;
}

/**
 * Load a playlist from the configured music directory.
 */
static struct playlist_provider *
73
playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
74
			   struct input_stream **is_r)
75 76 77
{
	assert(uri_safe_local(uri));

78 79
	Path path = map_uri_fs(uri);
	if (path.IsNull())
80 81
		return NULL;

82
	return playlist_open_path(path.c_str(), mutex, cond, is_r);
83 84 85
}

struct playlist_provider *
86
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
87
		     struct input_stream **is_r)
88 89 90 91
{
	struct playlist_provider *playlist;

	if (spl_valid_name(uri)) {
92 93
		playlist = playlist_open_in_playlist_dir(uri, mutex, cond,
							 is_r);
94 95 96 97 98
		if (playlist != NULL)
			return playlist;
	}

	if (uri_safe_local(uri)) {
99
		playlist = playlist_open_in_music_dir(uri, mutex, cond, is_r);
100 101 102 103 104 105
		if (playlist != NULL)
			return playlist;
	}

	return NULL;
}