PlaylistEdit.cxx 10.4 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
			start = current + 1;
		if (start < queue.GetLength())
115
			queue.ShuffleOrderLastWithPriority(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
	std::unique_ptr<DetachedSong> song(loader.LoadSong(uri));
129
	return AppendSong(pc, std::move(*song));
Max Kellermann's avatar
Max Kellermann committed
130 131
}

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

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

140
	queue.SwapPositions(song1, song2);
141

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

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

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

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

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

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

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

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

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

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

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

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

	/* apply the priority changes */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	/* now do it: remove the song */

256
	queue.DeletePosition(song);
257 258 259

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

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

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

270
	const DetachedSong *queued_song = GetQueuedSong();
271

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

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

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

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

	if (start >= end)
288
		return;
289

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

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

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

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

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

void
311
playlist::StaleSong(PlayerControl &pc, const char *uri)
312
{
313 314 315 316 317 318 319 320
	/* 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;

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

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

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

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

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

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

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

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

363
	if (!queue.random) {
364
		/* update current/queued */
365 366 367 368 369 370
		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;
371 372
	}

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

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

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

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

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

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

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

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

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

417
		current = -1;
418 419
	}

420
	queue.ShuffleRange(start, end);
421

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

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

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

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

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

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

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

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

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

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

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