From 0c197c10f970b12e11b78d5886b6e10575cbf74b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 21 Sep 2016 13:30:22 -0400 Subject: [PATCH] core/playlist: Move playlist length into the playlist struct Signed-off-by: Anna Schumaker --- core/playlists/generic.c | 12 +++++++----- core/queue.c | 2 -- gui/model.c | 2 +- include/core/playlists/playlist.h | 7 ++++--- include/core/queue.h | 1 - tests/core/playlist.c | 25 +++++++++++++------------ tests/core/queue.c | 2 -- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/core/playlists/generic.c b/core/playlists/generic.c index 0143c407..97d2a489 100644 --- a/core/playlists/generic.c +++ b/core/playlists/generic.c @@ -49,8 +49,10 @@ void playlist_generic_init(struct playlist *playlist, struct queue_ops *ops) void playlist_generic_deinit(struct playlist *playlist) { - if (playlist) + if (playlist) { queue_deinit(&playlist->pl_queue); + playlist->pl_length = 0; + } } void playlist_generic_save(struct playlist *playlist, struct file *file, @@ -145,7 +147,7 @@ void playlist_generic_clear(struct playlist *playlist) n = playlist_size(playlist); g_queue_clear(&playlist->pl_queue.q_tracks); - playlist->pl_queue.q_length = 0; + playlist->pl_length = 0; playlist->pl_queue.q_cur.it_iter = NULL; playlist->pl_queue.q_cur.it_pos = UINT_MAX; if (callbacks) @@ -157,7 +159,7 @@ bool playlist_generic_add(struct playlist *playlist, struct track *track) if (!playlist || !track || playlist_has(playlist, track)) return false; - playlist->pl_queue.q_length += track->tr_length; + playlist->pl_length += track->tr_length; if (playlist->pl_queue.q_sort) { g_queue_insert_sorted(&playlist->pl_queue.q_tracks, track, __playlist_generic_less_than, @@ -178,7 +180,7 @@ bool playlist_generic_add_front(struct playlist *playlist, struct track *track) if (!playlist || !track) return false; - playlist->pl_queue.q_length += track->tr_length; + playlist->pl_length += track->tr_length; g_queue_push_head(&playlist->pl_queue.q_tracks, track); if (callbacks) callbacks->pl_cb_added(playlist, track); @@ -196,7 +198,7 @@ bool playlist_generic_remove(struct playlist *playlist, struct track *track) queue_iter_prev(&playlist->pl_queue.q_cur); count = g_queue_remove_all(&playlist->pl_queue.q_tracks, track); - playlist->pl_queue.q_length -= (count * track->tr_length); + playlist->pl_length -= (count * track->tr_length); playlist->pl_queue.q_cur.it_pos = g_queue_link_index( &playlist->pl_queue.q_tracks, playlist->pl_queue.q_cur.it_iter); diff --git a/core/queue.c b/core/queue.c index 0addb8dd..30a68855 100644 --- a/core/queue.c +++ b/core/queue.c @@ -21,7 +21,6 @@ static inline void __queue_deinit(struct queue *queue) void queue_init(struct queue *queue, const struct queue_ops *ops, void *data) { - queue->q_length = 0; queue->q_sort = NULL; queue->q_ops = ops; @@ -37,7 +36,6 @@ void queue_deinit(struct queue *queue) __queue_deinit(queue); g_slist_free(queue->q_sort); queue->q_sort = NULL; - queue->q_length = 0; queue->q_cur.it_pos = UINT_MAX; queue->q_cur.it_iter = NULL; } diff --git a/gui/model.c b/gui/model.c index 0765f507..262adc4d 100644 --- a/gui/model.c +++ b/gui/model.c @@ -251,7 +251,7 @@ static void __gui_model_set_runtime(void) gchar *len = NULL; if (cur_playlist) - len = string_sec2str_long(cur_playlist->pl_queue.q_length); + len = string_sec2str_long(cur_playlist->pl_length); gtk_label_set_text(gui_model_runtime(), len); g_free(len); diff --git a/include/core/playlists/playlist.h b/include/core/playlists/playlist.h index c058d553..47a3e972 100644 --- a/include/core/playlists/playlist.h +++ b/include/core/playlists/playlist.h @@ -47,9 +47,10 @@ struct playlist { gchar *pl_name; /* This playlist's name. */ unsigned int pl_id; /* This playlist's identifier. */ - bool pl_random; /* This playlist's random setting. */ - void *pl_private; /* This playlist's private data. */ - struct queue pl_queue; /* This playlist's queue of tracks. */ + unsigned int pl_length; /* This playlist's length, in seconds. */ + bool pl_random; /* This playlist's random setting. */ + void *pl_private; /* This playlist's private data. */ + struct queue pl_queue; /* This playlist's queue of tracks. */ const struct playlist_ops *pl_ops; /* This playlist's supported operations. */ }; diff --git a/include/core/queue.h b/include/core/queue.h index 474b769c..8c78fef6 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -30,7 +30,6 @@ struct queue_iter { struct queue { - unsigned int q_length; /* The queue's total runtime (in seconds). */ GQueue q_tracks; /* The queue's list of tracks. */ GSList *q_sort; /* The queue's sort order. */ void *q_private; /* The queue's private data. */ diff --git a/tests/core/playlist.c b/tests/core/playlist.c index b2d00a97..442fa181 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -88,13 +88,14 @@ static void test_playlist() g_assert_cmpuint(playlist_size(&p), ==, 0); playlist_generic_init(&p, NULL); g_assert_cmpuint(playlist_size(&p), ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); for (i = 0; i < 13; i++) { ex_length += track_get(i)->tr_length; playlist_generic_add_front(&p, track_get(i)); g_assert_true(playlist_has(&p, track_get(i))); - g_assert_cmpuint(p.pl_queue.q_length, ==, ex_length); - g_assert_cmpuint(playlist_size(&p), ==, i + 1); + g_assert_cmpuint(p.pl_length, ==, ex_length); + g_assert_cmpuint(playlist_size(&p), ==, i + 1); g_assert(cb_playlist == &p); g_assert(cb_track == track_get(i)); } @@ -118,7 +119,7 @@ static void test_playlist() g_assert_false(playlist_has(&p, track_get(i))); g_assert(cb_playlist == &p); g_assert(cb_track == track_get(i)); - g_assert_cmpuint(p.pl_queue.q_length, ==, ex_length); + g_assert_cmpuint(p.pl_length, ==, ex_length); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, (11 - i)); g_assert_cmpuint(playlist_size(&p), ==, (12 - i)); if (i < 12) @@ -128,7 +129,7 @@ static void test_playlist() g_assert_false(playlist_has(&p, track_get(i))); g_assert(cb_playlist == &p); g_assert(cb_track == track_get(12)); - g_assert_cmpuint(p.pl_queue.q_length, ==, ex_length); + g_assert_cmpuint(p.pl_length, ==, ex_length); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); @@ -138,26 +139,26 @@ static void test_playlist() g_assert_true( playlist_add(&p, track_get(i))); g_assert_false(playlist_add(&p, track_get(i))); g_assert_true(playlist_has(&p, track_get(i))); - g_assert_cmpuint(p.pl_queue.q_length, ==, ex_length); + g_assert_cmpuint(p.pl_length, ==, ex_length); g_assert_cmpuint(playlist_size(&p), ==, i + 1); g_assert(cb_playlist == &p); g_assert(cb_track == track_get(i)); } g_assert_false(playlist_generic_add(&p, NULL)); - g_assert_cmpuint(p.pl_queue.q_length, ==, ex_length); + g_assert_cmpuint(p.pl_length, ==, ex_length); /* Now clear the playlist. */ queue_iter_set(&p.pl_queue, &p.pl_queue.q_cur, 12); playlist_generic_clear(&p); - g_assert_cmpuint(playlist_size(&p), ==, 0); - g_assert_cmpuint(p.pl_queue.q_length, ==, 0); + g_assert_cmpuint(playlist_size(&p), ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); /* Deinitialize the playlist. */ playlist_generic_deinit(&p); g_assert_cmpuint(playlist_size(&p), ==, 0); - g_assert_cmpuint(p.pl_queue.q_length, ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_queue.q_sort); @@ -241,7 +242,7 @@ static void test_sorting() /* Deinitialize the playlist. */ playlist_generic_deinit(&p); g_assert_cmpuint(playlist_size(&p), ==, 0); - g_assert_cmpuint(p.pl_queue.q_length, ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_queue.q_sort); @@ -287,7 +288,7 @@ static void test_next() /* Deinitialize the playlist. */ playlist_generic_deinit(&p); g_assert_cmpuint(playlist_size(&p), ==, 0); - g_assert_cmpuint(p.pl_queue.q_length, ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); g_assert_null(p.pl_queue.q_sort); @@ -339,7 +340,7 @@ static void test_save_load() /* Deinitialize the playlist. */ playlist_generic_deinit(&p); g_assert_cmpuint(playlist_size(&p), ==, 0); - g_assert_cmpuint(p.pl_queue.q_length, ==, 0); + g_assert_cmpuint(p.pl_length, ==, 0); g_assert_cmpuint(p.pl_queue.q_cur.it_pos, ==, UINT_MAX); g_assert_null(p.pl_queue.q_cur.it_iter); } diff --git a/tests/core/queue.c b/tests/core/queue.c index 5865caaf..0f452a89 100644 --- a/tests/core/queue.c +++ b/tests/core/queue.c @@ -39,7 +39,6 @@ static void test_init() g_assert_null(q.q_private); g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1); - g_assert_cmpuint(q.q_length, ==, 0); g_assert_null(q.q_sort); g_assert_null(q.q_ops); @@ -56,7 +55,6 @@ static void test_init() g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1); g_assert_cmpuint(q.q_cur.it_pos, ==, (unsigned int)-1); - g_assert_cmpuint(q.q_length, ==, 0); g_assert_null(q.q_sort); g_assert(q.q_ops == &test_ops);