PlaylistEdit.cxx 10.3 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
 * 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.
14 15 16 17
 *
 * 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.
18 19 20 21 22 23 24 25
 */

/*
 * Functions for editing the playlist (adding, removing, reordering
 * songs in the queue).
 *
 */

26
#include "config.h"
27
#include "Playlist.hxx"
28
#include "Listener.hxx"
29
#include "PlaylistError.hxx"
30
#include "player/Control.hxx"
31
#include "DetachedSong.hxx"
32
#include "SongLoader.hxx"
33

34 35
#include <memory>

36 37
#include <stdlib.h>

38 39
void
playlist::OnModified()
40
{
41 42 43 44 45 46
	if (bulk_edit) {
		/* postponed to CommitBulk() */
		bulk_modified = true;
		return;
	}

47
	queue.IncrementVersion();
48

49
	listener.OnQueueModified();
50 51
}

52
void
53
playlist::Clear(PlayerControl &pc)
54
{
55
	Stop(pc);
56

57 58
	queue.Clear();
	current = -1;
59

60
	OnModified();
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
void
playlist::BeginBulk()
{
	assert(!bulk_edit);

	bulk_edit = true;
	bulk_modified = false;
}

void
playlist::CommitBulk(PlayerControl &pc)
{
	assert(bulk_edit);

	bulk_edit = false;
	if (!bulk_modified)
		return;

	if (queued < 0)
		/* if no song was queued, UpdateQueuedSong() is being
		   ignored in "bulk" edit mode; now that we have
		   shuffled all new songs, we can pick a random one
		   (instead of always picking the first one that was
		   added) */
		UpdateQueuedSong(pc, nullptr);

	OnModified();
}

92
unsigned
93
playlist::AppendSong(PlayerControl &pc, DetachedSong &&song)
94 95 96
{
	unsigned id;

97 98 99
	if (queue.IsFull())
		throw PlaylistError(PlaylistResult::TOO_LARGE,
				    "Playlist is too large");
100

101
	const DetachedSong *const queued_song = GetQueuedSong();
102

103
	id = queue.Append(std::move(song), 0);
104

105
	if (queue.random) {
106
		/* shuffle the new song into the list of remaining
107 108 109
		   songs to play */

		unsigned start;
110 111
		if (queued >= 0)
			start = queued + 1;
112
		else
113 114 115
			start = current + 1;
		if (start < queue.GetLength())
			queue.ShuffleOrderLast(start, queue.GetLength());
116 117
	}

118 119
	UpdateQueuedSong(pc, queued_song);
	OnModified();
120

121
	return id;
122 123
}

124 125
unsigned
playlist::AppendURI(PlayerControl &pc, const SongLoader &loader,
126
		    const char *uri)
Max Kellermann's avatar
Max Kellermann committed
127
{
128
	return AppendSong(pc, loader.LoadSong(uri));
Max Kellermann's avatar
Max Kellermann committed
129 130
}

131
void
132
playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2)
133
{
134
	if (!queue.IsValidPosition(song1) || !queue.IsValidPosition(song2))
135
		throw PlaylistError::BadRange();
136

137
	const DetachedSong *const queued_song = GetQueuedSong();
138

139
	queue.SwapPositions(song1, song2);
140

141 142
	if (queue.random) {
		/* update the queue order, so that current
143 144
		   still points to the current song order */

145 146
		queue.SwapOrders(queue.PositionToOrder(song1),
				 queue.PositionToOrder(song2));
147 148 149
	} else {
		/* correct the "current" song order */

150 151 152 153
		if (current == (int)song1)
			current = song2;
		else if (current == (int)song2)
			current = song1;
154 155
	}

156 157
	UpdateQueuedSong(pc, queued_song);
	OnModified();
158 159
}

160
void
161
playlist::SwapIds(PlayerControl &pc, unsigned id1, unsigned id2)
162
{
163 164
	int song1 = queue.IdToPosition(id1);
	int song2 = queue.IdToPosition(id2);
165 166

	if (song1 < 0 || song2 < 0)
167
		throw PlaylistError::NoSuchSong();
168

169
	SwapPositions(pc, song1, song2);
170 171
}

172
void
173
playlist::SetPriorityRange(PlayerControl &pc,
174 175
			   unsigned start, unsigned end,
			   uint8_t priority)
