PlaylistQueue.cxx 2.25 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 "DetachedSong.hxx"
28
#include "thread/Mutex.hxx"
29
#include "thread/Cond.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "fs/Traits.hxx"
31

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

36 37
#include <memory>

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

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

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

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

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

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

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