libsaria: Move the rest of the deck code over
I also clean up the garbage collect code a bit. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
acb0334122
commit
98934ff42d
|
@ -38,23 +38,55 @@ static bool check_play()
|
|||
return true;
|
||||
}
|
||||
|
||||
static void remove_playlist(libsaria::Playlist *plist)
|
||||
{
|
||||
for (unsigned int i = 0; i < playlist_deck.size(); i++) {
|
||||
if (playlist_deck[i] == plist) {
|
||||
playlist_deck.erase(playlist_deck.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void renumber_playlists()
|
||||
{
|
||||
for (unsigned int i = 0; i < playlist_deck.size(); i++)
|
||||
playlist_deck[i]->renumber(i);
|
||||
}
|
||||
|
||||
static void read_playlist(ifstream &stream)
|
||||
{
|
||||
libsaria::Playlist *plist = new libsaria::Playlist(0);
|
||||
playlist_deck.push_back(plist);
|
||||
notify(PLAYLIST_NEW, plist);
|
||||
plist->load(stream);
|
||||
plist->renumber(playlist_deck.size() - 1);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void deck::garbage_collect()
|
||||
{
|
||||
for (unsigned int i = 0; i < playlist_deck.size(); i++) {
|
||||
Playlist *plist = playlist_deck[i];
|
||||
if (plist->get_size() == 0) {
|
||||
plist->prepare_for_removal();
|
||||
notify(PLAYLIST_DELETE, plist);
|
||||
delete plist;
|
||||
playlist_deck.erase(playlist_deck.begin() + i);
|
||||
}
|
||||
}
|
||||
renumber_playlists();
|
||||
}
|
||||
|
||||
void deck::next()
|
||||
{
|
||||
Playlist *plist = get_active_playlist();
|
||||
Track *track = plist->next();
|
||||
if (track)
|
||||
track->load(check_play());
|
||||
garbage_collect();
|
||||
}
|
||||
|
||||
void deck::prev()
|
||||
|
@ -64,6 +96,50 @@ namespace libsaria
|
|||
track->load_unlisted(true);
|
||||
}
|
||||
|
||||
Playlist *deck::new_playlist(list<Track *> &tracks, unsigned int flags, bool front)
|
||||
{
|
||||
Playlist *plist;
|
||||
|
||||
if (playlist_deck.size() == MAX_PLAYLISTS)
|
||||
return NULL;
|
||||
plist = new Playlist(flags);
|
||||
|
||||
if (front)
|
||||
playlist_deck.insert(playlist_deck.begin(), plist);
|
||||
else
|
||||
playlist_deck.push_back(plist);
|
||||
notify(PLAYLIST_NEW, plist);
|
||||
|
||||
renumber_playlists();
|
||||
plist->push_back(tracks);
|
||||
return plist;
|
||||
}
|
||||
|
||||
void deck::delete_playlist(Playlist *plist)
|
||||
{
|
||||
plist->prepare_for_removal();
|
||||
notify(PLAYLIST_DELETE, plist);
|
||||
remove_playlist(plist);
|
||||
garbage_collect();
|
||||
}
|
||||
|
||||
unsigned int deck::move_playlist(Playlist *plist, unsigned int index)
|
||||
{
|
||||
if (index >= playlist_deck.size())
|
||||
index = playlist_deck.size() - 1;
|
||||
|
||||
remove_playlist(plist);
|
||||
playlist_deck.insert(playlist_deck.begin() + index, plist);
|
||||
renumber_playlists();
|
||||
return index;
|
||||
}
|
||||
|
||||
void deck::track_removed(Track *track)
|
||||
{
|
||||
for (unsigned int i = 0; i < playlist_deck.size(); i++)
|
||||
playlist_deck[i]->remove_track(track);
|
||||
}
|
||||
|
||||
Playlist *deck::get_recent_plist()
|
||||
{
|
||||
return &recent_playlist;
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
// Copyright (c) 2012 Bryan Schumaker.
|
||||
#include <playlist.h>
|
||||
#include <library.h>
|
||||
#include <notify.h>
|
||||
#include <audio.h>
|
||||
#include <track.h>
|
||||
#include <print.h>
|
||||
#include <deck.h>
|
||||
|
||||
#include <list>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
static list<libsaria::Playlist *> playlist_deck;
|
||||
|
||||
static void renumber_playlists()
|
||||
{
|
||||
list<libsaria::Playlist *>::iterator it;
|
||||
int n = 0;
|
||||
|
||||
for (it = playlist_deck.begin(); it != playlist_deck.end(); it++)
|
||||
(*it)->renumber(n++);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void deck::garbage_collect()
|
||||
{
|
||||
int n = 0;
|
||||
list<libsaria::Playlist *>::iterator it;
|
||||
|
||||
for (it = playlist_deck.begin(); it != playlist_deck.end(); it++) {
|
||||
if ((*it)->get_size() == 0) {
|
||||
(*it)->prepare_for_removal();
|
||||
notify(PLAYLIST_DELETE, *it);
|
||||
delete (*it);
|
||||
it = playlist_deck.erase(it);
|
||||
it--;
|
||||
} else
|
||||
(*it)->renumber(n++);
|
||||
}
|
||||
}
|
||||
|
||||
Playlist *deck::new_playlist(list<Track *> &tracks, unsigned int flags, bool front)
|
||||
{
|
||||
Playlist *plist;
|
||||
|
||||
if (playlist_deck.size() == MAX_PLAYLISTS)
|
||||
return NULL;
|
||||
plist = new Playlist(flags);
|
||||
|
||||
if (front)
|
||||
playlist_deck.push_front(plist);
|
||||
else
|
||||
playlist_deck.push_back(plist);
|
||||
notify(PLAYLIST_NEW, plist);
|
||||
|
||||
renumber_playlists();
|
||||
|
||||
plist->push_back(tracks);
|
||||
return plist;
|
||||
}
|
||||
|
||||
void deck::delete_playlist(Playlist *plist)
|
||||
{
|
||||
plist->prepare_for_removal();
|
||||
notify(PLAYLIST_DELETE, plist);
|
||||
playlist_deck.remove(plist);
|
||||
garbage_collect();
|
||||
}
|
||||
|
||||
unsigned int deck::move_playlist(Playlist *plist, unsigned int index)
|
||||
{
|
||||
list<Playlist *>::iterator it;
|
||||
|
||||
if (index >= playlist_deck.size())
|
||||
index = playlist_deck.size() - 1;
|
||||
|
||||
playlist_deck.remove(plist);
|
||||
|
||||
it = playlist_deck.begin();
|
||||
for (unsigned int i = 0; i < index; i++)
|
||||
it++;
|
||||
|
||||
playlist_deck.insert(it, plist);
|
||||
garbage_collect();
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void deck::track_removed(Track *track)
|
||||
{
|
||||
list<Playlist *>::iterator it;
|
||||
for (it = playlist_deck.begin(); it != playlist_deck.end(); it++)
|
||||
(*it)->remove_track(track);
|
||||
}
|
||||
|
||||
|
||||
}; /* Namespace: libsaria */
|
Loading…
Reference in New Issue