PlaylistQueue.cxx 2.35 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 "InputStream.hxx"
27
#include "SongEnumerator.hxx"
28
#include "Song.hxx"
29
#include "thread/Cond.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "fs/Traits.hxx"
31

32
PlaylistResult
33
playlist_load_into_queue(const char *uri, SongEnumerator &e,
34
			 unsigned start_index, unsigned end_index,
35
			 playlist &dest, PlayerControl &pc,
36
			 bool secure)
37
{
Max Kellermann's avatar
Max Kellermann committed
38 39 40
	const std::string base_uri = uri != nullptr
		? PathTraits::GetParentUTF8(uri)
		: std::string(".");
41

Max Kellermann's avatar
Max Kellermann committed
42
	Song *song;
43
	for (unsigned i = 0;
44
	     i < end_index && (song = e.NextSong()) != nullptr;
45 46 47
	     ++i) {
		if (i < start_index) {
			/* skip songs before the start index */
48
			song->Free();
49 50 51
			continue;
		}

Max Kellermann's avatar
Max Kellermann committed
52 53
		song = playlist_check_translate_song(song, base_uri.c_str(),
						     secure);
54
		if (song == nullptr)
55 56
			continue;

Max Kellermann's avatar
Max Kellermann committed
57
		PlaylistResult result = dest.AppendSong(pc, song);
58
		song->Free();
Max Kellermann's avatar
Max Kellermann committed
59
		if (result != PlaylistResult::SUCCESS)
60 61 62
			return result;
	}

63
	return PlaylistResult::SUCCESS;
64 65
}

66
PlaylistResult
67
playlist_open_into_queue(const char *uri,
68
			 unsigned start_index, unsigned end_index,
69
			 playlist &dest, PlayerControl &pc,
70
			 bool secure)
71
{
72 73
	Mutex mutex;
	Cond cond;
74

75
	InputStream *is;
76
	auto playlist = playlist_open_any(uri, mutex, cond, &is);
77
	if (playlist == nullptr)
78
		return PlaylistResult::NO_SUCH_LIST;
79

80
	PlaylistResult result =
81 82
		playlist_load_into_queue(uri, *playlist,
					 start_index, end_index,
83
					 dest, pc, secure);
84
	delete playlist;
85

86
	if (is != nullptr)
87
		is->Close();
88

89
	return result;
90
}