QueueCommands.cxx 8.95 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 "QueueCommands.hxx"
22
#include "Request.hxx"
23
#include "CommandError.hxx"
24
#include "protocol/RangeArg.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26
#include "db/DatabaseQueue.hxx"
#include "db/Selection.hxx"
27
#include "song/Filter.hxx"
28
#include "SongLoader.hxx"
29
#include "song/DetachedSong.hxx"
30
#include "LocateUri.hxx"
31
#include "queue/Playlist.hxx"
32
#include "PlaylistPrint.hxx"
33
#include "client/Client.hxx"
34
#include "client/Response.hxx"
35
#include "Partition.hxx"
36
#include "Instance.hxx"
37
#include "BulkEdit.hxx"
38
#include "util/ConstBuffer.hxx"
39
#include "util/Exception.hxx"
40
#include "util/StringAPI.hxx"
41
#include "util/NumberParser.hxx"
42

43
#include <memory>
44
#include <limits>
45

46 47
static void
AddUri(Client &client, const LocatedUri &uri)
48
{
49
	auto &partition = client.GetPartition();
50 51
	partition.playlist.AppendSong(partition.pc,
				      SongLoader(client).LoadSong(uri));
52 53 54
}

static CommandResult
55 56
AddDatabaseSelection(Client &client, const char *uri,
		     gcc_unused Response &r)
57 58
{
#ifdef ENABLE_DATABASE
59 60
	auto &partition = client.GetPartition();
	const ScopeBulkEdit bulk_edit(partition);
61 62

	const DatabaseSelection selection(uri, true);
63
	AddFromDatabase(partition, selection);
64
	return CommandResult::OK;
65 66 67 68 69 70 71
#else
	(void)client;
	(void)uri;

	r.Error(ACK_ERROR_NO_EXIST, "No database");
	return CommandResult::ERROR;
#endif
72
}
73

74
CommandResult
75
handle_add(Client &client, Request args, Response &r)
76
{
77
	const char *uri = args.front();
78
	if (StringIsEqual(uri, "/"))
79 80 81 82 83 84 85
		/* this URI is malformed, but some clients are buggy
		   and use "add /" to add the whole database, which
		   was never intended to work, but once did; in order
		   to retain backwards compatibility, work around this
		   here */
		uri = "";

86 87
	const auto located_uri = LocateUri(UriPluginKind::INPUT, uri,
					   &client
88
#ifdef ENABLE_DATABASE
89
					   , nullptr
90
#endif
91
					   );
92 93
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
94 95 96 97
		AddUri(client, located_uri);
		client.GetInstance().LookupRemoteTag(located_uri.canonical_uri);
		return CommandResult::OK;

98
	case LocatedUri::Type::PATH:
99 100
		AddUri(client, located_uri);
		return CommandResult::OK;
101

102 103 104
	case LocatedUri::Type::RELATIVE:
		return AddDatabaseSelection(client, located_uri.canonical_uri,
					    r);
105 106
	}

107
	gcc_unreachable();
108 109
}

110
CommandResult
111
handle_addid(Client &client, Request args, Response &r)
112
{
113
	const char *const uri = args.front();
114

115
	auto &partition = client.GetPartition();
116
	const SongLoader loader(client);
117
	unsigned added_id = partition.AppendURI(loader, uri);
118
	partition.instance.LookupRemoteTag(uri);
119

120
	if (args.size == 2) {
121
		unsigned to = args.ParseUnsigned(1);
122

123
		try {
124
			partition.MoveId(added_id, to);
125 126
		} catch (...) {
			/* rollback */
127
			partition.DeleteId(added_id);
128
			throw;
129 130 131
		}
	}

132
	r.Format("Id: %u\n", added_id);
133
	return CommandResult::OK;
134 135
}

136 137 138 139 140 141
/**
 * Parse a string in the form "START:END", both being (optional)
 * fractional non-negative time offsets in seconds.  Returns both in
 * integer milliseconds.  Omitted values are zero.
 */
static bool
142
parse_time_range(const char *p, SongTime &start_r, SongTime &end_r)
143 144 145 146 147 148 149
{
	char *endptr;

	const float start = ParseFloat(p, &endptr);
	if (*endptr != ':' || start < 0)
		return false;

150 151 152
	start_r = endptr > p
		? SongTime::FromS(start)
		: SongTime::zero();
153 154 155 156 157 158 159

	p = endptr + 1;

	const float end = ParseFloat(p, &endptr);
	if (*endptr != 0 || end < 0)
		return false;

160 161 162
	end_r = endptr > p
		? SongTime::FromS(end)
		: SongTime::zero();
163

164
	return end_r.IsZero() || end_r > start_r;
165 166 167
}

CommandResult
168
handle_rangeid(Client &client, Request args, Response &r)
169
{
170
	unsigned id = args.ParseUnsigned(0);
171

172
	SongTime start, end;
173
	if (!parse_time_range(args[1], start, end)) {
174
		r.Error(ACK_ERROR_ARG, "Bad range");
175 176 177
		return CommandResult::ERROR;
	}

178 179
	client.GetPlaylist().SetSongIdRange(client.GetPlayerControl(),
					    id, start, end);
180 181 182
	return CommandResult::OK;
}

183
CommandResult
184
handle_delete(Client &client, Request args, gcc_unused Response &r)
185
{
186
	RangeArg range = args.ParseRange(0);
187
	client.GetPartition().DeleteRange(range.start, range.end);
188
	return CommandResult::OK;
189 190
}

