core/playlists/library: Adding a library starts a directory scan

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-05-09 11:24:53 -04:00 committed by Anna Schumaker
parent c448db2665
commit e1a722d04b
2 changed files with 74 additions and 7 deletions

View File

@ -4,7 +4,14 @@
#include <core/idle.h>
#include <core/playlists/system.h>
#include <core/playlists/library.h>
#include <core/playlists/system.h>
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;
}

View File

@ -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);