From e773ae6f82cce13c2f02c87354bdf4c03adce214 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 1 Oct 2016 10:38:23 -0400 Subject: [PATCH] core/playlists/user: Add support for renaming playlists Users may make typos in naming playlists, or they might think up better names for playlists later. Let's be friendly and give users a way to change playlist names if desired. Signed-off-by: Anna Schumaker --- core/playlists/user.c | 19 +++++++++++++++++++ include/core/playlists/user.h | 3 +++ tests/core/playlists/user.c | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/core/playlists/user.c b/core/playlists/user.c index b13a6304..2408abc9 100644 --- a/core/playlists/user.c +++ b/core/playlists/user.c @@ -156,3 +156,22 @@ void pl_user_delete_track(struct track *track) playlist_generic_remove(playlist, track); } } + +bool pl_user_rename(struct playlist *playlist, const gchar *name) +{ + struct db_entry *dbe; + + if (!playlist || db_get(&user_db, name)) + return false; + + dbe = db_get(&user_db, playlist->pl_name); + if (!dbe) + return false; + + g_free(playlist->pl_name); + playlist->pl_name = g_strdup(name); + db_rekey(&user_db, dbe); + + pl_user_save(); + return true; +} diff --git a/include/core/playlists/user.h b/include/core/playlists/user.h index 266a1771..d22c4fa1 100644 --- a/include/core/playlists/user.h +++ b/include/core/playlists/user.h @@ -26,5 +26,8 @@ void pl_user_deinit(); /* Called to tell user playlists that a track is getting deleted. */ void pl_user_delete_track(struct track *); +/* Called to rename a user playlist. */ +bool pl_user_rename(struct playlist *, const gchar *); + struct database *pl_user_db_get(); #endif /* OCARINA_CORE_PLAYLISTS_USER_H */ diff --git a/tests/core/playlists/user.c b/tests/core/playlists/user.c index 72736301..f68adda9 100644 --- a/tests/core/playlists/user.c +++ b/tests/core/playlists/user.c @@ -62,6 +62,14 @@ void test_user() playlist = playlist_lookup(PL_USER, "Test Playlist"); g_assert_nonnull(playlist); + g_assert_false(pl_user_rename(NULL, "Null Playlist")); + g_assert_true( pl_user_rename(playlist, "New Playlist Name")); + g_assert_null(playlist_lookup(PL_USER, "Test Playlist")); + g_assert_cmpstr(playlist->pl_name, ==, "New Playlist Name"); + + g_assert_nonnull(playlist_new(PL_USER, "Test Playlist")); + g_assert_false(pl_user_rename(playlist, "Test Playlist")); + g_assert_cmpuint(playlist_size(playlist), ==, 1); g_assert_true( playlist_has( playlist, track_get(0))); g_assert_true( playlist_remove(playlist, track_get(0))); @@ -69,6 +77,8 @@ void test_user() g_assert_false(playlist_has( playlist, track_get(0))); g_assert_cmpuint(playlist_size(playlist), ==, 0); + g_assert_true(playlist_delete(playlist)); + playlist = playlist_lookup(PL_USER, "Test Playlist"); g_assert_true(playlist_delete(playlist)); g_assert_cmpuint(db->db_size, ==, 0); g_assert_cmpuint(db_actual_size(db), ==, 0);