QueueCommands.cxx 8.58 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 47
	std::unique_ptr<DetachedSong> song(SongLoader(client).LoadSong(uri));
	assert(song);
48

49
	auto &partition = client.partition;
50
	partition.playlist.AppendSong(partition.pc, std::move(*song));
51 52 53
}

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

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

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

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

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

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

100
	gcc_unreachable();
101 102
}

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

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

111
	if (args.size == 2) {
112
		unsigned to = args.ParseUnsigned(1);
113

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

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

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

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

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

	p = endptr + 1;

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

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

155
	return end_r.IsZero() || end_r > start_r;
156 157 158
}

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

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

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

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

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

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

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

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

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

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

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

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

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

255
	return CommandResult::OK;
256 257
}

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

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

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

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

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

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

296
	return CommandResult::OK;
297 298
}

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

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

310
	return CommandResult::OK;
311 312
}

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

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

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

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