176
{
177
	if (start >= GetLength())
178
		throw PlaylistError::BadRange();
179

180 181
	if (end > GetLength())
		end = GetLength();
182 183

	if (start >= end)
184
		return;
185 186 187

	/* remember "current" and "queued" */

188
	const int current_position = GetCurrentPosition();
189
	const DetachedSong *const queued_song = GetQueuedSong();
190 191 192

	/* apply the priority changes */

193
	queue.SetPriorityRange(start, end, priority, current);
194 195 196 197

	/* restore "current" and choose a new "queued" */

	if (current_position >= 0)
198
		current = queue.PositionToOrder(current_position);
199

200 201
	UpdateQueuedSong(pc, queued_song);
	OnModified();
202 203
}

204
void
205
playlist::SetPriorityId(PlayerControl &pc,
206
			unsigned song_id, uint8_t priority)
207
{
208
	int song_position = queue.IdToPosition(song_id);
209
	if (song_position < 0)
210
		throw PlaylistError::NoSuchSong();
211

212
	SetPriorityRange(pc, song_position, song_position + 1, priority);
213 214
}

215
void
216
playlist::DeleteInternal(PlayerControl &pc,
217
			 unsigned song, const DetachedSong **queued_p)
218
{
219
	assert(song < GetLength());
220

221
	unsigned songOrder = queue.PositionToOrder(song);
222

223
	if (playing && current == (int)songOrder) {
224
		const bool paused = pc.GetState() == PlayerState::PAUSE;
225

226 227
		/* the current song is going to be deleted: see which
		   song is going to be played instead */
228

229 230 231
		current = queue.GetNextOrder(current);
		if (current == (int)songOrder)
			current = -1;
232

233
		if (current >= 0 && !paused)
234
			/* play the song after the deleted one */
235 236 237 238 239
			try {
				PlayOrder(pc, current);
			} catch (...) {
				/* TODO: log error? */
			}
240 241 242
		else {
			/* stop the player */

243
			pc.LockStop();
244 245
			playing = false;
		}
246

247
		*queued_p = nullptr;
248
	} else if (current == (int)songOrder)
249 250
		/* there's a "current song" but we're not playing
		   currently - clear "current" */
251
		current = -1;
252 253 254

	/* now do it: remove the song */

255
	queue.DeletePosition(song);
256 257 258

	/* update the "current" and "queued" variables */

259 260
	if (current > (int)songOrder)
		current--;
261 262
}

263
void
264
playlist::DeletePosition(PlayerControl &pc, unsigned song)
265
{
266
	if (song >= queue.GetLength())
267
		throw PlaylistError::BadRange();
268

269
	const DetachedSong *queued_song = GetQueuedSong();
270

271
	DeleteInternal(pc, song, &queued_song);
272

273 274
	UpdateQueuedSong(pc, queued_song);
	OnModified();
275 276
}

277
void
278
playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
279
{
280
	if (start >= queue.GetLength())
281
		throw PlaylistError::BadRange();
282

283 284
	if (end > queue.GetLength())
		end = queue.GetLength();
285 286

	if (start >= end)
287
		return;
288

289
	const DetachedSong *queued_song = GetQueuedSong();
290 291

	do {
292
		DeleteInternal(pc, --end, &queued_song);
293 294
	} while (end != start);

295 296
	UpdateQueuedSong(pc, queued_song);
	OnModified();
297 298
}

299
void
300
playlist::DeleteId(PlayerControl &pc, unsigned id)
301
{
302
	int song = queue.IdToPosition(id);
303
	if (song < 0)
304
		throw PlaylistError::NoSuchSong();
305

306
	DeletePosition(pc, song);
307 308 309
}

void
310
playlist::StaleSong(PlayerControl &pc, const char *uri)
311
{
312 313 314 315 316 317 318 319
	/* don't remove the song if it's currently being played, to
	   avoid disrupting playback; a deleted file may still be
	   played if it's still open */
	// TODO: mark the song as "stale" and postpone deletion
	int current_position = playing
		? GetCurrentPosition()
		: -1;

320
	for (int i = queue.GetLength() - 1; i >= 0; --i)
321
		if (i != current_position && queue.Get(i).IsURI(uri))
322
			DeletePosition(pc, i);
323 324
}

