QueueCommands.cxx 8.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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"
38
#include "util/NumberParser.hxx"
39

40
#include <memory>
41
#include <limits>
42

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

static CommandResult
52 53
AddDatabaseSelection(Client &client, const char *uri,
		     gcc_unused Response &r)
54 55 56 57 58
{
#ifdef ENABLE_DATABASE
	const ScopeBulkEdit bulk_edit(client.partition);

	const DatabaseSelection selection(uri, true);
59 60
	AddFromDatabase(client.partition, selection);
	return CommandResult::OK;
61 62 63 64 65 66 67
#else
	(void)client;
	(void)uri;

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

70
CommandResult
71
handle_add(Client &client, Request args, Response &r)
72
{
73
	const char *uri = args.front();
74
	if (StringIsEqual(uri, "/"))
75 76 77 78 79 80 81
		/* 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 = "";

82
	const auto located_uri = LocateUri(uri, &client
83
#ifdef ENABLE_DATABASE
84
					   , nullptr
85
#endif
86
					   );
87 88 89
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
	case LocatedUri::Type::PATH:
90 91
		AddUri(client, located_uri);
		return CommandResult::OK;
92

93 94 95
	case LocatedUri::Type::RELATIVE:
		return AddDatabaseSelection(client, located_uri.canonical_uri,
					    r);
96 97
	}

98
	gcc_unreachable();
99 100
}

101
CommandResult
102
handle_addid(Client &client, Request args, Response &r)
103
{
104
	const char *const uri = args.front();
105

106
	const SongLoader loader(client);
107
	unsigned added_id = client.partition.AppendURI(loader, uri);
108

109
	if (args.size == 2) {
110
		unsigned to = args.ParseUnsigned(1);
111

112 113 114 115
		try {
			client.partition.MoveId(added_id, to);
		} catch (...) {
			/* rollback */
116
			client.partition.DeleteId(added_id);
117
			throw;
118 119 120
		}
	}

121
	r.Format("Id: %u\n", added_id);
122
	return CommandResult::OK;
123 124
}

