1
0
mirror of https://github.com/clementine-player/Clementine synced 2025-01-31 03:27:40 +01:00

Use shared data for song

This commit is contained in:
David Sansome 2010-01-15 21:57:22 +00:00
parent 07ef49f380
commit e0bb24af07
2 changed files with 182 additions and 171 deletions

View File

@ -17,6 +17,8 @@
#include <QSqlQuery>
#include <QVariant>
#include "trackslider.h"
const char* Song::kColumnSpec =
"title, album, artist, albumartist, composer, "
"track, disc, bpm, year, genre, comment, compilation, "
@ -38,7 +40,7 @@ const char* Song::kUpdateSpec =
"directory = :directory_id, filename = :filename, mtime = :mtime, "
"ctime = :ctime, filesize = :filesize";
Song::Song()
SongData::SongData()
: valid_(false),
id_(-1),
track_(-1),
@ -56,9 +58,19 @@ Song::Song()
{
}
Song::Song()
: d(new SongData)
{
}
Song::Song(const Song &other)
: d(other.d)
{
}
void Song::InitFromFile(const QString& filename, int directory_id) {
filename_ = filename;
directory_id_ = directory_id;
d->filename_ = filename;
d->directory_id_ = directory_id;
TagLib::FileRef fileref = TagLib::FileRef(QFile::encodeName(filename).constData());
@ -66,23 +78,23 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
return;
QFileInfo info(filename);
filesize_ = info.size();
mtime_ = info.lastModified().toTime_t();
ctime_ = info.created().toTime_t();
d->filesize_ = info.size();
d->mtime_ = info.lastModified().toTime_t();
d->ctime_ = info.created().toTime_t();
TagLib::Tag* tag = fileref.tag();
if (tag) {
#define strip(x) TStringToQString( x ).trimmed()
title_ = strip(tag->title());
artist_ = strip(tag->artist());
album_ = strip(tag->album());
comment_ = strip(tag->comment());
genre_ = strip(tag->genre());
year_ = tag->year();
track_ = tag->track();
d->title_ = strip(tag->title());
d->artist_ = strip(tag->artist());
d->album_ = strip(tag->album());
d->comment_ = strip(tag->comment());
d->genre_ = strip(tag->genre());
d->year_ = tag->year();
d->track_ = tag->track();
#undef strip
valid_ = true;
d->valid_ = true;
}
QString disc;
@ -93,13 +105,13 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
disc = TStringToQString(file->ID3v2Tag()->frameListMap()["TPOS"].front()->toString()).trimmed();
if (!file->ID3v2Tag()->frameListMap()["TBPM"].isEmpty())
bpm_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TBPM"].front()->toString()).trimmed().toFloat();
d->bpm_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TBPM"].front()->toString()).trimmed().toFloat();
if (!file->ID3v2Tag()->frameListMap()["TCOM"].isEmpty())
composer_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TCOM"].front()->toString()).trimmed();
d->composer_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TCOM"].front()->toString()).trimmed();
if (!file->ID3v2Tag()->frameListMap()["TPE2"].isEmpty()) // non-standard: Apple, Microsoft
albumartist_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TPE2"].front()->toString()).trimmed();
d->albumartist_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TPE2"].front()->toString()).trimmed();
if (!file->ID3v2Tag()->frameListMap()["TCMP"].isEmpty())
compilation = TStringToQString(file->ID3v2Tag()->frameListMap()["TCMP"].front()->toString()).trimmed();
@ -108,10 +120,10 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
else if (TagLib::Ogg::Vorbis::File* file = dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref.file())) {
if (file->tag()) {
if ( !file->tag()->fieldListMap()["COMPOSER"].isEmpty() )
composer_ = TStringToQString(file->tag()->fieldListMap()["COMPOSER"].front()).trimmed();
d->composer_ = TStringToQString(file->tag()->fieldListMap()["COMPOSER"].front()).trimmed();
if ( !file->tag()->fieldListMap()["BPM"].isEmpty() )
bpm_ = TStringToQString(file->tag()->fieldListMap()["BPM"].front()).trimmed().toFloat();
d->bpm_ = TStringToQString(file->tag()->fieldListMap()["BPM"].front()).trimmed().toFloat();
if ( !file->tag()->fieldListMap()["DISCNUMBER"].isEmpty() )
disc = TStringToQString(file->tag()->fieldListMap()["DISCNUMBER"].front()).trimmed();
@ -123,10 +135,10 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
else if (TagLib::FLAC::File* file = dynamic_cast<TagLib::FLAC::File*>(fileref.file())) {
if ( file->xiphComment() ) {
if (!file->xiphComment()->fieldListMap()["COMPOSER"].isEmpty())
composer_ = TStringToQString( file->xiphComment()->fieldListMap()["COMPOSER"].front() ).trimmed();
d->composer_ = TStringToQString( file->xiphComment()->fieldListMap()["COMPOSER"].front() ).trimmed();
if (!file->xiphComment()->fieldListMap()["BPM"].isEmpty() )
bpm_ = TStringToQString( file->xiphComment()->fieldListMap()["BPM"].front() ).trimmed().toFloat();
d->bpm_ = TStringToQString( file->xiphComment()->fieldListMap()["BPM"].front() ).trimmed().toFloat();
if (!file->xiphComment()->fieldListMap()["DISCNUMBER"].isEmpty() )
disc = TStringToQString( file->xiphComment()->fieldListMap()["DISCNUMBER"].front() ).trimmed();
@ -140,55 +152,55 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
int i = disc.indexOf('/');
if ( i != -1 )
// disc.right( i ).toInt() is total number of discs, we don't use this at the moment
disc_ = disc.left( i ).toInt();
d->disc_ = disc.left( i ).toInt();
else
disc_ = disc.toInt();
d->disc_ = disc.toInt();
}
if ( compilation.isEmpty() ) {
// well, it wasn't set, but if the artist is VA assume it's a compilation
if ( artist_.toLower() == "various artists" )
compilation_ = true;
if ( d->artist_.toLower() == "various artists" )
d->compilation_ = true;
} else {
int i = compilation.toInt();
compilation_ = (i == 1);
d->compilation_ = (i == 1);
}
bitrate_ = fileref.audioProperties()->bitrate();
length_ = fileref.audioProperties()->length();
samplerate_ = fileref.audioProperties()->sampleRate();
d->bitrate_ = fileref.audioProperties()->bitrate();
d->length_ = fileref.audioProperties()->length();
d->samplerate_ = fileref.audioProperties()->sampleRate();
}
void Song::InitFromQuery(const QSqlQuery& q) {
valid_ = true;
d->valid_ = true;
#define tostr(n) (q.value(n).isNull() ? QString::null : q.value(n).toString())
#define toint(n) (q.value(n).isNull() ? -1 : q.value(n).toInt())
#define tofloat(n) (q.value(n).isNull() ? -1 : q.value(n).toDouble())
id_ = toint(0);
title_ = tostr(1);
album_ = tostr(2);
artist_ = tostr(3);
albumartist_ = tostr(4);
composer_ = tostr(5);
track_ = toint(6);
disc_ = toint(7);
bpm_ = tofloat(8);
year_ = toint(9);
genre_ = tostr(10);
comment_ = tostr(11);
compilation_ = q.value(12).toBool();
d->id_ = toint(0);
d->title_ = tostr(1);
d->album_ = tostr(2);
d->artist_ = tostr(3);
d->albumartist_ = tostr(4);
d->composer_ = tostr(5);
d->track_ = toint(6);
d->disc_ = toint(7);
d->bpm_ = tofloat(8);
d->year_ = toint(9);
d->genre_ = tostr(10);
d->comment_ = tostr(11);
d->compilation_ = q.value(12).toBool();
length_ = toint(13);
bitrate_ = toint(14);
samplerate_ = toint(15);
d->length_ = toint(13);
d->bitrate_ = toint(14);
d->samplerate_ = toint(15);
directory_id_ = toint(16);
filename_ = tostr(17);
mtime_ = toint(18);
ctime_ = toint(19);
filesize_ = toint(20);
d->directory_id_ = toint(16);
d->filename_ = tostr(17);
d->mtime_ = toint(18);
d->ctime_ = toint(19);
d->filesize_ = toint(20);
#undef tostr
#undef toint
@ -196,40 +208,40 @@ void Song::InitFromQuery(const QSqlQuery& q) {
}
void Song::InitFromLastFM(const lastfm::Track& track) {
valid_ = true;
d->valid_ = true;
title_ = track.title();
album_ = track.album();
artist_ = track.artist();
track_ = track.trackNumber();
length_ = track.duration();
d->title_ = track.title();
d->album_ = track.album();
d->artist_ = track.artist();
d->track_ = track.trackNumber();
d->length_ = track.duration();
}
void Song::BindToQuery(QSqlQuery *query) const {
#define intval(x) (x == -1 ? QVariant() : x)
query->bindValue(":title", title_);
query->bindValue(":album", album_);
query->bindValue(":artist", artist_);
query->bindValue(":albumartist", albumartist_);
query->bindValue(":composer", composer_);
query->bindValue(":track", intval(track_));
query->bindValue(":disc", intval(disc_));
query->bindValue(":bpm", intval(bpm_));
query->bindValue(":year", intval(year_));
query->bindValue(":genre", genre_);
query->bindValue(":comment", comment_);
query->bindValue(":compilation", compilation_ ? 1 : 0);
query->bindValue(":title", d->title_);
query->bindValue(":album", d->album_);
query->bindValue(":artist", d->artist_);
query->bindValue(":albumartist", d->albumartist_);
query->bindValue(":composer", d->composer_);
query->bindValue(":track", intval(d->track_));
query->bindValue(":disc", intval(d->disc_));
query->bindValue(":bpm", intval(d->bpm_));
query->bindValue(":year", intval(d->year_));
query->bindValue(":genre", d->genre_);
query->bindValue(":comment", d->comment_);
query->bindValue(":compilation", d->compilation_ ? 1 : 0);
query->bindValue(":length", intval(length_));
query->bindValue(":bitrate", intval(bitrate_));
query->bindValue(":samplerate", intval(samplerate_));
query->bindValue(":length", intval(d->length_));
query->bindValue(":bitrate", intval(d->bitrate_));
query->bindValue(":samplerate", intval(d->samplerate_));
query->bindValue(":directory_id", intval(directory_id_));
query->bindValue(":filename", filename_);
query->bindValue(":mtime", intval(mtime_));
query->bindValue(":ctime", intval(ctime_));
query->bindValue(":filesize", intval(filesize_));
query->bindValue(":directory_id", intval(d->directory_id_));
query->bindValue(":filename", d->filename_);
query->bindValue(":mtime", intval(d->mtime_));
query->bindValue(":ctime", intval(d->ctime_));
query->bindValue(":filesize", intval(d->filesize_));
#undef intval
}
@ -237,64 +249,55 @@ void Song::BindToQuery(QSqlQuery *query) const {
void Song::ToLastFM(lastfm::Track* track) const {
lastfm::MutableTrack mtrack(*track);
mtrack.setArtist(artist_);
mtrack.setAlbum(album_);
mtrack.setTitle(title_);
mtrack.setDuration(length_);
mtrack.setTrackNumber(track_);
mtrack.setArtist(d->artist_);
mtrack.setAlbum(d->album_);
mtrack.setTitle(d->title_);
mtrack.setDuration(d->length_);
mtrack.setTrackNumber(d->track_);
}
QString Song::PrettyTitleWithArtist() const {
QString title(title_);
QString title(d->title_);
if (title.isEmpty())
title = QFileInfo(filename_).baseName();
title = QFileInfo(d->filename_).baseName();
if (compilation_ && !artist_.isEmpty())
title = artist_ + " - " + title;
if (d->compilation_ && !d->artist_.isEmpty())
title = d->artist_ + " - " + title;
return title;
}
QString Song::PrettyTitle() const {
QString title(title_);
QString title(d->title_);
if (title.isEmpty())
title = QFileInfo(filename_).baseName();
title = QFileInfo(d->filename_).baseName();
return title;
}
QString Song::PrettyLength() const {
if (length_ == -1)
if (d->length_ == -1)
return QString::null;
int hours = length_ / (60*60);
int minutes = (length_ / 60) % 60;
int seconds = length_ % 60;
QString text;
if (hours)
text.sprintf("%d:%02d:%02d", hours, minutes, seconds);
else
text.sprintf("%d:%02d", minutes, seconds);
return text;
return TrackSlider::PrettyTime(d->length_);
}
bool Song::IsMetadataEqual(const Song& other) const {
return title_ == other.title_ &&
album_ == other.album_ &&
artist_ == other.artist_ &&
albumartist_ == other.albumartist_ &&
composer_ == other.composer_ &&
track_ == other.track_ &&
disc_ == other.disc_ &&
qFuzzyCompare(bpm_, other.bpm_) &&
year_ == other.year_ &&
genre_ == other.genre_ &&
comment_ == other.comment_ &&
compilation_ == other.compilation_ &&
length_ == other.length_ &&
bitrate_ == other.bitrate_ &&
samplerate_ == other.samplerate_;
return d->title_ == other.d->title_ &&
d->album_ == other.d->album_ &&
d->artist_ == other.d->artist_ &&
d->albumartist_ == other.d->albumartist_ &&
d->composer_ == other.d->composer_ &&
d->track_ == other.d->track_ &&
d->disc_ == other.d->disc_ &&
qFuzzyCompare(d->bpm_, other.d->bpm_) &&
d->year_ == other.d->year_ &&
d->genre_ == other.d->genre_ &&
d->comment_ == other.d->comment_ &&
d->compilation_ == other.d->compilation_ &&
d->length_ == other.d->length_ &&
d->bitrate_ == other.d->bitrate_ &&
d->samplerate_ == other.d->samplerate_;
}

View File

@ -4,69 +4,16 @@
#include <QString>
#include <QList>
#include <QSqlQuery>
#include <QSharedData>
#include <QSharedDataPointer>
namespace lastfm {
class Track;
}
// TODO: QSharedData
class Song {
public:
Song();
struct SongData : public QSharedData {
SongData();
static const char* kColumnSpec;
static const char* kBindSpec;
static const char* kUpdateSpec;
// Constructors
void InitFromFile(const QString& filename, int directory_id);
void InitFromQuery(const QSqlQuery& query);
void InitFromLastFM(const lastfm::Track& track);
// Save
void BindToQuery(QSqlQuery* query) const;
void ToLastFM(lastfm::Track* track) const;
// Simple accessors
bool is_valid() const { return valid_; }
int id() const { return id_; }
QString title() const { return title_; }
QString album() const { return album_; }
QString artist() const { return artist_; }
QString albumartist() const { return albumartist_; }
QString composer() const { return composer_; }
int track() const { return track_; }
int disc() const { return disc_; }
float bpm() const { return bpm_; }
int year() const { return year_; }
QString genre() const { return genre_; }
QString comment() const { return comment_; }
bool is_compilation() const { return compilation_; }
int length() const { return length_; }
int bitrate() const { return bitrate_; }
int samplerate() const { return samplerate_; }
int directory_id() const { return directory_id_; }
QString filename() const { return filename_; }
uint mtime() const { return mtime_; }
uint ctime() const { return ctime_; }
int filesize() const { return filesize_; }
// Pretty accessors
QString PrettyTitle() const;
QString PrettyTitleWithArtist() const;
QString PrettyLength() const;
// Setters
void set_id(int id) { id_ = id; }
void set_title(const QString& title) { title_ = title; }
// Comparison functions
bool IsMetadataEqual(const Song& other) const;
private:
bool valid_;
int id_;
@ -94,6 +41,67 @@ class Song {
int filesize_;
};
class Song {
public:
Song();
Song(const Song& other);
static const char* kColumnSpec;
static const char* kBindSpec;
static const char* kUpdateSpec;
// Constructors
void InitFromFile(const QString& filename, int directory_id);
void InitFromQuery(const QSqlQuery& query);
void InitFromLastFM(const lastfm::Track& track);
// Save
void BindToQuery(QSqlQuery* query) const;
void ToLastFM(lastfm::Track* track) const;
// Simple accessors
bool is_valid() const { return d->valid_; }
int id() const { return d->id_; }
QString title() const { return d->title_; }
QString album() const { return d->album_; }
QString artist() const { return d->artist_; }
QString albumartist() const { return d->albumartist_; }
QString composer() const { return d->composer_; }
int track() const { return d->track_; }
int disc() const { return d->disc_; }
float bpm() const { return d->bpm_; }
int year() const { return d->year_; }
QString genre() const { return d->genre_; }
QString comment() const { return d->comment_; }
bool is_compilation() const { return d->compilation_; }
int length() const { return d->length_; }
int bitrate() const { return d->bitrate_; }
int samplerate() const { return d->samplerate_; }
int directory_id() const { return d->directory_id_; }
QString filename() const { return d->filename_; }
uint mtime() const { return d->mtime_; }
uint ctime() const { return d->ctime_; }
int filesize() const { return d->filesize_; }
// Pretty accessors
QString PrettyTitle() const;
QString PrettyTitleWithArtist() const;
QString PrettyLength() const;
// Setters
void set_id(int id) { d->id_ = id; }
void set_title(const QString& title) { d->title_ = title; }
// Comparison functions
bool IsMetadataEqual(const Song& other) const;
private:
QSharedDataPointer<SongData> d;
};
typedef QList<Song> SongList;
#endif // SONG_H