325
void
326
playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
327
{
328
	if (!queue.IsValidPosition(start) || !queue.IsValidPosition(end - 1))
329
		throw PlaylistError::BadRange();
330

331 332
	if ((to >= 0 && to + end - start - 1 >= GetLength()) ||
	    (to < 0 && unsigned(abs(to)) > GetLength()))
333
		throw PlaylistError::BadRange();
334

335 336
	if ((int)start == to)
		/* nothing happens */
337
		return;
338

339
	const DetachedSong *const queued_song = GetQueuedSong();
340 341 342 343 344

	/*
	 * (to < 0) => move to offset from current song
	 * (-playlist.length == to) => move to position BEFORE current song
	 */
345
	const int currentSong = GetCurrentPosition();
346 347 348 349
	if (to < 0) {
		if (currentSong < 0)
			/* can't move relative to current song,
			   because there is no current song */
350
			throw PlaylistError::BadRange();
351

352
		if (start <= (unsigned)currentSong && (unsigned)currentSong < end)
353
			/* no-op, can't be moved to offset of itself */
354
			return;
355
		to = (currentSong + abs(to)) % GetLength();
356 357
		if (start < (unsigned)to)
			to--;
358 359
	}

360
	queue.MoveRange(start, end, to);
361

362
	if (!queue.random) {
363
		/* update current/queued */
364 365 366 367 368 369
		if ((int)start <= current && (unsigned)current < end)
			current += to - start;
		else if (current >= (int)end && current <= to)
			current -= end - start;
		else if (current >= to && current < (int)start)
			current += end - start;
370 371
	}

372 373
	UpdateQueuedSong(pc, queued_song);
	OnModified();
374 375
}

376
void
377
playlist::MoveId(PlayerControl &pc, unsigned id1, int to)
378
{
379
	int song = queue.IdToPosition(id1);
380
	if (song < 0)
381
		throw PlaylistError::NoSuchSong();
382

383
	MoveRange(pc, song, song + 1, to);
384 385
}

Max Kellermann's avatar
Max Kellermann committed
386
void
387
playlist::Shuffle(PlayerControl &pc, unsigned start, unsigned end)
388
{
389
	if (end > GetLength())
390
		/* correct the "end" offset */
391
		end = GetLength();
392

393
	if (start + 1 >= end)
394
		/* needs at least two entries. */
395 396
		return;

397
	const DetachedSong *const queued_song = GetQueuedSong();
398 399
	if (playing && current >= 0) {
		unsigned current_position = queue.OrderToPosition(current);
400

401
		if (current_position >= start && current_position < end) {
402
			/* put current playing song first */
403
			queue.SwapPositions(start, current_position);
404

405 406
			if (queue.random) {
				current = queue.PositionToOrder(start);
407
			} else
408
				current = start;
409 410 411 412

			/* start shuffle after the current song */
			start++;
		}
413
	} else {
414
		/* no playback currently: reset current */
415

416
		current = -1;
417 418
	}

419
	queue.ShuffleRange(start, end);
420

421 422
	UpdateQueuedSong(pc, queued_song);
	OnModified();
423
}
424

425
void
426
playlist::SetSongIdRange(PlayerControl &pc, unsigned id,
427
			 SongTime start, SongTime end)
428
{
429
	assert(end.IsZero() || start < end);
430 431

	int position = queue.IdToPosition(id);
432 433
	if (position < 0)
		throw PlaylistError::NoSuchSong();
434 435

	if (playing) {
436 437 438
		if (position == current)
			throw PlaylistError(PlaylistResult::DENIED,
					    "Cannot edit the current song");
439 440 441 442 443

		if (position == queued) {
			/* if we're manipulating the "queued" song,
			   the decoder thread may be decoding it
			   already; cancel that */
444
			pc.LockCancel();
445 446 447 448 449
			queued = -1;
		}
	}

	DetachedSong &song = queue.Get(position);
450 451 452

	const auto duration = song.GetTag().duration;
	if (!duration.IsNegative()) {
453 454
		/* validate the offsets */

455 456 457
		if (start > duration)
			throw PlaylistError(PlaylistResult::BAD_RANGE,
					    "Invalid start offset");
458

459
		if (end >= duration)
460
			end = SongTime::zero();
461 462 463
	}

	/* edit it */
464 465
	song.SetStartTime(start);
	song.SetEndTime(end);
466 467 468 469 470 471

	/* announce the change to all interested subsystems */
	UpdateQueuedSong(pc, nullptr);
	queue.ModifyAtPosition(position);
	OnModified();
}