libsaria: Reload a saved library
And add tracks to the library playlist. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
d0a1b9fdf8
commit
fad3019d83
|
@ -18,10 +18,13 @@ namespace libsaria
|
|||
{
|
||||
|
||||
void init(string);
|
||||
|
||||
void list_dir(string &, list<string> &);
|
||||
void mkdir(string);
|
||||
void mkdir();
|
||||
|
||||
void save(string, void (*)(ofstream &, void *), DataState *, void *);
|
||||
void read(string, void (*)(ifstream &));
|
||||
void read_now(string, void (*)(ifstream &));
|
||||
|
||||
} /* Namespace: app */
|
||||
|
|
|
@ -28,6 +28,17 @@ class WriteTask : public IdleTask
|
|||
void run_task();
|
||||
};
|
||||
|
||||
class ReadTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
string filepath;
|
||||
void (*func)(ifstream &);
|
||||
public:
|
||||
ReadTask(string, void (*)(ifstream &));
|
||||
~ReadTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
class IOTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
|
|
|
@ -39,6 +39,7 @@ namespace libsaria
|
|||
|
||||
public:
|
||||
Track(string, library::Path *);
|
||||
Track(ifstream &, library::Path *);
|
||||
~Track();
|
||||
void save(ofstream &);
|
||||
/* Track();
|
||||
|
|
|
@ -59,6 +59,19 @@ namespace libsaria
|
|||
closedir(dp);
|
||||
}
|
||||
|
||||
void app::list_dir(string &dir, list<string> &file_list)
|
||||
{
|
||||
list<string> files;
|
||||
list<string>::iterator it;
|
||||
|
||||
string d = appdir + "/" + dir;
|
||||
libsaria::list_dir(d, files);
|
||||
|
||||
/* Strip out appdir information */
|
||||
for (it = files.begin(); it != files.end(); it++)
|
||||
file_list.push_back( (*it).substr(d.size() + 1) );
|
||||
}
|
||||
|
||||
void app::mkdir()
|
||||
{
|
||||
make_dir(appdir.c_str());
|
||||
|
@ -86,6 +99,11 @@ namespace libsaria
|
|||
new WriteTask(appdir + "/" + file, func, state, data);
|
||||
}
|
||||
|
||||
void app::read(string file, void (*func)(ifstream &))
|
||||
{
|
||||
new ReadTask(appdir + "/" + file, func);
|
||||
}
|
||||
|
||||
void app::read_now(string file, void (*func)(ifstream &))
|
||||
{
|
||||
ifstream stream;
|
||||
|
|
|
@ -55,3 +55,23 @@ void WriteTask::run_task()
|
|||
func(stream, data);
|
||||
stream.close();
|
||||
}
|
||||
|
||||
ReadTask::ReadTask(string file, void (*fn)(ifstream &))
|
||||
{
|
||||
filepath = file;
|
||||
func = fn;
|
||||
|
||||
queue();
|
||||
}
|
||||
|
||||
ReadTask::~ReadTask()
|
||||
{
|
||||
}
|
||||
|
||||
void ReadTask::run_task()
|
||||
{
|
||||
ifstream stream;
|
||||
stream.open(filepath.c_str());
|
||||
func(stream);
|
||||
stream.close();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <libsaria/fs.h>
|
||||
#include "library.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
@ -42,6 +43,68 @@ void save_path(libsaria::library::Path *path)
|
|||
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;
|
||||
list<libsaria::Track *> tracks;
|
||||
struct libsaria::library::Path path, *path_ptr;
|
||||
|
||||
getline(stream, tmp);
|
||||
s << tmp;
|
||||
s >> version;
|
||||
if (version != 1)
|
||||
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++) {
|
||||
path_ptr->tracks.push_back(libsaria::Track(stream, path_ptr));
|
||||
tracks.push_back(&path_ptr->tracks.back());
|
||||
}
|
||||
|
||||
notify_path_updated(path_ptr);
|
||||
lib_playlist.add_tracks(tracks);
|
||||
}
|
||||
|
||||
static bool compare_libfiles(string one, string two)
|
||||
{
|
||||
unsigned int a = atoi(one.c_str());
|
||||
unsigned int b = atoi(two.c_str());
|
||||
return a < b;
|
||||
}
|
||||
|
||||
unsigned int schedule_load()
|
||||
{
|
||||
list<string> paths;
|
||||
list<string>::iterator it;
|
||||
|
||||
libsaria::app::list_dir(libdir, paths);
|
||||
if (paths.size() == 0)
|
||||
return 0;
|
||||
paths.sort(compare_libfiles);
|
||||
|
||||
for (it = paths.begin(); it != paths.end(); it++) {
|
||||
println("Reading library path: " + libdir + "/" + (*it));
|
||||
libsaria::app::read(libdir + "/" + (*it), read_path);
|
||||
}
|
||||
|
||||
return atoi(paths.back().c_str()) + 1;
|
||||
}
|
||||
|
||||
/*static void do_load()
|
||||
{
|
||||
unsigned int size;
|
||||
|
|
|
@ -16,9 +16,9 @@ using namespace std;
|
|||
#include <libsaria/playlist.h>
|
||||
#include <libsaria/stack.h>*/
|
||||
|
||||
static int next_id = 0;
|
||||
static unsigned int next_id = 0;
|
||||
static list<struct libsaria::library::Path> path_list;
|
||||
list<libsaria::library::Driver *> driver_list;
|
||||
static list<libsaria::library::Driver *> driver_list;
|
||||
libsaria::Set lib_playlist("Library", PL_STATIC);
|
||||
/*list<libsaria::LibraryPath> path_list;
|
||||
libsaria::Playlist lib_playlist(PL_RANDOM | PL_SEQUENTIAL | PL_FILTER);
|
||||
|
@ -41,13 +41,33 @@ static void do_update(libsaria::LibraryPath *path):
|
|||
}
|
||||
*/
|
||||
|
||||
void notify_path_updated(libsaria::library::Path *path)
|
||||
{
|
||||
list<libsaria::library::Driver *>::iterator it;
|
||||
for (it = driver_list.begin(); it != driver_list.end(); it++)
|
||||
(*it)->path_updated(path);
|
||||
}
|
||||
|
||||
libsaria::library::Path *push_path(libsaria::library::Path &path)
|
||||
{
|
||||
libsaria::library::Path *ptr;
|
||||
list<libsaria::library::Driver *>::iterator it;
|
||||
|
||||
path_list.push_back(path);
|
||||
ptr = &path_list.back();
|
||||
|
||||
/* Notify library drivers that a new path has been added */
|
||||
for (it = driver_list.begin(); it != driver_list.end(); it++)
|
||||
(*it)->path_added(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
void library::add_path(string dir)
|
||||
{
|
||||
struct Path path;
|
||||
list<libsaria::library::Driver *>::iterator it;
|
||||
struct Path path, *path_ptr;
|
||||
|
||||
path.visible = true;
|
||||
path.data_state = CLEAN;
|
||||
|
@ -55,13 +75,8 @@ namespace libsaria
|
|||
path.next_track = 0;
|
||||
path.path = dir;
|
||||
|
||||
path_list.push_back(path);
|
||||
|
||||
/* Notify library drivers that a new path has been added */
|
||||
for (it = driver_list.begin(); it != driver_list.end(); it++)
|
||||
(*it)->path_added(&path_list.back());
|
||||
|
||||
update_path(&path_list.back());
|
||||
path_ptr = push_path(path);
|
||||
update_path(path_ptr);
|
||||
}
|
||||
|
||||
Playlist *library::get_playlist()
|
||||
|
@ -81,8 +96,8 @@ namespace libsaria
|
|||
|
||||
void library::init()
|
||||
{
|
||||
//load();
|
||||
app::mkdir("library");
|
||||
next_id = schedule_load();
|
||||
stack::push(&lib_playlist);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,10 +4,13 @@
|
|||
#include <libsaria/playlist.h>
|
||||
#include <libsaria/library.h>
|
||||
|
||||
extern list<libsaria::library::Driver *>driver_list;
|
||||
extern libsaria::Set lib_playlist;
|
||||
|
||||
void update_path(struct libsaria::library::Path *);
|
||||
void notify_path_updated(struct libsaria::library::Path *);
|
||||
void save_path(struct libsaria::library::Path *);
|
||||
libsaria::library::Path *push_path(libsaria::library::Path &);
|
||||
unsigned int schedule_load();
|
||||
/*#include <string>
|
||||
using namespace std;
|
||||
*/
|
||||
|
|
|
@ -53,7 +53,6 @@ ScanTask::~ScanTask()
|
|||
void ScanTask::run_task()
|
||||
{
|
||||
list<string>::iterator it;
|
||||
list<libsaria::library::Driver *>::iterator d_it;
|
||||
list<libsaria::Track *> tracks;
|
||||
|
||||
for (it = file_list.begin(); it != file_list.end(); it++) {
|
||||
|
@ -66,9 +65,7 @@ void ScanTask::run_task()
|
|||
}
|
||||
}
|
||||
|
||||
for (d_it = driver_list.begin(); d_it != driver_list.end(); d_it++)
|
||||
(*d_it)->path_updated(path);
|
||||
|
||||
notify_path_updated(path);
|
||||
lib_playlist.add_tracks(tracks);
|
||||
}
|
||||
|
||||
|
|
|
@ -91,6 +91,40 @@ namespace libsaria
|
|||
make_lenstr(length, lenstr);
|
||||
}
|
||||
|
||||
Track::Track(ifstream &in, struct library::Path *lib_path)
|
||||
{
|
||||
string tmp;
|
||||
stringstream s;
|
||||
|
||||
path = lib_path;
|
||||
|
||||
getline(in, filepath);
|
||||
getline(in, title);
|
||||
getline(in, artist);
|
||||
getline(in, album);
|
||||
getline(in, comment);
|
||||
getline(in, genre);
|
||||
getline(in, lenstr);
|
||||
getline(in, tmp);
|
||||
|
||||
s.clear();
|
||||
s.str(tmp);
|
||||
s >> id;
|
||||
s >> year;
|
||||
s >> track;
|
||||
s >> count;
|
||||
s >> last_day;
|
||||
s >> last_month;
|
||||
s >> last_year;
|
||||
s >> length;
|
||||
s >> bitrate;
|
||||
s >> sample;
|
||||
s >> channels;
|
||||
|
||||
artist_lc = lowercase(artist);
|
||||
album_lc = lowercase(album);
|
||||
}
|
||||
|
||||
Track::~Track()
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue