2012-02-11 13:58:26 -05:00
|
|
|
// Copyright (c) 2011 Bryan Schumaker.
|
2012-09-12 08:15:31 -04:00
|
|
|
#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>
|
2012-03-17 13:29:12 -04:00
|
|
|
#include "library.h"
|
2012-03-12 08:08:04 -04:00
|
|
|
|
2011-10-18 10:02:55 -04:00
|
|
|
#include <string>
|
2012-11-22 11:43:52 -05:00
|
|
|
#include <vector>
|
2011-10-18 10:02:55 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2012-03-24 12:10:18 -04:00
|
|
|
static unsigned int next_id = 0;
|
2012-11-22 11:43:52 -05:00
|
|
|
static vector<struct libsaria::library::Path> path_list;
|
2012-11-03 15:34:13 -04:00
|
|
|
libsaria::Playlist lib_playlist(PL_STATIC | PL_NO_DRAIN | PL_SORTED);
|
2012-03-17 09:00:13 -04:00
|
|
|
|
2012-03-24 12:10:18 -04:00
|
|
|
libsaria::library::Path *push_path(libsaria::library::Path &path)
|
|
|
|
{
|
|
|
|
libsaria::library::Path *ptr;
|
|
|
|
|
|
|
|
path_list.push_back(path);
|
|
|
|
ptr = &path_list.back();
|
2012-08-21 17:55:27 -04:00
|
|
|
libsaria::notify(PATH_ADDED, ptr);
|
2012-03-24 12:10:18 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2012-11-24 16:50:53 -05:00
|
|
|
static void hide_track(libsaria::Track *track)
|
2012-07-01 11:51:28 -04:00
|
|
|
{
|
2012-11-24 16:59:25 -05:00
|
|
|
if (track->banned)
|
2012-11-24 16:50:53 -05:00
|
|
|
libsaria::ban::get_banned_plist()->remove_track(track);
|
2012-07-01 11:51:28 -04:00
|
|
|
else
|
2012-11-24 16:50:53 -05:00
|
|
|
lib_playlist.remove_track(track);
|
2012-07-01 11:51:28 -04:00
|
|
|
}
|
|
|
|
|
2012-03-31 10:56:35 -04:00
|
|
|
void pop_path(libsaria::library::Path *path)
|
|
|
|
{
|
2012-11-22 11:43:52 -05:00
|
|
|
for (unsigned int i = 0; i < path_list.size(); i++) {
|
|
|
|
if (&path_list[i] == path) {
|
2012-08-21 17:55:27 -04:00
|
|
|
libsaria::notify(PATH_DELETED, path);
|
2012-11-22 11:43:52 -05:00
|
|
|
path_list.erase(path_list.begin() + i);
|
2012-03-31 10:56:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-07 08:08:23 -04:00
|
|
|
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);
|
2012-05-13 11:07:00 -04:00
|
|
|
if (track)
|
|
|
|
track->load(false);
|
2012-05-07 08:08:23 -04:00
|
|
|
}
|
|
|
|
|
2011-10-18 10:02:55 -04:00
|
|
|
namespace libsaria
|
2011-09-30 08:12:26 -04:00
|
|
|
{
|
2012-03-12 08:21:32 -04:00
|
|
|
|
2012-03-17 09:00:13 -04:00
|
|
|
void library::add_path(string dir)
|
|
|
|
{
|
2012-03-24 12:10:18 -04:00
|
|
|
struct Path path, *path_ptr;
|
2012-03-17 09:00:13 -04:00
|
|
|
|
|
|
|
path.visible = true;
|
2012-03-24 09:00:44 -04:00
|
|
|
path.id = next_id++;
|
2012-03-17 09:00:13 -04:00
|
|
|
path.path = dir;
|
|
|
|
|
2012-03-24 12:10:18 -04:00
|
|
|
path_ptr = push_path(path);
|
2012-08-09 08:29:41 -04:00
|
|
|
update_path(path_ptr);
|
2012-03-17 09:00:13 -04:00
|
|
|
}
|
|
|
|
|
2012-03-31 10:56:35 -04:00
|
|
|
void library::delete_path(Path *path)
|
|
|
|
{
|
|
|
|
remove_file(path);
|
2012-11-22 14:01:25 -05:00
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
|
|
|
hide_track(path->tracks[i]);
|
2012-11-24 16:50:53 -05:00
|
|
|
libsaria::deck::track_removed(path->tracks[i]);
|
2012-11-22 14:01:25 -05:00
|
|
|
}
|
2012-03-31 10:56:35 -04:00
|
|
|
pop_path(path);
|
|
|
|
}
|
|
|
|
|
2012-04-01 18:28:12 -04:00
|
|
|
void library::update_path(Path *path)
|
|
|
|
{
|
2012-08-08 08:17:10 -04:00
|
|
|
if (path)
|
2012-04-01 18:28:12 -04:00
|
|
|
do_update_path(path);
|
2012-08-08 08:17:10 -04:00
|
|
|
}
|
2012-04-01 18:28:12 -04:00
|
|
|
|
2012-08-08 08:17:10 -04:00
|
|
|
void library::update_all()
|
|
|
|
{
|
2012-11-22 11:43:52 -05:00
|
|
|
for (unsigned int i = 0; i < path_list.size(); i++)
|
|
|
|
do_update_path(&path_list[i]);
|
2012-04-01 18:28:12 -04:00
|
|
|
}
|
|
|
|
|
2012-05-23 07:48:10 -04:00
|
|
|
void library::hide_path(Path *path)
|
|
|
|
{
|
2012-11-22 14:01:25 -05:00
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
|
|
|
hide_track(path->tracks[i]);
|
2012-11-24 16:50:53 -05:00
|
|
|
libsaria::deck::track_removed(path->tracks[i]);
|
2012-11-22 14:01:25 -05:00
|
|
|
}
|
2012-05-23 07:48:10 -04:00
|
|
|
path->visible = false;
|
|
|
|
save_path(path);
|
2012-08-21 17:55:27 -04:00
|
|
|
notify(PATH_UPDATED, path);
|
2012-05-23 07:48:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void library::show_path(Path *path)
|
|
|
|
{
|
2012-11-22 14:01:25 -05:00
|
|
|
list<Track *> ban, lib;
|
|
|
|
Track *track;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < path->tracks.size(); i++) {
|
2012-11-24 16:50:53 -05:00
|
|
|
track = path->tracks[i];
|
2012-11-24 16:59:25 -05:00
|
|
|
if (track->banned)
|
2012-11-22 14:01:25 -05:00
|
|
|
ban.push_back(track);
|
|
|
|
else
|
|
|
|
lib.push_back(track);
|
|
|
|
}
|
|
|
|
lib_playlist.push_back(lib);
|
|
|
|
ban::get_banned_plist()->push_back(ban);
|
2012-05-23 07:48:10 -04:00
|
|
|
|
|
|
|
path->visible = true;
|
|
|
|
save_path(path);
|
2012-08-21 17:55:27 -04:00
|
|
|
notify(PATH_UPDATED, path);
|
2012-05-23 07:48:10 -04:00
|
|
|
}
|
|
|
|
|
2012-04-07 09:28:53 -04:00
|
|
|
Track *library::lookup(unsigned int libid, unsigned int trackid)
|
|
|
|
{
|
2012-11-22 11:43:52 -05:00
|
|
|
if (libid >= path_list.size())
|
2012-04-07 09:28:53 -04:00
|
|
|
return NULL;
|
2012-11-22 14:01:25 -05:00
|
|
|
if (trackid >= path_list[libid].tracks.size())
|
2012-04-07 09:28:53 -04:00
|
|
|
return NULL;
|
2012-11-24 16:50:53 -05:00
|
|
|
return path_list[libid].tracks[trackid];
|
2012-04-07 09:28:53 -04:00
|
|
|
}
|
|
|
|
|
2012-09-16 10:00:07 -04:00
|
|
|
void library::set_random(bool random)
|
|
|
|
{
|
|
|
|
prefs::set("libsaria.library.random", random);
|
2012-11-03 22:34:03 -04:00
|
|
|
lib_playlist.set_flag(PL_RANDOM, random);
|
2012-09-16 10:00:07 -04:00
|
|
|
}
|
|
|
|
|
2012-03-20 08:30:00 -04:00
|
|
|
Playlist *library::get_playlist()
|
|
|
|
{
|
|
|
|
return &lib_playlist;
|
|
|
|
}
|
|
|
|
|
2012-02-25 17:21:19 -05:00
|
|
|
void library::init()
|
|
|
|
{
|
2012-09-16 10:00:07 -04:00
|
|
|
bool random = prefs::init("libsaria.library.random", true);
|
2012-11-03 22:34:03 -04:00
|
|
|
lib_playlist.set_flag(PL_RANDOM, random);
|
2012-05-07 08:08:23 -04:00
|
|
|
|
2012-03-24 09:44:08 -04:00
|
|
|
app::mkdir("library");
|
2012-03-24 12:10:18 -04:00
|
|
|
next_id = schedule_load();
|
2012-11-23 10:02:39 -05:00
|
|
|
idle::schedule(restore_current);
|
2012-02-25 17:21:19 -05:00
|
|
|
}
|
|
|
|
|
2012-11-24 16:50:53 -05:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 08:08:04 -04:00
|
|
|
}; /* namespace: libsaria */
|