125 126 127 128 129 130
/**
 * 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
131
parse_time_range(const char *p, SongTime &start_r, SongTime &end_r)
132 133 134 135 136 137 138
{
	char *endptr;

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

139 140 141
	start_r = endptr > p
		? SongTime::FromS(start)
		: SongTime::zero();
142 143 144 145 146 147 148

	p = endptr + 1;

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

149 150 151
	end_r = endptr > p
		? SongTime::FromS(end)
		: SongTime::zero();
152

153
	return end_r.IsZero() || end_r > start_r;
154 155 156
}

CommandResult
157
handle_rangeid(Client &client, Request args, Response &r)
158
{
159
	unsigned id = args.ParseUnsigned(0);
160

161
	SongTime start, end;
162
	if (!parse_time_range(args[1], start, end)) {
163
		r.Error(ACK_ERROR_ARG, "Bad range");
164 165 166
		return CommandResult::ERROR;
	}

167 168
	client.partition.playlist.SetSongIdRange(client.partition.pc,
						 id, start, end);
169 170 171
	return CommandResult::OK;
}

172
CommandResult
173
handle_delete(Client &client, Request args, gcc_unused Response &r)
174
{
175
	RangeArg range = args.ParseRange(0);
176 177
	client.partition.DeleteRange(range.start, range.end);
	return CommandResult::OK;
178 179
}

180
CommandResult
181
handle_deleteid(Client &client, Request args, gcc_unused Response &r)
182
{
183
	unsigned id = args.ParseUnsigned(0);
184 185
	client.partition.DeleteId(id);
	return CommandResult::OK;
186 187
}

188
CommandResult
189
handle_playlist(Client &client, gcc_unused Request args, Response &r)
190
{
191
	playlist_print_uris(r, client.partition, client.playlist);
192
	return CommandResult::OK;
193 194
}

195
CommandResult
196
handle_shuffle(gcc_unused Client &client, Request args, gcc_unused Response &r)
197
{
198
	RangeArg range = args.ParseOptional(0, RangeArg::All());
199
	client.partition.Shuffle(range.start, range.end);
200
	return CommandResult::OK;
201 202
}

203
CommandResult
204
handle_clear(Client &client, gcc_unused Request args, gcc_unused Response &r)
205
{
206
	client.partition.ClearQueue();
207
	return CommandResult::OK;
208 209
}

210
CommandResult
211
handle_plchanges(Client &client, Request args, Response &r)
212
{
213 214
	uint32_t version = ParseCommandArgU32(args.front());
	RangeArg range = args.ParseOptional(1, RangeArg::All());
215
	playlist_print_changes_info(r, client.partition,
216 217
				    client.playlist, version,
				    range.start, range.end);
218
	return CommandResult::OK;
219 220
}

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

231
CommandResult
232
handle_playlistinfo(Client &client, Request args, Response &r)
233
{
234
	RangeArg range = args.ParseOptional(0, RangeArg::All());
235

236 237
	playlist_print_info(r, client.partition, client.playlist,
			    range.start, range.end);
238
	return CommandResult::OK;
239 240
}

241
CommandResult
242
handle_playlistid(Client &client, Request args, Response &r)
243
{
244
	if (!args.IsEmpty()) {
245
		unsigned id = args.ParseUnsigned(0);
246 247
		playlist_print_id(r, client.partition,
				  client.playlist, id);
248
	} else {
249
		playlist_print_info(r, client.partition, client.playlist,
250
				    0, std::numeric_limits<unsigned>::max());
251 252
	}

253
	return CommandResult::OK;
254 255
}

256
static CommandResult
257
handle_playlist_match(Client &client, Request args, Response &r,
258 259
		      bool fold_case)
{
260
	SongFilter filter;
261
	if (!filter.Parse(args, fold_case)) {
262
		r.Error(ACK_ERROR_ARG, "incorrect arguments");
263
		return CommandResult::ERROR;
264 265
	}

266
	playlist_print_find(r, client.partition, client.playlist, filter);
267
	return CommandResult::OK;
268 269
}

270
CommandResult
271
handle_playlistfind(Client &client, Request args, Response &r)
272
{
273
	return handle_playlist_match(client, args, r, false);
274 275
}

276
CommandResult
277
handle_playlistsearch(Client &client, Request args, Response &r)
278
{
279
	return handle_playlist_match(client, args, r, true);
280 281
}

282
CommandResult
283
handle_prio(Client &client, Request args, gcc_unused Response &r)
284
{
285 286
	unsigned priority = args.ParseUnsigned(0, 0xff);
	args.shift();
287

288
	for (const char *i : args) {
289
		RangeArg range = ParseCommandArgRange(i);
290 291
		client.partition.SetPriorityRange(range.start, range.end,
						  priority);
292 293
	}

294
	return CommandResult::OK;
295 296
}

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

303
	for (const char *i : args) {
304
		unsigned song_id = ParseCommandArgUnsigned(i);
305
		client.partition.SetPriorityId(song_id, priority);
306 307
	}

308
	return CommandResult::OK;
309 310
}

311
CommandResult
312
handle_move(Client &client, Request args, gcc_unused Response &r)
313
{
314 315
	RangeArg range = args.ParseRange(0);
	int to = args.ParseInt(1);
316 317
	client.partition.MoveRange(range.start, range.end, to);
	return CommandResult::OK;
318 319
}

320
CommandResult
321
handle_moveid(Client &client, Request args, gcc_unused Response &r)
322
{
323 324
	unsigned id = args.ParseUnsigned(0);
	int to = args.ParseInt(1);
325 326
	client.partition.MoveId(id, to);
	return CommandResult::OK;
327 328
}

329
CommandResult
330
handle_swap(Client &client, Request args, gcc_unused Response &r)
331
{
332 333
	unsigned song1 = args.ParseUnsigned(0);
	unsigned song2 = args.ParseUnsigned(1);
334 335
	client.partition.SwapPositions(song1, song2);
	return CommandResult::OK;
336 337
}

338
CommandResult
339
handle_swapid(Client &client, Request args, gcc_unused Response &r)
340
{
341 342
	unsigned id1 = args.ParseUnsigned(0);
	unsigned id2 = args.ParseUnsigned(1);
343 344
	client.partition.SwapIds(id1, id2);
	return CommandResult::OK;
345
}