PlaylistQueue.cxx 2.32 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 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 "Song.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

Max Kellermann's avatar
Max Kellermann committed
41
	Song *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
			song->Free();
48 49 50
			continue;
		}

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

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

62
	return PlaylistResult::SUCCESS;
63 64
}

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

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

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

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

88
	return result;
89
}