PlaylistQueue.cxx 2.21 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "PlaylistError.hxx"
25
#include "queue/Playlist.hxx"
26
#include "SongEnumerator.hxx"
27
#include "song/DetachedSong.hxx"
28
#include "thread/Mutex.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "fs/Traits.hxx"
30

31 32 33 34
#ifdef ENABLE_DATABASE
#include "SongLoader.hxx"
#endif

35 36
#include <memory>

37
void
38
playlist_load_into_queue(const char *uri, SongEnumerator &e,
39
			 unsigned start_index, unsigned end_index,
40
			 playlist &dest, PlayerControl &pc,
41
			 const SongLoader &loader)
42
{
Max Kellermann's avatar
Max Kellermann committed
43
	const std::string base_uri = uri != nullptr
44
		? PathTraitsUTF8::GetParent(uri)
Max Kellermann's avatar
Max Kellermann committed
45
		: std::string(".");
46

47
	std::unique_ptr<DetachedSong> song;
48
	for (unsigned i = 0;
49
	     i < end_index && (song = e.NextSong()) != nullptr;
50 51 52 53 54 55
	     ++i) {
		if (i < start_index) {
			/* skip songs before the start index */
			continue;
		}

56
		if (!playlist_check_translate_song(*song, base_uri.c_str(),
57
						   loader)) {
58
			continue;
59
		}
60

61
		dest.AppendSong(pc, std::move(*song));
62 63 64
	}
}

65
void
66
playlist_open_into_queue(const char *uri,
67
			 unsigned start_index, unsigned end_index,
68
			 playlist &dest, PlayerControl &pc,
69
			 const SongLoader &loader)
70
{
71
	Mutex mutex;
72

73
	auto playlist = playlist_open_any(uri,
74
#ifdef ENABLE_DATABASE
75
					  loader.GetStorage(),
76
#endif
77
					  mutex);
78 79
	if (playlist == nullptr)
		throw PlaylistError::NoSuchList();
80

81 82 83
	playlist_load_into_queue(uri, *playlist,
				 start_index, end_index,
				 dest, pc, loader);
84
}