/* * Copyright 2016 (c) Anna Schumaker. */ #include #include static struct queue_ops *artist_ops = NULL; 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; queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT, artist_ops, playlist); return playlist; } static void __artist_pl_free(struct playlist *playlist) { if (playlist) { queue_deinit(&playlist->pl_queue); g_free(playlist); } } static bool __artist_pl_load(void *data) { struct playlist *playlist = (struct playlist *)data; struct artist *artist = artist_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_artist == artist) queue_add(&playlist->pl_queue, TRACK(dbe)); } queue_unset_flag(&playlist->pl_queue, Q_ADD_FRONT); return true; } static struct queue *pl_artist_get_queue(const gchar *name) { struct artist *artist = artist_lookup(name); struct playlist *playlist = artist ? artist->ar_playlist : NULL; return playlist ? &playlist->pl_queue : NULL; } 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) { struct queue *queue = pl_artist_get_queue(name); (enabled ? queue_set_flag : queue_unset_flag)(queue, flag); } static void pl_artist_sort(const gchar *name, enum compare_t sort, bool reset) { struct queue *queue = pl_artist_get_queue(name); queue_sort(queue, sort, reset); } struct playlist_type pl_artist = { .pl_get_queue = pl_artist_get_queue, .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, }; static bool __artist_pl_init(void *data) { struct db_entry *dbe, *next; struct playlist *playlist; db_for_each(dbe, next, artist_db_get()) { playlist = __artist_pl_alloc(ARTIST(dbe)->ar_name); ARTIST(dbe)->ar_playlist = playlist; idle_schedule(IDLE_SYNC, __artist_pl_load, playlist); } return true; } void pl_artist_init(struct queue_ops *ops) { artist_ops = ops; idle_schedule(IDLE_SYNC, __artist_pl_init, NULL); } 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); } }