PlaylistMapper.cxx 2.78 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 24

extern "C" {
25 26 27
#include "playlist_list.h"
#include "mapper.h"
#include "uri.h"
28
}
29 30 31 32

#include <assert.h>

static struct playlist_provider *
33 34
playlist_open_path(const char *path_fs, GMutex *mutex, GCond *cond,
		   struct input_stream **is_r)
35 36 37
{
	struct playlist_provider *playlist;

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

	return playlist;
}

/**
 * Load a playlist from the configured playlist directory.
 */
static struct playlist_provider *
51 52
playlist_open_in_playlist_dir(const char *uri, GMutex *mutex, GCond *cond,
			      struct input_stream **is_r)
53 54 55 56 57 58 59 60 61 62 63
{
	char *path_fs;

	assert(spl_valid_name(uri));

	const char *playlist_directory_fs = map_spl_path();
	if (playlist_directory_fs == NULL)
		return NULL;

	path_fs = g_build_filename(playlist_directory_fs, uri, NULL);

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

	return playlist;
}

/**
 * Load a playlist from the configured music directory.
 */
static struct playlist_provider *
75 76
playlist_open_in_music_dir(const char *uri, GMutex *mutex, GCond *cond,
			   struct input_stream **is_r)
77 78 79 80 81 82 83 84 85
{
	char *path_fs;

	assert(uri_safe_local(uri));

	path_fs = map_uri_fs(uri);
	if (path_fs == NULL)
		return NULL;

86 87
	struct playlist_provider *playlist =
		playlist_open_path(path_fs, mutex, cond, is_r);
88 89 90 91 92 93
	g_free(path_fs);

	return playlist;
}

struct playlist_provider *
94 95
playlist_mapper_open(const char *uri, GMutex *mutex, GCond *cond,
		     struct input_stream **is_r)
96 97 98 99
{
	struct playlist_provider *playlist;

	if (spl_valid_name(uri)) {
100 101
		playlist = playlist_open_in_playlist_dir(uri, mutex, cond,
							 is_r);
102 103 104 105 106
		if (playlist != NULL)
			return playlist;
	}

	if (uri_safe_local(uri)) {
107
		playlist = playlist_open_in_music_dir(uri, mutex, cond, is_r);
108 109 110 111 112 113
		if (playlist != NULL)
			return playlist;
	}

	return NULL;
}