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-01-27 08:34:32 -05:00
|
|
|
#include <core/string.h>
|
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-23 10:21:12 -04:00
|
|
|
static const std::string make_key(struct library *library, const std::string &path)
|
2015-10-18 10:59:20 -04:00
|
|
|
{
|
|
|
|
std :: string res;
|
|
|
|
|
|
|
|
if (library) {
|
|
|
|
gchar *g_res = g_strdup_printf("%u/%s", library->index(),
|
|
|
|
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()
|
2014-11-19 09:08:38 -05:00
|
|
|
: GenericTag(),
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_album(NULL), tr_artist(NULL), tr_genre(NULL), tr_library(NULL),
|
|
|
|
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-24 19:34:45 -04:00
|
|
|
track :: track(struct album *album, struct artist *artist, struct genre *genre,
|
2015-10-23 10:21:12 -04:00
|
|
|
struct library *library, const std::string &filepath,
|
2015-10-22 11:28:45 -04:00
|
|
|
const std::string &name, unsigned int length, unsigned int track)
|
2014-11-26 08:23:20 -05:00
|
|
|
: GenericTag(name),
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_album(album), tr_artist(artist), tr_genre(genre),
|
|
|
|
tr_library(library), tr_count(0), tr_length(length), tr_track(track),
|
|
|
|
tr_path(filepath)
|
2014-11-20 08:22:50 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
date_set(&tr_date, 0, 0, 0);
|
2014-12-01 10:33:19 -05:00
|
|
|
filter :: add(this->name(), index());
|
2015-10-24 19:34:45 -04:00
|
|
|
filter :: add(tr_artist->ar_name, index());
|
|
|
|
filter :: add(tr_album->al_name, index());
|
|
|
|
tr_library->li_size++;
|
2014-11-20 08:22:50 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
track :: track(const struct track &track)
|
2014-11-29 10:50:23 -05:00
|
|
|
: GenericTag(track),
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_album(track.tr_album), tr_artist(track.tr_artist),
|
|
|
|
tr_genre(track.tr_genre), tr_library(track.tr_library),
|
|
|
|
tr_count(track.tr_count), tr_length(track.tr_length),
|
|
|
|
tr_track(track.tr_track), tr_date(track.tr_date),
|
|
|
|
tr_path(track.tr_path)
|
2014-11-29 10:50:23 -05:00
|
|
|
{
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_library->li_size++;
|
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 :: date() const
|
2014-11-27 10:37:46 -05:00
|
|
|
{
|
2015-10-28 04:01:43 -04:00
|
|
|
char *buf;
|
2014-11-27 10:37:46 -05:00
|
|
|
std::string res = "Never";
|
|
|
|
|
2015-10-24 19:34:45 -04:00
|
|
|
if (tr_count > 0) {
|
|
|
|
buf = date_string(&tr_date);
|
2015-10-28 04:01:43 -04:00
|
|
|
res = buf;
|
|
|
|
g_free(buf);
|
2014-11-27 10:37:46 -05:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
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-24 19:34:45 -04:00
|
|
|
return make_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-09 13:09:47 -04:00
|
|
|
gchar *path;
|
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
|
|
|
|
2014-12-04 08:29:17 -05:00
|
|
|
GenericTag :: read(file);
|
2015-10-09 13:09:47 -04:00
|
|
|
path = file_readl(&file);
|
2015-10-24 19:34:45 -04:00
|
|
|
tr_path = path;
|
2015-10-09 13:09:47 -04:00
|
|
|
g_free(path);
|
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
|
|
|
|
2014-12-01 10:33:19 -05:00
|
|
|
filter :: add(name(), index());
|
2015-10-24 19:34:45 -04:00
|
|
|
filter :: add(tr_artist->ar_name, index());
|
|
|
|
filter :: add(tr_album->al_name, index());
|
|
|
|
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-10-24 19:34:45 -04:00
|
|
|
file_writef(&file, "%u %u %u %u %u ", tr_library->index(),
|
|
|
|
tr_artist->index(), tr_album->index(),
|
|
|
|
tr_genre->index(), tr_track);
|
|
|
|
date_write(&file, &tr_date);
|
|
|
|
file_writef(&file, " %u %u ", tr_count, tr_length);
|
2014-12-04 08:29:17 -05:00
|
|
|
GenericTag :: write(file);
|
2015-10-24 19:34:45 -04:00
|
|
|
file_writef(&file, "\n%s\n", tr_path.c_str());
|
2014-11-28 21:18:36 -05:00
|
|
|
}
|
2014-11-29 10:38:27 -05:00
|
|
|
|
|
|
|
|
2015-10-25 04:46:56 -04:00
|
|
|
void track_db_init()
|
2014-12-02 08:16:22 -05:00
|
|
|
{
|
2015-09-21 09:02:13 -04:00
|
|
|
db_init(&track_db, "track.db", false);
|
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-09-28 16:03:58 -04:00
|
|
|
struct track *track_add(struct album *album, struct artist *artist,
|
|
|
|
struct genre *genre, struct library *library,
|
|
|
|
const std::string &filepath, const std::string &name,
|
|
|
|
unsigned int length, unsigned int track)
|
2014-11-29 10:38:27 -05:00
|
|
|
{
|
|
|
|
std::string path = filepath.substr(library->primary_key().size() + 1);
|
2015-10-20 10:10:26 -04:00
|
|
|
if (db_get(&track_db, make_key(library, path).c_str()))
|
2015-10-18 10:59:20 -04:00
|
|
|
return NULL;
|
2015-10-24 19:34:45 -04:00
|
|
|
return db_insert(&track_db, new struct track(album, artist, genre, library,
|
2015-10-18 10:59:20 -04:00
|
|
|
path, name, length, track));
|
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 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();
|
|
|
|
}
|