helpers: restore old behavior when playing a song already in playlist

This commit is contained in:
Andrzej Rybczak
2015-06-13 16:10:26 +02:00
parent bca788153d
commit b3c0c0798e
2 changed files with 30 additions and 2 deletions

View File

@@ -103,12 +103,20 @@ bool addSongToPlaylist(const MPD::Song &s, bool play, int position)
{
bool result = false;
if (Config.space_add_mode == SpaceAddMode::AddRemove
&& !play
&& myPlaylist->checkForSong(s)
)
{
result = true;
removeSongFromPlaylist(myPlaylist->main(), s);
if (play)
{
const auto begin = myPlaylist->main().beginV(), end = myPlaylist->main().endV();
auto it = find_map_first(begin, end, s, [](const MPD::Song &found) {
Mpd.PlayID(found.getID());
});
assert(it != end);
}
else
removeSongFromPlaylist(myPlaylist->main(), s);
return result;
}
int id = Mpd.AddSong(s, position);

View File

@@ -61,6 +61,26 @@ struct pointer_extractor
ValueT *operator()(ValueT &v) const { return &v; }
};
/// Map over the first element in range satisfying the predicate.
template <typename InputIterator, typename PredicateT, typename MapT>
InputIterator find_map_first(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f)
{
auto it = std::find(first, last, std::forward<PredicateT>(p));
if (it != last)
f(*it);
return it;
}
/// Map over all elements in range satisfying the predicate.
template <typename InputIterator, typename PredicateT, typename MapT>
void find_map_all(InputIterator first, InputIterator last, PredicateT &&p, MapT &&f)
{
InputIterator it = first;
do
it = find_map_first(it, last, p, f);
while (it != last);
}
// identity function object
struct id_
{