core/tags/track: Count the number of unplayed tracks

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-01 09:51:53 -04:00
parent d825e2d481
commit a97ec66e7f
3 changed files with 43 additions and 4 deletions

View File

@ -10,6 +10,7 @@
static struct database track_db;
static unsigned int unplayed_count = 0;
static gchar *__track_key(struct library *library, gchar *path)
{
@ -69,6 +70,7 @@ struct db_entry *track_alloc(const gchar *key)
track->tr_genre = genre_find(taglib_tag_genre(tag));
track->tr_library = library;
unplayed_count++;
track->tr_count = 0;
track->tr_length = taglib_audioproperties_length(audio);
track->tr_track = taglib_tag_track(tag);
@ -90,11 +92,13 @@ static void track_free(struct db_entry *dbe)
{
struct track *track = TRACK(dbe);
g_free(track->tr_title);
g_free(track->tr_lower);
if (track->tr_count == 0)
unplayed_count--;
if (track->tr_library)
track->tr_library->li_size--;
g_free(track->tr_title);
g_free(track->tr_lower);
g_free(track);
}
@ -123,6 +127,9 @@ static struct db_entry *track_read(struct file *file)
date_read(file, &track->tr_date);
file_readf(file, "%u %u", &track->tr_count, &track->tr_length);
if (track->tr_count == 0)
unplayed_count++;
track->tr_album = album_get(album_id);
track->tr_artist = artist_get(artist_id);
track->tr_genre = genre_get(genre_id);
@ -185,6 +192,11 @@ const struct database *track_db_get()
return &track_db;
}
unsigned int track_db_count_unplayed()
{
return unplayed_count;
}
struct track *track_add(struct library *library, const gchar *filepath)
{
unsigned int offset = strlen(library->li_path) + 1;
@ -260,6 +272,8 @@ gchar *track_path(struct track *track)
void track_played(struct track *track)
{
if (track->tr_count == 0)
unplayed_count--;
track->tr_count++;
date_today(&track->tr_date);
track_db_commit();

View File

@ -74,6 +74,9 @@ void track_db_commit();
/* Called to access the track database. */
const struct database *track_db_get();
/* Called to find the number of unplayed tracks in the database. */
unsigned int track_db_count_unplayed();
/* Called to add a track tag to the database. */
struct track *track_add(struct library *, const gchar *);

View File

@ -167,6 +167,7 @@ static void test_track_compare()
test_equal(track_compare(title, title, COMPARE_COUNT), 0);
test_equal(track_compare(title, kokiri, COMPARE_COUNT), 42);
test_equal(track_compare(kokiri, title, COMPARE_COUNT), -42);
title->tr_count = 0;
/* Compare genres. */
kokiri->tr_genre = genre_find("Video Game");
@ -229,13 +230,16 @@ static void test_track_db()
struct database track_db;
struct track *track;
test_equal(track_db_count_unplayed(), 0);
track = track_add(library, path);
test_verify_track(track);
test_equal(track_db_count_unplayed(), 1);
test_equal((void *)track_add(library, path), NULL);
test_equal((void *)track_get(0), (void *)track);
test_equal((void *)track_get(1), NULL);
test_equal(track_db_get()->db_size, 1);
test_equal(track_db_count_unplayed(), 1);
db_init(&track_db, "track.db", false, track_ops);
db_load(&track_db);
@ -245,9 +249,12 @@ static void test_track_db()
db_load(&track_db);
test_equal(track_db.db_size, 1);
test_verify_track(TRACK(db_first(&track_db)));
/* Make sure our unplayed count isn't skewed */
db_deinit(&track_db);
track_remove(track);
test_equal(track_db_get()->db_size, 0);
test_equal(track_db_count_unplayed(), 0);
track = track_add(library, path);
test_not_equal((void *)track, NULL);
@ -256,15 +263,30 @@ static void test_track_db()
"tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg");
test_not_equal((void *)track, NULL);
test_equal(track_db_get()->db_size, 2);
test_equal(track_db_count_unplayed(), 2);
track_played(track);
test_equal(track_db_count_unplayed(), 1);
track_played(track);
test_equal(track_db_count_unplayed(), 1);
track = track_add(library, "tests/Music/invalid_track");
test_equal((void *)track, NULL);
test_equal(track_db_get()->db_size, 2);
test_equal(track_db_count_unplayed(), 1);
/* Reload the database */
track_db_deinit();
test_equal(track_db_count_unplayed(), 0);
test_equal(track_db_get()->db_size, 0);
track_db_init();
while (idle_run_task()) {};
test_equal(track_db_get()->db_size, 2);
test_equal(track_db_count_unplayed(), 1);
track_remove_all(library);
test_equal(track_db_get()->db_size, 0);
db_deinit(&track_db);
tags_deinit();
filter_deinit();
}