2012-02-18 10:01:30 -05:00
|
|
|
// Copyright (c) 2012 Bryan Schumaker.
|
2012-09-12 08:15:31 -04:00
|
|
|
#include <track.h>
|
|
|
|
#include <library.h>
|
|
|
|
#include <playlist.h>
|
2012-02-18 10:01:30 -05:00
|
|
|
|
2012-11-03 21:48:29 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-02-18 10:01:30 -05:00
|
|
|
namespace libsaria
|
|
|
|
{
|
|
|
|
|
2012-11-03 15:34:13 -04:00
|
|
|
Playlist::Playlist(unsigned int f)
|
2012-02-19 17:13:12 -05:00
|
|
|
{
|
2012-03-29 20:59:04 -04:00
|
|
|
number = -1;
|
2012-03-18 14:45:29 -04:00
|
|
|
flags = f;
|
2012-11-02 23:00:20 -04:00
|
|
|
cur = 0;
|
2012-02-19 17:13:12 -05:00
|
|
|
}
|
|
|
|
|
2012-02-18 10:01:30 -05:00
|
|
|
Playlist::~Playlist()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-03 22:34:03 -04:00
|
|
|
void Playlist::set_flag(PlaylistFlags flag, bool enabled)
|
2012-09-14 20:25:09 -04:00
|
|
|
{
|
2012-11-03 22:34:03 -04:00
|
|
|
if (enabled) {
|
2012-09-14 20:25:09 -04:00
|
|
|
flags |= flag;
|
2012-11-03 22:34:03 -04:00
|
|
|
if (flag == PL_SORTED)
|
|
|
|
do_sort();
|
|
|
|
} else
|
2012-09-14 20:25:09 -04:00
|
|
|
flags &= ~flag;
|
|
|
|
schedule_save();
|
2012-11-03 22:34:03 -04:00
|
|
|
notify(PLAYLIST_CHANGED, this);
|
2012-09-14 20:25:09 -04:00
|
|
|
}
|
|
|
|
|
2012-11-03 22:34:03 -04:00
|
|
|
bool Playlist::check_flag(PlaylistFlags flag)
|
2012-09-14 20:25:09 -04:00
|
|
|
{
|
2012-11-03 22:34:03 -04:00
|
|
|
return (flags & flag) == flag;
|
2012-05-18 11:25:51 -04:00
|
|
|
}
|
|
|
|
|
2012-05-19 19:42:17 -04:00
|
|
|
void Playlist::track_updated(Track *track)
|
|
|
|
{
|
2012-08-30 09:57:31 -04:00
|
|
|
notify_ui(PLAYLIST_UPDATE, track, find_index(track));
|
2012-05-19 19:42:17 -04:00
|
|
|
}
|
|
|
|
|
2012-03-25 09:09:14 -04:00
|
|
|
void Playlist::prepare_for_removal()
|
|
|
|
{
|
2012-11-02 23:00:20 -04:00
|
|
|
vector<libsaria::Track *>::iterator it;
|
2012-05-19 20:32:08 -04:00
|
|
|
|
|
|
|
/* Remove playlist from tracks */
|
|
|
|
for (it = plist.begin(); it != plist.end(); it++)
|
|
|
|
(*it)->rm_playlist(this);
|
|
|
|
|
2012-04-07 09:00:06 -04:00
|
|
|
rm_file();
|
2012-03-25 09:09:14 -04:00
|
|
|
}
|
|
|
|
|
2012-03-29 20:59:04 -04:00
|
|
|
void Playlist::renumber(int n)
|
2012-03-26 18:50:26 -04:00
|
|
|
{
|
2012-03-29 20:59:04 -04:00
|
|
|
if (number == n)
|
|
|
|
return;
|
2012-04-07 09:00:06 -04:00
|
|
|
if (n < number)
|
|
|
|
rm_file();
|
2012-03-29 20:59:04 -04:00
|
|
|
number = n;
|
2012-04-07 09:00:06 -04:00
|
|
|
schedule_save();
|
2012-09-02 12:55:08 -04:00
|
|
|
notify(PLAYLIST_RENUMBER, this);
|
2012-03-29 21:45:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Playlist::get_number()
|
|
|
|
{
|
|
|
|
return number;
|
2012-03-26 18:50:26 -04:00
|
|
|
}
|
|
|
|
|
2012-03-20 17:14:40 -04:00
|
|
|
unsigned int Playlist::get_size()
|
|
|
|
{
|
|
|
|
return plist.size();
|
|
|
|
}
|
|
|
|
|
2012-03-28 08:33:49 -04:00
|
|
|
unsigned int Playlist::get_length()
|
|
|
|
{
|
2012-11-03 15:59:54 -04:00
|
|
|
unsigned int res = 0;
|
|
|
|
for (unsigned int i = 0; i < plist.size(); i++)
|
2012-11-24 17:01:49 -05:00
|
|
|
res += plist[i]->length;
|
2012-11-03 15:59:54 -04:00
|
|
|
return res;
|
2012-03-28 08:33:49 -04:00
|
|
|
}
|
|
|
|
|
2012-03-25 10:06:37 -04:00
|
|
|
void Playlist::reset_iterator()
|
|
|
|
{
|
2012-11-02 23:00:20 -04:00
|
|
|
cur = 0;
|
|
|
|
notify_ui(PLAYLIST_GOTO, plist[cur], 0);
|
2012-03-25 10:06:37 -04:00
|
|
|
}
|
|
|
|
|
2012-08-30 09:57:31 -04:00
|
|
|
unsigned int Playlist::find_index(Track *track)
|
|
|
|
{
|
2012-11-03 17:03:16 -04:00
|
|
|
for (unsigned int i = 0; i < plist.size(); i++) {
|
|
|
|
if (plist[i] == track)
|
|
|
|
return i;
|
2012-08-30 09:57:31 -04:00
|
|
|
}
|
2012-11-03 17:03:16 -04:00
|
|
|
return plist.size();
|
2012-08-30 09:57:31 -04:00
|
|
|
}
|
|
|
|
|
2012-11-03 21:48:29 -04:00
|
|
|
Track *Playlist::next()
|
|
|
|
{
|
|
|
|
Track *track;
|
2012-11-11 14:03:14 -05:00
|
|
|
unsigned int size = get_size();
|
2012-11-03 21:48:29 -04:00
|
|
|
|
2012-11-11 14:03:14 -05:00
|
|
|
if (size == 0)
|
2012-11-03 21:48:29 -04:00
|
|
|
return NULL;
|
2012-11-11 14:03:14 -05:00
|
|
|
else if (size == 1)
|
|
|
|
cur = 0;
|
|
|
|
else if (check_flag(PL_RANDOM))
|
2012-11-08 08:10:17 -05:00
|
|
|
cur += rand() % ((get_size() * 3) / 4);
|
2012-11-03 21:48:29 -04:00
|
|
|
else if (flags & PL_NO_DRAIN)
|
|
|
|
cur++;
|
|
|
|
|
2012-11-11 14:03:14 -05:00
|
|
|
if (cur >= size)
|
|
|
|
cur -= size;
|
2012-11-03 21:48:29 -04:00
|
|
|
|
|
|
|
track = plist[cur];
|
|
|
|
if (!(flags & PL_NO_DRAIN))
|
|
|
|
remove_index(cur);
|
|
|
|
|
|
|
|
if (get_size() != 0)
|
|
|
|
notify_ui(PLAYLIST_GOTO, track, cur);
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2012-08-24 08:31:40 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-09-17 08:16:59 -04:00
|
|
|
void Playlist::notify_update_all()
|
|
|
|
{
|
2012-11-03 22:34:03 -04:00
|
|
|
for (unsigned int i = 0; i < plist.size(); i++)
|
|
|
|
notify_ui(PLAYLIST_UPDATE, plist[i], i);
|
2012-09-17 08:16:59 -04:00
|
|
|
}
|
|
|
|
|
2012-02-18 10:01:30 -05:00
|
|
|
}
|