PlaylistEdit: postpone UpdateQueuedSong() when adding multiple songs

Implement a "bulk" edit mode that postpones both UpdateQueuedSong()
and OnModified().  This way, the playlist version gets incremented
only once.  More importantly: when adding multiple songs to a queue
that consists of only one song, the first song that got added will
always be played next.  By postponing this choice, all newly added
songs get a chance to become the next song.  Fixes the second (and
last) part of Mantis ticket 0004005.
This commit is contained in:
Max Kellermann 2014-07-11 20:01:53 +02:00
parent a8a85143f6
commit 11a5ee821b
9 changed files with 111 additions and 1 deletions

View File

@ -181,6 +181,7 @@ src_mpd_SOURCES = \
src/PlaylistInfo.hxx \ src/PlaylistInfo.hxx \
src/PlaylistDatabase.cxx src/PlaylistDatabase.hxx \ src/PlaylistDatabase.cxx src/PlaylistDatabase.hxx \
src/PlaylistUpdate.cxx \ src/PlaylistUpdate.cxx \
src/BulkEdit.hxx \
src/IdTable.hxx \ src/IdTable.hxx \
src/Queue.cxx src/Queue.hxx \ src/Queue.cxx src/Queue.hxx \
src/QueuePrint.cxx src/QueuePrint.hxx \ src/QueuePrint.cxx src/QueuePrint.hxx \

1
NEWS
View File

@ -7,6 +7,7 @@ ver 0.18.12 (not yet released)
- audiofile: fix WAV stream playback - audiofile: fix WAV stream playback
- dsdiff, dsf: fix stream playback - dsdiff, dsf: fix stream playback
* randomize next song when enabling "random" mode while not playing * randomize next song when enabling "random" mode while not playing
* randomize next song when adding to single-song queue
ver 0.18.11 (2014/05/12) ver 0.18.11 (2014/05/12)
* decoder * decoder

41
src/BulkEdit.hxx Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2003-2014 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_BULK_EDIT_HXX
#define MPD_BULK_EDIT_HXX
#include "Partition.hxx"
/**
* Begin a "bulk edit" and commit it automatically.
*/
class ScopeBulkEdit {
Partition &partition;
public:
ScopeBulkEdit(Partition &_partition):partition(_partition) {
partition.playlist.BeginBulk();
}
~ScopeBulkEdit() {
partition.playlist.CommitBulk(partition.pc);
}
};
#endif

View File

@ -103,6 +103,12 @@ playlist::UpdateQueuedSong(PlayerControl &pc, const Song *prev)
if (!playing) if (!playing)
return; return;
if (prev == nullptr && bulk_edit)
/* postponed until CommitBulk() to avoid always
queueing the first song that is being added (in
random mode) */
return;
assert(!queue.IsEmpty()); assert(!queue.IsEmpty());
assert((queued < 0) == (prev == nullptr)); assert((queued < 0) == (prev == nullptr));

View File

@ -45,6 +45,18 @@ struct playlist {
*/ */
bool stop_on_error; bool stop_on_error;
/**
* If true, then a bulk edit has been initiated by
* BeginBulk(), and UpdateQueuedSong() and OnModified() will
* be postponed until CommitBulk()
*/
bool bulk_edit;
/**
* Has the queue been modified during bulk edit mode?
*/
bool bulk_modified;
/** /**
* Number of errors since playback was started. If this * Number of errors since playback was started. If this
* number exceeds the length of the playlist, MPD gives up, * number exceeds the length of the playlist, MPD gives up,
@ -69,7 +81,9 @@ struct playlist {
int queued; int queued;
playlist(unsigned max_length) playlist(unsigned max_length)
:queue(max_length), playing(false), current(-1), queued(-1) { :queue(max_length), playing(false),
bulk_edit(false),
current(-1), queued(-1) {
} }
~playlist() { ~playlist() {
@ -126,6 +140,9 @@ protected:
void UpdateQueuedSong(PlayerControl &pc, const Song *prev); void UpdateQueuedSong(PlayerControl &pc, const Song *prev);
public: public:
void BeginBulk();
void CommitBulk(PlayerControl &pc);
void Clear(PlayerControl &pc); void Clear(PlayerControl &pc);
/** /**

View File

@ -40,6 +40,12 @@
void void
playlist::OnModified() playlist::OnModified()
{ {
if (bulk_edit) {
/* postponed to CommitBulk() */
bulk_modified = true;
return;
}
queue.IncrementVersion(); queue.IncrementVersion();
idle_add(IDLE_PLAYLIST); idle_add(IDLE_PLAYLIST);
@ -56,6 +62,35 @@ playlist::Clear(PlayerControl &pc)
OnModified(); OnModified();
} }
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();
}
PlaylistResult PlaylistResult
playlist::AppendFile(PlayerControl &pc, playlist::AppendFile(PlayerControl &pc,
const char *path_utf8, unsigned *added_id) const char *path_utf8, unsigned *added_id)

View File

@ -30,6 +30,7 @@
#include "util/Error.hxx" #include "util/Error.hxx"
#include "SongFilter.hxx" #include "SongFilter.hxx"
#include "protocol/Result.hxx" #include "protocol/Result.hxx"
#include "BulkEdit.hxx"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -92,6 +93,8 @@ handle_match_add(Client &client, int argc, char *argv[], bool fold_case)
return CommandResult::ERROR; return CommandResult::ERROR;
} }
const ScopeBulkEdit bulk_edit(client.partition);
const DatabaseSelection selection("", true, &filter); const DatabaseSelection selection("", true, &filter);
Error error; Error error;
return AddFromDatabase(client.partition, selection, error) return AddFromDatabase(client.partition, selection, error)

View File

@ -26,6 +26,7 @@
#include "PlaylistFile.hxx" #include "PlaylistFile.hxx"
#include "PlaylistVector.hxx" #include "PlaylistVector.hxx"
#include "PlaylistQueue.hxx" #include "PlaylistQueue.hxx"
#include "BulkEdit.hxx"
#include "TimePrint.hxx" #include "TimePrint.hxx"
#include "Client.hxx" #include "Client.hxx"
#include "protocol/ArgParser.hxx" #include "protocol/ArgParser.hxx"
@ -67,6 +68,8 @@ handle_load(Client &client, int argc, char *argv[])
} else if (!check_range(client, &start_index, &end_index, argv[2])) } else if (!check_range(client, &start_index, &end_index, argv[2]))
return CommandResult::ERROR; return CommandResult::ERROR;
const ScopeBulkEdit bulk_edit(client.partition);
const PlaylistResult result = const PlaylistResult result =
playlist_open_into_queue(argv[1], playlist_open_into_queue(argv[1],
start_index, end_index, start_index, end_index,

View File

@ -28,6 +28,7 @@
#include "ClientFile.hxx" #include "ClientFile.hxx"
#include "Client.hxx" #include "Client.hxx"
#include "Partition.hxx" #include "Partition.hxx"
#include "BulkEdit.hxx"
#include "protocol/ArgParser.hxx" #include "protocol/ArgParser.hxx"
#include "protocol/Result.hxx" #include "protocol/Result.hxx"
#include "ls.hxx" #include "ls.hxx"
@ -73,6 +74,8 @@ handle_add(Client &client, gcc_unused int argc, char *argv[])
return print_playlist_result(client, result); return print_playlist_result(client, result);
} }
const ScopeBulkEdit bulk_edit(client.partition);
const DatabaseSelection selection(uri, true); const DatabaseSelection selection(uri, true);
Error error; Error error;
return AddFromDatabase(client.partition, selection, error) return AddFromDatabase(client.partition, selection, error)