141 lines
2.4 KiB
C++
141 lines
2.4 KiB
C++
// Copyright (c) 2012 Bryan Schumaker.
|
|
#include <track.h>
|
|
#include <library.h>
|
|
#include <playlist.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
Playlist::Playlist(unsigned int f)
|
|
{
|
|
number = -1;
|
|
flags = f;
|
|
cur = 0;
|
|
}
|
|
|
|
Playlist::~Playlist()
|
|
{
|
|
}
|
|
|
|
void Playlist::set_flag(PlaylistFlags flag, bool enabled)
|
|
{
|
|
if (enabled) {
|
|
flags |= flag;
|
|
if (flag == PL_SORTED)
|
|
do_sort();
|
|
} else
|
|
flags &= ~flag;
|
|
schedule_save();
|
|
notify(PLAYLIST_CHANGED, this);
|
|
}
|
|
|
|
bool Playlist::check_flag(PlaylistFlags flag)
|
|
{
|
|
return (flags & flag) == flag;
|
|
}
|
|
|
|
void Playlist::track_updated(Track *track)
|
|
{
|
|
notify_ui(PLAYLIST_UPDATE, track, find_index(track));
|
|
}
|
|
|
|
void Playlist::prepare_for_removal()
|
|
{
|
|
vector<libsaria::Track *>::iterator it;
|
|
|
|
/* Remove playlist from tracks */
|
|
for (it = plist.begin(); it != plist.end(); it++)
|
|
(*it)->rm_playlist(this);
|
|
|
|
rm_file();
|
|
}
|
|
|
|
void Playlist::renumber(int n)
|
|
{
|
|
if (number == n)
|
|
return;
|
|
if (n < number)
|
|
rm_file();
|
|
number = n;
|
|
schedule_save();
|
|
notify(PLAYLIST_RENUMBER, this);
|
|
}
|
|
|
|
unsigned int Playlist::get_number()
|
|
{
|
|
return number;
|
|
}
|
|
|
|
unsigned int Playlist::get_size()
|
|
{
|
|
return plist.size();
|
|
}
|
|
|
|
unsigned int Playlist::get_length()
|
|
{
|
|
unsigned int res = 0;
|
|
for (unsigned int i = 0; i < plist.size(); i++)
|
|
res += plist[i]->length;
|
|
return res;
|
|
}
|
|
|
|
void Playlist::reset_iterator()
|
|
{
|
|
cur = 0;
|
|
notify_ui(PLAYLIST_GOTO, plist[cur], 0);
|
|
}
|
|
|
|
unsigned int Playlist::find_index(Track *track)
|
|
{
|
|
for (unsigned int i = 0; i < plist.size(); i++) {
|
|
if (plist[i] == track)
|
|
return i;
|
|
}
|
|
return plist.size();
|
|
}
|
|
|
|
Track *Playlist::next()
|
|
{
|
|
Track *track;
|
|
unsigned int size = get_size();
|
|
|
|
if (size == 0)
|
|
return NULL;
|
|
else if (size == 1)
|
|
cur = 0;
|
|
else if (check_flag(PL_RANDOM))
|
|
cur += rand() % ((get_size() * 3) / 4);
|
|
else if (flags & PL_NO_DRAIN)
|
|
cur++;
|
|
|
|
if (cur >= size)
|
|
cur -= size;
|
|
|
|
track = plist[cur];
|
|
if (!(flags & PL_NO_DRAIN))
|
|
remove_index(cur);
|
|
|
|
if (get_size() != 0)
|
|
notify_ui(PLAYLIST_GOTO, track, cur);
|
|
return track;
|
|
}
|
|
|
|
void Playlist::notify_ui(notify_t type, Track *track, unsigned int index)
|
|
{
|
|
struct PlaylistNotification data;
|
|
data.plist = this;
|
|
data.track = track;
|
|
data.index = index;
|
|
notify(type, &data);
|
|
}
|
|
|
|
void Playlist::notify_update_all()
|
|
{
|
|
for (unsigned int i = 0; i < plist.size(); i++)
|
|
notify_ui(PLAYLIST_UPDATE, plist[i], i);
|
|
}
|
|
|
|
}
|