2016-05-15 10:19:31 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/idle.h>
|
|
|
|
#include <core/playlists/artist.h>
|
2016-08-09 08:28:02 -04:00
|
|
|
#include <core/string.h>
|
2016-05-15 10:19:31 -04:00
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
static struct queue_ops *artist_ops = NULL;
|
2016-08-17 14:17:52 -04:00
|
|
|
static struct file artist_file = FILE_INIT("playlist.artist", 0);
|
2016-05-15 10:19:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
static struct playlist *__artist_pl_alloc(gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = g_malloc(sizeof(struct playlist));
|
|
|
|
|
|
|
|
playlist->pl_name = name;
|
|
|
|
playlist->pl_type = PL_ARTIST;
|
2016-05-24 11:55:22 -04:00
|
|
|
playlist_generic_init(playlist, Q_REPEAT, artist_ops);
|
2016-05-15 10:19:31 -04:00
|
|
|
|
|
|
|
return playlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __artist_pl_free(struct playlist *playlist)
|
|
|
|
{
|
|
|
|
if (playlist) {
|
|
|
|
queue_deinit(&playlist->pl_queue);
|
|
|
|
g_free(playlist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
static bool __artist_pl_add(void *data)
|
2016-05-15 10:32:58 -04:00
|
|
|
{
|
2016-08-13 09:38:28 -04:00
|
|
|
struct playlist *playlist = (struct playlist *)data;
|
|
|
|
struct artist *artist = artist_lookup(playlist->pl_name);
|
2016-05-15 10:32:58 -04:00
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
|
2016-08-13 09:38:28 -04:00
|
|
|
queue_set_flag(&playlist->pl_queue, Q_ADD_FRONT);
|
2016-05-15 10:32:58 -04:00
|
|
|
db_for_each(dbe, next, track_db_get()) {
|
2016-08-29 11:19:58 -04:00
|
|
|
if (TRACK(dbe)->tr_album->al_artist == artist)
|
2016-08-13 09:38:28 -04:00
|
|
|
queue_add(&playlist->pl_queue, TRACK(dbe));
|
2016-05-15 10:32:58 -04:00
|
|
|
}
|
2016-08-13 09:38:28 -04:00
|
|
|
queue_unset_flag(&playlist->pl_queue, Q_ADD_FRONT);
|
2016-05-15 10:32:58 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-24 07:33:40 -04:00
|
|
|
static struct playlist *__artist_pl_lookup(const gchar *name)
|
|
|
|
{
|
|
|
|
struct artist *artist = artist_lookup(name);
|
|
|
|
return artist ? artist->ar_playlist : NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
static bool __artist_pl_load(void *data)
|
|
|
|
{
|
|
|
|
struct playlist *playlist;
|
|
|
|
unsigned int i, n;
|
|
|
|
gchar *name;
|
|
|
|
|
|
|
|
if (!file_open(&artist_file, OPEN_READ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
file_readf(&artist_file, "%u\n", &n);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
name = file_readl(&artist_file);
|
|
|
|
playlist = __artist_pl_lookup(name);
|
2016-09-29 14:48:43 -04:00
|
|
|
g_free(name);
|
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
if (!playlist)
|
|
|
|
continue;
|
|
|
|
queue_load_flags(&playlist->pl_queue, &artist_file, true);
|
|
|
|
queue_iter_set(&playlist->pl_queue, &playlist->pl_queue.q_cur,
|
|
|
|
playlist->pl_queue.q_cur.it_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_close(&artist_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-15 10:19:31 -04:00
|
|
|
|
2016-08-12 16:12:17 -04:00
|
|
|
static void pl_artist_save(void)
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
|
|
|
if (!file_open(&artist_file, OPEN_WRITE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
file_writef(&artist_file, "%u\n", artist_db_get()->db_size);
|
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
|
|
|
playlist = ARTIST(dbe)->ar_playlist;
|
|
|
|
file_writef(&artist_file, "%s\n", playlist->pl_name);
|
|
|
|
queue_save_flags(&playlist->pl_queue, &artist_file, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_close(&artist_file);
|
|
|
|
}
|
|
|
|
|
2016-05-15 10:51:33 -04:00
|
|
|
static struct queue *pl_artist_get_queue(const gchar *name)
|
|
|
|
{
|
2016-05-24 07:33:40 -04:00
|
|
|
struct playlist *playlist = __artist_pl_lookup(name);
|
2016-05-15 10:51:33 -04:00
|
|
|
return playlist ? &playlist->pl_queue : NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-28 16:03:47 -04:00
|
|
|
static unsigned int pl_artist_get_id(const gchar *name)
|
|
|
|
{
|
|
|
|
struct artist *artist = artist_lookup(name);
|
|
|
|
return artist ? artist->ar_dbe.dbe_index : -1;
|
|
|
|
}
|
|
|
|
|
2016-08-01 09:41:10 -04:00
|
|
|
static bool pl_artist_can_select(const gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __artist_pl_lookup(name);
|
|
|
|
return playlist ? playlist_generic_can_select(playlist) : false;
|
|
|
|
}
|
|
|
|
|
2016-07-28 16:03:47 -04:00
|
|
|
static gchar *pl_artist_get_name(unsigned int id)
|
|
|
|
{
|
|
|
|
struct artist *artist = ARTIST(db_at(artist_db_get(), id));
|
|
|
|
return artist ? g_strdup(artist->ar_name) : NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-15 10:51:33 -04:00
|
|
|
static bool pl_artist_new_delete(const gchar *name)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_artist_add_rm(const gchar *name, struct track *track)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_artist_update(const gchar *name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_artist_set_flag(const gchar *name, enum queue_flags flag,
|
|
|
|
bool enabled)
|
|
|
|
{
|
2016-05-24 07:48:11 -04:00
|
|
|
struct playlist *playlist = __artist_pl_lookup(name);
|
|
|
|
playlist_generic_set_flag(playlist, flag, enabled);
|
2016-08-12 16:12:17 -04:00
|
|
|
pl_artist_save();
|
2016-05-15 10:51:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_artist_sort(const gchar *name, enum compare_t sort, bool reset)
|
|
|
|
{
|
2016-05-24 07:33:40 -04:00
|
|
|
struct playlist *playlist = __artist_pl_lookup(name);
|
|
|
|
playlist_generic_sort(playlist, sort, reset);
|
2016-08-12 16:12:17 -04:00
|
|
|
pl_artist_save();
|
2016-05-15 10:51:33 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 08:00:13 -04:00
|
|
|
static struct track *pl_artist_next(const gchar *name)
|
|
|
|
{
|
|
|
|
struct playlist *playlist = __artist_pl_lookup(name);
|
2016-08-09 08:28:02 -04:00
|
|
|
struct track *track = playlist_generic_next(playlist);;
|
2016-08-12 16:12:17 -04:00
|
|
|
pl_artist_save();
|
2016-08-09 08:28:02 -04:00
|
|
|
return track;
|
2016-08-05 08:00:13 -04:00
|
|
|
}
|
|
|
|
|
2016-05-15 10:51:33 -04:00
|
|
|
|
|
|
|
struct playlist_type pl_artist = {
|
2016-08-12 16:12:17 -04:00
|
|
|
.pl_save = pl_artist_save,
|
2016-05-15 10:51:33 -04:00
|
|
|
.pl_get_queue = pl_artist_get_queue,
|
2016-07-28 16:03:47 -04:00
|
|
|
.pl_get_id = pl_artist_get_id,
|
|
|
|
.pl_get_name = pl_artist_get_name,
|
2016-08-01 09:41:10 -04:00
|
|
|
.pl_can_select = pl_artist_can_select,
|
2016-05-15 10:51:33 -04:00
|
|
|
.pl_new = pl_artist_new_delete,
|
|
|
|
.pl_delete = pl_artist_new_delete,
|
|
|
|
.pl_add_track = pl_artist_add_rm,
|
|
|
|
.pl_remove_track = pl_artist_add_rm,
|
|
|
|
.pl_update = pl_artist_update,
|
|
|
|
.pl_set_flag = pl_artist_set_flag,
|
|
|
|
.pl_sort = pl_artist_sort,
|
2016-08-05 08:00:13 -04:00
|
|
|
.pl_next = pl_artist_next,
|
2016-05-15 10:51:33 -04:00
|
|
|
};
|
2016-05-15 10:19:31 -04:00
|
|
|
|
|
|
|
|
2016-08-22 11:22:22 -04:00
|
|
|
void pl_artist_init(struct queue_ops *ops)
|
2016-05-15 10:19:31 -04:00
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
2016-08-22 11:22:22 -04:00
|
|
|
artist_ops = ops;
|
|
|
|
|
2016-05-15 10:19:31 -04:00
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
|
|
|
playlist = __artist_pl_alloc(ARTIST(dbe)->ar_name);
|
|
|
|
ARTIST(dbe)->ar_playlist = playlist;
|
2016-08-13 09:38:28 -04:00
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
idle_schedule(IDLE_SYNC, __artist_pl_add, playlist);
|
2016-05-15 10:19:31 -04:00
|
|
|
}
|
|
|
|
|
2016-08-09 08:28:02 -04:00
|
|
|
idle_schedule(IDLE_SYNC, __artist_pl_load, NULL);
|
2016-05-15 10:19:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pl_artist_deinit()
|
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
|
|
|
playlist = ARTIST(dbe)->ar_playlist;
|
|
|
|
ARTIST(dbe)->ar_playlist = NULL;
|
|
|
|
|
|
|
|
__artist_pl_free(playlist);
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 09:46:16 -04:00
|
|
|
|
|
|
|
void pl_artist_new_track(struct track *track)
|
|
|
|
{
|
2016-08-29 11:19:58 -04:00
|
|
|
struct artist *artist = track->tr_album->al_artist;
|
2016-08-28 09:46:16 -04:00
|
|
|
struct playlist *playlist = (struct playlist *)artist->ar_playlist;
|
|
|
|
|
|
|
|
if (!playlist) {
|
|
|
|
playlist = __artist_pl_alloc(artist->ar_name);
|
|
|
|
artist->ar_playlist = playlist;
|
|
|
|
}
|
|
|
|
|
2016-08-29 07:49:18 -04:00
|
|
|
playlist_generic_add_track(playlist, track);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pl_artist_delete_track(struct track *track)
|
|
|
|
{
|
2016-08-29 11:19:58 -04:00
|
|
|
struct artist *artist = track->tr_album->al_artist;
|
2016-08-29 07:49:18 -04:00
|
|
|
struct playlist *playlist = (struct playlist *)artist->ar_playlist;
|
|
|
|
|
|
|
|
if (playlist)
|
|
|
|
playlist_generic_remove_track(playlist, track);
|
2016-08-28 09:46:16 -04:00
|
|
|
}
|