I kept around the old list while I was converting everything over to the new list. Now that I support all the needed features, I can remove the old variable. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
112 lines
2.4 KiB
C++
112 lines
2.4 KiB
C++
/*
|
|
* Copyright (c) 2011 Bryan Schumaker.
|
|
* Manages the libsaria library save file
|
|
*/
|
|
#include <libsaria/library.h>
|
|
#include <libsaria/track.h>
|
|
#include <libsaria/print.h>
|
|
#include <libsaria/ban.h>
|
|
#include <libsaria/fs.h>
|
|
#include "library.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
using namespace std;
|
|
|
|
static string libdir = "library";
|
|
|
|
static string id_to_filename(unsigned int id)
|
|
{
|
|
stringstream s;
|
|
s << id;
|
|
return s.str();
|
|
}
|
|
|
|
static void save_track(libsaria::Track &track, void *data)
|
|
{
|
|
track.save((ofstream *)data);
|
|
}
|
|
|
|
static void do_save_path(ofstream &stream, void *data)
|
|
{
|
|
libsaria::library::Path *path = (libsaria::library::Path *)data;
|
|
list<libsaria::Track>::iterator it;
|
|
println("Saving library path: %d", path->id);
|
|
|
|
stream << 2 << "\n"; /* Save file version: 2 */
|
|
stream << path->path << "\n";
|
|
stream << path->id << " " << path->visible << " ";
|
|
stream << path->next_track << " " << path->tracks.size() << "\n";
|
|
|
|
path->tracks.for_each_item(save_track, &stream);
|
|
}
|
|
|
|
void libsaria::library::save_path(libsaria::library::Path *path)
|
|
{
|
|
string filepath = libdir + "/" + id_to_filename(path->id);
|
|
libsaria::app::save(filepath, do_save_path, &path->data_state, path);
|
|
}
|
|
|
|
void read_path(ifstream &stream)
|
|
{
|
|
string tmp;
|
|
stringstream s;
|
|
unsigned int size;
|
|
unsigned int version;
|
|
libsaria::Track *track;
|
|
list<libsaria::Track *> tracks, banned;
|
|
libsaria::ListItem<libsaria::Track> *item;
|
|
struct libsaria::library::Path path, *path_ptr;
|
|
|
|
getline(stream, tmp);
|
|
s << tmp;
|
|
s >> version;
|
|
if (version > 2)
|
|
return;
|
|
|
|
getline(stream, path.path);
|
|
|
|
getline(stream, tmp);
|
|
s.clear();
|
|
s.str(tmp);
|
|
|
|
s >> path.id;
|
|
s >> path.visible;
|
|
s >> path.next_track;
|
|
s >> size;
|
|
path.data_state = CLEAN;
|
|
|
|
path_ptr = push_path(path);
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
item = path_ptr->tracks.push_back(libsaria::Track(stream, path_ptr, version));
|
|
track = &item->get_value();
|
|
if (track->get_banned())
|
|
banned.push_back(track);
|
|
else
|
|
tracks.push_back(track);
|
|
}
|
|
|
|
if (version != 2) {
|
|
path_ptr->data_state = DIRTY;
|
|
libsaria::library::save_path(path_ptr);
|
|
}
|
|
|
|
notify_path_updated(path_ptr);
|
|
if (path.visible) {
|
|
lib_playlist.add_tracks(tracks);
|
|
libsaria::ban::get_banned_plist()->add_tracks(banned);
|
|
}
|
|
}
|
|
|
|
unsigned int schedule_load()
|
|
{
|
|
return libsaria::app::read_numdir(libdir, read_path);
|
|
}
|
|
|
|
void remove_file(libsaria::library::Path *path)
|
|
{
|
|
string file = id_to_filename(path->id);
|
|
libsaria::app::rm(libdir + "/" + file);
|
|
}
|