DatabasePlaylist.cxx 2.27 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "DatabasePlaylist.hxx"
21
#include "DatabaseSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "PlaylistFile.hxx"
23
#include "Interface.hxx"
24
#include "song/DetachedSong.hxx"
25
#include "protocol/Ack.hxx"
26 27 28

#include <functional>

29
static void
30
AddSong(const Storage *storage, const char *playlist_path_utf8,
31
	const LightSong &song)
32
{
33 34
	spl_append_song(playlist_path_utf8,
			DatabaseDetachSong(storage, song));
35 36
}

37
void
38
search_add_to_playlist(const Database &db, const Storage *storage,
39 40
		       const char *playlist_path_utf8,
		       const DatabaseSelection &selection)
41
{
42
	const auto f = [=](auto && arg1) { return AddSong(storage, playlist_path_utf8, arg1); };
43
	db.Visit(selection, f);
44
}
45

46
unsigned
47 48 49 50 51 52 53
SearchInsertIntoPlaylist(const Database &db, const Storage *storage,
			 const DatabaseSelection &selection,
			 PlaylistFileEditor &playlist,
			 unsigned position)
{
	assert(position <= playlist.size());

54 55
	unsigned n = 0;

56
	db.Visit(selection, [&playlist, position, &n, storage](const auto &song){
57
		playlist.Insert(position + n,
58
				DatabaseDetachSong(storage, song));
59
		++n;
60
	});
61 62

	return n;
63
}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

void
SearchInsertIntoPlaylist(const Database &db, const Storage *storage,
			 const DatabaseSelection &selection,
			 const char *playlist_name,
			 unsigned position)
{
	PlaylistFileEditor editor{
		playlist_name,
		PlaylistFileEditor::LoadMode::TRY,
	};

	if (position > editor.size())
		throw ProtocolError{ACK_ERROR_ARG, "Bad position"};

	if (SearchInsertIntoPlaylist(db, storage, selection,
				     editor, position) > 0)
		editor.Save();
}