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";
41 PRIOR_NAMES << "front" << "cover";
42 UNPRIOR_NAMES << "disk" << "back" << "booklet" << "cd";
45 bool CoverFinder::_find(QString path) {
47 QFileInfoList items = dir.entryInfoList(QDir::Files);
48 QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
50 QFileInfoList appropriate_pics;
51 foreach (QFileInfo item, items) {
52 if (SUFFIXES.contains(item.suffix().toLower())) {
54 if (NAMES.contains(item.baseName().toLower())) {
55 emit found(QImage(item.absoluteFilePath()));
60 foreach (QFileInfo item, pics) {
61 QImage i(item.absoluteFilePath());
62 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
63 appropriate_pics << item;
66 if (!appropriate_pics.isEmpty()) {
67 foreach (QString name, PRIOR_NAMES) {
68 foreach (QFileInfo item, appropriate_pics) {
69 if (item.baseName().toLower().contains(name)) {
70 emit found(QImage(item.absoluteFilePath()));
75 QFileInfoList unprior;
76 foreach (QString name, UNPRIOR_NAMES) {
77 foreach (QFileInfo item, appropriate_pics) {
78 if (item.baseName().toLower().contains(name)) {
83 if (appropriate_pics.size() > unprior.size()) {
84 foreach (QFileInfo item, appropriate_pics) {
85 if (!unprior.contains(item)) {
86 emit found(QImage(item.absoluteFilePath()));
91 emit found(QImage(unprior.at(0).absoluteFilePath()));
94 foreach(QFileInfo item, dirs) {
95 if (DIRS.contains(item.fileName().toLower())) {
96 if (_find(item.absoluteFilePath())) {
104 QImage &CoverFinder::defaultCover() {
105 return _defaultCover;
108 bool CoverFinder::_extract(QString file) {
109 QFileInfo info(file);
110 QString suffix = info.suffix().toLower();
111 TagLib::ID3v2::Tag *tag = NULL;
113 TagLib::File *f = NULL;
114 if (suffix == "mp3") {
115 f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
116 if (f == NULL) return false;
117 tag = ((TagLib::MPEG::File*)f)->ID3v2Tag();
119 } else if (suffix == "flac") {
120 f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
121 if (f == NULL) return false;
122 tag = ((TagLib::FLAC::File*)f)->ID3v2Tag();
125 if (todo && tag != NULL) {
126 TagLib::ID3v2::FrameList l = tag->frameList("APIC");
129 TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
131 img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
135 if (f != NULL) delete f;
139 void CoverFinder::find(Track track) {
140 QFileInfo filePath(track.source());
141 QtConcurrent::run(this, &CoverFinder::_async_find, filePath, track.metadata().artist(), track.metadata().album());
144 bool CoverFinder::_async_find(QFileInfo filePath, QString artist, QString album) {
145 if (!_find(filePath.absolutePath()) && !_tfind(artist, album) && !_extract(filePath.absoluteFilePath())) {
146 emit found(_defaultCover);
152 bool CoverFinder::_tfind(QString artist, QString album) {
153 QString aname = artist.toLower();
154 aname.replace("/", "");
155 QString aaname = aname+" - "+album;
156 aaname.replace("/", "");
157 QString fname1 = QDir::homePath()+"/.covers/"+aaname+".jpg";
158 QString fname2 = QDir::homePath()+"/.covers/"+aname+".jpg";
159 if (QFile::exists(fname1)) {
160 emit found(QImage(fname1));
162 } else if (QFile::exists(fname2)) {
163 emit found(QImage(fname2));