From 3286b61dcf06d02336fb3b3685ea7cffc5352398 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 22 Sep 2016 11:32:40 -0400 Subject: [PATCH] core/playlist: Add playlist_generic_{alloc,free}() functions Artist and library playlists are allocated manually, so there should be generic functions that both can use to get a playlist pointer and free it when we're done. I also add a callback for telling the UI when new playlists have been allocated. This isn't needed for Library playlists, but this is the only way the UI can know about new Artists. Signed-off-by: Anna Schumaker --- core/playlists/artist.c | 25 ++++--------------------- core/playlists/generic.c | 27 +++++++++++++++++++++++++++ core/playlists/library.c | 27 +++++---------------------- gui/playlist.c | 7 +++++-- include/core/playlists/generic.h | 11 +++++++++++ tests/core/playlist.c | 1 + tests/gui/model.c | 2 ++ tests/gui/playlists/artist.c | 2 +- 8 files changed, 56 insertions(+), 46 deletions(-) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 9529cf2d..46601941 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -17,23 +17,9 @@ static struct playlist_ops pl_artist_ops = { static struct playlist *__artist_pl_alloc(struct artist *artist) { - struct playlist *playlist = g_malloc(sizeof(struct playlist)); - - playlist->pl_name = artist->ar_name; - playlist->pl_type = PL_ARTIST; - playlist->pl_id = artist_index(artist); - playlist->pl_ops = &pl_artist_ops; - playlist_generic_init_sorted(playlist, artist_ops); - - return playlist; -} - -static void __artist_pl_free(struct playlist *playlist) -{ - if (playlist) { - playlist_generic_deinit(playlist); - g_free(playlist); - } + return playlist_generic_alloc(artist->ar_name, PL_ARTIST, + artist_index(artist), &pl_artist_ops, + artist_ops); } static bool __artist_pl_add(void *data) @@ -146,13 +132,10 @@ void pl_artist_init(struct queue_ops *ops) void pl_artist_deinit() { struct db_entry *dbe, *next; - struct playlist *playlist; db_for_each(dbe, next, artist_db_get()) { - playlist = ARTIST(dbe)->ar_playlist; + playlist_generic_free(ARTIST(dbe)->ar_playlist); ARTIST(dbe)->ar_playlist = NULL; - - __artist_pl_free(playlist); } } diff --git a/core/playlists/generic.c b/core/playlists/generic.c index b158a6e3..0504b692 100644 --- a/core/playlists/generic.c +++ b/core/playlists/generic.c @@ -71,6 +71,33 @@ void playlist_generic_deinit(struct playlist *playlist) } } +struct playlist *playlist_generic_alloc(gchar *name, + enum playlist_type_t type, + unsigned int id, + struct playlist_ops *ops, + struct queue_ops *qops) +{ + struct playlist *playlist = g_malloc(sizeof(struct playlist)); + + playlist->pl_name = name; + playlist->pl_type = type; + playlist->pl_id = id; + playlist->pl_ops = ops; + + playlist_generic_init_sorted(playlist, qops); + if (callbacks) + callbacks->pl_cb_alloc(playlist); + return playlist; +} + +void playlist_generic_free(struct playlist *playlist) +{ + if (playlist) { + playlist_generic_deinit(playlist); + g_free(playlist); + } +} + void playlist_generic_save(struct playlist *playlist, struct file *file, unsigned int flags) { diff --git a/core/playlists/library.c b/core/playlists/library.c index 28cfed83..de42b9e2 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -22,23 +22,9 @@ static struct playlist_ops pl_library_ops; static struct playlist *__lib_pl_alloc(struct library *library) { - struct playlist *playlist = g_malloc(sizeof(struct playlist)); - - playlist->pl_name = library->li_path; - playlist->pl_type = PL_LIBRARY; - playlist->pl_id = library_index(library); - playlist->pl_ops = &pl_library_ops; - playlist_generic_init_sorted(playlist, lib_ops); - - return playlist; -} - -static void __lib_pl_free(struct playlist *playlist) -{ - if (playlist) { - playlist_generic_deinit(playlist); - g_free(playlist); - } + return playlist_generic_alloc(library->li_path, PL_LIBRARY, + library_index(library), &pl_library_ops, + lib_ops); } static bool __lib_pl_add(void *data) @@ -182,7 +168,7 @@ static bool pl_library_delete(struct playlist *playlist) pl_artist_delete_track(playlist_iter_track(it)); pl_user_delete_track(playlist_iter_track(it)); } - __lib_pl_free(playlist); + playlist_generic_free(playlist); track_remove_all(library); library_remove(library); @@ -279,13 +265,10 @@ void pl_library_init(struct queue_ops *ops) void pl_library_deinit() { struct db_entry *dbe, *next; - struct playlist *playlist; db_for_each(dbe, next, library_db_get()) { - playlist = LIBRARY(dbe)->li_playlist; + playlist_generic_free(LIBRARY(dbe)->li_playlist); LIBRARY(dbe)->li_playlist = NULL; - - __lib_pl_free(playlist); } } diff --git a/gui/playlist.c b/gui/playlist.c index 16875744..2a10964b 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -30,11 +30,13 @@ static inline void __gui_playlist_update_size(struct playlist *playlist) static void *__gui_playlist_init(struct queue *queue, void *data) { - struct playlist *playlist = (struct playlist *)data; + return data; +} +static void __gui_playlist_alloc(struct playlist *playlist) +{ if (playlist->pl_type == PL_ARTIST) gui_pl_artist_add(playlist); - return playlist; } static void __gui_playlist_added(struct playlist *playlist, struct track *track) @@ -56,6 +58,7 @@ struct queue_ops playlist_ops = { }; struct playlist_callbacks playlist_cb = { + .pl_cb_alloc = __gui_playlist_alloc, .pl_cb_added = __gui_playlist_added, .pl_cb_removed = __gui_playlist_removed, .pl_cb_updated = gui_model_update, diff --git a/include/core/playlists/generic.h b/include/core/playlists/generic.h index 225e1b5c..45c00652 100644 --- a/include/core/playlists/generic.h +++ b/include/core/playlists/generic.h @@ -15,6 +15,9 @@ enum playlist_save_flags { #define PL_SAVE_ALL (PL_SAVE_TRACKS | PL_SAVE_METADATA) struct playlist_callbacks { + /* Called to notify that a new playlist has been allocated. */ + void (*pl_cb_alloc)(struct playlist *); + /* Called to notify that a track has been added. */ void (*pl_cb_added)(struct playlist *, struct track *); @@ -43,6 +46,14 @@ void playlist_generic_init_sorted(struct playlist *, struct queue_ops *); /* Generic playlist deinit function. */ void playlist_generic_deinit(struct playlist *); +/* Generic playlist alloc function. */ +struct playlist *playlist_generic_alloc(gchar *, enum playlist_type_t, + unsigned int, struct playlist_ops *, + struct queue_ops *); + +/* Generic playlist free function. */ +void playlist_generic_free(struct playlist *); + /* Generic playlist save function. */ void playlist_generic_save(struct playlist *, struct file *, unsigned int); diff --git a/tests/core/playlist.c b/tests/core/playlist.c index 0e86d3d5..a87aeb9e 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -41,6 +41,7 @@ static void test_null() g_assert_null(playlist_new(PL_MAX_TYPE, "NULL")); g_assert_null(playlist_new(PL_MAX_TYPE, NULL)); g_assert_false(playlist_delete(NULL)); + playlist_generic_free(NULL); playlist_generic_init(NULL, NULL); playlist_generic_init_sorted(NULL, NULL); diff --git a/tests/gui/model.c b/tests/gui/model.c index f99abcc4..8c6459f3 100644 --- a/tests/gui/model.c +++ b/tests/gui/model.c @@ -25,6 +25,7 @@ void on_row_changed(GtkTreeModel *model, GtkTreePath *path, void *test_queue_init(struct queue *queue, void *data) { return NULL; } +void test_cb_alloc(struct playlist *playlist) {} void test_on_load(struct track *track) {} void test_on_state_change(GstState state) {} void test_on_config_pause(int count) {} @@ -34,6 +35,7 @@ struct queue_ops test_ops = { }; struct playlist_callbacks test_cb = { + .pl_cb_alloc = test_cb_alloc, .pl_cb_added = gui_model_add, .pl_cb_removed = gui_model_remove, .pl_cb_updated = gui_model_update, diff --git a/tests/gui/playlists/artist.c b/tests/gui/playlists/artist.c index 250b3961..021cdf3b 100644 --- a/tests/gui/playlists/artist.c +++ b/tests/gui/playlists/artist.c @@ -12,7 +12,7 @@ #include struct core_init_data init_data = { - .playlist_ops = &playlist_ops, + .playlist_cb = &playlist_cb, }; static void test_artist()