169 lines
3.5 KiB
C++
169 lines
3.5 KiB
C++
// Copyright (c) 2011 Bryan Schumaker.
|
|
#include <playlist.h>
|
|
#include <library.h>
|
|
#include <notify.h>
|
|
#include <audio.h>
|
|
#include <track.h>
|
|
#include <prefs.h>
|
|
#include <deck.h>
|
|
#include <idle.h>
|
|
#include <ban.h>
|
|
#include <fs.h>
|
|
#include "library.h"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
static unsigned int next_id = 0;
|
|
static vector<struct libsaria::library::Path> path_list;
|
|
libsaria::Playlist lib_playlist(PL_STATIC | PL_NO_DRAIN | PL_SORTED);
|
|
|
|
libsaria::library::Path *push_path(libsaria::library::Path &path)
|
|
{
|
|
libsaria::library::Path *ptr;
|
|
|
|
path_list.push_back(path);
|
|
ptr = &path_list.back();
|
|
libsaria::notify(PATH_ADDED, ptr);
|
|
return ptr;
|
|
}
|
|
|
|
static void hide_track(libsaria::Track *track)
|
|
{
|
|
if (track->banned)
|
|
libsaria::ban::get_banned_plist()->remove_track(track);
|
|
else
|
|
lib_playlist.remove_track(track);
|
|
}
|
|
|
|
void pop_path(libsaria::library::Path *path)
|
|
{
|
|
for (unsigned int i = 0; i < path_list.size(); i++) {
|
|
if (&path_list[i] == path) {
|
|
libsaria::notify(PATH_DELETED, path);
|
|
path_list.erase(path_list.begin() + i);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void restore_current()
|
|
{
|
|
unsigned int l_id = libsaria::prefs::get("libsaria.current.library");
|
|
unsigned int t_id = libsaria::prefs::get("libsaria.current.track");
|
|
libsaria::Track *track = libsaria::library::lookup(l_id, t_id);
|
|
if (track)
|
|
track->load(false);
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void library::add_path(string dir)
|
|
{
|
|
struct Path path, *path_ptr;
|
|
|
|
path.visible = true;
|
|
path.id = next_id++;
|
|
path.path = dir;
|
|
|
|
path_ptr = push_path(path);
|
|
update_path(path_ptr);
|
|
}
|
|
|
|
void library::delete_path(Path *path)
|
|
{
|
|
remove_file(path);
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
|
hide_track(path->tracks[i]);
|
|
libsaria::deck::track_removed(path->tracks[i]);
|
|
}
|
|
pop_path(path);
|
|
}
|
|
|
|
void library::update_path(Path *path)
|
|
{
|
|
if (path)
|
|
do_update_path(path);
|
|
}
|
|
|
|
void library::update_all()
|
|
{
|
|
for (unsigned int i = 0; i < path_list.size(); i++)
|
|
do_update_path(&path_list[i]);
|
|
}
|
|
|
|
void library::hide_path(Path *path)
|
|
{
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
|
hide_track(path->tracks[i]);
|
|
libsaria::deck::track_removed(path->tracks[i]);
|
|
}
|
|
path->visible = false;
|
|
save_path(path);
|
|
notify(PATH_UPDATED, path);
|
|
}
|
|
|
|
void library::show_path(Path *path)
|
|
{
|
|
list<Track *> ban, lib;
|
|
Track *track;
|
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
|
track = path->tracks[i];
|
|
if (track->banned)
|
|
ban.push_back(track);
|
|
else
|
|
lib.push_back(track);
|
|
}
|
|
lib_playlist.push_back(lib);
|
|
ban::get_banned_plist()->push_back(ban);
|
|
|
|
path->visible = true;
|
|
save_path(path);
|
|
notify(PATH_UPDATED, path);
|
|
}
|
|
|
|
Track *library::lookup(unsigned int libid, unsigned int trackid)
|
|
{
|
|
if (libid >= path_list.size())
|
|
return NULL;
|
|
if (trackid >= path_list[libid].tracks.size())
|
|
return NULL;
|
|
return path_list[libid].tracks[trackid];
|
|
}
|
|
|
|
void library::set_random(bool random)
|
|
{
|
|
prefs::set("libsaria.library.random", random);
|
|
lib_playlist.set_flag(PL_RANDOM, random);
|
|
}
|
|
|
|
Playlist *library::get_playlist()
|
|
{
|
|
return &lib_playlist;
|
|
}
|
|
|
|
void library::init()
|
|
{
|
|
bool random = prefs::init("libsaria.library.random", true);
|
|
lib_playlist.set_flag(PL_RANDOM, random);
|
|
|
|
app::mkdir("library");
|
|
next_id = schedule_load();
|
|
idle::schedule(restore_current);
|
|
}
|
|
|
|
void library::quit()
|
|
{
|
|
struct libsaria::library::Path *path;
|
|
for (unsigned int i = 0; i < path_list.size(); i++) {
|
|
path = &path_list[i];
|
|
for (unsigned int j = 0; j < path->tracks.size(); j++)
|
|
delete path->tracks[j];
|
|
}
|
|
}
|
|
|
|
}; /* namespace: libsaria */
|