Improvements in cover search
[someplayer] / src / coverfinder.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20
21 #include "coverfinder.h"
22 #include <QDebug>
23 #include <QDir>
24 #include <QFileInfo>
25 #include <QtConcurrentRun>
26 #include <mpegfile.h>
27 #include <flacfile.h>
28 #include <oggfile.h>
29 #include <id3v2tag.h>
30 #include <mpeg/id3v2/frames/attachedpictureframe.h>
31
32 using namespace SomePlayer::DataObjects;
33
34 CoverFinder::CoverFinder(QObject *parent) :
35                 QObject(parent)
36 {
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 }
42
43 bool CoverFinder::_find(QString path) {
44         QDir dir(path);
45         QFileInfoList items = dir.entryInfoList(QDir::Files);
46         QFileInfoList dirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
47         QFileInfoList pics;
48         foreach (QFileInfo item, items) {
49                 if (SUFFIXES.contains(item.suffix().toLower())) {
50                         pics << item;
51                         if (NAMES.contains(item.baseName().toLower())) {
52                                 emit found(QImage(item.absoluteFilePath()));
53                                 return true;
54                         }
55                 }
56         }
57         foreach (QFileInfo item, pics) {
58                 QImage i(item.absoluteFilePath());
59                 if (0.8 <= (i.width()*1.0/(i.height()+1.0)) <= 1.2) {
60                         emit found(i);
61                         return true;
62                 }
63         }
64         foreach(QFileInfo item, dirs) {
65                 if (DIRS.contains(item.fileName().toLower())) {
66                         if (_find(item.absoluteFilePath())) {
67                                 return true;
68                         }
69                 }
70         }
71         return false;
72 }
73
74 QImage &CoverFinder::defaultCover() {
75         return _defaultCover;
76 }
77
78 bool CoverFinder::_extract(QString file) {
79         QFileInfo info(file);
80         QString suffix = info.suffix().toLower();
81         TagLib::ID3v2::Tag *tag = NULL;
82         bool todo = false;
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();
88                 todo = f->isValid();
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();
93                 todo = f->isValid();
94         }
95         if (todo && tag != NULL) {
96                 TagLib::ID3v2::FrameList l = tag->frameList("APIC");
97                 if (l.isEmpty())
98                         return false;
99                 TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
100                 QImage img;
101                 img.loadFromData((const uchar *) pic->picture().data(), pic->picture().size());
102                 emit found(img);
103                 return true;
104         }
105         if (f != NULL) delete f;
106         return false;
107 }
108
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());
112 }
113
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);
117                 return false;
118         }
119         return true;
120 }
121
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));
131                 return true;
132         } else if (QFile::exists(fname2)) {
133                 emit found(QImage(fname2));
134                 return true;
135         }
136         return false;
137 }