181 lines
4.1 KiB
C++
181 lines
4.1 KiB
C++
/**
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
extern "C" {
|
|
#include <core/collection.h>
|
|
#include <core/string.h>
|
|
}
|
|
#include <core/playlist.h>
|
|
|
|
static struct database playlist_db;
|
|
static struct queue playlist_q;
|
|
static std::string cur_plist;
|
|
|
|
|
|
static inline bool __playlist_is_static(const gchar *name)
|
|
{
|
|
if (!name)
|
|
return false;
|
|
if (string_compare(name, "Favorites") == 0)
|
|
return true;
|
|
return string_compare(name, "Banned") == 0;
|
|
}
|
|
|
|
static inline bool __playlist_is_dynamic(const gchar *name)
|
|
{
|
|
if (!name)
|
|
return false;
|
|
if (string_compare(name, "Most Played") == 0)
|
|
return true;
|
|
if (string_compare(name, "Least Played") == 0)
|
|
return true;
|
|
return string_compare(name, "Unplayed") == 0;
|
|
}
|
|
|
|
static unsigned int __playlist_find_average()
|
|
{
|
|
struct db_entry *track, *next;
|
|
unsigned int total = 0;
|
|
|
|
if (track_db_get()->db_size == 0)
|
|
return 0;
|
|
|
|
db_for_each(track, next, track_db_get())
|
|
total += TRACK(track)->tr_count;
|
|
return total / track_db_get()->db_size;
|
|
}
|
|
|
|
static void __playlist_fill(const gchar *name, struct set *set)
|
|
{
|
|
struct set_iter it;
|
|
|
|
queue_clear(&playlist_q);
|
|
cur_plist = name;
|
|
|
|
if (!set)
|
|
return;
|
|
|
|
queue_set_flag(&playlist_q, Q_ADD_FRONT);
|
|
set_for_each(set, &it)
|
|
queue_add(&playlist_q, track_get(it.it_val));
|
|
queue_unset_flag(&playlist_q, Q_ADD_FRONT);
|
|
|
|
queue_resort(&playlist_q);
|
|
}
|
|
|
|
static void __playlist_fill_static(const gchar *name)
|
|
{
|
|
struct index_entry *ent = INDEX_ENTRY(db_get(&playlist_db, name));
|
|
__playlist_fill(name, ent ? &ent->ie_set : NULL);
|
|
}
|
|
|
|
static void __playlist_fill_dynamic(const gchar *name)
|
|
{
|
|
struct db_entry *track, *next;
|
|
unsigned int count = 0, average = 0;
|
|
struct set set;
|
|
|
|
if (string_compare(name, "Most Played") == 0 ||
|
|
string_compare(name, "Least Played") == 0)
|
|
average = __playlist_find_average();
|
|
|
|
set_init(&set);
|
|
|
|
db_for_each(track, next, track_db_get()) {
|
|
count = TRACK(track)->tr_count;
|
|
if (string_compare(name, "Unplayed") == 0 && count == 0)
|
|
set_insert(&set, track->dbe_index);
|
|
else if (string_compare(name, "Most Played") == 0 &&
|
|
count > average)
|
|
set_insert(&set, track->dbe_index);
|
|
else if (string_compare(name, "Least Played") == 0 &&
|
|
count < average && count > 0)
|
|
set_insert(&set, track->dbe_index);
|
|
}
|
|
|
|
__playlist_fill(name, &set);
|
|
set_deinit(&set);
|
|
}
|
|
|
|
|
|
void playlist_init(struct queue_ops *ops)
|
|
{
|
|
struct set_iter it;
|
|
|
|
queue_init(&playlist_q, Q_ENABLED | Q_REPEAT, ops);
|
|
queue_sort(&playlist_q, COMPARE_ARTIST, true);
|
|
queue_sort(&playlist_q, COMPARE_YEAR, false);
|
|
queue_sort(&playlist_q, COMPARE_TRACK, false);
|
|
queue_set_flag(&playlist_q, Q_NO_SORT);
|
|
|
|
index_init(&playlist_db, "playlist.db", true);
|
|
db_load(&playlist_db);
|
|
|
|
index_entry *ent = playlist :: get_tracks("Banned");
|
|
if (!ent)
|
|
return;
|
|
|
|
set_for_each(&ent->ie_set, &it)
|
|
queue_remove_all(collection_get_queue(),
|
|
track_get(it.it_val));
|
|
}
|
|
|
|
void playlist_deinit()
|
|
{
|
|
queue_deinit(&playlist_q);
|
|
db_deinit(&playlist_db);
|
|
}
|
|
|
|
void playlist_add(const gchar *name, struct track *track)
|
|
{
|
|
if (!track || !__playlist_is_static(name))
|
|
return;
|
|
|
|
if (!playlist_has(name, track)) {
|
|
index_insert(&playlist_db, name, track->tr_dbe.dbe_index);
|
|
if (cur_plist == name)
|
|
queue_add(&playlist_q, track);
|
|
if (string_compare(name, "Banned") == 0)
|
|
queue_remove_all(collection_get_queue(), track);
|
|
}
|
|
}
|
|
|
|
void playlist_remove(const gchar *name, struct track *track)
|
|
{
|
|
if (!track || !name)
|
|
return;
|
|
|
|
if (playlist_has(name, track)) {
|
|
index_remove(&playlist_db, name, track->tr_dbe.dbe_index);
|
|
if (cur_plist == name)
|
|
queue_remove_all(&playlist_q, track);
|
|
if (string_compare(name, "Banned") == 0)
|
|
queue_add(collection_get_queue(), track);
|
|
}
|
|
}
|
|
|
|
bool playlist_has(const gchar *name, struct track *track)
|
|
{
|
|
if (!track || !name)
|
|
return false;
|
|
return index_has(&playlist_db, name, track->tr_dbe.dbe_index);
|
|
}
|
|
|
|
void playlist_select(const gchar *name)
|
|
{
|
|
if (__playlist_is_static(name))
|
|
__playlist_fill_static(name);
|
|
else if (__playlist_is_dynamic(name))
|
|
__playlist_fill_dynamic(name);
|
|
}
|
|
|
|
index_entry *playlist :: get_tracks(const std::string &name)
|
|
{
|
|
return INDEX_ENTRY(db_get(&playlist_db, name.c_str()));
|
|
}
|
|
|
|
struct queue *playlist_get_queue()
|
|
{
|
|
return &playlist_q;
|
|
}
|