PlaylistQueue.cxx 2.42 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
 * 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
#include "PlaylistQueue.hxx"
22
#include "PlaylistPlugin.hxx"
23 24
#include "PlaylistAny.hxx"
#include "PlaylistSong.hxx"
25
#include "Playlist.hxx"
26
#include "input_stream.h"
27 28

extern "C" {
29
#include "song.h"
30
}
31 32

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

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

51
		song = playlist_check_translate_song(song, base_uri, secure);
52
		if (song == NULL)
53 54
			continue;

55
		result = dest->AppendSong(*pc, song);
56
		song_free(song);
57
		if (result != PLAYLIST_RESULT_SUCCESS) {
58
			g_free(base_uri);
59 60 61 62
			return result;
		}
	}

63 64
	g_free(base_uri);

65 66 67
	return PLAYLIST_RESULT_SUCCESS;
}

68
enum playlist_result
69
playlist_open_into_queue(const char *uri,
70
			 unsigned start_index, unsigned end_index,
71 72
			 struct playlist *dest, struct player_control *pc,
			 bool secure)
73
{
74 75
	Mutex mutex;
	Cond cond;
76

77
	struct input_stream *is;
78 79
	struct playlist_provider *playlist =
		playlist_open_any(uri, mutex, cond, &is);
80
	if (playlist == NULL)
81
		return PLAYLIST_RESULT_NO_SUCH_LIST;
82

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

88 89
	if (is != NULL)
		input_stream_close(is);
90

91
	return result;
92
}