/* * Copyright 2016 (c) Anna Schumaker. */ #include #include #include static bool pl_system_add_track(const gchar *, struct track *); static bool pl_system_remove_track(const gchar *, struct track *); static void pl_system_update(const gchar *); static inline struct queue *__sys_pl_queue(enum sys_playlist_t); static void __sys_pl_save(); static bool __sys_pl_load(); static struct file sys_file = FILE_INIT("playlist.db", 0); static struct file sys_deck_f = FILE_INIT("deck", 1); static struct file sys_collection_f = FILE_INIT("library.q", 0); static struct file sys_pl_file = FILE_INIT("playlist.system", 0); static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS]; /* * Generic system playlist operations. */ static void sys_pl_generic_init(struct playlist *playlist, unsigned int flags, struct queue_ops *ops) { playlist_generic_init(playlist, Q_REPEAT | Q_ADD_FRONT, ops); } static void sys_pl_update_init(struct playlist *playlist, unsigned int flags, struct queue_ops *ops) { sys_pl_generic_init(playlist, flags, ops); pl_system_update(playlist->pl_name); } static void sys_pl_save_partial(struct playlist *playlist, struct file *file) { file_writef(file, "%s\n", playlist->pl_name); queue_save_flags(&playlist->pl_queue, file, true); } static void sys_pl_save_full(struct playlist *playlist, struct file *file) { sys_pl_save_partial(playlist, file); queue_save_tracks(&playlist->pl_queue, file); } static void sys_pl_load_partial(struct playlist *playlist, struct file *file) { queue_load_flags(&playlist->pl_queue, file, true); } static void sys_pl_load_full(struct playlist *playlist, struct file *file) { sys_pl_load_partial(playlist, file); queue_load_tracks(&playlist->pl_queue, file); } static bool sys_pl_generic_add(struct playlist *playlist, struct track *track) { if (queue_has(__sys_pl_queue(SYS_PL_HIDDEN), track)) return false; return playlist_generic_add_track(playlist, track); } /* * Favorite tracks playlist operations. */ static struct sys_playlist sys_favorites = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Favorites"), .spl_init = playlist_generic_init, .spl_save = sys_pl_save_full, .spl_load = sys_pl_load_full, .spl_can_select = playlist_generic_can_select, .spl_add = playlist_generic_add_track, .spl_remove = playlist_generic_remove_track, .spl_clear = playlist_generic_clear, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * Hidden tracks playlist operations. */ static bool sys_pl_hidden_add(struct playlist *playlist, struct track *track) { bool ret = playlist_generic_add_track(playlist, track); pl_system_remove_track("Collection", track); pl_system_remove_track("Unplayed", track); pl_system_remove_track("Most Played", track); pl_system_remove_track("Least Played", track); return ret; } static bool sys_pl_hidden_remove(struct playlist *playlist, struct track *track) { bool ret = playlist_generic_remove_track(playlist, track); pl_system_add_track("Collection", track); pl_system_add_track("Unplayed", track); pl_system_add_track("Most Played", track); pl_system_add_track("Least Played", track); return ret; } static void sys_pl_hidden_clear(struct playlist *playlist) { struct track *track; while (queue_size(&playlist->pl_queue) > 0) { track = queue_at(&playlist->pl_queue, 0); sys_pl_hidden_remove(playlist, track); } } static struct sys_playlist sys_hidden = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Hidden"), .spl_init = playlist_generic_init, .spl_save = sys_pl_save_full, .spl_load = sys_pl_load_full, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_hidden_add, .spl_remove = sys_pl_hidden_remove, .spl_clear = sys_pl_hidden_clear, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * Queued tracks playlist operations. */ static bool sys_pl_queued_load() { struct playlist *playlist = &sys_playlists[SYS_PL_QUEUED]->spl_playlist; struct queue *queue = &playlist->pl_queue; unsigned int num, i, flags = 0; if (!file_open(&sys_deck_f, OPEN_READ)) return true; file_readf(&sys_deck_f, "%u", &num); for (i = 0; i < num; i++) { file_readf(&sys_deck_f, "%u", &flags); flags &= ~(Q_SAVE_SORT | Q_SAVE_FLAGS); if (i == 0) queue->q_flags |= flags; queue_load_tracks(queue, &sys_deck_f); } file_close(&sys_deck_f); file_remove(&sys_deck_f); return true; } static void sys_pl_queued_init(struct playlist *playlist, unsigned int flags, struct queue_ops *ops) { queue_init(&playlist->pl_queue, Q_ENABLED, ops, playlist); } static struct sys_playlist sys_queued = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Queued Tracks"), .spl_init = sys_pl_queued_init, .spl_save = sys_pl_save_full, .spl_load = sys_pl_load_full, .spl_can_select = playlist_generic_can_select, .spl_add = playlist_generic_add_track, .spl_remove = playlist_generic_remove_track, .spl_clear = playlist_generic_clear, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * Collection playlist operations. */ static bool sys_pl_collection_load() { struct playlist *playlist = &sys_playlists[SYS_PL_COLLECTION]->spl_playlist; if (file_open(&sys_collection_f, OPEN_READ)) { queue_load_flags(&playlist->pl_queue, &sys_collection_f, false); queue_unset_flag(&playlist->pl_queue, Q_SAVE_FLAGS); queue_unset_flag(&playlist->pl_queue, Q_SAVE_SORT); file_close(&sys_collection_f); file_remove(&sys_collection_f); } return true; } static bool sys_pl_collection_can_select(struct playlist *playlist) { return true; } static bool sys_pl_collection_update(struct playlist *playlist, struct track *track) { return sys_pl_generic_add(playlist, track) || true; } static struct sys_playlist sys_collection = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Collection"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, .spl_can_select = sys_pl_collection_can_select, .spl_add = sys_pl_generic_add, .spl_remove = playlist_generic_remove_track, .spl_clear = playlist_noop_clear, .spl_update = sys_pl_collection_update, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * History playlist operations. */ static void sys_pl_history_init(struct playlist *playlist, unsigned int flags, struct queue_ops *ops) { queue_init(&playlist->pl_queue, Q_ENABLED | Q_REPEAT | Q_ADD_FRONT | Q_NO_SORT, ops, playlist); } static bool sys_pl_history_add(struct playlist *playlist, struct track *track) { queue_add(&playlist->pl_queue, track); queue_iter_set(&playlist->pl_queue, &playlist->pl_queue.q_cur, 0); return true; } static struct sys_playlist sys_history = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "History"), .spl_init = sys_pl_history_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, .spl_can_select = playlist_noop_can_select, .spl_add = sys_pl_history_add, .spl_remove = playlist_generic_remove_track, .spl_clear = playlist_noop_clear, .spl_set_flag = playlist_noop_set_flag, .spl_sort = playlist_noop_sort, .spl_next = playlist_generic_next, }; /* * Unplayed tracks playlist operations. */ static bool sys_pl_unplayed_add(struct playlist *playlist, struct track *track) { if (track->tr_count > 0) return false; return sys_pl_generic_add(playlist, track); } static bool sys_pl_unplayed_update(struct playlist *playlist, struct track *track) { if (track->tr_count > 0) return false; 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_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_unplayed_add, .spl_remove = playlist_generic_remove_track, .spl_clear = playlist_noop_clear, .spl_update = sys_pl_unplayed_update, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * Most played tracks playlist operations. */ static bool sys_pl_most_played_add(struct playlist *playlist, struct track *track) { unsigned int average = track_db_average_plays(); if (track->tr_count <= average) return false; return sys_pl_generic_add(playlist, track); } static bool sys_pl_most_played_update(struct playlist *playlist, struct track *track) { unsigned int average = track_db_average_plays(); if (track->tr_count <= average) return false; 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_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_add = sys_pl_most_played_add, .spl_remove = playlist_generic_remove_track, .spl_update = sys_pl_most_played_update, .spl_clear = playlist_noop_clear, .spl_set_flag = playlist_generic_set_flag, .spl_sort = playlist_generic_sort, .spl_next = playlist_generic_next, }; /* * Least played tracks playlist operations. */ static bool sys_pl_least_played_add(struct playlist *playlist, struct track *track) { unsigned int average = track_db_average_plays(); if (!track->tr_count || track->tr_count > average) return false; return sys_pl_generic_add(playlist, track); } static bool sys_pl_least_played_update(struct playlist *playlist, struct track *track) { unsigned int average = track_db_average_plays(); if (!track->tr_count || track->tr_count > average) return false; return sys_pl_generic_add(playlist, track) || true; } static struct sys_playlist sys_least_played = { .spl_playlist = DEFINE_PLAYLIST(PL_SYSTEM, "Least Played"), .spl_init = sys_pl_update_init, .spl_save = sys_pl_save_partial, .spl_load = sys_pl_load_partial, .spl_can_select = playlist_generic_can_select, .spl_clear = playlist_noop_clear, .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, .spl_next = playlist_generic_next, }; static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS] = { [SYS_PL_FAVORITES] = &sys_favorites, [SYS_PL_HIDDEN] = &sys_hidden, [SYS_PL_QUEUED] = &sys_queued, [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) { unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { if (string_match(name, sys_playlists[i]->spl_playlist.pl_name)) return sys_playlists[i]; } return NULL; } static inline struct queue *__sys_pl_queue(enum sys_playlist_t plist) { return &sys_playlists[plist]->spl_playlist.pl_queue; } static void __sys_pl_save() { struct sys_playlist *sys_pl; unsigned int i; if (!file_open(&sys_pl_file, OPEN_WRITE)) return; file_writef(&sys_pl_file, "%u\n", SYS_PL_NUM_PLAYLISTS); for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { sys_pl = sys_playlists[i]; sys_pl->spl_save(&sys_pl->spl_playlist, &sys_pl_file); } file_close(&sys_pl_file); } static bool __sys_pl_update_save() { __sys_pl_save(); return true; } static bool __sys_pl_load_new() { struct sys_playlist *sys_pl; unsigned int i, n; gchar *name; if (!file_open(&sys_pl_file, OPEN_READ)) { __sys_pl_load(); sys_pl_collection_load(); sys_pl_queued_load(); __sys_pl_update_save(); return true; } file_readf(&sys_pl_file, "%u\n", &n); for (i = 0; i < n; i++) { name = file_readl(&sys_pl_file); sys_pl = __sys_pl_lookup(name); if (sys_pl) sys_pl->spl_load(&sys_pl->spl_playlist, &sys_pl_file); g_free(name); } file_close(&sys_pl_file); return true; } static bool __sys_pl_load() { struct sys_playlist *plist; unsigned int i, n; gchar *name; if (!file_open(&sys_file, OPEN_READ)) return true; file_readf(&sys_file, "%u\n", &n); for (i = 0; i < n; i++) { file_readf(&sys_file, "%*u %m[^\n]\n", &name); if (string_match(name, "Banned")) { g_free(name); name = g_strdup("Hidden"); } plist = __sys_pl_lookup(name); if (plist) queue_load_tracks(&plist->spl_playlist.pl_queue, &sys_file); g_free(name); } file_close(&sys_file); file_remove(&sys_file); return true; } static void pl_system_save(void) { __sys_pl_save(); } static struct queue *pl_system_get_queue(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); return sys_pl ? &sys_pl->spl_playlist.pl_queue : NULL; } static unsigned int pl_system_get_id(const gchar *name) { unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { if (string_match(name, sys_playlists[i]->spl_playlist.pl_name)) return i; } return SYS_PL_NUM_PLAYLISTS; } static gchar *pl_system_get_name(unsigned int id) { if (id < SYS_PL_NUM_PLAYLISTS) return g_strdup(sys_playlists[id]->spl_playlist.pl_name); return NULL; } static bool pl_system_can_select(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); return sys_pl ? sys_pl->spl_can_select(&sys_pl->spl_playlist) : false; } static bool pl_system_new(const gchar *name) { return false; } static bool pl_system_delete(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); if (sys_pl) { sys_pl->spl_clear(&sys_pl->spl_playlist); __sys_pl_save(); } return false; /* Don't remove the playlist from the sidebar. */ } static bool pl_system_add_track(const gchar *name, struct track *track) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); bool ret = false; if (sys_pl) { ret = sys_pl->spl_add(&sys_pl->spl_playlist, track); if (ret) __sys_pl_save(); } return ret; } static bool pl_system_remove_track(const gchar *name, struct track *track) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); bool ret = false; if (sys_pl) { ret = sys_pl->spl_remove(&sys_pl->spl_playlist, track); if (ret) __sys_pl_save(); } return ret; } static void pl_system_update(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); if (sys_pl) playlist_generic_update(&sys_pl->spl_playlist, sys_pl->spl_update); } static void pl_system_set_flag(const gchar *name, enum queue_flags flag, bool enabled) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); if (sys_pl) { sys_pl->spl_set_flag(&sys_pl->spl_playlist, flag, enabled); __sys_pl_save(); } } static void pl_system_sort(const gchar *name, enum compare_t sort, bool reset) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); if (sys_pl) { sys_pl->spl_sort(&sys_pl->spl_playlist, sort, reset); __sys_pl_save(); } } static struct track *pl_system_next(const gchar *name) { struct sys_playlist *sys_pl = __sys_pl_lookup(name); struct track *track = NULL;; if (sys_pl) { track = sys_pl->spl_next(&sys_pl->spl_playlist); __sys_pl_save(); } return track; } struct playlist_type pl_system = { .pl_save = pl_system_save, .pl_get_queue = pl_system_get_queue, .pl_get_id = pl_system_get_id, .pl_get_name = pl_system_get_name, .pl_can_select = pl_system_can_select, .pl_new = pl_system_new, .pl_delete = pl_system_delete, .pl_add_track = pl_system_add_track, .pl_remove_track = pl_system_remove_track, .pl_update = pl_system_update, .pl_set_flag = pl_system_set_flag, .pl_sort = pl_system_sort, .pl_next = pl_system_next, }; void pl_system_init(struct queue_ops *ops) { struct sys_playlist *sys_pl; unsigned int i; idle_schedule(IDLE_SYNC, __sys_pl_load_new, NULL); for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { sys_pl = sys_playlists[i]; sys_pl->spl_init(&sys_pl->spl_playlist, Q_REPEAT, ops); } } void pl_system_deinit() { unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) queue_deinit(__sys_pl_queue(i)); } void pl_system_new_track(struct track *track) { pl_system_add_track("Collection", track); pl_system_add_track("Unplayed", track); } void pl_system_delete_track(struct track *track) { struct sys_playlist *sys_pl; unsigned int i; for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) { sys_pl = sys_playlists[i]; sys_pl->spl_remove(&sys_pl->spl_playlist, track); } }