diff --git a/core/playlists/library.c b/core/playlists/library.c index 78168c5a..65eb1a3e 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -5,6 +5,7 @@ #include #include #include +#include struct scan_data { struct library *sd_library; @@ -113,6 +114,31 @@ out: return true; } +static bool __lib_pl_update(void *data) +{ + struct playlist *playlist = (struct playlist *)data; + struct library *library = library_lookup(playlist->pl_name); + struct db_entry *dbe, *next; + gchar *path; + + db_for_each(dbe, next, track_db_get()) { + if (TRACK(dbe)->tr_library != library) + continue; + + path = track_path(TRACK(dbe)); + if (g_access(path, F_OK) < 0) { + pl_system_delete_track(TRACK(dbe)); + queue_remove_all(&playlist->pl_queue, TRACK(dbe)); + track_remove(TRACK(dbe)); + } + g_free(path); + } + + track_db_commit(); + __lib_pl_scan_dir_idle(library, library->li_path); + return true; +} + static struct queue *pl_library_get_queue(const gchar *name) { @@ -157,6 +183,13 @@ static bool pl_library_add_rm(const gchar *name, struct track *track) return false; } +static void pl_library_update(const gchar *name) +{ + struct playlist *playlist = __lib_pl_lookup(name); + if (playlist) + idle_schedule(IDLE_SYNC, __lib_pl_update, playlist); +} + static void pl_library_set_flag(const gchar *name, enum queue_flags flag, bool enabled) { @@ -181,6 +214,7 @@ struct playlist_type pl_library = { .pl_delete = pl_library_delete, .pl_add_track = pl_library_add_rm, .pl_remove_track = pl_library_add_rm, + .pl_update = pl_library_update, .pl_set_flag = pl_library_set_flag, .pl_sort = pl_library_sort, }; @@ -196,6 +230,7 @@ static bool __lib_pl_init(void *data) LIBRARY(dbe)->li_playlist = playlist; idle_schedule(IDLE_SYNC, __lib_pl_load, playlist); + pl_library_update(playlist->pl_name); } return true; diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index 51d1e60d..0dd5d685 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -77,6 +77,20 @@ void test_library() pl_library.pl_sort("tests/Music", COMPARE_TRACK, false); test_equal(g_slist_length(playlist->pl_queue.q_sort), 3); + g_rename("tests/Music/Hyrule Symphony/", "tests/Hyrule Symphony/"); + pl_library.pl_update("tests/Music"); + while (idle_run_task()) {} + test_equal(db_actual_size(track_db_get()), 48); + test_equal(track_db_get()->db_size, 35); + test_equal(queue_size(&playlist->pl_queue), 35); + + g_rename("tests/Hyrule Symphony", "tests/Music/Hyrule Symphony/"); + pl_library.pl_update("tests/Music"); + while (idle_run_task()) {} + test_equal(db_actual_size(track_db_get()), 61); + test_equal(track_db_get()->db_size, 48); + test_equal(queue_size(&playlist->pl_queue), 48); + 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);