103 lines
1.9 KiB
C++
103 lines
1.9 KiB
C++
// Copyright (c) 2012 Bryan Schumaker.
|
|
#include <playlist.h>
|
|
#include <track.h>
|
|
#include <print.h>
|
|
|
|
#include <algorithm>
|
|
using namespace std;
|
|
|
|
static bool compare_tracks(libsaria::Track *one, libsaria::Track *two)
|
|
{
|
|
/* I want to compare tracks and not pointers */
|
|
return *one < *two;
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void Playlist::add_track(Track *track, unsigned int ins_index)
|
|
{
|
|
track->add_playlist(this);
|
|
notify_ui(PLAYLIST_ADD, track, ins_index);
|
|
schedule_save();
|
|
if (plist.size() == 1)
|
|
cur = 0;
|
|
}
|
|
|
|
void Playlist::remove_index(unsigned int rm_index)
|
|
{
|
|
if (rm_index >= plist.size())
|
|
return;
|
|
|
|
Track *track = plist[rm_index];
|
|
track->rm_playlist(this);
|
|
plist.erase(plist.begin() + rm_index);
|
|
if (cur > rm_index)
|
|
cur -= 1;
|
|
if (cur >= plist.size())
|
|
cur = 0;
|
|
notify_ui(PLAYLIST_RM, track, rm_index);
|
|
schedule_save();
|
|
}
|
|
|
|
void Playlist::remove_track(Track *track)
|
|
{
|
|
remove_index(find_index(track));
|
|
}
|
|
|
|
void Playlist::remove_indices(list<unsigned int> &indices)
|
|
{
|
|
list<unsigned int>::iterator it;
|
|
|
|
if (check_flag(PL_STATIC))
|
|
return;
|
|
indices.sort();
|
|
|
|
for (it = indices.begin(); it != indices.end(); it++)
|
|
remove_index(*it);
|
|
}
|
|
|
|
void Playlist::push_front(Track *track)
|
|
{
|
|
if (check_flag(PL_UNIQUE))
|
|
remove_track(track);
|
|
|
|
plist.insert(plist.begin(), track);
|
|
add_track(track, 0);
|
|
|
|
if (check_flag(PL_SORTED))
|
|
do_sort();
|
|
}
|
|
|
|
void Playlist::push_back(Track *track)
|
|
{
|
|
if (check_flag(PL_UNIQUE))
|
|
remove_track(track);
|
|
|
|
plist.push_back(track);
|
|
add_track(track, plist.size());
|
|
|
|
if (check_flag(PL_SORTED))
|
|
do_sort();
|
|
}
|
|
|
|
void Playlist::push_back(list<Track *> &tracks)
|
|
{
|
|
list<Track *>::iterator it;
|
|
for (it = tracks.begin(); it != tracks.end(); it++) {
|
|
plist.push_back(*it);
|
|
add_track(*it, plist.size());
|
|
}
|
|
|
|
if (check_flag(PL_SORTED))
|
|
do_sort();
|
|
}
|
|
|
|
void Playlist::do_sort()
|
|
{
|
|
sort(plist.begin(), plist.end(), compare_tracks);
|
|
notify_update_all();
|
|
}
|
|
|
|
}; /* namespace: libsaria */
|