2016-05-09 07:52:10 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/idle.h>
|
2016-08-28 09:46:16 -04:00
|
|
|
#include <core/playlists/artist.h>
|
2016-05-09 07:52:10 -04:00
|
|
|
#include <core/playlists/library.h>
|
2016-05-09 11:24:53 -04:00
|
|
|
#include <core/playlists/system.h>
|
2016-05-10 08:08:42 -04:00
|
|
|
#include <unistd.h>
|
2016-05-09 11:24:53 -04:00
|
|
|
|
|
|
|
struct scan_data {
|
|
|
|
struct library *sd_library;
|
|
|
|
gchar *sd_path;
|
|
|
|
};
|
2016-05-09 07:52:10 -04:00
|
|
|
|
2016-05-09 11:24:53 -04:00
|
|
|
static bool __lib_pl_scan_dir(void *);
|
2016-05-09 07:52:10 -04:00
|
|
|
static struct queue_ops *lib_ops = NULL;
|
|
|
|
|
|
|
|
|
2016-09-01 11:54:28 -04:00
|
|
|
static struct playlist *__lib_pl_alloc(struct library *library)
|
2016-05-09 07:52:10 -04:00
|
|
|
{
|
|
|
|
struct playlist *playlist = g_malloc(sizeof(struct playlist));
|
|
|
|
|
2016-09-01 11:54:28 -04:00
|
|
|
playlist->pl_name = library->li_path;
|
2016-05-09 07:52:10 -04:00
|
|
|
playlist->pl_type = PL_LIBRARY;
|
2016-05-24 11:55:22 -04:00
|
|
|
playlist_generic_init(playlist, Q_REPEAT, lib_ops);
|
2016-05-09 07:52:10 -04:00
|
|
|
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __lib_pl_free(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
if (playlist) {
|
|
|
|
queue_deinit(&playlist->pl_queue);
|
|
|
|
g_free(playlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 08:04:59 -04:00
|
|
|
static bool __lib_pl_load(void *data)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = (struct playlist *)data;
|
|
|
|
struct library *library = library_lookup(playlist->pl_name);
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
|
|
|
|
queue_set_flag(&playlist->pl_queue, Q_ADD_FRONT);
|
|
|
|
db_for_each(dbe, next, track_db_get()) {
|
|
|
|
if (TRACK(dbe)->tr_library == library)
|
|
|
|
queue_add(&playlist->pl_queue, TRACK(dbe));
|
|
|
|
}
|
|
|
|
queue_unset_flag(&playlist->pl_queue, Q_ADD_FRONT);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-09 09:16:08 -04:00
|
|
|
static struct playlist *__lib_pl_lookup(const gchar *name)
|
|
|
|
{
|
|
|
|
struct library *library = library_lookup(name);
|
|
|
|
return library ? library->li_playlist : NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-09 11:24:53 -04:00
|
|
|
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);
|
2016-08-28 09:46:16 -04:00
|
|
|
pl_artist_new_track(track);
|
2016-05-09 11:24:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-10 08:08:42 -04:00
|
|
|
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));
|
2016-08-29 07:49:18 -04:00
|
|
|
pl_artist_delete_track(TRACK(dbe));
|
2016-05-10 08:08:42 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:52:10 -04:00
|
|
|
|
2016-05-09 08:18:40 -04:00
|
|
|
static struct queue *pl_library_get_queue(const gchar *name)
|
|
|
|
{
|
2016-05-09 09:16:08 -04:00
|
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
2016-05-09 08:18:40 -04:00
|
|
|
return playlist ? &playlist->pl_queue : NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-09 10:38:47 -04:00
|
|
|
static bool pl_library_new(const gchar *name)
|
|
|
|
{
|
|
|
|
struct library *library;
|
|
|
|
|
|
|
|
if (__lib_pl_lookup(name) || !g_file_test(name, G_FILE_TEST_IS_DIR))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
library = library_find(name);
|
2016-09-01 11:54:28 -04:00
|
|
|
library->li_playlist = __lib_pl_alloc(library);
|
2016-05-09 11:24:53 -04:00
|
|
|
|
|
|
|
__lib_pl_scan_dir_idle(library, name);
|
2016-05-09 10:38:47 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_library_delete(const gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
|
|
|
struct library *library = library_lookup(name);
|
|
|
|
struct queue_iter it;
|
|
|
|
|
|
|
|
if (!library)
|
|
|
|
return false;
|
|
|
|
|
2016-08-31 10:20:20 -04:00
|
|
|
queue_for_each(&playlist->pl_queue, &it) {
|
2016-05-09 10:38:47 -04:00
|
|
|
pl_system_delete_track(queue_iter_val(&it));
|
2016-08-31 10:20:20 -04:00
|
|
|
pl_artist_delete_track(queue_iter_val(&it));
|
|
|
|
}
|
2016-05-09 10:38:47 -04:00
|
|
|
__lib_pl_free(playlist);
|
|
|
|
|
|
|
|
track_remove_all(library);
|
|
|
|
library_remove(library);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-09 08:59:10 -04:00
|
|
|
static bool pl_library_add_rm(const gchar *name, struct track *track)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-10 08:08:42 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-09 09:16:08 -04:00
|
|
|
static void pl_library_set_flag(const gchar *name, enum queue_flags flag,
|
|
|
|
bool enabled)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
2016-05-24 07:48:11 -04:00
|
|
|
playlist_generic_set_flag(playlist, flag, enabled);
|
2016-05-09 09:16:08 -04:00
|
|
|
}
|
|
|
|
|
2016-05-09 09:23:41 -04:00
|
|
|
static void pl_library_sort(const gchar *name, enum compare_t sort, bool reset)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __lib_pl_lookup(name);
|
2016-05-24 07:33:40 -04:00
|
|
|
playlist_generic_sort(playlist, sort, reset);
|
2016-05-09 09:23:41 -04:00
|
|
|
}
|
|
|
|
|
2016-05-09 08:18:40 -04:00
|
|
|
|
|
|
|
struct playlist_type pl_library = {
|
|
|
|
.pl_get_queue = pl_library_get_queue,
|
2016-05-09 10:38:47 -04:00
|
|
|
.pl_new = pl_library_new,
|
|
|
|
.pl_delete = pl_library_delete,
|
2016-05-09 08:59:10 -04:00
|
|
|
.pl_add_track = pl_library_add_rm,
|
|
|
|
.pl_remove_track = pl_library_add_rm,
|
2016-05-10 08:08:42 -04:00
|
|
|
.pl_update = pl_library_update,
|
2016-05-09 09:16:08 -04:00
|
|
|
.pl_set_flag = pl_library_set_flag,
|
2016-05-09 09:23:41 -04:00
|
|
|
.pl_sort = pl_library_sort,
|
2016-05-09 08:18:40 -04:00
|
|
|
};
|
2016-05-09 07:52:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
static bool __lib_pl_init(void *data)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, library_db_get()) {
|
2016-09-01 11:54:28 -04:00
|
|
|
playlist = __lib_pl_alloc(LIBRARY(dbe));
|
2016-05-09 07:52:10 -04:00
|
|
|
LIBRARY(dbe)->li_playlist = playlist;
|
2016-05-09 08:04:59 -04:00
|
|
|
|
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_load, playlist);
|
2016-05-10 08:08:42 -04:00
|
|
|
pl_library_update(playlist->pl_name);
|
2016-05-09 07:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pl_library_init(struct queue_ops *ops)
|
|
|
|
{
|
|
|
|
lib_ops = ops;
|
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_init, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pl_library_deinit()
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, library_db_get()) {
|
|
|
|
playlist = LIBRARY(dbe)->li_playlist;
|
|
|
|
LIBRARY(dbe)->li_playlist = NULL;
|
|
|
|
|
|
|
|
__lib_pl_free(playlist);
|
|
|
|
}
|
|
|
|
}
|