From ca5f0701e9b32451f1e5c83dbf335b554f81e440 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 9 Sep 2016 08:35:52 -0400 Subject: [PATCH] core/playlist: playlist_new() returns a playlist pointer This is much more useful than a boolean status, since we can use the playlist pointer right away. Signed-off-by: Anna Schumaker --- core/playlist.c | 6 ++++-- core/playlists/artist.c | 5 ++--- core/playlists/library.c | 6 +++--- core/playlists/system.c | 6 ------ core/playlists/user.c | 10 ++++++---- gui/playlists/library.c | 10 +++++----- gui/playlists/user.c | 7 +++---- include/core/playlist.h | 2 +- include/core/playlists/type.h | 2 +- tests/core/.gitignore | 1 + tests/core/CMakeLists.txt | 2 +- tests/core/playlist.c | 34 ++++++++++++++++++++++++++++++++++ tests/core/playlists/artist.c | 2 +- tests/core/playlists/library.c | 8 +++++--- tests/core/playlists/system.c | 2 +- tests/core/playlists/user.c | 6 ++++-- 16 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 tests/core/playlist.c diff --git a/core/playlist.c b/core/playlist.c index 4af4999f..65a435ac 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -63,9 +63,11 @@ bool playlist_select(enum playlist_type_t type, const gchar *name) return true; } -bool playlist_new(enum playlist_type_t type, const gchar *name) +struct playlist *playlist_new(enum playlist_type_t type, const gchar *name) { - return playlist_types[type]->pl_new(name); + if (type < PL_MAX_TYPE && playlist_types[type]->pl_new) + return playlist_types[type]->pl_new(name); + return NULL; } bool playlist_delete(enum playlist_type_t type, const gchar *name) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index db0e852b..6c4d859c 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -117,7 +117,7 @@ static gchar *pl_artist_get_name(unsigned int id) return artist ? g_strdup(artist->ar_name) : NULL; } -static bool pl_artist_new_delete(const gchar *name) +static bool pl_artist_delete(const gchar *name) { return false; } @@ -161,8 +161,7 @@ struct playlist_type pl_artist = { .pl_get_id = pl_artist_get_id, .pl_get_name = pl_artist_get_name, .pl_can_select = pl_artist_can_select, - .pl_new = pl_artist_new_delete, - .pl_delete = pl_artist_new_delete, + .pl_delete = pl_artist_delete, .pl_add_track = pl_artist_add_rm, .pl_remove_track = pl_artist_add_rm, .pl_update = pl_artist_update, diff --git a/core/playlists/library.c b/core/playlists/library.c index d1ff522c..ebd965a7 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -208,18 +208,18 @@ static bool pl_library_can_select(const gchar *name) return playlist ? playlist_generic_can_select(playlist) : false; } -static bool pl_library_new(const gchar *name) +static struct playlist *pl_library_new(const gchar *name) { struct library *library; if (__lib_pl_lookup(name) || !g_file_test(name, G_FILE_TEST_IS_DIR)) - return false; + return NULL; library = library_find(name); library->li_playlist = __lib_pl_alloc(library); __lib_pl_scan_dir_idle(library, name); - return true; + return library->li_playlist; } static bool pl_library_delete(const gchar *name) diff --git a/core/playlists/system.c b/core/playlists/system.c index 05db9cd2..ce465281 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -510,11 +510,6 @@ static bool pl_system_can_select(const gchar *name) return sys_pl ? sys_pl->spl_can_select(&sys_pl->spl_playlist) : false; } -static bool pl_system_new(const gchar *name) -{ - return false; -} - static bool pl_system_delete(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); @@ -595,7 +590,6 @@ struct playlist_type pl_system = { .pl_get_id = pl_system_get_id, .pl_get_name = pl_system_get_name, .pl_can_select = pl_system_can_select, - .pl_new = pl_system_new, .pl_delete = pl_system_delete, .pl_add_track = pl_system_add_track, .pl_remove_track = pl_system_remove_track, diff --git a/core/playlists/user.c b/core/playlists/user.c index a1441847..0d7003c9 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -101,12 +101,14 @@ static bool pl_user_can_select(const gchar *name) return db_get(&user_db, name) != NULL; } -static bool pl_user_new(const gchar *name) +static struct playlist *pl_user_new(const gchar *name) { + struct db_entry *dbe; + if (db_get(&user_db, name)) - return false; - db_insert(&user_db, name); - return true; + return NULL; + dbe = db_insert(&user_db, name); + return dbe ? &USER_PLAYLIST(dbe)->pl_playlist : NULL; } static bool pl_user_delete(const gchar *name) diff --git a/gui/playlists/library.c b/gui/playlists/library.c index 9140a697..2d204c98 100644 --- a/gui/playlists/library.c +++ b/gui/playlists/library.c @@ -84,12 +84,12 @@ struct playlist *gui_pl_library_add(const gchar *filename) if (!__gui_pl_library_header(&iter)) return false; - if (!playlist_new(PL_LIBRARY, filename)) - return false; - playlist = playlist_get(PL_LIBRARY, filename); - gui_sidebar_iter_sort_child(&iter, playlist, "folder"); - gui_idle_enable(); + playlist = playlist_new(PL_LIBRARY, filename); + if (playlist) { + gui_sidebar_iter_sort_child(&iter, playlist, "folder"); + gui_idle_enable(); + } return playlist; } diff --git a/gui/playlists/user.c b/gui/playlists/user.c index 101b865d..020d5fce 100644 --- a/gui/playlists/user.c +++ b/gui/playlists/user.c @@ -50,11 +50,10 @@ struct playlist *gui_pl_user_add(const gchar *name) if (!__gui_pl_user_header(&iter)) return NULL; - if (!playlist_new(PL_USER, name)) - return NULL; - playlist = playlist_get(PL_USER, name); - gui_sidebar_iter_sort_child(&iter, playlist, "text-x-generic"); + playlist = playlist_new(PL_USER, name); + if (playlist) + gui_sidebar_iter_sort_child(&iter, playlist, "text-x-generic"); return playlist; } diff --git a/include/core/playlist.h b/include/core/playlist.h index 4cc2c926..e039a6c9 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -29,7 +29,7 @@ bool playlist_select(enum playlist_type_t, const gchar *); /* Called to create a new playlist. */ -bool playlist_new(enum playlist_type_t, const gchar *); +struct playlist *playlist_new(enum playlist_type_t, const gchar *); /* Called to delete a playlist. */ bool playlist_delete(enum playlist_type_t, const gchar *); diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 908325ea..b2b9bb37 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -46,7 +46,7 @@ struct playlist_type { bool (*pl_can_select)(const gchar *); /* Called to create a new playlist. */ - bool (*pl_new)(const gchar *); + struct playlist *(*pl_new)(const gchar *); /* Called to delete a playlist. */ bool (*pl_delete)(const gchar *); diff --git a/tests/core/.gitignore b/tests/core/.gitignore index 1e068d95..7134f6b1 100644 --- a/tests/core/.gitignore +++ b/tests/core/.gitignore @@ -6,4 +6,5 @@ idle settings database queue +playlist audio diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index b42e7e81..fe4d274f 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -14,6 +14,6 @@ core_unit_test(Database) add_subdirectory(tags/) core_unit_test(Queue) - +core_unit_test(Playlist) add_subdirectory(playlists/) core_unit_test(Audio) diff --git a/tests/core/playlist.c b/tests/core/playlist.c new file mode 100644 index 00000000..9e40719b --- /dev/null +++ b/tests/core/playlist.c @@ -0,0 +1,34 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include + +static void test_null() +{ + g_assert_null(playlist_new(PL_MAX_TYPE, "NULL")); + g_assert_null(playlist_new(PL_MAX_TYPE, NULL)); +} + +int main(int argc, char **argv) +{ + int ret; + + idle_init_sync(); + settings_init(); + tags_init(); + playlist_init(NULL); + while (idle_run_task()) {}; + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Core/Playlist/NULL", test_null); + ret = g_test_run(); + + playlist_deinit(); + tags_deinit(); + settings_deinit(); + idle_deinit(); + return ret; +} diff --git a/tests/core/playlists/artist.c b/tests/core/playlists/artist.c index b0ce9879..28e3926a 100644 --- a/tests/core/playlists/artist.c +++ b/tests/core/playlists/artist.c @@ -14,7 +14,7 @@ void test_artist() { struct artist *artist; - g_assert_false(playlist_new(PL_ARTIST, "Koji Kondo")); + g_assert_null(playlist_new(PL_ARTIST, "Koji Kondo")); g_assert_null(playlist_get_queue(PL_ARTIST, "Koji Kondo")); artist = artist_find("Koji Kondo"); diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index 8c3a1440..5c6c3051 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -13,12 +13,14 @@ void test_library() struct playlist *playlist; struct library *library; - g_assert_false(playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg")); + g_assert_null(playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg")); g_assert_null(playlist_get_queue(PL_LIBRARY, "tests/Music")); g_assert_false(playlist_select(PL_LIBRARY, "tests/Music")); - g_assert_true(playlist_new(PL_LIBRARY, "tests/Music")); - g_assert_false(playlist_new(PL_LIBRARY, "tests/Music")); + + playlist = playlist_new(PL_LIBRARY, "tests/Music"); + g_assert_nonnull(playlist); + g_assert_null(playlist_new(PL_LIBRARY, "tests/Music")); g_assert_nonnull(playlist_get_queue(PL_LIBRARY, "tests/Music")); g_assert_cmpuint(playlist_get_id(PL_LIBRARY, "tests/Music"), ==, 0); diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index 7cac542b..7120f94e 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -104,6 +104,7 @@ static void test_init() pl_system_new_track(track_get(0)); pl_system_new_track(track_get(1)); + g_assert_null(playlist_new(PL_SYSTEM, "New Playlist")); g_assert_cmpuint(playlist_size(PL_SYSTEM, "Favorites"), ==, 0); g_assert_cmpuint(playlist_size(PL_SYSTEM, "Hidden"), ==, 0); g_assert_cmpuint(playlist_size(PL_SYSTEM, "Queued"), ==, 0); @@ -125,7 +126,6 @@ static void test_invalid() SYS_PL_NUM_PLAYLISTS); g_assert_null(playlist_get_name(PL_SYSTEM, SYS_PL_NUM_PLAYLISTS)); - g_assert_false(playlist_new(PL_SYSTEM, "New Playlist")); g_assert_false(playlist_delete(PL_SYSTEM, "Favorites")); __test_playlist_noselect(NULL); diff --git a/tests/core/playlists/user.c b/tests/core/playlists/user.c index bbfb9710..930efbca 100644 --- a/tests/core/playlists/user.c +++ b/tests/core/playlists/user.c @@ -12,10 +12,12 @@ void test_user() { struct database *db = pl_user_db_get(); + struct playlist *playlist; g_assert_cmpuint(db->db_size, ==, 0); - g_assert_true( playlist_new(PL_USER, "Test Playlist")); - g_assert_false(playlist_new(PL_USER, "Test Playlist")); + playlist = playlist_new(PL_USER, "Test Playlist"); + g_assert_nonnull(playlist); + g_assert_null(playlist_new(PL_USER, "Test Playlist")); g_assert_cmpuint(db->db_size, ==, 1); g_assert_false(playlist_get_random(PL_USER, "Test Playlist"));