QueueCommands.cxx 8.92 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"
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
	const auto located_uri = LocateUri(uri, &client
87
#ifdef ENABLE_DATABASE
88
					   , nullptr
89
#endif
90
					   );
91 92
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
93 94 95 96
		AddUri(client, located_uri);
		client.GetInstance().LookupRemoteTag(located_uri.canonical_uri);
		return CommandResult::OK;

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

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

106
	gcc_unreachable();
107 108
}

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

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

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

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

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

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

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

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

	p = endptr + 1;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

261
	return CommandResult::OK;
262 263
}

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

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

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

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

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

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

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

307
	return CommandResult::OK;
308 309
}

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

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

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

323
	return CommandResult::OK;
324 325
}

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

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

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

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