PlaylistMapper.cxx 2.69 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/AllocatedPath.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "util/UriUtil.hxx"
27 28 29

#include <assert.h>

30
static SongEnumerator *
31
playlist_open_path(const char *path_fs, Mutex &mutex, Cond &cond,
32
		   InputStream **is_r)
33
{
34
	auto playlist = playlist_list_open_uri(path_fs, mutex, cond);
35 36
	if (playlist != nullptr)
		*is_r = nullptr;
37
	else
38
		playlist = playlist_list_open_path(path_fs, mutex, cond, is_r);
39 40 41 42 43 44 45

	return playlist;
}

/**
 * Load a playlist from the configured playlist directory.
 */
46
static SongEnumerator *
47
playlist_open_in_playlist_dir(const char *uri, Mutex &mutex, Cond &cond,
48
			      InputStream **is_r)
49 50 51
{
	assert(spl_valid_name(uri));

52
	const auto &playlist_directory_fs = map_spl_path();
53
	if (playlist_directory_fs.IsNull())
54
		return nullptr;
55

56
	const auto uri_fs = AllocatedPath::FromUTF8(uri);
57 58
	if (uri_fs.IsNull())
		return nullptr;
59

60 61
	const auto path_fs =
		AllocatedPath::Build(playlist_directory_fs, uri_fs);
62
	assert(!path_fs.IsNull());
63

64
	return playlist_open_path(path_fs.c_str(), mutex, cond, is_r);
65 66 67 68 69
}

/**
 * Load a playlist from the configured music directory.
 */
70
static SongEnumerator *
71
playlist_open_in_music_dir(const char *uri, Mutex &mutex, Cond &cond,
72
			   InputStream **is_r)
73 74 75
{
	assert(uri_safe_local(uri));

76
	const auto path = map_uri_fs(uri);
77
	if (path.IsNull())
78
		return nullptr;
79

80
	return playlist_open_path(path.c_str(), mutex, cond, is_r);
81 82
}

83
SongEnumerator *
84
playlist_mapper_open(const char *uri, Mutex &mutex, Cond &cond,
85
		     InputStream **is_r)
86 87
{
	if (spl_valid_name(uri)) {
88 89
		auto playlist = playlist_open_in_playlist_dir(uri, mutex, cond,
							      is_r);
90
		if (playlist != nullptr)
91 92 93 94
			return playlist;
	}

	if (uri_safe_local(uri)) {
95 96
		auto playlist = playlist_open_in_music_dir(uri, mutex, cond,
							   is_r);
97
		if (playlist != nullptr)
98 99 100
			return playlist;
	}

101
	return nullptr;
102
}