From e1a722d04b294fa3f8152d2fc05cda96ad5e29d9 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 9 May 2016 11:24:53 -0400 Subject: [PATCH] core/playlists/library: Adding a library starts a directory scan Signed-off-by: Anna Schumaker --- core/playlists/library.c | 63 ++++++++++++++++++++++++++++++++++ tests/core/playlists/library.c | 18 ++++++---- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/core/playlists/library.c b/core/playlists/library.c index 3645bd26..78168c5a 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -4,7 +4,14 @@ #include #include #include +#include +struct scan_data { + struct library *sd_library; + gchar *sd_path; +}; + +static bool __lib_pl_scan_dir(void *); static struct queue_ops *lib_ops = NULL; @@ -52,6 +59,60 @@ static struct playlist *__lib_pl_lookup(const gchar *name) return library ? library->li_playlist : NULL; } +static void __lib_pl_scan_dir_idle(struct library *library, const gchar *path) +{ + struct scan_data *scan = g_malloc(sizeof(struct scan_data)); + + scan->sd_library = library; + scan->sd_path = g_strdup(path); + + /* scan data is freed by __lib_pl_scan_dir() */ + idle_schedule(IDLE_SYNC, __lib_pl_scan_dir, scan); +} + +static void __lib_pl_read_path(struct scan_data *scan, const gchar *name) +{ + gchar *path = g_build_filename(scan->sd_path, name, NULL); + struct playlist *playlist = scan->sd_library->li_playlist; + struct track *track; + + if (g_file_test(path, G_FILE_TEST_IS_DIR)) + __lib_pl_scan_dir_idle(scan->sd_library, path); + else { + track = track_add(scan->sd_library, path); + if (track) { + queue_add(&playlist->pl_queue, track); + pl_system_new_track(track); + } + } +} + +static bool __lib_pl_scan_dir(void *data) +{ + struct scan_data *scan = (struct scan_data *)data; + const gchar *name; + GDir *dir; + + dir = g_dir_open(scan->sd_path, 0, NULL); + if (!dir) + goto out; + + name = g_dir_read_name(dir); + while (name != NULL) { + __lib_pl_read_path(scan, name); + name = g_dir_read_name(dir); + } + + g_dir_close(dir); + track_db_commit(); + +out: + /* Allocated by __lib_pl_scan_dir_idle() */ + g_free(scan->sd_path); + g_free(scan); + return true; +} + static struct queue *pl_library_get_queue(const gchar *name) { @@ -68,6 +129,8 @@ static bool pl_library_new(const gchar *name) library = library_find(name); library->li_playlist = __lib_pl_alloc(name); + + __lib_pl_scan_dir_idle(library, name); return true; } diff --git a/tests/core/playlists/library.c b/tests/core/playlists/library.c index c3949da5..51d1e60d 100644 --- a/tests/core/playlists/library.c +++ b/tests/core/playlists/library.c @@ -28,12 +28,16 @@ void test_library() test_equal(pl_library.pl_new("tests/Music"), (bool)false); test_not_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL); - library = library_get(0); - test_not_equal((void *)library, NULL); - test_not_equal((void *)library->li_playlist, NULL); + library = library_get(0); + playlist = library->li_playlist; + test_not_equal((void *)library, NULL); + test_not_equal((void *)playlist, NULL); - track_add(library, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg"); - track_add(library, "tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg"); + test_equal(queue_size(&playlist->pl_queue), 0); + while (idle_run_task()) {}; + test_equal(queue_size(&playlist->pl_queue), 48); + test_equal(queue_size(pl_system.pl_get_queue("Unplayed")), 48); + test_equal(queue_size(pl_system.pl_get_queue("Collection")), 48); test_equal(pl_library.pl_add_track("tests/Music", track_get(0)), (bool)false); test_equal(pl_library.pl_add_track("tests/Music", track_get(1)), (bool)false); @@ -49,7 +53,7 @@ void test_library() test_not_equal(library->li_playlist, NULL); test_not_equal((void *)pl_library.pl_get_queue("tests/Music"), NULL); - test_equal(queue_size(&playlist->pl_queue), 2); + test_equal(queue_size(&playlist->pl_queue), 48); test_equal((void *)pl_library.pl_get_queue("tests/Music"), (void *)&playlist->pl_queue); @@ -57,7 +61,7 @@ void test_library() test_equal(pl_library.pl_add_track("tests/Music", track_get(1)), (bool)false); test_equal(pl_library.pl_remove_track("tests/Music", track_get(0)), (bool)false); test_equal(pl_library.pl_remove_track("tests/Music", track_get(1)), (bool)false); - test_equal(queue_size(&playlist->pl_queue), 2); + test_equal(queue_size(&playlist->pl_queue), 48); test_equal(queue_has_flag(&playlist->pl_queue, Q_RANDOM), (bool)false); pl_library.pl_set_flag("tests/Music", Q_RANDOM, true);