libsaria: Save temporary playlists

I do this whenever the playlist changes (tracks added, removed or
playlist renumbered).  When playlists are deleted I remove the file.  I
also remove the file when new_number < cur_number.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-04-07 09:00:06 -04:00
parent 3f030872ba
commit 60d2e4fcd8
14 changed files with 117 additions and 8 deletions

View File

@ -11,6 +11,7 @@ namespace libsaria
int size();
void run_task();
void queue_task(IdleTask *, bool);
void cancel_all(void *);
void enable();
float progress();

View File

@ -3,6 +3,7 @@
#define LIBSARIA_PLAYLIST_H
#include <libsaria/index.h>
#include <libsaria/fs.h>
#include <list>
#include <string>
@ -38,9 +39,13 @@ namespace libsaria
unsigned int flags;
unsigned int length;
PlaylistType type;
DataState data_state;
list<Track *>::iterator cur;
Index index;
void schedule_save();
void rm_file();
void incr_iter();
Track *picked_next();
unsigned int find_cur_index();
@ -65,6 +70,7 @@ namespace libsaria
void set_renderer(PlaylistRenderer *);
PlaylistRenderer *get_renderer();
void save(ofstream &);
bool is_static();
void prepare_for_removal();
void reset_iterator();
@ -106,6 +112,7 @@ namespace libsaria
void add_tracks(list<Track *> &);
};
class List : public Playlist {
public:
List(unsigned int);

View File

@ -10,6 +10,7 @@ class IdleTask
IdleTask();
virtual ~IdleTask() = 0;
virtual void run_task() = 0;
virtual bool should_cancel(void *);
void queue();
void queue_front();
@ -25,6 +26,7 @@ class WriteTask : public IdleTask
public:
WriteTask(string, void (*)(ofstream &, void *), DataState *, void *);
~WriteTask();
bool should_cancel(void *);
void run_task();
};

View File

@ -55,6 +55,8 @@ namespace libsaria
int &get_length();
unsigned int &get_year();
unsigned int &get_track();
unsigned int &get_libid();
unsigned int &get_id();
bool operator<(Track &);
};

View File

@ -2,11 +2,11 @@
#include <libsaria/idle.h>
#include <libsaria/print.h>
#include <queue>
#include <list>
using namespace std;
static bool enabled = false;
static deque<IdleTask *> idle_queue;
static list<IdleTask *> idle_queue;
static float queued = 0.0;
static float serviced = 0.0;
@ -65,6 +65,17 @@ namespace libsaria
queued += 1.0;
}
void idle::cancel_all(void *data)
{
list<IdleTask *>::iterator it;
for (it = idle_queue.begin(); it != idle_queue.end(); it++) {
if ((*it)->should_cancel(data)) {
it = idle_queue.erase(it);
it--;
}
}
}
void idle::enable()
{
enabled = true;

View File

@ -29,6 +29,11 @@ void WriteTask::run_task()
stream.close();
}
bool WriteTask::should_cancel(void *d)
{
return d == data;
}
ReadTask::ReadTask(string file, void (*fn)(ifstream &))
{
filepath = file;

View File

@ -18,3 +18,8 @@ void IdleTask::queue_front()
{
libsaria::idle::queue_task(this, true);
}
bool IdleTask::should_cancel(void *data)
{
return false;
}

View File

@ -18,6 +18,7 @@ namespace libsaria
length += track->get_length();
index.add_track(track);
RENDER( insert(track, ins_index) );
data_state = DIRTY;
}
void Playlist::remove_track_it(list<Track *>::iterator &it, unsigned int rm_index)
@ -26,6 +27,7 @@ namespace libsaria
index.remove_track(*it);
it = plist.erase(it);
RENDER( remove_index(rm_index) );
data_state = DIRTY;
}
void Playlist::do_remove_tracks(list<Track *> &tracks)
@ -49,6 +51,7 @@ namespace libsaria
index++;
}
schedule_save();
RENDER( modify_done() );
}
@ -91,6 +94,7 @@ namespace libsaria
if (update_cur)
cur = plist.begin();
schedule_save();
RENDER(modify_done());
}
@ -108,6 +112,7 @@ namespace libsaria
if (plist.size() == tracks.size())
cur = plist.begin();
schedule_save();
RENDER(modify_done());
}
@ -122,6 +127,7 @@ namespace libsaria
add_track(*it, 0);
}
schedule_save();
RENDER(modify_done());
}

View File

@ -139,6 +139,7 @@ namespace libsaria
void deck::init()
{
app::mkdir("playlist");
push(&recent_plist);
}

View File

@ -1,24 +1,71 @@
// Copyright (c) 2012 Bryan Schumaker.
#include <libsaria/library.h>
#include <libsaria/playlist.h>
#include <libsaria/track.h>
#include <libsaria/idle.h>
#include <fstream>
#include <sstream>
using namespace std;
static string plistdir = "playlist";
static string number_to_filename(unsigned int id)
{
stringstream s;
s << id;
return s.str();
}
#include <stdio.h>
/*static void load_playlist(void *plist)
{
libsaria::Playlist *playlist = (libsaria::Playlist *)plist;
playlist->do_load();
}
}*/
static void save_playlist(void *plist)
static void save_playlist(ofstream &stream, void *plist)
{
libsaria::Playlist *playlist = (libsaria::Playlist *)plist;
playlist->do_save();
playlist->save(stream);
}
*/
namespace libsaria
{
void Playlist::save(ofstream &stream)
{
list<Track *>::iterator it;
stream << "1" << "\n";
stream << type << " " << plist.size() << "\n";
for (it = plist.begin(); it != plist.end(); it++) {
stream << (*it)->get_libid() << " ";
stream << (*it)->get_id() << "\n";
}
}
void Playlist::schedule_save()
{
string filepath;
if (number < 0)
return;
filepath = plistdir + "/" + number_to_filename(number);
app::save(filepath, save_playlist, &data_state, this);
}
void Playlist::rm_file()
{
string filepath;
if (number < 0)
return;
idle::cancel_all(this);
filepath = plistdir + "/" + number_to_filename(number);
app::rm(filepath);
}
/*void Playlist::do_load()
{
unsigned int size;

View File

@ -30,6 +30,7 @@ namespace libsaria
}
remove_track_it(cur, index);
schedule_save();
if (cur == plist.end())
cur = plist.begin();

View File

@ -40,6 +40,7 @@ namespace libsaria
renderer = NULL;
}
}
rm_file();
}
void Playlist::set_filter_text(string &text)
@ -62,7 +63,10 @@ namespace libsaria
{
if (number == n)
return;
if (n < number)
rm_file();
number = n;
schedule_save();
RENDER( renumbered(number) );
}

View File

@ -0,0 +1,7 @@
// Copyright (c) 2012 Bryan Schumaker.
#ifndef PLAYLIST_INTERNAL_H
#define PLAYLIST_INTERNAL_H
void schedule_save(libsaria::Playlist *);
#endif PLAYLIST_INTERNAL_H

View File

@ -54,6 +54,16 @@ namespace libsaria
return track;
}
unsigned int &Track::get_id()
{
return id;
}
unsigned int &Track::get_libid()
{
return path->id;
}
/* int Track::get_length()
{
return length;