+
+bool CoverFinder::_extract(QString file) {
+ QFileInfo info(file);
+ QString suffix = info.suffix().toLower();
+ TagLib::ID3v2::Tag *tag = NULL;
+ bool todo = false;
+ TagLib::File *f = NULL;
+ if (suffix == "mp3") {
+ f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
+ if (f == NULL) return false;
+ tag = ((TagLib::MPEG::File*)f)->ID3v2Tag();
+ todo = f->isValid();
+ } else if (suffix == "flac") {
+ f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
+ if (f == NULL) return false;
+ tag = ((TagLib::FLAC::File*)f)->ID3v2Tag();
+ todo = f->isValid();
+ }
+ if (todo && tag != NULL) {
+ TagLib::ID3v2::FrameList l = tag->frameList("APIC");
+ if (l.isEmpty())
+ return false;
+ TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
+ QImage img;
+ img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
+ emit found(img);
+ return true;
+ }
+ if (f != NULL) delete f;
+ return false;
+}
+
+void CoverFinder::find(Track track) {
+ QFileInfo filePath(track.source());
+ QtConcurrent::run(this, &CoverFinder::_async_find, filePath, track.metadata().artist(), track.metadata().album());
+}
+
+bool CoverFinder::_async_find(QFileInfo filePath, QString artist, QString album) {
+ if (!_find(filePath.absolutePath()) && !_tfind(artist, album) && !_extract(filePath.absoluteFilePath())) {
+ emit found(_defaultCover);
+ return false;
+ }
+ return true;
+}
+
+bool CoverFinder::_tfind(QString artist, QString album) {
+ QString aname = artist.toLower();
+ aname.replace("/", "");
+ QString aaname = aname+" - "+album;
+ aaname.replace("/", "");
+ QString fname1 = QDir::homePath()+"/.covers/"+aaname+".jpg";
+ QString fname2 = QDir::homePath()+"/.covers/"+aname+".jpg";
+ if (QFile::exists(fname1)) {
+ emit found(QImage(fname1));
+ return true;
+ } else if (QFile::exists(fname2)) {
+ emit found(QImage(fname2));
+ return true;
+ }
+ return false;
+}