ocarina/core/tags/track.cpp

145 lines
3.3 KiB
C++
Raw Normal View History

/**
* @file
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/tags/track.h>
#include <sstream>
#include <stdlib.h>
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.substr(library->primary_key().size() + 1))
{
//filter :: add(name(), index());
//filter :: add(_artist->name(), index());
//filter :: add(_album->name(), index());
_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 :: length_str() const
{
std::stringstream ss;
unsigned int minutes = _length / 60;
unsigned int seconds = _length % 60;
ss << minutes << ":";
if (seconds < 10)
ss << 0;
ss << seconds;
return ss.str();
}
const std::string Track :: primary_key() const
{
std::stringstream ss;
if (_library)
ss << _library->index() << "/" << _path;
return ss.str();
}
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;
//tagdb :: commit();
}
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 &f)
{
unsigned int library_id, artist_id, album_id, genre_id;
f >> library_id >> artist_id >> album_id >> genre_id;
f >> _track >> _date.year >> _date.month >> _date.day;
f >> _count >> _length;
GenericTag :: read(f);
_path = f.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 &f)
{
f << _library->index() << " " << _artist->index() << " ";
f << _album->index() << " " << _genre->index() << " " << _track << " ";
f << _date.year << " " << _date.month << " " << _date.day << " ";
f << _count << " " << _length << " ";
GenericTag :: write(f);
f << std::endl << _path << std::endl;
}