core/playlists: Add playlist_{new, delete}() functions

System playlists cannot be created or deleted, so these functions simply
return false in this case.  Library playlists will use this to add new
library paths to Ocarina.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-05-09 10:38:47 -04:00 committed by Anna Schumaker
parent 78aa0f9ff6
commit c448db2665
7 changed files with 91 additions and 5 deletions

View File

@ -23,6 +23,16 @@ void playlist_deinit()
pl_library_deinit();
}
bool playlist_new(enum playlist_type_t type, const gchar *name)
{
return playlist_types[type]->pl_new(name);
}
bool playlist_delete(enum playlist_type_t type, const gchar *name)
{
return playlist_types[type]->pl_delete(name);
}
bool playlist_add(enum playlist_type_t type, const gchar *name,
struct track *track)
{

View File

@ -2,6 +2,7 @@
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/playlists/system.h>
#include <core/playlists/library.h>
static struct queue_ops *lib_ops = NULL;
@ -58,6 +59,36 @@ static struct queue *pl_library_get_queue(const gchar *name)
return playlist ? &playlist->pl_queue : NULL;
}
static bool 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;
library = library_find(name);
library->li_playlist = __lib_pl_alloc(name);
return true;
}
static bool pl_library_delete(const gchar *name)
{
struct playlist *playlist = __lib_pl_lookup(name);
struct library *library = library_lookup(name);
struct queue_iter it;
if (!library)
return false;
queue_for_each(&playlist->pl_queue, &it)
pl_system_delete_track(queue_iter_val(&it));
__lib_pl_free(playlist);
track_remove_all(library);
library_remove(library);
return true;
}
static bool pl_library_add_rm(const gchar *name, struct track *track)
{
return false;
@ -83,6 +114,8 @@ static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset)
struct playlist_type pl_library = {
.pl_get_queue = pl_library_get_queue,
.pl_new = pl_library_new,
.pl_delete = pl_library_delete,
.pl_add_track = pl_library_add_rm,
.pl_remove_track = pl_library_add_rm,
.pl_set_flag = pl_library_set_flag,

View File

@ -206,6 +206,11 @@ static struct queue *pl_system_get_queue(const gchar *name)
return __sys_pl_queue(plist);
}
static bool pl_system_new_delete(const gchar *name)
{
return false;
}
static bool pl_system_add_track(const gchar *name, struct track *track)
{
enum sys_playlist_t plist = __sys_pl_convert(name);
@ -258,6 +263,8 @@ static void pl_system_sort(const gchar *name, enum compare_t sort, bool reset)
struct playlist_type pl_system = {
.pl_get_queue = pl_system_get_queue,
.pl_new = pl_system_new_delete,
.pl_delete = pl_system_new_delete,
.pl_add_track = pl_system_add_track,
.pl_remove_track = pl_system_remove_track,
.pl_update = pl_system_update,

View File

@ -19,6 +19,13 @@ void playlist_init(struct queue_ops *);
void playlist_deinit();
/* Called to create a new playlist. */
bool playlist_new(enum playlist_type_t, const gchar *);
/* Called to delete a playlist. */
bool playlist_delete(enum playlist_type_t, const gchar *);
/* Called to add a track to a playlist. */
bool playlist_add(enum playlist_type_t, const gchar *, struct track *);

View File

@ -30,6 +30,12 @@ struct playlist_type {
/* Called to get the queue for the playlist. */
struct queue *(*pl_get_queue)(const gchar *);
/* Called to create a new playlist. */
bool (*pl_new)(const gchar *);
/* Called to delete a playlist. */
bool (*pl_delete)(const gchar *);
/* Called to add a track to the playlist. */
bool (*pl_add_track)(const gchar *, struct track *);

View File

@ -4,6 +4,7 @@
#include <core/filter.h>
#include <core/idle.h>
#include <core/playlists/library.h>
#include <core/playlists/system.h>
#include <core/tags/library.h>
#include <core/tags/tags.h>
#include <tests/test.h>
@ -16,19 +17,32 @@ void test_library()
idle_init_sync();
filter_init();
tags_init();
pl_system_init(NULL);
pl_library_init(NULL);
while (idle_run_task()) {};
library = library_find("tests/Music");
test_equal(pl_library.pl_new("tests/Music/Hyrule Symphony/01 - Title Theme.ogg"), (bool)false);
test_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL);
test_equal(pl_library.pl_new("tests/Music"), (bool)true);
test_equal(pl_library.pl_new("tests/Music"), (bool)false);
test_not_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL);
library = library_get(0);
test_not_equal((void *)library, NULL);
test_not_equal((void *)library->li_playlist, NULL);
track_add(library, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg");
track_add(library, "tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg");
test_equal(library->li_playlist, NULL);
test_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL);
test_equal(pl_library.pl_add_track("tests/Music", track_get(0)), (bool)false);
test_equal(pl_library.pl_add_track("tests/Music", track_get(1)), (bool)false);
test_equal(pl_library.pl_remove_track("tests/Music", track_get(0)), (bool)false);
test_equal(pl_library.pl_remove_track("tests/Music", track_get(1)), (bool)false);
pl_library_deinit();
test_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL);
test_equal((void *)library->li_playlist, NULL);
pl_library_init(NULL);
while (idle_run_task()) {};
playlist = (struct playlist *)library->li_playlist;
@ -59,9 +73,15 @@ void test_library()
pl_library.pl_sort("tests/Music", COMPARE_TRACK, false);
test_equal(g_slist_length(playlist->pl_queue.q_sort), 3);
pl_library_deinit();
test_equal(library->li_playlist, NULL);
test_equal(pl_library.pl_delete("tests/Music"), (bool)true);
test_equal(pl_library.pl_delete("tests/Music"), (bool)false);
test_equal((void *)library_get(0), NULL);
test_equal(queue_size(pl_system.pl_get_queue("Unplayed")), 0);
test_equal(queue_size(pl_system.pl_get_queue("Collection")), 0);
test_equal(track_db_get()->db_size, 0);
pl_library_deinit();
pl_system_deinit();
tags_deinit();
filter_deinit();
idle_deinit();

View File

@ -79,6 +79,9 @@ static void test_invalid()
test_equal((void *)pl_system.pl_get_queue(NULL), NULL);
test_equal((void *)pl_system.pl_get_queue("Invalid"), NULL);
test_equal(pl_system.pl_new("New Playlist"), (bool)false);
test_equal(pl_system.pl_delete("Favorites"), (bool)false);
test_equal(pl_system.pl_add_track(NULL, track_get(0)), (bool)false);
test_equal(pl_system.pl_remove_track(NULL, track_get(0)), (bool)false);
}