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 CoverFinder::CoverFinder(QObject *parent) :
35 _defaultCover = QImage(":/images/defaultcover.png");
36 SUFFIXES << "png" << "jpg" << "jpeg" << "bmp" << "gif";
37 NAMES << "cover" << "folder" << "album";
38 DIRS << "cover" << "folder" << ".cover" << ".folder";
41 bool CoverFinder::_find(QString path) {
43 QFileInfoList items = dir.entryInfoList(QDir::Files);
44 QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
46 foreach (QFileInfo item, items) {
47 if (SUFFIXES.contains(item.suffix().toLower())) {
49 if (NAMES.contains(item.baseName().toLower())) {
50 emit found(QImage(item.absoluteFilePath()));
55 foreach (QFileInfo item, pics) {
56 QImage i(item.absoluteFilePath());
57 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
62 foreach(QFileInfo item, dirs) {
63 if (DIRS.contains(item.fileName().toLower())) {
64 if (_find(item.absoluteFilePath())) {
69 emit found(_defaultCover);
73 QImage &CoverFinder::defaultCover() {
77 bool CoverFinder::_extract(QString file) {
79 QString suffix = info.suffix().toLower();
80 TagLib::ID3v2::Tag *tag = NULL;
82 TagLib::File *f = NULL;
83 if (suffix == "mp3") {
84 f = new TagLib::MPEG::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
85 if (f == NULL) return false;
86 tag = ((TagLib::MPEG::File*)f)->ID3v2Tag();
88 } else if (suffix == "flac") {
89 f = new TagLib::FLAC::File(QFile::encodeName(file).data(), true, TagLib::AudioProperties::Fast);
90 if (f == NULL) return false;
91 tag = ((TagLib::FLAC::File*)f)->ID3v2Tag();
94 if (todo && tag != NULL) {
95 TagLib::ID3v2::FrameList l = tag->frameList("APIC");
98 TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
100 img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
104 if (f != NULL) delete f;
108 void CoverFinder::find(QFileInfo filePath) {
109 QtConcurrent::run(this, &CoverFinder::_async_find, filePath);
112 bool CoverFinder::_async_find(QFileInfo filePath) {
113 if (!_find(filePath.absolutePath()))
114 return _extract(filePath.absoluteFilePath());