2014-09-13 10:38:54 -04:00
|
|
|
/**
|
2013-10-27 17:25:26 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2015-11-10 14:18:11 -05:00
|
|
|
extern "C" {
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/idle.h>
|
2015-11-10 14:18:11 -05:00
|
|
|
}
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/library.h>
|
2014-12-02 08:24:36 -05:00
|
|
|
#include <core/print.h>
|
2013-10-27 17:25:26 -04:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2013-11-10 17:37:35 -05:00
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
class LibraryQueue : public queue {
|
2014-05-24 12:47:33 -04:00
|
|
|
private:
|
2015-09-10 08:00:42 -04:00
|
|
|
file f;
|
2014-05-24 12:47:33 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2015-11-23 08:36:29 -05:00
|
|
|
LibraryQueue()
|
2014-05-24 12:47:33 -04:00
|
|
|
{
|
2015-09-10 10:33:24 -04:00
|
|
|
file_init(&f, "library.q", 0);
|
2014-05-24 12:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void save()
|
|
|
|
{
|
2015-12-02 09:18:10 -05:00
|
|
|
GSList *cur = q_sort;
|
|
|
|
int field;
|
2014-05-24 12:47:33 -04:00
|
|
|
|
2015-11-24 08:18:31 -05:00
|
|
|
if (!file_open(&f, OPEN_WRITE))
|
|
|
|
return;
|
|
|
|
|
2015-12-02 09:18:10 -05:00
|
|
|
file_writef(&f, "%u %u", q_flags, g_slist_length(q_sort));
|
|
|
|
while (cur) {
|
|
|
|
field = GPOINTER_TO_INT(cur->data);
|
|
|
|
file_writef(&f, " %u %d", abs(field) - 1, field > 0);
|
|
|
|
cur = g_slist_next(cur);
|
|
|
|
}
|
2015-10-08 09:41:51 -04:00
|
|
|
file_writef(&f, "\n");
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-05-24 12:47:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void load()
|
|
|
|
{
|
|
|
|
unsigned int field;
|
2015-10-08 11:16:38 -04:00
|
|
|
int ascending;
|
2014-05-24 12:47:33 -04:00
|
|
|
unsigned int n;
|
|
|
|
|
2015-09-10 09:46:33 -04:00
|
|
|
if (!file_open(&f, OPEN_READ))
|
2014-05-24 12:47:33 -04:00
|
|
|
return;
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
file_readf(&f, "%u %u", &q_flags, &n);
|
2014-05-24 12:47:33 -04:00
|
|
|
for (unsigned int i = 0; i < n; i++) {
|
2015-10-08 11:16:38 -04:00
|
|
|
file_readf(&f, "%u %d", &field, &ascending);
|
2015-12-02 09:18:10 -05:00
|
|
|
queue_sort(this, (compare_t)(field + 1), (i == 0) ? true : false);
|
2014-05-24 12:47:33 -04:00
|
|
|
if (ascending == false)
|
2015-12-02 09:18:10 -05:00
|
|
|
queue_sort(this, (compare_t)(field + 1), false);
|
2014-05-24 12:47:33 -04:00
|
|
|
}
|
2015-09-10 09:12:48 -04:00
|
|
|
file_close(&f);
|
2014-05-24 12:47:33 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static LibraryQueue library_q;
|
|
|
|
|
2014-01-01 13:34:25 -05:00
|
|
|
struct scan_info {
|
2015-10-23 10:21:12 -04:00
|
|
|
struct library *library;
|
2014-01-01 13:34:25 -05:00
|
|
|
std :: string path;
|
|
|
|
};
|
2013-11-10 16:09:31 -05:00
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
static void scan_path(void *);
|
2014-05-24 12:47:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scanning functions are here
|
|
|
|
*/
|
2015-10-23 10:21:12 -04:00
|
|
|
static void tag_track(struct library *library, const std::string &filepath)
|
2014-12-02 08:24:36 -05:00
|
|
|
{
|
2015-11-12 09:38:35 -05:00
|
|
|
struct track *track = track_add(library, filepath.c_str());
|
2014-12-02 08:24:36 -05:00
|
|
|
if (track)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&library_q, track);
|
2014-12-02 08:24:36 -05:00
|
|
|
}
|
2013-11-10 16:09:31 -05:00
|
|
|
|
2015-10-23 10:21:12 -04:00
|
|
|
static void process_path(struct library *library, const std :: string &dir,
|
2013-11-10 16:09:31 -05:00
|
|
|
const std :: string &name)
|
|
|
|
{
|
2015-11-10 13:52:24 -05:00
|
|
|
struct scan_info *scan = new struct scan_info;
|
|
|
|
scan->library = library;
|
|
|
|
scan->path = dir + "/" + name;
|
|
|
|
|
|
|
|
if (g_file_test(scan->path.c_str(), G_FILE_TEST_IS_DIR) == true)
|
|
|
|
idle_schedule (scan_path, scan);
|
|
|
|
else {
|
|
|
|
tag_track(library, scan->path);
|
|
|
|
delete scan;
|
|
|
|
}
|
2013-11-10 17:37:35 -05:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
static void scan_path(void *data)
|
2013-11-10 16:09:31 -05:00
|
|
|
{
|
|
|
|
GDir *dir;
|
|
|
|
const char *name;
|
2015-11-10 13:52:24 -05:00
|
|
|
struct scan_info *scan = (struct scan_info *)data;
|
2013-11-10 16:09:31 -05:00
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
dir = g_dir_open(scan->path.c_str(), 0, NULL);
|
2013-11-10 16:09:31 -05:00
|
|
|
if (dir == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
name = g_dir_read_name(dir);
|
|
|
|
while (name != NULL) {
|
2015-11-10 13:52:24 -05:00
|
|
|
process_path(scan->library, scan->path, name);
|
2013-11-10 16:09:31 -05:00
|
|
|
name = g_dir_read_name(dir);
|
|
|
|
}
|
2014-01-15 20:40:24 -05:00
|
|
|
|
2015-09-28 15:39:26 -04:00
|
|
|
track_db_commit();
|
2016-01-11 09:55:49 -05:00
|
|
|
g_dir_close(dir);
|
2015-11-10 13:52:24 -05:00
|
|
|
delete scan;
|
2013-11-10 16:09:31 -05:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:52:24 -05:00
|
|
|
static void validate_library(void *data)
|
2013-12-22 15:38:27 -05:00
|
|
|
{
|
2015-11-10 13:52:24 -05:00
|
|
|
struct library *library = (struct library *)data;
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct track *track;
|
2015-11-12 09:38:35 -05:00
|
|
|
gchar *path;
|
2013-12-22 15:38:27 -05:00
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
db_for_each(dbe, next, track_db_get()) {
|
|
|
|
track = TRACK(dbe);
|
2015-10-27 17:25:18 -04:00
|
|
|
if (track->tr_library != library)
|
2013-12-22 15:38:27 -05:00
|
|
|
continue;
|
|
|
|
|
2015-11-12 09:38:35 -05:00
|
|
|
path = track_path(track);
|
|
|
|
if (g_file_test(path, G_FILE_TEST_EXISTS) == false) {
|
2015-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(&library_q, track);
|
2015-10-25 04:41:03 -04:00
|
|
|
track_remove(track);
|
2013-12-22 15:38:27 -05:00
|
|
|
}
|
2015-11-12 09:38:35 -05:00
|
|
|
g_free(path);
|
2013-12-22 15:38:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:09:31 -05:00
|
|
|
|
|
|
|
|
2013-10-27 17:25:26 -04:00
|
|
|
/*
|
2014-05-24 12:47:33 -04:00
|
|
|
* External API begins here
|
2013-10-27 17:25:26 -04:00
|
|
|
*/
|
|
|
|
|
2015-12-05 08:55:24 -05:00
|
|
|
void collection_init(struct queue_ops *ops)
|
2013-11-03 16:03:01 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *track, *next;
|
2014-05-24 12:47:33 -04:00
|
|
|
|
2015-11-25 09:04:10 -05:00
|
|
|
queue_init(&library_q, Q_ENABLED | Q_REPEAT | Q_ADD_FRONT, ops);
|
2014-02-01 17:29:31 -05:00
|
|
|
|
2015-10-25 05:06:23 -04:00
|
|
|
db_for_each(track, next, track_db_get()) {
|
2015-11-04 12:23:00 -05:00
|
|
|
if (TRACK(track)->tr_library->li_enabled)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&library_q, TRACK(track));
|
2014-01-25 13:19:40 -05:00
|
|
|
}
|
2015-12-03 10:03:52 -05:00
|
|
|
queue_unset_flag(&library_q, Q_ADD_FRONT);
|
2015-11-23 08:36:29 -05:00
|
|
|
|
|
|
|
library_q.load();
|
2015-12-02 09:18:10 -05:00
|
|
|
if (!library_q.q_sort) {
|
2015-12-03 13:43:15 -05:00
|
|
|
queue_sort(&library_q, COMPARE_ARTIST, true);
|
|
|
|
queue_sort(&library_q, COMPARE_YEAR, false);
|
|
|
|
queue_sort(&library_q, COMPARE_TRACK, false);
|
2015-11-23 08:36:29 -05:00
|
|
|
}
|
2015-11-24 08:18:31 -05:00
|
|
|
|
2015-12-03 13:41:39 -05:00
|
|
|
queue_set_flag(&library_q, Q_SAVE_SORT);
|
|
|
|
queue_set_flag(&library_q, Q_SAVE_FLAGS);
|
2015-11-24 08:18:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void collection :: save(struct queue *queue, enum queue_flags flag)
|
|
|
|
{
|
|
|
|
library_q.save();
|
2013-11-03 16:03:01 -05:00
|
|
|
}
|
|
|
|
|
2015-12-05 09:36:14 -05:00
|
|
|
struct library *collection_add(const gchar *path)
|
2013-10-27 17:25:26 -04:00
|
|
|
{
|
2015-10-23 10:21:12 -04:00
|
|
|
struct library *library = NULL;
|
2013-10-27 17:25:26 -04:00
|
|
|
|
2015-12-05 09:36:14 -05:00
|
|
|
if (g_file_test(path, G_FILE_TEST_IS_DIR) == false)
|
2014-05-24 12:47:33 -04:00
|
|
|
return library;
|
2014-01-15 20:40:24 -05:00
|
|
|
|
2015-12-05 09:36:14 -05:00
|
|
|
library = library_find(path);
|
2014-05-24 12:47:33 -04:00
|
|
|
if (library)
|
2015-12-05 09:36:14 -05:00
|
|
|
collection :: update(library);
|
2014-05-24 12:47:33 -04:00
|
|
|
return library;
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|
|
|
|
|
2015-12-05 10:21:24 -05:00
|
|
|
void collection_remove(struct library *library)
|
2013-10-27 17:25:26 -04:00
|
|
|
{
|
2014-05-24 12:47:33 -04:00
|
|
|
if (library) {
|
2015-12-05 10:21:24 -05:00
|
|
|
collection :: set_enabled(library, false);
|
2015-10-25 04:43:39 -04:00
|
|
|
track_remove_all(library);
|
2015-09-28 09:55:29 -04:00
|
|
|
library_remove(library);
|
2014-01-16 22:23:36 -05:00
|
|
|
}
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|
|
|
|
|
2015-10-23 10:21:12 -04:00
|
|
|
void collection :: update(struct library *library)
|
2013-11-10 16:09:31 -05:00
|
|
|
{
|
2015-11-10 13:52:24 -05:00
|
|
|
struct scan_info *scan = new struct scan_info;
|
|
|
|
scan->library = library;
|
2014-05-24 12:47:33 -04:00
|
|
|
|
|
|
|
if (library) {
|
2015-11-10 13:52:24 -05:00
|
|
|
scan->path = library->li_path;
|
|
|
|
idle_schedule(validate_library, library);
|
|
|
|
idle_schedule(scan_path, scan);
|
2014-05-24 12:47:33 -04:00
|
|
|
}
|
2013-11-10 16:09:31 -05:00
|
|
|
}
|
|
|
|
|
2015-10-23 10:21:12 -04:00
|
|
|
void collection :: update_all()
|
2014-01-12 13:04:16 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *library, *next;
|
2014-03-09 14:58:06 -04:00
|
|
|
|
2015-10-23 13:32:20 -04:00
|
|
|
db_for_each(library, next, library_db_get())
|
2015-11-04 12:23:00 -05:00
|
|
|
update(LIBRARY(library));
|
2014-01-12 13:04:16 -05:00
|
|
|
}
|
|
|
|
|
2015-10-23 10:21:12 -04:00
|
|
|
void collection :: set_enabled(struct library *library, bool enabled)
|
2014-01-18 12:32:41 -05:00
|
|
|
{
|
2015-11-04 12:23:00 -05:00
|
|
|
struct db_entry *dbe, *next;
|
|
|
|
struct track *track;
|
2014-05-24 12:47:33 -04:00
|
|
|
|
2015-10-23 14:10:59 -04:00
|
|
|
if (!library || (library->li_enabled == enabled))
|
2014-05-24 12:47:33 -04:00
|
|
|
return;
|
2014-01-18 12:32:41 -05:00
|
|
|
|
2015-10-23 14:19:42 -04:00
|
|
|
library_set_enabled(library, enabled);
|
2015-12-03 10:03:52 -05:00
|
|
|
queue_set_flag(&library_q, Q_ADD_FRONT);
|
2014-03-29 11:44:39 -04:00
|
|
|
|
2015-11-04 12:23:00 -05:00
|
|
|
db_for_each(dbe, next, track_db_get()) {
|
|
|
|
track = TRACK(dbe);
|
2015-10-27 17:25:18 -04:00
|
|
|
if (track->tr_library == library) {
|
2014-01-18 12:32:41 -05:00
|
|
|
if (enabled)
|
2015-11-25 08:24:04 -05:00
|
|
|
queue_add(&library_q, track);
|
2014-01-18 12:32:41 -05:00
|
|
|
else
|
2015-11-29 16:34:51 -05:00
|
|
|
queue_remove_all(&library_q, track);
|
2014-01-18 12:32:41 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-03 10:03:52 -05:00
|
|
|
|
|
|
|
queue_unset_flag(&library_q, Q_ADD_FRONT);
|
|
|
|
queue_resort(&library_q);
|
2014-01-18 12:32:41 -05:00
|
|
|
}
|
|
|
|
|
2015-11-16 09:12:15 -05:00
|
|
|
queue *collection :: get_queue()
|
2013-10-27 17:25:26 -04:00
|
|
|
{
|
2014-05-24 12:47:33 -04:00
|
|
|
return &library_q;
|
2013-10-27 17:25:26 -04:00
|
|
|
}
|