core/tags/track: Add a function to look up tracks using only the path
I'm struggling to come up with a good way to combine code for this with code for track_add(), since the lookup mechanism is basically the same. I'll keep them separate for now and think on it some more. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
e7ceed9b5d
commit
f9a573b6a3
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue