core/playlists: Add struct playlist

Containing the playlist name and queue.  I pass this to the gui through
the queue_init() function.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-05-02 08:13:35 -04:00 committed by Anna Schumaker
parent 44a57ed863
commit 62123eb025
2 changed files with 38 additions and 17 deletions

View File

@ -5,10 +5,17 @@
#include <core/playlists/system.h> #include <core/playlists/system.h>
#include <core/string.h> #include <core/string.h>
static struct file sys_file = FILE_INIT("playlist.db", 0, 0);
static struct queue sys_playlists[SYS_PL_NUM_PLAYLISTS];
static bool __sys_pl_remove(enum sys_playlist_t, struct track *); static bool __sys_pl_remove(enum sys_playlist_t, struct track *);
static struct file sys_file = FILE_INIT("playlist.db", 0, 0);
static struct playlist sys_playlists[SYS_PL_NUM_PLAYLISTS] = {
[SYS_PL_FAVORITES] = DEFINE_PLAYLIST("Favorites"),
[SYS_PL_HIDDEN] = DEFINE_PLAYLIST("Hidden"),
[SYS_PL_UNPLAYED] = DEFINE_PLAYLIST("Unplayed"),
[SYS_PL_MOST_PLAYED] = DEFINE_PLAYLIST("Most Played"),
[SYS_PL_LEAST_PLAYED] = DEFINE_PLAYLIST("Least Played"),
};
static enum sys_playlist_t __sys_pl_convert(const gchar *name) static enum sys_playlist_t __sys_pl_convert(const gchar *name)
{ {
@ -25,15 +32,20 @@ static enum sys_playlist_t __sys_pl_convert(const gchar *name)
return SYS_PL_NUM_PLAYLISTS; return SYS_PL_NUM_PLAYLISTS;
} }
static inline struct queue *__sys_pl_queue(enum sys_playlist_t plist)
{
return &sys_playlists[plist].pl_queue;
}
static void __sys_pl_save() static void __sys_pl_save()
{ {
if (!file_open(&sys_file, OPEN_WRITE)) if (!file_open(&sys_file, OPEN_WRITE))
return; return;
file_writef(&sys_file, "%u\n 1 %s\n", 2, "Favorites"); file_writef(&sys_file, "%u\n 1 %s\n", 2, "Favorites");
queue_save_tracks(&sys_playlists[SYS_PL_FAVORITES], &sys_file); queue_save_tracks(__sys_pl_queue(SYS_PL_FAVORITES), &sys_file);
file_writef(&sys_file, "\n1 %s\n", "Banned"); file_writef(&sys_file, "\n1 %s\n", "Banned");
queue_save_tracks(&sys_playlists[SYS_PL_HIDDEN], &sys_file); queue_save_tracks(__sys_pl_queue(SYS_PL_HIDDEN), &sys_file);
file_writef(&sys_file, "\n"); file_writef(&sys_file, "\n");
file_close(&sys_file); file_close(&sys_file);
@ -54,7 +66,7 @@ static bool __sys_pl_load()
plist = __sys_pl_convert(name); plist = __sys_pl_convert(name);
if (plist < SYS_PL_NUM_PLAYLISTS) if (plist < SYS_PL_NUM_PLAYLISTS)
queue_load_tracks(&sys_playlists[plist], &sys_file); queue_load_tracks(__sys_pl_queue(plist), &sys_file);
g_free(name); g_free(name);
} }
@ -79,13 +91,13 @@ static bool __sys_pl_add(enum sys_playlist_t plist, struct track *track,
if (plist != SYS_PL_FAVORITES && plist != SYS_PL_HIDDEN) { if (plist != SYS_PL_FAVORITES && plist != SYS_PL_HIDDEN) {
if (!__sys_pl_can_add(plist, track, average)) if (!__sys_pl_can_add(plist, track, average))
return false; return false;
if (queue_has(&sys_playlists[SYS_PL_HIDDEN], track)) if (queue_has(__sys_pl_queue(SYS_PL_HIDDEN), track))
return false; return false;
} }
if (queue_has(&sys_playlists[plist], track)) if (queue_has(__sys_pl_queue(plist), track))
return false; return false;
queue_add(&sys_playlists[plist], track); queue_add(__sys_pl_queue(plist), track);
switch (plist) { switch (plist) {
case SYS_PL_HIDDEN: case SYS_PL_HIDDEN:
@ -110,7 +122,7 @@ static bool __sys_pl_add(enum sys_playlist_t plist, struct track *track,
static bool __sys_pl_remove(enum sys_playlist_t plist, struct track *track) static bool __sys_pl_remove(enum sys_playlist_t plist, struct track *track)
{ {
if (!queue_remove_all(&sys_playlists[plist], track)) if (!queue_remove_all(__sys_pl_queue(plist), track))
return false; return false;
switch (plist) { switch (plist) {
@ -138,7 +150,7 @@ static bool __sys_pl_update(enum sys_playlist_t plist)
__sys_pl_remove(plist, TRACK(dbe)); __sys_pl_remove(plist, TRACK(dbe));
} }
queue_unset_flag(&sys_playlists[plist], Q_ADD_FRONT); queue_unset_flag(__sys_pl_queue(plist), Q_ADD_FRONT);
return true; return true;
} }
@ -148,7 +160,7 @@ static struct queue *pl_system_get_queue(const gchar *name)
enum sys_playlist_t plist = __sys_pl_convert(name); enum sys_playlist_t plist = __sys_pl_convert(name);
if (plist == SYS_PL_NUM_PLAYLISTS) if (plist == SYS_PL_NUM_PLAYLISTS)
return NULL; return NULL;
return &sys_playlists[plist]; return __sys_pl_queue(plist);
} }
static bool pl_system_add_track(const gchar *name, struct track *track) static bool pl_system_add_track(const gchar *name, struct track *track)
@ -189,12 +201,12 @@ void pl_system_init(struct queue_ops *ops)
unsigned int i; unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
queue_init(&sys_playlists[i], Q_ENABLED | Q_REPEAT, ops, NULL); queue_init(__sys_pl_queue(i), Q_ENABLED | Q_REPEAT, ops, &sys_playlists[i]);
queue_sort(&sys_playlists[i], COMPARE_ARTIST, true); queue_sort(__sys_pl_queue(i), COMPARE_ARTIST, true);
queue_sort(&sys_playlists[i], COMPARE_YEAR, false); queue_sort(__sys_pl_queue(i), COMPARE_YEAR, false);
queue_sort(&sys_playlists[i], COMPARE_TRACK, false); queue_sort(__sys_pl_queue(i), COMPARE_TRACK, false);
if (i >= SYS_PL_UNPLAYED) if (i >= SYS_PL_UNPLAYED)
queue_set_flag(&sys_playlists[i], Q_ADD_FRONT); queue_set_flag(__sys_pl_queue(i), Q_ADD_FRONT);
} }
idle_schedule(IDLE_SYNC, __sys_pl_load, NULL); idle_schedule(IDLE_SYNC, __sys_pl_load, NULL);
@ -208,5 +220,5 @@ void pl_system_deinit()
unsigned int i; unsigned int i;
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++)
queue_deinit(&sys_playlists[i]); queue_deinit(__sys_pl_queue(i));
} }

View File

@ -9,6 +9,15 @@
#include <stdbool.h> #include <stdbool.h>
struct playlist {
const gchar *pl_name; /* This playlist's name. */
struct queue pl_queue; /* This playlist's queue of tracks. */
};
#define DEFINE_PLAYLIST(name) { .pl_name = name, }
struct playlist_type { struct playlist_type {
/* Called to get the queue for the playlist. */ /* Called to get the queue for the playlist. */
struct queue *(*pl_get_queue)(const gchar *); struct queue *(*pl_get_queue)(const gchar *);