PlaylistQueue.cxx 2.34 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
 * 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 23
#include "PlaylistQueue.hxx"
#include "PlaylistAny.hxx"
#include "PlaylistSong.hxx"
24
#include "Playlist.hxx"
25
#include "InputStream.hxx"
26
#include "SongEnumerator.hxx"
27
#include "DetachedSong.hxx"
28
#include "thread/Cond.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "fs/Traits.hxx"
30

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

41
	DetachedSong *song;
42
	for (unsigned i = 0;
43
	     i < end_index && (song = e.NextSong()) != nullptr;
44 45 46
	     ++i) {
		if (i < start_index) {
			/* skip songs before the start index */
47
			delete song;
48 49 50
			continue;
		}

51 52 53
		if (!playlist_check_translate_song(*song, base_uri.c_str(),
						   secure)) {
			delete song;
54
			continue;
55
		}
56

57 58
		PlaylistResult result = dest.AppendSong(pc, std::move(*song));
		delete song;
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
}