QueueCommands.cxx 9.29 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "db/DatabaseQueue.hxx"
#include "db/Selection.hxx"
26 27
#include "SongFilter.hxx"
#include "SongLoader.hxx"
28 29
#include "DetachedSong.hxx"
#include "LocateUri.hxx"
30
#include "queue/Playlist.hxx"
31
#include "PlaylistPrint.hxx"
32
#include "client/Client.hxx"
33
#include "client/Response.hxx"
34
#include "Partition.hxx"
35
#include "BulkEdit.hxx"
36
#include "util/ConstBuffer.hxx"
37
#include "util/StringAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "util/UriUtil.hxx"
39
#include "util/NumberParser.hxx"
40
#include "util/Error.hxx"
41
#include "fs/AllocatedPath.hxx"
42

43
#include <limits>
44

45 46
#include <string.h>

47 48
static CommandResult
AddUri(Client &client, const LocatedUri &uri, Response &r)
49
{
50 51 52 53
	Error error;
	DetachedSong *song = SongLoader(client).LoadSong(uri, error);
	if (song == nullptr)
		return print_error(r, error);
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	auto &partition = client.partition;
	unsigned id = partition.playlist.AppendSong(partition.pc,
						    std::move(*song), error);
	delete song;
	if (id == 0)
		return print_error(r, error);

	return CommandResult::OK;
}

static CommandResult
AddDatabaseSelection(Client &client, const char *uri, Response &r)
{
#ifdef ENABLE_DATABASE
	const ScopeBulkEdit bulk_edit(client.partition);

	const DatabaseSelection selection(uri, true);
	Error error;
	return AddFromDatabase(client.partition, selection, error)
		? CommandResult::OK
		: print_error(r, error);
#else
	(void)client;
	(void)uri;

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

85
CommandResult
86
handle_add(Client &client, Request args, Response &r)
87
{
88
	const char *uri = args.front();
89
	if (StringIsEqual(uri, "/"))
90 91 92 93 94 95 96
		/* 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 = "";

97 98 99 100 101 102 103 104 105
	Error error;
	const auto located_uri = LocateUri(uri, &client,
#ifdef ENABLE_DATABASE
					   nullptr,
#endif
					   error);
	switch (located_uri.type) {
	case LocatedUri::Type::UNKNOWN:
		return print_error(r, error);
106

107 108 109
	case LocatedUri::Type::ABSOLUTE:
	case LocatedUri::Type::PATH:
		return AddUri(client, located_uri, r);
110

111 112 113
	case LocatedUri::Type::RELATIVE:
		return AddDatabaseSelection(client, located_uri.canonical_uri,
					    r);
114 115
	}

116
	gcc_unreachable();
117 118
}

119
CommandResult
120
handle_addid(Client &client, Request args, Response &r)
121
{
122
	const char *const uri = args.front();
123

124
	const SongLoader loader(client);
125 126 127
	Error error;
	unsigned added_id = client.partition.AppendURI(loader, uri, error);
	if (added_id == 0)
128
		return print_error(r, error);
129

130
	if (args.size == 2) {
131
		unsigned to = args.ParseUnsigned(1);
132

133 134 135 136 137
		try {
			client.partition.MoveId(added_id, to);
			return CommandResult::OK;
		} catch (...) {
			/* rollback */
138
			client.partition.DeleteId(added_id);
139
			throw;
140 141 142
		}
	}

143
	r.Format("Id: %u\n", added_id);
144
	return CommandResult::OK;
145 146
}

