core/playlists: Add functions for converting between names and ids

I need to have integer playlist ids to store into the settings database
once I allow changing the default playlist.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-07-28 16:03:47 -04:00
parent da19ddd388
commit bfcfaae977
11 changed files with 95 additions and 2 deletions

View File

@ -184,7 +184,7 @@ struct db_entry *db_next(const struct database *db, struct db_entry *ent)
return NULL;
}
struct db_entry *db_at(struct database *db, unsigned int index)
struct db_entry *db_at(const struct database *db, unsigned int index)
{
if (index >= db_actual_size(db))
return NULL;

View File

@ -105,3 +105,13 @@ struct queue *playlist_get_queue(enum playlist_type_t type, const gchar *name)
{
return playlist_types[type]->pl_get_queue(name);
}
unsigned int playlist_get_id(enum playlist_type_t type, const gchar *name)
{
return playlist_types[type]->pl_get_id(name);
}
gchar *playlist_get_name(enum playlist_type_t type, unsigned int id)
{
return playlist_types[type]->pl_get_name(id);
}

View File

@ -55,6 +55,18 @@ static struct queue *pl_artist_get_queue(const gchar *name)
return playlist ? &playlist->pl_queue : NULL;
}
static unsigned int pl_artist_get_id(const gchar *name)
{
struct artist *artist = artist_lookup(name);
return artist ? artist->ar_dbe.dbe_index : -1;
}
static gchar *pl_artist_get_name(unsigned int id)
{
struct artist *artist = ARTIST(db_at(artist_db_get(), id));
return artist ? g_strdup(artist->ar_name) : NULL;
}
static bool pl_artist_new_delete(const gchar *name)
{
return false;
@ -91,6 +103,8 @@ static struct track *pl_artist_next(const gchar *name)
struct playlist_type pl_artist = {
.pl_get_queue = pl_artist_get_queue,
.pl_get_id = pl_artist_get_id,
.pl_get_name = pl_artist_get_name,
.pl_new = pl_artist_new_delete,
.pl_delete = pl_artist_new_delete,
.pl_add_track = pl_artist_add_rm,

View File

@ -145,6 +145,18 @@ static struct queue *pl_library_get_queue(const gchar *name)
return playlist ? &playlist->pl_queue : NULL;
}
static unsigned int pl_library_get_id(const gchar *name)
{
struct library *library = library_find(name);
return library ? library->li_dbe.dbe_index : -1;
}
static gchar *pl_library_get_name(unsigned int id)
{
struct library *library = LIBRARY(db_at(library_db_get(), id));
return library ? g_strdup(library->li_path) : NULL;
}
static bool pl_library_new(const gchar *name)
{
struct library *library;
@ -213,6 +225,8 @@ static struct track *pl_library_next(const gchar *name)
struct playlist_type pl_library = {
.pl_get_queue = pl_library_get_queue,
.pl_get_id = pl_library_get_id,
.pl_get_name = pl_library_get_name,
.pl_new = pl_library_new,
.pl_delete = pl_library_delete,
.pl_add_track = pl_library_add_rm,

View File

@ -495,6 +495,24 @@ static struct queue *pl_system_get_queue(const gchar *name)
return sys_pl ? &sys_pl->spl_playlist.pl_queue : NULL;
}
static unsigned int pl_system_get_id(const gchar *name)
{
unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
if (string_match(name, sys_playlists[i]->spl_playlist.pl_name))
return i;
}
return SYS_PL_NUM_PLAYLISTS;
}
static gchar *pl_system_get_name(unsigned int id)
{
if (id < SYS_PL_NUM_PLAYLISTS)
return g_strdup(sys_playlists[id]->spl_playlist.pl_name);
return NULL;
}
static bool pl_system_new(const gchar *name)
{
return false;
@ -553,6 +571,8 @@ static struct track *pl_system_next(const gchar *name)
struct playlist_type pl_system = {
.pl_get_queue = pl_system_get_queue,
.pl_get_id = pl_system_get_id,
.pl_get_name = pl_system_get_name,
.pl_new = pl_system_new,
.pl_delete = pl_system_delete,
.pl_add_track = pl_system_add_track,

View File

@ -124,7 +124,7 @@ void db_remove(struct database *, struct db_entry *);
/* Returns the database item at the requested index. */
struct db_entry *db_at(struct database *, unsigned int);
struct db_entry *db_at(const struct database *, unsigned int);
/* Returns the database item with the specified key. */
struct db_entry *db_get(struct database *, const gchar *);

View File

@ -65,4 +65,10 @@ struct track *playlist_prev(void);
/* Called to access the playlist queue. */
struct queue *playlist_get_queue(enum playlist_type_t, const gchar *);
/* Called to convert a playlist name to an integer id. */
unsigned int playlist_get_id(enum playlist_type_t, const gchar *);
/* Called to convert a playlist id to a name. */
gchar *playlist_get_name(enum playlist_type_t, unsigned int);
#endif /* OCARINA_CORE_PLAYLIST_H */

View File

@ -31,6 +31,12 @@ struct playlist_type {
/* Called to get the queue for the playlist. */
struct queue *(*pl_get_queue)(const gchar *);
/* Called to convert a playlist name to an integer id. */
unsigned int (*pl_get_id)(const gchar *);
/* Called to convert a playlist id to a name. */
gchar *(*pl_get_name)(unsigned int);
/* Called to create a new playlist. */
bool (*pl_new)(const gchar *);

View File

@ -21,6 +21,8 @@ void test_artist()
g_assert_false(playlist_add(PL_ARTIST, "Koji Kondo", track_get(0)));
pl_artist_init(NULL);
g_assert_cmpuint(playlist_get_id(PL_ARTIST, "Koji Kondo"), ==, 0);
g_assert_cmpstr_free(playlist_get_name(PL_ARTIST, 0), ==, "Koji Kondo");
while (idle_run_task()) {};
g_assert_cmpuint(playlist_size(PL_ARTIST, "Koji Kondo"), ==, 2);

View File

@ -19,6 +19,9 @@ void test_library()
g_assert_false(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);
g_assert_cmpstr_free(playlist_get_name(PL_LIBRARY, 0), ==, "tests/Music");
library = library_get(0);
g_assert_nonnull(library);
g_assert_nonnull(library->li_playlist);

View File

@ -7,6 +7,10 @@
#include <tests/test.h>
#define __test_playlist_id(name, id) \
g_assert_cmpuint(playlist_get_id(PL_SYSTEM, name), ==, id); \
g_assert_cmpstr_free(playlist_get_name(PL_SYSTEM, id), ==, name)
#define __test_playlist_has(name, track, expected) \
if (expected) \
g_assert_true(playlist_has(PL_SYSTEM, name, track)); \
@ -81,6 +85,12 @@ static void test_invalid()
g_assert_null(playlist_get_queue(PL_SYSTEM, NULL));
g_assert_null(playlist_get_queue(PL_SYSTEM, "Invalid"));
g_assert_cmpuint(playlist_get_id(PL_SYSTEM, NULL), ==,
SYS_PL_NUM_PLAYLISTS);
g_assert_cmpuint(playlist_get_id(PL_SYSTEM, "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"));
@ -103,6 +113,7 @@ static void test_favorites()
g_assert_nonnull(queue);
g_assert_false(queue_has_flag(queue, Q_ADD_FRONT));
__test_playlist_id("Favorites", SYS_PL_FAVORITES);
__test_playlist_random("Favorites");
__test_playlist_add("Favorites");
__test_playlist_reinit("Favorites", 2, true, true);
@ -121,6 +132,7 @@ static void test_hidden()
g_assert(playlist_get_queue(PL_SYSTEM, "Banned") == queue);
g_assert_false(queue_has_flag(queue, Q_ADD_FRONT));
__test_playlist_id("Hidden", SYS_PL_HIDDEN);
__test_playlist_random("Hidden");
__test_playlist_add("Hidden");
__test_playlist_reinit("Hidden", 2, true, true);
@ -140,6 +152,7 @@ static void test_queued()
g_assert_false(queue_has_flag(queue, Q_NO_SORT));
g_assert_cmpuint(g_slist_length(queue->q_sort), ==, 0);
__test_playlist_id("Queued Tracks", SYS_PL_QUEUED);
__test_playlist_random("Queued Tracks");
__test_playlist_add("Queued Tracks");
__test_playlist_reinit("Queued Tracks", 2, true, true);
@ -168,6 +181,7 @@ static void test_collection()
g_assert_nonnull(queue);
g_assert_true(queue_has_flag(queue, Q_ADD_FRONT));
__test_playlist_id("Collection", SYS_PL_COLLECTION);
__test_playlist_random("Collection");
__test_playlist_add("Collection");
__test_playlist_hide_track("Collection", 1, false, true);
@ -186,6 +200,7 @@ static void test_history()
g_assert_nonnull(queue);
g_assert_true(queue_has_flag(queue, Q_ADD_FRONT));
g_assert_true(queue_has_flag(queue, Q_NO_SORT));
__test_playlist_id("History", SYS_PL_HISTORY);
g_assert_cmpuint(g_slist_length(queue->q_sort), ==, 0);
playlist_sort(PL_SYSTEM, "History", COMPARE_TRACK, true);
@ -224,6 +239,7 @@ static void test_unplayed()
g_assert_nonnull(queue);
g_assert_true(queue_has_flag(queue, Q_ADD_FRONT));
__test_playlist_id("Unplayed", SYS_PL_UNPLAYED);
__test_playlist_random("Unplayed");
__test_playlist_reinit("Unplayed", 2, true, true);
__test_playlist_remove("Unplayed");
@ -265,6 +281,7 @@ static void test_most_played()
g_assert_nonnull(most);
g_assert_true(queue_has_flag(most, Q_ADD_FRONT));
__test_playlist_id("Most Played", SYS_PL_MOST_PLAYED);
__test_playlist_random("Most Played");
__test_playlist_reinit("Most Played", 1, false, true);
@ -305,6 +322,7 @@ static void test_least_played()
g_assert_nonnull(least);
g_assert_true(queue_has_flag(least, Q_ADD_FRONT));
__test_playlist_id("Least Played", SYS_PL_LEAST_PLAYED);
__test_playlist_random("Least Played");
__test_playlist_reinit("Least Played", 1, false, true);