core/tags/track: Expand track_compare() to compare by different fields

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-17 09:18:12 -05:00
parent 08c7323dfc
commit 4e8a63cc36
4 changed files with 88 additions and 38 deletions

View File

@ -70,38 +70,6 @@ void Queue :: set_notifier(QNotifier *notify)
_notify = notify;
}
/*
* Returns:
* 0: lhs == rhs
* < 0: lhs < rhs, or rhs is empty
* > 0: lhs > rhs, or lhs is empty
*/
static inline int track_compare(struct track *lhs, struct track *rhs,
compare_t field)
{
switch (field) {
case COMPARE_ARTIST:
return artist_compare(lhs->tr_artist, rhs->tr_artist);
case COMPARE_COUNT:
return lhs->tr_count - rhs->tr_count;
case COMPARE_GENRE:
return genre_compare(lhs->tr_genre, rhs->tr_genre);
case COMPARE_LENGTH:
return lhs->tr_length - rhs->tr_length;
case COMPARE_PLAYED:
return date_compare(&lhs->tr_date, &rhs->tr_date);
case COMPARE_TITLE:
return track_compare(lhs, rhs);
case COMPARE_TRACK:
return lhs->tr_track - rhs->tr_track;
case COMPARE_YEAR:
return album_compare_year(lhs->tr_album, rhs->tr_album);
case COMPARE_ALBUM:
return album_compare(lhs->tr_album, rhs->tr_album);
}
return 0;
}
static bool track_less_than(struct track *lhs, struct track *rhs,
std::vector<struct sort_info> &order)
{

View File

@ -219,9 +219,30 @@ struct track *track_get(const unsigned int index)
return TRACK(db_at(&track_db, index));
}
int track_compare(struct track *lhs, struct track *rhs)
int track_compare(struct track *lhs, struct track *rhs, enum compare_t compare)
{
return string_compare(lhs->tr_lower, rhs->tr_lower);
switch (compare) {
case COMPARE_ARTIST:
return artist_compare(lhs->tr_artist, rhs->tr_artist);
case COMPARE_ALBUM:
return album_compare(lhs->tr_album, rhs->tr_album);
case COMPARE_COUNT:
return lhs->tr_count - rhs->tr_count;
case COMPARE_GENRE:
return genre_compare(lhs->tr_genre, rhs->tr_genre);
case COMPARE_LENGTH:
return lhs->tr_length - rhs->tr_length;
case COMPARE_PLAYED:
return date_compare(&lhs->tr_date, &rhs->tr_date);
case COMPARE_TITLE:
return string_compare(lhs->tr_lower, rhs->tr_lower);
case COMPARE_TRACK:
return lhs->tr_track - rhs->tr_track;
case COMPARE_YEAR:
return album_compare_year(lhs->tr_album, rhs->tr_album);
}
return 0; /* We should never get here. */
}
gchar *track_path(struct track *track)

View File

@ -87,7 +87,7 @@ void track_remove_all(struct library *);
struct track *track_get(const unsigned int);
/* Called to compare two track tags */
int track_compare(struct track *, struct track *);
int track_compare(struct track *, struct track *, enum compare_t);
/*
* Called to find the full path of the track tag.

View File

@ -147,9 +147,70 @@ static void test_track_compare()
struct track *title = test_alloc("0/Hyrule Symphony/01 - Title Theme.ogg");
struct track *kokiri = test_alloc("0/Hyrule Symphony/02 - Kokiri Forest.ogg");
test_equal(track_compare(title, title), 0);
test_equal(track_compare(kokiri, title), -1);
test_equal(track_compare(title, kokiri), 1);
/* Compare artists. */
title->tr_artist = artist_find("Hajime Wakai");
test_equal(track_compare(title, title, COMPARE_ARTIST), 0);
test_equal(track_compare(kokiri, title, COMPARE_ARTIST), 1);
test_equal(track_compare(title, kokiri, COMPARE_ARTIST), -1);
/* Compare albums. */
kokiri->tr_album = album_find("Ocarina of Time", 1996);
test_equal(track_compare(title, title, COMPARE_ALBUM), 0);
test_equal(track_compare(title, kokiri, COMPARE_ALBUM), -1);
test_equal(track_compare(kokiri, title, COMPARE_ALBUM), 1);
/* Compare play counts. */
title->tr_count = 42;
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);
/* Compare genres. */
kokiri->tr_genre = genre_find("Video Game");
test_equal(track_compare(title, title, COMPARE_GENRE), 0);
test_equal(track_compare(title, kokiri, COMPARE_GENRE), -1);
test_equal(track_compare(kokiri, title, COMPARE_GENRE), 1);
/* Compare lenghts (title = 243, kokiri = 130). */
test_equal(track_compare(title, title, COMPARE_LENGTH), 0);
test_equal(track_compare(title, kokiri, COMPARE_LENGTH), 113);
test_equal(track_compare(kokiri, title, COMPARE_LENGTH), -113);
/* Compare last played dates. */
date_set(&kokiri->tr_date, 0, 0, 17);
test_equal(track_compare(title, title, COMPARE_PLAYED), 0);
test_equal(track_compare(title, kokiri, COMPARE_PLAYED), -17);
test_equal(track_compare(kokiri, title, COMPARE_PLAYED), 17);
date_set(&kokiri->tr_date, 0, 6, 17);
test_equal(track_compare(title, title, COMPARE_PLAYED), 0);
test_equal(track_compare(title, kokiri, COMPARE_PLAYED), -6);
test_equal(track_compare(kokiri, title, COMPARE_PLAYED), 6);
date_set(&kokiri->tr_date, 1988, 6, 17);
test_equal(track_compare(title, title, COMPARE_PLAYED), 0);
test_equal(track_compare(title, kokiri, COMPARE_PLAYED), -1988);
test_equal(track_compare(kokiri, title, COMPARE_PLAYED), 1988);
/* Compare titles. */
test_equal(track_compare(title, title, COMPARE_TITLE), 0);
test_equal(track_compare(title, kokiri, COMPARE_TITLE), 1);
test_equal(track_compare(kokiri, title, COMPARE_TITLE), -1);
/* Compare track numbers. */
test_equal(track_compare(title, title, COMPARE_TRACK), 0);
test_equal(track_compare(title, kokiri, COMPARE_TRACK), -1);
test_equal(track_compare(kokiri, title, COMPARE_TRACK), 1);
/* Compare years. */
test_equal(track_compare(title, title, COMPARE_YEAR), 0);
test_equal(track_compare(title, kokiri, COMPARE_YEAR), 2);
test_equal(track_compare(kokiri, title, COMPARE_YEAR), -2);
kokiri->tr_album->al_year = title->tr_album->al_year;
test_equal(track_compare(title, title, COMPARE_YEAR), 0);
test_equal(track_compare(title, kokiri, COMPARE_YEAR), -1);
test_equal(track_compare(kokiri, title, COMPARE_YEAR), 1);
g_free(title->tr_path);
g_free(kokiri->tr_path);