playlist_queue.c 2.6 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_queue.h"
#include "playlist_plugin.h"
23
#include "playlist_any.h"
24
#include "playlist_song.h"
25
#include "playlist.h"
26 27 28 29
#include "song.h"
#include "input_stream.h"

enum playlist_result
30
playlist_load_into_queue(const char *uri, struct playlist_provider *source,
31
			 unsigned start_index, unsigned end_index,
32 33
			 struct playlist *dest, struct player_control *pc,
			 bool secure)
34 35 36
{
	enum playlist_result result;
	struct song *song;
37
	char *base_uri = uri != NULL ? g_path_get_dirname(uri) : NULL;
38

39 40 41 42 43 44 45 46 47 48
	for (unsigned i = 0;
	     i < end_index && (song = playlist_plugin_read(source)) != NULL;
	     ++i) {
		if (i < start_index) {
			/* skip songs before the start index */
			if (!song_in_database(song))
				song_free(song);
			continue;
		}

49
		song = playlist_check_translate_song(song, base_uri, secure);
50
		if (song == NULL)
51 52
			continue;

53
		result = playlist_append_song(dest, pc, song, NULL);
54
		if (result != PLAYLIST_RESULT_SUCCESS) {
55 56 57
			if (!song_in_database(song))
				song_free(song);
			g_free(base_uri);
58 59 60 61
			return result;
		}
	}

62 63
	g_free(base_uri);

64 65 66
	return PLAYLIST_RESULT_SUCCESS;
}

67
enum playlist_result
68
playlist_open_into_queue(const char *uri,
69
			 unsigned start_index, unsigned end_index,
70 71
			 struct playlist *dest, struct player_control *pc,
			 bool secure)
72
{
73 74 75
	GMutex *mutex = g_mutex_new();
	GCond *cond = g_cond_new();

76
	struct input_stream *is;
77 78 79 80 81
	struct playlist_provider *playlist =
		playlist_open_any(uri, mutex, cond, &is);
	if (playlist == NULL) {
		g_cond_free(cond);
		g_mutex_free(mutex);
82
		return PLAYLIST_RESULT_NO_SUCH_LIST;
83
	}
84

85
	enum playlist_result result =
86 87
		playlist_load_into_queue(uri, playlist, start_index, end_index,
					 dest, pc, secure);
88
	playlist_plugin_close(playlist);
89

90 91
	if (is != NULL)
		input_stream_close(is);
92

93 94 95
	g_cond_free(cond);
	g_mutex_free(mutex);

96
	return result;
97
}