QueueCommands.cxx 9.22 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "CommandError.hxx"
Max Kellermann's avatar
Max Kellermann committed
23 24
#include "db/DatabaseQueue.hxx"
#include "db/Selection.hxx"
25 26
#include "SongFilter.hxx"
#include "SongLoader.hxx"
27
#include "Playlist.hxx"
28
#include "PlaylistPrint.hxx"
29
#include "client/Client.hxx"
30
#include "Partition.hxx"
Max Kellermann's avatar
Max Kellermann committed
31 32
#include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "ls.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "util/UriUtil.hxx"
35
#include "util/Error.hxx"
36
#include "fs/AllocatedPath.hxx"
37

38
#include <limits>
39

40 41
#include <string.h>

42 43
static const char *
translate_uri(Client &client, const char *uri)
44
{
45 46 47 48 49 50 51 52
	if (memcmp(uri, "file:///", 8) == 0)
		/* drop the "file://", leave only an absolute path
		   (starting with a slash) */
		return uri + 7;

	if (PathTraitsUTF8::IsAbsolute(uri)) {
		command_error(client, ACK_ERROR_NO_EXIST, "Malformed URI");
		return nullptr;
53 54
	}

55 56
	return uri;
}
57

58 59 60 61 62 63 64 65 66 67
CommandResult
handle_add(Client &client, gcc_unused int argc, char *argv[])
{
	const char *const uri = translate_uri(client, argv[1]);
	if (uri == nullptr)
		return CommandResult::ERROR;

	if (uri_has_scheme(uri) || PathTraitsUTF8::IsAbsolute(uri)) {
		const SongLoader loader(client);
		auto result = client.partition.AppendURI(loader, uri);
68 69 70
		return print_playlist_result(client, result);
	}

71
#ifdef ENABLE_DATABASE
72
	const DatabaseSelection selection(uri, true);
73
	Error error;
74
	return AddFromDatabase(client.partition, selection, error)
75
		? CommandResult::OK
76
		: print_error(client, error);
77 78 79 80
#else
	command_error(client, ACK_ERROR_NO_EXIST, "No database");
	return CommandResult::ERROR;
#endif
81 82
}

83
CommandResult
84
handle_addid(Client &client, int argc, char *argv[])
85
{
86 87 88
	const char *const uri = translate_uri(client, argv[1]);
	if (uri == nullptr)
		return CommandResult::ERROR;
89

90
	const SongLoader loader(client);
91

92 93
	unsigned added_id;
	auto result = client.partition.AppendURI(loader, uri, &added_id);
94

95
	if (result != PlaylistResult::SUCCESS)
96 97 98 99 100
		return print_playlist_result(client, result);

	if (argc == 3) {
		unsigned to;
		if (!check_unsigned(client, &to, argv[2]))
101
			return CommandResult::ERROR;
102
		result = client.partition.MoveId(added_id, to);
103
		if (result != PlaylistResult::SUCCESS) {
104
			CommandResult ret =
105
				print_playlist_result(client, result);
106
			client.partition.DeleteId(added_id);
107 108 109 110 111
			return ret;
		}
	}

	client_printf(client, "Id: %u\n", added_id);
112
	return CommandResult::OK;
113 114
}

115
CommandResult
116
handle_delete(Client &client, gcc_unused int argc, char *argv[])
117 118 119 120
{
	unsigned start, end;

	if (!check_range(client, &start, &end, argv[1]))
121
		return CommandResult::ERROR;
122

123
	PlaylistResult result = client.partition.DeleteRange(start, end);
124 125 126
	return print_playlist_result(client, result);
}

127
CommandResult
128
handle_deleteid(Client &client, gcc_unused int argc, char *argv[])
129 130 131 132
{
	unsigned id;

	if (!check_unsigned(client, &id, argv[1]))
133
		return CommandResult::ERROR;
134

135
	PlaylistResult result = client.partition.DeleteId(id);
136 137 138
	return print_playlist_result(client, result);
}

139
CommandResult
140
handle_playlist(Client &client,
141
		gcc_unused int argc, gcc_unused char *argv[])
142
{
143
	playlist_print_uris(client, client.playlist);
144
	return CommandResult::OK;
145 146
}

147
CommandResult
148
handle_shuffle(gcc_unused Client &client,
149
	       gcc_unused int argc, gcc_unused char *argv[])
150
{
151
	unsigned start = 0, end = client.playlist.queue.GetLength();
152
	if (argc == 2 && !check_range(client, &start, &end, argv[1]))
153
		return CommandResult::ERROR;
154

155
	client.partition.Shuffle(start, end);
156
	return CommandResult::OK;
157 158
}

159
CommandResult
160
handle_clear(gcc_unused Client &client,
161
	     gcc_unused int argc, gcc_unused char *argv[])
162
{
163
	client.partition.ClearQueue();
164
	return CommandResult::OK;
165 166
}

167
CommandResult
168
handle_plchanges(Client &client, gcc_unused int argc, char *argv[])
169 170 171 172
{
	uint32_t version;

	if (!check_uint32(client, &version, argv[1]))
173
		return CommandResult::ERROR;
174

175
	playlist_print_changes_info(client, client.playlist, version);
176
	return CommandResult::OK;
177 178
}

179
CommandResult
180
handle_plchangesposid(Client &client, gcc_unused int argc, char *argv[])
181 182 183 184
{
	uint32_t version;

	if (!check_uint32(client, &version, argv[1]))
185
		return CommandResult::ERROR;
186

187
	playlist_print_changes_position(client, client.playlist, version);
188
	return CommandResult::OK;
189 190
}

