From 37613573e45d8495624778dcc3b27ad9d90a8b2f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 20 Nov 2014 08:16:13 -0500 Subject: [PATCH] Track: Hide the library field from the public And provide an accessor function. Signed-off-by: Anna Schumaker --- core/library.cpp | 6 +++--- core/tags.cpp | 24 ++++++++++++------------ core/tags/track.cpp | 4 +++- include/core/tags/track.h | 8 ++++++-- tests/core/tags/track.cpp | 2 +- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/core/library.cpp b/core/library.cpp index 9ef75b15..1df6e6a5 100644 --- a/core/library.cpp +++ b/core/library.cpp @@ -120,7 +120,7 @@ static void validate_library(Library *&library) for (it = db->begin(); it != db->end(); it = db->next(it)) { track = *it; - if (track->library != library) + if (track->library() != library) continue; if (g_file_test(track->path().c_str(), G_FILE_TEST_EXISTS) == false) { @@ -144,7 +144,7 @@ void library :: init() library_q.load(); for (it = db->begin(); it != db->end(); it = db->next(it)) { - if ((*it)->library->enabled()) + if ((*it)->library()->enabled()) library_q.add(*it); } } @@ -205,7 +205,7 @@ void library :: set_enabled(Library *library, bool enabled) library->set_enabled(enabled); for (it = db->begin(); it != db->end(); it = db->next(it)) { - if ((*it)->library == library) { + if ((*it)->library() == library) { if (enabled) library_q.add(*it); else diff --git a/core/tags.cpp b/core/tags.cpp index 0401be9f..e9090e6d 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -21,18 +21,18 @@ Database track_db("track.db", false); * */ -Track :: Track(const std::string &f, Library *l) - : library(l), artist(NULL), album(NULL), genre(NULL), +Track :: Track(const std::string &f, Library *library) + : _library(library), artist(NULL), album(NULL), genre(NULL), 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(); } Track :: ~Track() { - if (library) - library->dec_size(); + if (_library) + _library->dec_size(); } const std::string Track :: primary_key() const @@ -51,7 +51,7 @@ void Track :: read(File &f) title = f.getline(); filepath = f.getline(); - library = tags :: get_library(library_id); + _library = tags :: get_library(library_id); artist = tags :: get_artist(artist_id); album = tags :: get_album(album_id); genre = tags :: get_genre(genre_id); @@ -59,13 +59,13 @@ void Track :: read(File &f) title_lower = filter :: add(title, index()); filter :: add(artist->name(), index()); filter :: add(album->name(), index()); - library->inc_size(); + _library->inc_size(); set_length_str(); } void Track :: write(File &f) { - f << library->index() << " " << artist->index() << " "; + f << _library->index() << " " << artist->index() << " "; f << album->index() << " " << genre->index() << " " << track << " "; f << last_year << " " << last_month << " " << last_day << " "; f << play_count << " " << length << " " << title << std::endl; @@ -105,7 +105,7 @@ bool Track :: tag() TagLib :: AudioProperties *audio; TagLib :: FileRef ref(path().c_str(), true, TagLib::AudioProperties::Fast); - library->inc_size(); + _library->inc_size(); if (ref.isNull()) { print("WARNING: Could not read tags for file %s\n", path().c_str()); return false; @@ -132,9 +132,9 @@ bool Track :: tag() const std::string Track :: path() const { - if (!library) + if (!_library) return ""; - return library->primary_key() + "/" + filepath; + return _library->primary_key() + "/" + filepath; } void Track :: played() @@ -244,7 +244,7 @@ void tagdb :: remove_library(unsigned int library_id) { Database::iterator 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()); } tagdb :: commit(); diff --git a/core/tags/track.cpp b/core/tags/track.cpp index adbcf4aa..cc9626bf 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -7,6 +7,8 @@ Track :: Track() : 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) {} + +Library *Track :: library() { return _library; } diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 7f2a86fb..07a70068 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -39,11 +39,11 @@ enum sort_t { class Track : public GenericTag { private: + Library *_library; /**< Pointer to the Library containing this track. */ + void set_length_str(); public: - /** Pointer to the library containing this track */ - Library *library; /** Pointer to this track's artist information */ Artist *artist; /** Pointer to this track's album information */ @@ -86,6 +86,10 @@ public: /** Track destructor */ ~Track(); + + Library *library(); /**< @return Track::_library. */ + + /** * Called to access a track's primary key * @return The full path of the track diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index f71bf4d8..1cb6049b 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -9,7 +9,7 @@ static void test_track_tag() { Track track; - test_equal(track.library, (Library *)NULL); + test_equal(track.library(), (Library *)NULL); test_equal(track.artist, (Artist *)NULL); test_equal(track.album, (Album *)NULL); test_equal(track.genre, (Genre *)NULL);