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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-09-09 08:35:52 -04:00
parent 846f7df9c1
commit ca5f0701e9
16 changed files with 72 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,4 +6,5 @@ idle
settings
database
queue
playlist
audio

View File

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

34
tests/core/playlist.c Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/playlist.h>
#include <core/settings.h>
#include <core/tags/tags.h>
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;
}

View File

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

View File

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

View File

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

View File

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