121 lines
2.3 KiB
C++
121 lines
2.3 KiB
C++
// Copyright (c) 2012 Bryan Schumaker.
|
|
#include <playlist.h>
|
|
#include <library.h>
|
|
#include <track.h>
|
|
#include <print.h>
|
|
#include <deck.h>
|
|
#include <idle.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
using namespace std;
|
|
|
|
#define PLAYLIST_CURRENT_VERSION 3 // 9 / 14 / 2012
|
|
|
|
static string plistdir = "playlist";
|
|
|
|
static string number_to_filename(unsigned int id)
|
|
{
|
|
stringstream s;
|
|
s << id;
|
|
return s.str();
|
|
}
|
|
|
|
static void save_playlist(ofstream &stream, void *plist)
|
|
{
|
|
libsaria::Playlist *playlist = (libsaria::Playlist *)plist;
|
|
playlist->save(stream);
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void Playlist::load(ifstream &stream)
|
|
{
|
|
unsigned int version, type, size, libid, trackid;
|
|
bool disabled = false;
|
|
bool random = false;
|
|
bool sorted = false;
|
|
Track *track;
|
|
|
|
number = -1;
|
|
flags = 0;
|
|
cur = 0;
|
|
|
|
stream >> version;
|
|
if (version < 3)
|
|
stream >> type;
|
|
stream >> size >> disabled;
|
|
|
|
if (version >= 3)
|
|
stream >> random >> sorted;
|
|
else if (type == 0) { /* Old PLIST_SET type */
|
|
random = true;
|
|
sorted = true;
|
|
}
|
|
|
|
println("playlist size: %u disabled: %d random: %d sorted: %d",
|
|
size, disabled, random, sorted);
|
|
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
stream >> libid >> trackid;
|
|
track = library::lookup(libid, trackid);
|
|
if (track)
|
|
push_back(track);
|
|
}
|
|
|
|
if (disabled)
|
|
flags |= PL_DISABLED;
|
|
if (random)
|
|
flags |= PL_RANDOM;
|
|
if (sorted) {
|
|
flags |= PL_SORTED;
|
|
do_sort();
|
|
}
|
|
notify(PLAYLIST_CHANGED, this);
|
|
}
|
|
|
|
void Playlist::save(ofstream &stream)
|
|
{
|
|
stream << PLAYLIST_CURRENT_VERSION << "\n";
|
|
stream << plist.size() << " ";
|
|
stream << check_flag(PL_DISABLED) <<" ";
|
|
stream << check_flag(PL_RANDOM) << " ";
|
|
stream << check_flag(PL_SORTED) << "\n";
|
|
|
|
for (unsigned int i = 0; i < plist.size(); i++) {
|
|
stream << plist[i]->path->id << " ";
|
|
stream << plist[i]->id << "\n";
|
|
}
|
|
}
|
|
|
|
void Playlist::schedule_save()
|
|
{
|
|
string filepath;
|
|
|
|
if (number < 0)
|
|
return;
|
|
else if (get_size() == 0) {
|
|
rm_file();
|
|
return;
|
|
}
|
|
|
|
filepath = plistdir + "/" + number_to_filename(number);
|
|
app::save(filepath, save_playlist, this);
|
|
}
|
|
|
|
void Playlist::rm_file()
|
|
{
|
|
string filepath;
|
|
|
|
if (number < 0)
|
|
return;
|
|
|
|
idle::cancel_all(this);
|
|
filepath = plistdir + "/" + number_to_filename(number);
|
|
app::rm(filepath);
|
|
}
|
|
|
|
}; /* Namespace: libsaria */
|