Track: Hide the library field from the public

And provide an accessor function.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-20 08:16:13 -05:00
parent 7e52c9b364
commit 37613573e4
5 changed files with 25 additions and 19 deletions

View File

@ -120,7 +120,7 @@ static void validate_library(Library *&library)
for (it = db->begin(); it != db->end(); it = db->next(it)) { for (it = db->begin(); it != db->end(); it = db->next(it)) {
track = *it; track = *it;
if (track->library != library) if (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) {
@ -144,7 +144,7 @@ void library :: init()
library_q.load(); library_q.load();
for (it = db->begin(); it != db->end(); it = db->next(it)) { for (it = db->begin(); it != db->end(); it = db->next(it)) {
if ((*it)->library->enabled()) if ((*it)->library()->enabled())
library_q.add(*it); library_q.add(*it);
} }
} }
@ -205,7 +205,7 @@ void library :: set_enabled(Library *library, bool enabled)
library->set_enabled(enabled); library->set_enabled(enabled);
for (it = db->begin(); it != db->end(); it = db->next(it)) { for (it = db->begin(); it != db->end(); it = db->next(it)) {
if ((*it)->library == library) { if ((*it)->library() == library) {
if (enabled) if (enabled)
library_q.add(*it); library_q.add(*it);
else else

View File

@ -21,18 +21,18 @@ Database<Track> track_db("track.db", false);
* *
*/ */
Track :: Track(const std::string &f, Library *l) Track :: Track(const std::string &f, Library *library)
: library(l), artist(NULL), album(NULL), genre(NULL), : _library(library), artist(NULL), album(NULL), genre(NULL),
play_count(0), last_year(0), last_month(0), last_day(0), play_count(0), last_year(0), last_month(0), last_day(0),
filepath(f.substr(l->primary_key().size() + 1)) filepath(f.substr(library->primary_key().size() + 1))
{ {
library->inc_size(); library->inc_size();
} }
Track :: ~Track() Track :: ~Track()
{ {
if (library) if (_library)
library->dec_size(); _library->dec_size();
} }
const std::string Track :: primary_key() const const std::string Track :: primary_key() const
@ -51,7 +51,7 @@ void Track :: read(File &f)
title = f.getline(); title = f.getline();
filepath = f.getline(); filepath = f.getline();
library = tags :: get_library(library_id); _library = tags :: get_library(library_id);
artist = tags :: get_artist(artist_id); artist = tags :: get_artist(artist_id);
album = tags :: get_album(album_id); album = tags :: get_album(album_id);
genre = tags :: get_genre(genre_id); genre = tags :: get_genre(genre_id);
@ -59,13 +59,13 @@ void Track :: read(File &f)
title_lower = filter :: add(title, index()); title_lower = filter :: add(title, index());
filter :: add(artist->name(), index()); filter :: add(artist->name(), index());
filter :: add(album->name(), index()); filter :: add(album->name(), index());
library->inc_size(); _library->inc_size();
set_length_str(); set_length_str();
} }
void Track :: write(File &f) void Track :: write(File &f)
{ {
f << library->index() << " " << artist->index() << " "; f << _library->index() << " " << artist->index() << " ";
f << album->index() << " " << genre->index() << " " << track << " "; f << album->index() << " " << genre->index() << " " << track << " ";
f << last_year << " " << last_month << " " << last_day << " "; f << last_year << " " << last_month << " " << last_day << " ";
f << play_count << " " << length << " " << title << std::endl; f << play_count << " " << length << " " << title << std::endl;
@ -105,7 +105,7 @@ bool Track :: tag()
TagLib :: AudioProperties *audio; TagLib :: AudioProperties *audio;
TagLib :: FileRef ref(path().c_str(), true, TagLib::AudioProperties::Fast); TagLib :: FileRef ref(path().c_str(), true, TagLib::AudioProperties::Fast);
library->inc_size(); _library->inc_size();
if (ref.isNull()) { if (ref.isNull()) {
print("WARNING: Could not read tags for file %s\n", path().c_str()); print("WARNING: Could not read tags for file %s\n", path().c_str());
return false; return false;
@ -132,9 +132,9 @@ bool Track :: tag()
const std::string Track :: path() const const std::string Track :: path() const
{ {
if (!library) if (!_library)
return ""; return "";
return library->primary_key() + "/" + filepath; return _library->primary_key() + "/" + filepath;
} }
void Track :: played() void Track :: played()
@ -244,7 +244,7 @@ void tagdb :: remove_library(unsigned int library_id)
{ {
Database<Track>::iterator it; Database<Track>::iterator it;
for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) { for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) {
if ((*it)->library->index() == library_id) if ((*it)->library()->index() == library_id)
track_db.remove((*it)->index()); track_db.remove((*it)->index());
} }
tagdb :: commit(); tagdb :: commit();

View File

@ -7,6 +7,8 @@
Track :: Track() Track :: Track()
: GenericTag(), : GenericTag(),
library(NULL), artist(NULL), album(NULL), genre(NULL), _library(NULL), artist(NULL), album(NULL), genre(NULL),
track(0), length(0), play_count(0), last_year(0), last_month(0), last_day(0) track(0), length(0), play_count(0), last_year(0), last_month(0), last_day(0)
{} {}
Library *Track :: library() { return _library; }

View File

@ -39,11 +39,11 @@ enum sort_t {
class Track : public GenericTag { class Track : public GenericTag {
private: private:
Library *_library; /**< Pointer to the Library containing this track. */
void set_length_str(); void set_length_str();
public: public:
/** Pointer to the library containing this track */
Library *library;
/** Pointer to this track's artist information */ /** Pointer to this track's artist information */
Artist *artist; Artist *artist;
/** Pointer to this track's album information */ /** Pointer to this track's album information */
@ -86,6 +86,10 @@ public:
/** Track destructor */ /** Track destructor */
~Track(); ~Track();
Library *library(); /**< @return Track::_library. */
/** /**
* Called to access a track's primary key * Called to access a track's primary key
* @return The full path of the track * @return The full path of the track

View File

@ -9,7 +9,7 @@ static void test_track_tag()
{ {
Track track; Track track;
test_equal(track.library, (Library *)NULL); test_equal(track.library(), (Library *)NULL);
test_equal(track.artist, (Artist *)NULL); test_equal(track.artist, (Artist *)NULL);
test_equal(track.album, (Album *)NULL); test_equal(track.album, (Album *)NULL);
test_equal(track.genre, (Genre *)NULL); test_equal(track.genre, (Genre *)NULL);