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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-22 11:32:40 -04:00
parent 7f54562b71
commit 3286b61dcf
8 changed files with 56 additions and 46 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,7 +12,7 @@
#include <tests/test.h>
struct core_init_data init_data = {
.playlist_ops = &playlist_ops,
.playlist_cb = &playlist_cb,
};
static void test_artist()