2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#include "song.h"
|
|
|
|
|
2010-01-16 18:17:00 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#include <taglib/fileref.h>
|
|
|
|
#include <taglib/tag.h>
|
|
|
|
#include <taglib/tstring.h>
|
|
|
|
#include <taglib/mpegfile.h>
|
|
|
|
#include <taglib/id3v2tag.h>
|
|
|
|
#include <taglib/oggfile.h>
|
2010-03-07 23:46:41 +01:00
|
|
|
#include <taglib/oggflacfile.h>
|
2009-12-24 20:16:07 +01:00
|
|
|
#include <taglib/vorbisfile.h>
|
|
|
|
#include <taglib/flacfile.h>
|
2010-03-07 23:46:41 +01:00
|
|
|
#include <taglib/asffile.h>
|
|
|
|
#include <taglib/mp4file.h>
|
|
|
|
#include <taglib/mpcfile.h>
|
|
|
|
#include <taglib/aifffile.h>
|
|
|
|
#include <taglib/wavfile.h>
|
|
|
|
#include <taglib/speexfile.h>
|
|
|
|
#include <taglib/trueaudiofile.h>
|
2010-03-29 15:15:47 +02:00
|
|
|
#include <taglib/textidentificationframe.h>
|
|
|
|
#include <taglib/xiphcomment.h>
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-23 15:07:05 +01:00
|
|
|
#include "fixlastfm.h"
|
2009-12-26 23:15:57 +01:00
|
|
|
#include <lastfm/Track>
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QTime>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QVariant>
|
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
using boost::scoped_ptr;
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
#include "trackslider.h"
|
2010-04-04 19:50:11 +02:00
|
|
|
#include "engines/enginebase.h"
|
2010-03-03 15:29:53 +01:00
|
|
|
#include "albumcoverloader.h"
|
2010-01-15 22:57:22 +01:00
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
const char* Song::kColumnSpec =
|
|
|
|
"title, album, artist, albumartist, composer, "
|
|
|
|
"track, disc, bpm, year, genre, comment, compilation, "
|
|
|
|
"length, bitrate, samplerate, directory, filename, "
|
2010-03-07 23:46:41 +01:00
|
|
|
"mtime, ctime, filesize, sampler, art_automatic, art_manual, "
|
2010-03-21 00:59:39 +01:00
|
|
|
"filetype, playcount, lastplayed, rating, forced_compilation_on, "
|
2010-03-21 22:17:01 +01:00
|
|
|
"forced_compilation_off, effective_compilation";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
const char* Song::kBindSpec =
|
|
|
|
":title, :album, :artist, :albumartist, :composer, "
|
|
|
|
":track, :disc, :bpm, :year, :genre, :comment, :compilation, "
|
|
|
|
":length, :bitrate, :samplerate, :directory_id, :filename, "
|
2010-03-07 23:46:41 +01:00
|
|
|
":mtime, :ctime, :filesize, :sampler, :art_automatic, :art_manual, "
|
2010-03-21 00:59:39 +01:00
|
|
|
":filetype, :playcount, :lastplayed, :rating, :forced_compilation_on, "
|
2010-03-21 22:17:01 +01:00
|
|
|
":forced_compilation_off, :effective_compilation";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
const char* Song::kUpdateSpec =
|
|
|
|
"title = :title, album = :album, artist = :artist, "
|
|
|
|
"albumartist = :albumartist, composer = :composer, track = :track, "
|
|
|
|
"disc = :disc, bpm = :bpm, year = :year, genre = :genre, "
|
|
|
|
"comment = :comment, compilation = :compilation, length = :length, "
|
|
|
|
"bitrate = :bitrate, samplerate = :samplerate, "
|
|
|
|
"directory = :directory_id, filename = :filename, mtime = :mtime, "
|
2010-02-28 01:35:20 +01:00
|
|
|
"ctime = :ctime, filesize = :filesize, sampler = :sampler, "
|
2010-03-07 23:46:41 +01:00
|
|
|
"art_automatic = :art_automatic, art_manual = :art_manual, "
|
|
|
|
"filetype = :filetype, playcount = :playcount, lastplayed = :lastplayed, "
|
2010-03-21 00:59:39 +01:00
|
|
|
"rating = :rating, forced_compilation_on = :forced_compilation_on, "
|
2010-03-21 22:17:01 +01:00
|
|
|
"forced_compilation_off = :forced_compilation_off, "
|
|
|
|
"effective_compilation = :effective_compilation";
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-29 15:15:47 +02:00
|
|
|
static TagLib::String QStringToTaglibString(const QString& s);
|
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
TagLibFileRefFactory Song::kDefaultFactory;
|
|
|
|
|
2010-03-07 23:46:41 +01:00
|
|
|
Song::Private::Private()
|
2009-12-24 20:16:07 +01:00
|
|
|
: valid_(false),
|
|
|
|
id_(-1),
|
|
|
|
track_(-1),
|
|
|
|
disc_(-1),
|
|
|
|
bpm_(-1),
|
|
|
|
year_(-1),
|
|
|
|
compilation_(false),
|
2010-02-27 21:12:22 +01:00
|
|
|
sampler_(false),
|
2010-03-21 00:59:39 +01:00
|
|
|
forced_compilation_on_(false),
|
|
|
|
forced_compilation_off_(false),
|
2009-12-24 20:16:07 +01:00
|
|
|
length_(-1),
|
|
|
|
bitrate_(-1),
|
|
|
|
samplerate_(-1),
|
|
|
|
directory_id_(-1),
|
|
|
|
mtime_(-1),
|
|
|
|
ctime_(-1),
|
2010-03-07 23:46:41 +01:00
|
|
|
filesize_(-1),
|
|
|
|
filetype_(Type_Unknown)
|
2009-12-24 20:16:07 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
TagLib::FileRef* TagLibFileRefFactory::GetFileRef(const QString& filename) {
|
|
|
|
return new TagLib::FileRef(QFile::encodeName(filename).constData());
|
|
|
|
}
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
Song::Song()
|
2010-03-07 23:46:41 +01:00
|
|
|
: d(new Private),
|
2010-03-06 21:08:01 +01:00
|
|
|
factory_(&kDefaultFactory)
|
2010-01-15 22:57:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Song::Song(const Song &other)
|
2010-03-06 21:08:01 +01:00
|
|
|
: d(other.d),
|
|
|
|
factory_(&kDefaultFactory)
|
2010-01-15 22:57:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
Song::Song(FileRefFactory* factory)
|
2010-03-07 23:46:41 +01:00
|
|
|
: d(new Private),
|
2010-03-06 21:08:01 +01:00
|
|
|
factory_(factory) {
|
|
|
|
}
|
|
|
|
|
2010-03-09 18:17:32 +01:00
|
|
|
void Song::Init(const QString& title, const QString& artist, const QString& album, int length) {
|
2010-03-07 16:26:54 +01:00
|
|
|
d->valid_ = true;
|
|
|
|
d->title_ = title;
|
|
|
|
d->artist_ = artist;
|
2010-03-09 18:17:32 +01:00
|
|
|
d->album_ = album;
|
2010-03-07 16:26:54 +01:00
|
|
|
d->length_ = length;
|
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void Song::InitFromFile(const QString& filename, int directory_id) {
|
2010-01-15 22:57:22 +01:00
|
|
|
d->filename_ = filename;
|
|
|
|
d->directory_id_ = directory_id;
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
scoped_ptr<TagLib::FileRef> fileref(factory_->GetFileRef(filename));
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
if(fileref->isNull())
|
2009-12-24 20:16:07 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
QFileInfo info(filename);
|
2010-03-19 11:39:22 +01:00
|
|
|
d->basefilename_ = info.fileName();
|
2010-01-15 22:57:22 +01:00
|
|
|
d->filesize_ = info.size();
|
|
|
|
d->mtime_ = info.lastModified().toTime_t();
|
|
|
|
d->ctime_ = info.created().toTime_t();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
TagLib::Tag* tag = fileref->tag();
|
2009-12-24 20:16:07 +01:00
|
|
|
if (tag) {
|
|
|
|
#define strip(x) TStringToQString( x ).trimmed()
|
2010-01-15 22:57:22 +01:00
|
|
|
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();
|
2009-12-24 20:16:07 +01:00
|
|
|
#undef strip
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
d->valid_ = true;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString disc;
|
|
|
|
QString compilation;
|
2010-03-06 21:08:01 +01:00
|
|
|
if (TagLib::MPEG::File* file = dynamic_cast<TagLib::MPEG::File*>(fileref->file())) {
|
2009-12-24 20:16:07 +01:00
|
|
|
if (file->ID3v2Tag()) {
|
|
|
|
if (!file->ID3v2Tag()->frameListMap()["TPOS"].isEmpty())
|
|
|
|
disc = TStringToQString(file->ID3v2Tag()->frameListMap()["TPOS"].front()->toString()).trimmed();
|
|
|
|
|
|
|
|
if (!file->ID3v2Tag()->frameListMap()["TBPM"].isEmpty())
|
2010-01-15 22:57:22 +01:00
|
|
|
d->bpm_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TBPM"].front()->toString()).trimmed().toFloat();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!file->ID3v2Tag()->frameListMap()["TCOM"].isEmpty())
|
2010-01-15 22:57:22 +01:00
|
|
|
d->composer_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TCOM"].front()->toString()).trimmed();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!file->ID3v2Tag()->frameListMap()["TPE2"].isEmpty()) // non-standard: Apple, Microsoft
|
2010-01-15 22:57:22 +01:00
|
|
|
d->albumartist_ = TStringToQString(file->ID3v2Tag()->frameListMap()["TPE2"].front()->toString()).trimmed();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!file->ID3v2Tag()->frameListMap()["TCMP"].isEmpty())
|
|
|
|
compilation = TStringToQString(file->ID3v2Tag()->frameListMap()["TCMP"].front()->toString()).trimmed();
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 21:08:01 +01:00
|
|
|
else if (TagLib::Ogg::Vorbis::File* file = dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref->file())) {
|
2009-12-24 20:16:07 +01:00
|
|
|
if (file->tag()) {
|
|
|
|
if ( !file->tag()->fieldListMap()["COMPOSER"].isEmpty() )
|
2010-01-15 22:57:22 +01:00
|
|
|
d->composer_ = TStringToQString(file->tag()->fieldListMap()["COMPOSER"].front()).trimmed();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if ( !file->tag()->fieldListMap()["BPM"].isEmpty() )
|
2010-01-15 22:57:22 +01:00
|
|
|
d->bpm_ = TStringToQString(file->tag()->fieldListMap()["BPM"].front()).trimmed().toFloat();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if ( !file->tag()->fieldListMap()["DISCNUMBER"].isEmpty() )
|
|
|
|
disc = TStringToQString(file->tag()->fieldListMap()["DISCNUMBER"].front()).trimmed();
|
|
|
|
|
|
|
|
if ( !file->tag()->fieldListMap()["COMPILATION"].isEmpty() )
|
|
|
|
compilation = TStringToQString(file->tag()->fieldListMap()["COMPILATION"].front()).trimmed();
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 21:08:01 +01:00
|
|
|
else if (TagLib::FLAC::File* file = dynamic_cast<TagLib::FLAC::File*>(fileref->file())) {
|
2009-12-24 20:16:07 +01:00
|
|
|
if ( file->xiphComment() ) {
|
|
|
|
if (!file->xiphComment()->fieldListMap()["COMPOSER"].isEmpty())
|
2010-01-15 22:57:22 +01:00
|
|
|
d->composer_ = TStringToQString( file->xiphComment()->fieldListMap()["COMPOSER"].front() ).trimmed();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!file->xiphComment()->fieldListMap()["BPM"].isEmpty() )
|
2010-01-15 22:57:22 +01:00
|
|
|
d->bpm_ = TStringToQString( file->xiphComment()->fieldListMap()["BPM"].front() ).trimmed().toFloat();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (!file->xiphComment()->fieldListMap()["DISCNUMBER"].isEmpty() )
|
|
|
|
disc = TStringToQString( file->xiphComment()->fieldListMap()["DISCNUMBER"].front() ).trimmed();
|
|
|
|
|
|
|
|
if (!file->xiphComment()->fieldListMap()["COMPILATION"].isEmpty() )
|
|
|
|
compilation = TStringToQString( file->xiphComment()->fieldListMap()["COMPILATION"].front() ).trimmed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !disc.isEmpty() ) {
|
|
|
|
int i = disc.indexOf('/');
|
|
|
|
if ( i != -1 )
|
|
|
|
// disc.right( i ).toInt() is total number of discs, we don't use this at the moment
|
2010-01-15 22:57:22 +01:00
|
|
|
d->disc_ = disc.left( i ).toInt();
|
2009-12-24 20:16:07 +01:00
|
|
|
else
|
2010-01-15 22:57:22 +01:00
|
|
|
d->disc_ = disc.toInt();
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( compilation.isEmpty() ) {
|
|
|
|
// well, it wasn't set, but if the artist is VA assume it's a compilation
|
2010-01-15 22:57:22 +01:00
|
|
|
if ( d->artist_.toLower() == "various artists" )
|
|
|
|
d->compilation_ = true;
|
2009-12-24 20:16:07 +01:00
|
|
|
} else {
|
|
|
|
int i = compilation.toInt();
|
2010-01-15 22:57:22 +01:00
|
|
|
d->compilation_ = (i == 1);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
2010-03-06 21:08:01 +01:00
|
|
|
if (fileref->audioProperties()) {
|
|
|
|
d->bitrate_ = fileref->audioProperties()->bitrate();
|
|
|
|
d->length_ = fileref->audioProperties()->length();
|
|
|
|
d->samplerate_ = fileref->audioProperties()->sampleRate();
|
2010-01-17 20:03:54 +01:00
|
|
|
}
|
2010-03-07 23:46:41 +01:00
|
|
|
|
|
|
|
// Get the filetype if we can
|
|
|
|
GuessFileType(fileref.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Song::GuessFileType(TagLib::FileRef* fileref) {
|
2010-03-16 16:09:08 +01:00
|
|
|
#ifdef TAGLIB_WITH_ASF
|
2010-03-07 23:46:41 +01:00
|
|
|
if (dynamic_cast<TagLib::ASF::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Asf;
|
2010-03-16 16:09:08 +01:00
|
|
|
#endif
|
2010-03-07 23:46:41 +01:00
|
|
|
if (dynamic_cast<TagLib::FLAC::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Flac;
|
2010-03-16 16:09:08 +01:00
|
|
|
#ifdef TAGLIB_WITH_MP4
|
2010-03-07 23:46:41 +01:00
|
|
|
if (dynamic_cast<TagLib::MP4::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Mp4;
|
2010-03-16 16:09:08 +01:00
|
|
|
#endif
|
2010-03-07 23:46:41 +01:00
|
|
|
if (dynamic_cast<TagLib::MPC::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Mpc;
|
|
|
|
if (dynamic_cast<TagLib::MPEG::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Mpeg;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::FLAC::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_OggFlac;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::Speex::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_OggSpeex;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_OggVorbis;
|
|
|
|
if (dynamic_cast<TagLib::RIFF::AIFF::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Aiff;
|
|
|
|
if (dynamic_cast<TagLib::RIFF::WAV::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_Wav;
|
|
|
|
if (dynamic_cast<TagLib::TrueAudio::File*>(fileref->file()))
|
|
|
|
d->filetype_ = Type_TrueAudio;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Song::InitFromQuery(const QSqlQuery& q) {
|
2010-03-03 19:38:20 +01:00
|
|
|
if (!q.isValid())
|
|
|
|
return;
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
d->valid_ = true;
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
#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())
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
d->length_ = toint(13);
|
|
|
|
d->bitrate_ = toint(14);
|
|
|
|
d->samplerate_ = toint(15);
|
|
|
|
|
|
|
|
d->directory_id_ = toint(16);
|
|
|
|
d->filename_ = tostr(17);
|
2010-03-19 11:39:22 +01:00
|
|
|
d->basefilename_ = QFileInfo(d->filename_).fileName();
|
2010-01-15 22:57:22 +01:00
|
|
|
d->mtime_ = toint(18);
|
|
|
|
d->ctime_ = toint(19);
|
|
|
|
d->filesize_ = toint(20);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-02-27 21:12:22 +01:00
|
|
|
d->sampler_ = q.value(21).toBool();
|
|
|
|
|
2010-02-28 01:35:20 +01:00
|
|
|
d->art_automatic_ = q.value(22).toString();
|
|
|
|
d->art_manual_ = q.value(23).toString();
|
|
|
|
|
2010-03-07 23:46:41 +01:00
|
|
|
d->filetype_ = FileType(q.value(24).toInt());
|
|
|
|
// playcount = 25
|
|
|
|
// lastplayed = 26
|
|
|
|
// rating = 27
|
|
|
|
|
2010-03-21 00:59:39 +01:00
|
|
|
d->forced_compilation_on_ = q.value(28).toBool();
|
|
|
|
d->forced_compilation_off_ = q.value(29).toBool();
|
|
|
|
|
2010-03-21 22:17:01 +01:00
|
|
|
// effective_compilation = 30
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#undef tostr
|
|
|
|
#undef toint
|
|
|
|
#undef tofloat
|
|
|
|
}
|
|
|
|
|
2009-12-26 23:15:57 +01:00
|
|
|
void Song::InitFromLastFM(const lastfm::Track& track) {
|
2010-01-15 22:57:22 +01:00
|
|
|
d->valid_ = true;
|
2010-03-07 23:46:41 +01:00
|
|
|
d->filetype_ = Type_Stream;
|
2009-12-26 23:15:57 +01:00
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
d->title_ = track.title();
|
|
|
|
d->album_ = track.album();
|
|
|
|
d->artist_ = track.artist();
|
|
|
|
d->track_ = track.trackNumber();
|
|
|
|
d->length_ = track.duration();
|
2009-12-26 23:15:57 +01:00
|
|
|
}
|
|
|
|
|
2010-02-03 23:20:31 +01:00
|
|
|
void Song::InitFromSimpleMetaBundle(const Engine::SimpleMetaBundle &bundle) {
|
|
|
|
d->valid_ = true;
|
|
|
|
|
|
|
|
d->title_ = bundle.title;
|
|
|
|
d->artist_ = bundle.artist;
|
|
|
|
d->album_ = bundle.album;
|
|
|
|
d->comment_ = bundle.comment;
|
|
|
|
d->genre_ = bundle.genre;
|
|
|
|
d->bitrate_ = bundle.bitrate.toInt();
|
|
|
|
d->samplerate_ = bundle.samplerate.toInt();
|
|
|
|
d->length_ = bundle.length.toInt();
|
|
|
|
d->year_ = bundle.year.toInt();
|
|
|
|
d->track_ = bundle.tracknr.toInt();
|
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
void Song::BindToQuery(QSqlQuery *query) const {
|
|
|
|
#define intval(x) (x == -1 ? QVariant() : x)
|
|
|
|
|
2010-02-28 01:35:20 +01:00
|
|
|
// Remember to bind these in the same order as kBindSpec
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
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(d->length_));
|
|
|
|
query->bindValue(":bitrate", intval(d->bitrate_));
|
|
|
|
query->bindValue(":samplerate", intval(d->samplerate_));
|
|
|
|
|
|
|
|
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_));
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-02-27 21:12:22 +01:00
|
|
|
query->bindValue(":sampler", d->sampler_ ? 1 : 0);
|
2010-02-28 01:35:20 +01:00
|
|
|
query->bindValue(":art_automatic", d->art_automatic_);
|
|
|
|
query->bindValue(":art_manual", d->art_manual_);
|
2010-02-27 21:12:22 +01:00
|
|
|
|
2010-03-07 23:46:41 +01:00
|
|
|
query->bindValue(":filetype", d->filetype_);
|
|
|
|
query->bindValue(":playcount", 0); // TODO
|
|
|
|
query->bindValue(":lastplayed", -1); // TODO
|
|
|
|
query->bindValue(":rating", -1);
|
|
|
|
|
2010-03-21 14:50:00 +01:00
|
|
|
query->bindValue(":forced_compilation_on", d->forced_compilation_on_ ? 1 : 0);
|
|
|
|
query->bindValue(":forced_compilation_off", d->forced_compilation_off_ ? 1 : 0);
|
2010-03-21 00:59:39 +01:00
|
|
|
|
2010-03-21 22:17:01 +01:00
|
|
|
query->bindValue(":effective_compilation", is_compilation() ? 1 : 0);
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
#undef intval
|
|
|
|
}
|
|
|
|
|
2009-12-29 20:22:02 +01:00
|
|
|
void Song::ToLastFM(lastfm::Track* track) const {
|
|
|
|
lastfm::MutableTrack mtrack(*track);
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
mtrack.setArtist(d->artist_);
|
|
|
|
mtrack.setAlbum(d->album_);
|
|
|
|
mtrack.setTitle(d->title_);
|
|
|
|
mtrack.setDuration(d->length_);
|
|
|
|
mtrack.setTrackNumber(d->track_);
|
2010-02-22 22:26:49 +01:00
|
|
|
mtrack.setSource(lastfm::Track::Player);
|
2009-12-29 20:22:02 +01:00
|
|
|
}
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
QString Song::PrettyTitleWithArtist() const {
|
2010-01-15 22:57:22 +01:00
|
|
|
QString title(d->title_);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (title.isEmpty())
|
2010-01-15 22:57:22 +01:00
|
|
|
title = QFileInfo(d->filename_).baseName();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2010-03-24 20:05:49 +01:00
|
|
|
if (is_compilation() && !d->artist_.isEmpty() && !d->artist_.toLower().contains("various"))
|
2010-01-15 22:57:22 +01:00
|
|
|
title = d->artist_ + " - " + title;
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Song::PrettyTitle() const {
|
2010-01-15 22:57:22 +01:00
|
|
|
QString title(d->title_);
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
if (title.isEmpty())
|
2010-01-15 22:57:22 +01:00
|
|
|
title = QFileInfo(d->filename_).baseName();
|
2009-12-24 20:16:07 +01:00
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Song::PrettyLength() const {
|
2010-01-15 22:57:22 +01:00
|
|
|
if (d->length_ == -1)
|
2009-12-24 20:16:07 +01:00
|
|
|
return QString::null;
|
|
|
|
|
2010-01-15 22:57:22 +01:00
|
|
|
return TrackSlider::PrettyTime(d->length_);
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Song::IsMetadataEqual(const Song& other) const {
|
2010-01-15 22:57:22 +01:00
|
|
|
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_ &&
|
2010-02-28 01:35:20 +01:00
|
|
|
d->samplerate_ == other.d->samplerate_ &&
|
|
|
|
d->sampler_ == other.d->sampler_ &&
|
|
|
|
d->art_automatic_ == other.d->art_automatic_ &&
|
|
|
|
d->art_manual_ == other.d->art_manual_;
|
2009-12-24 20:16:07 +01:00
|
|
|
}
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-03-29 15:28:29 +02:00
|
|
|
void Song::SetTextFrame(const QString& id, const QString& value,
|
|
|
|
TagLib::ID3v2::Tag* tag) {
|
2010-03-29 15:15:47 +02:00
|
|
|
TagLib::ByteVector id_vector = id.toUtf8().constData();
|
|
|
|
|
|
|
|
// Remove the frame if it already exists
|
|
|
|
while (tag->frameListMap().contains(id_vector) &&
|
|
|
|
tag->frameListMap()[id_vector].size() != 0) {
|
|
|
|
tag->removeFrame(tag->frameListMap()[id_vector].front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and add a new frame
|
|
|
|
TagLib::ID3v2::TextIdentificationFrame* frame =
|
|
|
|
new TagLib::ID3v2::TextIdentificationFrame(id.toUtf8().constData(),
|
|
|
|
TagLib::String::UTF8);
|
|
|
|
frame->setText(QStringToTaglibString(value));
|
|
|
|
tag->addFrame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
TagLib::String QStringToTaglibString(const QString& s) {
|
|
|
|
return TagLib::String(s.toUtf8().constData(), TagLib::String::UTF8);
|
|
|
|
}
|
|
|
|
|
2010-01-16 17:12:47 +01:00
|
|
|
bool Song::Save() const {
|
|
|
|
if (d->filename_.isNull())
|
|
|
|
return false;
|
|
|
|
|
2010-03-29 15:15:47 +02:00
|
|
|
scoped_ptr<TagLib::FileRef> fileref(factory_->GetFileRef(d->filename_));
|
2010-02-23 15:52:22 +01:00
|
|
|
|
2010-03-29 15:15:47 +02:00
|
|
|
fileref->tag()->setTitle(QStringToTaglibString(d->title_));
|
|
|
|
fileref->tag()->setArtist(QStringToTaglibString(d->artist_));
|
|
|
|
fileref->tag()->setAlbum(QStringToTaglibString(d->album_));
|
|
|
|
fileref->tag()->setGenre(QStringToTaglibString(d->genre_));
|
|
|
|
fileref->tag()->setComment(QStringToTaglibString(d->comment_));
|
|
|
|
fileref->tag()->setYear(d->year_);
|
|
|
|
fileref->tag()->setTrack(d->track_);
|
2010-01-16 17:12:47 +01:00
|
|
|
|
2010-03-29 15:15:47 +02:00
|
|
|
if (TagLib::MPEG::File* file = dynamic_cast<TagLib::MPEG::File*>(fileref->file())) {
|
|
|
|
TagLib::ID3v2::Tag* tag = file->ID3v2Tag(true);
|
2010-03-29 15:28:29 +02:00
|
|
|
SetTextFrame("TPOS", d->disc_ <= 0 -1 ? QString() : QString::number(d->disc_), tag);
|
|
|
|
SetTextFrame("TBPM", d->bpm_ <= 0 -1 ? QString() : QString::number(d->bpm_), tag);
|
|
|
|
SetTextFrame("TCOM", d->composer_, tag);
|
|
|
|
SetTextFrame("TPE2", d->albumartist_, tag);
|
|
|
|
SetTextFrame("TCMP", d->compilation_ ? "1" : "0", tag);
|
2010-03-29 15:15:47 +02:00
|
|
|
}
|
|
|
|
else if (TagLib::Ogg::Vorbis::File* file = dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref->file())) {
|
|
|
|
TagLib::Ogg::XiphComment* tag = file->tag();
|
|
|
|
tag->addField("COMPOSER", QStringToTaglibString(d->composer_), true);
|
|
|
|
tag->addField("BPM", QStringToTaglibString(d->bpm_ <= 0 -1 ? QString() : QString::number(d->bpm_)), true);
|
|
|
|
tag->addField("DISCNUMBER", QStringToTaglibString(d->disc_ <= 0 -1 ? QString() : QString::number(d->disc_)), true);
|
|
|
|
tag->addField("COMPILATION", QStringToTaglibString(d->compilation_ ? "1" : "0"), true);
|
|
|
|
}
|
|
|
|
else if (TagLib::FLAC::File* file = dynamic_cast<TagLib::FLAC::File*>(fileref->file())) {
|
|
|
|
TagLib::Ogg::XiphComment* tag = file->xiphComment();
|
|
|
|
tag->addField("COMPOSER", QStringToTaglibString(d->composer_), true);
|
|
|
|
tag->addField("BPM", QStringToTaglibString(d->bpm_ <= 0 -1 ? QString() : QString::number(d->bpm_)), true);
|
|
|
|
tag->addField("DISCNUMBER", QStringToTaglibString(d->disc_ <= 0 -1 ? QString() : QString::number(d->disc_)), true);
|
|
|
|
tag->addField("COMPILATION", QStringToTaglibString(d->compilation_ ? "1" : "0"), true);
|
|
|
|
}
|
2010-02-23 15:52:22 +01:00
|
|
|
|
2010-03-29 15:15:47 +02:00
|
|
|
bool ret = fileref->save();
|
2010-02-10 14:15:18 +01:00
|
|
|
#ifdef Q_OS_LINUX
|
2010-01-16 18:17:00 +01:00
|
|
|
if (ret) {
|
|
|
|
// Linux: inotify doesn't seem to notice the change to the file unless we
|
|
|
|
// change the timestamps as well. (this is what touch does)
|
|
|
|
utimensat(0, QFile::encodeName(d->filename_).constData(), NULL, 0);
|
|
|
|
}
|
2010-02-10 14:15:18 +01:00
|
|
|
#endif // Q_OS_LINUX
|
2010-01-16 18:17:00 +01:00
|
|
|
|
|
|
|
return ret;
|
2010-01-16 17:12:47 +01:00
|
|
|
}
|
2010-02-28 01:35:20 +01:00
|
|
|
|
|
|
|
QImage Song::GetBestImage() const {
|
|
|
|
if (!d->image_.isNull())
|
|
|
|
return d->image_;
|
|
|
|
|
2010-03-03 15:29:53 +01:00
|
|
|
QImage art(AlbumCoverLoader::TryLoadImage(d->art_automatic_, d->art_manual_));
|
|
|
|
if (!art.isNull())
|
|
|
|
return art;
|
2010-02-28 01:35:20 +01:00
|
|
|
|
2010-02-28 02:03:13 +01:00
|
|
|
return QImage(":/nocover.png");
|
2010-02-28 01:35:20 +01:00
|
|
|
}
|