PlaylistEdit.cxx 10.3 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Playlist.hxx"
27
#include "Listener.hxx"
28
#include "PlaylistError.hxx"
29
#include "player/Control.hxx"
30
#include "song/DetachedSong.hxx"
31
#include "SongLoader.hxx"
32

33 34
#include <memory>

35 36
#include <stdlib.h>

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

46
	queue.IncrementVersion();
47

48
	listener.OnQueueModified();
49 50
}

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

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

59
	OnModified();
60 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
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();
}

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

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

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

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

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

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

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

120
	return id;
121 122
}

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

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

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

138
	queue.SwapPositions(song1, song2);
139

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

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

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

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

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

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

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

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

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

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

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

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

	/* apply the priority changes */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	/* now do it: remove the song */

254
	queue.DeletePosition(song);
255 256 257

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

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

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

268
	const DetachedSong *queued_song = GetQueuedSong();
269

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

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

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

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

	if (start >= end)
286
		return;
287

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

415
		current = -1;
416 417
	}

418
	queue.ShuffleRange(start, end);
419

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

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

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

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

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

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

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

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

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

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

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