core/tags/track: Replace tags :: track_size() with track_db_get()

We want to loop over the track database in several places, so let's make
this easier by just returning the database itself.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-25 05:06:23 -04:00
parent eea0f57cf8
commit 192bdc3ac7
5 changed files with 27 additions and 44 deletions

View File

@ -142,11 +142,10 @@ static void scan_path(struct scan_info &scan)
static void validate_library(struct library *&library) static void validate_library(struct library *&library)
{ {
struct track *track; struct track *track, *next;
for (unsigned int i = 0; i < tags :: track_size(); i++) { db_for_each(track, next, track_db_get()) {
track = track_get(i); if (track->library() != library)
if (!track || (track->library() != library))
continue; continue;
if (g_file_test(track->path().c_str(), G_FILE_TEST_EXISTS) == false) { if (g_file_test(track->path().c_str(), G_FILE_TEST_EXISTS) == false) {
@ -164,13 +163,12 @@ static void validate_library(struct library *&library)
void collection :: init() void collection :: init()
{ {
struct track *track; struct track *track, *next;
library_q.load(); library_q.load();
for (unsigned int i = 0; i < tags :: track_size(); i++) { db_for_each(track, next, track_db_get()) {
track = track_get(i); if (track->library()->li_enabled)
if (track && (track->library()->li_enabled))
library_q.add(track); library_q.add(track);
} }
} }
@ -220,16 +218,15 @@ void collection :: update_all()
void collection :: set_enabled(struct library *library, bool enabled) void collection :: set_enabled(struct library *library, bool enabled)
{ {
struct track *track; struct track *track, *next;
if (!library || (library->li_enabled == enabled)) if (!library || (library->li_enabled == enabled))
return; return;
library_set_enabled(library, enabled); library_set_enabled(library, enabled);
for (unsigned int i = 0; i < tags :: track_size(); i++) { db_for_each(track, next, track_db_get()) {
track = track_get(i); if (track->library() == library) {
if (track && (track->library() == library)) {
if (enabled) if (enabled)
library_q.add(track); library_q.add(track);
else else

View File

@ -33,15 +33,12 @@ public:
unsigned int find_average_count() unsigned int find_average_count()
{ {
struct track *track; struct track *track, *next;
unsigned int total = 0, count = 0; unsigned int total = 0, count = 0;
for (unsigned int i = 0; i < tags :: track_size(); i++) { db_for_each(track, next, track_db_get()) {
track = track_get(i); total += track->count();
if (track != NULL) { count++;
total += track->count();
count++;
}
} }
if (count > 0) if (count > 0)
@ -61,18 +58,15 @@ public:
void dynamic_fill(const std::string &name) void dynamic_fill(const std::string &name)
{ {
struct track *track; struct track *track, *next;
unsigned int avg = 0; unsigned int avg = 0;
if ((name == "Most Played") || (name == "Least Played")) if ((name == "Most Played") || (name == "Least Played"))
avg = find_average_count(); avg = find_average_count();
clear(); clear();
for (unsigned int i = 0; i < tags :: track_size(); i++) { db_for_each(track, next, track_db_get())
track = track_get(i); dynamic_add(name, track, avg);
if (track != NULL)
dynamic_add(name, track, avg);
}
} }
}; };

View File

@ -162,6 +162,11 @@ void track_db_commit()
db_save(&track_db); db_save(&track_db);
} }
const database<struct track> *track_db_get()
{
return &track_db;
}
struct track *track_add(struct album *album, struct artist *artist, struct track *track_add(struct album *album, struct artist *artist,
struct genre *genre, struct library *library, struct genre *genre, struct library *library,
const std::string &filepath, const std::string &name, const std::string &filepath, const std::string &name,
@ -194,8 +199,3 @@ struct track *track_get(const unsigned int index)
{ {
return db_at(&track_db, index); return db_at(&track_db, index);
} }
unsigned int tags :: track_size()
{
return db_actual_size(&track_db);
}

View File

@ -115,17 +115,6 @@ struct track : public GenericTag {
void write(file &); void write(file &);
}; };
namespace tags
{
/**
* Called to find the number of rows in the track_db,
* including NULL rows.
*
* @return The Database::actual_size() of the track_db.
*/
unsigned int track_size();
}
/* Called to initialize the track database. */ /* Called to initialize the track database. */
void track_db_init(); void track_db_init();
@ -136,6 +125,9 @@ void track_db_deinit();
/* Called to commit the track database. */ /* Called to commit the track database. */
void track_db_commit(); void track_db_commit();
/* Called to access the track database. */
const database<struct track> *track_db_get();
/* Called to add a track tag to the database. */ /* Called to add a track tag to the database. */
struct track *track_add(struct album *, struct artist *, struct genre *, struct track *track_add(struct album *, struct artist *, struct genre *,
struct library *, const std::string &, struct library *, const std::string &,

View File

@ -138,7 +138,7 @@ static void test_track_tag_lookup()
"Ocarina Medley", 232, 12); "Ocarina Medley", 232, 12);
test_not_equal(b, a); test_not_equal(b, a);
test_equal(library->li_size, (unsigned)2); test_equal(library->li_size, (unsigned)2);
test_equal(tags :: track_size(), b->index() + 1); test_equal(track_db_get()->db_size, b->index() + 1);
test_equal(track_get(a->index()), a); test_equal(track_get(a->index()), a);
@ -157,7 +157,7 @@ static void test_track_tag_lookup()
track_db_commit(); track_db_commit();
test_track_tag_load_db(1); test_track_tag_load_db(1);
test_equal(library->li_size, (unsigned)1); test_equal(library->li_size, (unsigned)1);
test_equal(tags :: track_size(), b->index() + 1); test_equal(db_actual_size(track_db_get()), b->index() + 1);
a = track_add(album, artist, genre, library, a = track_add(album, artist, genre, library,
@ -165,7 +165,7 @@ static void test_track_tag_lookup()
"Legend of Zelda Medley", 288, 13); "Legend of Zelda Medley", 288, 13);
test_not_equal(a, (struct track *)NULL); test_not_equal(a, (struct track *)NULL);
verify_track_tag(a, 2); verify_track_tag(a, 2);
test_equal(tags :: track_size(), a->index() + 1); test_equal(db_actual_size(track_db_get()), a->index() + 1);
track_remove_all(library); track_remove_all(library);
test_track_tag_load_db(0); test_track_tag_load_db(0);