PlaylistCommands.cxx 5.38 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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.
 */

#include "config.h"
#include "PlaylistCommands.hxx"
22
#include "Request.hxx"
23
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "db/DatabasePlaylist.hxx"
25
#include "CommandError.hxx"
26
#include "PlaylistSave.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "PlaylistFile.hxx"
28
#include "PlaylistError.hxx"
29
#include "db/PlaylistVector.hxx"
30
#include "SongLoader.hxx"
31
#include "song/DetachedSong.hxx"
32
#include "BulkEdit.hxx"
33 34
#include "playlist/PlaylistQueue.hxx"
#include "playlist/Print.hxx"
Max Kellermann's avatar
Max Kellermann committed
35
#include "TimePrint.hxx"
36
#include "client/Client.hxx"
37
#include "client/Response.hxx"
38 39
#include "Mapper.hxx"
#include "fs/AllocatedPath.hxx"
Max Kellermann's avatar
Max Kellermann committed
40
#include "util/UriUtil.hxx"
41
#include "util/ConstBuffer.hxx"
42
#include "util/ChronoUtil.hxx"
43
#include "LocateUri.hxx"
44

45
bool
46
playlist_commands_available() noexcept
47 48 49 50
{
	return !map_spl_path().IsNull();
}

51
static void
52
print_spl_list(Response &r, const PlaylistVector &list)
53
{
54
	for (const auto &i : list) {
55
		r.Format("playlist: %s\n", i.name.c_str());
56

57
		if (!IsNegative(i.mtime))
58
			time_print(r, "Last-Modified", i.mtime);
59 60 61
	}
}

62
CommandResult
63
handle_save(Client &client, Request args, gcc_unused Response &r)
64
{
65
	spl_save_playlist(args.front(), client.GetPlaylist());
66
	return CommandResult::OK;
67 68
}

69
CommandResult
70
handle_load(Client &client, Request args, gcc_unused Response &r)
71
{
72 73
	const auto uri = LocateUri(UriPluginKind::PLAYLIST, args.front(),
				   &client
74 75 76 77
#ifdef ENABLE_DATABASE
					   , nullptr
#endif
					   );
78
	RangeArg range = args.ParseOptional(1, RangeArg::All());
79

80
	const ScopeBulkEdit bulk_edit(client.GetPartition());
81

82 83 84
	auto &playlist = client.GetPlaylist();
	const unsigned old_size = playlist.GetLength();

85
	const SongLoader loader(client);
86
	playlist_open_into_queue(uri,
87
				 range.start, range.end,
88
				 playlist,
89
				 client.GetPlayerControl(), loader);
90 91 92 93 94 95 96

	/* invoke the RemoteTagScanner on all newly added songs */
	auto &instance = client.GetInstance();
	const unsigned new_size = playlist.GetLength();
	for (unsigned i = old_size; i < new_size; ++i)
		instance.LookupRemoteTag(playlist.queue.Get(i).GetURI());

97
	return CommandResult::OK;
98 99
}

100
CommandResult
101
handle_listplaylist(Client &client, Request args, Response &r)
102
{
103 104
	const auto name = LocateUri(UriPluginKind::PLAYLIST, args.front(),
				    &client
105 106 107 108
#ifdef ENABLE_DATABASE
					   , nullptr
#endif
					   );
109

110 111
	if (playlist_file_print(r, client.GetPartition(), SongLoader(client),
				name, false))
112
		return CommandResult::OK;
113

114
	throw PlaylistError::NoSuchList();
115 116
}

117
CommandResult
118
handle_listplaylistinfo(Client &client, Request args, Response &r)
119
{
120 121
	const auto name = LocateUri(UriPluginKind::PLAYLIST, args.front(),
				    &client
122 123 124 125
#ifdef ENABLE_DATABASE
					   , nullptr
#endif
					   );
126

127
	if (playlist_file_print(r, client.GetPartition(), SongLoader(client),
128
				name, true))
129
		return CommandResult::OK;
130

131
	throw PlaylistError::NoSuchList();
132 133
}

134
CommandResult
135
handle_rm(gcc_unused Client &client, Request args, gcc_unused Response &r)
136
{
137 138
	const char *const name = args.front();

139 140
	spl_delete(name);
	return CommandResult::OK;
141 142
}

143
CommandResult
144
handle_rename(gcc_unused Client &client, Request args, gcc_unused Response &r)
145
{
146 147 148
	const char *const old_name = args[0];
	const char *const new_name = args[1];

149 150
	spl_rename(old_name, new_name);
	return CommandResult::OK;
151 152
}

153
CommandResult
154 155
handle_playlistdelete(gcc_unused Client &client,
		      Request args, gcc_unused Response &r)
156 157
{
	const char *const name = args[0];
158
	unsigned from = args.ParseUnsigned(1);
159

160 161
	spl_remove_index(name, from);
	return CommandResult::OK;
162 163
}

164
CommandResult
165 166
handle_playlistmove(gcc_unused Client &client,
		    Request args, gcc_unused Response &r)
167
{
168
	const char *const name = args.front();
169 170
	unsigned from = args.ParseUnsigned(1);
	unsigned to = args.ParseUnsigned(2);
171

172 173
	spl_move_index(name, from, to);
	return CommandResult::OK;
174 175
}

176
CommandResult
177 178
handle_playlistclear(gcc_unused Client &client,
		     Request args, gcc_unused Response &r)
179
{
180 181
	const char *const name = args.front();

182 183
	spl_clear(name);
	return CommandResult::OK;
184 185
}

186
CommandResult
187
handle_playlistadd(Client &client, Request args, gcc_unused Response &r)
188
{
189 190
	const char *const playlist = args[0];
	const char *const uri = args[1];
191 192

	if (uri_has_scheme(uri)) {
193
		const SongLoader loader(client);
194
		spl_append_uri(playlist, loader, uri);
195 196
	} else {
#ifdef ENABLE_DATABASE
197
		const Database &db = client.GetDatabaseOrThrow();
198

199
		search_add_to_playlist(db, client.GetStorage(),
200
				       uri, playlist, nullptr);
201
#else
202
		r.Error(ACK_ERROR_NO_EXIST, "directory or file not found");
203
		return CommandResult::ERROR;
204
#endif
205 206
	}

207
	return CommandResult::OK;
208 209
}

210
CommandResult
211 212
handle_listplaylists(gcc_unused Client &client, gcc_unused Request args,
		     Response &r)
213
{
214
	print_spl_list(r, ListPlaylistFiles());
215
	return CommandResult::OK;
216
}