diff --git a/core/playlists/system.c b/core/playlists/system.c index 783e057e..37e16d92 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -10,8 +10,9 @@ static bool pl_system_remove_track(const gchar *, struct track *); static inline struct queue *__sys_pl_queue(enum sys_playlist_t); static void __sys_pl_save(); -static struct file sys_file = FILE_INIT("playlist.db", 0, 0); -static struct file sys_collection = FILE_INIT("library.q", 0, 0); +static struct file sys_file = FILE_INIT("playlist.db", 0, 0); +static struct file sys_collection_f = FILE_INIT("library.q", 0, 0); +static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS]; /* @@ -48,6 +49,15 @@ static bool sys_pl_favorites_remove(struct playlist *playlist, struct track *tra return ret; } +static struct sys_playlist sys_favorites = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites"), + .spl_init = playlist_generic_init, + .spl_add = sys_pl_favorites_add, + .spl_remove = sys_pl_favorites_remove, + .spl_set_flag = playlist_generic_set_flag, + .spl_sort = playlist_generic_sort, +}; + /* * Hidden tracks playlist operations. @@ -74,25 +84,34 @@ static bool sys_pl_hidden_remove(struct playlist *playlist, struct track *track) return ret; } +static struct sys_playlist sys_hidden = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden"), + .spl_init = playlist_generic_init, + .spl_add = sys_pl_hidden_add, + .spl_remove = sys_pl_hidden_remove, + .spl_set_flag = playlist_generic_set_flag, + .spl_sort = playlist_generic_sort, +}; + /* * Collection playlist operations. */ static void sys_pl_collection_save(struct playlist *playlist) { - if (file_open(&sys_collection, OPEN_WRITE)) { - queue_save_flags(&playlist->pl_queue, &sys_collection); - file_close(&sys_collection); + if (file_open(&sys_collection_f, OPEN_WRITE)) { + queue_save_flags(&playlist->pl_queue, &sys_collection_f); + file_close(&sys_collection_f); } } static void sys_pl_collection_load(struct playlist *playlist) { - if (file_open(&sys_collection, OPEN_READ)) { - queue_load_flags(&playlist->pl_queue, &sys_collection); + if (file_open(&sys_collection_f, OPEN_READ)) { + queue_load_flags(&playlist->pl_queue, &sys_collection_f); queue_unset_flag(&playlist->pl_queue, Q_SAVE_FLAGS); queue_unset_flag(&playlist->pl_queue, Q_SAVE_SORT); - file_close(&sys_collection); + file_close(&sys_collection_f); } } @@ -125,6 +144,16 @@ static void sys_pl_collection_sort(struct playlist *playlist, sys_pl_collection_save(playlist); } +static struct sys_playlist sys_collection = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection"), + .spl_init = sys_pl_generic_init, + .spl_add = sys_pl_collection_add, + .spl_remove = playlist_generic_remove_track, + .spl_update = sys_pl_collection_update, + .spl_set_flag = sys_pl_collection_set_flag, + .spl_sort = sys_pl_collection_sort, +}; + /* * History playlist operations. @@ -144,6 +173,15 @@ static bool sys_pl_history_add(struct playlist *playlist, struct track *track) return true; } +static struct sys_playlist sys_history = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History"), + .spl_init = sys_pl_history_init, + .spl_add = sys_pl_history_add, + .spl_remove = playlist_generic_remove_track, + .spl_set_flag = playlist_noop_set_flag, + .spl_sort = playlist_noop_sort, +}; + /* * Unplayed tracks playlist operations. @@ -163,6 +201,16 @@ static bool sys_pl_unplayed_update(struct playlist *playlist, return sys_pl_generic_add(playlist, track) || true; } +static struct sys_playlist sys_unplayed = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed"), + .spl_init = sys_pl_generic_init, + .spl_add = sys_pl_unplayed_add, + .spl_remove = playlist_generic_remove_track, + .spl_update = sys_pl_unplayed_update, + .spl_set_flag = playlist_generic_set_flag, + .spl_sort = playlist_generic_sort, +}; + /* * Most played tracks playlist operations. @@ -184,6 +232,16 @@ static bool sys_pl_most_played_update(struct playlist *playlist, return sys_pl_generic_add(playlist, track) || true; } +static struct sys_playlist sys_most_played = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played"), + .spl_init = sys_pl_generic_init, + .spl_add = sys_pl_most_played_add, + .spl_remove = playlist_generic_remove_track, + .spl_update = sys_pl_most_played_update, + .spl_set_flag = playlist_generic_set_flag, + .spl_sort = playlist_generic_sort, +}; + /* * Least played tracks playlist operations. @@ -205,99 +263,50 @@ static bool sys_pl_least_played_update(struct playlist *playlist, return sys_pl_generic_add(playlist, track) || true; } - -static struct sys_playlist sys_playlists[SYS_PL_NUM_PLAYLISTS] = { - [SYS_PL_FAVORITES] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites"), - .spl_init = playlist_generic_init, - .spl_add = sys_pl_favorites_add, - .spl_remove = sys_pl_favorites_remove, - .spl_set_flag = playlist_generic_set_flag, - .spl_sort = playlist_generic_sort, - }, - [SYS_PL_HIDDEN] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden"), - .spl_init = playlist_generic_init, - .spl_add = sys_pl_hidden_add, - .spl_remove = sys_pl_hidden_remove, - .spl_set_flag = playlist_generic_set_flag, - .spl_sort = playlist_generic_sort, - }, - [SYS_PL_COLLECTION] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection"), - .spl_init = sys_pl_generic_init, - .spl_add = sys_pl_collection_add, - .spl_remove = playlist_generic_remove_track, - .spl_update = sys_pl_collection_update, - .spl_set_flag = sys_pl_collection_set_flag, - .spl_sort = sys_pl_collection_sort, - }, - [SYS_PL_HISTORY] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History"), - .spl_init = sys_pl_history_init, - .spl_add = sys_pl_history_add, - .spl_remove = playlist_generic_remove_track, - .spl_set_flag = playlist_noop_set_flag, - .spl_sort = playlist_noop_sort, - }, - [SYS_PL_UNPLAYED] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Unplayed"), - .spl_init = sys_pl_generic_init, - .spl_add = sys_pl_unplayed_add, - .spl_remove = playlist_generic_remove_track, - .spl_update = sys_pl_unplayed_update, - .spl_set_flag = playlist_generic_set_flag, - .spl_sort = playlist_generic_sort, - }, - [SYS_PL_MOST_PLAYED] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Most Played"), - .spl_init = sys_pl_generic_init, - .spl_add = sys_pl_most_played_add, - .spl_remove = playlist_generic_remove_track, - .spl_update = sys_pl_most_played_update, - .spl_set_flag = playlist_generic_set_flag, - .spl_sort = playlist_generic_sort, - }, - [SYS_PL_LEAST_PLAYED] = { - .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played"), - .spl_init = sys_pl_generic_init, - .spl_add = sys_pl_least_played_add, - .spl_remove = playlist_generic_remove_track, - .spl_update = sys_pl_least_played_update, - .spl_set_flag = playlist_generic_set_flag, - .spl_sort = playlist_generic_sort, - }, +static struct sys_playlist sys_least_played = { + .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played"), + .spl_init = sys_pl_generic_init, + .spl_add = sys_pl_least_played_add, + .spl_remove = playlist_generic_remove_track, + .spl_update = sys_pl_least_played_update, + .spl_set_flag = playlist_generic_set_flag, + .spl_sort = playlist_generic_sort, }; -static enum sys_playlist_t __sys_pl_convert(const gchar *name) +static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS] = { + [SYS_PL_FAVORITES] = &sys_favorites, + [SYS_PL_HIDDEN] = &sys_hidden, + [SYS_PL_COLLECTION] = &sys_collection, + [SYS_PL_HISTORY] = &sys_history, + [SYS_PL_UNPLAYED] = &sys_unplayed, + [SYS_PL_MOST_PLAYED] = &sys_most_played, + [SYS_PL_LEAST_PLAYED] = &sys_least_played, +}; + + +static struct sys_playlist * __sys_pl_lookup(const gchar *name) { if (string_match(name, "Favorites")) - return SYS_PL_FAVORITES; + return &sys_favorites; else if (string_match(name, "Hidden") || string_match(name, "Banned")) - return SYS_PL_HIDDEN; + return &sys_hidden; else if (string_match(name, "Collection")) - return SYS_PL_COLLECTION; + return &sys_collection; else if (string_match(name, "History")) - return SYS_PL_HISTORY; + return &sys_history; else if (string_match(name, "Unplayed")) - return SYS_PL_UNPLAYED; + return &sys_unplayed; else if (string_match(name, "Most Played")) - return SYS_PL_MOST_PLAYED; + return &sys_most_played; else if (string_match(name, "Least Played")) - return SYS_PL_LEAST_PLAYED; - return SYS_PL_NUM_PLAYLISTS; + return &sys_least_played; + return NULL; } static inline struct queue *__sys_pl_queue(enum sys_playlist_t plist) { - return &sys_playlists[plist].spl_playlist.pl_queue; -} - -static struct sys_playlist *__sys_pl_lookup(const gchar *name) -{ - enum sys_playlist_t plist = __sys_pl_convert(name); - return (plist < SYS_PL_NUM_PLAYLISTS) ? &sys_playlists[plist] : NULL; + return &sys_playlists[plist]->spl_playlist.pl_queue; } static void __sys_pl_save() @@ -316,7 +325,7 @@ static void __sys_pl_save() static void __sys_pl_load_playlists() { - enum sys_playlist_t plist; + struct sys_playlist *plist; unsigned int i, n; gchar *name; @@ -327,9 +336,9 @@ static void __sys_pl_load_playlists() for (i = 0; i < n; i++) { file_readf(&sys_file, "%*u %m[^\n]\n", &name); - plist = __sys_pl_convert(name); - if (plist < SYS_PL_NUM_PLAYLISTS) - queue_load_tracks(__sys_pl_queue(plist), &sys_file); + plist = __sys_pl_lookup(name); + if (plist) + queue_load_tracks(&plist->spl_playlist.pl_queue, &sys_file); g_free(name); } @@ -409,7 +418,7 @@ void pl_system_init(struct queue_ops *ops) unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { - sys_pl = &sys_playlists[i]; + sys_pl = sys_playlists[i]; sys_pl->spl_init(&sys_pl->spl_playlist, 0, ops); } @@ -440,7 +449,7 @@ void pl_system_delete_track(struct track *track) unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { - sys_pl = &sys_playlists[i]; + sys_pl = sys_playlists[i]; sys_pl->spl_remove(&sys_pl->spl_playlist, track); } }