QueueCommands.cxx 8.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "protocol/RangeArg.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "db/DatabaseQueue.hxx"
#include "db/Selection.hxx"
26
#include "song/Filter.hxx"
27
#include "SongLoader.hxx"
28
#include "song/DetachedSong.hxx"
29
#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 "Instance.hxx"
36
#include "BulkEdit.hxx"
37
#include "util/ConstBuffer.hxx"
38
#include "util/Exception.hxx"
39
#include "util/StringAPI.hxx"
40
#include "util/NumberParser.hxx"
41

42
#include <limits>
43

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

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

	const DatabaseSelection selection(uri, true);
61
	AddFromDatabase(partition, selection);
62
	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 85
	const auto located_uri = LocateUri(UriPluginKind::INPUT, uri,
					   &client
86
#ifdef ENABLE_DATABASE
87
					   , nullptr
88
#endif
89
					   );
90 91
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
92 93 94 95
		AddUri(client, located_uri);
		client.GetInstance().LookupRemoteTag(located_uri.canonical_uri);
		return CommandResult::OK;

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

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

105
	gcc_unreachable();
106 107
}

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

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

118
	if (args.size == 2) {
119
		unsigned to = args.ParseUnsigned(1);
120

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

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

134 135 136 137 138 139
/**
 * 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
140
parse_time_range(const char *p, SongTime &start_r, SongTime &end_r)
141 142 143 144 145 146 147
{
	char *endptr;

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

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

	p = endptr + 1;

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

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

162
	return end_r.IsZero() || end_r > start_r;
163 164 165
}

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

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

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

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

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

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

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

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

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

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

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

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

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

260
	return CommandResult::OK;
261 262
}

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

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

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

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

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

299 300
	auto &partition = client.GetPartition();

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

306
	return CommandResult::OK;
307 308
}

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

315 316
	auto &partition = client.GetPartition();

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

322
	return CommandResult::OK;
323 324
}

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

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

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

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