147 148 149 150 151 152
/**
 * 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
153
parse_time_range(const char *p, SongTime &start_r, SongTime &end_r)
154 155 156 157 158 159 160
{
	char *endptr;

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

161 162 163
	start_r = endptr > p
		? SongTime::FromS(start)
		: SongTime::zero();
164 165 166 167 168 169 170

	p = endptr + 1;

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

171 172 173
	end_r = endptr > p
		? SongTime::FromS(end)
		: SongTime::zero();
174

175
	return end_r.IsZero() || end_r > start_r;
176 177 178
}

CommandResult
179
handle_rangeid(Client &client, Request args, Response &r)
180
{
181
	unsigned id = args.ParseUnsigned(0);
182

183
	SongTime start, end;
184
	if (!parse_time_range(args[1], start, end)) {
185
		r.Error(ACK_ERROR_ARG, "Bad range");
186 187 188 189 190
		return CommandResult::ERROR;
	}

	Error error;
	if (!client.partition.playlist.SetSongIdRange(client.partition.pc,
191
						      id, start, end,
192
						      error))
193
		return print_error(r, error);
194 195 196 197

	return CommandResult::OK;
}

198
CommandResult
199
handle_delete(Client &client, Request args, gcc_unused Response &r)
200
{
201
	RangeArg range = args.ParseRange(0);
202 203
	client.partition.DeleteRange(range.start, range.end);
	return CommandResult::OK;
204 205
}

206
CommandResult
207
handle_deleteid(Client &client, Request args, gcc_unused Response &r)
208
{
209
	unsigned id = args.ParseUnsigned(0);
210 211
	client.partition.DeleteId(id);
	return CommandResult::OK;
212 213
}

214
CommandResult
215
handle_playlist(Client &client, gcc_unused Request args, Response &r)
216
{
217
	playlist_print_uris(r, client.partition, client.playlist);
218
	return CommandResult::OK;
219 220
}

221
CommandResult
222
handle_shuffle(gcc_unused Client &client, Request args, gcc_unused Response &r)
223
{
224
	RangeArg range = args.ParseOptional(0, RangeArg::All());
225
	client.partition.Shuffle(range.start, range.end);
226
	return CommandResult::OK;
227 228
}

229
CommandResult
230
handle_clear(Client &client, gcc_unused Request args, gcc_unused Response &r)
231
{
232
	client.partition.ClearQueue();
233
	return CommandResult::OK;
234 235
}

236
CommandResult
237
handle_plchanges(Client &client, Request args, Response &r)
238
{
239 240
	uint32_t version = ParseCommandArgU32(args.front());
	RangeArg range = args.ParseOptional(1, RangeArg::All());
241
	playlist_print_changes_info(r, client.partition,
242 243
				    client.playlist, version,
				    range.start, range.end);
244
	return CommandResult::OK;
245 246
}

247
CommandResult
248
handle_plchangesposid(Client &client, Request args, Response &r)
249
{
250 251
	uint32_t version = ParseCommandArgU32(args.front());
	RangeArg range = args.ParseOptional(1, RangeArg::All());
252 253
	playlist_print_changes_position(r, client.playlist, version,
					range.start, range.end);
254
	return CommandResult::OK;
255 256
}

257
CommandResult
258
handle_playlistinfo(Client &client, Request args, Response &r)
259
{
260
	RangeArg range = args.ParseOptional(0, RangeArg::All());
261

262
	if (!playlist_print_info(r, client.partition, client.playlist,
263
				 range.start, range.end))
264
		return print_playlist_result(r,
265
					     PlaylistResult::BAD_RANGE);
266

267
	return CommandResult::OK;
268 269
}

270
CommandResult
271
handle_playlistid(Client &client, Request args, Response &r)
272
{
273
	if (!args.IsEmpty()) {
274
		unsigned id = args.ParseUnsigned(0);
275 276
		bool ret = playlist_print_id(r, client.partition,
					     client.playlist, id);
277
		if (!ret)
278
			return print_playlist_result(r, PlaylistResult::NO_SUCH_SONG);
279
	} else {
280
		playlist_print_info(r, client.partition, client.playlist,
281
				    0, std::numeric_limits<unsigned>::max());
282 283
	}

284
	return CommandResult::OK;
285 286
}

287
static CommandResult
288
handle_playlist_match(Client &client, Request args, Response &r,
289 290
		      bool fold_case)
{
291
	SongFilter filter;
292
	if (!filter.Parse(args, fold_case)) {
293
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
294
		return CommandResult::ERROR;
295 296
	}

297
	playlist_print_find(r, client.partition, client.playlist, filter);
298
	return CommandResult::OK;
299 300
}

301
CommandResult
302
handle_playlistfind(Client &client, Request args, Response &r)
303
{
304
	return handle_playlist_match(client, args, r, false);
305 306
}

307
CommandResult
308
handle_playlistsearch(Client &client, Request args, Response &r)
309
{
310
	return handle_playlist_match(client, args, r, true);
311 312
}

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

319
	for (const char *i : args) {
320
		RangeArg range = ParseCommandArgRange(i);
321 322
		client.partition.SetPriorityRange(range.start, range.end,
						  priority);
323 324
	}

325
	return CommandResult::OK;
326 327
}

328
CommandResult
329
handle_prioid(Client &client, Request args, gcc_unused Response &r)
330
{
331 332
	unsigned priority = args.ParseUnsigned(0, 0xff);
	args.shift();
333

334
	for (const char *i : args) {
335
		unsigned song_id = ParseCommandArgUnsigned(i);
336
		client.partition.SetPriorityId(song_id, priority);
337 338
	}

339
	return CommandResult::OK;
340 341
}

342
CommandResult
343
handle_move(Client &client, Request args, gcc_unused Response &r)
344
{
345 346
	RangeArg range = args.ParseRange(0);
	int to = args.ParseInt(1);
347 348
	client.partition.MoveRange(range.start, range.end, to);
	return CommandResult::OK;
349 350
}

351
CommandResult
352
handle_moveid(Client &client, Request args, gcc_unused Response &r)
353
{
354 355
	unsigned id = args.ParseUnsigned(0);
	int to = args.ParseInt(1);
356 357
	client.partition.MoveId(id, to);
	return CommandResult::OK;
358 359
}

360
CommandResult
361
handle_swap(Client &client, Request args, gcc_unused Response &r)
362
{
363 364
	unsigned song1 = args.ParseUnsigned(0);
	unsigned song2 = args.ParseUnsigned(1);
365 366
	client.partition.SwapPositions(song1, song2);
	return CommandResult::OK;
367 368
}

369
CommandResult
370
handle_swapid(Client &client, Request args, gcc_unused Response &r)
371
{
372 373
	unsigned id1 = args.ParseUnsigned(0);
	unsigned id2 = args.ParseUnsigned(1);
374 375
	client.partition.SwapIds(id1, id2);
	return CommandResult::OK;
376
}