2014-09-15 08:26:52 -04:00
|
|
|
/**
|
2013-12-22 17:24:09 -05:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/library.h>
|
|
|
|
#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-10-21 10:41:42 -04:00
|
|
|
void fill(index_entry *ent)
|
2015-02-06 14:11:58 -05:00
|
|
|
{
|
2015-10-21 15:57:21 -04:00
|
|
|
struct set_iter it;
|
2014-02-02 14:05:56 -05:00
|
|
|
|
2015-02-06 14:11:58 -05:00
|
|
|
clear();
|
2015-12-03 10:03:52 -05:00
|
|
|
queue_set_flag(this, Q_ADD_FRONT);
|
|
|
|
|
2015-10-21 15:57:21 -04:00
|
|
|
set_for_each(&ent->ie_set, &it)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(this, track_get(it.it_val));
|
2015-12-03 10:03:52 -05:00
|
|
|
|
|
|
|
queue_unset_flag(this, Q_ADD_FRONT);
|
|
|
|
queue_resort(this);
|
2014-05-18 21:09:46 -04: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-10-27 17:34:00 -04:00
|
|
|
if ((name == "Least Played") && (track->tr_count < avg))
|
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
|
|
|
|
2014-05-18 21:09:46 -04:00
|
|
|
|
2015-11-29 19:45:39 -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-10-21 10:41:42 -04:00
|
|
|
index_entry *ent = 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-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(collection :: get_queue(),
|
|
|
|
track_get(it.it_val));
|
2014-05-18 21:09:46 -04:00
|
|
|
}
|
2014-02-02 14:11:38 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
bool playlist :: has(struct track *track, const std::string &name)
|
2014-05-18 21:09:46 -04:00
|
|
|
{
|
2015-11-23 08:36:29 -05:00
|
|
|
if (playlist_db.db_size == 0)
|
|
|
|
return false;
|
2015-11-12 08:41:46 -05:00
|
|
|
return index_has(&playlist_db, name.c_str(), track->tr_dbe.dbe_index);
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void playlist :: add(struct track *track, const std::string &name)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2014-05-18 21:09:46 -04:00
|
|
|
if (!( (name == "Banned") || (name == "Favorites") ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!has(track, name)) {
|
2015-11-12 08:41:46 -05:00
|
|
|
index_insert(&playlist_db, name.c_str(), 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);
|
2014-05-24 13:31:45 -04:00
|
|
|
if (name == "Banned")
|
2015-11-29 16:34:51 -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-10-24 19:34:45 -04:00
|
|
|
void playlist :: del(struct track *track, const std::string &name)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2015-11-12 08:41:46 -05:00
|
|
|
index_remove(&playlist_db, name.c_str(), track->tr_dbe.dbe_index);
|
2014-05-18 21:09:46 -04:00
|
|
|
if (cur_plist == name)
|
2015-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(&playlist_q, track);
|
2014-05-24 13:31:45 -04:00
|
|
|
if (name == "Banned")
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(collection :: get_queue(), track);
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
2013-12-22 20:53:06 -05:00
|
|
|
|
2014-02-02 14:05:56 -05:00
|
|
|
void playlist :: select(const std::string &name)
|
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
index_entry *ent = INDEX_ENTRY(db_get(&playlist_db, name.c_str()));
|
2014-02-02 14:05:56 -05:00
|
|
|
|
2015-02-06 14:31:37 -05:00
|
|
|
if (ent != NULL)
|
|
|
|
playlist_q.fill(ent);
|
|
|
|
else
|
|
|
|
playlist_q.dynamic_fill(name);
|
2014-02-02 14:05:56 -05:00
|
|
|
|
2014-05-18 21:09:46 -04: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-11-16 09:12:15 -05:00
|
|
|
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
|
|
|
}
|