core: Cut back on hardcoded dbe_index uses

The dbe_index of a given database item might change in the future, so we
can't rely on it everywhere.  Let's just use it for saving and loading
files, with the expectation that changes will happen sometime after
startup.

Implements #69: Reduce use of dbe_index in Ocarina code
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-07-27 08:15:15 -04:00
parent e82beb719f
commit c2178bc265
10 changed files with 54 additions and 24 deletions

View File

@ -17,7 +17,7 @@ static guint audio_bus = 0;
static void __audio_save()
{
file_open(&audio_file, OPEN_WRITE);
file_writef(&audio_file, "%u\n", audio_track->tr_dbe.dbe_index);
file_writef(&audio_file, "%u\n", track_index(audio_track));
file_close(&audio_file);
}

View File

@ -174,7 +174,7 @@ void queue_save_tracks(struct queue *queue, struct file *file)
file_writef(file, "%u", queue_size(queue));
queue_for_each(queue, &it)
file_writef(file, " %u", queue_iter_val(&it)->tr_dbe.dbe_index);
file_writef(file, " %u", track_index(queue_iter_val(&it)));
}
void queue_load_flags(struct queue *queue, struct file *file)

View File

@ -17,7 +17,7 @@ static gchar *__track_key(struct library *library, gchar *path)
gchar *res;
if (library)
res = g_strdup_printf("%u/%s", library->li_dbe.dbe_index, path);
res = g_strdup_printf("%u/%s", library_index(library), path);
else
res = g_strdup("");
@ -157,10 +157,10 @@ static void track_write(struct file *file, struct db_entry *dbe)
struct track *track = TRACK(dbe);
gchar *path = __track_path(track);
file_writef(file, "%u %u %u %u %u ", track->tr_library->li_dbe.dbe_index,
track->tr_artist->ar_dbe.dbe_index,
track->tr_album->al_dbe.dbe_index,
track->tr_genre->ge_dbe.dbe_index,
file_writef(file, "%u %u %u %u %u ", library_index(track->tr_library),
artist_index(track->tr_artist),
album_index(track->tr_album),
genre_index(track->tr_genre),
track->tr_track);
date_write(file, &track->tr_date);
file_writef(file, " %hu %hu %s\n%s\n", track->tr_count,

View File

@ -51,6 +51,12 @@ int album_compare_year(struct album *, struct album *);
/* Called to check if an artist has a token that matches the given string. */
bool album_match_token(struct album *album, const gchar *);
/* Called to find the database index of the album tag. */
static inline unsigned int album_index(struct album *album)
{
return album->al_dbe.dbe_index;
}
/* Called to check if album artwork has been downloaded. */
bool album_artwork_exists(struct album *);

View File

@ -52,6 +52,12 @@ int artist_compare(struct artist *, struct artist *);
/* Called to check if an artist has a token that matches the given string. */
bool artist_match_token(struct artist *artist, const gchar *);
/* Called to find the database index of the artist tag. */
static inline unsigned int artist_index(struct artist *artist)
{
return artist->ar_dbe.dbe_index;
}
#ifdef CONFIG_TESTING
const struct db_ops *test_artist_ops();
#endif /* CONFIG_TESTING */

View File

@ -43,6 +43,12 @@ int genre_compare(struct genre *, struct genre *);
/* Called to check if a genre has a token that matches the given string. */
bool genre_match_token(struct genre *, const gchar *);
/* Called to find the database index of the genre tag. */
static inline unsigned int genre_index(struct genre *genre)
{
return genre->ge_dbe.dbe_index;
}
#ifdef CONFIG_TESTING
const struct db_ops *test_genre_ops();
#endif /* CONFIG_TESTING */

View File

@ -52,6 +52,12 @@ void library_remove(struct library *);
/* Called to configure if the library tag is enabled. */
void library_set_enabled(struct library *, bool);
/* Called to find the database index of the library tag. */
static inline unsigned int library_index(struct library *library)
{
return library->li_dbe.dbe_index;
}
/*
* Called to find the full path of files under the library directory.
* This function allocates a new string that MUST be freed with g_free().

View File

@ -102,6 +102,12 @@ int track_compare(struct track *, struct track *, enum compare_t);
/* Called to check if a track has a token that matches the given string */
bool track_match_token(struct track *, const gchar *);
/* Called to find the database index of the track tag. */
static inline unsigned int track_index(struct track *track)
{
return track->tr_dbe.dbe_index;
}
/*
* Called to find the full path of the track tag.
* This function returns a new string that MUST be freed with g_free().

View File

@ -135,7 +135,7 @@ static void test_next()
for (i = 2; i >= 0; i--) {
test_loop_equal(queue_size(temp_q), i + 1, i);
if (i > 0) {
test_loop_equal(audio_next()->tr_dbe.dbe_index, i, i);
test_loop_equal(audio_next()->tr_track, track_get(i)->tr_track, i);
} else /* Simulate an error. */
test_send_error();
test_loop_equal((void *)queue_at(history_q, 0),
@ -179,22 +179,22 @@ static void test_prev()
test_equal(state_count, 2);
test_equal(audio_pause(), (bool)true);
test_equal(audio_prev()->tr_dbe.dbe_index, 0);
test_equal(audio_prev()->tr_track, track_get(0)->tr_track);
test_equal((void *)queue_at(history_q, 0), (void *)track);
test_equal(audio_cur_state(), GST_STATE_PLAYING);
test_equal(audio_cur_track()->tr_dbe.dbe_index, 0);
test_equal(audio_cur_track()->tr_track, track_get(0)->tr_track);
test_equal(state_count, 4);
test_equal(audio_prev()->tr_dbe.dbe_index, 1);
test_equal(audio_prev()->tr_track, track_get(1)->tr_track);
test_equal((void *)queue_at(history_q, 0), (void *)track);
test_equal(audio_cur_state(), GST_STATE_PLAYING);
test_equal(audio_cur_track()->tr_dbe.dbe_index, 1);
test_equal(audio_cur_track()->tr_track, track_get(1)->tr_track);
test_equal(state_count, 5);
test_equal(audio_prev()->tr_dbe.dbe_index, 2);
test_equal(audio_prev()->tr_track, track_get(2)->tr_track);
test_equal((void *)queue_at(history_q, 0), (void *)track);
test_equal(audio_cur_state(), GST_STATE_PLAYING);
test_equal(audio_cur_track()->tr_dbe.dbe_index, 2);
test_equal(audio_cur_track()->tr_track, track_get(2)->tr_track);
test_equal(state_count, 6);
}

View File

@ -404,9 +404,9 @@ static void test_rand_select()
static void test_sorting()
{
unsigned int ex_count[] = { 6, 7, 8, 9, 10, 11, 12, 0, 1, 2, 3, 4, 5 };
unsigned int ex_title[] = { 6, 9, 8, 3, 2, 5, 1, 4, 11, 10, 12, 0, 7 };
unsigned int ex_co_ti[] = { 3, 2, 5, 1, 4, 0, 6, 9, 8, 11, 10, 12, 7 };
unsigned int ex_count[] = { 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 };
unsigned int ex_title[] = { 7, 10, 9, 4, 3, 6, 2, 5, 12, 11, 13, 1, 8 };
unsigned int ex_co_ti[] = { 4, 3, 6, 2, 5, 1, 7, 10, 9, 12, 11, 13, 8 };
struct track *track;
unsigned int i;
struct queue q;
@ -414,7 +414,7 @@ static void test_sorting()
queue_init(&q, Q_SAVE_SORT | Q_ADD_FRONT, &test_ops, NULL);
for (i = 0; i < 13; i++) {
track = track_get(i);
track->tr_count = (i < 6) ? 4 : 2;
track->tr_count = (track->tr_track <= 6) ? 4 : 2;
queue_add(&q, track);
}
@ -429,7 +429,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, i, i);
test_loop_equal(track->tr_track, i + 1, i);
} test_loop_passed();
test_equal(count_sort, 1);
@ -437,7 +437,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i);
test_loop_equal(track->tr_track, ex_count[i], i);
} test_loop_passed();
test_equal(count_sort, 2);
@ -446,7 +446,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, ex_count[i], i);
test_loop_equal(track->tr_track, ex_count[i], i);
} test_loop_passed();
test_equal(count_sort, 2);
@ -455,7 +455,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, ex_title[i], i);
test_loop_equal(track->tr_track, ex_title[i], i);
} test_loop_passed();
test_equal(count_sort, 3);
@ -465,7 +465,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, ex_co_ti[i], i);
test_loop_equal(track->tr_track, ex_co_ti[i], i);
} test_loop_passed();
test_equal(count_sort, 6);
@ -477,7 +477,7 @@ static void test_sorting()
for (i = 0; i < 13; i++) {
track = queue_at(&q, i);
test_loop_not_equal((void *)track, NULL, i);
test_loop_equal(track->tr_dbe.dbe_index, 12 - i, i);
test_loop_equal(track->tr_track, 13 - i, i);
} test_loop_passed();
test_equal(count_sort, 6);