diff --git a/core/tags/track.c b/core/tags/track.c index b5963147..949f7d73 100644 --- a/core/tags/track.c +++ b/core/tags/track.c @@ -285,6 +285,23 @@ struct track *track_get(const unsigned int index) return TRACK(db_at(&track_db, index)); } +struct track *track_lookup(const gchar *filepath) +{ + struct library *library = library_lookup(filepath); + unsigned int offset; + struct track *track; + gchar *key; + + if (!library) + return NULL; + + offset = strlen(library->li_path) + 1; + key = __track_key(library, g_strdup(filepath + offset)); + track = TRACK(db_get(&track_db, key)); + g_free(key); + return track; +} + int track_compare(struct track *lhs, struct track *rhs, enum compare_t compare) { switch (compare) { diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 8bdc3573..51116861 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -99,6 +99,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 look up a track tag using only a filepath. */ +struct track *track_lookup(const gchar *); + /* Called to compare two track tags */ int track_compare(struct track *, struct track *, enum compare_t); diff --git a/tests/core/tags/track.c b/tests/core/tags/track.c index eae4134f..82f2bed4 100644 --- a/tests/core/tags/track.c +++ b/tests/core/tags/track.c @@ -209,8 +209,14 @@ static void __test_track_db_subprocess() g_assert_cmpuint(track_db_count_unplayed(), ==, 0); g_assert_cmpuint(track_db_count_plays(), ==, 0); g_assert_cmpuint(track_db_average_plays(), ==, 0); + + g_assert_null(track_lookup(NULL)); + g_assert_null(track_lookup("/home/Zelda/Music/ocarina.ogg")); + g_assert_null(track_lookup(path)); + track = track_add(library, path); test_verify_track(track); + g_assert(track_lookup(path) == track); g_assert_cmpuint(track_db_count_unplayed(), ==, 1); g_assert_null(track_add(library, path));