2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "coverfinder.h"
25 #include <QtConcurrentRun>
30 #include <mpeg/id3v2/frames/attachedpictureframe.h>
32 using namespace SomePlayer::DataObjects;
34 CoverFinder::CoverFinder(QObject *parent) :
37 _defaultCover = QImage(":/images/defaultcover.png");
38 SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
39 NAMES << "cover" << "folder" << "album";
40 DIRS << "cover" << "folder" << ".cover" << ".folder";
43 bool CoverFinder::_find(QString path) {
45 QFileInfoList items = dir.entryInfoList(QDir::Files);
46 QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
48 foreach (QFileInfo item, items) {
49 if (SUFFIXES.contains(item.suffix().toLower())) {
51 if (NAMES.contains(item.baseName().toLower())) {
52 emit found(QImage(item.absoluteFilePath()));
57 foreach (QFileInfo item, pics) {
58 QImage i(item.absoluteFilePath());
59 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
64 foreach(QFileInfo item, dirs) {
65 if (DIRS.contains(item.fileName().toLower())) {
66 if (_find(item.absoluteFilePath())) {
74 QImage &CoverFinder::defaultCover() {
78 bool CoverFinder::_extract(QString file) {
80 QString suffix = info.suffix().toLower();
81 TagLib::ID3v2::Tag *tag = NULL;
83 TagLib::File *f = NULL;
84 if (suffix == "mp3") {
85 f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
86 if (f == NULL) return false;
87 tag = ((TagLib::MPEG::File*)f)->ID3v2Tag();
89 } else if (suffix == "flac") {
90 f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
91 if (f == NULL) return false;
92 tag = ((TagLib::FLAC::File*)f)->ID3v2Tag();
95 if (todo && tag != NULL) {
96 TagLib::ID3v2::FrameList l = tag->frameList("APIC");
99 TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
101 img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
105 if (f != NULL) delete f;
109 void CoverFinder::find(Track track) {
110 QFileInfo filePath(track.source());
111 QtConcurrent::run(this, &CoverFinder::_async_find, filePath, track.metadata().artist(), track.metadata().album());
114 bool CoverFinder::_async_find(QFileInfo filePath, QString artist, QString album) {
115 if (!_find(filePath.absolutePath()) && !_tfind(artist, album) && !_extract(filePath.absoluteFilePath())) {
116 emit found(_defaultCover);
122 bool CoverFinder::_tfind(QString artist, QString album) {
123 QString aname = artist.toLower();
124 aname.replace("/", "");
125 QString aaname = aname+" - "+album;
126 aaname.replace("/", "");
127 QString fname1 = QDir::homePath()+"/.covers/"+aaname+".jpg";
128 QString fname2 = QDir::homePath()+"/.covers/"+aname+".jpg";
129 if (QFile::exists(fname1)) {
130 emit found(QImage(fname1));
132 } else if (QFile::exists(fname2)) {
133 emit found(QImage(fname2));