ocarina/libsaria/library/path.cpp

124 lines
2.1 KiB
C++

#include <string>
using namespace std;
#include <libsaria/tags.h>
#include <libsaria/library.h>
#include "library.h"
/*
* Definitions for the LibraryPath class
*/
LibraryPath::LibraryPath(string dir)
{
path = dir;
}
LibraryPath::~LibraryPath()
{
}
void LibraryPath::insert_track(TrackTag &tag)
{
file_list.push_back(tag);
}
void LibraryPath::update()
{
ScanTask *task = new ScanTask(this, path, false);
task->queue();
}
unsigned int LibraryPath::size()
{
return file_list.size();
}
/*
* Definitions for the ScanTask class
*/
ScanTask::ScanTask(LibraryPath *lib_path, string scan_dir, bool end)
{
library = lib_path;
dir = scan_dir;
end_dir = end;
}
ScanTask::~ScanTask()
{}
void ScanTask::tag_file(file filepath)
{
try {
TrackTag tag(dir + "/" + filepath.name, filepath.d_ino);
library->insert_track(tag);
} catch (string message) {
println(message);
}
}
/*
* 1) Read a directory
* 2) Find tags for each music file
* 3) Create and queue a new ScanTask for each subdirectory
*/
void ScanTask::run_task()
{
int last, i = 0;
list<file> files;
list<file> dirs;
list<file>::iterator it;
ScanTask *scan;
SaveTask *save;
readdir(dir, files, dirs);
for (it = files.begin(); it != files.end(); it++)
tag_file(*it);
last = dirs.size() - 1;
for (it = dirs.begin(); it != dirs.end(); it++) {
scan = new ScanTask(library, dir + "/" + (*it).name, i == last);
scan->queue();
i++;
}
if (end_dir == true) {
print("Library path size: ");
println(library->size());
save = new SaveTask(libsaria::library::save);
save->queue_front();
libsaria::library::refresh();
}
}
namespace libsaria
{
void library::add_path(string dir)
{
get_path_list()->push_back(LibraryPath(dir));
update_path(dir);
}
void library::remove_path(string dir)
{
list<LibraryPath> *path_list = get_path_list();
list<LibraryPath>::iterator it;
for (it = path_list->begin(); it != path_list->end(); it++) {
if (it->get_path() == dir) {
path_list->erase(it);
save();
refresh();
return;
}
}
}
void library::update_path(string dir)
{
get_path(dir)->update();
}
}; /* Namespace: libsaria */