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-09-09 10:17:37 -04:00
|
|
|
#include <core/playlists/user.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 *);
|
2018-02-16 15:50:46 -05:00
|
|
|
static struct file lib_file = FILE_INIT_DATA("", "playlist.library", 0);
|
2016-05-09 07:52:10 -04:00
|
|
|
|
2016-09-09 10:17:37 -04:00
|
|
|
static struct playlist_ops pl_library_ops;
|
|
|
|
|
2016-05-09 07:52:10 -04:00
|
|
|
|
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
|
|
|
{
|
2016-09-22 11:32:40 -04:00
|
|
|
return playlist_generic_alloc(library->li_path, PL_LIBRARY,
|
2016-09-22 13:37:34 -04:00
|
|
|
library_index(library), &pl_library_ops);
|
2016-05-09 07:52:10 -04:00
|
|
|
}
|
|
|
|
|
2016-08-11 08:14:02 -04:00
|
|
|
static bool __lib_pl_add(void *data)
|
2016-05-09 08:04:59 -04:00
|
|
|
{
|
|
|
|
struct playlist *playlist = (struct playlist *)data;
|
|
|
|
struct library *library = library_lookup(playlist->pl_name);
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, track_db_get()) {
|
|
|
|
if (TRACK(dbe)->tr_library == library)
|
2016-09-21 09:36:44 -04:00
|
|
|
playlist_generic_add_front(playlist, TRACK(dbe));
|
2016-05-09 08:04:59 -04:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:33:09 -04:00
|
|
|
playlist_generic_resort(playlist);
|
2016-05-09 08:04:59 -04:00
|
|
|
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-08-11 08:14:02 -04:00
|
|
|
static bool __lib_pl_load(void *data)
|
|
|
|
{
|
|
|
|
struct playlist *playlist;
|
|
|
|
unsigned int i, n;
|
|
|
|
gchar *name;
|
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
if (!file_open(&lib_file, OPEN_READ))
|
2016-08-11 08:14:02 -04:00
|
|
|
return true;
|
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
n = file_readu(&lib_file);
|
2016-08-11 08:14:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-02-21 16:01:15 -05:00
|
|
|
name = file_readl(&lib_file);
|
2016-08-11 08:14:02 -04:00
|
|
|
playlist = __lib_pl_lookup(name);
|
2016-09-16 11:12:41 -04:00
|
|
|
if (playlist)
|
|
|
|
playlist_generic_load(playlist, &lib_file,
|
|
|
|
PL_SAVE_METADATA);
|
2016-09-29 14:48:43 -04:00
|
|
|
g_free(name);
|
2016-08-11 08:14:02 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:53:53 -05:00
|
|
|
file_close(&lib_file);
|
2016-08-11 08:14:02 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-09-21 10:44:06 -04:00
|
|
|
playlist_generic_add(playlist, track);
|
2016-05-09 11:24:53 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-09-28 17:01:38 -04:00
|
|
|
|
|
|
|
g_free(path);
|
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-09-20 14:38:46 -04:00
|
|
|
playlist_generic_remove(playlist, TRACK(dbe));
|
2016-05-10 08:08:42 -04:00
|
|
|
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-09-09 10:17:37 -04:00
|
|
|
static bool pl_library_delete(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
struct library *library = library_lookup(playlist->pl_name);
|
2016-09-22 10:54:08 -04:00
|
|
|
playlist_iter it;
|
2016-09-09 10:17:37 -04:00
|
|
|
|
|
|
|
if (!library)
|
|
|
|
return false;
|
|
|
|
|
2016-09-22 10:54:08 -04:00
|
|
|
playlist_for_each(playlist, it) {
|
|
|
|
pl_system_delete_track(playlist_iter_track(it));
|
|
|
|
pl_artist_delete_track(playlist_iter_track(it));
|
|
|
|
pl_user_delete_track(playlist_iter_track(it));
|
2016-09-09 10:17:37 -04:00
|
|
|
}
|
2016-09-22 11:32:40 -04:00
|
|
|
playlist_generic_free(playlist);
|
2016-09-09 10:17:37 -04:00
|
|
|
|
|
|
|
track_remove_all(library);
|
|
|
|
library_remove(library);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct playlist_ops pl_library_ops = {
|
2016-09-12 13:08:24 -04:00
|
|
|
.pl_can_select = playlist_generic_can_select,
|
|
|
|
.pl_delete = pl_library_delete,
|
2016-09-19 10:55:29 -04:00
|
|
|
.pl_set_random = playlist_generic_set_random,
|
2016-09-12 13:08:24 -04:00
|
|
|
.pl_sort = playlist_generic_sort,
|
2016-09-09 10:17:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-12 16:12:17 -04:00
|
|
|
static void pl_library_save(void)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
2018-02-20 15:38:28 -05:00
|
|
|
if (!file_open(&lib_file, OPEN_WRITE))
|
2016-08-12 16:12:17 -04:00
|
|
|
return;
|
|
|
|
|
2018-02-21 08:19:19 -05:00
|
|
|
file_writef(&lib_file, "%u\n", library_db_get()->db_size);
|
2016-08-12 16:12:17 -04:00
|
|
|
db_for_each(dbe, next, library_db_get()) {
|
|
|
|
playlist = LIBRARY(dbe)->li_playlist;
|
2018-02-21 08:19:19 -05:00
|
|
|
file_writef(&lib_file, "%s\n", playlist->pl_name);
|
2016-09-16 10:04:51 -04:00
|
|
|
playlist_generic_save(playlist, &lib_file, PL_SAVE_METADATA);
|
2016-08-12 16:12:17 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:53:53 -05:00
|
|
|
file_close(&lib_file);
|
2016-08-12 16:12:17 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 13:49:27 -04:00
|
|
|
static struct playlist *pl_library_lookup(const gchar *name)
|
2016-05-09 08:18:40 -04:00
|
|
|
{
|
2016-08-20 19:13:18 -04:00
|
|
|
return __lib_pl_lookup(name);
|
2016-05-09 08:18:40 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:59:46 -04:00
|
|
|
static struct playlist *pl_library_get(unsigned int id)
|
2016-07-28 16:03:47 -04:00
|
|
|
{
|
2016-09-12 10:59:46 -04:00
|
|
|
struct library *library = LIBRARY(db_at(library_db_get(), id));
|
|
|
|
return library ? library->li_playlist : NULL;
|
2016-07-28 16:03:47 -04:00
|
|
|
}
|
|
|
|
|
2016-09-09 08:35:52 -04:00
|
|
|
static struct playlist *pl_library_new(const gchar *name)
|
2016-05-09 10:38:47 -04:00
|
|
|
{
|
|
|
|
struct library *library;
|
|
|
|
|
|
|
|
if (__lib_pl_lookup(name) || !g_file_test(name, G_FILE_TEST_IS_DIR))
|
2016-09-09 08:35:52 -04:00
|
|
|
return NULL;
|
2016-05-09 10:38:47 -04:00
|
|
|
|
|
|
|
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-09-09 08:35:52 -04:00
|
|
|
return library->li_playlist;
|
2016-05-09 10:38:47 -04:00
|
|
|
}
|
|
|
|
|
2016-09-13 10:59:20 -04:00
|
|
|
static void pl_library_played(struct track *track)
|
|
|
|
{
|
2016-09-20 11:01:32 -04:00
|
|
|
struct library *library = track->tr_library;
|
|
|
|
playlist_generic_update(library->li_playlist, track);
|
2016-09-13 10:59:20 -04:00
|
|
|
}
|
|
|
|
|
2016-05-09 08:18:40 -04:00
|
|
|
|
|
|
|
struct playlist_type pl_library = {
|
2016-09-20 10:00:12 -04:00
|
|
|
.pl_save = pl_library_save,
|
|
|
|
.pl_lookup = pl_library_lookup,
|
|
|
|
.pl_get = pl_library_get,
|
|
|
|
.pl_new = pl_library_new,
|
|
|
|
.pl_played = pl_library_played,
|
|
|
|
.pl_selected = pl_library_played,
|
2016-05-09 08:18:40 -04:00
|
|
|
};
|
2016-05-09 07:52:10 -04:00
|
|
|
|
|
|
|
|
2016-09-22 13:37:34 -04:00
|
|
|
void pl_library_init(void)
|
2016-05-09 07:52:10 -04:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-09-13 11:10:34 -04:00
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_add, playlist);
|
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_update, playlist);
|
2016-05-09 07:52:10 -04:00
|
|
|
}
|
|
|
|
|
2016-08-11 08:14:02 -04:00
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_load, NULL);
|
2016-05-09 07:52:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pl_library_deinit()
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, library_db_get()) {
|
2016-09-22 11:32:40 -04:00
|
|
|
playlist_generic_free(LIBRARY(dbe)->li_playlist);
|
2016-05-09 07:52:10 -04:00
|
|
|
LIBRARY(dbe)->li_playlist = NULL;
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 11:10:34 -04:00
|
|
|
|
|
|
|
void pl_library_update(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
idle_schedule(IDLE_SYNC, __lib_pl_update, playlist);
|
|
|
|
}
|