2013-12-22 17:24:09 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2013-12-31 15:44:44 -05:00
|
|
|
#include <database.h>
|
2013-12-31 16:58:40 -05:00
|
|
|
#include <error.h>
|
|
|
|
#include <playlist.h>
|
2013-12-22 17:24:09 -05:00
|
|
|
|
2013-12-31 15:44:44 -05:00
|
|
|
static std::set<unsigned int> empty_set;
|
2013-12-31 16:58:40 -05:00
|
|
|
static Database<IndexEntry> playlist_db("playlist.db");
|
2014-02-02 14:05:56 -05:00
|
|
|
static Playqueue playlist_pq(PQ_ENABLED);
|
|
|
|
static std::string cur_pq;
|
2013-12-22 18:12:57 -05:00
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
void playlist :: init()
|
2013-12-22 17:24:09 -05:00
|
|
|
{
|
2014-02-02 14:05:56 -05:00
|
|
|
std::set<unsigned int> ids;
|
|
|
|
std::set<unsigned int>::iterator it;
|
|
|
|
|
|
|
|
playlist_pq.add_sort(SORT_ARTIST);
|
|
|
|
playlist_pq.add_sort(SORT_YEAR);
|
|
|
|
playlist_pq.add_sort(SORT_TRACK);
|
|
|
|
playlist_pq.set_flag(PQ_NEVER_SORT);
|
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
playlist_db.load();
|
2014-02-02 14:05:56 -05:00
|
|
|
if (cur_pq == "")
|
|
|
|
return;
|
|
|
|
|
|
|
|
ids = get_tracks(cur_pq);
|
|
|
|
for (it = ids.begin(); it != ids.end(); it++)
|
|
|
|
playlist_pq.add(*it);
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
void playlist :: add(const std::string &name, unsigned int track_id)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2013-12-31 16:58:40 -05:00
|
|
|
if ((name == "Banned") || (name == "Favorites")) {
|
|
|
|
index_insert(playlist_db, name, track_id);
|
|
|
|
playlist_db.save();
|
2014-02-02 14:05:56 -05:00
|
|
|
if (name == cur_pq)
|
|
|
|
playlist_pq.add(track_id);
|
2013-12-31 16:58:40 -05:00
|
|
|
} else
|
2014-01-04 11:55:52 -05:00
|
|
|
throw -E_EXIST;
|
2013-12-22 17:53:58 -05:00
|
|
|
}
|
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
void playlist :: del(const std::string &name, unsigned int track_id)
|
2013-12-22 17:53:58 -05:00
|
|
|
{
|
2013-12-31 16:58:40 -05:00
|
|
|
if ((name == "Banned") || (name == "Favorites")) {
|
|
|
|
index_remove(playlist_db, name, track_id);
|
|
|
|
playlist_db.save();
|
2014-02-02 14:05:56 -05:00
|
|
|
if (name == cur_pq)
|
|
|
|
playlist_pq.del_track(track_id);
|
2013-12-31 16:58:40 -05:00
|
|
|
} else
|
2014-01-04 11:55:52 -05:00
|
|
|
throw -E_EXIST;
|
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)
|
|
|
|
{
|
|
|
|
std::set<unsigned int> ids = get_tracks(name);
|
|
|
|
std::set<unsigned int>::iterator it;
|
|
|
|
|
|
|
|
while (playlist_pq.size() > 0)
|
|
|
|
playlist_pq.del(0);
|
|
|
|
|
|
|
|
for (it = ids.begin(); it != ids.end(); it++)
|
|
|
|
playlist_pq.add(*it);
|
|
|
|
cur_pq = name;
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
const std::set<unsigned int> &playlist :: get_tracks(const std::string &name)
|
2013-12-22 20:53:06 -05:00
|
|
|
{
|
2013-12-31 16:58:40 -05:00
|
|
|
if ((name == "Banned") || (name == "Favorites")) {
|
|
|
|
try {
|
|
|
|
return playlist_db.find(name).values;
|
|
|
|
} catch (int error) {
|
|
|
|
return empty_set;
|
|
|
|
}
|
2013-12-22 20:53:06 -05:00
|
|
|
}
|
2014-01-04 11:55:52 -05:00
|
|
|
throw -E_EXIST;
|
2013-12-31 16:58:40 -05:00
|
|
|
}
|
|
|
|
|
2014-02-02 14:05:56 -05:00
|
|
|
Playqueue *playlist :: get_pq()
|
|
|
|
{
|
|
|
|
return &playlist_pq;
|
|
|
|
}
|
|
|
|
|
2013-12-31 16:58:40 -05:00
|
|
|
#ifdef CONFIG_TEST
|
|
|
|
void playlist :: clear()
|
|
|
|
{
|
|
|
|
playlist_db.clear();
|
2013-12-22 20:53:06 -05:00
|
|
|
}
|
2013-12-31 16:58:40 -05:00
|
|
|
#endif /* CONFIG_TEST */
|