diff --git a/core/playlist.c b/core/playlist.c index 0494233d..c7a0692d 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -2,11 +2,13 @@ * Copyright 2013 (c) Anna Schumaker. */ #include +#include #include #include struct playlist_type *playlist_types[] = { [PL_SYSTEM] = &pl_system, + [PL_ARTIST] = &pl_artist, [PL_LIBRARY] = &pl_library, }; @@ -14,12 +16,14 @@ struct playlist_type *playlist_types[] = { void playlist_init(struct queue_ops *ops) { pl_system_init(ops); + pl_artist_init(ops); pl_library_init(ops); } void playlist_deinit() { pl_system_deinit(); + pl_artist_deinit(); pl_library_deinit(); } diff --git a/core/playlists/artist.c b/core/playlists/artist.c new file mode 100644 index 00000000..2b8f6e40 --- /dev/null +++ b/core/playlists/artist.c @@ -0,0 +1,63 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include + +static struct queue_ops *artist_ops = NULL; + + +static struct playlist *__artist_pl_alloc(gchar *name) +{ + struct playlist *playlist = g_malloc(sizeof(struct playlist)); + + playlist->pl_name = name; + playlist->pl_type = PL_ARTIST; + queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT, artist_ops, playlist); + + return playlist; +} + +static void __artist_pl_free(struct playlist *playlist) +{ + if (playlist) { + queue_deinit(&playlist->pl_queue); + g_free(playlist); + } +} + + +struct playlist_type pl_artist; + + +static bool __artist_pl_init(void *data) +{ + struct db_entry *dbe, *next; + struct playlist *playlist; + + db_for_each(dbe, next, artist_db_get()) { + playlist = __artist_pl_alloc(ARTIST(dbe)->ar_name); + ARTIST(dbe)->ar_playlist = playlist; + } + + return true; +} + +void pl_artist_init(struct queue_ops *ops) +{ + artist_ops = ops; + idle_schedule(IDLE_SYNC, __artist_pl_init, NULL); +} + +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; + ARTIST(dbe)->ar_playlist = NULL; + + __artist_pl_free(playlist); + } +} diff --git a/include/core/playlists/artist.h b/include/core/playlists/artist.h new file mode 100644 index 00000000..6bd73399 --- /dev/null +++ b/include/core/playlists/artist.h @@ -0,0 +1,18 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#ifndef OCARINA_CORE_PLAYLISTS_ARTIST_H +#define OCARINA_CORE_PLAYLISTS_ARTIST_H +#include + +/* Artist playlist type. */ +extern struct playlist_type pl_artist; + + +/* Called to initialize artist playlists. */ +void pl_artist_init(struct queue_ops *ops); + +/* Called to deinitialize library playlists. */ +void pl_artist_deinit(); + +#endif /* OCARINA_CORE_PLAYLISTS_ARTIST_H */ diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index c22a6719..5a9bbf5a 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -11,6 +11,7 @@ enum playlist_type_t { PL_SYSTEM, + PL_ARTIST, PL_LIBRARY, PL_MAX_TYPE, }; diff --git a/tests/core/.gitignore b/tests/core/.gitignore index 5d27b38b..6fbc2fc7 100644 --- a/tests/core/.gitignore +++ b/tests/core/.gitignore @@ -12,6 +12,7 @@ tags/library tags/track queue playlists/system +playlists/artist playlists/library playlist collection diff --git a/tests/core/playlists/Sconscript b/tests/core/playlists/Sconscript index b1b544fe..6aba4498 100644 --- a/tests/core/playlists/Sconscript +++ b/tests/core/playlists/Sconscript @@ -11,6 +11,7 @@ def PlaylistTest(name): return run res += [ PlaylistTest("system") ] +res += [ PlaylistTest("artist") ] res += [ PlaylistTest("library") ] Return("res") diff --git a/tests/core/playlists/artist.c b/tests/core/playlists/artist.c new file mode 100644 index 00000000..56c2af26 --- /dev/null +++ b/tests/core/playlists/artist.c @@ -0,0 +1,38 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include +#include + + +void test_artist() +{ + struct artist *artist; + + idle_init_sync(); + filter_init(); + tags_init(); + while (idle_run_task()) {}; + + artist = artist_find("Koji Kondo"); + test_equal(artist->ar_playlist, NULL); + + pl_artist_init(NULL); + while (idle_run_task()) {}; + test_not_equal(artist->ar_playlist, NULL); + + pl_artist_deinit(); + test_equal(artist->ar_playlist, NULL); + + tags_deinit(); + filter_deinit(); + idle_deinit(); +} + +DECLARE_UNIT_TESTS( + UNIT_TEST("Artist Playlists", test_artist), +);