191
CommandResult
192
handle_playlistinfo(Client &client, int argc, char *argv[])
193
{
194
	unsigned start = 0, end = std::numeric_limits<unsigned>::max();
195 196 197
	bool ret;

	if (argc == 2 && !check_range(client, &start, &end, argv[1]))
198
		return CommandResult::ERROR;
199

200
	ret = playlist_print_info(client, client.playlist, start, end);
201 202
	if (!ret)
		return print_playlist_result(client,
203
					     PlaylistResult::BAD_RANGE);
204

205
	return CommandResult::OK;
206 207
}

208
CommandResult
209
handle_playlistid(Client &client, int argc, char *argv[])
210 211 212 213
{
	if (argc >= 2) {
		unsigned id;
		if (!check_unsigned(client, &id, argv[1]))
214
			return CommandResult::ERROR;
215

216
		bool ret = playlist_print_id(client, client.playlist, id);
217 218
		if (!ret)
			return print_playlist_result(client,
219
						     PlaylistResult::NO_SUCH_SONG);
220
	} else {
221
		playlist_print_info(client, client.playlist,
222
				    0, std::numeric_limits<unsigned>::max());
223 224
	}

225
	return CommandResult::OK;
226 227
}

228
static CommandResult
229
handle_playlist_match(Client &client, int argc, char *argv[],
230 231
		      bool fold_case)
{
232 233
	SongFilter filter;
	if (!filter.Parse(argc - 1, argv + 1, fold_case)) {
234
		command_error(client, ACK_ERROR_ARG, "incorrect arguments");
235
		return CommandResult::ERROR;
236 237
	}

238
	playlist_print_find(client, client.playlist, filter);
239
	return CommandResult::OK;
240 241
}

242
CommandResult
243
handle_playlistfind(Client &client, int argc, char *argv[])
244 245 246 247
{
	return handle_playlist_match(client, argc, argv, false);
}

248
CommandResult
249
handle_playlistsearch(Client &client, int argc, char *argv[])
250 251 252 253
{
	return handle_playlist_match(client, argc, argv, true);
}

254
CommandResult
255
handle_prio(Client &client, int argc, char *argv[])
256 257 258 259
{
	unsigned priority;

	if (!check_unsigned(client, &priority, argv[1]))
260
		return CommandResult::ERROR;
261 262 263 264

	if (priority > 0xff) {
		command_error(client, ACK_ERROR_ARG,
			      "Priority out of range: %s", argv[1]);
265
		return CommandResult::ERROR;
266 267 268 269 270 271
	}

	for (int i = 2; i < argc; ++i) {
		unsigned start_position, end_position;
		if (!check_range(client, &start_position, &end_position,
				 argv[i]))
272
			return CommandResult::ERROR;
273

274
		PlaylistResult result =
275
			client.partition.SetPriorityRange(start_position,
276 277
							   end_position,
							   priority);
278
		if (result != PlaylistResult::SUCCESS)
279 280 281
			return print_playlist_result(client, result);
	}

282
	return CommandResult::OK;
283 284
}

285
CommandResult
286
handle_prioid(Client &client, int argc, char *argv[])
287 288 289 290
{
	unsigned priority;

	if (!check_unsigned(client, &priority, argv[1]))
291
		return CommandResult::ERROR;
292 293 294 295

	if (priority > 0xff) {
		command_error(client, ACK_ERROR_ARG,
			      "Priority out of range: %s", argv[1]);
296
		return CommandResult::ERROR;
297 298 299 300 301
	}

	for (int i = 2; i < argc; ++i) {
		unsigned song_id;
		if (!check_unsigned(client, &song_id, argv[i]))
302
			return CommandResult::ERROR;
303

304
		PlaylistResult result =
305
			client.partition.SetPriorityId(song_id, priority);
306
		if (result != PlaylistResult::SUCCESS)
307 308 309
			return print_playlist_result(client, result);
	}

310
	return CommandResult::OK;
311 312
}

313
CommandResult
314
handle_move(Client &client, gcc_unused int argc, char *argv[])
315 316 317 318 319
{
	unsigned start, end;
	int to;

	if (!check_range(client, &start, &end, argv[1]))
320
		return CommandResult::ERROR;
321
	if (!check_int(client, &to, argv[2]))
322
		return CommandResult::ERROR;
323

324
	PlaylistResult result =
325
		client.partition.MoveRange(start, end, to);
326 327 328
	return print_playlist_result(client, result);
}

329
CommandResult
330
handle_moveid(Client &client, gcc_unused int argc, char *argv[])
331 332 333 334 335
{
	unsigned id;
	int to;

	if (!check_unsigned(client, &id, argv[1]))
336
		return CommandResult::ERROR;
337
	if (!check_int(client, &to, argv[2]))
338
		return CommandResult::ERROR;
339
	PlaylistResult result = client.partition.MoveId(id, to);
340 341 342
	return print_playlist_result(client, result);
}

343
CommandResult
344
handle_swap(Client &client, gcc_unused int argc, char *argv[])
345 346 347 348
{
	unsigned song1, song2;

	if (!check_unsigned(client, &song1, argv[1]))
349
		return CommandResult::ERROR;
350
	if (!check_unsigned(client, &song2, argv[2]))
351
		return CommandResult::ERROR;
352

353
	PlaylistResult result =
354
		client.partition.SwapPositions(song1, song2);
355 356 357
	return print_playlist_result(client, result);
}

358
CommandResult
359
handle_swapid(Client &client, gcc_unused int argc, char *argv[])
360 361 362 363
{
	unsigned id1, id2;

	if (!check_unsigned(client, &id1, argv[1]))
364
		return CommandResult::ERROR;
365
	if (!check_unsigned(client, &id2, argv[2]))
366
		return CommandResult::ERROR;
367

368
	PlaylistResult result = client.partition.SwapIds(id1, id2);
369 370
	return print_playlist_result(client, result);
}