191
CommandResult
192
handle_deleteid(Client &client, Request args, gcc_unused Response &r)
193
{
194
	unsigned id = args.ParseUnsigned(0);
195
	client.GetPartition().DeleteId(id);
196
	return CommandResult::OK;
197 198
}

199
CommandResult
200
handle_playlist(Client &client, gcc_unused Request args, Response &r)
201
{
202
	playlist_print_uris(r, client.GetPlaylist());
203
	return CommandResult::OK;
204 205
}

206
CommandResult
207
handle_shuffle(gcc_unused Client &client, Request args, gcc_unused Response &r)
208
{
209
	RangeArg range = args.ParseOptional(0, RangeArg::All());
210
	client.GetPartition().Shuffle(range.start, range.end);
211
	return CommandResult::OK;
212 213
}

214
CommandResult
215
handle_clear(Client &client, gcc_unused Request args, gcc_unused Response &r)
216
{
217
	client.GetPartition().ClearQueue();
218
	return CommandResult::OK;
219 220
}

221
CommandResult
222
handle_plchanges(Client &client, Request args, Response &r)
223
{
224 225
	uint32_t version = ParseCommandArgU32(args.front());
	RangeArg range = args.ParseOptional(1, RangeArg::All());
226
	playlist_print_changes_info(r, client.GetPlaylist(), version,
227
				    range.start, range.end);
228
	return CommandResult::OK;
229 230
}

231
CommandResult
232
handle_plchangesposid(Client &client, Request args, Response &r)
233
{
234 235
	uint32_t version = ParseCommandArgU32(args.front());
	RangeArg range = args.ParseOptional(1, RangeArg::All());
236
	playlist_print_changes_position(r, client.GetPlaylist(), version,
237
					range.start, range.end);
238
	return CommandResult::OK;
239 240
}

241
CommandResult
242
handle_playlistinfo(Client &client, Request args, Response &r)
243
{
244
	RangeArg range = args.ParseOptional(0, RangeArg::All());
245

246
	playlist_print_info(r, client.GetPlaylist(),
247
			    range.start, range.end);
248
	return CommandResult::OK;
249 250
}

251
CommandResult
252
handle_playlistid(Client &client, Request args, Response &r)
253
{
254
	if (!args.empty()) {
255
		unsigned id = args.ParseUnsigned(0);
256
		playlist_print_id(r, client.GetPlaylist(), id);
257
	} else {
258
		playlist_print_info(r, client.GetPlaylist(),
259
				    0, std::numeric_limits<unsigned>::max());
260 261
	}

262
	return CommandResult::OK;
263 264
}

265
static CommandResult
266
handle_playlist_match(Client &client, Request args, Response &r,
267 268
		      bool fold_case)
{
269
	SongFilter filter;
270 271 272 273 274
	try {
		filter.Parse(args, fold_case);
	} catch (...) {
		r.Error(ACK_ERROR_ARG,
			GetFullMessage(std::current_exception()).c_str());
275
		return CommandResult::ERROR;
276
	}
277
	filter.Optimize();
278

279
	playlist_print_find(r, client.GetPlaylist(), filter);
280
	return CommandResult::OK;
281 282
}

283
CommandResult
284
handle_playlistfind(Client &client, Request args, Response &r)
285
{
286
	return handle_playlist_match(client, args, r, false);
287 288
}

289
CommandResult
290
handle_playlistsearch(Client &client, Request args, Response &r)
291
{
292
	return handle_playlist_match(client, args, r, true);
293 294
}

295
CommandResult
296
handle_prio(Client &client, Request args, gcc_unused Response &r)
297
{
298 299
	unsigned priority = args.ParseUnsigned(0, 0xff);
	args.shift();
300

301 302
	auto &partition = client.GetPartition();

303
	for (const char *i : args) {
304
		RangeArg range = ParseCommandArgRange(i);
305
		partition.SetPriorityRange(range.start, range.end, priority);
306 307
	}

308
	return CommandResult::OK;
309 310
}

311
CommandResult
312
handle_prioid(Client &client, Request args, gcc_unused Response &r)
313
{
314 315
	unsigned priority = args.ParseUnsigned(0, 0xff);
	args.shift();
316

317 318
	auto &partition = client.GetPartition();

319
	for (const char *i : args) {
320
		unsigned song_id = ParseCommandArgUnsigned(i);
321
		partition.SetPriorityId(song_id, priority);
322 323
	}

324
	return CommandResult::OK;
325 326
}

327
CommandResult
328
handle_move(Client &client, Request args, gcc_unused Response &r)
329
{
330 331
	RangeArg range = args.ParseRange(0);
	int to = args.ParseInt(1);
332
	client.GetPartition().MoveRange(range.start, range.end, to);
333
	return CommandResult::OK;
334 335
}

336
CommandResult
337
handle_moveid(Client &client, Request args, gcc_unused Response &r)
338
{
339 340
	unsigned id = args.ParseUnsigned(0);
	int to = args.ParseInt(1);
341
	client.GetPartition().MoveId(id, to);
342
	return CommandResult::OK;
343 344
}

345
CommandResult
346
handle_swap(Client &client, Request args, gcc_unused Response &r)
347
{
348 349
	unsigned song1 = args.ParseUnsigned(0);
	unsigned song2 = args.ParseUnsigned(1);
350
	client.GetPartition().SwapPositions(song1, song2);
351
	return CommandResult::OK;
352 353
}

354
CommandResult
355
handle_swapid(Client &client, Request args, gcc_unused Response &r)
356
{
357 358
	unsigned id1 = args.ParseUnsigned(0);
	unsigned id2 = args.ParseUnsigned(1);
359
	client.GetPartition().SwapIds(id1, id2);
360
	return CommandResult::OK;
361
}