diff --git a/core/playlist.c b/core/playlist.c index f410149a..79c0523a 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -2,21 +2,25 @@ * Copyright 2013 (c) Anna Schumaker. */ #include +#include #include struct playlist_type *playlist_types[] = { [PL_SYSTEM] = &pl_system, + [PL_LIBRARY] = &pl_library, }; void playlist_init(struct queue_ops *ops) { pl_system_init(ops); + pl_library_init(ops); } void playlist_deinit() { pl_system_deinit(); + pl_library_deinit(); } bool playlist_add(enum playlist_type_t type, const gchar *name, diff --git a/core/playlists/library.c b/core/playlists/library.c new file mode 100644 index 00000000..45000279 --- /dev/null +++ b/core/playlists/library.c @@ -0,0 +1,63 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include + +static struct queue_ops *lib_ops = NULL; + + +static struct playlist *__lib_pl_alloc(const gchar *path) +{ + struct playlist *playlist = g_malloc(sizeof(struct playlist)); + + playlist->pl_name = path; + playlist->pl_type = PL_LIBRARY; + queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT, lib_ops, playlist); + + return playlist; +} + +static void __lib_pl_free(struct playlist *playlist) +{ + if (playlist) { + queue_deinit(&playlist->pl_queue); + g_free(playlist); + } +} + + +struct playlist_type pl_library; + + +static bool __lib_pl_init(void *data) +{ + struct db_entry *dbe, *next; + struct playlist *playlist; + + db_for_each(dbe, next, library_db_get()) { + playlist = __lib_pl_alloc(LIBRARY(dbe)->li_path); + LIBRARY(dbe)->li_playlist = playlist; + } + + return true; +} + +void pl_library_init(struct queue_ops *ops) +{ + lib_ops = ops; + idle_schedule(IDLE_SYNC, __lib_pl_init, NULL); +} + +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; + LIBRARY(dbe)->li_playlist = NULL; + + __lib_pl_free(playlist); + } +} diff --git a/include/core/playlists/library.h b/include/core/playlists/library.h new file mode 100644 index 00000000..c9091533 --- /dev/null +++ b/include/core/playlists/library.h @@ -0,0 +1,19 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#ifndef OCARINA_CORE_PLAYLISTS_LIBRARY_H +#define OCARINA_CORE_PLAYLISTS_LIBRARY_H +#include + + +/* Library playlist type. */ +extern struct playlist_type pl_library; + + +/* Called to initialize library playlists. */ +void pl_library_init(struct queue_ops *); + +/* Called to deinitialize system playlists. */ +void pl_library_deinit(); + +#endif /* OCARINA_CORE_PLAYLISTS_LIBRARY_H */ diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 2f2192f8..1a03f4fc 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -11,6 +11,7 @@ enum playlist_type_t { PL_SYSTEM, + PL_LIBRARY, PL_MAX_TYPE, }; diff --git a/tests/core/.gitignore b/tests/core/.gitignore index da8489bc..5d27b38b 100644 --- a/tests/core/.gitignore +++ b/tests/core/.gitignore @@ -12,6 +12,7 @@ tags/library tags/track queue playlists/system +playlists/library playlist collection tempq diff --git a/tests/core/playlists/Sconscript b/tests/core/playlists/Sconscript index 573b16c3..b1b544fe 100644 --- a/tests/core/playlists/Sconscript +++ b/tests/core/playlists/Sconscript @@ -7,9 +7,10 @@ def PlaylistTest(name): run = CoreTest("playlists/%s" % name) Alias("tests/core/playlists", run) if len(res) > 0 and testing_group([ "tests/core/playlists" ]): - Depends(run, res[1]) + Depends(run, res[-1]) return run -res += [ PlaylistTest("system") ] +res += [ PlaylistTest("system") ] +res += [ PlaylistTest("library") ] Return("res") diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c new file mode 100644 index 00000000..8af679ed --- /dev/null +++ b/tests/core/playlists/library.c @@ -0,0 +1,37 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include +#include + +void test_library() +{ + struct library *library; + + idle_init_sync(); + filter_init(); + tags_init(); + while (idle_run_task()) {}; + + library = library_find("tests/Music"); + test_equal(library->li_playlist, NULL); + + pl_library_init(NULL); + while (idle_run_task()) {}; + test_not_equal(library->li_playlist, NULL); + + pl_library_deinit(); + test_equal(library->li_playlist, NULL); + + tags_deinit(); + filter_deinit(); + idle_deinit(); +} + +DECLARE_UNIT_TESTS( + UNIT_TEST("Library Playlists", test_library), +);