diff --git a/core/playlist.c b/core/playlist.c index cc38c22a..f78db712 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -115,13 +115,11 @@ bool playlist_select(struct playlist *playlist) current = playlist; settings_set(SETTINGS_CUR_TYPE, current->pl_type); - settings_set(SETTINGS_CUR_ID, playlist_get_id(current->pl_type, - current->pl_name)); + settings_set(SETTINGS_CUR_ID, current->pl_id); if (previous) { settings_set(SETTINGS_PREV_TYPE, previous->pl_type); - settings_set(SETTINGS_PREV_ID, - playlist_get_id(previous->pl_type, previous->pl_name)); + settings_set(SETTINGS_PREV_ID, previous->pl_id); } return true; } @@ -217,8 +215,3 @@ struct queue *playlist_get_queue(enum playlist_type_t type, const gchar *name) struct playlist *playlist = playlist_lookup(type, name); return playlist ? &playlist->pl_queue : NULL; } - -unsigned int playlist_get_id(enum playlist_type_t type, const gchar *name) -{ - return playlist_types[type]->pl_get_id(name); -} diff --git a/core/playlists/artist.c b/core/playlists/artist.c index c05e2813..41eb1535 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -15,12 +15,13 @@ static struct playlist_ops pl_artist_ops = { }; -static struct playlist *__artist_pl_alloc(gchar *name) +static struct playlist *__artist_pl_alloc(struct artist *artist) { struct playlist *playlist = g_malloc(sizeof(struct playlist)); - playlist->pl_name = name; + playlist->pl_name = artist->ar_name; playlist->pl_type = PL_ARTIST; + playlist->pl_id = artist_index(artist); playlist->pl_ops = &pl_artist_ops; playlist_generic_init(playlist, Q_REPEAT, artist_ops); @@ -112,12 +113,6 @@ static struct playlist *pl_artist_get(unsigned int id) return artist ? artist->ar_playlist : 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 void pl_artist_update(const gchar *name) { } @@ -135,7 +130,6 @@ struct playlist_type pl_artist = { .pl_save = pl_artist_save, .pl_lookup = pl_artist_lookup, .pl_get = pl_artist_get, - .pl_get_id = pl_artist_get_id, .pl_update = pl_artist_update, .pl_next = pl_artist_next, }; @@ -149,7 +143,7 @@ void pl_artist_init(struct queue_ops *ops) artist_ops = ops; db_for_each(dbe, next, artist_db_get()) { - playlist = __artist_pl_alloc(ARTIST(dbe)->ar_name); + playlist = __artist_pl_alloc(ARTIST(dbe)); ARTIST(dbe)->ar_playlist = playlist; idle_schedule(IDLE_SYNC, __artist_pl_add, playlist); @@ -177,7 +171,7 @@ void pl_artist_new_track(struct track *track) struct playlist *playlist = (struct playlist *)artist->ar_playlist; if (!playlist) { - playlist = __artist_pl_alloc(artist->ar_name); + playlist = __artist_pl_alloc(artist); artist->ar_playlist = playlist; } diff --git a/core/playlists/library.c b/core/playlists/library.c index 4401d599..b824d684 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -26,6 +26,7 @@ static struct playlist *__lib_pl_alloc(struct library *library) playlist->pl_name = library->li_path; playlist->pl_type = PL_LIBRARY; + playlist->pl_id = library_index(library); playlist->pl_ops = &pl_library_ops; playlist_generic_init(playlist, Q_REPEAT, lib_ops); @@ -229,12 +230,6 @@ static struct playlist *pl_library_get(unsigned int id) return library ? library->li_playlist : 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 struct playlist *pl_library_new(const gchar *name) { struct library *library; @@ -269,7 +264,6 @@ struct playlist_type pl_library = { .pl_save = pl_library_save, .pl_lookup = pl_library_lookup, .pl_get = pl_library_get, - .pl_get_id = pl_library_get_id, .pl_new = pl_library_new, .pl_update = pl_library_update, .pl_next = pl_library_next, diff --git a/core/playlists/system.c b/core/playlists/system.c index a4b15b5a..4ba2ea44 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -86,11 +86,12 @@ static struct playlist_ops favorites_ops = { }; static struct sys_playlist sys_favorites = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites", &favorites_ops), - .spl_init = playlist_generic_init, - .spl_save = sys_pl_save_full, - .spl_load = sys_pl_load_full, - .spl_next = playlist_generic_next, + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites", + SYS_PL_FAVORITES, &favorites_ops), + .spl_init = playlist_generic_init, + .spl_save = sys_pl_save_full, + .spl_load = sys_pl_load_full, + .spl_next = playlist_generic_next, }; @@ -144,11 +145,12 @@ static struct playlist_ops hidden_ops = { }; static struct sys_playlist sys_hidden = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden", &hidden_ops), - .spl_init = playlist_generic_init, - .spl_save = sys_pl_save_full, - .spl_load = sys_pl_load_full, - .spl_next = playlist_generic_next, + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden", + SYS_PL_HIDDEN, &hidden_ops), + .spl_init = playlist_generic_init, + .spl_save = sys_pl_save_full, + .spl_load = sys_pl_load_full, + .spl_next = playlist_generic_next, }; @@ -193,7 +195,8 @@ static struct playlist_ops queued_ops = { }; static struct sys_playlist sys_queued = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Queued Tracks", &queued_ops), + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Queued Tracks", + SYS_PL_QUEUED, &queued_ops), .spl_init = sys_pl_queued_init, .spl_save = sys_pl_save_full, .spl_load = sys_pl_load_full, @@ -232,7 +235,7 @@ static struct playlist_ops collection_ops = { static struct sys_playlist sys_collection = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection", - &collection_ops), + SYS_PL_COLLECTION, &collection_ops), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, @@ -262,7 +265,8 @@ static struct playlist_ops history_ops = { }; static struct sys_playlist sys_history = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History", &history_ops), + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History", + SYS_PL_HISTORY, &history_ops), .spl_init = sys_pl_history_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, @@ -288,7 +292,8 @@ static struct playlist_ops unplayed_ops = { }; static struct sys_playlist sys_unplayed = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed", &unplayed_ops), + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed", + SYS_PL_UNPLAYED, &unplayed_ops), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, @@ -317,7 +322,7 @@ static struct playlist_ops most_played_ops = { static struct sys_playlist sys_most_played = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played", - &most_played_ops), + SYS_PL_MOST_PLAYED, &most_played_ops), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, @@ -346,7 +351,7 @@ static struct playlist_ops least_played_ops = { static struct sys_playlist sys_least_played = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played", - &least_played_ops), + SYS_PL_LEAST_PLAYED, &least_played_ops), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, @@ -473,17 +478,6 @@ static struct playlist *pl_system_get(unsigned int id) return (id < SYS_PL_NUM_PLAYLISTS) ? &sys_playlists[id]->spl_playlist : 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, pl_system_get(i)->pl_name)) - return i; - } - return SYS_PL_NUM_PLAYLISTS; -} - static void pl_system_update(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); @@ -507,7 +501,6 @@ struct playlist_type pl_system = { .pl_save = pl_system_save, .pl_lookup = pl_system_lookup, .pl_get = pl_system_get, - .pl_get_id = pl_system_get_id, .pl_update = pl_system_update, .pl_next = pl_system_next, }; diff --git a/core/playlists/user.c b/core/playlists/user.c index 0d656b65..e58a1ca3 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -7,13 +7,14 @@ static struct queue_ops *user_pl_ops = NULL; static struct database user_db; static struct playlist_ops user_ops; -static struct user_playlist *__user_db_alloc(gchar *name) +static struct user_playlist *__user_db_alloc(gchar *name, unsigned int index) { struct user_playlist *playlist = g_malloc(sizeof(struct user_playlist)); dbe_init(&playlist->pl_dbe, playlist); playlist->pl_playlist.pl_name = name; playlist->pl_playlist.pl_type = PL_USER; + playlist->pl_playlist.pl_id = index; playlist->pl_playlist.pl_ops = &user_ops; playlist_generic_init(&playlist->pl_playlist, Q_REPEAT, user_pl_ops); @@ -22,7 +23,7 @@ static struct user_playlist *__user_db_alloc(gchar *name) static struct db_entry *user_db_alloc(const gchar *name, unsigned int index) { - return &__user_db_alloc(g_strdup(name))->pl_dbe; + return &__user_db_alloc(g_strdup(name), index)->pl_dbe; } static void user_db_free(struct db_entry *dbe) @@ -40,7 +41,7 @@ static gchar *user_db_key(struct db_entry *dbe) static struct db_entry *user_db_read(struct file *file, unsigned int index) { gchar *name = file_readl(file); - struct user_playlist *playlist = __user_db_alloc(name); + struct user_playlist *playlist = __user_db_alloc(name, index); queue_load_flags(&playlist->pl_playlist.pl_queue, file, true); queue_load_tracks(&playlist->pl_playlist.pl_queue, file); @@ -108,12 +109,6 @@ static struct playlist *pl_user_get(unsigned int id) return dbe ? &USER_PLAYLIST(dbe)->pl_playlist : NULL; } -static unsigned int pl_user_get_id(const gchar *name) -{ - struct db_entry *dbe = db_get(&user_db, name); - return dbe ? dbe->dbe_index : -1; -} - static struct playlist *pl_user_new(const gchar *name) { struct db_entry *dbe; @@ -141,7 +136,6 @@ struct playlist_type pl_user = { .pl_save = pl_user_save, .pl_lookup = pl_user_lookup, .pl_get = pl_user_get, - .pl_get_id = pl_user_get_id, .pl_new = pl_user_new, .pl_update = pl_user_update, .pl_next = pl_user_next, diff --git a/include/core/playlist.h b/include/core/playlist.h index fd46e065..1ce4505a 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -81,7 +81,4 @@ 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 *); - #endif /* OCARINA_CORE_PLAYLIST_H */ diff --git a/include/core/playlists/type.h b/include/core/playlists/type.h index 2ebcdf7b..ca02ca3c 100644 --- a/include/core/playlists/type.h +++ b/include/core/playlists/type.h @@ -44,6 +44,7 @@ struct playlist_ops { struct playlist { enum playlist_type_t pl_type; /* This playlist's type. */ gchar *pl_name; /* This playlist's name. */ + unsigned int pl_id; /* This playlist's identifier. */ void *pl_private; /* This playlist's private data. */ struct queue pl_queue; /* This playlist's queue of tracks. */ @@ -51,9 +52,10 @@ struct playlist { }; -#define DEFINE_PLAYLIST(type, name, ops) { \ +#define DEFINE_PLAYLIST(type, name, id, ops) { \ .pl_type = type, \ .pl_name = name, \ + .pl_id = id, \ .pl_ops = ops, \ } @@ -66,9 +68,6 @@ struct playlist_type { struct playlist *(*pl_lookup)(const gchar *); struct playlist *(*pl_get)(unsigned int); - /* Called to convert a playlist name to an integer id. */ - unsigned int (*pl_get_id)(const gchar *); - /* Called to create a new playlist. */ struct playlist *(*pl_new)(const gchar *); diff --git a/tests/core/playlists/artist.c b/tests/core/playlists/artist.c index 16bfe3c2..337e4879 100644 --- a/tests/core/playlists/artist.c +++ b/tests/core/playlists/artist.c @@ -26,12 +26,12 @@ void test_artist() pl_artist_deinit(); pl_artist_init(NULL); - g_assert_cmpuint(playlist_get_id(PL_ARTIST, "Koji Kondo"), ==, 0); while (idle_run_task()) {}; playlist = playlist_lookup(PL_ARTIST, "Koji Kondo"); g_assert_nonnull(playlist); + g_assert_cmpuint(playlist->pl_id, ==, artist_index(artist)); g_assert_cmpuint(playlist_size(playlist), ==, 2); g_assert_nonnull(artist->ar_playlist); g_assert_false(playlist_remove(playlist, track_get(0))); diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index 4f7490f7..598fbd64 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -23,12 +23,11 @@ void test_library() 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); - library = library_get(0); playlist = library->li_playlist; g_assert_nonnull(library); g_assert_nonnull(playlist); + g_assert_cmpuint(playlist->pl_id, ==, library_index(library)); g_assert_cmpuint(playlist_size(playlist), ==, 0); while (idle_run_task()) {}; diff --git a/tests/core/playlists/system.c b/tests/core/playlists/system.c index 0e8faf21..2320cb26 100644 --- a/tests/core/playlists/system.c +++ b/tests/core/playlists/system.c @@ -8,9 +8,6 @@ #include -#define __test_playlist_id(name, id) \ - g_assert_cmpuint(playlist_get_id(PL_SYSTEM, name), ==, id); \ - #define __test_playlist_has(name, track, expected) \ if (expected) \ g_assert_true(playlist_has(playlist_lookup(PL_SYSTEM, name), track)); \ @@ -98,6 +95,7 @@ static void test_init() playlist = playlist_get(PL_SYSTEM, i); g_assert_nonnull(playlist); g_assert(playlist_lookup(PL_SYSTEM, playlist->pl_name) == playlist); + g_assert_cmpuint(playlist->pl_id, ==, i); g_assert_false(playlist_select(playlist)); } @@ -110,6 +108,7 @@ static void test_init() g_assert_null(playlist_new(PL_SYSTEM, "New Playlist")); for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { playlist = playlist_get(PL_SYSTEM, i); + if (i == SYS_PL_COLLECTION || i == SYS_PL_UNPLAYED) g_assert_cmpuint(playlist_size(playlist), ==, 2); else @@ -121,12 +120,6 @@ 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); - playlist_update(PL_SYSTEM, NULL); } @@ -137,7 +130,6 @@ static void test_favorites() g_assert_nonnull(queue); g_assert_true(queue_has_flag(queue, Q_REPEAT)); - __test_playlist_id("Favorites", SYS_PL_FAVORITES); __test_playlist_add("Favorites"); __test_playlist_reinit("Favorites", 2, true, true); __test_playlist_update("Favorites", 2, true, true); @@ -152,7 +144,6 @@ static void test_hidden() g_assert_null(playlist_get_queue(PL_SYSTEM, "Banned")); g_assert_true(queue_has_flag(queue, Q_REPEAT)); - __test_playlist_id("Hidden", SYS_PL_HIDDEN); __test_playlist_add("Hidden"); __test_playlist_reinit("Hidden", 2, true, true); __test_playlist_update("Hidden", 2, true, true); @@ -167,7 +158,6 @@ static void test_queued() g_assert_false(queue_has_flag(queue, Q_REPEAT)); g_assert_cmpuint(g_slist_length(queue->q_sort), ==, 0); - __test_playlist_id("Queued Tracks", SYS_PL_QUEUED); __test_playlist_add("Queued Tracks"); __test_playlist_reinit("Queued Tracks", 2, true, true); @@ -196,7 +186,6 @@ static void test_collection() g_assert_nonnull(queue); g_assert_true(queue_has_flag(queue, Q_REPEAT)); - __test_playlist_id("Collection", SYS_PL_COLLECTION); __test_playlist_update("Collection", 2, true, true); __test_playlist_hide_track("Collection", 1, false, true); __test_playlist_reinit("Collection", 1, false, true); @@ -212,7 +201,6 @@ static void test_history() g_assert_nonnull(queue); g_assert_true(queue_has_flag(queue, Q_REPEAT)); - __test_playlist_id("History", SYS_PL_HISTORY); __test_playlist_state("History", 0, false, false); g_assert_true(playlist_add(__test_pl_history(), track_get(0))); @@ -241,7 +229,6 @@ static void test_unplayed() g_assert_nonnull(queue); g_assert_true(queue_has_flag(queue, Q_REPEAT)); - __test_playlist_id("Unplayed", SYS_PL_UNPLAYED); __test_playlist_reinit("Unplayed", 2, true, true); track_get(1)->tr_count = 1; @@ -278,7 +265,6 @@ static void test_most_played() g_assert_nonnull(most); g_assert_true(queue_has_flag(most, Q_REPEAT)); - __test_playlist_id("Most Played", SYS_PL_MOST_PLAYED); __test_playlist_reinit("Most Played", 1, false, true); track_get(0)->tr_count = 3; @@ -307,7 +293,6 @@ static void test_least_played() g_assert_nonnull(least); g_assert_true(queue_has_flag(least, Q_REPEAT)); - __test_playlist_id("Least Played", SYS_PL_LEAST_PLAYED); __test_playlist_reinit("Least Played", 1, false, true); track_get(0)->tr_count = 1; diff --git a/tests/core/playlists/user.c b/tests/core/playlists/user.c index d9ff272d..2f66c6d1 100644 --- a/tests/core/playlists/user.c +++ b/tests/core/playlists/user.c @@ -17,6 +17,8 @@ void test_user() g_assert_cmpuint(db->db_size, ==, 0); playlist = playlist_new(PL_USER, "Test Playlist"); g_assert_nonnull(playlist); + g_assert_cmpuint(playlist->pl_id, ==, 0); + g_assert_null(playlist_new(PL_USER, "Test Playlist")); g_assert_cmpuint(db->db_size, ==, 1); @@ -26,10 +28,6 @@ void test_user() playlist_set_random(playlist, false); g_assert_false(playlist_get_random(playlist)); - g_assert_cmpuint(playlist_get_id(PL_USER, "Test Playlist"), ==, 0); - g_assert_cmpuint(playlist_get_id(PL_USER, "No Playlist"), ==, - (unsigned int)-1); - g_assert(playlist_current() != playlist); g_assert_false(playlist_select(playlist)); g_assert(playlist_current() != playlist);