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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-20 10:00:12 -04:00
parent 07bf09c2ad
commit bd8df2a169
13 changed files with 64 additions and 32 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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 *);

View File

@ -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 *);
};

View File

@ -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)));

View File

@ -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);

View File

@ -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));

View File

@ -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());

View File

@ -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();