PlaylistQueue.cxx 2.43 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 "SongEnumerator.hxx"
26
#include "DetachedSong.hxx"
27
#include "thread/Mutex.hxx"
28
#include "thread/Cond.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
PlaylistResult
36
playlist_load_into_queue(const char *uri, SongEnumerator &e,
37
			 unsigned start_index, unsigned end_index,
38
			 playlist &dest, PlayerControl &pc,
39
			 const SongLoader &loader)
40
{
Max Kellermann's avatar
Max Kellermann committed
41
	const std::string base_uri = uri != nullptr
42
		? PathTraitsUTF8::GetParent(uri)
Max Kellermann's avatar
Max Kellermann committed
43
		: std::string(".");
44

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

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

61 62
		PlaylistResult result = dest.AppendSong(pc, std::move(*song));
		delete song;
Max Kellermann's avatar
Max Kellermann committed
63
		if (result != PlaylistResult::SUCCESS)
64 65 66
			return result;
	}

67
	return PlaylistResult::SUCCESS;
68 69
}

70
PlaylistResult
71
playlist_open_into_queue(const char *uri,
72
			 unsigned start_index, unsigned end_index,
73
			 playlist &dest, PlayerControl &pc,
74
			 const SongLoader &loader)
75
{
76 77
	Mutex mutex;
	Cond cond;
78

79 80 81 82 83
	auto playlist = playlist_open_any(uri,
#ifdef ENABLE_DATABASE
					  loader.GetStorage(),
#endif
					  mutex, cond);
84
	if (playlist == nullptr)
85
		return PlaylistResult::NO_SUCH_LIST;
86

87
	PlaylistResult result =
88 89
		playlist_load_into_queue(uri, *playlist,
					 start_index, end_index,
90
					 dest, pc, loader);
91
	delete playlist;
92
	return result;
93
}