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
|
|
|
|
2018-02-16 13:30:23 -05:00
|
|
|
static struct file artist_file = DATA_FILE_INIT("playlist.artist", 0);
|
2016-05-15 10:19:31 -04:00
|
|
|
|
2016-09-12 08:17:41 -04:00
|
|
|
static struct playlist_ops pl_artist_ops = {
|
2016-09-12 13:08:24 -04:00
|
|
|
.pl_can_select = playlist_generic_can_select,
|
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-12 08:17:41 -04:00
|
|
|
};
|
2016-09-09 10:17:37 -04:00
|
|
|
|
2016-05-15 10:19:31 -04:00
|
|
|
|
2016-09-12 15:42:56 -04:00
|
|
|
static struct playlist *__artist_pl_alloc(struct artist *artist)
|
2016-05-15 10:19:31 -04:00
|
|
|
{
|
2016-09-22 11:32:40 -04:00
|
|
|
return playlist_generic_alloc(artist->ar_name, PL_ARTIST,
|
2016-09-22 13:37:34 -04:00
|
|
|
artist_index(artist), &pl_artist_ops);
|
2016-05-15 10:19:31 -04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-09-21 09:36:44 -04:00
|
|
|
playlist_generic_add_front(playlist, TRACK(dbe));
|
2016-05-15 10:32:58 -04:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:33:09 -04:00
|
|
|
playlist_generic_resort(playlist);
|
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;
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
if (!data_file_open(&artist_file, OPEN_READ))
|
2016-08-09 08:28:02 -04:00
|
|
|
return true;
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_readf(&artist_file, "%u\n", &n);
|
2016-08-09 08:28:02 -04:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-02-16 09:34:06 -05:00
|
|
|
name = data_file_readl(&artist_file);
|
2016-08-09 08:28:02 -04:00
|
|
|
playlist = __artist_pl_lookup(name);
|
2016-09-16 11:12:41 -04:00
|
|
|
if (playlist)
|
|
|
|
playlist_generic_load(playlist, &artist_file,
|
|
|
|
PL_SAVE_METADATA);
|
2016-09-29 14:48:43 -04:00
|
|
|
g_free(name);
|
2016-08-09 08:28:02 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_close(&artist_file);
|
2016-08-09 08:28:02 -04:00
|
|
|
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;
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
if (!data_file_open(&artist_file, OPEN_WRITE))
|
2016-08-12 16:12:17 -04:00
|
|
|
return;
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_writef(&artist_file, "%u\n", artist_db_get()->db_size);
|
2016-08-12 16:12:17 -04:00
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
|
|
|
playlist = ARTIST(dbe)->ar_playlist;
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_writef(&artist_file, "%s\n", playlist->pl_name);
|
2016-09-16 10:04:51 -04:00
|
|
|
playlist_generic_save(playlist, &artist_file, PL_SAVE_METADATA);
|
2016-08-12 16:12:17 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 09:34:06 -05:00
|
|
|
data_file_close(&artist_file);
|
2016-08-12 16:12:17 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 13:49:27 -04:00
|
|
|
static struct playlist *pl_artist_lookup(const gchar *name)
|
2016-05-15 10:51:33 -04:00
|
|
|
{
|
2016-08-20 19:13:18 -04:00
|
|
|
return __artist_pl_lookup(name);
|
2016-05-15 10:51:33 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:59:46 -04:00
|
|
|
static struct playlist *pl_artist_get(unsigned int id)
|
|
|
|
{
|
|
|
|
struct artist *artist = artist_get(id);
|
|
|
|
return artist ? artist->ar_playlist : NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-13 10:59:20 -04:00
|
|
|
static void pl_artist_played(struct track *track)
|
|
|
|
{
|
2016-09-20 11:01:32 -04:00
|
|
|
struct artist *artist = track->tr_album->al_artist;
|
|
|
|
playlist_generic_update(artist->ar_playlist, track);
|
2016-09-13 10:59:20 -04:00
|
|
|
}
|
|
|
|
|
2016-05-15 10:51:33 -04:00
|
|
|
|
|
|
|
struct playlist_type pl_artist = {
|
2016-09-20 10:00:12 -04:00
|
|
|
.pl_save = pl_artist_save,
|
|
|
|
.pl_lookup = pl_artist_lookup,
|
|
|
|
.pl_get = pl_artist_get,
|
|
|
|
.pl_played = pl_artist_played,
|
|
|
|
.pl_selected = pl_artist_played,
|
2016-05-15 10:51:33 -04:00
|
|
|
};
|
2016-05-15 10:19:31 -04:00
|
|
|
|
|
|
|
|
2016-09-22 13:37:34 -04:00
|
|
|
void pl_artist_init(void)
|
2016-05-15 10:19:31 -04:00
|
|
|
{
|
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct playlist *playlist;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
2016-09-12 15:42:56 -04:00
|
|
|
playlist = __artist_pl_alloc(ARTIST(dbe));
|
2016-05-15 10:19:31 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
db_for_each(dbe, next, artist_db_get()) {
|
2016-09-22 11:32:40 -04:00
|
|
|
playlist_generic_free(ARTIST(dbe)->ar_playlist);
|
2016-05-15 10:19:31 -04:00
|
|
|
ARTIST(dbe)->ar_playlist = NULL;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2016-09-12 15:42:56 -04:00
|
|
|
playlist = __artist_pl_alloc(artist);
|
2016-08-28 09:46:16 -04:00
|
|
|
artist->ar_playlist = playlist;
|
|
|
|
}
|
|
|
|
|
2016-09-21 10:44:06 -04:00
|
|
|
playlist_generic_add(playlist, track);
|
2016-08-29 07:49:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void pl_artist_delete_track(struct track *track)
|
|
|
|
{
|
2016-09-20 14:38:46 -04:00
|
|
|
struct artist *artist = track->tr_album->al_artist;
|
|
|
|
playlist_generic_remove(artist->ar_playlist, track);
|
2016-08-28 09:46:16 -04:00
|
|
|
}
|