2014-09-15 08:26:52 -04:00
|
|
|
/**
|
2013-12-22 17:24:09 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-12-07 09:35:58 -05:00
|
|
|
extern "C" {
|
|
|
|
#include <core/collection.h>
|
2015-12-09 09:55:55 -05:00
|
|
|
#include <core/string.h>
|
2015-12-07 09:35:58 -05:00
|
|
|
}
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/playlist.h>
|
2013-12-22 17:24:09 -05:00
|
|
|
|
2013-12-22 18:12:57 -05:00
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
class PlaylistQueue : public queue {
|
2014-05-18 21:09:46 -04:00
|
|
|
public:
|
2014-02-02 14:20:56 -05:00
|
|
|
|
2015-02-06 14:11:58 -05:00
|
|
|
void clear()
|
2014-05-18 21:09:46 -04:00
|
|
|
{
|
2016-01-29 10:12:09 -05:00
|
|
|
queue_clear(this);
|
2015-02-06 14:11:58 -05:00
|
|
|
}
|
|
|
|
|
2015-02-06 15:08:31 -05:00
|
|
|
unsigned int find_average_count()
|
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *track, *next;
|
2015-02-06 15:08:31 -05:00
|
|
|
unsigned int total = 0, count = 0;
|
|
|
|
|
2015-10-25 05:06:23 -04:00
|
|
|
db_for_each(track, next, track_db_get()) {
|
2015-11-04 12:23:00 -05:00
|
|
|
total += TRACK(track)->tr_count;
|
2015-10-25 05:06:23 -04:00
|
|
|
count++;
|
2015-02-06 15:08:31 -05:00
|
|
|
}
|
2015-12-02 15:25:34 -05:00
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
return total / count;
|
|
|
|
return 0;
|
2015-02-06 15:08:31 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void dynamic_add(const std::string &name, struct track *track, unsigned int avg)
|
2015-02-06 15:08:31 -05:00
|
|
|
{
|
2015-10-27 17:34:00 -04:00
|
|
|
if ((name == "Unplayed") && (track->tr_count == 0))
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(this, track);
|
2015-10-27 17:34:00 -04:00
|
|
|
if ((name == "Most Played") && (track->tr_count > avg))
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(this, track);
|
2015-12-10 10:28:02 -05:00
|
|
|
if ((name == "Least Played") && (track->tr_count < avg) &&
|
|
|
|
(track->tr_count > 0))
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(this, track);
|
2015-02-06 15:08:31 -05:00
|
|
|
}
|
|
|
|
|
2015-02-06 14:31:37 -05:00
|
|
|
void dynamic_fill(const std::string &name)
|
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *track, *next;
|
2015-02-06 15:08:31 -05:00
|
|
|
unsigned int avg = 0;
|
2015-02-06 14:31:37 -05:00
|
|
|
|
2015-02-06 15:32:59 -05:00
|
|
|
if ((name == "Most Played") || (name == "Least Played"))
|
2015-02-06 15:08:31 -05:00
|
|
|
avg = find_average_count();
|
2015-02-06 14:31:37 -05:00
|
|
|
|
|
|
|
clear();
|
2015-12-03 10:03:52 -05:00
|
|
|
queue_set_flag(this, Q_ADD_FRONT);
|
|
|
|
|
2015-10-25 05:06:23 -04:00
|
|
|
db_for_each(track, next, track_db_get())
|
2015-11-04 12:23:00 -05:00
|
|
|
dynamic_add(name, TRACK(track), avg);
|
2015-12-03 10:03:52 -05:00
|
|
|
|
|
|
|
queue_unset_flag(this, Q_ADD_FRONT);
|
|
|
|
queue_resort(this);
|
2015-02-06 14:31:37 -05:00
|
|
|
}
|
|
|
|
|
2014-05-18 21:09:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
static struct database playlist_db;
|
2014-05-18 21:09:46 -04:00
|
|
|
static PlaylistQueue playlist_q;
|
|
|
|
static std::string cur_plist;
|
2014-02-02 14:20:56 -05:00
|
|
|
|
2015-12-09 09:55:55 -05:00
|
|
|
static inline bool __playlist_is_static(const gchar *name)
|
|
|
|
{
|
|
|
|
if (string_compare(name, "Favorites") == 0)
|
|
|
|
return true;
|
|
|
|
return string_compare(name, "Banned") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool __playlist_is_dynamic(const gchar *name)
|
|
|
|
{
|
|
|
|
if (string_compare(name, "Most Played") == 0)
|
|
|
|
return true;
|
|
|
|
if (string_compare(name, "Least Played") == 0)
|
|
|
|
return true;
|
|
|
|
return string_compare(name, "Unplayed") == 0;
|
|
|
|
}
|
|
|
|
|
2015-12-10 09:48:16 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-18 21:09:46 -04:00
|
|
|
|
2015-12-09 08:11:21 -05:00
|
|
|
void playlist_init(struct queue_ops *ops)
|
2014-05-18 21:09:46 -04:00
|
|
|
{
|
2015-10-21 15:57:21 -04:00
|
|
|
struct set_iter it;
|
2014-05-24 13:31:45 -04:00
|
|
|
|
2015-11-29 19:45:39 -05:00
|
|
|
queue_init(&playlist_q, Q_ENABLED | Q_REPEAT, ops);
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&playlist_q, COMPARE_ARTIST, true);
|
|
|
|
queue_sort(&playlist_q, COMPARE_YEAR, false);
|
|
|
|
queue_sort(&playlist_q, COMPARE_TRACK, false);
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&playlist_q, Q_NO_SORT);
|
2015-11-23 08:36:29 -05:00
|
|
|
|
2015-09-29 16:25:16 -04:00
|
|
|
index_init(&playlist_db, "playlist.db", true);
|
2015-09-17 08:12:25 -04:00
|
|
|
db_load(&playlist_db);
|
2014-05-24 13:31:45 -04:00
|
|
|
|
2015-12-09 08:11:21 -05:00
|
|
|
index_entry *ent = playlist :: get_tracks("Banned");
|
2014-06-06 08:20:15 -04:00
|
|
|
if (!ent)
|
|
|
|
return;
|
|
|
|
|
2015-10-21 15:57:21 -04:00
|
|
|
set_for_each(&ent->ie_set, &it)
|
2015-12-05 12:12:01 -05:00
|
|
|
queue_remove_all(collection_get_queue(),
|
2015-11-29 16:34:51 -05:00
|
|
|
track_get(it.it_val));
|
2014-05-18 21:09:46 -04:00
|
|
|
}
|
2014-02-02 14:11:38 -05:00
|
|
|
|
2015-12-09 09:20:18 -05:00
|
|
|
void playlist_deinit()
|
|
|
|
{
|
|
|
|
queue_deinit(&playlist_q);
|
|
|
|
db_deinit(&playlist_db);
|
|
|
|
}
|
|
|
|
|
2015-12-10 08:26:51 -05:00
|
|
|
void playlist_add(const gchar *name, struct track *track)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2015-12-10 08:26:51 -05:00
|
|
|
if (!track || !name || !__playlist_is_static(name))
|
2014-05-18 21:09:46 -04:00
|
|
|
return;
|
|
|
|
|
2015-12-10 08:26:51 -05:00
|
|
|
if (!playlist_has(name, track)) {
|
|
|
|
index_insert(&playlist_db, name, track->tr_dbe.dbe_index);
|
2014-05-18 21:09:46 -04:00
|
|
|
if (cur_plist == name)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&playlist_q, track);
|
2015-12-10 08:26:51 -05:00
|
|
|
if (string_compare(name, "Banned") == 0)
|
2015-12-05 12:12:01 -05:00
|
|
|
queue_remove_all(collection_get_queue(), track);
|
2014-05-18 21:09:46 -04:00
|
|
|
}
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
|
|
|
|
2015-12-10 09:20:42 -05:00
|
|
|
void playlist_remove(const gchar *name, struct track *track)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2015-12-10 09:20:42 -05:00
|
|
|
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);
|
|
|
|
}
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
2013-12-22 20:53:06 -05:00
|
|
|
|
2015-12-09 09:31:12 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-12-09 09:55:55 -05:00
|
|
|
void playlist_select(const gchar *name)
|
2014-02-02 14:05:56 -05:00
|
|
|
{
|
2015-12-10 09:48:16 -05:00
|
|
|
struct index_entry *ent;
|
2014-02-02 14:05:56 -05:00
|
|
|
|
2015-12-09 09:55:55 -05:00
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (__playlist_is_static(name)) {
|
|
|
|
ent = INDEX_ENTRY(db_get(&playlist_db, name));
|
2015-12-10 09:48:16 -05:00
|
|
|
__playlist_fill(name, ent ? &ent->ie_set : NULL);
|
|
|
|
} else if (__playlist_is_dynamic(name)) {
|
2015-02-06 14:31:37 -05:00
|
|
|
playlist_q.dynamic_fill(name);
|
2015-12-10 09:48:16 -05:00
|
|
|
cur_plist = name;
|
|
|
|
}
|
2013-12-31 16:58:40 -05:00
|
|
|
}
|
|
|
|
|
2015-10-21 10:41:42 -04:00
|
|
|
index_entry *playlist :: get_tracks(const std::string &name)
|
2014-02-02 14:05:56 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
return INDEX_ENTRY(db_get(&playlist_db, name.c_str()));
|
2014-02-02 14:05:56 -05:00
|
|
|
}
|
|
|
|
|
2015-12-09 10:38:57 -05:00
|
|
|
struct queue *playlist_get_queue()
|
2013-12-31 16:58:40 -05:00
|
|
|
{
|
2014-05-18 21:09:46 -04:00
|
|
|
return &playlist_q;
|
2013-12-22 20:53:06 -05:00
|
|
|
}
|