core/tags/track: Directly inherit from DatabaseEntry

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-29 03:10:19 -04:00
parent ee71786c09
commit 33e20989fa
6 changed files with 45 additions and 27 deletions

View File

@ -90,7 +90,7 @@ static inline int track_compare(struct track *lhs, struct track *rhs, sort_t fie
case SORT_PLAYED:
return date_compare(&lhs->tr_date, &rhs->tr_date);
case SORT_TITLE:
return lhs->compare(rhs);
return track_compare(lhs, rhs);
case SORT_TRACK:
return lhs->tr_track - rhs->tr_track;
case SORT_YEAR:

View File

@ -2,7 +2,9 @@
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/filter.h>
extern "C" {
#include <core/string.h>
}
#include <core/tags/track.h>
#include <glib.h>
@ -25,33 +27,35 @@ static const std::string make_key(struct library *library, const std::string &pa
}
track :: track()
: GenericTag(),
tr_album(NULL), tr_artist(NULL), tr_genre(NULL), tr_library(NULL),
: tr_album(NULL), tr_artist(NULL), tr_genre(NULL), tr_library(NULL),
tr_count(0), tr_length(0), tr_track(0)
{}
track :: track(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)
: GenericTag(name),
tr_album(album), tr_artist(artist), tr_genre(genre),
: tr_album(album), tr_artist(artist), tr_genre(genre),
tr_library(library), tr_count(0), tr_length(length), tr_track(track),
tr_path(filepath)
tr_path(filepath), tr_title(name)
{
gchar *lower = string_lowercase(name.c_str());
tr_lower = lower;
g_free(lower);
date_set(&tr_date, 0, 0, 0);
filter :: add(this->name(), index());
filter :: add(this->tr_title, index());
filter :: add(tr_artist->ar_name, index());
filter :: add(tr_album->al_name, index());
tr_library->li_size++;
}
track :: track(const struct track &track)
: GenericTag(track),
tr_album(track.tr_album), tr_artist(track.tr_artist),
: 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)
tr_path(track.tr_path), tr_title(track.tr_title),
tr_lower(track.tr_lower)
{
tr_library->li_size++;
}
@ -70,24 +74,29 @@ const std::string track :: primary_key() const
void track :: read(file &file)
{
unsigned int library_id, artist_id, album_id, genre_id;
gchar *path;
gchar *path, *name, *lower;
file_readf(&file, "%u %u %u %u %u", &library_id, &artist_id,
&album_id, &genre_id, &tr_track);
date_read(&file, &tr_date);
file_readf(&file, "%u %u", &tr_count, &tr_length);
GenericTag :: read(file);
name = file_readl(&file);
path = file_readl(&file);
tr_path = path;
lower = string_lowercase(name);
tr_title = name;
tr_path = path;
tr_lower = lower;
g_free(name);
g_free(path);
g_free(lower);
tr_library = library_get(library_id);
tr_artist = artist_get(artist_id);
tr_album = album_get(album_id);
tr_genre = genre_get(genre_id);
filter :: add(name(), index());
filter :: add(tr_title, index());
filter :: add(tr_artist->ar_name, index());
filter :: add(tr_album->al_name, index());
tr_library->li_size++;
@ -99,9 +108,8 @@ void track :: write(file &file)
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);
GenericTag :: write(file);
file_writef(&file, "\n%s\n", tr_path.c_str());
file_writef(&file, " %u %u %s\n%s\n", tr_count, tr_length,
tr_title.c_str(), tr_path.c_str());
}
@ -159,6 +167,11 @@ struct track *track_get(const unsigned int index)
return db_at(&track_db, index);
}
int track_compare(struct track *lhs, struct track *rhs)
{
return string_compare(lhs->tr_lower.c_str(), rhs->tr_lower.c_str());
}
const std::string track_path(struct track *track)
{
if (track->tr_library)

View File

@ -68,7 +68,7 @@ public:
set_markup(o_album, "x-large", "From: " + track->tr_album->al_name);
set_markup(o_artist, "x-large", "By: " + track->tr_artist->ar_name);
set_markup(o_title, "xx-large", track->name());
set_markup(o_title, "xx-large", track->tr_title);
o_duration->set_text(len);
g_free(len);

View File

@ -110,7 +110,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column,
case 0:
return set_val(track->tr_track, value);
case 1:
return set_val(track->name(), value);
return set_val(track->tr_title, value);
case 2:
str = string_sec2str(track->tr_length);
set_val(Glib::ustring(str), value);

View File

@ -7,7 +7,7 @@
extern "C" {
#include <core/date.h>
}
#include <core/tags/generic.h>
#include <core/database.h>
#include <core/tags/artist.h>
#include <core/tags/album.h>
#include <core/tags/genre.h>
@ -18,7 +18,7 @@ extern "C" {
* The Track tag is used to store information about tracks that
* have been added to the tag database.
*/
struct track : public GenericTag {
struct track : public DatabaseEntry {
struct album *tr_album; /* This track's associated album. */
struct artist *tr_artist; /* This track's associated artist. */
struct genre *tr_genre; /* This track's associated genre. */
@ -28,8 +28,10 @@ struct track : public GenericTag {
unsigned int tr_length; /* This track's length, in seconds. */
unsigned int tr_track; /* This track's track number. */
struct date tr_date; /* This track's last-played date. */
std::string tr_path; /* This track's path, relative to the library. */
struct date tr_date; /* This track's last-played date. */
std::string tr_path; /* This track's path, relative to the library. */
std::string tr_title; /* This track's title. */
std::string tr_lower; /* This track's title (lowercased). */
/**
* Track constructor
@ -109,6 +111,9 @@ void track_remove_all(struct library *);
/* Called to get a track tag with a specific index. */
struct track *track_get(const unsigned int);
/* Called to compare two track tags */
int track_compare(struct track *, struct track *);
/* Called to find the full path of the track tag. */
const std::string track_path(struct track *);

View File

@ -29,8 +29,8 @@ static void test_track_tag_default()
test_equal(track.tr_genre, (struct genre *)NULL);
test_equal(track.tr_library, (struct library *)NULL);
test_equal(track.name(), (std::string)"");
test_equal(track.lowercase(), (std::string)"");
test_equal(track.tr_title, (std::string)"");
test_equal(track.tr_lower, (std::string)"");
test_equal(track.primary_key(), (std::string)"");
test_equal(track_last_play(&track), (std::string)"Never");
test_equal(track_path(&track), (std::string)"");
@ -47,8 +47,8 @@ static void verify_track_tag(struct track *track, unsigned int size)
test_equal(track->tr_genre, genre);
test_equal(track->tr_library, library);
test_equal(track->name(), (std::string)"Legend of Zelda Medley");
test_equal(track->lowercase(), (std::string)"legend of zelda medley");
test_equal(track->tr_title, (std::string)"Legend of Zelda Medley");
test_equal(track->tr_lower, (std::string)"legend of zelda medley");
test_equal(track_last_play(track), (std::string)"Never");
test_equal(track_path(track), (std::string)MUSIC_DIR + "/Hyrule Symphony/13 - Legend of Zelda Medley.mp3");
test_equal(track->primary_key(), (std::string)"0/Hyrule Symphony/13 - Legend of Zelda Medley.mp3");