/** * Copyright 2014 (c) Anna Schumaker. */ #include #include #include #include static Database track_db("track.db", false); Track :: Track() : GenericTag(), _album(NULL), _artist(NULL), _genre(NULL), _library(NULL), _count(0), _length(0), _track(0) {} Track :: Track(Album *album, Artist *artist, Genre *genre, Library *library, const std::string &filepath, const std::string &name, unsigned int length, unsigned int track) : GenericTag(name), _album(album), _artist(artist), _genre(genre), _library(library), _count(0), _length(length), _track(track), _path(filepath) { filter :: add(this->name(), index()); filter :: add(_artist->name(), index()); filter :: add(_album->name(), index()); _library->inc_size(); } Track :: Track(const Track &track) : GenericTag(track), _album(track._album), _artist(track._artist), _genre(track._genre), _library(track._library), _count(track._count), _length(track._length), _track(track._track), _date(track._date), _path(track._path) { _library->inc_size(); } Track :: ~Track() { if (_library) _library->dec_size(); } Album *Track :: album() { return _album; } Artist *Track :: artist() { return _artist; } Genre *Track :: genre() { return _genre; } Library *Track :: library() { return _library; } unsigned int Track :: count() { return _count; } unsigned int Track :: length() { return _length; } unsigned int Track :: track() { return _track; } const std::string Track :: date() const { struct tm tm; char buffer[20]; std::string res = "Never"; if (_count > 0) { tm.tm_mday = _date.day; tm.tm_mon = _date.month - 1; tm.tm_year = _date.year - 1900; strftime(buffer, 20, "%Ex", &tm); res = buffer; } return res; } const std::string Track :: path() const { if (_library) return _library->primary_key() + "/" + _path; return ""; } const std::string Track :: primary_key() const { if (_library) return string :: utos(_library->index()) + "/" + _path; return ""; } void Track :: played() { time_t rawtime = time(NULL); struct tm *now = localtime(&rawtime); _count++; _date.day = now->tm_mday; _date.month = now->tm_mon + 1; _date.year = now->tm_year + 1900; tags :: commit_track_db(); } int Track :: compare_date(const Track *rhs) { int ret = _date.year - rhs->_date.year; if (ret == 0) { ret = _date.month - rhs->_date.month; if (ret == 0) ret = _date.day - rhs->_date.day; } return ret; } void Track :: read(File &file) { unsigned int library_id, artist_id, album_id, genre_id; file >> library_id >> artist_id >> album_id >> genre_id; file >> _track >> _date.year >> _date.month >> _date.day; file >> _count >> _length; GenericTag :: read(file); _path = file.getline(); _library = tags :: get_library(library_id); _artist = tags :: get_artist(artist_id); _album = tags :: get_album(album_id); _genre = tags :: get_genre(genre_id); filter :: add(name(), index()); filter :: add(_artist->name(), index()); filter :: add(_album->name(), index()); _library->inc_size(); } void Track :: write(File &file) { file << _library->index() << " " << _artist->index() << " "; file << _album->index() << " " << _genre->index() << " " << _track << " "; file << _date.year << " " << _date.month << " " << _date.day << " "; file << _count << " " << _length << " "; GenericTag :: write(file); file << std::endl << _path << std::endl; } void tags :: init_track_db() { track_db.load(); } Track *tags :: add_track(Album *album, Artist *artist, Genre *genre, Library *library, const std::string &filepath, const std::string &name, unsigned int length, unsigned int track) { std::string path = filepath.substr(library->primary_key().size() + 1); return track_db.insert(Track(album, artist, genre, library, path, name, length, track)); } Track *tags :: get_track(const unsigned int index) { return track_db.at(index); } void tags :: remove_track(Track *track) { track_db.remove(track->index()); } void tags :: remove_library_tracks(Library *library) { Database::iterator it; for (it = track_db.begin(); it != track_db.end(); it = track_db.next(it)) { if ((*it)->library() == library) track_db.remove((*it)->index()); } tags :: commit_track_db(); } unsigned int tags :: track_size() { return track_db.actual_size(); } void tags :: commit_track_db() { track_db.save(); }