From bfcfaae977b73fc39aca1787bb3545814230261b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 28 Jul 2016 16:03:47 -0400 Subject: [PATCH] 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 --- core/database.c | 2 +- core/playlist.c | 10 ++++++++++ core/playlists/artist.c | 14 ++++++++++++++ core/playlists/library.c | 14 ++++++++++++++ core/playlists/system.c | 20 ++++++++++++++++++++ include/core/database.h | 2 +- include/core/playlist.h | 6 ++++++ include/core/playlists/type.h | 6 ++++++ tests/core/playlists/artist.c | 2 ++ tests/core/playlists/library.c | 3 +++ tests/core/playlists/system.c | 18 ++++++++++++++++++ 11 files changed, 95 insertions(+), 2 deletions(-) diff --git a/core/database.c b/core/database.c index b83709d8..543c1838 100644 --- a/core/database.c +++ b/core/database.c @@ -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; diff --git a/core/playlist.c b/core/playlist.c index 53ed332b..9d6cbb14 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -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); +} diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 466470b9..e37d5938 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -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, diff --git a/core/playlists/library.c b/core/playlists/library.c index ad264c6e..84ef361c 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -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, diff --git a/core/playlists/system.c b/core/playlists/system.c index c6ec638a..df82e415 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -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, diff --git a/include/core/database.h b/include/core/database.h index 545245b8..b12d7f40 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -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 *); diff --git a/include/core/playlist.h b/include/core/playlist.h index 50977904..afda8b68 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -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 */ diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 6b1c9cbb..af113042 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -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 *); diff --git a/tests/core/playlists/artist.c b/tests/core/playlists/artist.c index 7e46e042..88cc1a4e 100644 --- a/tests/core/playlists/artist.c +++ b/tests/core/playlists/artist.c @@ -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); diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index d4d62e6d..065a0b2c 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -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); diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index 868f55f8..e24f0404 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -7,6 +7,10 @@ #include +#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);