From bd8df2a169bfa7e0bf2399e38ccf66df78c0473b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 20 Sep 2016 10:00:12 -0400 Subject: [PATCH] core/playlist: Add playlist_selected() for selected tracks In most cases this function just triggers a UI update, but system playlists have a little extra bookkeeping to do to remove the track from the Queued Tracks playlist. Signed-off-by: Anna Schumaker --- core/audio.c | 2 +- core/playlist.c | 14 ++++++++++++-- core/playlists/artist.c | 9 +++++---- core/playlists/library.c | 11 ++++++----- core/playlists/system.c | 31 ++++++++++++++++--------------- core/playlists/user.c | 11 ++++++----- include/core/playlist.h | 3 +++ include/core/playlists/playlist.h | 3 +++ tests/core/playlist.c | 2 ++ tests/core/playlists/artist.c | 2 ++ tests/core/playlists/library.c | 2 ++ tests/core/playlists/system.c | 4 ++++ tests/core/playlists/user.c | 2 ++ 13 files changed, 64 insertions(+), 32 deletions(-) diff --git a/core/audio.c b/core/audio.c index 93de3d47..6421e1eb 100644 --- a/core/audio.c +++ b/core/audio.c @@ -52,7 +52,7 @@ static struct track *__audio_do_load(struct track *track, GstState state) if (track) { __audio_gst_load(track, state); - queue_updated(&playlist_lookup(PL_SYSTEM, "Collection")->pl_queue, track); + playlist_selected(track); } playlist_played(prev); diff --git a/core/playlist.c b/core/playlist.c index 6fae406a..1e2d1523 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -72,6 +72,18 @@ void playlist_played(struct track *track) } } +void playlist_selected(struct track *track) +{ + unsigned int i; + if (track) { + for (i = 0; i < PL_MAX_TYPE; i++) + playlist_types[i]->pl_selected(track); + + if (playlist_size(current) == 0) + playlist_select(previous); + } +} + struct playlist *playlist_new(enum playlist_type_t type, const gchar *name) { if (type < PL_MAX_TYPE && playlist_types[type]->pl_new) @@ -145,8 +157,6 @@ struct track *playlist_next(void) track = current->pl_ops->pl_next(current); if (track) playlist_types[current->pl_type]->pl_save(); - if (playlist_size(current) == 0) - playlist_select(previous); return track; } diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 537dc59a..fc8c33fd 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -121,10 +121,11 @@ static void pl_artist_played(struct track *track) struct playlist_type pl_artist = { - .pl_save = pl_artist_save, - .pl_lookup = pl_artist_lookup, - .pl_get = pl_artist_get, - .pl_played = pl_artist_played, + .pl_save = pl_artist_save, + .pl_lookup = pl_artist_lookup, + .pl_get = pl_artist_get, + .pl_played = pl_artist_played, + .pl_selected = pl_artist_played, }; diff --git a/core/playlists/library.c b/core/playlists/library.c index 9c0cc079..5a14f63a 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -252,11 +252,12 @@ static void pl_library_played(struct track *track) struct playlist_type pl_library = { - .pl_save = pl_library_save, - .pl_lookup = pl_library_lookup, - .pl_get = pl_library_get, - .pl_new = pl_library_new, - .pl_played = pl_library_played, + .pl_save = pl_library_save, + .pl_lookup = pl_library_lookup, + .pl_get = pl_library_get, + .pl_new = pl_library_new, + .pl_played = pl_library_played, + .pl_selected = pl_library_played, }; diff --git a/core/playlists/system.c b/core/playlists/system.c index f42e451a..89034308 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -163,21 +163,11 @@ static bool sys_pl_queued_load() return true; } -static struct track *sys_pl_queued_next(struct playlist *playlist) -{ - struct track *track = playlist_generic_next(playlist); - unsigned int pos = playlist->pl_queue.q_cur.it_pos; - - queue_iter_prev(&playlist->pl_queue.q_cur); - queue_remove(&playlist->pl_queue, pos); - return track; -} - static struct playlist_ops queued_ops = { .pl_add = playlist_generic_add_track, .pl_can_select = playlist_generic_can_select, .pl_delete = sys_pl_delete_clear, - .pl_next = sys_pl_queued_next, + .pl_next = playlist_generic_next, .pl_remove = playlist_generic_remove_track, .pl_set_flag = playlist_generic_set_flag, .pl_sort = playlist_generic_sort, @@ -382,12 +372,23 @@ static void pl_system_played(struct track *track) sys_pl_update(pl_system_lookup("Least Played")); } +static void pl_system_selected(struct track *track) +{ + unsigned int i; + + queue_iter_prev(&pl_system_get(SYS_PL_QUEUED)->pl_queue.q_cur); + queue_remove_all(&pl_system_get(SYS_PL_QUEUED)->pl_queue, track); + for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) + queue_updated(&pl_system_get(i)->pl_queue, track); +} + struct playlist_type pl_system = { - .pl_save = pl_system_save, - .pl_lookup = pl_system_lookup, - .pl_get = pl_system_get, - .pl_played = pl_system_played, + .pl_save = pl_system_save, + .pl_lookup = pl_system_lookup, + .pl_get = pl_system_get, + .pl_played = pl_system_played, + .pl_selected = pl_system_selected, }; diff --git a/core/playlists/user.c b/core/playlists/user.c index aff8e62d..dd35c40c 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -127,11 +127,12 @@ static void pl_user_played(struct track *track) struct playlist_type pl_user = { - .pl_save = pl_user_save, - .pl_lookup = pl_user_lookup, - .pl_get = pl_user_get, - .pl_new = pl_user_new, - .pl_played = pl_user_played, + .pl_save = pl_user_save, + .pl_lookup = pl_user_lookup, + .pl_get = pl_user_get, + .pl_new = pl_user_new, + .pl_played = pl_user_played, + .pl_selected = pl_user_played, }; diff --git a/include/core/playlist.h b/include/core/playlist.h index 99af6766..a79a1dea 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -25,6 +25,9 @@ void playlist_save(); /* Called to notify all playlists that a track has been played. */ void playlist_played(struct track *); +/* Called to notify all playlists that a track has been selected. */ +void playlist_selected(struct track *); + /* Called to create a new playlist. */ struct playlist *playlist_new(enum playlist_type_t, const gchar *); diff --git a/include/core/playlists/playlist.h b/include/core/playlists/playlist.h index 0183ba02..c55ca25b 100644 --- a/include/core/playlists/playlist.h +++ b/include/core/playlists/playlist.h @@ -75,6 +75,9 @@ struct playlist_type { /* Called to notify that a track has been played. */ void (*pl_played)(struct track *); + + /* Called to notify that a track has been selected. */ + void (*pl_selected)(struct track *); }; diff --git a/tests/core/playlist.c b/tests/core/playlist.c index 7450f8cc..18fd308b 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -32,6 +32,8 @@ static void test_null() g_assert(playlist_current() == playlist_lookup(PL_SYSTEM, "Collection")); g_assert_false(playlist_select(NULL)); g_assert(playlist_current() == playlist_lookup(PL_SYSTEM, "Collection")); + playlist_selected(NULL); + playlist_played(NULL); g_assert_false(playlist_add(NULL, NULL)); g_assert_false(playlist_add(NULL, track_get(0))); diff --git a/tests/core/playlists/artist.c b/tests/core/playlists/artist.c index c5e9f759..ab3a0cce 100644 --- a/tests/core/playlists/artist.c +++ b/tests/core/playlists/artist.c @@ -43,6 +43,8 @@ void test_artist() g_assert_cmpuint(settings_get("core.playlist.cur.id"), ==, 0); g_assert(playlist_current() == playlist); g_assert_false(playlist_select(playlist)); + + playlist_selected(track_get(0)); playlist_played(track_get(0)); g_assert_cmpuint(playlist_next()->tr_track, ==, 1); diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index 04c92fe1..b153afb2 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -65,6 +65,8 @@ void test_library() g_assert_false(playlist_remove(playlist, track_get(0))); g_assert_false(playlist_remove(playlist, track_get(1))); g_assert_cmpuint(playlist_size(playlist), ==, 48); + + playlist_selected(track_get(1)); playlist_played(track_get(1)); g_assert_false(playlist_get_random(playlist)); diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index 764b3983..94a4178f 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -319,10 +319,14 @@ static void test_next() g_assert(playlist_current() == __test_pl_queued()); g_assert(playlist_next() == track_get(0)); + g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 2); + playlist_selected(track_get(0)); g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 1); g_assert(playlist_current() == __test_pl_queued()); g_assert(playlist_next() == track_get(1)); + g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 1); + playlist_selected(track_get(1)); g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 0); g_assert(playlist_current() == __test_pl_collection()); diff --git a/tests/core/playlists/user.c b/tests/core/playlists/user.c index 63574608..5c400a64 100644 --- a/tests/core/playlists/user.c +++ b/tests/core/playlists/user.c @@ -47,6 +47,8 @@ void test_user() g_assert(playlist_next() == track_get(0)); g_assert_true(playlist_has(playlist, track_get(0))); g_assert_cmpuint(playlist_size(playlist), ==, 1); + + playlist_selected(track_get(0)); playlist_played(track_get(0)); pl_user_deinit();