2014-11-19 09:08:38 -05:00
|
|
|
/**
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-12-01 10:33:19 -05:00
|
|
|
#include <core/filter.h>
|
2015-10-29 03:10:19 -04:00
|
|
|
extern "C" {
|
2015-01-27 08:34:32 -05:00
|
|
|
#include <core/string.h>
|
2015-10-31 10:53:40 -04:00
|
|
|
#include <taglib/tag_c.h>
|
2015-10-29 03:10:19 -04:00
|
|
|
}
|
2014-11-19 09:08:38 -05:00
|
|
|
#include <core/tags/track.h>
|
|
|
|
|
2015-09-01 08:58:34 -04:00
|
|
|
#include <glib.h>
|
2014-11-26 08:55:06 -05:00
|
|
|
|
2014-11-19 09:08:38 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
static database<struct track> track_db;
|
2014-11-29 10:38:27 -05:00
|
|
|
|
2015-10-31 10:53:40 -04:00
|
|
|
static const std::string __track_key(struct library *library, const std::string &path)
|
2015-10-18 10:59:20 -04:00
|
|
|
{
|
|
|
|
std :: string res;
|
|
|
|
|
|
|
|
if (library) {
|
2015-11-02 14:48:00 -05:00
|
|
|
gchar *g_res = g_strdup_printf("%u/%s", library->dbe_index,
|
2015-10-18 10:59:20 -04:00
|
|
|
path.c_str());
|
|
|
|
res = g_res;
|
|
|
|
g_free(g_res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2014-11-29 10:38:27 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
track :: track()
|
2015-10-29 03:10:19 -04:00
|
|
|
: tr_album(NULL), tr_artist(NULL), tr_genre(NULL), tr_library(NULL),
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_count(0), tr_length(0), tr_track(0)
|
2014-11-19 09:08:38 -05:00
|
|
|
{}
|
2014-11-20 08:16:13 -05:00
|
|
|
|
2015-10-31 10:53:40 -04:00
|
|
|
track :: track(const std::string &key)
|
|
|
|
{
|
|
|
|
const TagLib_AudioProperties *audio;
|
|
|
|
struct library *library;
|
|
|
|
unsigned int lib_id;
|
|
|
|
TagLib_File *file;
|
|
|
|
TagLib_Tag *tag;
|
|
|
|
std::string full;
|
|
|
|
char *path, *lower;
|
|
|
|
|
|
|
|
sscanf(key.c_str(), "%u/%m[^\n]", &lib_id, &path);
|
|
|
|
library = library_get(lib_id);
|
|
|
|
full = library_file(library, path);
|
|
|
|
file = taglib_file_new(full.c_str());
|
|
|
|
if (!file || !taglib_file_is_valid(file)) {
|
|
|
|
printf("WARNING: Could not read tags for: %s\n", full.c_str());
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
tag = taglib_file_tag(file);
|
|
|
|
audio = taglib_file_audioproperties(file);
|
|
|
|
|
|
|
|
tr_album = album_find(taglib_tag_album(tag), taglib_tag_year(tag));
|
|
|
|
tr_artist = artist_find(taglib_tag_artist(tag));
|
|
|
|
tr_genre = genre_find(taglib_tag_genre(tag));
|
|
|
|
tr_library = library;
|
|
|
|
tr_library->li_size++;
|
|
|
|
|
|
|
|
date_set(&tr_date, 0, 0, 0);
|
|
|
|
tr_count = 0;
|
|
|
|
tr_length = taglib_audioproperties_length(audio);
|
|
|
|
tr_track = taglib_tag_track(tag);
|
|
|
|
|
|
|
|
tr_path = path;
|
|
|
|
tr_title = taglib_tag_title(tag);
|
|
|
|
lower = string_lowercase(tr_title.c_str());
|
2015-10-29 03:10:19 -04:00
|
|
|
tr_lower = lower;
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
date_set(&tr_date, 0, 0, 0);
|
2015-11-02 14:48:00 -05:00
|
|
|
filter :: add(tr_title, dbe_index);
|
|
|
|
filter :: add(tr_artist->ar_name, dbe_index);
|
|
|
|
filter :: add(tr_album->al_name, dbe_index);
|
2014-11-20 08:22:50 -05:00
|
|
|
|
2015-10-31 10:53:40 -04:00
|
|
|
taglib_tag_free_strings();
|
|
|
|
taglib_file_free(file);
|
|
|
|
g_free(lower);
|
|
|
|
out:
|
|
|
|
g_free(path);
|
2014-11-29 10:50:23 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
track :: ~track()
|
2014-11-21 08:36:49 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
if (tr_library)
|
|
|
|
tr_library->li_size--;
|
2014-11-21 08:36:49 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
const std::string track :: primary_key() const
|
2014-11-26 21:25:39 -05:00
|
|
|
{
|
2015-10-31 10:53:40 -04:00
|
|
|
return __track_key(tr_library, tr_path);
|
2014-11-26 21:25:39 -05:00
|
|
|
}
|
2014-11-27 10:37:46 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void track :: read(file &file)
|
2014-11-28 21:18:36 -05:00
|
|
|
{
|
|
|
|
unsigned int library_id, artist_id, album_id, genre_id;
|
2015-10-29 03:10:19 -04:00
|
|
|
gchar *path, *name, *lower;
|
2014-11-28 21:18:36 -05:00
|
|
|
|
2015-10-28 03:31:09 -04:00
|
|
|
file_readf(&file, "%u %u %u %u %u", &library_id, &artist_id,
|
2015-10-24 19:34:45 -04:00
|
|
|
&album_id, &genre_id, &tr_track);
|
|
|
|
date_read(&file, &tr_date);
|
|
|
|
file_readf(&file, "%u %u", &tr_count, &tr_length);
|
2014-11-28 21:18:36 -05:00
|
|
|
|
2015-10-29 03:10:19 -04:00
|
|
|
name = file_readl(&file);
|
2015-10-09 13:09:47 -04:00
|
|
|
path = file_readl(&file);
|
2015-10-29 03:10:19 -04:00
|
|
|
lower = string_lowercase(name);
|
|
|
|
tr_title = name;
|
|
|
|
tr_path = path;
|
|
|
|
tr_lower = lower;
|
|
|
|
g_free(name);
|
2015-10-09 13:09:47 -04:00
|
|
|
g_free(path);
|
2015-10-29 03:10:19 -04:00
|
|
|
g_free(lower);
|
2014-11-28 21:18:36 -05:00
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_library = library_get(library_id);
|
|
|
|
tr_artist = artist_get(artist_id);
|
|
|
|
tr_album = album_get(album_id);
|
|
|
|
tr_genre = genre_get(genre_id);
|
2014-11-28 21:18:36 -05:00
|
|
|
|
2015-11-02 14:48:00 -05:00
|
|
|
filter :: add(tr_title, dbe_index);
|
|
|
|
filter :: add(tr_artist->ar_name, dbe_index);
|
|
|
|
filter :: add(tr_album->al_name, dbe_index);
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_library->li_size++;
|
2014-11-28 21:18:36 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
void track :: write(file &file)
|
2014-11-28 21:18:36 -05:00
|
|
|
{
|
2015-11-02 14:48:00 -05:00
|
|
|
file_writef(&file, "%u %u %u %u %u ", tr_library->dbe_index,
|
|
|
|
tr_artist->dbe_index, tr_album->dbe_index,
|
|
|
|
tr_genre->dbe_index, tr_track);
|
2015-10-24 19:34:45 -04:00
|
|
|
date_write(&file, &tr_date);
|
2015-10-29 03:10:19 -04:00
|
|
|
file_writef(&file, " %u %u %s\n%s\n", tr_count, tr_length,
|
|
|
|
tr_title.c_str(), tr_path.c_str());
|
2014-11-28 21:18:36 -05:00
|
|
|
}
|
2014-11-29 10:38:27 -05:00
|
|
|
|
|
|
|
|
2015-11-03 10:39:06 -05:00
|
|
|
static const struct db_ops track_ops = {
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-10-25 04:46:56 -04:00
|
|
|
void track_db_init()
|
2014-12-02 08:16:22 -05:00
|
|
|
{
|
2015-11-03 10:39:06 -05:00
|
|
|
db_init(&track_db, "track.db", false, &track_ops);
|
2015-09-17 08:12:25 -04:00
|
|
|
db_load(&track_db);
|
2014-12-02 08:16:22 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 04:46:56 -04:00
|
|
|
void track_db_deinit()
|
|
|
|
{
|
|
|
|
db_deinit(&track_db);
|
|
|
|
}
|
|
|
|
|
2015-09-28 15:39:26 -04:00
|
|
|
void track_db_commit()
|
|
|
|
{
|
|
|
|
db_save(&track_db);
|
|
|
|
}
|
|
|
|
|
2015-10-25 05:06:23 -04:00
|
|
|
const database<struct track> *track_db_get()
|
|
|
|
{
|
|
|
|
return &track_db;
|
|
|
|
}
|
|
|
|
|
2015-10-31 10:53:40 -04:00
|
|
|
struct track *track_add(struct library *library, const std::string &filepath)
|
2014-11-29 10:38:27 -05:00
|
|
|
{
|
|
|
|
std::string path = filepath.substr(library->primary_key().size() + 1);
|
2015-10-31 10:53:40 -04:00
|
|
|
std::string key = __track_key(library, path);
|
|
|
|
if (db_get(&track_db, key.c_str()))
|
2015-10-18 10:59:20 -04:00
|
|
|
return NULL;
|
2015-10-31 10:53:40 -04:00
|
|
|
return db_insert(&track_db, new struct track(key));
|
2014-11-29 10:38:27 -05:00
|
|
|
}
|
2014-11-29 11:11:49 -05:00
|
|
|
|
2015-10-25 04:41:03 -04:00
|
|
|
void track_remove(struct track *track)
|
2014-12-01 08:24:56 -05:00
|
|
|
{
|
2015-10-20 08:05:13 -04:00
|
|
|
db_remove(&track_db, track);
|
2014-12-01 08:24:56 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 04:43:39 -04:00
|
|
|
void track_remove_all(struct library *library)
|
2014-12-01 09:36:37 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
struct track *it, *next;
|
2014-12-01 09:36:37 -05:00
|
|
|
|
2015-09-18 09:37:48 -04:00
|
|
|
db_for_each(it, next, &track_db) {
|
2015-10-27 17:25:18 -04:00
|
|
|
if (it->tr_library == library)
|
2015-09-18 09:37:48 -04:00
|
|
|
db_remove(&track_db, it);
|
2014-12-01 09:36:37 -05:00
|
|
|
}
|
2015-09-28 15:39:26 -04:00
|
|
|
track_db_commit();
|
2014-12-01 09:36:37 -05:00
|
|
|
}
|
|
|
|
|
2015-09-28 15:54:21 -04:00
|
|
|
struct track *track_get(const unsigned int index)
|
|
|
|
{
|
|
|
|
return db_at(&track_db, index);
|
|
|
|
}
|
2015-09-29 09:34:06 -04:00
|
|
|
|
2015-10-29 03:10:19 -04:00
|
|
|
int track_compare(struct track *lhs, struct track *rhs)
|
|
|
|
{
|
|
|
|
return string_compare(lhs->tr_lower.c_str(), rhs->tr_lower.c_str());
|
|
|
|
}
|
|
|
|
|
2015-10-29 02:43:39 -04:00
|
|
|
const std::string track_path(struct track *track)
|
|
|
|
{
|
|
|
|
if (track->tr_library)
|
|
|
|
return library_file(track->tr_library, track->tr_path);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-09-29 09:34:06 -04:00
|
|
|
void track_played(struct track *track)
|
|
|
|
{
|
|
|
|
track->tr_count++;
|
|
|
|
date_today(&track->tr_date);
|
|
|
|
track_db_commit();
|
|
|
|
}
|
2015-10-29 02:51:19 -04:00
|
|
|
|
|
|
|
const std::string track_last_play(struct track *track)
|
|
|
|
{
|
|
|
|
std::string res = "Never";
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
if (track->tr_count > 0) {
|
|
|
|
buf = date_string(&track->tr_date);
|
|
|
|
res = buf;
|
|
|
|
g_free(buf);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2015-11-03 10:39:06 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_TESTING
|
|
|
|
const struct db_ops *test_track_ops() { return &track_ops; }
|
|
|
|
#endif /* CONFIG_TESTING */
|